forked from gerritvoss/OpenSGToolbox
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path07GridLayout.cpp
More file actions
382 lines (286 loc) · 13.5 KB
/
07GridLayout.cpp
File metadata and controls
382 lines (286 loc) · 13.5 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*************************************************
Welcome to the 07GridLayout tutorial. This
tutorial details how to use the GridLayout
to place components within a frame. The following
components are introduced:
GridLayout
GridLayout provides a simple way to put components
into a grid layout. It allows you to make an X
by Y
The following all are shown and explained
within this tutorial:
Creating BoxLayout
Setting Alignments of Layout
Explanation/Examples on automatic resizing
These tutorials contain potentially relevant
information:
*************************************************/
// OpenSG Tutorial Example: Using the Grid Layout
// to place Components
//
// This tutorial explains how to place ExampleButtons within a
// frame utilizing the Grid Layout command to
// manage the layout through the OSG User Interface library.
//
// Includes: placing multiple ExampleButtons using Grid Layout
// General OpenSG configuration, needed everywhere
#include "OSGConfig.h"
// Methods to create simple geometry: boxes, spheres, tori etc.
#include "OSGSimpleGeometry.h"
// A little helper to simplify scene management and interaction
#include "OSGSimpleSceneManager.h"
#include "OSGNode.h"
#include "OSGGroup.h"
#include "OSGViewport.h"
// The general scene file loading handler
#include "OSGSceneFileHandler.h"
// Input
#include "OSGWindowUtils.h"
//Text Foreground
#include "OSGSimpleTextForeground.h"
//Animation
#include "OSGKeyframeSequences.h"
#include "OSGKeyframeAnimator.h"
#include "OSGFieldAnimation.h"
// UserInterface Headers
#include "OSGUIForeground.h"
#include "OSGInternalWindow.h"
#include "OSGUIDrawingSurface.h"
#include "OSGGraphics2D.h"
#include "OSGLookAndFeelManager.h"
// Activate the OpenSG namespace
OSG_USING_NAMESPACE
// Forward declaration so we can have the interesting stuff upfront
void display(SimpleSceneManager *mgr);
void reshape(Vec2f Size, SimpleSceneManager *mgr);
// 07GridLayout Headers
#include "OSGButton.h"
#include "OSGColorLayer.h"
#include "OSGGridLayout.h"
void keyPressed(KeyEventDetails* const details)
{
if(details->getKey() == KeyEventDetails::KEY_Q && details->getModifiers() & KeyEventDetails::KEY_MODIFIER_COMMAND)
{
dynamic_cast<WindowEventProducer*>(details->getSource())->closeWindow();
}
}
class SimpleScreenDoc
{
public:
SimpleScreenDoc(SimpleSceneManager* SceneManager,
WindowEventProducer* MainWindow);
private:
SimpleTextForegroundRecPtr _DocForeground;
SimpleTextForegroundRecPtr _DocShowForeground;
FieldAnimationRecPtr _ShowDocFadeOutAnimation;
SimpleScreenDoc(void);
SimpleScreenDoc(const SimpleScreenDoc& );
SimpleTextForegroundTransitPtr makeDocForeground(void);
SimpleTextForegroundTransitPtr makeDocShowForeground(void);
void keyTyped(KeyEventDetails* const details);
};
/******************************************************
Documentation Foreground
******************************************************/
SimpleTextForegroundTransitPtr SimpleScreenDoc::makeDocForeground(void)
{
SimpleTextForegroundRecPtr DocForeground = SimpleTextForeground::create();
DocForeground->addLine("This tutorial is a simple demonstration of the use");
DocForeground->addLine("of a \\{\\color=AAAA00FF GridLayout}.");
DocForeground->addLine("");
DocForeground->addLine("\\{\\color=AAAAAAFF Key Commands}:");
DocForeground->addLine(" \\{\\color=AAAAFFFF Cmd+q}: Close the application");
DocForeground->addLine(" \\{\\color=AAAAFFFF ?}: Show/hide this documentation");
return SimpleTextForegroundTransitPtr(DocForeground);
}
int main(int argc, char **argv)
{
// OSG init
osgInit(argc,argv);
{
// Set up Window
WindowEventProducerRecPtr TutorialWindow = createNativeWindow();
TutorialWindow->initWindow();
// Create the SimpleSceneManager helper
SimpleSceneManagerRefPtr sceneManager = SimpleSceneManager::create();
TutorialWindow->setDisplayCallback(boost::bind(display, sceneManager));
TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, sceneManager));
// Tell the Manager what to manage
sceneManager->setWindow(TutorialWindow);
TutorialWindow->connectKeyTyped(boost::bind(keyPressed, _1));
// Make Torus Node (creates Torus in background of scene)
NodeRecPtr TorusGeometryNode = makeTorus(.5, 2, 16, 16);
// Make Main Scene Node and add the Torus
NodeRecPtr scene = Node::create();
scene->setCore(Group::create());
scene->addChild(TorusGeometryNode);
// Create the Graphics
GraphicsRecPtr TutorialGraphics = Graphics2D::create();
// Initialize the LookAndFeelManager to enable default settings
LookAndFeelManager::the()->getLookAndFeel()->init();
/******************************************************
Create Grid Layout. Grid Layout arranges the
objects in a grid, with user specified rows,
columns, and gap size (conceptually imagine that
an invisible grid is drawn, and components are
placed into that grid one per "box").
Objects within the Grid Layout fill from left
to right, and top to bottom, filling in each space
sequentially. The Grid Layout "boxes" are each the
same size as the largest object within the Layout.
Smaller objects are automatically resized to fit
this size unless they have Max/Min sizes assigned
(similar to Box Layout).
You can experiment with this by changing the size of
the Buttons as shown in 01Button, editing the Max/Min
size of the Buttons, or adding more Buttons to the
scene.
Note that if the Frame is too small, the objects will
appear out of the Frame background.
-setRows(int): Determine the number of rows
in the Layout.
-setColumns(int): Determine the number of
columns in the Layout.
-setHorizontalGap(int): Determine the number
of pixels between each column.
setVerticalGap(int): Determine the number
of pixels between each row.
******************************************************/
GridLayoutRecPtr MainInternalWindowLayout = GridLayout::create();
MainInternalWindowLayout->setRows(3);
MainInternalWindowLayout->setColumns(2);
MainInternalWindowLayout->setHorizontalGap(4);
MainInternalWindowLayout->setVerticalGap(4);
/******************************************************
Create and edit some Button Components.
Note that as with BoxLayout, Components
are resized to fit their respective
grid boxes. Unless a MaxSize is set,
this will be the case. This will
override even PreferredSizes (see
ExampleButton3).
******************************************************/
ButtonRecPtr ExampleButton1 = Button::create();
ButtonRecPtr ExampleButton2 = Button::create();
ButtonRecPtr ExampleButton3 = Button::create();
ButtonRecPtr ExampleButton4 = Button::create();
ButtonRecPtr ExampleButton5 = Button::create();
ButtonRecPtr ExampleButton6 = Button::create();
ExampleButton1->setPreferredSize(Vec2f(50,50));
ExampleButton1->setMaxSize(Vec2f(50,50)); //if MaxSize is commented out, this button then will revert to being the same size as the others in the grid.
ExampleButton2->setPreferredSize(Vec2f(200,100)); //<----
// |
ExampleButton3->setPreferredSize(Vec2f(50,100)); //Notice that even though these two differ in size they appear the same on the grid
// Create The Main InternalWindow
// Create Background to be used with the Main InternalWindow
ColorLayerRecPtr MainInternalWindowBackground = ColorLayer::create();
MainInternalWindowBackground->setColor(Color4f(1.0,1.0,1.0,0.5));
InternalWindowRecPtr MainInternalWindow = InternalWindow::create();
MainInternalWindow->pushToChildren(ExampleButton1);
MainInternalWindow->pushToChildren(ExampleButton2);
MainInternalWindow->pushToChildren(ExampleButton3);
MainInternalWindow->pushToChildren(ExampleButton4);
MainInternalWindow->pushToChildren(ExampleButton5);
MainInternalWindow->pushToChildren(ExampleButton6);
MainInternalWindow->setLayout(MainInternalWindowLayout);
MainInternalWindow->setBackgrounds(MainInternalWindowBackground);
MainInternalWindow->setAlignmentInDrawingSurface(Vec2f(0.5f,0.5f));
MainInternalWindow->setScalingInDrawingSurface(Vec2f(0.5f,0.5f));
MainInternalWindow->setDrawTitlebar(false);
MainInternalWindow->setResizable(false);
// Create the Drawing Surface
UIDrawingSurfaceRecPtr TutorialDrawingSurface = UIDrawingSurface::create();
TutorialDrawingSurface->setGraphics(TutorialGraphics);
TutorialDrawingSurface->setEventProducer(TutorialWindow);
TutorialDrawingSurface->openWindow(MainInternalWindow);
// Create the UI Foreground Object
UIForegroundRecPtr TutorialUIForeground = UIForeground::create();
TutorialUIForeground->setDrawingSurface(TutorialDrawingSurface);
// Tell the Manager what to manage
sceneManager->setRoot(scene);
// Add the UI Foreground Object to the Scene
ViewportRecPtr TutorialViewport = sceneManager->getWindow()->getPort(0);
TutorialViewport->addForeground(TutorialUIForeground);
//Create the Documentation Foreground and add it to the viewport
SimpleScreenDoc TheSimpleScreenDoc(sceneManager, TutorialWindow);
// Show the whole Scene
sceneManager->showAll();
//Open Window
Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
TutorialWindow->openWindow(WinPos,
WinSize,
"07GridLayout");
//Enter main Loop
TutorialWindow->mainLoop();
}
osgExit();
return 0;
}
// Callback functions
// Redraw the window
void display(SimpleSceneManager *mgr)
{
mgr->redraw();
}
// React to size changes
void reshape(Vec2f Size, SimpleSceneManager *mgr)
{
mgr->resize(Size.x(), Size.y());
}
SimpleTextForegroundTransitPtr SimpleScreenDoc::makeDocShowForeground(void)
{
SimpleTextForegroundRecPtr DocShowForeground = SimpleTextForeground::create();
DocShowForeground->setSize(20.0f);
DocShowForeground->setBgColor(Color4f(0.0f,0.0f,0.0f,0.0f));
DocShowForeground->setShadowColor(Color4f(0.0f,0.0f,0.0f,0.0f));
DocShowForeground->setBorderColor(Color4f(1.0f,1.0f,1.0f,0.0f));
DocShowForeground->setHorizontalAlign(SimpleTextForeground::Middle);
DocShowForeground->setVerticalAlign(SimpleTextForeground::Top);
DocShowForeground->addLine("Press ? for help.");
return SimpleTextForegroundTransitPtr(DocShowForeground);
}
SimpleScreenDoc::SimpleScreenDoc(SimpleSceneManager* SceneManager,
WindowEventProducer* MainWindow)
{
_DocForeground = makeDocForeground();
_DocForeground->setBgColor(Color4f(0.0f,0.0f,0.0f,0.8f));
_DocForeground->setBorderColor(Color4f(1.0f,1.0f,1.0f,1.0f));
_DocForeground->setTextMargin(Vec2f(5.0f,5.0f));
_DocForeground->setHorizontalAlign(SimpleTextForeground::Left);
_DocForeground->setVerticalAlign(SimpleTextForeground::Top);
_DocForeground->setActive(false);
_DocShowForeground = makeDocShowForeground();
ViewportRefPtr TutorialViewport = SceneManager->getWindow()->getPort(0);
TutorialViewport->addForeground(_DocForeground);
TutorialViewport->addForeground(_DocShowForeground);
MainWindow->connectKeyTyped(boost::bind(&SimpleScreenDoc::keyTyped,
this,
_1));
//Color Keyframe Sequence
KeyframeColorSequenceRecPtr ColorKeyframes = KeyframeColorSequenceColor4f::create();
ColorKeyframes->addKeyframe(Color4f(1.0f,1.0f,1.0f,1.0f),0.0f);
ColorKeyframes->addKeyframe(Color4f(1.0f,1.0f,1.0f,1.0f),5.0f);
ColorKeyframes->addKeyframe(Color4f(1.0f,1.0f,1.0f,0.0f),7.0f);
//Animator
KeyframeAnimatorRecPtr TheAnimator = KeyframeAnimator::create();
TheAnimator->setKeyframeSequence(ColorKeyframes);
//Animation
_ShowDocFadeOutAnimation = FieldAnimation::create();
_ShowDocFadeOutAnimation->setAnimator(TheAnimator);
_ShowDocFadeOutAnimation->setInterpolationType(TBAnimator::LINEAR_INTERPOLATION);
_ShowDocFadeOutAnimation->setCycling(1);
_ShowDocFadeOutAnimation->setAnimatedField(_DocShowForeground,
SimpleTextForeground::ColorFieldId);
_ShowDocFadeOutAnimation->attachUpdateProducer(MainWindow);
_ShowDocFadeOutAnimation->start();
}
void SimpleScreenDoc::keyTyped(KeyEventDetails* const details)
{
switch(details->getKeyChar())
{
case '?':
_DocForeground->setActive(!_DocForeground->getActive());
break;
}
}