Skip to content

Commit

Permalink
Kivy/Android
Browse files Browse the repository at this point in the history
 - switching of redeal images (redeal/stopped) corrected.
 - new wrapper class for options management. Refactorings.
 - redeal, pause and demo logo style selections updated.
  • Loading branch information
lufebe16 committed Dec 12, 2023
1 parent 611bd37 commit c5b2256
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 56 deletions.
13 changes: 10 additions & 3 deletions pysollib/game/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,8 @@ def winAnimation(self, perfect=0):
return
if TOOLKIT == 'gtk':
return
if TOOLKIT == 'kivy':
return
if not Image:
return
self.canvas.hideAllItems()
Expand All @@ -1801,6 +1803,8 @@ def winAnimation(self, perfect=0):
return

def redealAnimation(self):
if TOOLKIT == 'kivy':
return
if self.preview:
return
if not self.app.opt.animations or not self.app.opt.redeal_animation:
Expand Down Expand Up @@ -3428,6 +3432,11 @@ def updatePlayTime(self, do_after=1):
d = time.time() - self.stats.update_time + self.stats.elapsed_time
self.updateStatus(time=format_time(d))

def displayPauseImage(self):
n = self.random.initial_seed % len(self.app.gimages.pause)
self.pause_logo = self.app.gimages.pause[int(n)]
self.canvas.setTopImage(self.pause_logo)

def doPause(self):
if self.finished:
return
Expand All @@ -3439,9 +3448,7 @@ def doPause(self):
if self.pause:
# self.updateTime()
self.canvas.hideAllItems()
n = self.random.initial_seed % len(self.app.gimages.pause)
self.pause_logo = self.app.gimages.pause[int(n)]
self.canvas.setTopImage(self.pause_logo)
self.displayPauseImage()
else:
self.stats.update_time = time.time()
self.updatePlayTime()
Expand Down
8 changes: 8 additions & 0 deletions pysollib/kivy/LApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,19 +698,27 @@ def __init__(self, **kw):
self.game = None
self.card = None
self.group = None
self.image_type = "undefined"
if 'game' in kw:
self.game = kw['game']
if 'card' in kw:
self.card = kw['card']
self.image_type = "card"
if 'group' in kw:
self.group = kw['group']
if 'image_type' in kw:
self.image_type = kw['image_type']

self.dragstart = None
# ev. noch globales cache für stacks->game und cards->stack
# einrichten. Aber: stacks hängt vom jeweiligen spiel ab.

def __str__(self):
return f'<LImageItem @ {hex(id(self))}>'

def get_image_type(self):
return self.image_type

def send_event_pressed_n(self, event, n):
if self.group and n in self.group.bindings:
self.group.bindings[n](event)
Expand Down
69 changes: 69 additions & 0 deletions pysollib/kivy/LObjWrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/python
# -*- mode: python; coding: utf-8; -*-
# =============================================================================
# Copyright (C) 2017-2023 LB
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Flake8: noqa
#
# =============================================================================

import logging

from kivy.event import EventDispatcher
from kivy.properties import BooleanProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty
from kivy.properties import StringProperty

# =============================================================================
# Joins kivy properties to object members of referenced class.
# Usage:
# - use derived classes LBoolWrap etc. according to type.
# - write: obj.value = <new value>
# - read: <actual value> = obj.value
# - If you need additional functionality on change add a callback function
# as 'command'. It will be called whenever the value changes.

class LObjWrap(EventDispatcher):
def __init__(self,obj,ref,command=None):
self.obj = obj
self.ref = ref
self.value = getattr(self.obj,self.ref)
# logging.info("LObjWrap: setup for %s" % (self.ref))
self.bind(value=self.on_value)
if command is not None:
self.bind(value=command)

def on_value(self,inst,val):
logging.info("LObjWrap: %s = %s" % (self.ref,val))
setattr(self.obj,self.ref,val)

class LBoolWrap(LObjWrap):
value = BooleanProperty(False)

def __init__(self,obj,ref,command=None):
super(LBoolWrap,self).__init__(obj,ref,command)

class LNumWrap(LObjWrap):
value = NumericProperty(0)

def __init__(self,obj,ref,command=None):
super(LNumWrap,self).__init__(obj,ref,command)

class LStringWrap(LObjWrap):
value = StringProperty('')

def __init__(self,obj,ref,command=None):
super(LStringWrap,self).__init__(obj,ref,command)

class LListWrap(LObjWrap):
value = ListProperty([])

def __init__(self,obj,ref,command=None):
super(LListWrap,self).__init__(obj,ref,command)

