refactored plugin api; refactored callback library; added signals library
[xonotic/netradiant.git] / radiant / grid.cpp
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 "signal/signal.h"
32 #include "stringio.h"
33
34 #include "gtkmisc.h"
35 #include "commands.h"
36 #include "preferences.h"
37
38
39
40 Signal0 g_gridChange_callbacks;
41
42 void AddGridChangeCallback(const SignalHandler& handler)
43 {
44   g_gridChange_callbacks.connectLast(handler);
45   handler();
46 }
47
48 void GridChangeNotify()
49 {
50   g_gridChange_callbacks();
51 }
52
53 enum GridPower
54 {
55   GRIDPOWER_0125 = -3,
56   GRIDPOWER_025 = -2,
57   GRIDPOWER_05 = -1,
58   GRIDPOWER_1 = 0,
59   GRIDPOWER_2 = 1,
60   GRIDPOWER_4 = 2,
61   GRIDPOWER_8 = 3,
62   GRIDPOWER_16 = 4,
63   GRIDPOWER_32 = 5,
64   GRIDPOWER_64 = 6,
65   GRIDPOWER_128 = 7,
66   GRIDPOWER_256 = 8,
67 };
68
69
70 typedef const char* GridName;
71 // this must match the GridPower enumeration
72 const GridName g_gridnames[] = {
73   "0.125",
74   "0.25",
75   "0.5",
76   "1",
77   "2",
78   "4",
79   "8",
80   "16",
81   "32",
82   "64",
83   "128",
84   "256",
85 };
86
87 inline GridPower GridPower_forGridDefault(int gridDefault)
88 {
89   return static_cast<GridPower>(gridDefault - 3);
90 }
91
92 inline int GridDefault_forGridPower(GridPower gridPower)
93 {
94   return gridPower + 3;
95 }
96
97 int g_grid_default = GridDefault_forGridPower(GRIDPOWER_8);
98
99 int g_grid_power = GridPower_forGridDefault(g_grid_default);
100
101 int Grid_getPower()
102 {
103   return g_grid_power;
104 }
105
106 inline float GridSize_forGridPower(int gridPower)
107 {
108   return pow(2.0f, gridPower);
109 }
110
111 float g_gridsize = GridSize_forGridPower(g_grid_power);
112
113 float GetGridSize()
114 {
115   return g_gridsize;
116 }
117
118
119 void setGridPower(GridPower power);
120
121 class GridMenuItem
122 {
123   GridPower m_id;
124
125   GridMenuItem(const GridMenuItem& other); // NOT COPYABLE
126   GridMenuItem& operator=(const GridMenuItem& other); // NOT ASSIGNABLE
127 public:
128   ToggleItem m_item;
129
130   GridMenuItem(GridPower id) : m_id(id), m_item(ExportCaller(*this))
131   {
132   }
133   void set()
134   {
135     g_grid_power = m_id;
136     m_item.update();
137     setGridPower(m_id);
138   }
139   typedef MemberCaller<GridMenuItem, &GridMenuItem::set> SetCaller;
140   void active(const BoolImportCallback& importCallback)
141   {
142     importCallback(g_grid_power == m_id);
143   }
144   typedef MemberCaller1<GridMenuItem, const BoolImportCallback&, &GridMenuItem::active> ExportCaller;
145 };
146
147 GridMenuItem g_gridMenu0125(GRIDPOWER_0125);
148 GridMenuItem g_gridMenu025(GRIDPOWER_025);
149 GridMenuItem g_gridMenu05(GRIDPOWER_05);
150 GridMenuItem g_gridMenu1(GRIDPOWER_1);
151 GridMenuItem g_gridMenu2(GRIDPOWER_2);
152 GridMenuItem g_gridMenu4(GRIDPOWER_4);
153 GridMenuItem g_gridMenu8(GRIDPOWER_8);
154 GridMenuItem g_gridMenu16(GRIDPOWER_16);
155 GridMenuItem g_gridMenu32(GRIDPOWER_32);
156 GridMenuItem g_gridMenu64(GRIDPOWER_64);
157 GridMenuItem g_gridMenu128(GRIDPOWER_128);
158 GridMenuItem g_gridMenu256(GRIDPOWER_256);
159
160 void setGridPower(GridPower power)
161 {
162   g_gridsize = GridSize_forGridPower(power);
163
164   g_gridMenu0125.m_item.update();
165   g_gridMenu025.m_item.update();
166   g_gridMenu05.m_item.update();
167   g_gridMenu1.m_item.update();
168   g_gridMenu2.m_item.update();
169   g_gridMenu4.m_item.update();
170   g_gridMenu8.m_item.update();
171   g_gridMenu16.m_item.update();
172   g_gridMenu32.m_item.update();
173   g_gridMenu64.m_item.update();
174   g_gridMenu128.m_item.update();
175   g_gridMenu256.m_item.update();
176   GridChangeNotify();
177 }
178
179 void GridPrev()
180 {
181   if(g_grid_power > GRIDPOWER_0125)
182   {
183     setGridPower(static_cast<GridPower>(--g_grid_power));
184   }
185 }
186
187 void GridNext()
188 {
189   if(g_grid_power < GRIDPOWER_256)
190   {
191     setGridPower(static_cast<GridPower>(++g_grid_power));
192   }
193 }
194
195
196 void Grid_registerCommands()
197 {
198   GlobalCommands_insert("GridDown", FreeCaller<GridPrev>(), Accelerator('['));
199   GlobalCommands_insert("GridUp", FreeCaller<GridNext>(), Accelerator(']'));
200
201   GlobalToggles_insert("SetGrid0.125", GridMenuItem::SetCaller(g_gridMenu0125), ToggleItem::AddCallbackCaller(g_gridMenu0125.m_item));
202   GlobalToggles_insert("SetGrid0.25", GridMenuItem::SetCaller(g_gridMenu025), ToggleItem::AddCallbackCaller(g_gridMenu025.m_item));
203   GlobalToggles_insert("SetGrid0.5", GridMenuItem::SetCaller(g_gridMenu05), ToggleItem::AddCallbackCaller(g_gridMenu05.m_item));
204   GlobalToggles_insert("SetGrid1", GridMenuItem::SetCaller(g_gridMenu1), ToggleItem::AddCallbackCaller(g_gridMenu1.m_item), Accelerator('1'));
205   GlobalToggles_insert("SetGrid2", GridMenuItem::SetCaller(g_gridMenu2), ToggleItem::AddCallbackCaller(g_gridMenu2.m_item), Accelerator('2'));
206   GlobalToggles_insert("SetGrid4", GridMenuItem::SetCaller(g_gridMenu4), ToggleItem::AddCallbackCaller(g_gridMenu4.m_item), Accelerator('3'));
207   GlobalToggles_insert("SetGrid8", GridMenuItem::SetCaller(g_gridMenu8), ToggleItem::AddCallbackCaller(g_gridMenu8.m_item), Accelerator('4'));
208   GlobalToggles_insert("SetGrid16", GridMenuItem::SetCaller(g_gridMenu16), ToggleItem::AddCallbackCaller(g_gridMenu16.m_item), Accelerator('5'));
209   GlobalToggles_insert("SetGrid32", GridMenuItem::SetCaller(g_gridMenu32), ToggleItem::AddCallbackCaller(g_gridMenu32.m_item), Accelerator('6'));
210   GlobalToggles_insert("SetGrid64", GridMenuItem::SetCaller(g_gridMenu64), ToggleItem::AddCallbackCaller(g_gridMenu64.m_item), Accelerator('7'));
211   GlobalToggles_insert("SetGrid128", GridMenuItem::SetCaller(g_gridMenu128), ToggleItem::AddCallbackCaller(g_gridMenu128.m_item), Accelerator('8'));
212   GlobalToggles_insert("SetGrid256", GridMenuItem::SetCaller(g_gridMenu256), ToggleItem::AddCallbackCaller(g_gridMenu256.m_item), Accelerator('9'));
213 }
214
215
216 void Grid_constructMenu(GtkMenu* menu)
217 {
218   create_check_menu_item_with_mnemonic(menu, "Grid0.125", "SetGrid0.125");
219   create_check_menu_item_with_mnemonic(menu, "Grid0.25", "SetGrid0.25");
220   create_check_menu_item_with_mnemonic(menu, "Grid0.5", "SetGrid0.5");
221   create_check_menu_item_with_mnemonic(menu, "Grid1", "SetGrid1");
222   create_check_menu_item_with_mnemonic(menu, "Grid2", "SetGrid2");
223   create_check_menu_item_with_mnemonic(menu, "Grid4", "SetGrid4");
224   create_check_menu_item_with_mnemonic(menu, "Grid8", "SetGrid8");
225   create_check_menu_item_with_mnemonic(menu, "Grid16", "SetGrid16");
226   create_check_menu_item_with_mnemonic(menu, "Grid32", "SetGrid32");
227   create_check_menu_item_with_mnemonic(menu, "Grid64", "SetGrid64");
228   create_check_menu_item_with_mnemonic(menu, "Grid128", "SetGrid128");
229   create_check_menu_item_with_mnemonic(menu, "Grid256", "SetGrid256");
230 }
231
232 void Grid_registerShortcuts()
233 {
234   command_connect_accelerator("ToggleGrid");
235   command_connect_accelerator("GridDown");
236   command_connect_accelerator("GridUp");
237 }
238
239 void Grid_constructPreferences(PreferencesPage& page)
240 {
241   page.appendCombo(
242     "Default grid spacing",
243     g_grid_default,
244     ARRAY_RANGE(g_gridnames)
245   );
246 }
247 void Grid_constructPage(PreferenceGroup& group)
248 {
249   PreferencesPage page(group.createPage("Grid", "Grid Settings"));
250   Grid_constructPreferences(page);
251 }
252 void Grid_registerPreferencesPage()
253 {
254   PreferencesDialog_addSettingsPage(FreeCaller1<PreferenceGroup&, Grid_constructPage>());
255 }
256
257 void Grid_construct()
258 {
259   Grid_registerPreferencesPage();
260
261   g_grid_default = GridDefault_forGridPower(GRIDPOWER_8);
262
263   GlobalPreferenceSystem().registerPreference("GridDefault", IntImportStringCaller(g_grid_default), IntExportStringCaller(g_grid_default));
264
265   g_grid_power = GridPower_forGridDefault(g_grid_default);
266   g_gridsize = GridSize_forGridPower(g_grid_power);
267 }
268
269 void Grid_destroy()
270 {
271 }