summaryrefslogtreecommitdiff
path: root/normal.py
blob: f63d2beda637b072349904d508286ecac79d93fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from press import press
from Xlib import X
from evdev import ecodes

from clipboard import copy
from constants import KEYSYM_MAP, NORMAL, VIM, SAVE_OBJECT, OBJECT, SAVE_STYLE, STYLE, DISABLED

from mode import mode
from vim import open_vim
import styles
import disabled

pressed = set()
shift = False

def normal_mode(event, keysym, manager):
    global shift
    if event.state & X.ControlMask:
        # there are modifiers
        # eg. X.ControlMask
        # ~or X.ShiftMask~
        return

    if keysym in KEYSYM_MAP:
        character = KEYSYM_MAP.get(keysym, 0)

        if event.type == X.KeyPress:
            if event.state & X.ShiftMask:
                shift = True

            pressed.add(character)

        elif event.type == X.KeyRelease:

            if 'ESC' in pressed:
                press(ecodes.KEY_F1)
                pressed.clear()

            if character in pressed:
                if len(pressed) >= 2:
                    fire(pressed)
                else:
                    key = pressed.pop()
                    if key == 'w':
                        press(ecodes.KEY_F6) # pencil
                    if key == 'e':
                        press(ecodes.KEY_F5) # ellipse
                    if key == 'r':
                        press(ecodes.KEY_F4) # rectangle
                    if key == 't':
                        manager.teardown()
                        mode(VIM)
                        compile_latex = shift # shift-t compiles latex
                        open_vim(compile_latex)
                        mode(NORMAL)
                        manager.listen(normal_mode)
                    if key == 'y':
                        press(ecodes.KEY_F8) #text
                    
                    if key == '`':
                        manager.teardown()
                        mode(DISABLED)
                        manager.listen(disabled.disabled_mode)

                    if shift and key == 'a':
                        mode(SAVE_OBJECT)
                        manager.teardown()
                        styles.save_object_mode()
                        shift = False
                        mode(NORMAL)
                        manager.listen(normal_mode)
                    elif key == 'a':
                        mode(OBJECT)
                        manager.teardown()
                        manager.listen(styles.object_mode)

                    if shift and key == 's':
                        mode(SAVE_STYLE)
                        manager.teardown()
                        styles.save_style_mode()
                        shift = False
                        manager.listen(normal_mode)
                        mode(NORMAL)
                    elif key == 's':
                        mode(STYLE)
                        manager.teardown()
                        manager.listen(styles.style_mode)


                    if key == 'd':
                        press(ecodes.KEY_F7) # dropper
                    if key == 'f':
                        press(ecodes.KEY_F6, [ecodes.KEY_LEFTSHIFT]) # bezier
                    if key == 'h':
                        press(ecodes.KEY_H, [ecodes.KEY_LEFTSHIFT]) # flip

                    if key == 'z':
                        press(ecodes.KEY_DELETE) #delete

                    if key == 'x':
                        press(ecodes.KEY_5, [ecodes.KEY_LEFTSHIFT]) # snap

                    if key == 'v':
                        press(ecodes.KEY_V, [ecodes.KEY_LEFTSHIFT]) # flip

                pressed.clear()

                if shift:
                    shift = False

            if not (event.state & X.ShiftMask):
                shift = False


def fire(combination):
    pt = 1.327 # pixels
    w = 0.4 * pt
    thick_width = 0.8 * pt
    very_thick_width = 1.2 * pt

    style = {
        'stroke-opacity': 1
    }

    if {'s', 'a', 'd', 'g', 'h', 'x', 'e'} & combination:
        style['stroke'] = 'black'
        style['stroke-width'] = w
        style['marker-end'] = 'none'
        style['marker-start'] = 'none'
        style['stroke-dasharray'] = 'none'
    else:
        style['stroke'] = 'none'

    if 'g' in combination:
        w = thick_width
        style['stroke-width'] = w

    if 'h' in combination:
        w = very_thick_width
        style['stroke-width'] = w

    if 'a' in combination:
        style['marker-end'] = f'url(#marker-arrow-{w})'

    if 'x' in combination:
        style['marker-start'] = f'url(#marker-arrow-{w})'
        style['marker-end'] = f'url(#marker-arrow-{w})'

    if 'd' in combination:
        style['stroke-dasharray'] = f'{w},{2*pt}'

    if 'e' in combination:
        style['stroke-dasharray'] = f'{3*pt},{3*pt}'

    if 'f' in combination:
        style['fill'] = 'black'
        style['fill-opacity'] = 0.12
    if 'b' in combination:
        style['fill'] = 'black'
        style['fill-opacity'] = 1
    if not {'f', 'b'} & combination:
        style['fill'] = 'none'
        style['fill-opacity'] = 1


    if style['fill'] == 'none' and style['stroke'] == 'none':
        return

    svg = '''
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
'''

    if ('marker-end' in style and style['marker-end'] != 'none') or \
    ('marker-start' in style and style['marker-start'] != 'none'):
        svg += f'''
<defs id="marker-defs">
<marker
id="marker-arrow-{w}"
orient="auto-start-reverse"
refY="0" refX="0"
markerHeight="1.690" markerWidth="0.911">
  <g transform="scale({(2.40 * w + 3.87)/(4.5*w)})">
    <path
       d="M -1.55415,2.0722 C -1.42464,1.29512 0,0.1295 0.38852,0 0,-0.1295 -1.42464,-1.29512 -1.55415,-2.0722"
       style="fill:none;stroke:#000000;stroke-width:{0.6};stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
       inkscape:connector-curvature="0" />
   </g>
</marker>
</defs>
'''

    style_string = ';'.join('{}: {}'.format(key, value)
        for key, value in sorted(style.items(), key=lambda x: x[0])
    )
    svg += f'<inkscape:clipboard style="{style_string}" /></svg>'

    copy(svg, target='image/x-inkscape-svg')
    press(ecodes.KEY_V, [ecodes.KEY_LEFTSHIFT, ecodes.KEY_LEFTCTRL])