input.hpp 3.67 KB
Newer Older
1
2
3
4
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.hpp for GDW SS15
//****************************************
5
6
7
#ifndef __INPUT_HPP__
#define __INPUT_HPP__

8
#include <memory>
9
#include <unordered_map>
10
#include <vector>
11
#include <SDL.h>
12
13
#include <asset/asset.hpp>
#include <core/engine.hpp>
14
#include <util/logger.hpp>
15
16

namespace gdw{
17
18
19
20
21
	struct pos{
		float x = 0.0f;
		float y = 0.0f;
	};

22
	class engine;
23
	class input_util;
24
	class controller_manager;
25
26
27
28
29
	class mouse;
	class keyboard;
	class controller;
	class input_mapping;
	class input_events;
30
	class input{
31
		private:
32
			std::shared_ptr<gdw::input_util> util_;
33
34
			std::shared_ptr<gdw::keyboard> keyboard_;
			std::shared_ptr<gdw::mouse> mouse_;
35
36
37
			std::shared_ptr<gdw::input_mapping> mapping_;
			std::shared_ptr<gdw::input_events> events_;
			std::shared_ptr<gdw::controller_manager> controller_manager_;
38
39

			engine &engine_;
40
41
42
			pos pos_;
			pos last_pos_;
			pos relative_pos_;
43

44
45
			int window_width_ = 800;
			int window_height_ = 600;
46
47
48
49
		public:
			input(engine& engine);
			~input();

50
			/**needs to be called in the event loop*/
51
			void update_events(const SDL_Event &event);
52

53
			/**needs to be called after the the event loop but in the game loop*/
54
			void update(float delta);
55
56

			/**needs to be called at the end of every game loop*/
57
			void reset();
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
			/**it sets the name for a mapping to add a key, button or axis
			*
			* use it in the settings.main or settings.user:
			*
			* https://wiki.libsdl.org/SDL_Keycode?highlight=%28%5CbCategoryEnum%5Cb%29%7C%28CategoryKeyboard%29
			* use the 'Key Name'
			* key1: <name>_key1 : <key>
			* example:	test_key1 : t
			* key2: <name>_key2 : <key>
			* example:	test_key2 : t
			*
			* use 'left', 'middle', 'right', 'mouse4' or 'mouse5'
			* mouse button: <name>_mouse_button : <button>
			* example:	test_mouse_button : left
			*
			* https://wiki.libsdl.org/SDL_GameControllerButton?highlight=%28%5CbCategoryGameController%5Cb%29%7C%28CategoryEnum%29
75
76
			* without 'SDL_CONTROLLER_BUTTON_'
			* dpad is 'dp' + direction		dpad_up is 'dpup'
77
78
79
80
81
			* controller button: <name>_controller_button : <button>
			* example:	test_controller_button: start
			*
			* https://wiki.libsdl.org/SDL_GameControllerAxis?highlight=%28%5CbCategoryGameController%5Cb%29%7C%28CategoryEnum%29
			* without 'SDL_CONTROLLER_AXIS_'
82
			* and trigger got the inverted name 'lefttrigger' is the name for SDL_CONTROLLER_AXIS_TRIGGERLEFT
83
84
85
			* controller axis: <name>_controller_axis : <axis> + '_positiv' or '_negativ' (ignored if trigger)
			* example:	test_controller_axis : leftx_positiv
			*/
86
			void add_mapping(std::string name);
87

88
			/**prints the mappings*/
89
			void print_mapping();
90

91
			/**Returns true once it is pressed
92
93
			* controller_id only for multi controller
			*/
94
			bool pressed(std::string mapping, int controller_id = -1);
Benjamin 'Albsi' Albsmeier's avatar
Benjamin 'Albsi' Albsmeier committed
95

96
			/**Returns true every frame, as long as it is down
97
98
			* controller_id only for multi controller
			*/
99
			bool down(std::string mapping, int controller_id = -1);
Benjamin 'Albsi' Albsmeier's avatar
Benjamin 'Albsi' Albsmeier committed
100

101
102
103
			/**Returns true once it is released
			* controller_id only for multi controller
			*/
104
			bool released(std::string mapping, int controller_id = -1);
105

106
			/**returns x position of the mouse cursor or of the controller cross-hair*/
107
			int position_x();
108

109
			/**returns y position of the mouse cursor or of the controller cross-hair*/
110
			int position_y();
111

112
113
114
115
116
117
			/**returns the relative x position of the mouse cursor or of the controller cross-hair*/
			int relative_position_x();

			/**returns the relative y position of the mouse cursor or of the controller cross-hair*/
			int relative_position_y();

118
			/**x motion of the mouse wheel*/
119
			int mouse_wheel_x();
120
121

			/**y motion of the mouse wheel*/
122
			int mouse_wheel_y();
123
124
125
126
	};
}

#endif