/* * 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/profile.h" #include "common/restext.h" #include "graphics/engine/engine.h" #include "object/robotmain.h" #include #include template<> CInput* CSingleton::m_instance = nullptr; CInput::CInput() { m_kmodState = 0; m_mousePos = Math::Point(); m_mouseButtonsState = 0; for(int i=0; iWindowToInterfaceCoords(pos); } int CInput::GetKmods() const { return m_kmodState; } bool CInput::GetKmodState(int kmod) const { return (m_kmodState & kmod) != 0; } bool CInput::GetKeyState(InputSlot key) const { return m_keyPresses[key]; } bool CInput::GetMouseButtonState(int index) const { return (m_mouseButtonsState & (1<Trace("Reset key states\n"); m_kmodState = 0; m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f); m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f); for(int i=0; i(slot); m_inputBindings[index] = binding; } const InputBinding& CInput::GetInputBinding(InputSlot slot) { unsigned int index = static_cast(slot); return m_inputBindings[index]; } void CInput::SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding) { unsigned int index = static_cast(slot); m_joyAxisBindings[index] = binding; } const JoyAxisBinding& CInput::GetJoyAxisBinding(JoyAxisSlot slot) { unsigned int index = static_cast(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(i); InputBinding b = GetInputBinding(slot); if(b.primary == key || b.secondary == key) return slot; } return INPUT_SLOT_MAX; } static std::map 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_VISIT, "visit" }, { INPUT_SLOT_SPEED05, "speed05" }, { INPUT_SLOT_SPEED10, "speed10" }, { INPUT_SLOT_SPEED15, "speed15" }, { INPUT_SLOT_SPEED20, "speed20" }, { INPUT_SLOT_SPEED30, "speed30" }, { INPUT_SLOT_SPEED40, "speed40" }, { INPUT_SLOT_CAMERA_UP, "camup" }, { INPUT_SLOT_CAMERA_DOWN, "camdown" }, { INPUT_SLOT_PAUSE, "pause" }, }; void CInput::SaveKeyBindings() { std::stringstream key; for (int i = 0; i < INPUT_SLOT_MAX; i++) { InputBinding b = GetInputBinding(static_cast(i)); key.clear(); key.str(""); key << b.primary << " " << b.secondary; CProfile::GetInstancePointer()->SetStringProperty("Keybindings", keyTable[static_cast(i)], key.str()); } for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++) { JoyAxisBinding b = GetJoyAxisBinding(static_cast(i)); CProfile::GetInstancePointer()->SetIntProperty("Setup", "JoystickAxisBinding"+boost::lexical_cast(i), b.axis); CProfile::GetInstancePointer()->SetIntProperty("Setup", "JoystickAxisInvert"+boost::lexical_cast(i), b.invert); } CProfile::GetInstancePointer()->SetFloatProperty("Setup", "JoystickDeadzone", GetJoystickDeadzone()); } void CInput::LoadKeyBindings() { std::stringstream skey; std::string keys; for (int i = 0; i < INPUT_SLOT_MAX; i++) { InputBinding b; if (!CProfile::GetInstancePointer()->GetStringProperty("Keybindings", keyTable[static_cast(i)], keys)) continue; skey.clear(); skey.str(keys); skey >> b.primary; skey >> b.secondary; SetInputBinding(static_cast(i), b); } for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++) { JoyAxisBinding b; if (!CProfile::GetInstancePointer()->GetIntProperty("Setup", "JoystickAxisBinding"+boost::lexical_cast(i), b.axis)) continue; int x = 0; CProfile::GetInstancePointer()->GetIntProperty("Setup", "JoystickAxisInvert"+boost::lexical_cast(i), x); // If doesn't exist, use default (0) b.invert = (x != 0); SetJoyAxisBinding(static_cast(i), b); } float deadzone; if (CProfile::GetInstancePointer()->GetFloatProperty("Setup", "JoystickDeadzone", deadzone)) SetJoystickDeadzone(deadzone); } 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); }