1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12 */
13
14
15 #include <osgUI/Dialog>
16 #include <osgUI/PushButton>
17 #include <osgUI/Label>
18 #include <osgUI/Callbacks>
19
20 #include <osgText/String>
21 #include <osgText/Font>
22 #include <osgText/Text>
23 #include <osg/Notify>
24
25 using namespace osgUI;
26
Dialog()27 Dialog::Dialog()
28 {
29 }
30
Dialog(const osgUI::Dialog & dialog,const osg::CopyOp & copyop)31 Dialog::Dialog(const osgUI::Dialog& dialog, const osg::CopyOp& copyop):
32 Widget(dialog, copyop),
33 _title(dialog._title)
34 {
35 }
36
handleImplementation(osgGA::EventVisitor *,osgGA::Event * event)37 bool Dialog::handleImplementation(osgGA::EventVisitor* /*ev*/, osgGA::Event* event)
38 {
39 osgGA::GUIEventAdapter* ea = event->asGUIEventAdapter();
40 if (!ea) return false;
41
42 switch(ea->getEventType())
43 {
44 //case(osgGA::GUIEventAdapter::KEYDOWN):
45 case(osgGA::GUIEventAdapter::KEYUP):
46 OSG_NOTICE<<"Key pressed : "<<ea->getKey()<<std::endl;
47
48 break;
49 default:
50 break;
51 }
52
53 return false;
54 }
55
createGraphicsImplementation()56 void Dialog::createGraphicsImplementation()
57 {
58 _group = new osg::Group;
59
60 Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
61
62 float titleHeight = 10.0;
63 osg::BoundingBox titleBarExtents(_extents.xMin(), _extents.yMax(), _extents.zMin(), _extents.xMax()-titleHeight, _extents.yMax()+titleHeight, _extents.zMin());
64 osg::BoundingBox closeButtonExtents(_extents.xMax()-titleHeight, _extents.yMax(), _extents.zMin(), _extents.xMax(), _extents.yMax()+titleHeight, _extents.zMin());
65
66 osg::Vec4 dialogBackgroundColor(0.84,0.82,0.82,1.0);
67 osg::Vec4 dialogTitleBackgroundColor(0.5,0.5,1.0,1.0);
68
69 _group->addChild( style->createPanel(_extents, dialogBackgroundColor) );
70
71 _group->addChild( style->createPanel(titleBarExtents, dialogTitleBackgroundColor) );
72
73 osg::BoundingBox dialogWithTitleExtents(_extents);
74 dialogWithTitleExtents.expandBy(titleBarExtents);
75 dialogWithTitleExtents.expandBy(closeButtonExtents);
76
77 bool requiresFrame = (getFrameSettings() && getFrameSettings()->getShape()!=osgUI::FrameSettings::NO_FRAME);
78 if (requiresFrame)
79 {
80 _group->addChild(style->createFrame(dialogWithTitleExtents, getFrameSettings(), dialogBackgroundColor));
81
82 titleBarExtents.xMin() += getFrameSettings()->getLineWidth();
83 titleBarExtents.yMax() -= getFrameSettings()->getLineWidth();
84 closeButtonExtents.xMax() -= getFrameSettings()->getLineWidth();
85 closeButtonExtents.yMax() -= getFrameSettings()->getLineWidth();
86 }
87
88 OSG_NOTICE<<"Dialog::_extents ("<<_extents.xMin()<<", "<<_extents.yMin()<<", "<<_extents.zMin()<<"), ("<<_extents.xMax()<<", "<<_extents.yMax()<<", "<<_extents.zMax()<<")"<<std::endl;
89 OSG_NOTICE<<"Dialog::titleBarExtents ("<<titleBarExtents.xMin()<<", "<<titleBarExtents.yMin()<<", "<<titleBarExtents.zMin()<<"), ("<<titleBarExtents.xMax()<<", "<<titleBarExtents.yMax()<<", "<<titleBarExtents.zMax()<<")"<<std::endl;
90 OSG_NOTICE<<"Dialog::dialogWithTitleExtents ("<<dialogWithTitleExtents.xMin()<<", "<<dialogWithTitleExtents.yMin()<<", "<<dialogWithTitleExtents.zMin()<<"), ("<<dialogWithTitleExtents.xMax()<<", "<<dialogWithTitleExtents.yMax()<<", "<<dialogWithTitleExtents.zMax()<<")"<<std::endl;
91
92 #if 0
93 osg::ref_ptr<Node> node = style->createText(titleBarExtents, getAlignmentSettings(), getTextSettings(), _title);
94 _titleDrawable = dynamic_cast<osgText::Text*>(node.get());
95 _titleDrawable->setDataVariance(osg::Object::DYNAMIC);
96 _group->addChild(_titleDrawable.get());
97 #endif
98
99 osg::ref_ptr<PushButton> closeButton = new osgUI::PushButton;
100 closeButton->setExtents(closeButtonExtents);
101 closeButton->setText("x");
102 closeButton->setAlignmentSettings(getAlignmentSettings());
103 closeButton->setTextSettings(getTextSettings());
104 //closeButton->setFrameSettings(getFrameSettings());
105 closeButton->getOrCreateUserDataContainer()->addUserObject(new osgUI::CloseCallback("released", this));
106
107 osg::ref_ptr<Label> titleLabel = new osgUI::Label;
108 titleLabel->setExtents(titleBarExtents);
109 titleLabel->setText(_title);
110 titleLabel->setAlignmentSettings(getAlignmentSettings());
111 titleLabel->setTextSettings(getTextSettings());
112 titleLabel->setFrameSettings(getFrameSettings());
113 titleLabel->getOrCreateUserDataContainer()->addUserObject(new osgUI::DragCallback);
114
115 _group->addChild(closeButton.get());
116 _group->addChild(titleLabel.get());
117
118 style->setupDialogStateSet(getOrCreateWidgetStateSet(), 5);
119 style->setupClipStateSet(dialogWithTitleExtents, getOrCreateWidgetStateSet());
120
121 // render before the subgraph
122 setGraphicsSubgraph(-1, _group.get());
123
124 // render after the subgraph
125 setGraphicsSubgraph(1, style->createDepthSetPanel(dialogWithTitleExtents));
126 }
127