aboutsummaryrefslogtreecommitdiff
path: root/sons_of_sol/private_db/ControlMultiplexer.cpp
blob: 97ae2f471b026659580fec43b4ed80f166951136 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "sons_of_sol/ControlMultiplexer.hpp"

ControlMultiplexer::ControlMultiplexer() {
	
}

void ControlMultiplexer::onKeyUp( const SDL_KeyboardEvent& evt ) {
	std::map< SDLKey, M_MapVal >::iterator itr;
	itr = m_keysym_map.find( evt.keysym.sym );

	if( itr != m_keysym_map.end() ) {
		ControlMotionEvent event( (*itr).second.type, 0, ControlMotionEvent::BUTTON );
		fireControlEvent( event );
	}
}

void ControlMultiplexer::onKeyDown( const SDL_KeyboardEvent& evt ) {
	std::map< SDLKey, M_MapVal >::iterator itr;
	itr = m_keysym_map.find( evt.keysym.sym );

	if( itr != m_keysym_map.end() ) {
		ControlMotionEvent event( (*itr).second.type, 1 * (*itr).second.multiplier, ControlMotionEvent::BUTTON );
		fireControlEvent( event );
	}
}

void ControlMultiplexer::onMouseMoved( const SDL_MouseMotionEvent& evt ) {
	std::map< uint16_t, M_MapVal >::iterator itr; 
	/* x direction on mousepad */
	itr = m_joy_axis_map.find( 0xFF00 );
	const SDL_VideoInfo* info = SDL_GetVideoInfo();   //<-- calls SDL_GetVideoInfo();   
	int screen_width = info->current_w;
	int screen_height = info->current_h;

	float x_norm = (evt.x - screen_width / 2) / ((float) screen_width / 2.0);
	if( itr != m_joy_axis_map.end() ) {
		ControlMotionEvent event( itr->second.type, x_norm * (*itr).second.multiplier,
			ControlMotionEvent::MOUSE );
		fireControlEvent( event );
	}

	/* y direction */
	float y_norm = (evt.y - screen_height / 2) / ((float) screen_height / 2.0);
	itr = m_joy_axis_map.find( 0xFF01 );
	if( itr != m_joy_axis_map.end() ) {
		ControlMotionEvent event( itr->second.type, y_norm * (*itr).second.multiplier,
			ControlMotionEvent::MOUSE );
		fireControlEvent( event );
	}
}

void ControlMultiplexer::fireControlEvent( const ControlMotionEvent& evt ) {
	std::vector< ControlMotionListener* >::iterator itr;

	for( itr = m_motion_listeners.begin(); itr != m_motion_listeners.end() ; ++ itr ) {
		(*itr)->onControlMotion( evt );
	}
}