Debian Codegraph @

src/properties.rs

raw symbols SCIP
1 /*
2 * Copyright 2020-2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General pub License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General pub License for more details.
13 *
14 * You should have received a copy of the GNU General pub License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use crate::{
21 controls::{key_from_i32, key_into_i32},
22 game::{MoveSafety, PlayerCommand},
23 utils::SettingsWarnExt,
24 };
25 use gtk::{gdk, gio, gio::prelude::*};
26 use std::marker::PhantomData;
27
28 const BACKGROUND_COLOR: &str = "background-color";
29 const GAME_CONFIGURATION: &str = "configuration";
30 const ENABLE_SOUND: &str = "enable-sound";
31 const SAFE_MOVES: &str = "use-safe-moves";
32 const SUPER_SAFE_MOVES: &str = "use-super-safe-moves";
33 const THEME: &str = "theme";
34
35 #[derive(Clone, Copy)]
36 pub struct ControlKey {
37 pub settings_key: &'static str,
38 pub command: PlayerCommand,
39 _seal: PhantomData<()>,
40 }
41
42 impl ControlKey {
43 const fn new(settings_key: &'static str, command: PlayerCommand) -> Self {
44 Self {
45 settings_key,
46 command,
47 _seal: PhantomData,
48 }
49 }
50 }
51
52 pub const CONTROL_KEY_NW: ControlKey = ControlKey::new("key00", PlayerCommand::NW);
53 pub const CONTROL_KEY_N: ControlKey = ControlKey::new("key01", PlayerCommand::N);
54 pub const CONTROL_KEY_NE: ControlKey = ControlKey::new("key02", PlayerCommand::NE);
55 pub const CONTROL_KEY_W: ControlKey = ControlKey::new("key03", PlayerCommand::W);
56 pub const CONTROL_KEY_HOLD: ControlKey = ControlKey::new("key04", PlayerCommand::Stay);
57 pub const CONTROL_KEY_E: ControlKey = ControlKey::new("key05", PlayerCommand::E);
58 pub const CONTROL_KEY_SW: ControlKey = ControlKey::new("key06", PlayerCommand::SW);
59 pub const CONTROL_KEY_S: ControlKey = ControlKey::new("key07", PlayerCommand::S);
60 pub const CONTROL_KEY_SE: ControlKey = ControlKey::new("key08", PlayerCommand::SE);
61
62 pub const CONTROL_KEYS: &[&ControlKey] = &[
63 &CONTROL_KEY_NW,
64 &CONTROL_KEY_N,
65 &CONTROL_KEY_NE,
66 &CONTROL_KEY_W,
67 &CONTROL_KEY_HOLD,
68 &CONTROL_KEY_E,
69 &CONTROL_KEY_SW,
70 &CONTROL_KEY_S,
71 &CONTROL_KEY_SE,
72 ];
73
74 pub trait Properties {
75 fn safe_moves(&self) -> bool;
76 fn set_safe_moves(&self, value: bool);
77 fn super_safe_moves(&self) -> bool;
78 fn set_super_safe_moves(&self, value: bool);
79 fn sound(&self) -> bool;
80 fn set_sound(&self, value: bool);
81 fn bgcolour(&self) -> gdk::RGBA;
82 fn set_bgcolour(&self, value: gdk::RGBA);
83 fn selected_config(&self) -> String;
84 fn set_selected_config(&self, value: &str);
85 fn theme(&self) -> String;
86 fn set_theme(&self, value: &str);
87
88 fn get_control_key(&self, k: &ControlKey) -> gdk::Key;
89 fn set_control_key(&self, k: &ControlKey, key: gdk::Key);
90 fn reset_control_key(&self, k: &ControlKey);
91 fn reset_all_control_keys(&self) {
92 self.reset_control_key(&CONTROL_KEY_NW);
93 self.reset_control_key(&CONTROL_KEY_N);
94 self.reset_control_key(&CONTROL_KEY_NE);
95 self.reset_control_key(&CONTROL_KEY_W);
96 self.reset_control_key(&CONTROL_KEY_HOLD);
97 self.reset_control_key(&CONTROL_KEY_E);
98 self.reset_control_key(&CONTROL_KEY_SW);
99 self.reset_control_key(&CONTROL_KEY_S);
100 self.reset_control_key(&CONTROL_KEY_SE);
101 }
102
103 fn move_safety(&self) -> MoveSafety {
104 match (self.safe_moves(), self.super_safe_moves()) {
105 (_, true) => MoveSafety::SuperSafe,
106 (true, false) => MoveSafety::Safe,
107 _ => MoveSafety::Unsafe,
108 }
109 }
110 }
111
112 impl Properties for gio::Settings {
113 fn safe_moves(&self) -> bool {
114 self.boolean(SAFE_MOVES)
115 }
116
117 fn set_safe_moves(&self, value: bool) {
118 self.set_boolean_or_warn(SAFE_MOVES, value);
119 }
120
121 fn super_safe_moves(&self) -> bool {
122 self.boolean(SUPER_SAFE_MOVES)
123 }
124
125 fn set_super_safe_moves(&self, value: bool) {
126 self.set_boolean_or_warn(SUPER_SAFE_MOVES, value);
127 }
128
129 fn sound(&self) -> bool {
130 self.boolean(ENABLE_SOUND)
131 }
132
133 fn set_sound(&self, value: bool) {
134 self.set_boolean_or_warn(ENABLE_SOUND, value);
135 }
136
137 fn bgcolour(&self) -> gdk::RGBA {
138 gdk::RGBA::parse(self.string(BACKGROUND_COLOR)).unwrap_or(gdk::RGBA::BLACK)
139 }
140
141 fn set_bgcolour(&self, value: gdk::RGBA) {
142 self.set_string_or_warn(BACKGROUND_COLOR, &value.to_string());
143 }
144
145 fn selected_config(&self) -> String {
146 self.string(GAME_CONFIGURATION).into()
147 }
148
149 fn set_selected_config(&self, value: &str) {
150 self.set_string_or_warn(GAME_CONFIGURATION, value);
151 }
152
153 fn theme(&self) -> String {
154 self.string(THEME).into()
155 }
156
157 fn set_theme(&self, value: &str) {
158 self.set_string_or_warn(THEME, value);
159 }
160
161 fn get_control_key(&self, k: &ControlKey) -> gdk::Key {
162 key_from_i32(self.int(k.settings_key))
163 }
164
165 fn set_control_key(&self, k: &ControlKey, key: gdk::Key) {
166 self.set_int_or_warn(k.settings_key, key_into_i32(key));
167 }
168
169 fn reset_control_key(&self, k: &ControlKey) {
170 self.reset(k.settings_key);
171 }
172 }