-
Notifications
You must be signed in to change notification settings - Fork 2
/
settingsscreen.py
154 lines (125 loc) · 4.67 KB
/
settingsscreen.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
# KIVY
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import FadeTransition
from kivy.properties import *
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
# CUSTOM
from realbutton import RealButton
from slot import SettingsSlot
'''
screen to alter settings. basically a scrollview containing settings slots.
also access kivy settings from here via button.
'''
class SettingsScreen(Screen):
logic = ObjectProperty(None)
mainlayout = ObjectProperty(None)
menubutton = ObjectProperty(None)
settingsbutton = ObjectProperty(None)
settingsview = ObjectProperty(None)
def __init__(self, logic, iconsize, iconratio_x, iconratio_y, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
self.logic = logic
self.iconsize = iconsize
self.iconratio_x = iconratio_x
self.iconratio_y = iconratio_y
self.setting_items = {}
self.build_interface()
def on_enter(self):
'''get settings from logic'''
for key in self.logic.settings:
if key in self.setting_items:
self.setting_items[key].value = self.logic.settings[key]
def on_leave(self):
'''write setting to logic and save to file'''
for key in self.logic.settings:
if key in self.setting_items:
self.logic.settings[key] = self.setting_items[key].value
self.logic.apply_settings()
App.get_running_app().settings.save()
def build_interface(self):
self.mainlayout = FloatLayout()
self.settingsview = ScrollView(
size_hint=(0.7, 0.7),
pos_hint={'x': 0.15, 'y': 0.15}
)
# use gridlayout to put items into scrollview
self.settingslayout = GridLayout(
cols=1,
size_hint_y=None,
spacing=80
)
# magic binding
self.settingslayout.bind(
minimum_height=self.settingslayout.setter('height')
)
controller = self.logic.settings
model = controller.model
# sort items by seqnum
itemlist = [
item for item in sorted(model, key=lambda x: model[x].seqnum)
]
for settings_item in itemlist:
item = model[settings_item]
if not item.show:
continue
settings_type = item.settings_type
widget = None
if settings_type == 'select':
widget = SettingsSlot(
size_hint=(1, None),
setting_type='select',
label_text=item.label,
items=item.values
)
elif settings_type == 'bool':
widget = SettingsSlot(
size_hint=(1, None),
setting_type='bool',
label_text=item.label
)
elif settings_type == 'number':
widget = SettingsSlot(
size_hint=(1, None),
setting_type='number',
label_text=item.label,
setting_max=item.max_value,
setting_min=item.min_value,
setting_value=item.value
)
if not widget:
continue
self.setting_items[settings_item] = widget
self.settingslayout.add_widget(widget)
self.settingsbutton = Button(
text='KivySettings',
size_hint=(1, None),
on_press=self.switchto_kivysettings
)
self.settingslayout.add_widget(self.settingsbutton)
self.menubutton = RealButton(
'./media/icons/arrowleft.png',
'./media/icons/arrowleft.png',
self.switchto_menu,
size_hint=(None, None),
size=(self.iconsize, self.iconsize),
pos_hint={'x': 0, 'y': 0},
source='./media/icons/arrowleft.png',
always_release=True
)
# add settingslayout to scrollview
self.settingsview.add_widget(self.settingslayout)
self.mainlayout.add_widget(self.menubutton)
self.mainlayout.add_widget(self.settingsview)
self.add_widget(self.mainlayout)
def switchto_menu(self, instance):
self.manager.transition = FadeTransition()
self.manager.current = 'menu'
def switchto_credits(self, instance):
self.manager.transition = FadeTransition()
self.manager.current = 'credits'
def switchto_kivysettings(self, instance):
App.get_running_app().open_settings()