/
Widget.cpp
443 lines (378 loc) · 14.1 KB
/
Widget.cpp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Geode>
#include <osg/ScriptEngine>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/ValueObject>
#include <osg/io_utils>
#include <osgUI/Widget>
#include <osgGA/EventVisitor>
#include <osgGA/GUIActionAdapter>
#include <algorithm>
using namespace osgUI;
Widget::Widget():
_focusBehaviour(FOCUS_FOLLOWS_POINTER),
_hasEventFocus(false),
_graphicsInitialized(false),
_visible(true),
_enabled(true)
{
setNumChildrenRequiringEventTraversal(1);
}
Widget::Widget(const Widget& widget, const osg::CopyOp& copyop):
osg::Group(),
_focusBehaviour(widget._focusBehaviour),
_hasEventFocus(false),
_graphicsInitialized(false),
_alignmentSettings(osg::clone(widget._alignmentSettings.get(), copyop)),
_frameSettings(osg::clone(widget._frameSettings.get(), copyop)),
_textSettings(osg::clone(widget._textSettings.get(), copyop)),
_visible(widget._visible),
_enabled(widget._enabled)
{
setNumChildrenRequiringEventTraversal(1);
}
void Widget::setExtents(const osg::BoundingBoxf& bb)
{
_extents = bb;
}
void Widget::updateFocus(osg::NodeVisitor& nv)
{
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(&nv);
osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0;
if (ev && aa)
{
// OSG_NOTICE<<"updateFocus"<<std::endl;
osgGA::EventQueue::Events& events = ev->getEvents();
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
osgGA::GUIEventAdapter* ea = (*itr)->asGUIEventAdapter();
if (ea)
{
bool previousFocus = _hasEventFocus;
if (_focusBehaviour==CLICK_TO_FOCUS)
{
if (ea->getEventType()==osgGA::GUIEventAdapter::PUSH)
{
int numButtonsPressed = 0;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) ++numButtonsPressed;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) ++numButtonsPressed;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) ++numButtonsPressed;
if (numButtonsPressed==1)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
bool withinWidget = aa->computeIntersections(*ea, nv.getNodePath(), intersections);
if (withinWidget) _hasEventFocus = true;
else _hasEventFocus = false;
}
}
}
else if (_focusBehaviour==FOCUS_FOLLOWS_POINTER)
{
bool checkWithinWidget = false;
if (!_hasEventFocus)
{
checkWithinWidget = (ea->getEventType()!=osgGA::GUIEventAdapter::FRAME) && ea->getButtonMask()==0;
}
else
{
// if mouse move or mouse release check to see if mouse still within widget to retain focus
if (ea->getEventType()==osgGA::GUIEventAdapter::MOVE)
{
checkWithinWidget = true;
}
else if (ea->getEventType()==osgGA::GUIEventAdapter::RELEASE)
{
// if no buttons pressed then check
if (ea->getButtonMask()==0) checkWithinWidget = true;
}
}
if (checkWithinWidget)
{
#if 0
osgUtil::LineSegmentIntersector::Intersections intersections;
bool withinWidget = aa->computeIntersections(*ea, nv.getNodePath(), intersections);
#else
Intersections intersections;
bool withinWidget = computeIntersections( ev, ea, intersections);
#endif
_hasEventFocus = withinWidget;
}
}
if (previousFocus != _hasEventFocus)
{
if (_hasEventFocus)
{
enter();
#if 0
if (view->getCameraManipulator())
{
view->getCameraManipulator()->finishAnimation();
view->requestContinuousUpdate( false );
}
#endif
}
else
{
leave();
}
}
}
}
}
}
void Widget::setHasEventFocus(bool focus)
{
if (_hasEventFocus == focus) return;
_hasEventFocus = focus;
if (_hasEventFocus) enter();
else leave();
}
bool Widget::getHasEventFocus() const
{
return _hasEventFocus;
}
void Widget::enter()
{
if (!runCallbacks("enter")) enterImplementation();
}
void Widget::enterImplementation()
{
OSG_NOTICE<<"Widget::enter()"<<std::endl;
}
void Widget::leave()
{
if (!runCallbacks("leave")) leaveImplementation();
}
void Widget::leaveImplementation()
{
OSG_NOTICE<<"Widget::leave()"<<std::endl;
}
void Widget::traverse(osg::NodeVisitor& nv)
{
if (nv.referenceCount()!=0)
{
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(&nv);
if (runCallbacks("traverse",inputParameters, outputParameters)) return;
}
traverseImplementation(nv);
}
void Widget::traverseImplementation(osg::NodeVisitor& nv)
{
if (!_graphicsInitialized && nv.getVisitorType()!=osg::NodeVisitor::CULL_VISITOR) createGraphics();
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(&nv);
if (ev)
{
if (_visible && _enabled)
{
updateFocus(nv);
// OSG_NOTICE<<"EventTraversal getHasEventFocus()="<<getHasEventFocus()<<std::endl;
if (getHasEventFocus())
{
// signify that event has been taken by widget with focus
ev->setEventHandled(true);
osgGA::EventQueue::Events& events = ev->getEvents();
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
if (handle(ev, itr->get()))
{
(*itr)->setHandled(true);
}
}
}
osg::Group::traverse(nv);
}
}
else if (_visible ||
(nv.getVisitorType()!=osg::NodeVisitor::UPDATE_VISITOR && nv.getVisitorType()!=osg::NodeVisitor::CULL_VISITOR && nv.getVisitorType()!=osg::NodeVisitor::INTERSECTION_VISITOR) )
{
GraphicsSubgraphMap::iterator itr = _graphicsSubgraphMap.begin();
while(itr!= _graphicsSubgraphMap.end() && itr->first<=0)
{
itr->second->accept(nv);
++itr;
}
Group::traverse(nv);
while(itr!= _graphicsSubgraphMap.end())
{
itr->second->accept(nv);
++itr;
}
}
}
bool Widget::handle(osgGA::EventVisitor* ev, osgGA::Event* event)
{
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
// so don't attempt to call the sctipt.
if (ev->referenceCount()!=0)
{
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(ev);
inputParameters.push_back(event);
if (runCallbacks("handle",inputParameters, outputParameters))
{
if (outputParameters.size()>=1)
{
osg::BoolValueObject* bvo = dynamic_cast<osg::BoolValueObject*>(outputParameters[0].get());
return bvo ? bvo->getValue() : false;
}
}
}
return handleImplementation(ev, event);
}
bool Widget::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event)
{
return false;
}
void Widget::dirty()
{
_graphicsInitialized = false;
}
void Widget::createGraphics()
{
if (!runCallbacks("createGraphics")) createGraphicsImplementation();
}
void Widget::createGraphicsImplementation()
{
_graphicsInitialized = true;
}
osg::BoundingSphere Widget::computeBound() const
{
osg::BoundingSphere bs;
if (_extents.valid()) bs.expandBy(_extents);
bs.expandBy(Group::computeBound());
return bs;
}
void Widget::resizeGLObjectBuffers(unsigned int maxSize)
{
for(GraphicsSubgraphMap::iterator itr = _graphicsSubgraphMap.begin();
itr != _graphicsSubgraphMap.end();
++itr)
{
itr->second->resizeGLObjectBuffers(maxSize);
}
Group::resizeGLObjectBuffers(maxSize);
}
void Widget::releaseGLObjects(osg::State* state) const
{
for(GraphicsSubgraphMap::const_iterator itr = _graphicsSubgraphMap.begin();
itr != _graphicsSubgraphMap.end();
++itr)
{
itr->second->releaseGLObjects(state);
}
Group::releaseGLObjects(state);
}
bool Widget::computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3& localPosition) const
{
osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0;
osgUtil::LineSegmentIntersector::Intersections intersections;
if (aa && aa->computeIntersections(*event, ev->getNodePath(), intersections))
{
localPosition = intersections.begin()->getLocalIntersectPoint();
return (_extents.contains(localPosition, 1e-6));
}
else
{
return false;
}
}
struct SortTraversalOrder
{
bool operator() (const osgUtil::LineSegmentIntersector::Intersection* lhs, const osgUtil::LineSegmentIntersector::Intersection* rhs) const
{
double epsilon = 1e-6;
if (lhs->ratio > (rhs->ratio+epsilon)) return true;
if (lhs->ratio < (rhs->ratio-epsilon)) return false;
const osg::NodePath& np_lhs = lhs->nodePath;
const osg::NodePath& np_rhs = rhs->nodePath;
osg::NodePath::const_iterator itr_lhs = np_lhs.begin();
osg::NodePath::const_iterator end_lhs = np_lhs.end();
osg::NodePath::const_iterator itr_rhs = np_rhs.begin();
osg::NodePath::const_iterator end_rhs = np_rhs.end();
const osg::Group* parent = 0;
while(itr_lhs!=end_lhs && itr_rhs!=end_rhs)
{
if (*itr_lhs == *itr_rhs)
{
parent = (*itr_lhs)->asGroup();
++itr_lhs;
++itr_rhs;
}
else if (parent==0)
{
OSG_NOTICE<<"SortTraversalOrder::operator() NodePath has no parent, just have to use default less than operator for Intersection"<<std::endl;
return (*lhs)<(*rhs);
}
else
{
const osgUI::Widget* widget = dynamic_cast<const osgUI::Widget*>(parent);
unsigned int lhs_index = parent->getChildIndex(*itr_lhs);
double lhs_sort_value = static_cast<double>(lhs_index)/static_cast<double>(parent->getNumChildren());
unsigned int rhs_index = parent->getChildIndex(*itr_rhs);
double rhs_sort_value = (static_cast<double>(rhs_index)+epsilon)/static_cast<double>(parent->getNumChildren());
if (widget)
{
const osgUI::Widget::GraphicsSubgraphMap& gsm = widget->getGraphicsSubgraphMap();
for(osgUI::Widget::GraphicsSubgraphMap::const_iterator itr=gsm.begin();
itr!=gsm.end();
++itr)
{
if (itr->second==(*itr_lhs)) lhs_sort_value = itr->first;
if (itr->second==(*itr_rhs)) rhs_sort_value = itr->first;
}
}
if (lhs_sort_value>rhs_sort_value) return true;
if (lhs_sort_value<rhs_sort_value) return false;
}
}
return false;
}
};
bool Widget::computeIntersections(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, Intersections& intersections, osg::Node::NodeMask traversalMask) const
{
osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0;
osgUtil::LineSegmentIntersector::Intersections source_intersections;
if (aa && aa->computeIntersections(*event, ev->getNodePath(), source_intersections, traversalMask))
{
typedef std::vector<const osgUtil::LineSegmentIntersector::Intersection*> IntersectionPointerList;
IntersectionPointerList intersectionsToSort;
// populate the temporay vector of poiners to the original intersection pointers.
for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = source_intersections.begin();
itr != source_intersections.end();
++itr)
{
if (itr->drawable->getName()!="DepthSetPanel")
{
intersectionsToSort.push_back(&(*itr));
}
}
// sort the pointer list into order based on child traversal order, to be consistent with osgUI rendering order.
std::sort(intersectionsToSort.begin(), intersectionsToSort.end(), SortTraversalOrder());
// copy the pointers to final Intersection container
for(IntersectionPointerList::iterator itr = intersectionsToSort.begin();
itr != intersectionsToSort.end();
++itr)
{
intersections.push_back(*(*itr));
}
return true;
}
return false;
}