Refactoring of input and input bindings, CInput class

dev-mp
krzys-h 2014-12-11 19:01:57 +01:00
parent e3e551de22
commit eec6041104
20 changed files with 747 additions and 662 deletions

View File

@ -61,6 +61,7 @@ endif()
# Source files
set(BASE_SOURCES
app/app.cpp
app/input.cpp
app/pausemanager.cpp
app/system.cpp
app/${SYSTEM_CPP_MODULE}

View File

@ -21,6 +21,7 @@
#include "app/app.h"
#include "app/input.h"
#include "app/system.h"
#include "common/logger.h"
@ -104,6 +105,7 @@ CApplication::CApplication()
m_objMan = new CObjectManager();
m_eventQueue = new CEventQueue();
m_profile = new CProfile();
m_input = new CInput();
m_engine = nullptr;
m_device = nullptr;
@ -147,10 +149,6 @@ CApplication::CApplication()
m_mouseMode = MOUSE_SYSTEM;
m_kmodState = 0;
m_mouseButtonsState = 0;
m_trackedKeys = 0;
#ifdef PORTABLE
m_dataPath = "./data";
m_langPath = "./lang";
@ -181,6 +179,9 @@ CApplication::~CApplication()
{
delete m_private;
m_private = nullptr;
delete m_input;
m_input = nullptr;
delete m_objMan;
m_objMan = nullptr;
@ -890,7 +891,7 @@ void CApplication::UpdateMouse()
{
Math::IntPoint pos;
SDL_GetMouseState(&pos.x, &pos.y);
m_mousePos = m_engine->WindowToInterfaceCoords(pos);
m_input->MouseMove(pos);
}
int CApplication::Run()
@ -1082,9 +1083,7 @@ Event CApplication::ProcessSystemEvent()
event.key.virt = false;
event.key.key = m_private->currentEvent.key.keysym.sym;
event.key.unicode = m_private->currentEvent.key.keysym.unicode;
// Use the occasion to update kmods
m_kmodState = m_private->currentEvent.key.keysym.mod;
event.kmodState = m_private->currentEvent.key.keysym.mod;
}
else if ( (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) ||
(m_private->currentEvent.type == SDL_MOUSEBUTTONUP) )
@ -1109,24 +1108,13 @@ Event CApplication::ProcessSystemEvent()
event.type = EVENT_MOUSE_BUTTON_UP;
event.mouseButton.button = static_cast<MouseButton>(1 << m_private->currentEvent.button.button);
// Use the occasion to update mouse button state
if (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN)
m_mouseButtonsState |= event.mouseButton.button;
else
m_mouseButtonsState &= ~event.mouseButton.button;
}
// Use the occasion to update mouse pos
m_mousePos = m_engine->WindowToInterfaceCoords(
Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y));
}
else if (m_private->currentEvent.type == SDL_MOUSEMOTION)
{
event.type = EVENT_MOUSE_MOVE;
m_mousePos = m_engine->WindowToInterfaceCoords(
Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y));
m_input->MouseMove(Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y));
}
else if (m_private->currentEvent.type == SDL_JOYAXISMOTION)
{
@ -1158,51 +1146,8 @@ Event CApplication::ProcessSystemEvent()
event.active.gain = m_private->currentEvent.active.gain == 1;
}
if (event.type == EVENT_KEY_DOWN)
{
if (event.key.key == KEY(KP8))
m_trackedKeys |= TRKEY_NUM_UP;
else if (event.key.key == KEY(KP2))
m_trackedKeys |= TRKEY_NUM_DOWN;
else if (event.key.key == KEY(KP4))
m_trackedKeys |= TRKEY_NUM_LEFT;
else if (event.key.key == KEY(KP6))
m_trackedKeys |= TRKEY_NUM_RIGHT;
else if (event.key.key == KEY(KP_PLUS))
m_trackedKeys |= TRKEY_NUM_PLUS;
else if (event.key.key == KEY(KP_MINUS))
m_trackedKeys |= TRKEY_NUM_MINUS;
else if (event.key.key == KEY(PAGEUP))
m_trackedKeys |= TRKEY_PAGE_UP;
else if (event.key.key == KEY(PAGEDOWN))
m_trackedKeys |= TRKEY_PAGE_DOWN;
}
else if (event.type == EVENT_KEY_UP)
{
if (event.key.key == KEY(KP8))
m_trackedKeys &= ~TRKEY_NUM_UP;
else if (event.key.key == KEY(KP2))
m_trackedKeys &= ~TRKEY_NUM_DOWN;
else if (event.key.key == KEY(KP4))
m_trackedKeys &= ~TRKEY_NUM_LEFT;
else if (event.key.key == KEY(KP6))
m_trackedKeys &= ~TRKEY_NUM_RIGHT;
else if (event.key.key == KEY(KP_PLUS))
m_trackedKeys &= ~TRKEY_NUM_PLUS;
else if (event.key.key == KEY(KP_MINUS))
m_trackedKeys &= ~TRKEY_NUM_MINUS;
else if (event.key.key == KEY(PAGEUP))
m_trackedKeys &= ~TRKEY_PAGE_UP;
else if (event.key.key == KEY(PAGEDOWN))
m_trackedKeys &= ~TRKEY_PAGE_DOWN;
}
event.trackedKeysState = m_trackedKeys;
event.kmodState = m_kmodState;
event.mousePos = m_mousePos;
event.mouseButtonsState = m_mouseButtonsState;
m_input->EventProcess(event);
return event;
}
@ -1398,10 +1343,7 @@ Event CApplication::CreateUpdateEvent()
}
Event frameEvent(EVENT_FRAME);
frameEvent.trackedKeysState = m_trackedKeys;
frameEvent.kmodState = m_kmodState;
frameEvent.mousePos = m_mousePos;
frameEvent.mouseButtonsState = m_mouseButtonsState;
m_input->EventProcess(frameEvent);
frameEvent.rTime = m_relTime;
return frameEvent;
@ -1541,34 +1483,6 @@ bool CApplication::ParseDebugModes(const std::string& str, int& debugModes)
return true;
}
int CApplication::GetKmods() const
{
return m_kmodState;
}
bool CApplication::GetKmodState(int kmod) const
{
return (m_kmodState & kmod) != 0;
}
bool CApplication::GetTrackedKeyState(TrackedKey key) const
{
return (m_trackedKeys & key) != 0;
}
bool CApplication::GetMouseButtonState(int index) const
{
return (m_mouseButtonsState & (1<<index)) != 0;
}
void CApplication::ResetKeyStates()
{
GetLogger()->Trace("Reset key states\n");
m_trackedKeys = 0;
m_kmodState = 0;
m_robotMain->ResetKeyStates();
}
void CApplication::SetGrabInput(bool grab)
{
SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
@ -1594,16 +1508,10 @@ MouseMode CApplication::GetMouseMode() const
return m_mouseMode;
}
Math::Point CApplication::GetMousePos() const
{
return m_mousePos;
}
void CApplication::MoveMouse(Math::Point pos)
{
m_mousePos = pos;
Math::IntPoint windowPos = m_engine->InterfaceToWindowCoords(pos);
m_input->MouseMove(windowPos);
SDL_WarpMouse(windowPos.x, windowPos.y);
}

View File

