Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
input.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "common/event.h"
28 #include "common/key.h"
29 #include "common/singleton.h"
30 
31 #include "math/intpoint.h"
32 
33 
34 
40 {
43  unsigned int primary, secondary;
44 
45  InputBinding(unsigned int p = KEY_INVALID, unsigned int s = KEY_INVALID)
46  : primary(p), secondary(s) {}
47 };
48 
54 {
56  int axis;
58  bool invert;
59 };
60 
62 const int AXIS_INVALID = -1;
63 
68 class CInput : public CSingleton<CInput>
69 {
70 public:
72  CInput();
73 
75  void EventProcess(Event &event);
76 
78  void MouseMove(Math::IntPoint pos);
79 
80 
82  int GetKmods() const;
83 
85  bool GetKmodState(int kmod) const;
86 
88  bool GetKeyState(InputSlot key) const;
89 
91  bool GetMouseButtonState(int index) const;
92 
94  void ResetKeyStates();
95 
97  Math::Point GetMousePos() const;
98 
99 
102 
104 
105  void SetInputBinding(InputSlot slot, InputBinding binding);
106  const InputBinding& GetInputBinding(InputSlot slot);
108 
110 
111  void SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding);
112  const JoyAxisBinding& GetJoyAxisBinding(JoyAxisSlot slot);
114 
116 
117  void SetJoystickDeadzone(float zone);
118  float GetJoystickDeadzone();
120 
122  InputSlot FindBinding(unsigned int key);
123 
125 
126  std::string SaveKeyBindings();
127  void LoadKeyBindings(std::string keys);
129 
131  InputSlot SearchKeyById(std::string name);
132 
134 
135  std::string GetKeysString(InputBinding binding);
136  std::string GetKeysString(InputSlot slot);
138 
139 private:
141  unsigned int m_kmodState;
143  bool m_keyPresses[INPUT_SLOT_MAX];
144 
145 
147  Math::Point m_mousePos;
149  unsigned int m_mouseButtonsState;
150 
151 
153  Math::Vector m_keyMotion;
155  Math::Vector m_joyMotion;
156 
157 
159  InputBinding m_inputBindings[INPUT_SLOT_MAX];
160  JoyAxisBinding m_joyAxisBindings[JOY_AXIS_SLOT_MAX];
161  float m_joystickDeadzone;
162 };
CSingleton base class for singletons.
void SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding)
Management of joystick axis bindings.
Definition: input.cpp:238
InputSlot
Available slots for input bindings NOTE: When adding new values, remember to also update keyTable in ...
Definition: key.h:76
const unsigned int KEY_INVALID
Special value for invalid key bindings.
Definition: key.h:69
void MouseMove(Math::IntPoint pos)
Called by CApplication on SDL MOUSE_MOTION event.
Definition: input.cpp:135
void SetJoystickDeadzone(float zone)
Management of joystick deadzone.
Definition: input.cpp:250
void SetInputBinding(InputSlot slot, InputBinding binding)
Management of input bindings.
Definition: input.cpp:226
Definition: singleton.h:30
int axis
Axis index or AXIS_INVALID.
Definition: input.h:56
Math::Point GetMousePos() const
Returns the position of mouse cursor (in interface coords)
Definition: input.cpp:170
InputSlot FindBinding(unsigned int key)
Get binding slot for given key.
Definition: input.cpp:260
std::string GetKeysString(InputBinding binding)
Returns string describing keys to pressed.
Definition: input.cpp:339
InputSlot SearchKeyById(std::string name)
Seeks a InputSlot by id. Returns INPUT_SLOT_MAX if not found.
Definition: input.cpp:327
bool invert
True to invert axis value.
Definition: input.h:58
int GetKmods() const
Returns the current key modifiers.
Definition: input.cpp:140
void ResetKeyStates()
Resets tracked key states and modifiers.
Definition: input.cpp:160
2D point
Definition: point.h:49
Binding for joystick axis.
Definition: input.h:53
Key-related macros and enums.
void SetDefaultInputBindings()
Sets the default input bindings (key and axes)
Definition: input.cpp:175
bool GetMouseButtonState(int index) const
Returns whether the mouse button is pressed.
Definition: input.cpp:155
std::string SaveKeyBindings()
Saving/loading key bindings to string (for storing in colobot.ini)
Definition: input.cpp:272
Event types, structs and event queue.
3D (3x1) vector
Definition: vector.h:52
2D Point with integer coords
Definition: intpoint.h:38
unsigned int primary
Definition: input.h:43
Binding for input slot.
Definition: input.h:39
Event sent by system, interface or game.
Definition: event.h:678
IntPoint struct.
void EventProcess(Event &event)
Process an incoming event, also sets .kmodState, .mousePos, .mouseButtonsState and ...
Definition: input.cpp:49
bool GetKeyState(InputSlot key) const
Returns whether the key is pressed.
Definition: input.cpp:150
CInput()
Constructor.
Definition: input.cpp:36
const int AXIS_INVALID
Invalid value for axis binding (no axis assigned)
Definition: input.h:62
bool GetKmodState(int kmod) const
Returns whether the given kmod is active.
Definition: input.cpp:145
JoyAxisSlot
Slots for joystick axes inputs.
Definition: key.h:112
Management of mouse, keyboard and joystick.
Definition: input.h:68