Initial version
[tenra-roller.git] / main.py
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
13
14 import re
15 import random
16
17 MAX_DICE = 40
18 MAX_SKILL = 5
19 INITIAL_RESULT_LABEL = "No result"
20
21 class SlavedSlider(Slider):
22 pass
23
24 class SlavedTextInput(TextInput):
25 pat = re.compile('[^0-9]')
26
27 def insert_text(self, substring, from_undo=False):
28 pat = self.pat
29 s = re.sub(pat, '', substring)
30 return super(SlavedTextInput, self).insert_text(s, from_undo=from_undo)
31
32 class PaddedLabel(Label):
33 line_height = 2
34
35 class TenraRoller(BoxLayout):
36
37 max_dice = NumericProperty(MAX_DICE)
38 max_skill = NumericProperty(MAX_SKILL)
39 initial_result_label_text = StringProperty(INITIAL_RESULT_LABEL)
40
41 dice_slider = ObjectProperty(None)
42 dice_text = ObjectProperty(None)
43 dice_value = BoundedNumericProperty(int(MAX_DICE / 2), min=1, max=MAX_DICE)
44
45 skill_slider = ObjectProperty(None)
46 skill_text = ObjectProperty(None)
47 skill_value = BoundedNumericProperty(int(MAX_SKILL / 2), min=1, max=MAX_SKILL)
48
49 result_label = ObjectProperty(None)
50 previous_results = ObjectProperty(None)
51
52 previous_results_la = SimpleListAdapter(data=[],
53 cls=PaddedLabel)
54
55
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)
64
65 def text_value_change(self, myid):
66 print("Text {} changed to {}".format(myid, myid.text))
67 try:
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
74 except ValueError:
75 myid.background_color = [1, 0.5, 0.5, 1]
76 else:
77 myid.background_color = [1, 1, 1, 1]
78
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:
83 # try:
84 # self.skill_value = int(myid.text)
85 # except ValueError:
86 # myid.background_color = [1, 0.5, 0.5, 1]
87 # else:
88 # myid.background_color = [1, 1, 1, 1]
89 # self.skill_slider.value = self.skill_value
90
91
92 def roll_dice(self):
93 successes = 0
94 for i in range(self.dice_value):
95 if random.randint(1, 6) <= self.skill_value:
96 successes += 1
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,
100 succ=successes)
101
102
103 # def slider_value_change(slider, value):
104 # print("Slider {} changed to {}".format(slider, value))
105
106 class TenraRollerApp(App):
107 # dice_slider = ObjectProperty(None)
108 # dice_text = ObjectProperty(None)
109 # dice_value = BoundedNumericProperty(10, min=1, max=30)
110
111 def build(self):
112 roller = TenraRoller()
113 return roller
114
115
116 if __name__ == '__main__':
117 TenraRollerApp().run()