@ -44,6 +44,7 @@ class CInstanceManager;
class CEventQueue;
class CRobotMain;
class CSoundInterface;
class CInput;
namespace Gfx {
class CModelManager;
@ -80,23 +81,6 @@ enum VideoQueryResult
VIDEO_QUERY_OK
};
/**
* \enum TrackedKey
* \brief Additional keys whose state (pressed/released) is tracked by CApplication
*/
enum TrackedKey
{
TRKEY_NUM_UP = (1<<0),
TRKEY_NUM_DOWN = (1<<1),
TRKEY_NUM_LEFT = (1<<2),
TRKEY_NUM_RIGHT = (1<<3),
TRKEY_NUM_PLUS = (1<<4),
TRKEY_NUM_MINUS = (1<<5),
TRKEY_PAGE_UP = (1<<6),
TRKEY_PAGE_DOWN = (1<<7)
};
/**
* \enum ParseArgsStatus
* \brief State of parsing commandline arguments
@ -296,20 +280,6 @@ public:
//! Updates the mouse position explicitly
void UpdateMouse();
//! Returns the current key modifiers
int GetKmods() const;
//! Returns whether the given kmod is active
bool GetKmodState(int kmod) const;
//! Returns whether the tracked key is pressed
bool GetTrackedKeyState(TrackedKey key) const;
//! Returns whether the mouse button is pressed
bool GetMouseButtonState(int index) const;
//! Resets tracked key states and modifiers
void ResetKeyStates();
//! Management of the grab mode for input (keyboard & mouse)
//@{
void SetGrabInput(bool grab);
@ -322,9 +292,6 @@ public:
MouseMode GetMouseMode() const;
//@}
//! Returns the position of mouse cursor (in interface coords)
Math::Point GetMousePos() const;
//! Moves (warps) the mouse cursor to the specified position (in interface coords)
void MoveMouse(Math::Point pos);
@ -411,6 +378,8 @@ protected:
CRobotMain* m_robotMain;
//! Profile (INI) reader/writer
CProfile* m_profile;
//! Input manager
CInput* m_input;
//! Code to return at exit
int m_exitCode;
@ -456,17 +425,8 @@ protected:
bool m_simulationSuspended;
//@}
//! Current state of key modifiers (bitmask of SDLMod)
unsigned int m_kmodState;
//! Current state of some tracked keys (bitmask of TrackedKey enum values)
unsigned int m_trackedKeys;
//! Current mode of mouse
MouseMode m_mouseMode;
//! Current position of mouse cursor
Math::Point m_mousePos;
//! Current state of mouse buttons (bitmask of MouseButton enum values)
unsigned int m_mouseButtonsState;
//! Info about current joystick device
JoystickDevice m_joystick;

401
src/app/input.cpp Normal file
View File

@ -0,0 +1,401 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#include "app/input.h"
#include "common/logger.h"
#include "common/restext.h"
#include "graphics/engine/engine.h"
#include "object/robotmain.h"
#include <sstream>
template<> CInput* CSingleton<CInput>::m_instance = nullptr;
CInput::CInput()
{
m_kmodState = 0;
m_trackedKeys = 0;
m_mousePos = Math::Point();
m_mouseButtonsState = 0;
m_joystickDeadzone = 0.2f;
SetDefaultInputBindings();
}
void CInput::EventProcess(Event& event)
{
if(event.type == EVENT_KEY_DOWN ||
event.type == EVENT_KEY_UP)
{
// Use the occasion to update kmods
m_kmodState = event.kmodState;
}
// Use the occasion to update mouse button state
if (event.type == EVENT_MOUSE_BUTTON_DOWN)
{
m_mouseButtonsState |= event.mouseButton.button;
}
if(event.type == EVENT_MOUSE_BUTTON_UP)
{
m_mouseButtonsState &= ~event.mouseButton.button;
}
if (event.type == EVENT_KEY_DOWN)
{
if (event.key.key == KEY(KP8))
m_trackedKeys |= TRKEY_NUM_UP;
else if (event.key.key == KEY(KP2))
m_trackedKeys |= TRKEY_NUM_DOWN;
else if (event.key.key == KEY(KP4))
m_trackedKeys |= TRKEY_NUM_LEFT;
else if (event.key.key == KEY(KP6))
m_trackedKeys |= TRKEY_NUM_RIGHT;
else if (event.key.key == KEY(KP_PLUS))
m_trackedKeys |= TRKEY_NUM_PLUS;
else if (event.key.key == KEY(KP_MINUS))
m_trackedKeys |= TRKEY_NUM_MINUS;
else if (event.key.key == KEY(PAGEUP))
m_trackedKeys |= TRKEY_PAGE_UP;
else if (event.key.key == KEY(PAGEDOWN))
m_trackedKeys |= TRKEY_PAGE_DOWN;
}
else if (event.type == EVENT_KEY_UP)
{
if (event.key.key == KEY(KP8))
m_trackedKeys &= ~TRKEY_NUM_UP;
else if (event.key.key == KEY(KP2))
m_trackedKeys &= ~TRKEY_NUM_DOWN;
else if (event.key.key == KEY(KP4))
m_trackedKeys &= ~TRKEY_NUM_LEFT;
else if (event.key.key == KEY(KP6))
m_trackedKeys &= ~TRKEY_NUM_RIGHT;
else if (event.key.key == KEY(KP_PLUS))
m_trackedKeys &= ~TRKEY_NUM_PLUS;
else if (event.key.key == KEY(KP_MINUS))
m_trackedKeys &= ~TRKEY_NUM_MINUS;
else if (event.key.key == KEY(PAGEUP))
m_trackedKeys &= ~TRKEY_PAGE_UP;
else if (event.key.key == KEY(PAGEDOWN))
m_trackedKeys &= ~TRKEY_PAGE_DOWN;
}
if(event.type == EVENT_KEY_DOWN ||
event.type == EVENT_KEY_UP)
{
event.key.slot = FindBinding(event.key.key);
}
event.trackedKeysState = m_trackedKeys;
event.kmodState = m_kmodState;
event.mousePos = m_mousePos;
event.mouseButtonsState = m_mouseButtonsState;
/* Motion vector management */
if (event.type == EVENT_KEY_DOWN)
{
if (event.key.slot == INPUT_SLOT_UP ) m_keyMotion.y = 1.0f;
if (event.key.slot == INPUT_SLOT_DOWN ) m_keyMotion.y = -1.0f;
if (event.key.slot == INPUT_SLOT_LEFT ) m_keyMotion.x = -1.0f;
if (event.key.slot == INPUT_SLOT_RIGHT) m_keyMotion.x = 1.0f;
if (event.key.slot == INPUT_SLOT_GUP ) m_keyMotion.z = 1.0f;
if (event.key.slot == INPUT_SLOT_GDOWN) m_keyMotion.z = -1.0f;
}
else if (event.type == EVENT_KEY_UP)
{
if (event.key.slot == INPUT_SLOT_UP ) m_keyMotion.y = 0.0f;
if (event.key.slot == INPUT_SLOT_DOWN ) m_keyMotion.y = 0.0f;
if (event.key.slot == INPUT_SLOT_LEFT ) m_keyMotion.x = 0.0f;
if (event.key.slot == INPUT_SLOT_RIGHT) m_keyMotion.x = 0.0f;
if (event.key.slot == INPUT_SLOT_GUP ) m_keyMotion.z = 0.0f;
if (event.key.slot == INPUT_SLOT_GDOWN) m_keyMotion.z = 0.0f;
}
else if (event.type == EVENT_JOY_AXIS)
{
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_X).axis)
{
m_joyMotion.x = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_X).invert)
m_joyMotion.x *= -1.0f;
}
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Y).axis)
{
m_joyMotion.y = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_Y).invert)
m_joyMotion.y *= -1.0f;
}
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Z).axis)
{
m_joyMotion.z = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_Z).invert)
m_joyMotion.z *= -1.0f;
}
}
event.motionInput = Math::Clamp(m_joyMotion + m_keyMotion, Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector(1.0f, 1.0f, 1.0f));
}
void CInput::MouseMove(Math::IntPoint pos)
{
m_mousePos = Gfx::CEngine::GetInstancePointer()->WindowToInterfaceCoords(pos);
}
int CInput::GetKmods() const
{
return m_kmodState;
}
bool CInput::GetKmodState(int kmod) const
{
return (m_kmodState & kmod) != 0;
}
bool CInput::GetTrackedKeyState(TrackedKey key) const
{
return (m_trackedKeys & key) != 0;
}
bool CInput::GetMouseButtonState(int index) const
{
return (m_mouseButtonsState & (1<<index)) != 0;
}
void CInput::ResetKeyStates()
{
CLogger::GetInstancePointer()->Trace("Reset key states\n");
m_trackedKeys = 0;
m_kmodState = 0;
m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
}
Math::Point CInput::GetMousePos() const
{
return m_mousePos;
}
void CInput::SetDefaultInputBindings()
{
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
m_inputBindings[i].primary = m_inputBindings[i].secondary = KEY_INVALID;
}
for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++)
{
m_joyAxisBindings[i].axis = AXIS_INVALID;
m_joyAxisBindings[i].invert = false;
}
m_inputBindings[INPUT_SLOT_LEFT ].primary = KEY(LEFT);
m_inputBindings[INPUT_SLOT_RIGHT ].primary = KEY(RIGHT);
m_inputBindings[INPUT_SLOT_UP ].primary = KEY(UP);
m_inputBindings[INPUT_SLOT_DOWN ].primary = KEY(DOWN);
m_inputBindings[INPUT_SLOT_LEFT ].secondary = KEY(a);
m_inputBindings[INPUT_SLOT_RIGHT ].secondary = KEY(d);
m_inputBindings[INPUT_SLOT_UP ].secondary = KEY(w);
m_inputBindings[INPUT_SLOT_DOWN ].secondary = KEY(s);
m_inputBindings[INPUT_SLOT_GUP ].primary = VIRTUAL_KMOD(SHIFT);
m_inputBindings[INPUT_SLOT_GDOWN ].primary = VIRTUAL_KMOD(CTRL);
m_inputBindings[INPUT_SLOT_CAMERA ].primary = KEY(SPACE);
// m_inputBindings[INPUT_SLOT_CAMERA ].secondary = VIRTUAL_JOY(2);
m_inputBindings[INPUT_SLOT_DESEL ].primary = KEY(KP0);
// m_inputBindings[INPUT_SLOT_DESEL ].secondary = VIRTUAL_JOY(6);
m_inputBindings[INPUT_SLOT_ACTION ].primary = KEY(RETURN);
// m_inputBindings[INPUT_SLOT_ACTION ].secondary = VIRTUAL_JOY(1);
m_inputBindings[INPUT_SLOT_ACTION ].secondary = KEY(e);
m_inputBindings[INPUT_SLOT_NEAR ].primary = KEY(KP_PLUS);
// m_inputBindings[INPUT_SLOT_NEAR ].secondary = VIRTUAL_JOY(5);
m_inputBindings[INPUT_SLOT_AWAY ].primary = KEY(KP_MINUS);
// m_inputBindings[INPUT_SLOT_AWAY ].secondary = VIRTUAL_JOY(4);
m_inputBindings[INPUT_SLOT_NEXT ].primary = KEY(TAB);
// m_inputBindings[INPUT_SLOT_NEXT ].secondary = VIRTUAL_JOY(3);
m_inputBindings[INPUT_SLOT_HUMAN ].primary = KEY(HOME);
// m_inputBindings[INPUT_SLOT_HUMAN ].secondary = VIRTUAL_JOY(7);
m_inputBindings[INPUT_SLOT_QUIT ].primary = KEY(ESCAPE);
m_inputBindings[INPUT_SLOT_HELP ].primary = KEY(F1);
m_inputBindings[INPUT_SLOT_PROG ].primary = KEY(F2);
m_inputBindings[INPUT_SLOT_CBOT ].primary = KEY(F3);
m_inputBindings[INPUT_SLOT_VISIT ].primary = KEY(KP_PERIOD);
m_inputBindings[INPUT_SLOT_SPEED10].primary = KEY(F4);
m_inputBindings[INPUT_SLOT_SPEED15].primary = KEY(F5);
m_inputBindings[INPUT_SLOT_SPEED20].primary = KEY(F6);
m_joyAxisBindings[JOY_AXIS_SLOT_X].axis = 0;
m_joyAxisBindings[JOY_AXIS_SLOT_Y].axis = 1;
m_joyAxisBindings[JOY_AXIS_SLOT_Z].axis = 2;
}
void CInput::SetInputBinding(InputSlot slot, InputBinding binding)
{
unsigned int index = static_cast<unsigned int>(slot);
m_inputBindings[index] = binding;
}
const InputBinding& CInput::GetInputBinding(InputSlot slot)
{
unsigned int index = static_cast<unsigned int>(slot);
return m_inputBindings[index];
}
void CInput::SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding)
{
unsigned int index = static_cast<unsigned int>(slot);
m_joyAxisBindings[index] = binding;
}
const JoyAxisBinding& CInput::GetJoyAxisBinding(JoyAxisSlot slot)
{
unsigned int index = static_cast<unsigned int>(slot);
return m_joyAxisBindings[index];
}
void CInput::SetJoystickDeadzone(float zone)
{
m_joystickDeadzone = zone;
}
float CInput::GetJoystickDeadzone()
{
return m_joystickDeadzone;
}
InputSlot CInput::FindBinding(unsigned int key)
{
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputSlot slot = static_cast<InputSlot>(i);
InputBinding b = GetInputBinding(slot);
if(b.primary == key || b.secondary == key)
return slot;
}
return INPUT_SLOT_MAX;
}
std::string CInput::SaveKeyBindings()
{
std::stringstream key;
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputBinding b = GetInputBinding(static_cast<InputSlot>(i));
key << b.primary << " ";
key << b.secondary << " ";
}
return key.str();
}
void CInput::LoadKeyBindings(std::string keys)
{
std::stringstream skey;
skey.str(keys);
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputBinding b;
skey >> b.primary;
skey >> b.secondary;
SetInputBinding(static_cast<InputSlot>(i), b);
}
}
static std::map<InputSlot, std::string> keyTable =
{
{ INPUT_SLOT_LEFT, "left" },
{ INPUT_SLOT_RIGHT, "right" },
{ INPUT_SLOT_UP, "up" },
{ INPUT_SLOT_DOWN, "down" },
{ INPUT_SLOT_GUP, "gup" },
{ INPUT_SLOT_GDOWN, "gdown" },
{ INPUT_SLOT_CAMERA, "camera" },
{ INPUT_SLOT_DESEL, "desel" },
{ INPUT_SLOT_ACTION, "action" },
{ INPUT_SLOT_NEAR, "near" },
{ INPUT_SLOT_AWAY, "away" },
{ INPUT_SLOT_NEXT, "next" },
{ INPUT_SLOT_HUMAN, "human" },
{ INPUT_SLOT_QUIT, "quit" },
{ INPUT_SLOT_HELP, "help" },
{ INPUT_SLOT_PROG, "prog" },
{ INPUT_SLOT_CBOT, "cbot" },
{ INPUT_SLOT_VISIT, "visit" },
{ INPUT_SLOT_SPEED10, "speed10" },
{ INPUT_SLOT_SPEED15, "speed15" },
{ INPUT_SLOT_SPEED20, "speed20" }
};
InputSlot CInput::SearchKeyById(std::string id)
{
for(auto& key : keyTable)
{
if ( id == key.second )
{
return key.first;
}
}
return INPUT_SLOT_MAX;
}
std::string CInput::GetKeysString(InputBinding b)
{
std::ostringstream ss;
if ( b.primary != KEY_INVALID )
{
std::string iNameStr;
if ( GetResource(RES_KEY, b.primary, iNameStr) )
{
ss << iNameStr;
if ( b.secondary != KEY_INVALID )
{
if ( GetResource(RES_KEY, b.secondary, iNameStr) )
{
std::string textStr;
GetResource(RES_TEXT, RT_KEY_OR, textStr);
ss << textStr << iNameStr;
}
}
}
}
else
{
return "?";
}
return ss.str();
}
std::string CInput::GetKeysString(InputSlot slot)
{
InputBinding b = GetInputBinding(slot);
return GetKeysString(b);
}

