1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
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/>.
21 #include "nel/gui/input_handler.h"
29 CInputHandler::CInputHandler()
34 CInputHandler::~CInputHandler()
40 bool CInputHandler::handleEvent( const NLMISC::CEvent
&evnt
)
42 if( evnt
== NLMISC::EventSetFocusId
)
43 return handleSetFocusEvent( evnt
);
45 if( evnt
== NLMISC::EventKeyDownId
||
46 evnt
== NLMISC::EventKeyUpId
||
47 evnt
== NLMISC::EventCharId
||
48 evnt
== NLMISC::EventStringId
)
49 return handleKeyboardEvent( evnt
);
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
);
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
)
73 if( evnt
== NLMISC::EventKeyDownId
||
74 evnt
== NLMISC::EventKeyUpId
||
75 evnt
== NLMISC::EventCharId
||
76 evnt
== NLMISC::EventStringId
)
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
);
89 if( evnt
== NLMISC::EventMouseDownId
)
90 return handleMouseButtonDownEvent( evnt
);
92 if( evnt
== NLMISC::EventMouseUpId
)
93 return handleMouseButtonUpEvent( evnt
);
95 if( evnt
== NLMISC::EventMouseDblClkId
)
96 return handleMouseDblClickEvent( evnt
);
98 if( evnt
== NLMISC::EventMouseWheelId
)
99 return handleMouseWheelEvent( evnt
);
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
);
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
);
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
);
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
)
206 if( mouseWheel
!= 0 )
208 eventDesc
.setEventTypeExtended( CEventDescriptorMouse::mousewheel
);
209 eventDesc
.setWheel( mouseWheel
);
210 return listener
->handleEvent( eventDesc
);
216 void CInputHandler::setListener( IInputEventListener
*listener
)
218 this->listener
= listener
;