generated from kivy-garden/flower
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
323 lines (280 loc) · 9.92 KB
/
__init__.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
'''A round menu that appears on a long touch
'''
from __future__ import print_function
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.animation import Animation
from kivy.properties import (
NumericProperty, ListProperty, ObjectProperty, DictProperty)
from kivy.app import App
from functools import partial
from copy import copy
from math import pi
KV = '''
#:import pi math.pi
#:import cos math.cos
#:import sin math.sin
#:import V kivy.vector.Vector
<ModernMenu>:
canvas.before:
Color:
rgba: root.cancel_color
Ellipse:
pos: self.center_x - self.radius, self.center_y - self.radius
size: self.radius * 2, self.radius * 2
angle_start: 0
angle_end: self.circle_progress * 360 * self.creation_direction
Color:
rgba: self.color
Line:
circle:
(
self.center_x, self.center_y,
self.radius, 0,
self.circle_progress * 360 * self.creation_direction
)
width: self.line_width
on_touch_down:
V(args[1].pos).distance(self.center) < self.radius and (
self.back() if self.choices_history else self.dismiss())
<ModernMenuLabel>:
size: self.texture_size
padding: 5, 5
halign: 'center'
valign: 'middle'
on_press: self.callback and self.callback(self)
canvas.before:
Color:
rgba: self.bg_color
Rectangle:
pos: self.pos
size: self.size
Line:
points:
(
self.center_x, self.center_y,
self.parent.center_x + cos(
self.start_angle +
self.opacity * self.index_adj * self.angle / self.siblings_adj
) * self.parent.radius,
self.parent.center_y + sin(
self.start_angle +
self.opacity * self.index_adj * self.angle / self.siblings_adj
) * self.parent.radius
) if self.parent else []
width: self.parent.line_width if self.parent else 1
center:
(
self.parent.center_x +
cos(self.start_angle +
self.opacity * self.index_adj * self.angle / self.siblings_adj
) * self.radius,
self.parent.center_y +
sin(self.start_angle +
self.opacity * self.index_adj * self.angle / self.siblings_adj
) * self.radius
) if (self.size and self.parent and self.parent.children) else (0, 0)
'''
def squared_dist(p1, p2):
(x1, y1) = p1
(x2, y2) = p2
return ((x1 - x2) ** 2 + (y1 - y2) ** 2)
class ModernMenuLabel(ButtonBehavior, Label):
index = NumericProperty(0)
radius = NumericProperty(100)
siblings = NumericProperty(1)
callback = ObjectProperty(None)
bg_color = ListProperty([.1, .4, .4, .9])
def calculate_angle(self, *args):
if self.parent is None:
return
factor = 2.0
self.start_angle = 0
left = top = False
if self.parent.center_x < self.radius:
factor /= 2
self.start_angle = 1.5 * pi
left = True
if self.parent.center_y < self.radius:
factor /= 2
self.start_angle = 0
if self.parent.parent:
if self.parent.center_y + self.radius > self.parent.parent.height:
factor /= 2
self.start_angle = 1.5 * pi if left else pi
top = True
if self.parent.center_x + self.radius > self.parent.parent.width:
factor /= 2
self.start_angle = pi if top else pi / 2
self.angle = factor * pi
# index adjustment: if 1, the items will spread the whole angle,
# which is what we want if angle is < 2 * pi
# if angle is 2 * pi, first item would be at the
# same location as the last one, so we set 0
idx_adj = 0 if factor == 2 else 1
self.index_adj = self.index - idx_adj
self.siblings_adj = max(1, self.siblings - idx_adj)
def on_parent(self, *args):
if self.parent:
self.parent.bind(children=self.update_siblings,
center=self.calculate_angle)
if self.parent.parent:
self.parent.parent.bind(size=self.calculate_angle)
self.calculate_angle()
def update_siblings(self, *args):
if self.parent:
self.siblings = max(1, len(self.parent.children))
else:
self.siblings = 1
self.calculate_angle()
class ModernMenu(Widget):
radius = NumericProperty(50)
circle_width = NumericProperty(5)
line_width = NumericProperty(2)
color = ListProperty([.3, .3, .3, 1])
circle_progress = NumericProperty(0)
creation_direction = NumericProperty(1)
creation_timeout = NumericProperty(1)
choices = ListProperty([])
item_cls = ObjectProperty(ModernMenuLabel)
item_args = DictProperty({'opacity': 0})
animation = ObjectProperty(Animation(opacity=1, d=.5))
choices_history = ListProperty([])
cancel_color = ListProperty([1, 0, 0, .4])
def start_display(self, touch):
touch.grab(self)
a = Animation(circle_progress=1, d=self.creation_timeout)
a.bind(on_complete=self.open_menu)
touch.ud['animation'] = a
a.start(self)
def open_menu(self, *args):
self.clear_widgets()
for i in self.choices:
kwargs = copy(self.item_args)
kwargs.update(i)
ml = self.item_cls(**kwargs)
self.animation.start(ml)
self.add_widget(ml)
def open_submenu(self, choices, *args):
self.choices_history.append(self.choices)
self.choices = choices
self.open_menu()
def back(self, *args):
self.choices = self.choices_history.pop()
self.open_menu()
def on_touch_move(self, touch, *args):
if (
touch.grab_current == self and
squared_dist(touch.pos, touch.opos) > self.radius ** 2 and
self.parent and
self.circle_progress < 1
):
self.parent.remove_widget(self)
return super(ModernMenu, self).on_touch_move(touch, *args)
def on_touch_up(self, touch, *args):
if (
touch.grab_current == self and
self.parent and
self.circle_progress < 1
):
self.parent.remove_widget(self)
return super(ModernMenu, self).on_touch_up(touch, *args)
def dismiss(self):
a = Animation(opacity=0)
a.bind(on_complete=self._remove)
a.start(self)
def _remove(self, *args):
if self.parent:
self.parent.remove_widget(self)
class MenuSpawner(Widget):
timeout = NumericProperty(0.1)
menu_cls = ObjectProperty(ModernMenu)
cancel_distance = NumericProperty(10)
menu_args = DictProperty({})
def on_touch_down(self, touch, *args):
t = partial(self.display_menu, touch)
touch.ud['menu_timeout'] = t
Clock.schedule_once(t, self.timeout)
return super(MenuSpawner, self).on_touch_down(touch, *args)
def on_touch_move(self, touch, *args):
if (
touch.ud['menu_timeout'] and
squared_dist(touch.pos, touch.opos) > self.cancel_distance ** 2
):
Clock.unschedule(touch.ud['menu_timeout'])
return super(MenuSpawner, self).on_touch_move(touch, *args)
def on_touch_up(self, touch, *args):
if touch.ud.get('menu_timeout'):
Clock.unschedule(touch.ud['menu_timeout'])
return super(MenuSpawner, self).on_touch_up(touch, *args)
def display_menu(self, touch, dt):
menu = self.menu_cls(center=touch.pos, **self.menu_args)
self.add_widget(menu)
menu.start_display(touch)
Builder.load_string(KV)
TESTAPP_KV = '''
FloatLayout:
ScrollView:
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: 1000
Button:
text: 'test1'
Label:
text: 'label1'
Button:
text: 'test2'
Label:
text: 'label2'
Button:
text: 'test3'
Label:
text: 'label3'
MenuSpawner:
timeout: .8
menu_args:
dict(
creation_direction=-1,
radius=30,
creation_timeout=.4,
choices=[
dict(text='submenu 1', index=1, callback=app.callback1),
dict(text='action 1', index=2, callback=app.callback2),
dict(text='action 2', index=3, callback=app.callback3),
dict(text='submenu 2', index=4, callback=app.callback4),
dict(text='action 3', index=5, callback=app.callback5),
])
'''
class ModernMenuApp(App):
def build(self):
return Builder.load_string(TESTAPP_KV)
def callback1(self, *args):
print("test 1")
args[0].parent.open_submenu(
choices=[
dict(text='action 1', index=1, callback=self.callback2),
dict(text='action 2', index=2, callback=self.callback2),
dict(text='action 3', index=3, callback=self.callback2),
])
def callback2(self, *args):
print("test 2")
args[0].parent.dismiss()
def callback3(self, *args):
print("test 3")
args[0].parent.dismiss()
def callback4(self, *args):
print("test 4")
args[0].parent.open_submenu(
choices=[
dict(text='hey', index=1, callback=self.callback2),
dict(text='oh', index=2, callback=self.callback2),
])
def callback5(self, *args):
print("test 5")
args[0].parent.dismiss()
if __name__ == '__main__':
ModernMenuApp().run()