177
src/app/input.h Normal file
View File

@ -0,0 +1,177 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
/**
* \file app/input.h
* \brief CInput class
*/
#pragma once
#include "common/event.h"
#include "common/key.h"
#include "common/singleton.h"
#include "math/intpoint.h"
/**
* \enum TrackedKey
* \brief Additional keys whose state (pressed/released) is tracked by CInput
*/
enum TrackedKey
{
TRKEY_NUM_UP = (1<<0),
TRKEY_NUM_DOWN = (1<<1),
TRKEY_NUM_LEFT = (1<<2),
TRKEY_NUM_RIGHT = (1<<3),
TRKEY_NUM_PLUS = (1<<4),
TRKEY_NUM_MINUS = (1<<5),
TRKEY_PAGE_UP = (1<<6),
TRKEY_PAGE_DOWN = (1<<7)
};
/**
* \struct InputBinding
* \brief Binding for input slot
*/
struct InputBinding
{
//! Primary and secondary bindings
//! Can be regular key, virtual key or virtual joystick button
unsigned int primary, secondary;
InputBinding(unsigned int p = KEY_INVALID, unsigned int s = KEY_INVALID)
: primary(p), secondary(s) {}
};
/**
* \struct JoyAxisBinding
* \brief Binding for joystick axis
*/
struct JoyAxisBinding
{
//! Axis index or AXIS_INVALID
int axis;
//! True to invert axis value
bool invert;
};
//! Invalid value for axis binding (no axis assigned)
const int AXIS_INVALID = -1;
/**
* \class CInput
* \brief Management of mouse, keyboard and joystick
*/
class CInput : public CSingleton<CInput>
{
public:
//! Constructor
CInput();
//! Process an incoming event, also sets .trackedKeysState, .kmodState, .mousePos, .mouseButtonsState and .key.slot
void EventProcess(Event &event);
//! Called by CApplication on SDL MOUSE_MOTION event
void MouseMove(Math::IntPoint pos);
//! Returns the current key modifiers
int GetKmods() const;
//! Returns whether the given kmod is active
bool GetKmodState(int kmod) const;
//! Returns whether the tracked key is pressed
bool GetTrackedKeyState(TrackedKey key) const;
//! Returns whether the mouse button is pressed
bool GetMouseButtonState(int index) const;
//! Resets tracked key states and modifiers
void ResetKeyStates();
//! Returns the position of mouse cursor (in interface coords)
Math::Point GetMousePos() const;
//! Sets the default input bindings (key and axes)
void SetDefaultInputBindings();
//! Management of input bindings
//@{
void SetInputBinding(InputSlot slot, InputBinding binding);
const InputBinding& GetInputBinding(InputSlot slot);
//@}
//! Management of joystick axis bindings
//@{
void SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding);
const JoyAxisBinding& GetJoyAxisBinding(JoyAxisSlot slot);
//@}
//! Management of joystick deadzone
//@{
void SetJoystickDeadzone(float zone);
float GetJoystickDeadzone();
//@}
//! Get binding slot for given key
InputSlot FindBinding(unsigned int key);
//! Saving/loading key bindings to string (for storing in colobot.ini)
//@{
std::string SaveKeyBindings();
void LoadKeyBindings(std::string keys);
//@}
//! Seeks a InputSlot by id. Returns INPUT_SLOT_MAX if not found
InputSlot SearchKeyById(std::string name);
//! Returns string describing keys to pressed
//@{
std::string GetKeysString(InputBinding binding);
std::string GetKeysString(InputSlot slot);
//@}
private:
//! Current state of key modifiers (bitmask of SDLMod)
unsigned int m_kmodState;
//! Current state of some tracked keys (bitmask of TrackedKey enum values)
unsigned int m_trackedKeys;
//! Current position of mouse cursor
Math::Point m_mousePos;
//! Current state of mouse buttons (bitmask of MouseButton enum values)
unsigned int m_mouseButtonsState;
//! Motion vector set by keyboard or joystick buttons
Math::Vector m_keyMotion;
//! Motion vector set by joystick axes
Math::Vector m_joyMotion;
//! Bindings for user inputs
InputBinding m_inputBindings[INPUT_SLOT_MAX];
JoyAxisBinding m_joyAxisBindings[JOY_AXIS_SLOT_MAX];
float m_joystickDeadzone;
};

