Hi,
John Boyd <
jbo...@gmail.com> writes:
> I overrode the (private)_start_animation function to be able to add widget
> to Carousel on swipe.
In order to add a widget on swipe, you could bind on the "on_index"
event.
--8<---------------cut here---------------start------------->8---
class MYCarousel(Carousel):
def on_index(self, inst, pos):
super(MyCarousel, self).on_index(inst, pos)
...
--8<---------------cut here---------------end--------------->8---
> Is there any other/better way to do this ?
> Would this cause any problems ?
I can show another situation where overriding the _start_animation was
needed.
If you want to bind the swipe event on the keyboard, you will probably
want to prevent the swipe event to occur went one is already being
done. To do that, I changed it to :
--8<---------------cut here---------------start------------->8---
def _start_animation(self, *args, **kwargs):
# compute target offset for ease back, next or prev
new_offset = 0
direction = kwargs.get('direction', self.direction)
is_horizontal = direction[0] in ['r', 'l']
extent = self.width if is_horizontal else self.height
min_move = kwargs.get('min_move', self.min_move)
_offset = kwargs.get('offset', self._offset)
if _offset < min_move * -extent:
new_offset = -extent
elif _offset > min_move * extent:
new_offset = extent
# if new_offset is 0, it wasnt enough to go next/prev
dur = self.anim_move_duration
if new_offset == 0:
dur = self.anim_cancel_duration
# detect edge cases if not looping
len_slides = len(self.slides)
index = self.index
if not self.loop or len_slides == 1:
is_first = (index == 0)
is_last = (index == len_slides - 1)
if direction[0] in ['r', 't']:
towards_prev = (new_offset > 0)
towards_next = (new_offset < 0)
else:
towards_prev = (new_offset < 0)
towards_next = (new_offset > 0)
if (is_first and towards_prev) or (is_last and towards_next):
new_offset = 0
anim = Animation(_offset=new_offset, d=dur, t=self.anim_type)
anim.cancel_all(self)
def _cmp(*l):
if self._skip_slide is not None:
self.index = self._skip_slide
self._skip_slide = None
self.in_anim = False
self.in_anim = True
anim.bind(on_complete=_cmp)
anim.start(self)
--8<---------------cut here---------------end--------------->8---
This way, I could run:
--8<---------------cut here---------------start------------->8---
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] in ["right", "left"] and self.in_anim:
return False
if keycode[1] == 'right':
self.load_next()
elif keycode[1] == 'left':
self.load_previous()
else:
return False
return True
--8<---------------cut here---------------end--------------->8---
My two cents!
--
Konubinix
GPG Key : 7439106A
Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A