forked from jonazpiazu/PTAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLWindowMenu.cc
326 lines (288 loc) · 8.91 KB
/
GLWindowMenu.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2008 Isis Innovation Limited
#include "OpenGL.h"
#include "GLWindowMenu.h"
#include <gvars3/instances.h>
#include <gvars3/GStringUtil.h>
#include <sstream>
using namespace GVars3;
using namespace CVD;
using namespace std;
GLWindowMenu::GLWindowMenu(string sName, string sTitle)
{
msName = sName;
msTitle = sTitle;
GUI.RegisterCommand(msName+".AddMenuButton", GUICommandCallBack, this);
GUI.RegisterCommand(msName+".AddMenuToggle", GUICommandCallBack, this);
GUI.RegisterCommand(msName+".AddMenuSlider", GUICommandCallBack, this);
GUI.RegisterCommand(msName+".AddMenuMonitor", GUICommandCallBack, this);
GUI.RegisterCommand(msName+".ShowMenu", GUICommandCallBack, this);
GV2.Register(mgvnMenuItemWidth, msName+".MenuItemWidth", 90, HIDDEN | SILENT);
GV2.Register(mgvnMenuTextOffset, msName+".MenuTextOffset", 20, HIDDEN | SILENT);
GV2.Register(mgvnEnabled, msName+".Enabled", 1, HIDDEN | SILENT);
mmSubMenus.clear();
msCurrentSubMenu="";
}
GLWindowMenu::~GLWindowMenu()
{
GUI.UnRegisterCommand(msName+".AddMenuButton");
GUI.UnRegisterCommand(msName+".AddMenuToggle");
GUI.UnRegisterCommand(msName+".AddMenuSlider");
GUI.UnRegisterCommand(msName+".AddMenuMonitor");
GUI.UnRegisterCommand(msName+".ShowMenu");
};
void GLWindowMenu::GUICommandCallBack(void *ptr, string sCommand, string sParams)
{
((GLWindowMenu*) ptr)->GUICommandHandler(sCommand, sParams);
}
void GLWindowMenu::GUICommandHandler(string sCommand, string sParams)
{
vector<string> vs = ChopAndUnquoteString(sParams);
if(sCommand==msName+".AddMenuButton")
{
if(vs.size()<3)
{
cout<< "? GLWindowMenu.AddMenuButton: Need 3/4 params: Target Menu, Name, Command , NextMenu=\"\"" << endl;
return;
};
MenuItem m;
m.type = Button;
m.sName = vs[1];
m.sParam = UncommentString(vs[2]);
m.sNextMenu = (vs.size()>3)?(vs[3]):("");
mmSubMenus[vs[0]].mvItems.push_back(m);
return;
}
if(sCommand==msName+".AddMenuToggle")
{
if(vs.size()<3)
{
cout<< "? GLWindowMenu.AddMenuToggle: Need 3/4 params: Target Menu, Name, gvar_int name , NextMenu=\"\"" << endl;
return;
};
MenuItem m;
m.type = Toggle;
m.sName = vs[1];
GV2.Register(m.gvnIntValue, vs[2]);
m.sNextMenu = (vs.size()>3)?(vs[3]):("");
mmSubMenus[vs[0]].mvItems.push_back(m);
return;
}
if(sCommand==msName+".AddMenuMonitor")
{
if(vs.size()<3)
{
cout<< "? GLWindowMenu.AddMenuMonitor: Need 3/4 params: Target Menu, Name, gvar name , NextMenu=\"\"" << endl;
return;
};
MenuItem m;
m.type = Monitor;
m.sName = vs[1];
m.sParam = vs[2];
m.sNextMenu = (vs.size()>3)?(vs[3]):("");
mmSubMenus[vs[0]].mvItems.push_back(m);
return;
}
if(sCommand==msName+".AddMenuSlider")
{
if(vs.size()<5)
{
cout<< "? GLWindowMenu.AddMenuSlider: Need 5/6 params: Target Menu, Name, gvar_int name, min, max, NextMenu=\"\"" << endl;
return;
};
MenuItem m;
m.type = Slider;
m.sName = vs[1];
GV2.Register(m.gvnIntValue, vs[2]);
int *a;
a = ParseAndAllocate<int>(vs[3]);
if(a)
{
m.min = *a;
delete a;
}
a = ParseAndAllocate<int>(vs[4]);
if(a)
{
m.max = *a;
delete a;
}
m.sNextMenu = (vs.size()>5)?(vs[5]):("");
mmSubMenus[vs[0]].mvItems.push_back(m);
return;
}
if(sCommand==msName+".ShowMenu")
{
if(vs.size()==0)
msCurrentSubMenu = "";
else
msCurrentSubMenu = vs[0];
};
};
void GLWindowMenu::LineBox(int l, int r, int t, int b)
{
glBegin(GL_LINE_STRIP);
glVertex2i(l,t);
glVertex2i(l,b);
glVertex2i(r,b);
glVertex2i(r,t);
glVertex2i(l,t);
glEnd();
}
void GLWindowMenu::FillBox(int l, int r, int t, int b)
{
glBegin(GL_QUADS);
glVertex2i(l,t);
glVertex2i(l,b);
glVertex2i(r,b);
glVertex2i(r,t);
glEnd();
}
void GLWindowMenu::Render(int nTop, int nHeight, int nWidth, GLWindow2 &glw)
{
if(!*mgvnEnabled)
return;
mnWidth = nWidth;
mnMenuTop = nTop;
mnMenuHeight = nHeight;
double dAlpha = 0.8;
if(msCurrentSubMenu=="") // No Menu selected - draw little arrow.
{
glColor4d(0,0.5,0,0.5);
FillBox(mnWidth - 30, mnWidth - 1, mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(0,1,0,0.5);
LineBox(mnWidth - 30, mnWidth - 1, mnMenuTop, mnMenuTop + mnMenuHeight);
mnLeftMostCoord = mnWidth - 30;
return;
};
SubMenu &m = mmSubMenus[msCurrentSubMenu];
mnLeftMostCoord = mnWidth - (1 + m.mvItems.size()) * *mgvnMenuItemWidth;
int nBase = mnLeftMostCoord;
for(vector<MenuItem>::reverse_iterator i = m.mvItems.rbegin(); i!= m.mvItems.rend(); i++)
{
switch(i->type)
{
case Button:
glColor4d(0,0.5,0,dAlpha);
FillBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(0,1,0,dAlpha);
LineBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glw.PrintString(ImageRef( nBase + 3, mnMenuTop + *mgvnMenuTextOffset),
i->sName);
break;
case Toggle:
if(*(i->gvnIntValue))
glColor4d(0,0.5,0.5,dAlpha);
else
glColor4d(0.5,0,0,dAlpha);
FillBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
if(*(i->gvnIntValue))
glColor4d(0,1,0.5,dAlpha);
else
glColor4d(1,0,0,dAlpha);
LineBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glw.PrintString(ImageRef( nBase + 3, mnMenuTop + *mgvnMenuTextOffset),
i->sName + " " + ((*(i->gvnIntValue))?("On"):("Off")));
break;
case Monitor:
glColor4d(0,0.5,0.5,dAlpha);
FillBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(0,1,1,dAlpha);
LineBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glw.PrintString(ImageRef( nBase + 3, mnMenuTop + *mgvnMenuTextOffset),
i->sName + " " + GV2.StringValue(i->sParam, true));
break;
case Slider:
{
glColor4d(0.0,0.0,0.5,dAlpha);
FillBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(0.5,0.0,0.5,dAlpha);
double dFrac = (double) (*(i->gvnIntValue) - i->min) / (i->max - i->min);
if(dFrac<0.0)
dFrac = 0.0;
if(dFrac>1.0)
dFrac = 1.0;
FillBox(nBase, (int) (nBase + dFrac * (*mgvnMenuItemWidth +1)), mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(0,1,1,dAlpha);
LineBox(nBase, nBase + *mgvnMenuItemWidth +1, mnMenuTop, mnMenuTop + mnMenuHeight);
ostringstream ost;
ost << i->sName << " " << *(i->gvnIntValue);
glw.PrintString(ImageRef( nBase + 3, mnMenuTop + *mgvnMenuTextOffset),
ost.str());
}
break;
}
nBase += *mgvnMenuItemWidth;
};
glColor4d(0.5, 0.5, 0,dAlpha);
FillBox(mnWidth - *mgvnMenuItemWidth, mnWidth-1, mnMenuTop, mnMenuTop + mnMenuHeight);
glColor4d(1,1,0,dAlpha);
LineBox(mnWidth - *mgvnMenuItemWidth, mnWidth-1, mnMenuTop, mnMenuTop + mnMenuHeight);
ImageRef ir( mnWidth - *mgvnMenuItemWidth + 5, mnMenuTop + *mgvnMenuTextOffset);
if(msCurrentSubMenu == "Root")
glw.PrintString(ir, msTitle+":");
else
glw.PrintString(ir, msCurrentSubMenu+":");
};
bool GLWindowMenu::HandleClick(int nMouseButton, int state, int x, int y)
{
if(!*mgvnEnabled)
return false;
if((y<mnMenuTop)||(y>mnMenuTop + mnMenuHeight))
return false;
if(x<mnLeftMostCoord)
return false;
// if no menu displayed, then must display root menu!
if(msCurrentSubMenu == "")
{
msCurrentSubMenu = "Root";
return true;
};
// Figure out which button was pressed:
int nButtonNumber = (mnWidth - x) / *mgvnMenuItemWidth;
if(nButtonNumber > (int)(mmSubMenus[msCurrentSubMenu].mvItems.size()))
nButtonNumber = 0;
if(nButtonNumber==0) // Clicked on menu name .. . go to root.
{
if(msCurrentSubMenu =="Root")
msCurrentSubMenu = "";
else
msCurrentSubMenu = "Root";
return true;
};
MenuItem SelectedItem = mmSubMenus[msCurrentSubMenu].mvItems[nButtonNumber-1];
msCurrentSubMenu=SelectedItem.sNextMenu;
switch(SelectedItem.type)
{
case Button:
GUI.ParseLine(SelectedItem.sParam);
break;
case Toggle:
*(SelectedItem.gvnIntValue)^=1;
break;
case Slider:
{
if(nMouseButton == GLWindow::BUTTON_WHEEL_UP)
{
*(SelectedItem.gvnIntValue)+=1;
if(*(SelectedItem.gvnIntValue) > SelectedItem.max)
*(SelectedItem.gvnIntValue) = SelectedItem.max;
}
else if(nMouseButton == GLWindow::BUTTON_WHEEL_DOWN)
{
*(SelectedItem.gvnIntValue)-=1;
if(*(SelectedItem.gvnIntValue) < SelectedItem.min)
*(SelectedItem.gvnIntValue) = SelectedItem.min;
}
else
{
int nPos = *mgvnMenuItemWidth - ((mnWidth - x) % *mgvnMenuItemWidth);
double dFrac = (double) nPos / *mgvnMenuItemWidth;
*(SelectedItem.gvnIntValue) = (int)(dFrac * (1.0 + SelectedItem.max - SelectedItem.min)) + SelectedItem.min;
};
}
break;
case Monitor:
break;
};
return true;
};