View File

@ -579,6 +579,8 @@ struct KeyEventData
//! Unicode character
//! NOTE: applicable only to EVENT_KEY_DOWN events!
unsigned int unicode;
//! Input binding slot for this key
InputSlot slot;
};
/**

View File

@ -229,53 +229,6 @@ enum ResearchType
RESEARCH_SNIFFER = (1<<12) //! < sniffer
};
/**
* \enum InputSlot
* \brief Available slots for input bindings
*/
enum InputSlot
{
INPUT_SLOT_LEFT = 0,
INPUT_SLOT_RIGHT = 1,
INPUT_SLOT_UP = 2,
INPUT_SLOT_DOWN = 3,
INPUT_SLOT_GUP = 4,
INPUT_SLOT_GDOWN = 5,
INPUT_SLOT_CAMERA = 6,
INPUT_SLOT_DESEL = 7,
INPUT_SLOT_ACTION = 8,
INPUT_SLOT_NEAR = 9,
INPUT_SLOT_AWAY = 10,
INPUT_SLOT_NEXT = 11,
INPUT_SLOT_HUMAN = 12,
INPUT_SLOT_QUIT = 13,
INPUT_SLOT_HELP = 14,
INPUT_SLOT_PROG = 15,
INPUT_SLOT_VISIT = 16,
INPUT_SLOT_SPEED10 = 17,
INPUT_SLOT_SPEED15 = 18,
INPUT_SLOT_SPEED20 = 19,
INPUT_SLOT_SPEED30 = 20,
INPUT_SLOT_AIMUP = 21,
INPUT_SLOT_AIMDOWN = 22,
INPUT_SLOT_CBOT = 23,
INPUT_SLOT_MAX
};
/**
* \enum JoyAxisSlot
* \brief Slots for joystick axes inputs
*/
enum JoyAxisSlot
{
JOY_AXIS_SLOT_X,
JOY_AXIS_SLOT_Y,
JOY_AXIS_SLOT_Z,
JOY_AXIS_SLOT_MAX
};
// TODO: move to CRobotMain
extern long g_id; // unique identifier

View File

@ -65,3 +65,49 @@ enum VirtualKmod
//! Special value for invalid key bindings
const unsigned int KEY_INVALID = SDLK_LAST + 1000;
/**
* \enum InputSlot
* \brief Available slots for input bindings
*/
enum InputSlot
{
INPUT_SLOT_LEFT = 0,
INPUT_SLOT_RIGHT = 1,
INPUT_SLOT_UP = 2,
INPUT_SLOT_DOWN = 3,
INPUT_SLOT_GUP = 4,
INPUT_SLOT_GDOWN = 5,
INPUT_SLOT_CAMERA = 6,
INPUT_SLOT_DESEL = 7,
INPUT_SLOT_ACTION = 8,
INPUT_SLOT_NEAR = 9,
INPUT_SLOT_AWAY = 10,
INPUT_SLOT_NEXT = 11,
INPUT_SLOT_HUMAN = 12,
INPUT_SLOT_QUIT = 13,
INPUT_SLOT_HELP = 14,
INPUT_SLOT_PROG = 15,
INPUT_SLOT_VISIT = 16,
INPUT_SLOT_SPEED10 = 17,
INPUT_SLOT_SPEED15 = 18,
INPUT_SLOT_SPEED20 = 19,
INPUT_SLOT_SPEED30 = 20,
INPUT_SLOT_AIMUP = 21,
INPUT_SLOT_AIMDOWN = 22,
INPUT_SLOT_CBOT = 23,
INPUT_SLOT_MAX
};
/**
* \enum JoyAxisSlot
* \brief Slots for joystick axes inputs
*/
enum JoyAxisSlot
{
JOY_AXIS_SLOT_X,
JOY_AXIS_SLOT_Y,
JOY_AXIS_SLOT_Z,
JOY_AXIS_SLOT_MAX
};

View File

@ -20,6 +20,8 @@
#include "common/restext.h"
#include "app/input.h"
#include "common/config.h"
#include "common/global.h"
@ -745,53 +747,6 @@ void SetGlobalGamerName(std::string name)
strcpy(g_gamerName, name.c_str());
}
struct KeyDesc
{
InputSlot key;
char name[20];
};
static KeyDesc keyTable[22] =
{
{ INPUT_SLOT_LEFT, "left;" },
{ INPUT_SLOT_RIGHT, "right;" },
{ INPUT_SLOT_UP, "up;" },
{ INPUT_SLOT_DOWN, "down;" },
{ INPUT_SLOT_GUP, "gup;" },
{ INPUT_SLOT_GDOWN, "gdown;" },
{ INPUT_SLOT_CAMERA, "camera;" },
{ INPUT_SLOT_DESEL, "desel;" },
{ INPUT_SLOT_ACTION, "action;" },
{ INPUT_SLOT_NEAR, "near;" },
{ INPUT_SLOT_AWAY, "away;" },
{ INPUT_SLOT_NEXT, "next;" },
{ INPUT_SLOT_HUMAN, "human;" },
{ INPUT_SLOT_QUIT, "quit;" },
{ INPUT_SLOT_HELP, "help;" },
{ INPUT_SLOT_PROG, "prog;" },
{ INPUT_SLOT_CBOT, "cbot;" },
{ INPUT_SLOT_VISIT, "visit;" },
{ INPUT_SLOT_SPEED10, "speed10;" },
{ INPUT_SLOT_SPEED15, "speed15;" },
{ INPUT_SLOT_SPEED20, "speed20;" }
};
// Seeks a key.
bool SearchKey(const char *cmd, InputSlot &key)
{
for (int i = 0; i < 22 ;i++)
{
if ( strstr(cmd, keyTable[i].name) == cmd )
{
key = keyTable[i].key;
return true;
}
}
return false;
}
// Replaces the commands "\key name;" in a text.
static void PutKeyName(std::string& dst, const char* src)
@ -807,21 +762,12 @@ static void PutKeyName(std::string& dst, const char* src)
src[s+3] == 'y' &&
src[s+4] == ' ' )
{
InputSlot key;
if ( SearchKey(src+s+5, key) )
{
unsigned int res = CRobotMain::GetInstancePointer()->GetInputBinding(key).primary;
if (res != KEY_INVALID)
{
std::string keyName;
if ( GetResource(RES_KEY, res, keyName) )
{
dst.append(keyName);
while ( src[s++] != ';' );
continue;
}
}
}
int count;
for(count = 0; src[s+5+count] != ';'; count++);
CInput* input = CInput::GetInstancePointer();
InputSlot key = input->SearchKeyById(std::string(&src[s+5], count));
dst.append(input->GetKeysString(key));
s = s+5+count+1;
}
dst.append(1, src[s++]);

