/
main.py
823 lines (790 loc) · 35.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# pylint: disable=unused-argument, no-name-in-module, import-error
# pylint: disable=super-on-old-class
'''requires kivy and kivy garden graph'''
from itertools import cycle as itertools_cycle
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.dropdown import DropDown
from kivy.properties import (StringProperty, BooleanProperty,
ObjectProperty, ListProperty)
from kivy.clock import Clock
from kivy.uix.carousel import Carousel
import dicetables as dt
import dt_gui_mvm as mvm
from kivy.garden.graph import MeshLinePlot
INTRO_TEXT = ('this is a platform for finding the probability of dice ' +
'rolls for any set of dice. For example, the chance of ' +
'rolling a 4 with 3 six-sided dice is 3 out of 216.\n\n' +
'Swipe right ===> to get to the add box. pick a die size, ' +
'and pick a number of dice to add. Add as many kinds of ' +
'dice as you want. You can also add a modifier to the die ' +
'(for example 3-sided die +4), or you can make the die a ' +
'weighted die (a 2-sided die with weights 1:3, 2:8 rolls ' +
'a \'one\' 3 times out of every 11 times).\n\n' +
'come back to this window to add or subtract ' +
'dice already added.\n\n' +
'The graph area is for getting a graph of the set of dice. ' +
'It records every set of dice that have been graphed and ' +
'you can reload those dice at any time.\n\n' +
'The stats area will give you the stats of any set of ' +
'rolls you choose. The last window gives you details of ' +
'the raw data.')
### FlashButton, FlashLabel and NumberInput used in various places ###
# kv file line NONE
class FlashButton(Button):
'''a button that flashes for delay_time=0.25 sec after pressed, so you know
you done taht press real clear-like. assign on_press using self.delay OR
make the function it calls use self.delay or you won't see the flash. can
hold info about a die or list for keeping track of stuff.'''
die = ObjectProperty(dt.Die(1))
lst = ListProperty([])
def __init__(self, delay_time=0.25, **kwargs):
super(FlashButton, self).__init__(**kwargs)
self.delay_time = delay_time
self._original_color = self.color
self._original_background = self.background_color
def delay(self, function, *args):
'''delays a function call so that button has time to flash. use with
on_press=self.delay(function, function_args)'''
Clock.schedule_once(lambda delta_time: function(*args),
self.delay_time)
def on_press(self, *args):
'''sets background to flash on press'''
self.color = [1, 0, 0, 1]
self.background_color = [0.2, 0.2, 1, 1]
Clock.schedule_once(self.callback, self.delay_time)
def callback(self, delta_time):
'''sets background to normal'''
self.color = self._original_color
self.background_color = self._original_background
# kv file line NONE
class FlashLabel(Button):
'''a label that flashes for delay_time=0.5 sec when text is added by
add_text. can be turned off with boolean do_flash'''
def __init__(self, delay_time=0.5, **kwargs):
super(FlashLabel, self).__init__(**kwargs)
self.background_normal = ''
self.delay_time = delay_time
self.color = [1, 1, 1, 1]
self.background_color = [0, 0, 0, 0]
def flash_it(self, *args):
'''flashes the label'''
self.color = [1, 0, 0, 1]
self.background_color = [0.2, 0.2, 1, 0.2]
Clock.schedule_once(self.callback, self.delay_time)
def add_text(self, text, do_flash=True):
'''flahes (or not) when text is changed'''
self.text = text
self.color = [1, 1, 1, 1]
self.background_color = [0, 0, 0, 0]
if do_flash:
self.color = [1, 0, 0, 1]
self.background_color = [0.2, 0.2, 1, 0.2]
Clock.schedule_once(self.callback, self.delay_time)
def callback(self, delta_time):
'''reset background and color after delay'''
self.color = [1, 1, 1, 1]
self.background_color = [0, 0, 0, 0]
# kv file line NONE
class NumberInput(Button):
'''a button that opens a number pad for input. fire input events using
on_changed to always fire when pressing enter or on_text to only fire when
the value actually changes. on_changed specifically written for resetting
assigned weights.'''
changed = BooleanProperty(True)
def __init__(self, **kwargs):
super(NumberInput, self).__init__(**kwargs)
pad = StackLayout(orientation='lr-tb')
texts = itertools_cycle(['+', '-', '='])
for digit in range(1, 10):
pad.add_widget(Button(text=str(digit), size_hint=(0.29, 0.25),
on_press=self.add_digit))
if digit % 3 == 0:
pad.add_widget(Button(text=next(texts), size_hint=(0.1, 0.25),
on_press=self.plus_minus))
pad.add_widget(Button(text='<-BS-', size_hint=(0.29, 0.25),
on_press=self.back_space))
pad.add_widget(Button(text='0', size_hint=(0.29, 0.25),
on_press=self.add_digit))
pad.add_widget(Button(text='ENT', size_hint=(0.39, 0.25),
on_press=self.enter_val))
self.num_pad = Popup(title='', content=pad, size_hint=(0.8, 0.5),
pos_hint={'x':0.1, 'y':0})
self.text = ''
self.background_color = (0.4, 0.2, 1.0, 0.8)
self.bind(on_release=self.open_pad)
self.to_add = 0
self.sign = 1
def add_digit(self, btn):
'''fired when 0-9 is pressed'''
if self.num_pad.title == ' ':
self.num_pad.title = btn.text
else:
self.num_pad.title += btn.text
def back_space(self, btn):
'''fired when BS is pressed'''
if self.num_pad.title != ' ':
if self.num_pad.title[:-1]:
self.num_pad.title = self.num_pad.title[:-1]
else:
self.num_pad.title = ' '
def open_pad(self, btn):
'''no new info'''
self.to_add = 0
self.sign = 1
self.num_pad.open(self)
self.num_pad.title = ' '
self.num_pad.title_size = self.num_pad.height/8
def plus_minus(self, btn):
'''uses self.to_add and self.sign to do plus, minus, equals. on equals,
makes title to self.to_add and resets'''
if self.num_pad.title != ' ':
self.to_add += int(self.num_pad.title) * self.sign
if btn.text == '-':
self.sign = -1
if btn.text == '+':
self.sign = 1
if btn.text == '=':
self.num_pad.title = str(self.to_add)
self.to_add = 0
self.sign = 1
else:
self.num_pad.title = ' '
def enter_val(self, btn):
'''when you press enter, always changes changed, you can use it to fire
events'''
self.num_pad.dismiss()
if self.num_pad.title != ' ':
self.text = self.num_pad.title
#pressing enter can fire event with changed.
self.changed = not self.changed
############### AddBox classes ###############
#for weightpopup
# kv file line 5
class NumberSelect(BoxLayout):
'''makes a grid of numbers to choose from'''
def __init__(self, start, stop, **kwargs):
'''start and stop are ints. range of numbers that select displays'''
super(NumberSelect, self).__init__(**kwargs)
self.the_range = range(start, stop + 1)
def open_pad(self, *args):
'''creates a popup pad and opens it'''
pad = SelectPad(self, size_hint=(0.8, 0.5), pos_hint={'x':0.1, 'y':0})
pad.open()
def get_values(self):
'''gets info from numberselect for getting weights'''
return (self.ids['title'].text, int(self.ids['number'].text))
def set_text(self, title, number):
'''title is string, number is int. sets both internal buttons'''
self.ids['title'].text = title
self.ids['number'].text = str(number)
# for NumerberSelect
# kv file line NONE
class SelectPad(Popup):
'''a popup that is called by NumberSelect. creates a number pad of number
choices.'''
def __init__(self, parent_btn, **kwargs):
'''parent_btn is the NumberSelect associated with this select pad'''
super(SelectPad, self).__init__(**kwargs)
self.parent_btn = parent_btn
self.content = StackLayout(orientation='lr-tb', size_hint=(1, 1))
rows, remainder = divmod(len(self.parent_btn.the_range), 3)
rows += bool(remainder)
y_hint_ = 0.01 * (100 // rows)
#y_hint_ = 0.01* int(100 /(1 + len(self.parent_btn.the_range)//3))
for number in self.parent_btn.the_range:
self.content.add_widget(Button(text=str(number),
size_hint=(0.33, y_hint_),
on_press=self.record_number))
self.title = self.parent_btn.ids['title'].text
self.title_align = 'center'
def record_number(self, btn):
'''assigns button's number to parent'''
self.parent_btn.ids['number'].text = btn.text
self.dismiss()
#for AddBox.add_weights and AddBox.record_weights
# kv file line 19
class WeightsPopup(Popup):
'''the popup called when weighting a die'''
def __init__(self, parent_obj, text_list, **kwargs):
'''parent_obj is the owner of the popup where the weights will be
recorded. text_list is a list of strings for NumberSelect titles.'''
super(WeightsPopup, self).__init__(**kwargs)
self.parent_obj = parent_obj
self.pack(text_list)
def pack(self, text_list):
'''sizes popup appropiately and packs with right number of weights'''
spacing = 10.
cols_within_frame = 3
die_size = len(text_list)
col_width = int(self.parent_obj.width / cols_within_frame)
add_drag = False
cols = ((die_size)//10 +1)
if cols > cols_within_frame:
cols = ((die_size+2)//10 +1)
add_drag = True
drag_it = Label(text='DRAG\n====>', bold=True)
height = int(self.parent_obj.height* 0.9)
sz_hint = ((col_width - spacing)/(cols*col_width),
0.1 * (height-spacing)/height)
self.size = (min(1.1 * cols*col_width, self.parent_obj.width),
self.parent_obj.height)
contents = self.ids['contents']
contents.clear_widgets()
contents.size = (cols*col_width*0.88, height)
contents.spacing = spacing
if add_drag:
drag_it.size_hint = sz_hint
contents.add_widget(drag_it)
contents.add_widget(Button(on_press=self.record_weights,
text='record\nweights',
size_hint=sz_hint))
for text in text_list:
weighter = NumberSelect(0, 10, size_hint=sz_hint)
weighter.set_text(text, 1)
contents.add_widget(weighter)
contents.add_widget(Button(on_press=self.record_weights,
text='record\nweights', size_hint=sz_hint))
def record_weights(self, button):
'''records the weights from the weight popup'''
out = []
for child in self.ids['contents'].children[:]:
if isinstance(child, NumberSelect):
out.append(child.get_values())
self.parent_obj.record_weights(out)
self.dismiss()
# kv file line 170
class AddBox(BoxLayout):
'''box for adding new dice. parent app is what's called for dice actions
and info updates. all calls are self.parent_app.request_something(*args).'''
view_model = ObjectProperty(mvm.AddBox(mvm.TableManager()))
def __init__(self, **kwargs):
super(AddBox, self).__init__(**kwargs)
def initialize(self):
'''called at main app init. workaround for kv file loading after py'''
for preset_text in self.view_model.presets:
btn = Button(text=preset_text, on_press=self.assign_size_btn)
self.ids['presets'].add_widget(btn)
self.ids['multiplier'].bind(text=self.assign_multiplier)
self.display_die()
def update(self):
'''called by main app at dice change'''
self.ids['current'].text = (self.view_model.display_current())
def assign_size_btn(self, btn):
'''assigns the die size and die when a preset btn is pushed'''
die_size = int(btn.text[1:])
self.view_model.set_size(die_size)
self.display_die()
def assign_size_text(self, text):
'''asigns the die size and die when text is entered'''
top = 200
bottom = 2
int_string = text
if int_string:
die_size = int(text)
die_size = min(top, max(bottom, die_size))
if text != str(die_size):
self.ids['custom_input'].text = str(die_size)
self.view_model.set_size(die_size)
self.display_die()
def assign_mod(self):
'''assigns a die modifier and new die when slider is moved'''
mod = int(self.ids['modifier'].value)
self.view_model.set_mod(mod)
self.display_die()
def assign_multiplier(self, spinner, text):
'''assigns a die multiplier and new_die based on spinner's text.'''
multiplier = int(text[1:])
self.view_model.set_multiplier(multiplier)
self.display_die()
def display_die(self):
'''all changes to size, mod and weight call this function'''
self.ids['add_it'].clear_widgets()
to_add = self.view_model.display_die()
x_hint = round(1./(len(to_add) + 1), 2)
flash = FlashLabel(text=to_add[0], size_hint=(2*x_hint, 1))
self.ids['add_it'].add_widget(flash)
flash.flash_it()
for add_val in to_add[1:]:
btn = FlashButton(text=add_val, size_hint=(x_hint, 1),
on_press=lambda btn: btn.delay(self.add, btn))
self.ids['add_it'].add_widget(btn)
def add(self, btn):
'''uses btn text and die stored in view_model to add to current table'''
self.view_model.add(int(btn.text))
self.parent.parent.do_update()
def record_weights(self, text_val_lst):
'''takes a list of [('weight for <roll>', int=the_weight)...] and makes
a weighted die with it.'''
self.view_model.record_weights_text(text_val_lst)
self.display_die()
def add_weights(self):
'''opens the weightpopup and sizes accordingly'''
popup = WeightsPopup(self, self.view_model.get_weights_text())
popup.open()
############### ChangeBox has no extra classes ###############
# kv file line 160
class ChangeBox(GridLayout):
'''displays current dice and allows to change. parent app is what's called
for dice actions and info updates. all calls are
self.parent_app.request_something(*args).'''
view_model = ObjectProperty(mvm.ChangeBox(mvm.TableManager()))
def __init__(self, **kwargs):
super(ChangeBox, self).__init__(**kwargs)
self.cols = 1
self.old_dice = []
def add_rm(self, btn):
'''uses die stored in button and btn text to request add or rm'''
self.view_model.add_rm(int(btn.text), btn.die)
self.parent.parent.do_update()
def reset(self, btn):
'''resets current table back to empty and display instructions'''
self.view_model.reset()
self.parent.parent.do_update()
self.clear_widgets()
self.add_widget(Label(text=INTRO_TEXT, text_size=self.size,
valign='top', halign='center'))
def update(self):
'''updates the current dice after add, rm or clear'''
new_dice = []
button_list = self.view_model.display()
self.clear_widgets()
max_height = self.height/10
reset = Button(text='reset table', on_press=self.reset,
size_hint=(1, None), height=0.75*max_height)
self.add_widget(reset)
if button_list:
new_height = min((self.height - reset.height) / len(button_list),
max_height)
for labels, die_ in button_list:
temp = labels[:]
labels = []
for label in temp:
if '50' not in label or 'D' in label:
labels.append(label)
box = BoxLayout(size_hint=(0.8, None), height=new_height,
orientation='horizontal')
self.add_widget(box)
x_hint = round(1./(len(labels) + 2), 2)
for label in labels:
if label[0] == '-' or label[0] == '+':
btn = FlashButton(
text=label, size_hint=(x_hint, 1), die=die_,
on_press=lambda btn: btn.delay(self.add_rm, btn)
)
box.add_widget(btn)
else:
flash = FlashLabel(text=label, size_hint=(3 * x_hint, 1))
box.add_widget(flash)
new_dice.append(label)
if label not in self.old_dice:
Clock.schedule_once(flash.flash_it, 0.01)
self.old_dice = new_dice
############### GraphBox classes ###############
# for PlotPopup legend
# kv file line NONE
class ListButton(Button):
'''simply a button with an object attached'''
lst = ListProperty([])
#for GraphBox.graph_it()
# kv file line 32
class PlotPopup(Popup):
'''popup containing the graph'''
def __init__(self, x_range, y_range, plot_list, **kwargs):
super(PlotPopup, self).__init__(**kwargs)
self._plot_list = plot_list
self.x_range = list(x_range)
self.y_range = [0, y_range[1]]
self.legend = DropDown(dismiss_on_select=False)
self._color_list = [
[0.2, 1.0, 0, 1], [1, 0, 0.2, 1], [0, 0.2, 1, 1],
[0.6, 0, 0.8, 1], [1, 0.4, 0.2, 1], [1, 0.8, 0, 1],
[0.8, 1.0, 0.1, 1]
]
self.make_graph()
self.make_legend()
def make_graph(self):
'''makes a graph and plots'''
colors = itertools_cycle(self._color_list)
y_ticks = [0.05, 0.1, 0.2, 0.5, 1, 5, 10]
x_ticks = [1, 2, 5, 10, 20, 30, 50, 100, 200,
300, 500, 1000, 2000, 5000]
for text_pts in self._plot_list:
color_ = next(colors)
self.ids['graph'].add_plot(MeshLinePlot(points=text_pts[1],
color=color_))
#get numbers for graph ticks
x_tick_num = (self.x_range[1]-self.x_range[0])/9.
for tick in x_ticks:
if x_tick_num < tick:
x_tick_num = tick
break
y_tick_num = (self.y_range[1]-self.y_range[0])/20.
for tick in y_ticks:
if y_tick_num < tick:
y_tick_num = tick
break
self.x_range[0] -= self.x_range[0] % x_tick_num
#adjust font size according to tick numbers
current = self.ids['graph'].font_size
font_factor = 1.
if x_tick_num > 49:
font_factor = 0.75
if x_tick_num > 99:
font_factor = 0.66
if x_tick_num > 499:
font_factor = 0.5
self.ids['graph'].font_size = int(font_factor * current)
self.ids['graph'].x_ticks_major = x_tick_num
self.ids['graph'].y_ticks_major = y_tick_num
self.ids['graph'].xmin = self.x_range[0]
self.ids['graph'].ymin = -y_tick_num
self.ids['graph'].xmax = self.x_range[1]
self.ids['graph'].ymax = self.y_range[1]
def make_legend(self):
'''created the dropdown menu that's called by 'legend' button'''
colors = itertools_cycle(self._color_list)
for text, pts in self._plot_list:
btn = ListButton(text=text, size_hint=(None, None),
height=80, lst=pts, color=next(colors),
valign='middle')
btn.bind(on_release=self.legend.select)
self.legend.add_widget(btn)
self.legend.on_select = self.flash_plot
self.ids['legend'].bind(on_release=self.legend.open)
self.ids['legend'].bind(on_release=self.resize)
self.legend.bind(on_dismiss=self.shrink_button)
def shrink_button(self, event):
'''make legend button small again after dismiss drop down'''
self.ids['legend'].width = self.ids['legend'].texture_size[0]
def resize(self, *args):
'''on release, resize drop down to fit widest button'''
widths = [self.ids['legend'].texture_size[0]]
for btn in self.legend.children[0].children:
#height control for multiline buttons
single_line_ht = btn.texture_size[1]
lines, remainder = divmod(int(btn.texture_size[0] + 10),
int(self.parent.width))
lines += bool(remainder)
#make long btn.text multiline
split_at, remainder = divmod(len(btn.text), lines)
split_at += bool(remainder)
new_text_lst = []
copy = btn.text
while len(copy) > split_at:
new_text_lst.append(copy[:split_at])
copy = copy[split_at:]
new_text_lst.append(copy)
btn.text = '\n'.join(new_text_lst)
btn.width = min(btn.texture_size[0] + 10, self.parent.width)
btn.height = max(self.ids['legend'].height, single_line_ht * lines)
widths.append(btn.width)
self.ids['legend'].width = max(widths)
def flash_plot(self, btn, second_time=False, flash_time=0.5):
'''on press, highlight selected graph'''
for plot in self.ids['graph'].plots:
if plot.points == btn.lst:
#temp_color = [1, 1, 1, 1]
temp_color = [1-val for val in btn.color]
temp_color[3] = 1
self.ids['graph'].remove_plot(plot)
new_plot = MeshLinePlot(points=btn.lst, color=temp_color)
self.ids['graph'].add_plot(new_plot)
if second_time:
Clock.schedule_once(
lambda dt: self._callback(btn, flash_time, second_time=True),
flash_time)
else:
Clock.schedule_once(lambda dt: self._callback(btn, flash_time),
flash_time)
def _callback(self, btn, flash_time, second_time=False):
'''resets graph to original color'''
for plot in self.ids['graph'].plots:
if plot.points == btn.lst:
plot.color = btn.color
if not second_time:
Clock.schedule_once(lambda dt: self.flash_plot(btn, True),
flash_time)
# for displaying history choices to graph
# kv file line 61
class PlotCheckBox(BoxLayout):
'''a checkbox with associated label and function to return label if box
checked'''
tuple_list = ObjectProperty([(0, 1)])
text = StringProperty('')
active = BooleanProperty(False)
def __init__(self, reloader=True, **kwargs):
super(PlotCheckBox, self).__init__(**kwargs)
self.ids['check_box'].bind(active=self._change_active)
self.bind(text=self.split_text)
def _change_active(self, checkbox, value):
'''a helper function to bind checkbox active to main active'''
self.active = self.ids['check_box'].active
def split_text(self, instance, text, split_char='\\'):
'''makes a new two-line display label while preserving original in'''
cut_off = 30
if len(self.text) <= cut_off:
self.ids['label'].text = text
else:
line_1 = text[:len(self.text)/2]
line_2 = text[len(self.text)/2:].replace(split_char, '\n', 1)
self.ids['label'].text = (line_1 + line_2)
# kv file line 273
class GraphBox(BoxLayout):
'''buttons for making graphs. parent app is what's called for dice actions
and info updates. all calls are self.parent_app.request_something(*args).'''
view_model = ObjectProperty(mvm.GraphBox(mvm.TableManager(),
mvm.HistoryManager(), True))
def __init__(self, **kwargs):
super(GraphBox, self).__init__(**kwargs)
self.confirm = Popup(title='Delete everything?', content=BoxLayout(),
size_hint=(0.8, 0.4), title_align='center',
title_size=75)
self.confirm.content.add_widget(Button(text='EVERY\nTHING!!!',
on_press=self.clear_all,
texture_size=self.size))
self.confirm.content.add_widget(Button(text='never\nmind',
on_press=self.confirm.dismiss))
def initialize(self):
'''called at main app init. workaround for kv file loading after py'''
self.ids['graph_space'].add_widget(PlotCheckBox(size_hint=(1, 0.5),
parent_obj=self))
self.update()
def update(self):
'''updates the current window to display new graph history and current
table to graph'''
current, history = self.view_model.display()
#sz_hint for 'past graphs' label to take up all the space
#base_y make sure other widgets fit
rows = len(history) + 3
base_y = .99/rows
if base_y > 0.1:
base_y = 0.1
sz_hint = (1, 1 - (rows - 1) * base_y)
self.ids['graph_space'].clear_widgets()
self.ids['graph_space'].add_widget(Label(text='past graphs',
halign='center',
size_hint=sz_hint))
for text_, tuple_list_ in history:
check = PlotCheckBox(size_hint=(0.79, base_y), active=False,
tuple_list=tuple_list_)
reload_ = FlashButton(
size_hint=(0.2, base_y), lst=[text_, tuple_list_], max_lines=1,
text='reload', valign='middle', halign='center',
on_press=lambda btn: btn.delay(self.reload, btn)
)
self.ids['graph_space'].add_widget(check)
self.ids['graph_space'].add_widget(reload_)
check.text = text_
self.ids['graph_space'].add_widget(Label(text='new table',
size_hint=(1, base_y)))
check = PlotCheckBox(size_hint=(1, base_y), active=True,
tuple_list=current[1])
self.ids['graph_space'].add_widget(check)
check.text = current[0]
Clock.schedule_once(lambda dt: check.ids['label'].flash_it(), 0.01)
def reload(self, btn):
'''reloads from history to current table'''
self.view_model.reload(btn.lst[0], btn.lst[1])
self.parent.parent.do_update()
def graph_it(self):
'''prepares plot and calls PlotPopup'''
to_plot = []
for item in self.ids['graph_space'].children[:]:
if isinstance(item, PlotCheckBox):
if item.active:
to_plot.append((item.text, item.tuple_list))
plots = self.view_model.graph_it(to_plot)
self.update()
if plots[2]:
plotter = PlotPopup(*plots)
plotter.open()
def clear_all(self, btn):
'''clear graph history'''
self.confirm.dismiss()
self.view_model.clear_all()
self.update()
def clear_selected(self):
'''clear selected checked items from graph history'''
to_clear = []
for item in self.ids['graph_space'].children[1:]:
if isinstance(item, PlotCheckBox):
if item.active:
to_clear.append((item.text, item.tuple_list))
self.view_model.clear_selected(to_clear)
self.update()
############### StatBox has no extra classes ###############
# kv file line 311
class StatBox(BoxLayout):
'''box for getting and displaying stats about rolls. parent app is what's
called for dice actions and info updates. all calls are
self.parent_app.request_something(*args).'''
view_model = ObjectProperty(mvm.StatBox(mvm.TableManager()))
def __init__(self, **kwargs):
super(StatBox, self).__init__(**kwargs)
def display_stats(self, stat_text, vals):
'''takes a stat text and two values, and displays them.'''
self.ids['stat_text'].text = stat_text
self.ids['slider_1'].value = vals[0]
self.ids['slider_2'].value = vals[1]
def update(self):
'''called when dice list changes.'''
val_1 = int(self.ids['slider_1'].value)
val_2 = int(self.ids['slider_2'].value)
info_text, stat_text, vals, min_max = self.view_model.display(val_1,
val_2)
self.ids['info_text'].text = info_text
self.ids['slider_1'].max = self.ids['slider_2'].max = min_max[1]
self.ids['slider_1'].min = self.ids['slider_2'].min = min_max[0]
self.display_stats(stat_text, vals)
def assign_text_value(self):
'''called by text_input to assign that value to sliders and
show stats'''
val_1 = int(self.ids['slider_1_text'].text.replace(',', ''))
val_2 = int(self.ids['slider_2_text'].text.replace(',', ''))
self.ids['slider_1'].value = val_1
self.ids['slider_2'].value = val_2
def assign_slider_value(self):
'''the main function. displays stats of current slider values.'''
val_1 = int(self.ids['slider_1'].value)
val_2 = int(self.ids['slider_2'].value)
self.display_stats(*self.view_model.display_stats(val_1, val_2))
############### InfoBox classes ###############
#for InfoBox displays
# kv file line 87
class PageBox(BoxLayout):
'''a box to display pages and buttons and slider to move through them.
parent_obj is the top-level owner of the box that it accepts changes from'''
parent_obj = ObjectProperty(BoxLayout)
def __init__(self, **kwargs):
'''inits the same as a boxlayout'''
super(PageBox, self).__init__(**kwargs)
def reset_sizes(self, ratios):
'''reset font_size = f_size,
[title ratio, , slider ratio, button ratio, text ratio] = ratios'''
self.ids['page_box_title'].size_hint_y = ratios[0]
self.ids['buttons_container'].size_hint_y = ratios[1]
self.ids['text_shell'].size_hint_y = ratios[2]
def set_title(self, title):
'''title is a string. sets the title of the box.'''
self.ids['page_box_title'].text = title
def get_lines_number(self, fudge_factor=0.83):
'''returns an int. the number of lines the pagebox can display'''
return int(self.ids['text_container'].height * fudge_factor/
float(self.ids['text_container'].font_size))
def set_text(self, page, page_num, total_pages):
'''sets displays according page=str-text to display, page_num=int-
current page, total_pages=int'''
self.ids['text_container'].text = page
self.ids['text_container'].text_size = self.ids['text_container'].size
self.ids['pages'].text = '{}/{}'.format(page_num, total_pages)
#in order to reverse slider movement,the value used by slider (having
#the same max and min as the pages) is the opposite
slider_val = total_pages + 1 - page_num
self.ids['choose'].max = total_pages
self.ids['choose'].value = slider_val
# kv file line 382
class InfoBox(BoxLayout):
'''displays basic info about the die.'''
view_model = ObjectProperty(mvm.InfoBox(mvm.TableManager()))
def __init__(self, **kwargs):
super(InfoBox, self).__init__(**kwargs)
def initialize(self):
'''called at main app init. workaround for kv file loading after py'''
self.ids['full_text'].set_title(
'here are all the rolls and their frequency'
)
self.ids['full_text'].ids['page_box_title'].font_size *= 0.75
self.ids['weights_info'].reset_sizes([0.1, 0.1, 0.80])
self.ids['weights_info'].set_title('full weight info')
def choose(self, slider, key):
'''chooses a page for pagebox with key=string-which box to display in.
value=int-page number.'''
lines = self.ids[key].get_lines_number()
#reversing the slider
page = int(slider.max) + int(slider.min) - int(slider.value)
self.ids[key].set_text(*self.view_model.display_chosen_page(page,
key, lines))
def previous(self, key):
'''displays previous page and updates view for pagebox[key=string]'''
lines = self.ids[key].get_lines_number()
self.ids[key].set_text(*self.view_model.display_previous_page(key,
lines))
def next(self, key):
'''displays next page and updates view for pagebox[key=string]'''
lines = self.ids[key].get_lines_number()
self.ids[key].set_text(*self.view_model.display_next_page(key, lines))
def update(self):
'''updates views for all parts of the box.'''
all_info = self.view_model.display_paged(
self.ids['weights_info'].get_lines_number(),
self.ids['full_text'].get_lines_number()
)
self.ids['general_info'].text = all_info[0]
self.ids['table_str'].text = all_info[1]
self.ids['weights_info'].set_text(*all_info[2])
self.ids['full_text'].set_text(*all_info[3])
# kv file line 420
class DicePlatform(Carousel):
'''the main box. the parent_app.'''
def __init__(self, **kwargs):
super(DicePlatform, self).__init__(**kwargs)
self.direction = 'right'
self.loop = 'true'
table = mvm.TableManager()
history = mvm.HistoryManager()
self._read_hist_msg = history.read_history()
change = mvm.ChangeBox(table)
add = mvm.AddBox(table)
stat = mvm.StatBox(table)
graph = mvm.GraphBox(table, history, False)
info = mvm.InfoBox(table)
self.ids['change_box'].view_model = change
self.ids['add_box'].view_model = add
self.ids['stat_box'].view_model = stat
self.ids['graph_box'].view_model = graph
self.ids['info_box'].view_model = info
self.initializer()
def initializer(self):
'''initializes various values that couldn't be written before both .py
file and .kv file were called'''
self.ids['add_box'].initialize()
self.ids['graph_box'].initialize()
self.ids['info_box'].initialize()
if self._read_hist_msg == 'ok':
header = ('IF YOU GO TO THE GRAPH AREA,\n'+
'YOU\'LL FIND YOUR PREVIOUS HISTORY\n\n')
elif self._read_hist_msg in ['ok: no history', 'error: no file']:
header = ''
else:
header = ('TRIED TO LOAD HISTORY BUT\nTHE FILE HAD AN ERROR\n'+
'whatcha gonna do about it? cry?\n\n')
self.ids['change_box'].ids['intro'].text = header + INTRO_TEXT
def do_update(self):
'''updates appropriate things for any die add or remove'''
self.ids['change_box'].update()
self.ids['add_box'].update()
self.ids['stat_box'].update()
self.ids['graph_box'].update()
self.ids['info_box'].update()
# kv file line NONE
class DiceCarouselApp(App):
'''the app. it's the dice platform'''
def build(self):
'''builds app'''
current_app = DicePlatform()
return current_app
def on_pause(self):
'''allows pausing on android'''
return True
def on_resume(self):
'''required with on_pause()'''
pass
if __name__ == '__main__':
RUNNING_APP = DiceCarouselApp()
RUNNING_APP.run()