1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 #include "PointsEventHandler.h"
15 
16 #include <osg/Notify>
17 
PointsEventHandler()18 PointsEventHandler::PointsEventHandler()
19 {
20     _point = new osg::Point;
21     //_point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.05f));
22 }
23 
handle(const osgGA::GUIEventAdapter & ea,osgGA::GUIActionAdapter &)24 bool PointsEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
25 {
26     switch(ea.getEventType())
27     {
28         case(osgGA::GUIEventAdapter::KEYDOWN):
29         {
30             if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
31             {
32                changePointSize(1.0f);
33                return true;
34             }
35             else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
36             {
37                changePointSize(-1.0f);
38                return true;
39             }
40             else if (ea.getKey()=='<')
41             {
42                changePointAttenuation(1.1f);
43                return true;
44             }
45             else if (ea.getKey()=='>')
46             {
47                changePointAttenuation(1.0f/1.1f);
48                return true;
49             }
50             break;
51         }
52         default:
53             break;
54     }
55     return false;
56 }
57 
getUsage(osg::ApplicationUsage & usage) const58 void PointsEventHandler::getUsage(osg::ApplicationUsage& usage) const
59 {
60     usage.addKeyboardMouseBinding("+","Increase point size");
61     usage.addKeyboardMouseBinding("-","Reduce point size");
62     usage.addKeyboardMouseBinding(">","Increase point size");
63     usage.addKeyboardMouseBinding("<","Reduce point size");
64 }
65 
getPointSize() const66 float PointsEventHandler::getPointSize() const
67 {
68     return _point->getSize();
69 }
70 
setPointSize(float psize)71 void PointsEventHandler::setPointSize(float psize)
72 {
73     if (psize>0.0)
74     {
75         _point->setSize(psize);
76         _stateset->setAttribute(_point.get());
77     }
78     else
79     {
80         _stateset->setAttribute(_point.get(),osg::StateAttribute::INHERIT);
81     }
82     osg::notify(osg::INFO)<<"Point size "<<psize<<std::endl;
83 }
84 
changePointSize(float delta)85 void PointsEventHandler::changePointSize(float delta)
86 {
87     setPointSize(getPointSize()+delta);
88 }
89 
changePointAttenuation(float scale)90 void PointsEventHandler::changePointAttenuation(float scale)
91 {
92     if (_point.valid())
93     {
94         _point->setDistanceAttenuation(_point->getDistanceAttenuation()*scale);
95         if (_stateset.valid()) _stateset->setAttribute(_point.get());
96     }
97 }
98