View File

@ -154,6 +154,5 @@ enum ResTextType
void InitializeRestext();
void SetGlobalGamerName(std::string name);
bool SearchKey(const char *cmd, InputSlot& slot);
bool GetResource(ResType type, int num, std::string& text);

View File

@ -21,6 +21,7 @@
#include "graphics/engine/camera.h"
#include "app/app.h"
#include "app/input.h"
#include "common/iman.h"

View File

@ -21,6 +21,7 @@
#include "graphics/engine/engine.h"
#include "app/app.h"
#include "app/input.h"
#include "common/image.h"
#include "common/key.h"
@ -4292,7 +4293,7 @@ void CEngine::DrawMouse()
int index = static_cast<int>(m_mouseType);
Math::Point pos = m_app->GetMousePos();
Math::Point pos = CInput::GetInstancePointer()->GetMousePos();
pos.x = pos.x - (m_mice[index].hotPoint.x * m_mouseSize.x) / 32.0f;
pos.y = pos.y - ((32.0f - m_mice[index].hotPoint.y) * m_mouseSize.y) / 32.0f;

View File

@ -226,8 +226,7 @@ bool CBrain::EventProcess(const Event &event)
action = EVENT_NULL;
if ( event.type == EVENT_KEY_DOWN &&
(event.key.key == m_main->GetInputBinding(INPUT_SLOT_ACTION).primary ||
event.key.key == m_main->GetInputBinding(INPUT_SLOT_ACTION).secondary ) &&
event.key.slot == INPUT_SLOT_ACTION &&
!m_main->GetEditLock() )
{
pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));

View File

