OpenWalnut  1.5.0dev
WOnscreenSelectionHandler.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include "WOnscreenSelectionHandler.h"
26 
27 static const unsigned int SHIFT_DOWN = 65505;
28 static const unsigned int SHIFT_UP = 16777248;
29 
31  m_manager( manager ),
32  m_mousePressed( false ),
33  m_shiftPressed( false )
34 {
35 }
36 
37 bool WOnscreenSelectionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
38 {
39  float x = ( ea.getXnormalized() + 1.0 ) / 2.0;
40  float y = ( ea.getYnormalized() + 1.0 ) / 2.0;
41 
42  if( ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
43  {
44  if( ea.getKey() == SHIFT_DOWN )
45  {
46  m_shiftPressed = true;
47  }
48  }
49  if( ea.getEventType() == osgGA::GUIEventAdapter::KEYUP )
50  {
51  if( ea.getKey() == SHIFT_UP )
52  {
53  m_shiftPressed = false;
54  m_manager->end( x, y );
55  }
56  }
57 
58  if( !m_shiftPressed )
59  {
60  if( ea.getEventType() == osgGA::GUIEventAdapter::DRAG )
61  {
62  m_manager->clear();
63  }
64  return false;
65  }
66 
67  if( ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON || ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
68  {
69  m_manager->setClickType( ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON );
70  if( ea.getEventType() == osgGA::GUIEventAdapter::PUSH )
71  {
72  m_mousePressed = true;
73  m_manager->start( x, y );
74  }
75  else if( ea.getEventType() == osgGA::GUIEventAdapter::RELEASE )
76  {
77  m_mousePressed = false;
78  m_manager->end( x, y );
79  }
80  return true;
81  }
82 
83  if( m_mousePressed && ea.getEventType() == osgGA::GUIEventAdapter::DRAG )
84  {
85  m_manager->move( x, y );
86  return true;
87  }
88 
89  return false;
90 }
bool m_shiftPressed
Whether shift is pressed or not.
bool m_mousePressed
Whether the mouse is pressed or not.
WOnscreenSelection * m_manager
The WOnscreenSelection this belongs to.
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
Handles the incoming events.
WOnscreenSelectionHandler(WOnscreenSelection *manager)
Construct a new WDrawHandler object.
Manages different types of selection.
void clear()
Clears the current selection.
void start(float x, float y)
Starts the selection.
void setClickType(bool clickType)
Sets the click type.
void move(float x, float y)
Handles mouse move while selecting.
void end(float x, float y)
Ends the selection.