forked from gerritvoss/OpenSGToolbox
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path08OverlayLayout.cpp
More file actions
332 lines (246 loc) · 11.5 KB
/
08OverlayLayout.cpp
File metadata and controls
332 lines (246 loc) · 11.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
// OpenSG Tutorial Example: Using the Overlay Layout
// to place Components
//
// This tutorial explains how to place ExampleButtons within a
// frame utilizing the Overlay Layout command to
// manage the layout through the OSG User Interface library.
//
// Includes: placing multiple ExampleButtons using Overlay 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);
// 08OverlayLayout Headers
#include "OSGButton.h"
#include "OSGColorLayer.h"
#include "OSGOverlayLayout.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 an \\{\\color=AAAA00FF OverlayLayout}.");
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();
/******************************************************
Creates some Button components
******************************************************/
ButtonRecPtr ExampleButton1 = Button::create();
ButtonRecPtr ExampleButton2 = Button::create();
ButtonRecPtr ExampleButton3 = Button::create();
ButtonRecPtr ExampleButton4 = Button::create();
ButtonRecPtr ExampleButton5 = Button::create();
ButtonRecPtr ExampleButton6 = Button::create();
/******************************************************
Create OverlayLayout. This layout simply
places all Components within it on top of
each other.
They are placed in reverse order of how they
are added to the MainFrame (Components added
first are rendered last, those added last are
rendered first).
Note: OverlayLayout has no options which
can be set.
******************************************************/
OverlayLayoutRecPtr MainInternalWindowLayout = OverlayLayout::create();
// OverlayLayout has no options to edit!
// NOTHING : )
/******************************************************
Create and edit some Button Components.
******************************************************/
ExampleButton1->setPreferredSize(Vec2f(50,50));
ExampleButton1->setMaxSize(Vec2f(50,50));
ExampleButton2->setPreferredSize(Vec2f(300,50));
ExampleButton3->setPreferredSize(Vec2f(50,300));
// 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,
"08OverlayLayout");
//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;
}
}