-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
298 lines (254 loc) · 10.6 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
'''
Copyright (c) 2010-2016 Kivy Team and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
'''
Most of the code if taken directly from a kivy module, I have only made some
changes.
'''
from kivy.clock import Clock
from kivy.properties import OptionProperty, ObjectProperty, NumericProperty, \
ReferenceListProperty, BooleanProperty, ListProperty, AliasProperty, \
StringProperty
from kivy.config import Config
from kivy.metrics import sp
from functools import partial
from kivy.animation import Animation
_scroll_timeout = _scroll_distance = 0
if Config:
_scroll_timeout = Config.getint('widgets', 'scroll_timeout')
_scroll_distance = Config.getint('widgets', 'scroll_distance')
class SwipeBehavior(object):
'''Swipe behavior. When combined with a widget, swiping in the rectangle
defined by :attr:`swipe_rectangle` will swipe the widget.
In this case to left or to right.
The example.py file demonstrates the use.
'''
move_to = ListProperty()
'''To move widget.
:attr:`remove_from_left` is a :class:`~kivy.properties.ListProperty`,
defaults to [None,None].
'''
remove_from_left = BooleanProperty(True)
'''Choice to remove the widget from the left or not.
:attr:`remove_from_left` is a :class:`~kivy.properties.BooleanProperty`,
defaults to True.
'''
remove_from_right = BooleanProperty(True)
'''Choice to remove the widget from the right or not.
:attr:`remove_from_right` is a :class:`~kivy.properties.BooleanProperty`,
defaults to True.
'''
opacity_reduction_rate = NumericProperty(.005)
'''Change the rate at which opacity reduces.
:attr:`opacity_reduction_rate` is a :class:
`~kivy.properties.NumericProperty`, defaults to (.005).
'''
animation_type = StringProperty('linear')
'''For setting animation type.
:attr:`animation_type` is a :class:`~kivy.properties.StringProperty`,
defaults to linear.
'''
animation_duration = NumericProperty(.5)
'''For setting animation duration.
:attr:`animation_duration` is a :class:`~kivy.properties.NumericProperty`,
defaults to (.5).
'''
right_percentage = NumericProperty(35)
'''When to delete from right side.
:attr:`right_percentage` is a :class:`~kivy.properties.NumericProperty`,
defaults to 35.
'''
left_percentage = NumericProperty(35)
'''When to delete from left side.
:attr:`left_percentage` is a :class:`~kivy.properties.NumericProperty`,
defaults to 35.
'''
swipe_distance = NumericProperty(_scroll_distance)
'''Distance to move before swiping the :class:`swipeBehavior`, in pixels.
As soon as the distance has been traveled, the :class:`swipeBehavior` will
start to swipe, and no touch event will go to children.
:attr:`swipe_distance` is a :class:`~kivy.properties.NumericProperty`,
defaults to 20 (pixels), according to the default value of scroll_distance
in user configuration.
'''
swipe_timeout = NumericProperty(_scroll_timeout)
'''Timeout allowed to trigger the :attr:`swipe_distance`, in milliseconds.
If the user has not moved :attr:`swipe_distance` within the timeout,
swiping will be disabled, and the touch event will go to the children.
:attr:`swipe_timeout` is a :class:`~kivy.properties.NumericProperty`,
defaults to 55 (milliseconds), according to the default value of
scroll_timeout in user configuration.
'''
swipe_rect_x = NumericProperty(0)
'''X position of the axis aligned bounding rectangle where swiping
is allowed. In window coordinates.
:attr:`swipe_rect_x` is a :class:`~kivy.properties.NumericProperty`,
defaults to 0.
'''
swipe_rect_y = NumericProperty(0)
'''Y position of the axis aligned bounding rectangle where swiping
is allowed. In window coordinates.
:attr:`swipe_rect_Y` is a :class:`~kivy.properties.NumericProperty`,
defaults to 0.
'''
swipe_rect_width = NumericProperty(100)
'''Width of the axis aligned bounding rectangle where swiping is allowed.
:attr:`swipe_rect_width` is a :class:`~kivy.properties.NumericProperty`,
defaults to 100.
'''
swipe_rect_height = NumericProperty(100)
'''Height of the axis aligned bounding rectangle where swiping is allowed.
:attr:`swipe_rect_height` is a :class:`~kivy.properties.NumericProperty`,
defaults to 100.
'''
swipe_rectangle = ReferenceListProperty(swipe_rect_x, swipe_rect_y,
swipe_rect_width, swipe_rect_height)
'''Position and size of the axis aligned bounding rectangle where swiping
is allowed.
:attr:`swipe_rectangle` is a :class:`~kivy.properties.ReferenceListProperty`
of (:attr:`swipe_rect_x`, :attr:`swipe_rect_y`, :attr:`swipe_rect_width`,
:attr:`swipe_rect_height`) properties.
'''
def __init__(self, **kwargs):
self._swipe_touch = None
super(SwipeBehavior, self).__init__(**kwargs)
def _get_uid(self, prefix='sv'):
return '{0}.{1}'.format(prefix, self.uid)
def animate_back(self):
'''Take the widget back to its original position by using animation.
'''
if not self.move_to==[]:
anim1 = Animation(x=self.move_to[0], y=self.move_to[1],
t=self.animation_type,
duration=self.animation_duration)
anim1.start(self)
else:
pass
def on_touch_down(self, touch):
xx, yy, w, h = self.swipe_rectangle
x, y = touch.pos
if not self.collide_point(x, y):
touch.ud[self._get_uid('svavoid')] = True
return super(SwipeBehavior, self).on_touch_down(touch)
if self._swipe_touch or ('button' in touch.profile and
touch.button.startswith('scroll')) or\
not ((xx < x <= xx + w) and (yy < y <= yy + h)):
return super(SwipeBehavior, self).on_touch_down(touch)
# no mouse scrolling, so the user is going to swipe with this touch.
self._swipe_touch = touch
uid = self._get_uid()
touch.grab(self)
touch.ud[uid] = {
'mode': 'unknown',
'dx': 0,
'dy': 0}
Clock.schedule_once(self._change_touch_mode,
self.swipe_timeout / 1000.)
return True
def on_touch_move(self, touch):
if self._get_uid('svavoid') in touch.ud or\
self._swipe_touch is not touch:
return super(SwipeBehavior, self).on_touch_move(touch) or\
self._get_uid() in touch.ud
if touch.grab_current is not self:
return True
uid = self._get_uid()
ud = touch.ud[uid]
mode = ud['mode']
if mode == 'unknown':
ud['dx'] += abs(touch.dx)
if ud['dx'] > sp(self.swipe_distance):
mode = 'drag'
ud['mode'] = mode
if mode == 'drag':
self.x += touch.dx
return True
def on_touch_up(self, touch):
if self._get_uid('svavoid') in touch.ud:
return super(SwipeBehavior, self).on_touch_up(touch)
if self._swipe_touch and self in [x() for x in touch.grab_list]:
touch.ungrab(self)
self._swipe_touch = None
ud = touch.ud[self._get_uid()]
if ud['mode'] == 'unknown':
super(SwipeBehavior, self).on_touch_down(touch)
Clock.schedule_once(partial(self._do_touch_up, touch), .1)
else:
if self._swipe_touch is not touch:
super(SwipeBehavior, self).on_touch_up(touch)
return self._get_uid() in touch.ud
def _do_touch_up(self, touch, *largs):
super(SwipeBehavior, self).on_touch_up(touch)
# don't forget about grab event!
for x in touch.grab_list[:]:
touch.grab_list.remove(x)
x = x()
if not x:
continue
touch.grab_current = x
super(SwipeBehavior, self).on_touch_up(touch)
touch.grab_current = None
def _change_touch_mode(self, *largs):
if not self._swipe_touch:
return
uid = self._get_uid()
touch = self._swipe_touch
ud = touch.ud[uid]
if ud['mode'] != 'unknown':
return
touch.ungrab(self)
self._swipe_touch = None
super(SwipeBehavior, self).on_touch_down(touch)
return
def check_for_left(self):
'''
Delete the widget when on_touch_up method is called and the widget is
left_percentage from the left.
'''
if hasattr(self.parent, 'width'):
if (((self.x+self.width) - self.parent.x) / self.width
) * 100 <= self.left_percentage:
if self.remove_from_left:
self.parent.remove_widget(self)
else:
self.get_widget_back()
self.animate_back()
else:
pass
def check_for_right(self):
'''
Delete the widget when on_touch_up method is called and the widget is
right_percentage from the right.
'''
if hasattr(self.parent, 'width'):
if ((self.parent.width - self.x) / self.width
) * 100 <= self.right_percentage:
if self.remove_from_right:
self.parent.remove_widget(self)
else:
self.get_widget_back()
self.animate_back()
else:
pass
'''
get the opacity of widget back
'''
def reduce_opacity(self):
self.opacity -= self.opacity_reduction_rate
def get_widget_back(self):
self.opacity = 1.0