-
Notifications
You must be signed in to change notification settings - Fork 20
/
library_widgets.py
520 lines (404 loc) · 17.6 KB
/
library_widgets.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# generic widgets should be placed here
import os
from kivy import Logger, platform
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.factory import Factory
from kivy.event import EventDispatcher
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.modalview import ModalView
from kivy.uix.popup import Popup
from kivy.uix.image import Image, AsyncImage
from kivy.properties import StringProperty, ListProperty, OptionProperty, NumericProperty, BooleanProperty
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.clock import Clock
from utils import import_kv
import_kv(__file__)
class AnimationMixin(EventDispatcher):
animation = ObjectProperty()
def inner_animation_completed_handler(self, *_):
self.dispatch('on_animation_completed')
pass
def on_animation_completed(self):
pass
def on_animation(self, _, value):
value.bind(on_complete=self.inner_animation_completed_handler)
def start_animation(self):
self.animation.cancel(self)
self.animation.start(self)
def __init__(self, **kwargs):
super(AnimationMixin, self).__init__(**kwargs)
self.register_event_type('on_animation_completed')
Factory.register('AnimationMixin', AnimationMixin)
class OnPressAnimationMixin(ButtonBehavior, AnimationMixin):
def __init__(self, **kwargs):
super(OnPressAnimationMixin, self).__init__(**kwargs)
self.bind(on_press=self._on_press)
def _on_press(self, *_):
self.animation.start(self)
class OnPressBlinkMixin(OnPressAnimationMixin):
pass
Factory.register('OnPressBlinkMixin', OnPressBlinkMixin)
class ImageButton(ButtonBehavior, AsyncImage):
name = StringProperty()
def on_press(self):
App.get_running_app().sounds['tap'].play()
class TitledMixin(EventDispatcher):
title = StringProperty("")
orientation = OptionProperty("upper", options=["upper", "lower", "right", "left"])
shift = NumericProperty(0.5)
label = ObjectProperty()
def layout_label(self, instance, _):
if self.orientation in ["upper", "lower"]:
y_shift = instance.height * self.shift + self.label.font_size
if instance.orientation == "lower":
y_shift = -y_shift
instance.label.center = (instance.center[0], instance.center[1] + y_shift)
if self.orientation in ["right", "left"]:
x_shift = instance.width * self.shift + self.label.font_size
if instance.orientation == "left":
x_shift = -x_shift
instance.label.center = (instance.center[0] + x_shift, instance.center[1])
def __init__(self, **kwargs):
super(TitledMixin, self).__init__(**kwargs)
self.bind(size=self.layout_label,
pos=self.layout_label)
class CircleDiagram(FloatLayout):
part = NumericProperty(0)
inner_part = NumericProperty()
label = ObjectProperty()
color = ListProperty()
COEFFICIENT = 0.8
def _draw_circle(self, d, part):
from kivy.graphics import Ellipse
pos = (self.pos[0] + self.size[0] / 2 - d / 2,
self.pos[1] + self.size[1] / 2 - d / 2)
Ellipse(size=(d, d), pos=pos, angle_start=0, angle_end=part * 360, segments=45)
def on_resize(self, instance, value):
from kivy.graphics import Color
self.canvas.before.clear()
if self.inner_part > 0:
with self.canvas.before:
Color(*self.color)
d = min(instance.size)
for _ in range(int(self.inner_part)):
self._draw_circle(d, 1)
d *= self.COEFFICIENT
self._draw_circle(d, self.inner_part - int(self.inner_part))
def on_inner_part(self, instance, value):
instance.label.text = "%.0f %%" % (value * 100)
def on_part(self, instance, value):
Animation(inner_part=value, t=self.transition, d=self.duration).start(self)
def __init__(self, **kwargs):
super(CircleDiagram, self).__init__(**kwargs)
self.bind(size=self.on_resize,
pos=self.on_resize,
inner_part=self.on_resize)
class ColorSquare(Widget):
RADIUS = .1
background_color = ListProperty()
def fill_canvas(self):
self.canvas.before.clear()
from kivy.graphics.vertex_instructions import Rectangle, Ellipse
from kivy.graphics.context_instructions import Color
with self.canvas:
self._color = Color(*self.background_color)
self.rectangles = [Rectangle(), Rectangle(), Rectangle(), Rectangle(), Rectangle()]
self.ellipses = [Ellipse(angle_start=270, angle_end=0, segments=10),
Ellipse(angle_start=0, angle_end=90, segments=10),
Ellipse(angle_start=90, angle_end=180, segments=10),
Ellipse(angle_start=180, angle_end=270, segments=10)]
def relocate(self, *args, **kwargs):
if not self.rectangles:
return
edge = min(self.size)
radius = edge * self.RADIUS
self.rectangles[0].pos = (self.center[0] - edge / 2 + radius, self.center[1] - edge / 2 + radius)
self.rectangles[0].size = (edge - 2 * radius, edge - 2 * radius)
self.rectangles[1].pos = (self.center[0] - edge / 2 + radius, self.center[1] - edge / 2)
self.rectangles[1].size = (edge - 2 * radius, radius)
self.rectangles[2].pos = (self.center[0] - edge / 2 + radius, self.center[1] + edge / 2 - radius)
self.rectangles[2].size = (edge - 2 * radius, radius)
self.rectangles[3].pos = (self.center[0] - edge / 2, self.center[1] - edge / 2 + radius)
self.rectangles[3].size = (radius, edge - 2 * radius)
self.rectangles[4].pos = (self.center[0] + edge / 2 - radius, self.center[1] - edge / 2 + radius)
self.rectangles[4].size = (radius, edge - 2 * radius)
self.ellipses[0].pos = (self.center[0] - edge / 2, self.center[1] + edge / 2 - radius * 2)
self.ellipses[0].size = (radius * 2, radius * 2)
self.ellipses[1].pos = (
self.center[0] + edge / 2 - radius * 2, self.center[1] + edge / 2 - radius * 2)
self.ellipses[1].size = (radius * 2, radius * 2)
self.ellipses[2].pos = (self.center[0] + edge / 2 - radius * 2, self.center[1] - edge / 2)
self.ellipses[2].size = (radius * 2, radius * 2)
self.ellipses[3].pos = (self.center[0] - edge / 2, self.center[1] - edge / 2)
self.ellipses[3].size = (radius * 2, radius * 2)
def on_background_color(self, *args, **kwargs):
if self._color:
self._color.rgba = self.background_color
on_size = on_pos = relocate
def __init__(self, **kwargs):
self.rectangles = []
self.ellipses = []
self._color = None
super(ColorSquare, self).__init__(**kwargs)
self.fill_canvas()
class MarkRedMixin(Widget):
"""
Utility class for marking the widget borders
"""
pass
class TrackingScreenMixin(object):
def on_enter(self, *args):
from kivy.app import App
tracker = App.get_running_app().tracker
tracker.send_screen(self)
def on_pre_leave(self, *args):
from kivy.app import App
tracker = App.get_running_app().tracker
tracker.clear_screen()
class PngAnimator(Image):
sources = ListProperty()
current_index = NumericProperty(0)
playing = BooleanProperty(False)
def reset(self):
self.stop()
self.current_index = 0
self.start()
def on_sources(self, instance, value):
self.source = self.sources[0]
def on_repeat(self, *args, **kwargs):
pass
def on_current_index(self, *args):
self.source = self.sources[self.current_index]
def next_source(self, _):
if self.playing:
if self.current_index == len(self.sources) - 1:
self.dispatch('on_repeat')
self.current_index = (self.current_index + 1) % len(self.sources)
def start(self):
Clock.unschedule(self.next_source)
Clock.schedule_interval(self.next_source, timeout=1.)
self.playing = True
def stop(self):
Clock.unschedule(self.next_source)
self.playing = False
def __init__(self, **kwargs):
self.register_event_type('on_repeat')
super(PngAnimator, self).__init__(**kwargs)
class OkPopup(Popup):
text = StringProperty()
ok_callback = ObjectProperty(None, allownone=True)
class OkCancelPopup(Popup):
text = StringProperty()
ok_callback = ObjectProperty(None, allownone=True)
cancel_callback = ObjectProperty(None, allownone=True)
class CrashPopup(Popup):
text = StringProperty()
ok_callback = ObjectProperty(None, allownone=True)
class LoadingWidget(BoxLayout):
icon = StringProperty()
def _detach(self, *args, **kwargs):
self.parent.remove_widget(self)
def hide(self, content):
content.opacity = 0
self.parent.add_widget(content)
animation = Animation(opacity=1, d=.5)
animation.start(content)
animation = Animation(opacity=0, d=.5)
animation.bind(on_complete=self._detach)
animation.start(self)
class TutorialAnimator(PngAnimator):
task_key = StringProperty()
def on_task_key(self, instance, value):
path = "data/img/task_tutorials/%s" % self.task_key
paths = [os.path.join(path, f) for f in os.listdir(path)]
paths.sort()
self.sources = paths
class DidYouKnowLabel(Label):
texts = None
def _set_text(self):
from random import choice
text = choice(DidYouKnowLabel.texts)
self.text = text['text']
self.on_ref_press = text['action']
@staticmethod
def to_purchases_screen(*args, **kwargs):
App.get_running_app().manager.to_screen('purchases')
def mail(self, ref, *args, **kwargs):
try:
import webbrowser
from utils import _ # noqa F401 looks like i18n on mail subject wouldn't work without this import
webbrowser.open("mailto:[email protected]?subject=%s" % ref)
except Exception:
Logger.error("Browser: could not start mailto...")
def facebook(self, *args, **kwargs):
try:
import webbrowser
import settings
webbrowser.open(settings.FACEBOOK_PAGE_URL)
except Exception:
Logger.error("Browser: could not start facebook...")
def beta_testers_group(self, *args, **kwargs):
try:
import webbrowser
import settings
webbrowser.open(settings.BETA_TESTERS_GROUP_URL)
except Exception:
Logger.error("Browser: could not start google group...")
def settings(self, *args, **kwargs):
App.get_running_app().open_settings()
def __init__(self, **kwargs):
super(DidYouKnowLabel, self).__init__(**kwargs)
from utils import _
import settings
if not DidYouKnowLabel.texts:
DidYouKnowLabel.texts = [
{
"text": _(
u"Found a bug? Let me know: "
u"[ref=Kognitivo: I found a bug][b][font=glyphicons]\uE127[/font] [email protected][/b][/ref]"),
"action": self.mail
},
{
"text": _(u"Got a feature idea? "
u"Share it on [ref=feature][b][font=glyphicons]\uE127[/font] FACEBOOK[/b][/ref]"),
"action": self.facebook
},
{
"text": _(u"Found a misspelled word? Let me know: "
u"[ref=Kognitivo: Grammar][b][font=glyphicons]\uE127[/font] [email protected][/b][/ref]"),
"action": self.mail
},
{
"text": _(
u"Change font size in [ref=font][b][font=glyphicons]\uE127[/font]settings[/b][/ref] "
u"if it is too big or to small"),
"action": self.settings
},
{
"text": _(
u"Want most recent features? Become a"
u" [ref=tester][b][font=glyphicons]\uE127[/font]tester[/b][/ref]"),
"action": self.beta_testers_group
},
{
"text": _(
u"The more you practice, the more accurate is the statistics"),
"action": None
},
]
if platform == 'android' and not settings.PROFILE:
DidYouKnowLabel.texts += [
{
"text": _(u"Need more tasks? "
u"Take a look [ref=purchase][b][font=glyphicons]\uE127[/font] HERE[/b][/ref]"),
"action": self.to_purchases_screen
},
{
"text": _(u"Try 7 days of full features for free with "
u"[ref=premium_subscription][b][font=glyphicons]"
u"\uE127[/font]premium subscription[/b][/ref]"),
"action": self.to_purchases_screen
},
{
"text": _(u"Connect your [ref=google][b][font=glyphicons]\uE127[/font]Google account[/b][/ref] "
u"in order to compete with friends"),
"action": self.settings
},
]
self._set_text()
class TutorialContentWidget(ModalView):
point_widget = ObjectProperty()
label = ObjectProperty()
def on_touch_down(self, touch):
App.get_running_app().sounds['success'].play()
self.dismiss()
return True
class TutorialMixin(Widget):
tutorial_text = StringProperty()
_tutorial_content = ObjectProperty()
tutorial_position = ObjectProperty()
tutorial_size = ListProperty()
point_screen = StringProperty('activity')
def __init__(self, **kwargs):
self.register_event_type('on_tutorial_start')
self.register_event_type('on_tutorial_finish')
super(TutorialMixin, self).__init__(**kwargs)
self.window = None
self.screen = None
self._background_color = None
self._next_widgets = None
self._left_rectangle = None
self._right_rectangle = None
self._bottom_rectangle = None
self._top_rectangle = None
self._tutorial_stopped = False
def _to_next_tutorial(self, *args):
if self._next_widgets:
next_widget = self._next_widgets.pop(0)
next_widget.show_tutorial(self._next_widgets)
def stop_tutorial(self):
if self._next_widgets:
del self._next_widgets[:]
self._tutorial_stopped = True
def hide_tutorial(self, *args):
animation = Animation(rgba=(1, 1, 1, 0), d=.5, t='in_out_cubic')
animation.start(self._background_color)
animation = Animation(color=(1, 1, 1, 0), d=.5, t='in_out_cubic')
if self._next_widgets:
animation.bind(on_complete=self._to_next_tutorial)
else:
if not self._tutorial_stopped:
self.dispatch('on_tutorial_finish')
animation.start(self._tutorial_content.label)
self.unbind(size=self._place_rectangles, pos=self._place_rectangles)
def on_tutorial_start(self):
pass
def on_tutorial_finish(self):
pass
def _place_rectangles(self, *args):
self._left_rectangle.size = (self.x, self.window.height)
self._right_rectangle.pos = (self.x + self.width, 0)
self._right_rectangle.size = (
self.window.width - self.x - self.width,
self.window.height
)
self._bottom_rectangle.pos = (self.x, 0)
self._bottom_rectangle.size = (self.width, self.y)
self._top_rectangle.pos = (self.x, self.y + self.height)
self._top_rectangle.size = (self.width, self.y + self.height)
def show_tutorial(self, next_widgets=None):
self.screen = App.get_running_app().manager.current_screen
if self.screen and not self.screen.name == self.point_screen:
self.stop_tutorial()
return
self.dispatch('on_tutorial_start')
self._next_widgets = next_widgets
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.animation import Animation
self.window = App.get_running_app().root_window
with self.window.canvas:
self._background_color = Color(1, 1, 1, 0)
self._left_rectangle = Rectangle()
self._right_rectangle = Rectangle()
self._bottom_rectangle = Rectangle()
self._top_rectangle = Rectangle()
self._place_rectangles()
animation = Animation(rgba=(1, 1, 1, 1), d=.25, t='in_out_cubic')
animation.bind(on_complete=self._show_tutorial_content)
animation.start(self._background_color)
self.bind(size=self._place_rectangles, pos=self._place_rectangles)
def _show_tutorial_content(self, *args):
self._tutorial_content = TutorialContentWidget(
point_widget=self,
)
self._tutorial_content.bind(on_dismiss=self.hide_tutorial)
self._tutorial_content.open()
import settings
animation = Animation(color=settings.TEXT_COLOR, d=.25, t='in_out_cubic')
animation.start(self._tutorial_content.label)