@ -23,6 +23,7 @@
#include "CBot/CBotDll.h"
#include "app/app.h"
#include "app/input.h"
#include "common/event.h"
#include "common/global.h"
@ -629,6 +630,7 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile)
m_lightning = m_engine->GetLightning();
m_planet = m_engine->GetPlanet();
m_pause = CPauseManager::GetInstancePointer();
m_input = CInput::GetInstancePointer();
m_interface = new Ui::CInterface();
m_terrain = new Gfx::CTerrain();
@ -714,9 +716,6 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile)
m_autosaveSlots = 3;
m_autosaveLast = 0.0f;
m_joystickDeadzone = 0.2f;
SetDefaultInputBindings();
FlushDisplayInfo();
m_fontSize = 19.0f;
@ -935,6 +934,8 @@ CRobotMain::~CRobotMain()
delete m_map;
m_map = nullptr;
m_input = nullptr;
m_pause = nullptr;
m_app = nullptr;
}
@ -1002,98 +1003,6 @@ void CRobotMain::CreateIni()
GetProfile().Save();
}
void CRobotMain::SetDefaultInputBindings()
{
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
m_inputBindings[i].primary = m_inputBindings[i].secondary = KEY_INVALID;
}
for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++)
{
m_joyAxisBindings[i].axis = AXIS_INVALID;
m_joyAxisBindings[i].invert = false;
}
m_inputBindings[INPUT_SLOT_LEFT ].primary = KEY(LEFT);
m_inputBindings[INPUT_SLOT_RIGHT ].primary = KEY(RIGHT);
m_inputBindings[INPUT_SLOT_UP ].primary = KEY(UP);
m_inputBindings[INPUT_SLOT_DOWN ].primary = KEY(DOWN);
m_inputBindings[INPUT_SLOT_LEFT ].secondary = KEY(a);
m_inputBindings[INPUT_SLOT_RIGHT ].secondary = KEY(d);
m_inputBindings[INPUT_SLOT_UP ].secondary = KEY(w);
m_inputBindings[INPUT_SLOT_DOWN ].secondary = KEY(s);
m_inputBindings[INPUT_SLOT_GUP ].primary = VIRTUAL_KMOD(SHIFT);
m_inputBindings[INPUT_SLOT_GDOWN ].primary = VIRTUAL_KMOD(CTRL);
m_inputBindings[INPUT_SLOT_CAMERA ].primary = KEY(SPACE);
// m_inputBindings[INPUT_SLOT_CAMERA ].secondary = VIRTUAL_JOY(2);
m_inputBindings[INPUT_SLOT_DESEL ].primary = KEY(KP0);
// m_inputBindings[INPUT_SLOT_DESEL ].secondary = VIRTUAL_JOY(6);
m_inputBindings[INPUT_SLOT_ACTION ].primary = KEY(RETURN);
// m_inputBindings[INPUT_SLOT_ACTION ].secondary = VIRTUAL_JOY(1);
m_inputBindings[INPUT_SLOT_ACTION ].secondary = KEY(e);
m_inputBindings[INPUT_SLOT_NEAR ].primary = KEY(KP_PLUS);
// m_inputBindings[INPUT_SLOT_NEAR ].secondary = VIRTUAL_JOY(5);
m_inputBindings[INPUT_SLOT_AWAY ].primary = KEY(KP_MINUS);
// m_inputBindings[INPUT_SLOT_AWAY ].secondary = VIRTUAL_JOY(4);
m_inputBindings[INPUT_SLOT_NEXT ].primary = KEY(TAB);
// m_inputBindings[INPUT_SLOT_NEXT ].secondary = VIRTUAL_JOY(3);
m_inputBindings[INPUT_SLOT_HUMAN ].primary = KEY(HOME);
// m_inputBindings[INPUT_SLOT_HUMAN ].secondary = VIRTUAL_JOY(7);
m_inputBindings[INPUT_SLOT_QUIT ].primary = KEY(ESCAPE);
m_inputBindings[INPUT_SLOT_HELP ].primary = KEY(F1);
m_inputBindings[INPUT_SLOT_PROG ].primary = KEY(F2);
m_inputBindings[INPUT_SLOT_CBOT ].primary = KEY(F3);
m_inputBindings[INPUT_SLOT_VISIT ].primary = KEY(KP_PERIOD);
m_inputBindings[INPUT_SLOT_SPEED10].primary = KEY(F4);
m_inputBindings[INPUT_SLOT_SPEED15].primary = KEY(F5);
m_inputBindings[INPUT_SLOT_SPEED20].primary = KEY(F6);
m_joyAxisBindings[JOY_AXIS_SLOT_X].axis = 0;
m_joyAxisBindings[JOY_AXIS_SLOT_Y].axis = 1;
m_joyAxisBindings[JOY_AXIS_SLOT_Z].axis = 2;
}
void CRobotMain::SetInputBinding(InputSlot slot, InputBinding binding)
{
unsigned int index = static_cast<unsigned int>(slot);
m_inputBindings[index] = binding;
}
const InputBinding& CRobotMain::GetInputBinding(InputSlot slot)
{
unsigned int index = static_cast<unsigned int>(slot);
return m_inputBindings[index];
}
void CRobotMain::SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding)
{
unsigned int index = static_cast<unsigned int>(slot);
m_joyAxisBindings[index] = binding;
}
const JoyAxisBinding& CRobotMain::GetJoyAxisBinding(JoyAxisSlot slot)
{
unsigned int index = static_cast<unsigned int>(slot);
return m_joyAxisBindings[index];
}
void CRobotMain::SetJoystickDeadzone(float zone)
{
m_joystickDeadzone = zone;
}
float CRobotMain::GetJoystickDeadzone()
{
return m_joystickDeadzone;
}
void CRobotMain::ResetKeyStates()
{
m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
}
//! Changes phase
void CRobotMain::ChangePhase(Phase phase)
{
@ -1345,66 +1254,6 @@ void CRobotMain::ChangePhase(Phase phase)
//! Processes an event
bool CRobotMain::ProcessEvent(Event &event)
{
/* Motion vector management */
if (event.type == EVENT_KEY_DOWN)
{
if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).primary) m_keyMotion.y = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).secondary) m_keyMotion.y = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).primary) m_keyMotion.y = -1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).secondary) m_keyMotion.y = -1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).primary) m_keyMotion.x = -1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).secondary) m_keyMotion.x = -1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).primary) m_keyMotion.x = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).secondary) m_keyMotion.x = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).primary) m_keyMotion.z = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).secondary) m_keyMotion.z = 1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).primary) m_keyMotion.z = -1.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).secondary) m_keyMotion.z = -1.0f;
}
else if (event.type == EVENT_KEY_UP)
{
if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).primary) m_keyMotion.y = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).secondary) m_keyMotion.y = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).primary) m_keyMotion.y = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).secondary) m_keyMotion.y = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).primary) m_keyMotion.x = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).secondary) m_keyMotion.x = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).primary) m_keyMotion.x = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).secondary) m_keyMotion.x = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).primary) m_keyMotion.z = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).secondary) m_keyMotion.z = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).primary) m_keyMotion.z = 0.0f;
if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).secondary) m_keyMotion.z = 0.0f;
}
else if (event.type == EVENT_JOY_AXIS)
{
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_X).axis)
{
m_joyMotion.x = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_X).invert)
m_joyMotion.x *= -1.0f;
}
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Y).axis)
{
m_joyMotion.y = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_Y).invert)
m_joyMotion.y *= -1.0f;
}
if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Z).axis)
{
m_joyMotion.z = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
if (GetJoyAxisBinding(JOY_AXIS_SLOT_Z).invert)
m_joyMotion.z *= -1.0f;
}
}
event.motionInput = Math::Clamp(m_joyMotion + m_keyMotion, Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector(1.0f, 1.0f, 1.0f));
if (event.type == EVENT_FRAME)
{
if (!m_movie->EventProcess(event)) // end of the movie?
@ -1494,10 +1343,8 @@ bool CRobotMain::ProcessEvent(Event &event)
if (event.type == EVENT_KEY_DOWN)
{
if (event.key.key == GetInputBinding(INPUT_SLOT_HELP).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_HELP).secondary ||
event.key.key == GetInputBinding(INPUT_SLOT_PROG).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_PROG).secondary ||
if (event.key.slot == INPUT_SLOT_HELP ||
event.key.slot == INPUT_SLOT_PROG ||
event.key.key == KEY(ESCAPE))
{
StopDisplayInfo();
@ -1521,7 +1368,7 @@ bool CRobotMain::ProcessEvent(Event &event)
switch (event.type)
{
case EVENT_KEY_DOWN:
KeyCamera(event.type, event.key.key);
KeyCamera(event.type, event.key.slot);
HiliteClear();
if (event.key.key == KEY(F11))
{
@ -1530,14 +1377,12 @@ bool CRobotMain::ProcessEvent(Event &event)
}
if (m_editLock) // current edition?
{
if (event.key.key == GetInputBinding(INPUT_SLOT_HELP).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_HELP).secondary)
if (event.key.slot == INPUT_SLOT_HELP)
{
StartDisplayInfo(SATCOM_HUSTON, false);
return false;
}
if (event.key.key == GetInputBinding(INPUT_SLOT_PROG).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_PROG).secondary)
if (event.key.slot == INPUT_SLOT_PROG)
{
StartDisplayInfo(SATCOM_PROG, false);
return false;
@ -1546,8 +1391,7 @@ bool CRobotMain::ProcessEvent(Event &event)
}
if (m_movieLock) // current movie?
{
if (event.key.key == GetInputBinding(INPUT_SLOT_QUIT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_QUIT).secondary ||
if (event.key.slot == INPUT_SLOT_QUIT ||
event.key.key == KEY(ESCAPE))
{
AbortMovie();
@ -1556,21 +1400,18 @@ bool CRobotMain::ProcessEvent(Event &event)
}
if (m_camera->GetType() == Gfx::CAM_TYPE_VISIT)
{
if (event.key.key == GetInputBinding(INPUT_SLOT_VISIT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_VISIT).secondary)
if (event.key.slot == INPUT_SLOT_VISIT)
{
StartDisplayVisit(EVENT_NULL);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_QUIT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_QUIT).secondary ||
if (event.key.slot == INPUT_SLOT_QUIT ||
event.key.key == KEY(ESCAPE))
{
StopDisplayVisit();
}
return false;
}
if (event.key.key == GetInputBinding(INPUT_SLOT_QUIT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_QUIT).secondary)
if (event.key.slot == INPUT_SLOT_QUIT)
{
if (m_movie->IsExist())
StartDisplayInfo(SATCOM_HUSTON, false);
@ -1581,7 +1422,7 @@ bool CRobotMain::ProcessEvent(Event &event)
else if (!m_cmdEdit)
m_dialog->StartAbort(); // do you want to leave?
}
if (event.key.key == KEY(PAUSE))
if (event.key.key == KEY(PAUSE)) //TODO: key binding
{
if (!m_movieLock && !m_editLock && !m_cmdEdit &&
m_camera->GetType() != Gfx::CAM_TYPE_VISIT &&
@ -1590,62 +1431,52 @@ bool CRobotMain::ProcessEvent(Event &event)
ChangePause(m_pause->GetPause(PAUSE_USER) ? PAUSE_NONE : PAUSE_USER);
}
}
if (event.key.key == GetInputBinding(INPUT_SLOT_CAMERA).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_CAMERA).secondary)
if (event.key.slot == INPUT_SLOT_CAMERA)
{
ChangeCamera();
}
if (event.key.key == GetInputBinding(INPUT_SLOT_DESEL).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_DESEL).secondary)
if (event.key.slot == INPUT_SLOT_DESEL)
{
if (m_shortCut)
DeselectObject();
}
if (event.key.key == GetInputBinding(INPUT_SLOT_HUMAN).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_HUMAN).secondary)
if (event.key.slot == INPUT_SLOT_HUMAN)
{
SelectHuman();
}
if (event.key.key == GetInputBinding(INPUT_SLOT_NEXT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_NEXT).secondary)
if (event.key.slot == INPUT_SLOT_NEXT)
{
if (m_shortCut)
m_short->SelectNext();
}
if (event.key.key == GetInputBinding(INPUT_SLOT_HELP).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_HELP).secondary)
if (event.key.slot == INPUT_SLOT_HELP)
{
StartDisplayInfo(SATCOM_HUSTON, true);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_PROG).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_PROG).secondary)
if (event.key.slot == INPUT_SLOT_PROG)
{
StartDisplayInfo(SATCOM_PROG, true);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_VISIT).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_VISIT).secondary)
if (event.key.slot == INPUT_SLOT_VISIT)
{
StartDisplayVisit(EVENT_NULL);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_SPEED10).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_SPEED10).secondary)
if (event.key.slot == INPUT_SLOT_SPEED10)
{
SetSpeed(1.0f);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_SPEED15).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_SPEED15).secondary)
if (event.key.slot == INPUT_SLOT_SPEED15)
{
SetSpeed(1.5f);
}
if (event.key.key == GetInputBinding(INPUT_SLOT_SPEED20).primary ||
event.key.key == GetInputBinding(INPUT_SLOT_SPEED20).secondary)
if (event.key.slot == INPUT_SLOT_SPEED20)
{
SetSpeed(2.0f);
}
break;
case EVENT_KEY_UP:
KeyCamera(event.type, event.key.key);
KeyCamera(event.type, event.key.slot);
break;
case EVENT_MOUSE_BUTTON_DOWN:
@ -3263,32 +3094,26 @@ void CRobotMain::ChangeCamera()
}
//! Remote control the camera using the arrow keys
void CRobotMain::KeyCamera(EventType type, unsigned int key)
void CRobotMain::KeyCamera(EventType type, InputSlot key)
{
// TODO: rewrite key handling to input bindings
if (type == EVENT_KEY_UP)
{
if (key == GetInputBinding(INPUT_SLOT_LEFT).primary ||
key == GetInputBinding(INPUT_SLOT_LEFT).secondary)
if (key == INPUT_SLOT_LEFT)
{
m_cameraPan = 0.0f;
}
if (key == GetInputBinding(INPUT_SLOT_RIGHT).primary ||
key == GetInputBinding(INPUT_SLOT_RIGHT).secondary)
if (key == INPUT_SLOT_RIGHT)
{
m_cameraPan = 0.0f;
}
if (key == GetInputBinding(INPUT_SLOT_UP).primary ||
key == GetInputBinding(INPUT_SLOT_UP).secondary)
if (key == INPUT_SLOT_UP)
{
m_cameraZoom = 0.0f;
}
if (key == GetInputBinding(INPUT_SLOT_DOWN).primary ||
key == GetInputBinding(INPUT_SLOT_DOWN).secondary)
if (key == INPUT_SLOT_DOWN)
{
m_cameraZoom = 0.0f;
}
@ -3304,26 +3129,22 @@ void CRobotMain::KeyCamera(EventType type, unsigned int key)
if (type == EVENT_KEY_DOWN)
{
if (key == GetInputBinding(INPUT_SLOT_LEFT).primary ||
key == GetInputBinding(INPUT_SLOT_LEFT).secondary)
if (key == INPUT_SLOT_LEFT)
{
m_cameraPan = -1.0f;
}
if (key == GetInputBinding(INPUT_SLOT_RIGHT).primary ||
key == GetInputBinding(INPUT_SLOT_RIGHT).secondary)
if (key == INPUT_SLOT_RIGHT)
{
m_cameraPan = 1.0f;
}
if (key == GetInputBinding(INPUT_SLOT_UP).primary ||
key == GetInputBinding(INPUT_SLOT_UP).secondary)
if (key == INPUT_SLOT_UP)
{
m_cameraZoom = -1.0f;
}
if (key == GetInputBinding(INPUT_SLOT_DOWN).primary ||
key == GetInputBinding(INPUT_SLOT_DOWN).secondary)
if (key == INPUT_SLOT_DOWN)
{
m_cameraZoom = 1.0f;
}
@ -4750,7 +4571,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
CreateShortcuts();
m_map->UpdateMap();
// TODO: m_engine->TimeInit(); ??
m_app->ResetKeyStates();
m_input->ResetKeyStates();
m_time = 0.0f;
m_gameTime = 0.0f;
m_autosaveLast = 0.0f;
@ -6828,7 +6649,7 @@ void CRobotMain::SetEditLock(bool lock, bool edit)
m_map->ShowMap(!m_editLock && m_mapShow);
m_displayText->HideText(lock);
m_app->ResetKeyStates();
m_input->ResetKeyStates();
if (m_editLock)
HiliteClear();

View File

@ -77,6 +77,7 @@ enum Phase
class CEventQueue;
class CSoundInterface;
class CLevelParserLine;
class CInput;
namespace Gfx {
class CEngine;
@ -166,36 +167,6 @@ const int SATCOM_PROG = 4;
const int SATCOM_SOLUCE = 5;
const int SATCOM_MAX = 6;
/**
* \struct InputBinding
* \brief Binding for input slot
*/
struct InputBinding
{
//! Primary and secondary bindings
//! Can be regular key, virtual key or virtual joystick button
unsigned int primary, secondary;
InputBinding(unsigned int p = KEY_INVALID, unsigned int s = KEY_INVALID)
: primary(p), secondary(s) {}
};
/**
* \struct JoyAxisBinding
* \brief Binding for joystick axis
*/
struct JoyAxisBinding
{
//! Axis index or AXIS_INVALID
int axis;
//! True to invert axis value
bool invert;
};
//! Invalid value for axis binding (no axis assigned)
const int AXIS_INVALID = -1;
class CRobotMain : public CSingleton<CRobotMain>
{
public:
@ -214,30 +185,6 @@ public:
void ResetAfterDeviceChanged();
//! Sets the default input bindings (key and axes)
void SetDefaultInputBindings();
//! Management of input bindings
//@{
void SetInputBinding(InputSlot slot, InputBinding binding);
const InputBinding& GetInputBinding(InputSlot slot);
//@}
//! Management of joystick axis bindings
//@{
void SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding);
const JoyAxisBinding& GetJoyAxisBinding(JoyAxisSlot slot);
//@}
//! Management of joystick deadzone
//@{
void SetJoystickDeadzone(float zone);
float GetJoystickDeadzone();
//@}
//! Resets tracked key states (motion vectors)
void ResetKeyStates();
void ChangePhase(Phase phase);
bool ProcessEvent(Event &event);
@ -421,7 +368,7 @@ protected:
CObject* DetectObject(Math::Point pos);
void ChangeCamera();
void RemoteCamera(float pan, float zoom, float rTime);
void KeyCamera(EventType event, unsigned int key);
void KeyCamera(EventType event, InputSlot key);
void AbortMovie();
bool IsSelectable(CObject* pObj);
void SelectOneObject(CObject* pObj, bool displayError=true);
@ -462,15 +409,7 @@ protected:
Ui::CDisplayInfo* m_displayInfo;
CSoundInterface* m_sound;
CPauseManager* m_pause;
//! Bindings for user inputs
InputBinding m_inputBindings[INPUT_SLOT_MAX];
JoyAxisBinding m_joyAxisBindings[JOY_AXIS_SLOT_MAX];
float m_joystickDeadzone;
//! Motion vector set by keyboard or joystick buttons
Math::Vector m_keyMotion;
//! Motion vector set by joystick axes
Math::Vector m_joyMotion;
CInput* m_input;
float m_time;

View File

@ -21,6 +21,7 @@
#include "ui/edit.h"
#include "app/app.h"
#include "app/input.h"
#include "clipboard/clipboard.h"
@ -1443,9 +1444,8 @@ void CEdit::LoadImage(std::string name)
bool CEdit::ReadText(std::string filename, int addSize)
{
char *buffer;
int len, i, j, n, font, iIndex, iLines, iCount, iLink, res;
int len, i, j, n, font, iIndex, iLines, iCount, iLink;
char iName[50];
char text[50];
float iWidth;
InputSlot slot;
bool bInSoluce, bBOL;
@ -1782,71 +1782,36 @@ bool CEdit::ReadText(std::string filename, int addSize)
buffer[i+3] == 'y' &&
buffer[i+4] == ' ' )
{
if ( m_bSoluce || !bInSoluce )
int count;
for(count = 0; buffer[i+5+count] != ';'; count++);
if ( m_bSoluce || !bInSoluce ) //TODO: ???
{
if ( SearchKey(buffer+i+5, slot) )
CInput* input = CInput::GetInstancePointer();
slot = input->SearchKeyById(std::string(&buffer[i+5], count));
if ( slot != INPUT_SLOT_MAX )
{
CRobotMain* main = CRobotMain::GetInstancePointer();
res = main->GetInputBinding(slot).primary;
if ( res != 0 )
std::string iNameStr = input->GetKeysString(slot);
strcpy(iName, iNameStr.c_str());
m_text[j] = ' ';
m_format[j] = font;
j ++;
n = 0;
while ( iName[n] != 0 )
{
std::string iNameStr;
if ( GetResource(RES_KEY, res, iNameStr) )
{
strcpy(iName, iNameStr.c_str());
m_text[j] = ' ';
m_format[j] = font;
j ++;
n = 0;
while ( iName[n] != 0 )
{
m_text[j] = iName[n++];
m_format[j] = font;
j ++;
}
m_text[j] = ' ';
m_format[j] = font;
j ++;
res = main->GetInputBinding(slot).secondary;
if ( res != 0 )
{
if ( GetResource(RES_KEY, res, iNameStr) )
{
strcpy(iName, iNameStr.c_str());
std::string textStr;
GetResource(RES_TEXT, RT_KEY_OR, textStr);
strcpy(text, textStr.c_str());
n = 0;
while ( text[n] != 0 )
{
m_text[j] = text[n++];
m_format[j] = font&~Gfx::FONT_MASK_HIGHLIGHT;
j ++;
}
n = 0;
while ( iName[n] != 0 )
{
m_text[j] = iName[n++];
m_format[j] = font;
j ++;
}
m_text[j] = ' ';
m_format[j] = font;
j ++;
}
}
while ( buffer[i++] != ';' );
continue;
}
m_text[j] = iName[n++];
m_format[j] = font;
j ++;
}
m_text[j] = ' ';
m_format[j] = font;
j ++;
} else {
m_text[j] = '?';
m_format[j] = font;
j ++;
}
m_text[j] = '?';
m_format[j] = font;
j ++;
}
while ( buffer[i++] != ';' );
i = i+5+count+1;
}
else
{

View File

@ -28,25 +28,16 @@
namespace Ui {
static void GetKeyName(std::string& name, unsigned int key)
{
if (!GetResource(RES_KEY, key, name))
{
name = StrUtils::Format("Code %d", key);
}
}
CKey::CKey() : CControl()
{
m_catch = false;
m_robotMain = CRobotMain::GetInstancePointer();
m_input = CInput::GetInstancePointer();
}
CKey::~CKey()
{
m_robotMain = nullptr;
m_input = nullptr;
}
bool CKey::Create(Math::Point pos, Math::Point dim, int icon, EventType eventMsg)
@ -110,17 +101,19 @@ bool CKey::EventProcess(const Event &event)
bool CKey::TestKey(unsigned int key)
{
if (key == KEY(PAUSE) || key == KEY(PRINT)) return true; // blocked key
if (key == KEY(PAUSE) || key == KEY(PRINT) || key == KEY(ESCAPE)) return true; // blocked key
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputSlot slot = static_cast<InputSlot>(i);
InputBinding b = m_robotMain->GetInputBinding(slot);
if (key == b.primary || key == b.secondary)
m_robotMain->SetInputBinding(slot, InputBinding()); // nothing!
InputBinding b = m_input->GetInputBinding(slot);
if (key == b.primary)
m_input->SetInputBinding(slot, InputBinding(b.secondary, KEY_INVALID)); // nothing!
if (key == b.secondary)
m_input->SetInputBinding(slot, InputBinding(b.primary, KEY_INVALID)); // nothing!
if (b.primary == KEY_INVALID) // first free option?
m_robotMain->SetInputBinding(slot, InputBinding(b.secondary, b.primary)); // shift
m_input->SetInputBinding(slot, InputBinding(b.secondary, b.primary)); // shift
}
return false; // not used
@ -139,7 +132,7 @@ void CKey::Draw()
m_engine->SetTexture("textures/interface/button1.png");
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL); // was D3DSTATENORMAL
m_engine->SetState(Gfx::ENG_RSTATE_NORMAL);
float zoomExt = 1.00f;
float zoomInt = 0.95f;
@ -181,18 +174,7 @@ void CKey::Draw()
float h = m_engine->GetText()->GetHeight(m_fontType, m_fontSize) / 2.0f;
std::string keyName;
GetKeyName(keyName, m_binding.primary);
if (m_binding.secondary != KEY_INVALID)
{
std::string orText;
GetResource(RES_TEXT, RT_KEY_OR, orText);
keyName.append(orText);
std::string secondaryKeyName;
GetKeyName(secondaryKeyName, m_binding.secondary);
keyName.append(secondaryKeyName);
}
std::string keyName = m_input->GetKeysString(m_binding);
Math::Point pos;
pos.x = m_pos.x + m_dim.x * 0.5f;

View File

@ -26,6 +26,8 @@
#include "ui/control.h"
#include "app/input.h"
#include "common/event.h"
#include "common/restext.h"
#include "common/key.h"
@ -58,7 +60,7 @@ protected:
bool TestKey(unsigned int key);
protected:
CRobotMain* m_robotMain;
CInput* m_input;
InputBinding m_binding;
bool m_catch;

View File

@ -21,6 +21,7 @@
#include "ui/maindialog.h"
#include "app/app.h"
#include "app/input.h"
#include "app/system.h"
#include "common/config.h"
@ -2657,7 +2658,7 @@ bool CMainDialog::EventProcess(const Event &event)
break;
case EVENT_INTERFACE_KDEF:
m_main->SetDefaultInputBindings();
CInput::GetInstancePointer()->SetDefaultInputBindings();
UpdateKey();
break;
@ -4085,13 +4086,13 @@ void CMainDialog::IOReadList()
pl->SetSelect(m_saveList.size());
pl->ShowSelect(false); // shows the selected columns
int i;
unsigned int i;
std::string screenName;
for ( i=0; i < m_saveList.size(); i++ )
{
screenName = "textures/../" + m_saveList.at(i) + "/screen.png";
m_engine->DeleteTexture(screenName);
screenName = "textures/../" + m_saveList.at(i) + "/screen.png";
m_engine->DeleteTexture(screenName);
}
}
@ -5050,16 +5051,7 @@ void CMainDialog::SetupMemorize()
// TODO: Default value
}
std::stringstream key;
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputBinding b = m_main->GetInputBinding(static_cast<InputSlot>(i));
key << b.primary << " ";
key << b.secondary << " ";
}
GetProfile().SetStringProperty("Setup", "KeyMap", key.str());
GetProfile().SetStringProperty("Setup", "KeyMap", CInput::GetInstancePointer()->SaveKeyBindings());
GetProfile().SetIntProperty("Setup", "DeleteGamer", m_bDeleteGamer);
}
@ -5270,15 +5262,7 @@ void CMainDialog::SetupRecall()
if (GetProfile().GetStringProperty("Setup", "KeyMap", key))
{
std::stringstream skey;
skey.str(key);
for (int i = 0; i < INPUT_SLOT_MAX; i++)
{
InputBinding b;
skey >> b.primary;
skey >> b.secondary;
m_main->SetInputBinding(static_cast<InputSlot>(i), b);
}
CInput::GetInstancePointer()->LoadKeyBindings(key);
}
if ( GetProfile().GetIntProperty("Setup", "DeleteGamer", iValue) )
@ -5426,7 +5410,7 @@ void CMainDialog::UpdateKey()
CKey* pk = static_cast<CKey*>(pw->SearchControl(key_event[first+i]));
if (pk == nullptr) break;
pk->SetBinding(m_main->GetInputBinding(key_table[first+i]));
pk->SetBinding(CInput::GetInstancePointer()->GetInputBinding(key_table[first+i]));
pos.y -= dim.y;
}
}
@ -5448,7 +5432,7 @@ void CMainDialog::ChangeKey(EventType event)
CKey* pk = static_cast<CKey*>(pw->SearchControl(key_event[i]));
if (pk == nullptr) break;
m_main->SetInputBinding(key_table[i], pk->GetBinding());
CInput::GetInstancePointer()->SetInputBinding(key_table[i], pk->GetBinding());
}
}
}

