1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

Kivy/Android

- refactoring, hardening, flake8
This commit is contained in:
lufebe16 2023-11-30 11:40:13 +01:00
parent 10c6b5c61d
commit 224d21a2d2
5 changed files with 48 additions and 41 deletions

View file

@ -1455,7 +1455,7 @@ class Game(object):
c0 = cards[0] c0 = cards[0]
dx, dy = (x - c0.x), (y - c0.y) dx, dy = (x - c0.x), (y - c0.y)
base = float(self.app.opt.animations) base = float(self.app.opt.animations)
duration = base*base/25.0 + 0.05 duration = base*base/30.0 + 0.05
for card in cards: for card in cards:
card.animatedMove(dx, dy, duration) card.animatedMove(dx, dy, duration)
# self.top.waitAnimation(swallow=True, pickup=True) # self.top.waitAnimation(swallow=True, pickup=True)

View file

@ -135,30 +135,28 @@ class LAnimationTask(LTask, LBase):
class LAnimationMgr(object): class LAnimationMgr(object):
def __init__(self, **kw): def __init__(self, **kw):
super(LAnimationMgr, self).__init__() super(LAnimationMgr, self).__init__()
self.animations = [] self.tasks = []
self.widgets = {}
self.callbacks = [] self.callbacks = []
self.taskQ = LTaskQ() self.taskQ = LTaskQ()
def checkRunning(self): def checkRunning(self):
return len(self.animations) > 0 return len(self.tasks) > 0
def addEndCallback(self, cb): def addEndCallback(self, cb):
self.callbacks.append(cb) self.callbacks.append(cb)
# print('len of callbacks', len(self.callbacks))
def animEnd(self, anim, widget): def taskEnd(self, task, value):
# print('LAnimationMgr: animEnd = %s.%s' % (anim, widget)) if value:
print('LAnimationMgr: taskEnd = %s %s' % (task, value))
self.tasks.remove(task)
if not self.checkRunning():
# print('LAnimationMgr: taskEnd ->', len(self.callbacks), 'callbacks') # noqa
for cb in self.callbacks:
cb()
# print('LAnimationMgr: taskEnd -> callbacks done')
self.callbacks = []
self.animations.remove(anim) print('Clock.get_fps() ->', Clock.get_fps())
if not self.checkRunning():
# print('LAnimationMgr: animEnd ->', len(self.callbacks), 'callbacks') # noqa
for cb in self.callbacks:
cb()
# print('LAnimationMgr: animEnd -> callbacks done')
self.callbacks = []
print('Clock.get_fps() ->', Clock.get_fps())
def create(self, spos, widget, **kw): def create(self, spos, widget, **kw):
x = 0.0 x = 0.0
@ -175,16 +173,17 @@ class LAnimationMgr(object):
transition = kw['transition'] transition = kw['transition']
anim = Animation(x=x, y=y, duration=duration, transition=transition) anim = Animation(x=x, y=y, duration=duration, transition=transition)
anim.bind(on_complete=self.animEnd)
if 'bindE' in kw: if 'bindE' in kw:
anim.bind(on_complete=kw['bindE']) anim.bind(on_complete=kw['bindE'])
if 'bindS' in kw: if 'bindS' in kw:
anim.bind(on_start=kw['bindS']) anim.bind(on_start=kw['bindS'])
self.animations.append(anim)
offset = duration / 3.0 offset = duration / 3.0
animTask = LAnimationTask(anim, spos, widget, offset) task = LAnimationTask(anim, spos, widget, offset)
Clock.schedule_once(lambda dt: self.taskQ.taskInsert(animTask), 0.016) self.tasks.append(task)
task.bind(done=self.taskEnd)
Clock.schedule_once(lambda dt: self.taskQ.taskInsert(task), 0.016)
LAnimationManager = LAnimationMgr() LAnimationManager = LAnimationMgr()

View file

@ -21,11 +21,10 @@ from kivy.properties import BooleanProperty, ListProperty
class LTask(EventDispatcher): class LTask(EventDispatcher):
done = BooleanProperty(False) done = BooleanProperty(True)
def __init__(self, name): def __init__(self, name):
super(LTask, self).__init__() super(LTask, self).__init__()
self.done = False
self.name = name self.name = name
self.delay = 0.01 self.delay = 0.01
@ -37,10 +36,6 @@ class LTask(EventDispatcher):
# print('stop of ',self.name) # print('stop of ',self.name)
self.done = True self.done = True
def on_done(self, instance, value):
# print ('on_done',instance,value)
pass
class LTaskQ(EventDispatcher): class LTaskQ(EventDispatcher):
waitQ = ListProperty([]) waitQ = ListProperty([])

View file

