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
, StringProperty
10 from kivy
.vector
import Vector
11 from kivy
.clock
import Clock
12 from kivy
.adapters
.simplelistadapter
import SimpleListAdapter
19 INITIAL_RESULT_LABEL
= "No result"
21 class SlavedSlider(Slider
):
24 class SlavedTextInput(TextInput
):
25 pat
= re
.compile('[^0-9]')
27 def insert_text(self
, substring
, from_undo
=False):
29 s
= re
.sub(pat
, '', substring
)
30 return super(SlavedTextInput
, self
).insert_text(s
, from_undo
=from_undo
)
32 class PaddedLabel(Label
):
35 class TenraRoller(BoxLayout
):
37 max_dice
= NumericProperty(MAX_DICE
)
38 max_skill
= NumericProperty(MAX_SKILL
)
39 initial_result_label_text
= StringProperty(INITIAL_RESULT_LABEL
)
41 dice_slider
= ObjectProperty(None)
42 dice_text
= ObjectProperty(None)
43 dice_value
= BoundedNumericProperty(int(MAX_DICE
/ 2), min=1, max=MAX_DICE
)
45 skill_slider
= ObjectProperty(None)
46 skill_text
= ObjectProperty(None)
47 skill_value
= BoundedNumericProperty(int(MAX_SKILL
/ 2), min=1, max=MAX_SKILL
)
49 result_label
= ObjectProperty(None)
50 previous_results
= ObjectProperty(None)
52 previous_results_la
= SimpleListAdapter(data
=[],
56 def slider_value_change(self
, myid
):
57 print("Slider {} changed to {}".format(myid
, myid
.value
))
58 if myid
== self
.dice_slider
:
59 self
.dice_value
= int(myid
.value
)
60 self
.dice_text
.text
= str(self
.dice_value
)
61 if myid
== self
.skill_slider
:
62 self
.skill_value
= int(myid
.value
)
63 self
.skill_text
.text
= str(self
.skill_value
)
65 def text_value_change(self
, myid
):
66 print("Text {} changed to {}".format(myid
, myid
.text
))
68 if myid
== self
.skill_text
:
69 self
.skill_value
= int(myid
.text
)
70 self
.skill_slider
.value
= self
.skill_value
71 if myid
== self
.dice_text
:
72 self
.dice_value
= int(myid
.text
)
73 self
.dice_slider
.value
= self
.dice_value
75 myid
.background_color
= [1, 0.5, 0.5, 1]
77 myid
.background_color
= [1, 1, 1, 1]
79 # if myid == self.dice_text:
80 # self.dice_value = int(myid.text)
81 # self.dice_slider.value = self.dice_value
82 # if myid == self.skill_text:
84 # self.skill_value = int(myid.text)
86 # myid.background_color = [1, 0.5, 0.5, 1]
88 # myid.background_color = [1, 1, 1, 1]
89 # self.skill_slider.value = self.skill_value
94 for i
in range(self
.dice_value
):
95 if random
.randint(1, 6) <= self
.skill_value
:
97 if self
.result_label
.text
!= INITIAL_RESULT_LABEL
:
98 self
.previous_results_la
.data
= [self
.result_label
.text
] + self
.previous_results_la
.data
99 self
.result_label
.text
= 'Rolled {dice}({skill}) for {succ} successes'.format(dice
=self
.dice_value
, skill
=self
.skill_value
,
103 # def slider_value_change(slider, value):
104 # print("Slider {} changed to {}".format(slider, value))
106 class TenraRollerApp(App
):
107 # dice_slider = ObjectProperty(None)
108 # dice_text = ObjectProperty(None)
109 # dice_value = BoundedNumericProperty(10, min=1, max=30)
112 roller
= TenraRoller()
116 if __name__
== '__main__':
117 TenraRollerApp().run()