Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 271 lines (223 sloc) 8.037 kb
0991a5c @tbradshaw The GtkRadiant sources as originally released under the GPL license.
tbradshaw authored
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "grid.h"
23
24 #include <math.h>
25 #include <vector>
26 #include <algorithm>
27
28 #include "preferencesystem.h"
29
30 #include "gtkutil/widget.h"
31 #include "stringio.h"
32
33 #include "gtkmisc.h"
34 #include "commands.h"
35 #include "preferences.h"
36
37
38
39 std::vector<Callback> g_gridChange_callbacks;
40
41 void AddGridChangeCallback(const Callback& callback)
42 {
43 g_gridChange_callbacks.push_back(callback);
44 callback();
45 }
46
47 void GridChangeNotify()
48 {
49 std::for_each(g_gridChange_callbacks.begin(), g_gridChange_callbacks.end(), CallbackInvoke());
50 }
51
52 enum GridPower
53 {
54 GRIDPOWER_0125 = -3,
55 GRIDPOWER_025 = -2,
56 GRIDPOWER_05 = -1,
57 GRIDPOWER_1 = 0,
58 GRIDPOWER_2 = 1,
59 GRIDPOWER_4 = 2,
60 GRIDPOWER_8 = 3,
61 GRIDPOWER_16 = 4,
62 GRIDPOWER_32 = 5,
63 GRIDPOWER_64 = 6,
64 GRIDPOWER_128 = 7,
65 GRIDPOWER_256 = 8,
66 };
67
68
69 typedef const char* GridName;
70 // this must match the GridPower enumeration
71 const GridName g_gridnames[] = {
72 "0.125",
73 "0.25",
74 "0.5",
75 "1",
76 "2",
77 "4",
78 "8",
79 "16",
80 "32",
81 "64",
82 "128",
83 "256",
84 };
85
86 inline GridPower GridPower_forGridDefault(int gridDefault)
87 {
88 return static_cast<GridPower>(gridDefault - 3);
89 }
90
91 inline int GridDefault_forGridPower(GridPower gridPower)
92 {
93 return gridPower + 3;
94 }
95
96 int g_grid_default = GridDefault_forGridPower(GRIDPOWER_8);
97
98 int g_grid_power = GridPower_forGridDefault(g_grid_default);
99
100 int Grid_getPower()
101 {
102 return g_grid_power;
103 }
104
105 inline float GridSize_forGridPower(int gridPower)
106 {
107 return pow(2.0f, gridPower);
108 }
109
110 float g_gridsize = GridSize_forGridPower(g_grid_power);
111
112 float GetGridSize()
113 {
114 return g_gridsize;
115 }
116
117
118 void setGridPower(GridPower power);
119
120 class GridMenuItem
121 {
122 GridPower m_id;
123
124 GridMenuItem(const GridMenuItem& other); // NOT COPYABLE
125 GridMenuItem& operator=(const GridMenuItem& other); // NOT ASSIGNABLE
126 public:
127 ToggleItem m_item;
128
129 GridMenuItem(GridPower id) : m_id(id), m_item(ExportCaller(*this))
130 {
131 }
132 void set()
133 {
134 g_grid_power = m_id;
135 m_item.update();
136 setGridPower(m_id);
137 }
138 typedef MemberCaller<GridMenuItem, &GridMenuItem::set> SetCaller;
139 void active(const BoolImportCallback& importCallback)
140 {
141 importCallback(g_grid_power == m_id);
142 }
143 typedef MemberCaller1<GridMenuItem, const BoolImportCallback&, &GridMenuItem::active> ExportCaller;
144 };
145
146 GridMenuItem g_gridMenu0125(GRIDPOWER_0125);
147 GridMenuItem g_gridMenu025(GRIDPOWER_025);
148 GridMenuItem g_gridMenu05(GRIDPOWER_05);
149 GridMenuItem g_gridMenu1(GRIDPOWER_1);
150 GridMenuItem g_gridMenu2(GRIDPOWER_2);
151 GridMenuItem g_gridMenu4(GRIDPOWER_4);
152 GridMenuItem g_gridMenu8(GRIDPOWER_8);
153 GridMenuItem g_gridMenu16(GRIDPOWER_16);
154 GridMenuItem g_gridMenu32(GRIDPOWER_32);
155 GridMenuItem g_gridMenu64(GRIDPOWER_64);
156 GridMenuItem g_gridMenu128(GRIDPOWER_128);
157 GridMenuItem g_gridMenu256(GRIDPOWER_256);
158
159 void setGridPower(GridPower power)
160 {
161 g_gridsize = GridSize_forGridPower(power);
162
163 g_gridMenu0125.m_item.update();
164 g_gridMenu025.m_item.update();
165 g_gridMenu05.m_item.update();
166 g_gridMenu1.m_item.update();
167 g_gridMenu2.m_item.update();
168 g_gridMenu4.m_item.update();
169 g_gridMenu8.m_item.update();
170 g_gridMenu16.m_item.update();
171 g_gridMenu32.m_item.update();
172 g_gridMenu64.m_item.update();
173 g_gridMenu128.m_item.update();
174 g_gridMenu256.m_item.update();
175 GridChangeNotify();
176 }
177
178 void GridPrev()
179 {
180 if(g_grid_power > GRIDPOWER_0125)
181 {
182 setGridPower(static_cast<GridPower>(--g_grid_power));
183 }
184 }
185
186 void GridNext()
187 {
188 if(g_grid_power < GRIDPOWER_256)
189 {
190 setGridPower(static_cast<GridPower>(++g_grid_power));
191 }
192 }
193
194
195 void Grid_registerCommands()
196 {
197 GlobalCommands_insert("GridDown", FreeCaller<GridPrev>(), Accelerator('['));
198 GlobalCommands_insert("GridUp", FreeCaller<GridNext>(), Accelerator(']'));
199
200 GlobalToggles_insert("SetGrid0.125", GridMenuItem::SetCaller(g_gridMenu0125), ToggleItem::AddCallbackCaller(g_gridMenu0125.m_item));
201 GlobalToggles_insert("SetGrid0.25", GridMenuItem::SetCaller(g_gridMenu025), ToggleItem::AddCallbackCaller(g_gridMenu025.m_item));
202 GlobalToggles_insert("SetGrid0.5", GridMenuItem::SetCaller(g_gridMenu05), ToggleItem::AddCallbackCaller(g_gridMenu05.m_item));
203 GlobalToggles_insert("SetGrid1", GridMenuItem::SetCaller(g_gridMenu1), ToggleItem::AddCallbackCaller(g_gridMenu1.m_item), Accelerator('1'));
204 GlobalToggles_insert("SetGrid2", GridMenuItem::SetCaller(g_gridMenu2), ToggleItem::AddCallbackCaller(g_gridMenu2.m_item), Accelerator('2'));
205 GlobalToggles_insert("SetGrid4", GridMenuItem::SetCaller(g_gridMenu4), ToggleItem::AddCallbackCaller(g_gridMenu4.m_item), Accelerator('3'));
206 GlobalToggles_insert("SetGrid8", GridMenuItem::SetCaller(g_gridMenu8), ToggleItem::AddCallbackCaller(g_gridMenu8.m_item), Accelerator('4'));
207 GlobalToggles_insert("SetGrid16", GridMenuItem::SetCaller(g_gridMenu16), ToggleItem::AddCallbackCaller(g_gridMenu16.m_item), Accelerator('5'));
208 GlobalToggles_insert("SetGrid32", GridMenuItem::SetCaller(g_gridMenu32), ToggleItem::AddCallbackCaller(g_gridMenu32.m_item), Accelerator('6'));
209 GlobalToggles_insert("SetGrid64", GridMenuItem::SetCaller(g_gridMenu64), ToggleItem::AddCallbackCaller(g_gridMenu64.m_item), Accelerator('7'));
210 GlobalToggles_insert("SetGrid128", GridMenuItem::SetCaller(g_gridMenu128), ToggleItem::AddCallbackCaller(g_gridMenu128.m_item), Accelerator('8'));
211 GlobalToggles_insert("SetGrid256", GridMenuItem::SetCaller(g_gridMenu256), ToggleItem::AddCallbackCaller(g_gridMenu256.m_item), Accelerator('9'));
212 }
213
214
215 void Grid_constructMenu(GtkMenu* menu)
216 {
217 create_check_menu_item_with_mnemonic(menu, "Grid0.125", "SetGrid0.125");
218 create_check_menu_item_with_mnemonic(menu, "Grid0.25", "SetGrid0.25");
219 create_check_menu_item_with_mnemonic(menu, "Grid0.5", "SetGrid0.5");
220 create_check_menu_item_with_mnemonic(menu, "Grid1", "SetGrid1");
221 create_check_menu_item_with_mnemonic(menu, "Grid2", "SetGrid2");
222 create_check_menu_item_with_mnemonic(menu, "Grid4", "SetGrid4");
223 create_check_menu_item_with_mnemonic(menu, "Grid8", "SetGrid8");
224 create_check_menu_item_with_mnemonic(menu, "Grid16", "SetGrid16");
225 create_check_menu_item_with_mnemonic(menu, "Grid32", "SetGrid32");
226 create_check_menu_item_with_mnemonic(menu, "Grid64", "SetGrid64");
227 create_check_menu_item_with_mnemonic(menu, "Grid128", "SetGrid128");
228 create_check_menu_item_with_mnemonic(menu, "Grid256", "SetGrid256");
229 }
230
231 void Grid_registerShortcuts()
232 {
233 command_connect_accelerator("ToggleGrid");
234 command_connect_accelerator("GridDown");
235 command_connect_accelerator("GridUp");
236 }
237
238 void Grid_constructPreferences(PreferencesPage& page)
239 {
240 page.appendCombo(
241 "Default grid spacing",
242 g_grid_default,
243 ARRAY_RANGE(g_gridnames)
244 );
245 }
246 void Grid_constructPage(PreferenceGroup& group)
247 {
248 PreferencesPage page(group.createPage("Grid", "Grid Settings"));
249 Grid_constructPreferences(page);
250 }
251 void Grid_registerPreferencesPage()
252 {
253 PreferencesDialog_addSettingsPage(FreeCaller1<PreferenceGroup&, Grid_constructPage>());
254 }
255
256 void Grid_construct()
257 {
258 Grid_registerPreferencesPage();
259
260 g_grid_default = GridDefault_forGridPower(GRIDPOWER_8);
261
262 GlobalPreferenceSystem().registerPreference("GridDefault", IntImportStringCaller(g_grid_default), IntExportStringCaller(g_grid_default));
263
264 g_grid_power = GridPower_forGridDefault(g_grid_default);
265 g_gridsize = GridSize_forGridPower(g_grid_power);
266 }
267
268 void Grid_destroy()
269 {
270 }
Something went wrong with that request. Please try again.