View File

@ -128,9 +128,8 @@ bool CStudio::EventProcess(const Event &event)
if ( event.type == EVENT_KEY_DOWN )
{
if ( (event.key.key == m_main->GetInputBinding(INPUT_SLOT_ACTION).primary ||
event.key.key == m_main->GetInputBinding(INPUT_SLOT_ACTION).secondary) &&
(event.kmodState & KEY_MOD(CTRL)) != 0 )
if ( event.key.slot == INPUT_SLOT_ACTION &&
(event.kmodState & KEY_MOD(CTRL)) != 0 )
{
Event newEvent = event;
newEvent.type = EVENT_STUDIO_OK;
@ -256,8 +255,7 @@ bool CStudio::EventProcess(const Event &event)
if ( event.type == EVENT_KEY_DOWN )
{
if ( event.key.key == m_main->GetInputBinding(INPUT_SLOT_CBOT).primary ||
event.key.key == m_main->GetInputBinding(INPUT_SLOT_CBOT).secondary )
if ( event.key.slot == INPUT_SLOT_CBOT )
{
if ( m_helpFilename.length() > 0 )
{
@ -1411,10 +1409,10 @@ void CStudio::UpdateChangeList()
void CStudio::SetFilenameField(CEdit* edit, const std::string& filename)
{
std::string name = filename;
if(name.length() > edit->GetMaxChar()) {
if(name.length() > static_cast<unsigned int>(edit->GetMaxChar())) {
if(name.substr(name.length()-4) == ".txt")
name = name.substr(0, name.length()-4);
if(name.length() > edit->GetMaxChar()) {
if(name.length() > static_cast<unsigned int>(edit->GetMaxChar())) {
CLogger::GetInstancePointer()->Warn("Tried to load too long filename!\n");
name = name.substr(0, edit->GetMaxChar()); // truncates according to max length
}