# =============================================================================
97 changes: 46 additions & 51 deletions pysollib/kivy/menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
from pysollib.kivy.LApp import LTopLevel
from pysollib.kivy.LApp import LTreeNode
from pysollib.kivy.LApp import LTreeRoot
from pysollib.kivy.LObjWrap import LBoolWrap
from pysollib.kivy.LObjWrap import LNumWrap
from pysollib.kivy.LObjWrap import LStringWrap
from pysollib.kivy.androidrot import AndroidScreenRotation
from pysollib.kivy.findcarddialog import destroy_find_card_dialog
from pysollib.kivy.fullpicturedialog import destroy_full_picture_dialog
Expand Down Expand Up @@ -119,16 +122,18 @@ def auto_close_command():
self.closeWindow(0)
return auto_close_command

def make_auto_command(self, variable, command):
def make_toggle_command(self, variable, command):
def auto_command():
variable.set(not variable.get())
command()
variable.value = not variable.value
if command is not None:
command()
return auto_command

def make_val_command(self, variable, value, command):
def val_command():
variable.value = value
command()
if command is not None:
command()
return val_command

def make_vars_command(self, command, key):
Expand All @@ -149,7 +154,7 @@ def _command():
return _command

def addCheckNode(self, tv, rg, title, auto_var, auto_com):
command = self.make_auto_command(auto_var, auto_com)
command = self.make_toggle_command(auto_var, auto_com)
rg1 = tv.add_node(
LTreeNode(text=title, command=command, variable=auto_var), rg)
return rg1
Expand Down Expand Up @@ -278,7 +283,7 @@ def __init__(self, menubar, parent, title, app, **kw):
menubar, parent, title, app, **kw)

print('MainMenuDialog starting')
AndroidScreenRotation.unlock()
AndroidScreenRotation.unlock(toaster=False)

def buildTree(self, tv, node):
rg = tv.add_node(
Expand Down Expand Up @@ -1134,17 +1139,23 @@ def buildTree(self, tv, node):
self.menubar.tkopt.animations, 5,
self.menubar.mOptAnimations)

# submenu.add_separator()

# NOTE: All the following animation features only work on the
# desktop and only if pillow is installed. So its useless to
# present them here.
'''
self.addCheckNode(tv, rg,
_('Redeal animation'),
self.menubar.tkopt.redeal_animation,
self.menubar.mRedealAnimation)

self.addCheckNode(tv, rg,
_('Winning animation'),
self.menubar.tkopt.win_animation,
self.menubar.mWinAnimation)
self.addCheckNode(tv, rg,
_('Flip animation'),
self.menubar.tkopt.flip_animation,
None)
'''

yield
# -------------------------------------------
Expand Down Expand Up @@ -1472,6 +1483,7 @@ def __init__(self, app, top, progress=None):
self.progress.update(step=1)

