-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsHandler.py
More file actions
137 lines (105 loc) · 6 KB
/
SettingsHandler.py
File metadata and controls
137 lines (105 loc) · 6 KB
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
import json
class SettingsHandler:
def __init__(self): #loads current settings and sets the pygame_gui theme to the current settings.
self.__settings = self.__load_settings()
self.__update_theme_file()
def __load_settings(self) -> dict: #if settings.json file exists, load it as a dictionary.
try:
with open("settings.json", "r") as f:
settings = json.load(f)
except FileNotFoundError: #if doesn't exist, create a new settings dist.
settings = {
"bg_vol": 100,
"sfx_vol": 100,
"bg_col": (230, 229, 229),
"wall_col": (0, 0, 0),
"btn_col": (156, 150, 150),
"highlight_col": (0, 153, 255),
"player_col": (2, 186, 13),
"enemy_col": (255, 0, 0),
"selected_item": "bg_col"
}
return settings
def __update_theme_file(self): #load the theme.json file as a dictionary, change the dictionary to match the colours in the settings dict, then dump the dictionary into theme.json using the json module.
with open("theme.json", "r") as f:
theme = json.load(f)
theme["#colour_rect"]["colours"]["dark_bg"] = self.__rgb_to_hex(self.__settings[self.__settings["selected_item"]])
theme["label"]["colours"]["normal_text"] = self.__rgb_to_hex(self.__settings["wall_col"])
theme["button"]["colours"]["normal_bg"] = self.__rgb_to_hex(self.__settings["btn_col"])
theme["button"]["colours"]["normal_text"] = self.__rgb_to_hex(self.__settings["wall_col"])
theme["horizontal_slider"]["colours"]["dark_bg"] = self.__rgb_to_hex(self.__settings["btn_col"])
theme["horizontal_slider.#left_button"]["colours"]["normal_bg"] = self.__rgb_to_hex(self.__settings["btn_col"])
theme["horizontal_slider.#left_button"]["colours"]["normal_text"] = self.__rgb_to_hex(self.__settings["wall_col"])
theme["horizontal_slider.#right_button"]["colours"]["normal_bg"] = self.__rgb_to_hex(self.__settings["btn_col"])
theme["horizontal_slider.#right_button"]["colours"]["normal_text"] = self.__rgb_to_hex(self.__settings["wall_col"])
theme["horizontal_slider.#sliding_button"]["colours"]["normal_bg"] = self.__rgb_to_hex(self.__settings["btn_col"])
theme["horizontal_slider.#sliding_button"]["colours"]["normal_text"] = self.__rgb_to_hex(self.__settings["wall_col"])
with open("theme.json", "w") as f:
json.dump(theme, f, indent=4)
f = open("theme.json", "a")
f.close()
def __rgb_to_hex(self, col):
r,g,b = col
return '#%02x%02x%02x' % (r, g, b) #not designed by me.
def set_light_theme(self): #sets colours to the light theme created by me and updates the theme.json file to change the pygame_gui theme.
self.__settings["bg_col"] = (230, 229, 229)
self.__settings["wall_col"] = (0, 0, 0)
self.__settings["btn_col"] = (156, 150, 150)
self.__settings["highlight_col"] = (0, 153, 255)
self.__settings["player_col"] = (2, 186, 13)
self.__settings["enemy_col"] = (255, 0, 0)
self.__update_theme_file()
def set_light_hc_theme(self): #sets colours to the light high contrast theme created by me and updates the theme.json file to change the pygame_gui theme.
self.__settings["bg_col"] = (255, 255, 255)
self.__settings["wall_col"] = (0, 0, 0)
self.__settings["btn_col"] = (255, 255, 255)
self.__settings["highlight_col"] = (240, 228, 66)
self.__settings["player_col"] = (0, 158, 115)
self.__settings["enemy_col"] = (213,94, 0)
self.__update_theme_file()
def set_dark_theme(self): #sets colours to the dark theme created by me and updates the theme.json file to change the pygame_gui theme.
self.__settings["bg_col"] = (97, 97, 97)
self.__settings["wall_col"] = (246, 110, 13)
self.__settings["btn_col"] = (51, 51, 51)
self.__settings["highlight_col"] = (246, 199, 13)
self.__settings["player_col"] = (2, 186, 13)
self.__settings["enemy_col"] = (255, 0, 0)
self.__update_theme_file()
def set_dark_hc_theme(self): #sets colours to the dark high contrast theme created by me and updates the theme.json file to change the pygame_gui theme.
self.__settings["bg_col"] = (0, 0, 0)
self.__settings["wall_col"] = (255, 255, 255)
self.__settings["btn_col"] = (0, 0, 0)
self.__settings["highlight_col"] = (240, 228, 66)
self.__settings["player_col"] = (0, 158, 115)
self.__settings["enemy_col"] = (213,94, 0)
self.__update_theme_file()
def get_bg_volume(self):
return self.__settings["bg_vol"]
def get_sfx_volume(self):
return self.__settings["sfx_vol"]
def set_bg_volume(self, vol):
self.__settings["bg_vol"] = vol
def set_sfx_volume(self, vol):
self.__settings["sfx_vol"] = vol
def get_bg_col(self):
return self.__settings["bg_col"]
def get_wall_col(self):
return self.__settings["wall_col"]
def get_btn_col(self):
return self.__settings["btn_col"]
def get_highlight_col(self):
return self.__settings["highlight_col"]
def get_player_col(self):
return self.__settings["player_col"]
def get_enemy_col(self):
return self.__settings["enemy_col"]
def get_selected_item(self):
return self.__settings["selected_item"]
def get_settings(self):
return self.__settings.copy()
def set_settings(self, settings):
self.__settings = settings
self.__update_theme_file()
def save_settings(self): #save settings by dumping the dictionary in settings.json using the json module. If settings.json does not exist, opening in write mode will create it.
with open("settings.json", "w") as f:
json.dump(self.__settings, f, indent=4)