]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/grid.cpp
be267002a7ec66508bfa6e8fc2259b7731161b1b
[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         g_gridChange_callbacks.connectLast( handler );
44         handler();
45 }
46
47 void GridChangeNotify(){
48         g_gridChange_callbacks();
49 }
50
51 enum GridPower
52 {
53         GRIDPOWER_0125 = -3,
54         GRIDPOWER_025 = -2,
55         GRIDPOWER_05 = -1,
56         GRIDPOWER_1 = 0,
57         GRIDPOWER_2 = 1,
58         GRIDPOWER_4 = 2,
59         GRIDPOWER_8 = 3,
60         GRIDPOWER_16 = 4,
61         GRIDPOWER_32 = 5,
62         GRIDPOWER_64 = 6,
63         GRIDPOWER_128 = 7,
64         GRIDPOWER_256 = 8,
65 };
66
67
68 typedef const char* GridName;
69 // this must match the GridPower enumeration
70 const GridName g_gridnames[] = {
71         "0.125",
72         "0.25",
73         "0.5",
74         "1",
75         "2",
76         "4",
77         "8",
78         "16",
79         "32",
80         "64",
81         "128",
82         "256",
83 };
84
85 inline GridPower GridPower_forGridDefault( int gridDefault ){
86         return static_cast<GridPower>( gridDefault - 3 );
87 }
88
89 inline int GridDefault_forGridPower( GridPower gridPower ){
90         return gridPower + 3;
91 }
92
93 int g_grid_default = GridDefault_forGridPower( GRIDPOWER_8 );
94
95 int g_grid_power = GridPower_forGridDefault( g_grid_default );
96
97 bool g_grid_snap = true;
98
99 int Grid_getPower(){
100         return g_grid_power;
101 }
102
103 inline float GridSize_forGridPower( int gridPower ){
104         return pow( 2.0f, gridPower );
105 }
106
107 float g_gridsize = GridSize_forGridPower( g_grid_power );
108
109 float GetSnapGridSize(){
110         return g_grid_snap ? g_gridsize : 0;
111 }
112
113 float GetGridSize(){
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 void set(){
132         g_grid_power = m_id;
133         m_item.update();
134         setGridPower( m_id );
135 }
136 typedef MemberCaller<GridMenuItem, void(), &GridMenuItem::set> SetCaller;
137 void active( const ImportExportCallback<bool>::Import_t& importCallback ){
138         importCallback( g_grid_power == m_id );
139 }
140 typedef MemberCaller<GridMenuItem, void(const ImportExportCallback<bool>::Import_t&), &GridMenuItem::active> ExportCaller;
141 };
142
143 GridMenuItem g_gridMenu0125( GRIDPOWER_0125 );
144 GridMenuItem g_gridMenu025( GRIDPOWER_025 );
145 GridMenuItem g_gridMenu05( GRIDPOWER_05 );
146 GridMenuItem g_gridMenu1( GRIDPOWER_1 );
147 GridMenuItem g_gridMenu2( GRIDPOWER_2 );
148 GridMenuItem g_gridMenu4( GRIDPOWER_4 );
149 GridMenuItem g_gridMenu8( GRIDPOWER_8 );
150 GridMenuItem g_gridMenu16( GRIDPOWER_16 );
151 GridMenuItem g_gridMenu32( GRIDPOWER_32 );
152 GridMenuItem g_gridMenu64( GRIDPOWER_64 );
153 GridMenuItem g_gridMenu128( GRIDPOWER_128 );
154 GridMenuItem g_gridMenu256( GRIDPOWER_256 );
155
156 void setGridPower( GridPower power ){
157         g_grid_snap = true;
158         g_gridsize = GridSize_forGridPower( power );
159
160         g_gridMenu0125.m_item.update();
161         g_gridMenu025.m_item.update();
162         g_gridMenu05.m_item.update();
163         g_gridMenu1.m_item.update();
164         g_gridMenu2.m_item.update();
165         g_gridMenu4.m_item.update();
166         g_gridMenu8.m_item.update();
167         g_gridMenu16.m_item.update();
168         g_gridMenu32.m_item.update();
169         g_gridMenu64.m_item.update();
170         g_gridMenu128.m_item.update();
171         g_gridMenu256.m_item.update();
172         GridChangeNotify();
173 }
174
175 void GridPrev(){
176         g_grid_snap = true;
177         if ( g_grid_power > GRIDPOWER_0125 ) {
178                 setGridPower( static_cast<GridPower>( --g_grid_power ) );
179         }
180 }
181
182 void GridNext(){
183         g_grid_snap = true;
184         if ( g_grid_power < GRIDPOWER_256 ) {
185                 setGridPower( static_cast<GridPower>( ++g_grid_power ) );
186         }
187 }
188
189 void ToggleGridSnap(){
190         g_grid_snap = !g_grid_snap;
191         GridChangeNotify();
192 }
193
194 void Grid_registerCommands(){
195         GlobalCommands_insert( "GridDown", makeCallbackF(GridPrev), Accelerator( '[' ) );
196         GlobalCommands_insert( "GridUp", makeCallbackF(GridNext), Accelerator( ']' ) );
197
198         GlobalCommands_insert( "ToggleGridSnap", makeCallbackF(ToggleGridSnap) );
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( ui::Menu menu ){
216         create_check_menu_item_with_mnemonic( menu, "Grid0.125", "SetGrid0.125" );
217         create_check_menu_item_with_mnemonic( menu, "Grid0.25", "SetGrid0.25" );
218         create_check_menu_item_with_mnemonic( menu, "Grid0.5", "SetGrid0.5" );
219         create_check_menu_item_with_mnemonic( menu, "Grid1", "SetGrid1" );
220         create_check_menu_item_with_mnemonic( menu, "Grid2", "SetGrid2" );
221         create_check_menu_item_with_mnemonic( menu, "Grid4", "SetGrid4" );
222         create_check_menu_item_with_mnemonic( menu, "Grid8", "SetGrid8" );
223         create_check_menu_item_with_mnemonic( menu, "Grid16", "SetGrid16" );
224         create_check_menu_item_with_mnemonic( menu, "Grid32", "SetGrid32" );
225         create_check_menu_item_with_mnemonic( menu, "Grid64", "SetGrid64" );
226         create_check_menu_item_with_mnemonic( menu, "Grid128", "SetGrid128" );
227         create_check_menu_item_with_mnemonic( menu, "Grid256", "SetGrid256" );
228 }
229
230 void Grid_registerShortcuts(){
231         command_connect_accelerator( "ToggleGrid" );
232         command_connect_accelerator( "GridDown" );
233         command_connect_accelerator( "GridUp" );
234         command_connect_accelerator( "ToggleGridSnap" );
235 }
236
237 void Grid_constructPreferences( PreferencesPage& page ){
238         page.appendCombo(
239                 "Default grid spacing",
240                 g_grid_default,
241                 ARRAY_RANGE( g_gridnames )
242                 );
243 }
244 void Grid_constructPage( PreferenceGroup& group ){
245         PreferencesPage page( group.createPage( "Grid", "Grid Settings" ) );
246         Grid_constructPreferences( page );
247 }
248 void Grid_registerPreferencesPage(){
249         PreferencesDialog_addSettingsPage( makeCallbackF(Grid_constructPage) );
250 }
251
252 void Grid_construct(){
253         Grid_registerPreferencesPage();
254
255         g_grid_default = GridDefault_forGridPower( GRIDPOWER_8 );
256
257         GlobalPreferenceSystem().registerPreference( "GridDefault", IntImportStringCaller( g_grid_default ), IntExportStringCaller( g_grid_default ) );
258
259         g_grid_power = GridPower_forGridDefault( g_grid_default );
260         g_gridsize = GridSize_forGridPower( g_grid_power );
261 }
262
263 void Grid_destroy(){
264 }