1 from kivy
.app
import App
2 from kivy
.uix
.widget
import Widget
3 from kivy
.uix
.boxlayout
import BoxLayout
4 from kivy
.uix
.slider
import Slider
5 from kivy
.uix
.textinput
import TextInput
6 from kivy
.uix
.label
import Label
7 from kivy
.uix
.listview
import ListView
8 from kivy
.properties
import NumericProperty
, ReferenceListProperty
,\
9 ObjectProperty
, BoundedNumericProperty
10 from kivy
.vector
import Vector
11 from kivy
.clock
import Clock
12 from kivy
.adapters
.simplelistadapter
import SimpleListAdapter
20 class SlavedSlider(Slider
):
23 class SlavedTextInput(TextInput
):
24 pat
= re
.compile('[^0-9]')
26 def insert_text(self
, substring
, from_undo
=False):
28 s
= re
.sub(pat
, '', substring
)
29 return super(SlavedTextInput
, self
).insert_text(s
, from_undo
=from_undo
)
31 class TenraRoller(BoxLayout
):
33 max_dice
= NumericProperty(MAX_DICE
)
34 max_skill
= NumericProperty(MAX_SKILL
)
36 dice_slider
= ObjectProperty(None)
37 dice_text
= ObjectProperty(None)
38 dice_value
= BoundedNumericProperty(int(MAX_DICE
/ 2), min=1, max=MAX_DICE
)
40 skill_slider
= ObjectProperty(None)
41 skill_text
= ObjectProperty(None)
42 skill_value
= BoundedNumericProperty(int(MAX_SKILL
/ 2), min=1, max=MAX_SKILL
)
44 result_label
= ObjectProperty(None)
45 previous_results
= ObjectProperty(None)
47 last_results_texts
= []
48 previous_results_la
= SimpleListAdapter(data
=last_results_texts
,
52 def slider_value_change(self
, myid
):
53 print("Slider {} changed to {}".format(myid
, myid
.value
))
54 if myid
== self
.dice_slider
:
55 self
.dice_value
= int(myid
.value
)
56 self
.dice_text
.text
= str(self
.dice_value
)
57 if myid
== self
.skill_slider
:
58 self
.skill_value
= int(myid
.value
)
59 self
.skill_text
.text
= str(self
.skill_value
)
61 def text_value_change(self
, myid
):
62 print("Text {} changed to {}".format(myid
, myid
.text
))
64 if myid
== self
.skill_text
:
65 self
.skill_value
= int(myid
.text
)
66 self
.skill_slider
.value
= self
.skill_value
67 if myid
== self
.dice_text
:
68 self
.dice_value
= int(myid
.text
)
69 self
.dice_slider
.value
= self
.dice_value
71 myid
.background_color
= [1, 0.5, 0.5, 1]
73 myid
.background_color
= [1, 1, 1, 1]
75 # if myid == self.dice_text:
76 # self.dice_value = int(myid.text)
77 # self.dice_slider.value = self.dice_value
78 # if myid == self.skill_text:
80 # self.skill_value = int(myid.text)
82 # myid.background_color = [1, 0.5, 0.5, 1]
84 # myid.background_color = [1, 1, 1, 1]
85 # self.skill_slider.value = self.skill_value
90 for i
in range(self
.dice_value
):
91 if random
.randint(1, 6) <= self
.skill_value
:
93 self
.last_results_texts
= [self
.result_label
.text
] + self
.last_results_texts
94 self
.result_label
.text
= 'Rolled {dice}({skill}) for {succ} successes'.format(dice
=self
.dice_value
, skill
=self
.skill_value
,
96 self
.previous_results_la
.data
= self
.last_results_texts
99 # def slider_value_change(slider, value):
100 # print("Slider {} changed to {}".format(slider, value))
102 class TenraRollerApp(App
):
103 # dice_slider = ObjectProperty(None)
104 # dice_text = ObjectProperty(None)
105 # dice_value = BoundedNumericProperty(10, min=1, max=30)
108 roller
= TenraRoller(max_dice
=MAX_DICE
, max_skill
=MAX_SKILL
)
112 if __name__
== '__main__':
113 TenraRollerApp().run()