Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / gui / input_handler.cpp
blobf00caf64de9603ffb0be4c24e6c17b7503c356c5
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdpch.h"
21 #include "nel/gui/input_handler.h"
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 namespace NLGUI
29 CInputHandler::CInputHandler()
31 listener = NULL;
34 CInputHandler::~CInputHandler()
36 listener = NULL;
40 bool CInputHandler::handleEvent( const NLMISC::CEvent &evnt )
42 if( evnt == NLMISC::EventSetFocusId )
43 return handleSetFocusEvent( evnt );
44 else
45 if( evnt == NLMISC::EventKeyDownId ||
46 evnt == NLMISC::EventKeyUpId ||
47 evnt == NLMISC::EventCharId ||
48 evnt == NLMISC::EventStringId )
49 return handleKeyboardEvent( evnt );
50 else
51 if( evnt == NLMISC::EventMouseMoveId ||
52 evnt == NLMISC::EventMouseDownId ||
53 evnt == NLMISC::EventMouseUpId ||
54 evnt == NLMISC::EventMouseWheelId ||
55 evnt == NLMISC::EventMouseDblClkId )
56 return handleMouseEvent( evnt );
59 return false;
62 bool CInputHandler::handleSetFocusEvent( const NLMISC::CEvent &evnt )
64 nlassert( evnt == NLMISC::EventSetFocusId );
65 const NLMISC::CEventSetFocus *e = reinterpret_cast< const NLMISC::CEventSetFocus* >( &evnt );
67 return listener->handleEvent( CEventDescriptorSetFocus( e->Get ) );
70 bool CInputHandler::handleKeyboardEvent( const NLMISC::CEvent &evnt )
72 bool ok = false;
73 if( evnt == NLMISC::EventKeyDownId ||
74 evnt == NLMISC::EventKeyUpId ||
75 evnt == NLMISC::EventCharId ||
76 evnt == NLMISC::EventStringId )
77 ok = true;
79 nlassert( ok );
81 return listener->handleEvent( NLGUI::CEventDescriptorKey( reinterpret_cast< const NLMISC::CEventKey& >( evnt ) ) );
84 bool CInputHandler::handleMouseEvent( const NLMISC::CEvent &evnt )
86 if( evnt == NLMISC::EventMouseMoveId )
87 return handleMouseMoveEvent( evnt );
88 else
89 if( evnt == NLMISC::EventMouseDownId )
90 return handleMouseButtonDownEvent( evnt );
91 else
92 if( evnt == NLMISC::EventMouseUpId )
93 return handleMouseButtonUpEvent( evnt );
94 else
95 if( evnt == NLMISC::EventMouseDblClkId )
96 return handleMouseDblClickEvent( evnt );
97 else
98 if( evnt == NLMISC::EventMouseWheelId )
99 return handleMouseWheelEvent( evnt );
101 return false;
104 bool CInputHandler::handleMouseMoveEvent( const NLMISC::CEvent &evnt )
106 const NLMISC::CEventMouseMove &mouseMoveEvent = static_cast< const NLMISC::CEventMouseMove& >( evnt );
108 CEventDescriptorMouse eventDesc;
109 float x = mouseMoveEvent.X;
110 float y = mouseMoveEvent.Y;
112 // These bloody hacks here are used so that we can send the x, and y float coordinates
113 // from the NEL mouse move event, to the GUI event listener, without having to change
114 // CEventDescriptorMouse or without having to couple with the consumer class
115 eventDesc.setX( *reinterpret_cast< sint32* >( &x ) );
116 eventDesc.setY( *reinterpret_cast< sint32* >( &y ) );
118 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mousemove );
120 return listener->handleEvent( eventDesc );
123 bool CInputHandler::handleMouseButtonDownEvent( const NLMISC::CEvent &evnt )
125 nlassert( evnt == NLMISC::EventMouseDownId );
127 CEventDescriptorMouse eventDesc;
129 const NLMISC::CEventMouseDown *mouseDownEvent = static_cast< const NLMISC::CEventMouseDown* >( &evnt );
131 if( mouseDownEvent->Button & NLMISC::leftButton )
133 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mouseleftdown );
134 return listener->handleEvent( eventDesc );
137 if(mouseDownEvent->Button & NLMISC::rightButton)
139 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mouserightdown );
140 return listener->handleEvent( eventDesc );
143 return false;
146 bool CInputHandler::handleMouseButtonUpEvent( const NLMISC::CEvent &evnt )
148 nlassert( evnt == NLMISC::EventMouseUpId );
150 CEventDescriptorMouse eventDesc;
152 const NLMISC::CEventMouseUp *mouseUpEvent = static_cast< const NLMISC::CEventMouseUp* >( &evnt );
154 if( mouseUpEvent->Button & NLMISC::leftButton )
156 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mouseleftup );
157 return listener->handleEvent( eventDesc );
160 if( mouseUpEvent->Button & NLMISC::rightButton )
162 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mouserightup );
163 return listener->handleEvent( eventDesc );
166 return false;
169 bool CInputHandler::handleMouseDblClickEvent( const NLMISC::CEvent &evnt )
171 nlassert( evnt == NLMISC::EventMouseDblClkId );
173 CEventDescriptorMouse eventDesc;
175 const NLMISC::CEventMouseDblClk *dblClickEvent = static_cast< const NLMISC::CEventMouseDblClk* >( &evnt );
177 if( dblClickEvent->Button & NLMISC::leftButton )
179 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mouseleftdblclk );
180 return listener->handleEvent (eventDesc);
183 if( dblClickEvent->Button & NLMISC::rightButton )
185 eventDesc.setEventTypeExtended(NLGUI::CEventDescriptorMouse::mouserightdblclk);
186 return listener->handleEvent (eventDesc);
189 return false;
192 bool CInputHandler::handleMouseWheelEvent( const NLMISC::CEvent &evnt )
194 nlassert( evnt == NLMISC::EventMouseWheelId );
196 CEventDescriptorMouse eventDesc;
197 sint32 mouseWheel = 0;
199 const NLMISC::CEventMouseWheel *wheelEvent = static_cast< const NLMISC::CEventMouseWheel* >( &evnt );
201 if( wheelEvent->Direction )
202 mouseWheel = 1;
203 else
204 mouseWheel = -1;
206 if( mouseWheel != 0 )
208 eventDesc.setEventTypeExtended( CEventDescriptorMouse::mousewheel );
209 eventDesc.setWheel( mouseWheel );
210 return listener->handleEvent( eventDesc );
213 return false;
216 void CInputHandler::setListener( IInputEventListener *listener )
218 this->listener = listener;