Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
BetSize widgets bindings, refs #5
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Mar 26, 2018
1 parent 789ec48 commit 484e16e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/main.kv
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
PushUp:


<BetSize@BoxLayout>:
<BetSize>:
orientation: "vertical"
MDLabel:
text: "Bet size"
Expand All @@ -46,6 +46,8 @@
size_hint_x: None
MDSlider:
id: bet_size_slider_id
range: 0, 10
step: 0.1

<ChanceOfWinning>:
orientation: "vertical"
Expand Down
47 changes: 36 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,58 @@ class RollResultsScreen(SubScreen):
pass


class ChanceOfWinning(BoxLayout):
class BetSize(BoxLayout):

def __init__(self, **kwargs):
super(ChanceOfWinning, self).__init__(**kwargs)
super(BetSize, self).__init__(**kwargs)
Clock.schedule_once(self._after_init)

def _after_init(self, dt):
"""
Binds events.
"""
slider = self.ids.bet_size_slider_id
inpt = self.ids.bet_size_input_id

def cast_to(value): return round(float(value), 1)
BetSize.bind_slider_input(slider, inpt, cast_to)

@staticmethod
def bind_slider_input(slider, inpt, cast_to=float):
"""
Binds slider <-> input both ways.
"""
# slider -> input
chances_slider = self.ids.chances_slider_id
chances_input = self.ids.chances_input_id
chances_slider.bind(
slider.bind(
value=lambda instance, value:
setattr(chances_input, 'text', str(int(value))))
setattr(inpt, 'text', str(cast_to(value))))
# input -> slider
chances_input.bind(
inpt.bind(
on_text_validate=lambda instance:
setattr(chances_slider, 'value', int(chances_input.text)))
setattr(slider, 'value', cast_to(inpt.text)))
# also when unfocused
chances_input.bind(
inpt.bind(
focus=lambda instance, focused:
chances_input.dispatch('on_text_validate')
inpt.dispatch('on_text_validate')
if not focused else False)
# synchronises values slider <-> input once
chances_input.dispatch('on_text_validate')
inpt.dispatch('on_text_validate')


class ChanceOfWinning(BoxLayout):

def __init__(self, **kwargs):
super(ChanceOfWinning, self).__init__(**kwargs)
Clock.schedule_once(self._after_init)

def _after_init(self, dt):
"""
Binds events.
"""
slider = self.ids.chances_slider_id
inpt = self.ids.chances_input_id
cast_to = int
BetSize.bind_slider_input(slider, inpt, cast_to)


class RollScreen(Screen):
Expand Down

0 comments on commit 484e16e

Please sign in to comment.