From 901a04bbb4614487c6b74aa94a287c06bf25eb56 Mon Sep 17 00:00:00 2001 From: lufebe16 Date: Sat, 14 Oct 2023 14:24:11 +0200 Subject: [PATCH] Kivy/Android: - reworked screen rotation lock - refactorings --- pysollib/kivy/LApp.py | 10 +++++++-- pysollib/kivy/androidrot.py | 16 ++++++------- pysollib/kivy/fullpicturedialog.py | 12 ++-------- pysollib/kivy/menubar.py | 36 +++++++++++++----------------- 4 files changed, 33 insertions(+), 41 deletions(-) diff --git a/pysollib/kivy/LApp.py b/pysollib/kivy/LApp.py index 10e87abd..9a71f4ac 100644 --- a/pysollib/kivy/LApp.py +++ b/pysollib/kivy/LApp.py @@ -1637,6 +1637,9 @@ class LMainWindow(BoxLayout, LTkBase): def on_touch_down(self, touch): ret = False + if super().on_touch_down(touch): + return True + # print(dir(touch)) # multitouch detection @@ -1687,12 +1690,15 @@ class LMainWindow(BoxLayout, LTkBase): def on_touch_up(self, touch): ret = False + if super().on_touch_up(touch): + return True + # long press only on playground. pgs = self.workStack.peek('playground') if pgs: pg = pgs[1] if pg.collide_point(*touch.pos): - if (touch.time_end-touch.time_start) > 4.5: + if (touch.time_end-touch.time_start) > 2.5: self.longPress = touch.time_end # standard notifikation: @@ -1710,7 +1716,7 @@ class LMainWindow(BoxLayout, LTkBase): def on_longPress(self, instance, timestamp): print('longPressed at {time}'.format(time=timestamp)) - AndroidScreenRotation.toggle() + AndroidScreenRotation.lock() # Menubar: diff --git a/pysollib/kivy/androidrot.py b/pysollib/kivy/androidrot.py index e4809e5f..72499be7 100644 --- a/pysollib/kivy/androidrot.py +++ b/pysollib/kivy/androidrot.py @@ -22,25 +22,25 @@ class AndroidRot(object): def lock(self): if jnius is not None: - self.currentActivity.setRequestedOrientation( - self.ActivityInfo.SCREEN_ORIENTATION_LOCKED) + if not self.locked: + self.currentActivity.setRequestedOrientation( + self.ActivityInfo.SCREEN_ORIENTATION_LOCKED) + AndroidToaster.toastShort(_("screen rotation disabled")) self.locked = True def unlock(self): if jnius is not None: - self.currentActivity.setRequestedOrientation( - self.ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR) + if self.locked: + self.currentActivity.setRequestedOrientation( + self.ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR) + AndroidToaster.toastShort(_("screen rotation enabled")) self.locked = False def toggle(self): if self.locked: self.unlock() - if jnius is not None: - AndroidToaster.toastShort(_("screen rotation enabled")) else: self.lock() - if jnius is not None: - AndroidToaster.toastShort(_("screen rotation disabled")) return self.isLocked() AndroidScreenRotation = AndroidRot() diff --git a/pysollib/kivy/fullpicturedialog.py b/pysollib/kivy/fullpicturedialog.py index 754d139b..5bbda95b 100644 --- a/pysollib/kivy/fullpicturedialog.py +++ b/pysollib/kivy/fullpicturedialog.py @@ -36,7 +36,6 @@ from pysollib.mygettext import _ class ImageStacker(StackLayout): def __init__(self, **kw): super().__init__(**kw) - self.images = [] self.dx = 1 self.dy = 1 self.asp = 1 # width/height. @@ -48,10 +47,6 @@ class ImageStacker(StackLayout): def set_aspect(self, asp): self.asp = asp # width/height. - def add_image(self, image): - self.images.append(image) - self.add_widget(image) - def on_size(self, instance, value): hint_x = 1.0 / self.dx hint_y = 1.0 / self.dx @@ -62,7 +57,7 @@ class ImageStacker(StackLayout): else: hint_x = hint_x * self.asp / vasp # apply to all images - for i in self.images: + for i in self.children: i.size_hint = (hint_x, hint_y) @@ -75,19 +70,16 @@ class FullPictureDialog(object): self.images = game.app.subsampled_images dx, dy = self.images.CARDW, self.images.CARDH - print (self.images) # noqa - print (dx,dy) # noqa cards = self.game.gameinfo.trumps cols = int(math.ceil(math.sqrt(len(cards)))) - print (cols) # noqa self.stp = ImageStacker() self.stp.set_aspect(dx/dy) self.stp.set_matrix(cols, cols) for card in cards: image = LImage(texture=self.images._card[card].texture) - self.stp.add_image(image) + self.stp.add_widget(image) self.frame.add_widget(self.stp) diff --git a/pysollib/kivy/menubar.py b/pysollib/kivy/menubar.py index b9aac576..6c155a49 100644 --- a/pysollib/kivy/menubar.py +++ b/pysollib/kivy/menubar.py @@ -101,6 +101,12 @@ class LMenuDialog(object): parent.popWork(title) return pop_command + def auto_close(self, command): + def auto_close_command(): + command() + self.closeWindow(0) + return auto_close_command + def __init__(self, menubar, parent, title, app, **kw): super(LMenuDialog, self).__init__() @@ -170,44 +176,38 @@ class MainMenuDialog(LMenuDialog): super(MainMenuDialog, self).__init__( menubar, parent, title, app, **kw) - def make_game_command(self, command): - def game_command(): - command() - self.closeWindow(0) - return game_command - def buildTree(self, tv, node): rg = tv.add_node( LTreeNode( text=_("File"), - command=self.make_game_command(self.menubar.mFileMenuDialog))) + command=self.auto_close(self.menubar.mFileMenuDialog))) rg = tv.add_node( LTreeNode( text=_("Games"), - command=self.make_game_command( + command=self.auto_close( self.menubar.mSelectGameDialog))) rg = tv.add_node( LTreeNode( text=_("Tools"), - command=self.make_game_command(self.menubar.mEditMenuDialog))) + command=self.auto_close(self.menubar.mEditMenuDialog))) rg = tv.add_node( LTreeNode( text=_("Statistics"), - command=self.make_game_command(self.menubar.mGameMenuDialog))) + command=self.auto_close(self.menubar.mGameMenuDialog))) rg = tv.add_node( LTreeNode( text=_("Assist"), - command=self.make_game_command( + command=self.auto_close( self.menubar.mAssistMenuDialog))) rg = tv.add_node( LTreeNode( text=_("Options"), - command=self.make_game_command( + command=self.auto_close( self.menubar.mOptionsMenuDialog))) rg = tv.add_node( LTreeNode( text=_("Help"), - command=self.make_game_command(self.menubar.mHelpMenuDialog))) + command=self.auto_close(self.menubar.mHelpMenuDialog))) del rg # ************************************************************************ @@ -372,7 +372,7 @@ class GameMenuDialog(LMenuDialog): def buildTree(self, tv, node): tv.add_node(LTreeNode( text=_('Current game...'), - command=self.make_command(101, self.menubar.mPlayerStats)), None) + command=self.auto_close(self.make_command(101, self.menubar.mPlayerStats))), None) # noqa # tv.add_node(LTreeNode( # text='All games ...', @@ -432,12 +432,6 @@ class AssistMenuDialog(LMenuDialog): super(AssistMenuDialog, self).__init__( menubar, parent, title, app, **kw) - def make_auto_close(self, command): - def auto_close_command(): - command() - self.closeWindow(0) - return auto_close_command - def buildTree(self, tv, node): tv.add_node(LTreeNode( text=_('Hint'), command=self.menubar.mHint)) @@ -447,7 +441,7 @@ class AssistMenuDialog(LMenuDialog): tv.add_node(LTreeNode( text=_('Show full picture...'), - command=self.make_auto_close(self.menubar.mFullPicture))) + command=self.auto_close(self.menubar.mFullPicture))) # tv.add_node(LTreeNode( # text='Find Card', command=self.menubar.mFindCard))