def _createTkOpt(self):
opt = self.app.opt
# structure to convert menu-options to Toolkit variables
self.tkopt = Struct(
gameid=IntVar(),
Expand Down Expand Up @@ -1501,9 +1513,10 @@ def _createTkOpt(self):
sound_music_volume=IntVar(),
cardback=IntVar(),
tabletile=IntVar(),
animations=IntVar(),
redeal_animation=BooleanVar(),
win_animation=BooleanVar(),
animations=LNumWrap(opt, "animations"),
redeal_animation=LBoolWrap(opt, "redeal_animation"),
win_animation=LBoolWrap(opt, "win_animation"),
flip_animation=LBoolWrap(opt, "flip_animation"),
shadow=BooleanVar(),
shade=BooleanVar(),
shade_filled_stacks=BooleanVar(),
Expand All @@ -1518,10 +1531,10 @@ def _createTkOpt(self):
helpbar=BooleanVar(),
save_games_geometry=BooleanVar(),
splashscreen=BooleanVar(),
demo_logo=BooleanVar(),
demo_logo_style=StringVar(),
pause_text_style=StringVar(),
redeal_icon_style=StringVar(),
demo_logo=LBoolWrap(opt, "demo_logo"),
demo_logo_style=LStringWrap(opt, "demo_logo_style"),
pause_text_style=LStringWrap(opt, "pause_text_style"),
redeal_icon_style=LStringWrap(opt, "redeal_icon_style"),
mouse_type=StringVar(),
mouse_undo=BooleanVar(),
negative_bottom=BooleanVar(),
Expand Down Expand Up @@ -1570,9 +1583,6 @@ def _setOptions(self):
tkopt.sound_music_volume.set(opt.sound_music_volume)
tkopt.cardback.set(self.app.cardset.backindex)
tkopt.tabletile.set(self.app.tabletile_index)
tkopt.animations.set(opt.animations)
tkopt.redeal_animation.set(opt.redeal_animation)
tkopt.win_animation.set(opt.win_animation)
tkopt.shadow.set(opt.shadow)
tkopt.shade.set(opt.shade)
tkopt.toolbar.set(opt.toolbar)
Expand All @@ -1583,10 +1593,6 @@ def _setOptions(self):
tkopt.toolbar_relief.set(opt.toolbar_relief)
tkopt.statusbar.set(opt.statusbar)
tkopt.save_games_geometry.set(opt.save_games_geometry)
tkopt.demo_logo.set(opt.demo_logo)
tkopt.demo_logo_style.set(opt.demo_logo_style)
tkopt.pause_text_style.set(opt.pause_text_style)
tkopt.redeal_icon_style.set(opt.redeal_icon_style)
tkopt.splashscreen.set(opt.splashscreen)
tkopt.mouse_type.set(opt.mouse_type)
tkopt.mouse_undo.set(opt.mouse_undo)
Expand Down Expand Up @@ -2334,17 +2340,14 @@ def mOptEnableStuckNotification(self, *args):
def mOptAnimations(self, *args):
if self._cancelDrag(break_pause=False):
return
self.app.opt.animations = self.tkopt.animations.value

def mRedealAnimation(self, *args):
if self._cancelDrag(break_pause=False):
return
self.app.opt.redeal_animation = self.tkopt.redeal_animation.value

def mWinAnimation(self, *args):
if self._cancelDrag(break_pause=False):
return
self.app.opt.win_animation = self.tkopt.win_animation.value

def mWinDialog(self, *args):
if self._cancelDrag(break_pause=False):
Expand Down Expand Up @@ -2499,13 +2502,13 @@ def mOptToolbarConfig(self, w):
self.toolbarConfig(w, self.tkopt.toolbar_vars[w].get())

def mOptDemoLogoStyle(self, *event):
self.setDemoLogoStyle(self.tkopt.demo_logo_style.get())
self.setDemoLogoStyle()

def mOptPauseTextStyle(self, *event):
self.setPauseTextStyle(self.tkopt.pause_text_style.get())
self.setPauseTextStyle()

def mOptRedealIconStyle(self, *event):
self.setRedealIconStyle(self.tkopt.redeal_icon_style.get())
self.setRedealIconStyle()

def mOptStatusbar(self, *event):
if self._cancelDrag(break_pause=False):
Expand Down Expand Up @@ -2542,7 +2545,6 @@ def mOptSaveGamesGeometry(self, *event):
def mOptDemoLogo(self, *event):
if self._cancelDrag(break_pause=False):
return
self.app.opt.demo_logo = self.tkopt.demo_logo.get()

def mOptSplashscreen(self, *event):
if self._cancelDrag(break_pause=False):
Expand Down Expand Up @@ -2630,20 +2632,15 @@ def toolbarConfig(self, w, v):
# other graphics
#

def setDemoLogoStyle(self, style):
def setDemoLogoStyle(self, style=None):
if self._cancelDrag(break_pause=False):
return
if style == "none":
self.app.opt.demo_logo = False
if self.tkopt.demo_logo_style.value == "none":
self.tkopt.demo_logo.value = False
else:
self.app.opt.demo_logo = True
self.app.opt.demo_logo_style = style
self.tkopt.demo_logo_style.set(style) # update radiobutton
self.tkopt.demo_logo.value = True
self.app.loadImages2()
self.app.loadImages4()
self.app.updateCardset()
self.game.endGame(bookmark=1)
self.game.quitGame(bookmark=1)

def setDialogIconStyle(self, style):
if self._cancelDrag(break_pause=False):
Expand All @@ -2653,27 +2650,25 @@ def setDialogIconStyle(self, style):
self.app.loadImages1()
self.app.loadImages4()

def setPauseTextStyle(self, style):
def setPauseTextStyle(self, style=None):
if self._cancelDrag(break_pause=False):
return
self.app.opt.pause_text_style = style
self.tkopt.pause_text_style.set(style) # update radiobutton
self.app.loadImages2()
self.app.loadImages4()
self.app.updateCardset()
self.game.endGame(bookmark=1)
self.game.quitGame(bookmark=1)
if self.tkopt.pause.value:
self.app.game.displayPauseImage()

def setRedealIconStyle(self, style):
def setRedealIconStyle(self, style=None):
if self._cancelDrag(break_pause=False):
return
self.app.opt.redeal_icon_style = style
self.tkopt.redeal_icon_style.set(style) # update radiobutton
self.app.loadImages2()
self.app.loadImages4()
self.app.updateCardset()
self.game.endGame(bookmark=1)
self.game.quitGame(bookmark=1)
try:
images = self.app.game.canvas.findImagesByType("redeal_image")
for i in images:
i.group.stack.updateRedealImage()
except: # noqa
pass

#
# stacks descriptions
Expand Down
Loading

0 comments on commit c5b2256

Please sign in to comment.