-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresizable_widget.py
459 lines (404 loc) · 15 KB
/
resizable_widget.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ColorProperty
from kivy.metrics import dp
from kivy.core.window import Window
kv = """
<ResizeSelect>:
canvas.before:
# TOP LINE
Color:
rgba: root.top_color
Line:
width: dp(1)
points: (self.x + dp(7), self.top, self.right - dp(7), self.top) # Top line
cap: 'round'
joint: 'round'
dash_offset: 2
dash_length: 10
close: False
# BOTTOM LINE
Color:
rgba: root.bottom_color
Line:
width: dp(1)
points: (self.x + dp(7), self.y, self.right - dp(7), self.y) # Bottom
cap: 'round'
joint: 'round'
dash_offset: 2
dash_length: 10
close: False
# LEFT LINE
Color:
rgba: root.left_color
Line:
width: dp(1)
points: (self.x, self.y+dp(7), self.x, self.top - dp(7)) # Left
cap: 'round'
joint: 'round'
dash_offset: 2
dash_length: 10
close: False
# RIGHT LINE
Color:
rgba: root.right_color
Line:
width: dp(1)
points: (self.right, self.y + dp(7), self.right, self.top - dp(7)) # Right
cap: 'round'
joint: 'round'
dash_offset: 2
dash_length: 10
close: False
# Upper left rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.x, self.top - dp(7)
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.x, self.top - dp(7), \
self.x + dp(7), self.top - dp(7), self.x + dp(7), self.top, \
self.x, self.top, \
self.x, self.top - dp(7)) # Horizontal
cap: 'round'
joint: 'round'
close: False
# Bottom left rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.x, self.y
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.x, self.y, \
self.x + dp(7), self.y, self.x + dp(7), self.y + dp(7), \
self.x, self.y + dp(7), \
self.x, self.y) # Vertical
cap: 'round'
joint: 'round'
close: True
# Bottom right rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.right - dp(7), self.y
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.right - dp(7), self.y, \
self.right - dp(7), self.y + dp(7), self.right, self.y + dp(7), \
self.right, self.y, \
self.right - dp(7), self.y) # Vertical
cap: 'round'
joint: 'round'
close: True
# Upper right rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.right - dp(7), self.top - dp(7)
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.right - dp(7), self.top - dp(7), \
self.right - dp(7), self.top, self.right, self.top, \
self.right, self.top - dp(7), \
self.right - dp(7), self.top - dp(7)) # Horizontal
cap: 'round'
joint: 'round'
close: True
# Upper edge rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.x + self.width/2 - dp(3.5), self.top - dp(7)
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.x + self.width/2 - dp(3.5), self.top - dp(7), \
self.x + self.width/2 + dp(3.5), self.top - dp(7), self.x + self.width/2 + dp(3.5), self.top, \
self.x + self.width/2 - dp(3.5), self.top, \
self.x + self.width/2 - dp(3.5), self.top - dp(7)) # Horizontal
cap: 'round'
joint: 'round'
close: True
# Left edge rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.x, self.y + self.height/2 - dp(3.5)
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.x, self.y + self.height/2 - dp(3.5), \
self.x + dp(7), self.y + self.height/2 - dp(3.5), \
self.x + dp(7), self.y + self.height/2 + dp(3.5), \
self.x, self.y + self.height/2 + dp(3.5), \
self.x, self.y + self.height/2 - dp(3.5)) # Vertical
cap: 'round'
joint: 'round'
close: True
# Right edge rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.right - dp(7), self.y + self.height/2 - dp(3.5)
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.right - dp(7), self.y + self.height/2 - dp(3.5), \
self.right, self.y + self.height/2 - dp(3.5), \
self.right, self.y + self.height/2 + dp(3.5), \
self.right - dp(7), self.y + self.height/2 + dp(3.5), \
self.right - dp(7), self.y + self.height/2 - dp(3.5)) # Vertical
cap: 'round'
joint: 'round'
close: True
# Bottom edge rectangle
Color:
rgba: 62/255, 254/255, 1, 1
Rectangle:
pos: self.x + self.width/2 - dp(3.5), self.y
size: dp(7), dp(7)
Color:
rgba: 1,1,1,1
Line:
width: dp(1)
points: ( \
self.x + self.width/2 - dp(3.5), self.y, \
self.x + self.width/2 + dp(3.5), self.y, \
self.x + self.width/2 + dp(3.5), self.y + dp(7), \
self.x + self.width/2 - dp(3.5), self.y + dp(7), \
self.x + self.width/2 - dp(3.5), self.y) # Horizontal
cap: 'round'
joint: 'round'
close: True
RelativeLayout:
ResizeSelect:
size_hint: None, None
size: dp(300),dp(300)
pos: 20, 40
<DashedLabel@Label>:
canvas:
Color:
rgb: 1, 1, 1
Line:
dash_offset: 2
dash_length: 10
rectangle: (*self.pos, *self.size)
"""
class AbstractClass:
pos = (0, 0)
class ResizeSelect(FloatLayout):
top_color = ColorProperty("red")
bottom_color = ColorProperty("red")
left_color = ColorProperty("red")
right_color = ColorProperty("red")
highlight_color = ColorProperty("red")
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.selected_side = None
Window.bind(mouse_pos=self.on_mouse_pos)
def on_mouse_pos(self, something, touch):
"""
When the mouse moves, we check the position of the mouse
and update the cursor accordingly.
"""
if self.collide_point(touch[0], touch[1]):
collision = self.collides_with_control_points(something, touch)
if collision in ["top left", "bottom right"]:
Window.set_system_cursor("size_nwse")
elif collision in ["top right", "bottom left"]:
Window.set_system_cursor("size_nesw")
elif collision in ["top", "bottom"]:
Window.set_system_cursor("size_ns")
elif collision in ["left", "right"]:
Window.set_system_cursor("size_we")
else:
Window.set_system_cursor("size_all")
else:
Window.set_system_cursor("arrow")
def collides_with_control_points(self, _, touch):
"""
Returns True if the mouse is over a control point.
"""
x, y = touch[0], touch[1]
# Checking mouse is on left edge
if self.x <= x <= self.x + dp(7):
if self.y <= y <= self.y + dp(7):
return "bottom left"
elif self.y + dp(7) <= y <= self.y + self.height - dp(7):
return "left"
else:
return "top left"
# Checking mouse is on top edge
elif self.x + dp(7) <= x <= self.x + self.width - dp(7):
if self.y <= y <= self.y + dp(7):
return "bottom"
elif self.y + self.height - dp(7) <= y <= self.y + self.height:
return "top"
else:
return False
# Checking mouse is on right edge
elif self.x + self.width - dp(7) <= x <= self.x + self.width:
if self.y <= y <= self.y + dp(7):
return "bottom right"
elif self.y + dp(7) <= y <= self.y + self.height - dp(7):
return "right"
else:
return "top right"
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
touch.grab(self)
x, y = touch.pos[0], touch.pos[1]
collision = self.collides_with_control_points("", touch.pos)
if collision == "top":
self.top_color = self.highlight_color
self.selected_side = "top"
elif collision == "bottom":
self.bottom_color = self.highlight_color
self.selected_side = "bottom"
elif collision == "left":
self.left_color = self.highlight_color
self.selected_side = "left"
elif collision == "right":
self.right_color = self.highlight_color
self.selected_side = "right"
else:
if collision == "top left":
self.selected_side = "top left"
self.top_color = self.highlight_color
self.left_color = self.highlight_color
elif collision == "bottom right":
self.selected_side = "bottom right"
self.bottom_color = self.highlight_color
self.right_color = self.highlight_color
elif collision == "top right":
self.selected_side = "top right"
self.top_color = self.highlight_color
self.right_color = self.highlight_color
elif collision == "bottom left":
self.selected_side = "bottom left"
self.bottom_color = self.highlight_color
self.left_color = self.highlight_color
else:
self.selected_side = None
self.top_color = self.highlight_color
self.bottom_color = self.highlight_color
self.left_color = self.highlight_color
self.right_color = self.highlight_color
return super().on_touch_down(touch)
def on_touch_move(self, touch):
MINIMUM_HEIGHT = 50.0
MINIMUM_WIDTH = 50.0
if touch.grab_current is self:
x, y = self.to_window(*self.pos)
top = y + self.height # top of our widget
right = x + self.width # right of our widget
if x < self.parent.x or right > self.parent.right:
print("X out of bounds")
if y < self.parent.y or top > self.parent.top:
print("y out of bounds")
if self.selected_side == "top":
if self.height + touch.dy <= MINIMUM_HEIGHT:
return False
self.height += touch.dy
elif self.selected_side == "bottom":
if self.height - touch.dy <= MINIMUM_HEIGHT:
return False
self.height -= touch.dy
self.y += touch.dy
elif self.selected_side == "left":
if self.width - touch.dx <= MINIMUM_WIDTH:
return False
self.width -= touch.dx
self.x += touch.dx
elif self.selected_side == "right":
if self.width + touch.dx <= MINIMUM_WIDTH:
return False
self.width += touch.dx
elif self.selected_side == "top left":
if touch.dx > 0:
if self.width - touch.dx <= MINIMUM_WIDTH:
return False
if touch.dy < 0:
if self.height + touch.dy <= MINIMUM_HEIGHT:
return False
self.width -= touch.dx
self.x += touch.dx # OK
self.height += touch.dy
elif self.selected_side == "top right":
if touch.dx < 0:
if self.width + touch.dx <= MINIMUM_WIDTH:
return False
if touch.dy < 0:
if self.height + touch.dy <= MINIMUM_HEIGHT:
return False
self.width += touch.dx
self.height += touch.dy
elif self.selected_side == "bottom left":
if touch.dx > 0:
if self.width - touch.dx <= MINIMUM_WIDTH:
return False
if touch.dy > 0:
if self.height - touch.dy <= MINIMUM_HEIGHT:
return False
self.width -= touch.dx # OK
self.x += touch.dx
self.height -= touch.dy
self.y += touch.dy
elif self.selected_side == "bottom right":
if touch.dx < 0:
if self.width + touch.dx <= MINIMUM_WIDTH:
return False
if touch.dy > 0:
if self.height - touch.dy <= MINIMUM_HEIGHT:
return False
self.width += touch.dx
self.height -= touch.dy
self.y += touch.dy
elif not self.selected_side:
print("No side selected: ")
self.x += touch.dx
self.y += touch.dy
return super().on_touch_move(touch)
def on_touch_up(self, touch):
if touch.grab_current is self:
touch.ungrab(self)
self.bottom_color = (
self.top_color
) = self.left_color = self.right_color = "white"
return True
return super().on_touch_up(touch)
Builder.load_string(kv)
class ResizeWidgetApp(App):
def build(self):
return Builder.load_string(kv)
if __name__ == "__main__":
ResizeWidgetApp().run()