@ -23,6 +23,7 @@
from pysollib.acard import AbstractCard from pysollib.acard import AbstractCard
from pysollib.kivy.LApp import LImageItem from pysollib.kivy.LApp import LImageItem
# from pysollib.kivy.LApp import LAnimationManager
from pysollib.kivy.LImage import LImage from pysollib.kivy.LImage import LImage
from pysollib.kivy.tkcanvas import MfxCanvasImage from pysollib.kivy.tkcanvas import MfxCanvasImage
@ -89,18 +90,24 @@ class _OneImageCard(_HideableCard):
def showFace(self, unhide=1): def showFace(self, unhide=1):
# print ('card: showFace = %s' % self._face_image.source) # print ('card: showFace = %s' % self._face_image.source)
if not self.face_up: def flip():
self._setImage(image=self._face_image) self._setImage(image=self._face_image)
self.tkraise(unhide) self.tkraise(unhide)
self.face_up = 1 self.face_up = 1
if not self.face_up:
flip()
def showBack(self, unhide=1): def showBack(self, unhide=1):
# print ('card: showBack = %s' % self._back_image.source) # print ('card: showBack = %s' % self._back_image.source)
if self.face_up: def flip():
self._setImage(image=self._back_image) self._setImage(image=self._back_image)
self.tkraise(unhide) self.tkraise(unhide)
self.face_up = 0 self.face_up = 0
if self.face_up:
flip()
def updateCardBackground(self, image): def updateCardBackground(self, image):
print('card: updateCardBackground = %s' % image.source) print('card: updateCardBackground = %s' % image.source)
self._back_image = LImage(texture=image.texture) self._back_image = LImage(texture=image.texture)

View file

@ -276,8 +276,8 @@ class MfxCanvasImage(object):
super(MfxCanvasImage, self).__init__() super(MfxCanvasImage, self).__init__()
self.canvas = canvas self.canvas = canvas
# animation support: # animation mode support:
self.animation = None self.animation = 0
self.deferred_raises = [] self.deferred_raises = []
self.deferred_pos = [] self.deferred_pos = []
@ -337,9 +337,10 @@ class MfxCanvasImage(object):
# print('stack[2] = ', inspect.stack()[2].frame) # print('stack[2] = ', inspect.stack()[2].frame)
# print('stack[3] = ', inspect.stack()[3].frame) # print('stack[3] = ', inspect.stack()[3].frame)
if self.animation: if self.animation > 0:
# print('defer tkraise to animation', abitm) # print('defer tkraise to animation', abitm)
self.deferred_raises.append(self.makeDeferredRaise(abitm)) if len(self.deferred_raises) < self.animation:
self.deferred_raises.append(self.makeDeferredRaise(abitm))
return return
# print('direct tkraise', abitm) # print('direct tkraise', abitm)
@ -368,11 +369,12 @@ class MfxCanvasImage(object):
dsize = image.coreSize dsize = image.coreSize
dpos = (image.corePos[0] + dx, image.corePos[1] + dy) dpos = (image.corePos[0] + dx, image.corePos[1] + dy)
image.corePos = dpos image.corePos = dpos
if not self.animation: if self.animation == 0:
image.pos, image.size = self.canvas.CoreToKivy(dpos, dsize) image.pos, image.size = self.canvas.CoreToKivy(dpos, dsize)
else: else:
pos, size = self.canvas.CoreToKivy(dpos, dsize) if len(self.deferred_pos) < self.animation:
self.deferred_pos.append(pos) pos, size = self.canvas.CoreToKivy(dpos, dsize)
self.deferred_pos.append(pos)
def makeAnimStart(self): def makeAnimStart(self):
def animStart(anim, widget): def animStart(anim, widget):
@ -399,7 +401,13 @@ class MfxCanvasImage(object):
def makeAnimEnd(self, dpos, dsize): def makeAnimEnd(self, dpos, dsize):
def animEnd(anim, widget): def animEnd(anim, widget):
# print('MfxCanvasImage: animEnd %s' % self) # print('MfxCanvasImage: animEnd %s' % self)
self.animation = False if self.animation > 0:
self.animation -= 1
if self.animation == 0:
# just for the case, keep in sync:
self.deferred_raises = []
self.deferred_pos = []
# print('MfxCanvasImage: animEnd moved to %s, %s' % (dpos[0], dpos[1])) # noqa # print('MfxCanvasImage: animEnd moved to %s, %s' % (dpos[0], dpos[1])) # noqa
return animEnd return animEnd
@ -422,7 +430,7 @@ class MfxCanvasImage(object):
if self.canvas.wmain.app.game.demo: if self.canvas.wmain.app.game.demo:
transition = transition1 transition = transition1
self.animation = True self.animation += 1
ssize = image.coreSize ssize = image.coreSize
spos = (image.corePos[0], image.corePos[1]) spos = (image.corePos[0], image.corePos[1])
spos, ssize = self.canvas.CoreToKivy(spos, ssize) spos, ssize = self.canvas.CoreToKivy(spos, ssize)
@ -776,16 +784,14 @@ class MfxCanvas(LImage):
# print('MfxCanvas: tag_lower: to bottom') # print('MfxCanvas: tag_lower: to bottom')
self.remove_widget(itm) self.remove_widget(itm)
k = len(self.children.index) k = len(self.children.index)
self.add_widget(itm,index=k) self.add_widget(itm, index=k)
else: else:
# print('MfxCanvas: tag_lower: to specified position') # print('MfxCanvas: tag_lower: to specified position')
self.remove_widget(itm) self.remove_widget(itm)
k = self.children.index(belowThis) k = self.children.index(belowThis)
k += 1 k += 1
self.add_widget(itm, index=k) self.add_widget(itm, index=k)
#
#
#
def setInitialSize(self, width, height): def setInitialSize(self, width, height):
self.r_width = width self.r_width = width
self.r_height = height self.r_height = height