From 3f59d3cd428dd31bdc134d7a487e74a9033a2919 Mon Sep 17 00:00:00 2001 From: Joel Leclerc Date: Sun, 29 Apr 2012 22:41:49 -0600 Subject: [PATCH] Added input.cpp and input.h (moved from main.cpp) --- CMakeLists.txt | 6 +- src/CMakeLists.txt | 1 + src/input.cpp | 308 ++++++++++++++++++++++++++++++++ src/input.h | 158 +++++++++++++++++ src/main.cpp | 430 ++++++--------------------------------------- 5 files changed, 521 insertions(+), 382 deletions(-) create mode 100644 src/input.cpp create mode 100644 src/input.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 719e763..7afdf16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,14 +5,14 @@ if(${CMAKE_VERSION} STREQUAL "2.8.2") endif(${CMAKE_VERSION} STREQUAL "2.8.2") # This can be read from ${PROJECT_NAME} after project() is called -project(minetest) +project(blockplanet) set(VERSION_EXTRA "" CACHE STRING "Stuff to append to version string") # Also remember to set PROTOCOL_VERSION in clientserver.h when releasing set(VERSION_MAJOR 0) -set(VERSION_MINOR 4) -set(VERSION_PATCH dev-20120408) +set(VERSION_MINOR 1) +set(VERSION_PATCH dev-20120430) if(VERSION_EXTRA) set(VERSION_PATCH ${VERSION_PATCH}-${VERSION_EXTRA}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d08d3af..b578d66 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -231,6 +231,7 @@ endif() set(minetest_SRCS ${common_SRCS} ${sound_SRCS} + input.cpp localplayer.cpp sky.cpp clientmap.cpp diff --git a/src/input.cpp b/src/input.cpp new file mode 100644 index 0000000..f8bb7e6 --- /dev/null +++ b/src/input.cpp @@ -0,0 +1,308 @@ +/* +BlockPlanet +Copyright (C) 2012 MiJyn, Joel Leclerc +Licensed under GPLv3 + + +Based on: +Minetest-c55 +Copyright (C) 2010-2011 celeron55, Perttu Ahola + +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 2 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, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "input.h" +#include "utility.h" + +bool MyEventReceiver::IsKeyDown(const KeyPress &keyCode) const +{ + return keyIsDown[keyCode]; +} + +// Checks whether a key was down and resets the state +bool MyEventReceiver::WasKeyDown(const KeyPress &keyCode) +{ + bool b = keyWasDown[keyCode]; + if (b) + { + keyWasDown.unset(keyCode); + } + return b; +} + +s32 MyEventReceiver::getMouseWheel() +{ + s32 a = mouse_wheel; + mouse_wheel = 0; + return a; +} + +void MyEventReceiver::clearInput() +{ + keyIsDown.clear(); + keyWasDown.clear(); + + leftclicked = false; + rightclicked = false; + leftreleased = false; + rightreleased = false; + + left_active = false; + middle_active = false; + right_active = false; + + mouse_wheel = 0; +} + +MyEventReceiver::MyEventReceiver() +{ + clearInput(); +} + + +bool RealInputHandler::isKeyDown(const KeyPress &keyCode) +{ + return m_receiver->IsKeyDown(keyCode); +} +bool RealInputHandler::wasKeyDown(const KeyPress &keyCode) +{ + return m_receiver->WasKeyDown(keyCode); +} +v2s32 RealInputHandler::getMousePos() +{ + return m_device->getCursorControl()->getPosition(); +} +void RealInputHandler::setMousePos(s32 x, s32 y) +{ + m_device->getCursorControl()->setPosition(x, y); +} + +bool RealInputHandler::getLeftState() +{ + return m_receiver->left_active; +} +bool RealInputHandler::getRightState() +{ + return m_receiver->right_active; +} + +bool RealInputHandler::getLeftClicked() +{ + return m_receiver->leftclicked; +} +bool RealInputHandler::getRightClicked() +{ + return m_receiver->rightclicked; +} +void RealInputHandler::resetLeftClicked() +{ + m_receiver->leftclicked = false; +} +void RealInputHandler::resetRightClicked() +{ + m_receiver->rightclicked = false; +} + +bool RealInputHandler::getLeftReleased() +{ + return m_receiver->leftreleased; +} +bool RealInputHandler::getRightReleased() +{ + return m_receiver->rightreleased; +} +void RealInputHandler::resetLeftReleased() +{ + m_receiver->leftreleased = false; +} +void RealInputHandler::resetRightReleased() +{ + m_receiver->rightreleased = false; +} + +s32 RealInputHandler::getMouseWheel() +{ + return m_receiver->getMouseWheel(); +} + +void RealInputHandler::clear() +{ + m_receiver->clearInput(); +} + + +RandomInputHandler::RandomInputHandler() +{ + leftdown = false; + rightdown = false; + leftclicked = false; + rightclicked = false; + leftreleased = false; + rightreleased = false; + keydown.clear(); +} +bool RandomInputHandler::isKeyDown(const KeyPress &keyCode) +{ + return keydown[keyCode]; +} +bool RandomInputHandler::wasKeyDown(const KeyPress &keyCode) +{ + return false; +} +v2s32 RandomInputHandler::getMousePos() +{ + return mousepos; +} +void RandomInputHandler::setMousePos(s32 x, s32 y) +{ + mousepos = v2s32(x,y); +} + +bool RandomInputHandler::getLeftState() +{ + return leftdown; +} +bool RandomInputHandler::getRightState() +{ + return rightdown; +} + +bool RandomInputHandler::getLeftClicked() +{ + return leftclicked; +} +bool RandomInputHandler::getRightClicked() +{ + return rightclicked; +} +void RandomInputHandler::resetLeftClicked() +{ + leftclicked = false; +} +void RandomInputHandler::resetRightClicked() +{ + rightclicked = false; +} + +bool RandomInputHandler::getLeftReleased() +{ + return leftreleased; +} +bool RandomInputHandler::getRightReleased() +{ + return rightreleased; +} +void RandomInputHandler::resetLeftReleased() +{ + leftreleased = false; +} +void RandomInputHandler::resetRightReleased() +{ + rightreleased = false; +} + +s32 RandomInputHandler::getMouseWheel() +{ + return 0; +} + +void RandomInputHandler::step(float dtime) +{ + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 40); + keydown.toggle(getKeySetting("keymap_jump")); + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 40); + keydown.toggle(getKeySetting("keymap_special1")); + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 40); + keydown.toggle(getKeySetting("keymap_forward")); + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 40); + keydown.toggle(getKeySetting("keymap_left")); + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 20); + mousespeed = v2s32(Rand(-20,20), Rand(-15,20)); + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 30); + leftdown = !leftdown; + if(leftdown) + { + leftclicked = true; + } + if(!leftdown) + { + leftreleased = true; + } + } + } + { + static float counter1 = 0; + counter1 -= dtime; + if(counter1 < 0.0) + { + counter1 = 0.1*Rand(1, 15); + rightdown = !rightdown; + if(rightdown) + { + rightclicked = true; + } + if(!rightdown) + { + rightreleased = true; + } + } + } + mousepos += mousespeed; +} + +s32 RandomInputHandler::Rand(s32 min, s32 max) +{ + return (myrand()%(max-min+1))+min; +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..0cc8447 --- /dev/null +++ b/src/input.h @@ -0,0 +1,158 @@ +/* +BlockPlanet +Copyright (C) 2012 MiJyn, Joel Leclerc +Licensed under GPLv3 + + +Based on: +Minetest-c55 +Copyright (C) 2010-2011 celeron55, Perttu Ahola + +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 2 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, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef INPUT_HEADER +#define INPUT_HEADER + +#include "common_irrlicht.h" +#include "keycode.h" +#include "game.h" + +#ifndef SERVER + +/* + Event handler for Irrlicht + + NOTE: Everything possible should be moved out from here, + probably to InputHandler and the_game +*/ + +class MyEventReceiver : public IEventReceiver +{ +public: + // This is the one method that we have to implement + virtual bool OnEvent(const SEvent& event); + + bool IsKeyDown(const KeyPress &keyCode) const; + + // Checks whether a key was down and resets the state + bool WasKeyDown(const KeyPress &keyCode); + + s32 getMouseWheel(); + + void clearInput(); + + MyEventReceiver(); + + bool leftclicked; + bool rightclicked; + bool leftreleased; + bool rightreleased; + + bool left_active; + bool middle_active; + bool right_active; + + s32 mouse_wheel; + +private: + IrrlichtDevice *m_device; + + // The current state of keys + KeyList keyIsDown; + // Whether a key has been pressed or not + KeyList keyWasDown; +}; + +/* + Separated input handler +*/ + +class RealInputHandler : public InputHandler +{ +public: + RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver): + m_device(device), + m_receiver(receiver) + { + } + virtual bool isKeyDown(const KeyPress &keyCode); + virtual bool wasKeyDown(const KeyPress &keyCode); + virtual v2s32 getMousePos(); + virtual void setMousePos(s32 x, s32 y); + + virtual bool getLeftState(); + virtual bool getRightState(); + + virtual bool getLeftClicked(); + virtual bool getRightClicked(); + virtual void resetLeftClicked(); + virtual void resetRightClicked(); + + virtual bool getLeftReleased(); + virtual bool getRightReleased(); + virtual void resetLeftReleased(); + virtual void resetRightReleased(); + + virtual s32 getMouseWheel(); + + void clear(); +private: + IrrlichtDevice *m_device; + MyEventReceiver *m_receiver; +}; + +class RandomInputHandler : public InputHandler +{ +public: + RandomInputHandler(); + virtual bool isKeyDown(const KeyPress &keyCode); + virtual bool wasKeyDown(const KeyPress &keyCode); + virtual v2s32 getMousePos(); + virtual void setMousePos(s32 x, s32 y); + + virtual bool getLeftState(); + virtual bool getRightState(); + + virtual bool getLeftClicked(); + virtual bool getRightClicked(); + virtual void resetLeftClicked(); + virtual void resetRightClicked(); + + virtual bool getLeftReleased(); + virtual bool getRightReleased(); + virtual void resetLeftReleased(); + virtual void resetRightReleased(); + + virtual s32 getMouseWheel(); + + virtual void step(float dtime); + + s32 Rand(s32 min, s32 max); +private: + KeyList keydown; + v2s32 mousepos; + v2s32 mousespeed; + bool leftdown; + bool rightdown; + bool leftclicked; + bool rightclicked; + bool leftreleased; + bool rightreleased; +}; + +#endif + +#endif diff --git a/src/main.cpp b/src/main.cpp index bb546d1..18324ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,6 +72,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "subgame.h" #include "quicktune.h" #include "speedtests.h" +#include "input.h" /* Settings. @@ -159,396 +160,67 @@ public: #ifndef SERVER -/* - Event handler for Irrlicht - - NOTE: Everything possible should be moved out from here, - probably to InputHandler and the_game -*/ - -class MyEventReceiver : public IEventReceiver +bool MyEventReceiver::OnEvent(const SEvent& event) { -public: - // This is the one method that we have to implement - virtual bool OnEvent(const SEvent& event) + /* + React to nothing here if a menu is active + */ + if(noMenuActive() == false) + { + return false; + } + + // Remember whether each key is down or up + if(event.EventType == irr::EET_KEY_INPUT_EVENT) + { + if(event.KeyInput.PressedDown) { + keyIsDown.set(event.KeyInput); + keyWasDown.set(event.KeyInput); + } else { + keyIsDown.unset(event.KeyInput); + } + } + + if(event.EventType == irr::EET_MOUSE_INPUT_EVENT) { - /* - React to nothing here if a menu is active - */ if(noMenuActive() == false) { - return false; + left_active = false; + middle_active = false; + right_active = false; } - - // Remember whether each key is down or up - if(event.EventType == irr::EET_KEY_INPUT_EVENT) + else { - if(event.KeyInput.PressedDown) { - keyIsDown.set(event.KeyInput); - keyWasDown.set(event.KeyInput); - } else { - keyIsDown.unset(event.KeyInput); + left_active = event.MouseInput.isLeftPressed(); + middle_active = event.MouseInput.isMiddlePressed(); + right_active = event.MouseInput.isRightPressed(); + + if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) + { + leftclicked = true; + } + if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) + { + rightclicked = true; + } + if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) + { + leftreleased = true; + } + if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) + { + rightreleased = true; + } + if(event.MouseInput.Event == EMIE_MOUSE_WHEEL) + { + mouse_wheel += event.MouseInput.Wheel; } } - - if(event.EventType == irr::EET_MOUSE_INPUT_EVENT) - { - if(noMenuActive() == false) - { - left_active = false; - middle_active = false; - right_active = false; - } - else - { - left_active = event.MouseInput.isLeftPressed(); - middle_active = event.MouseInput.isMiddlePressed(); - right_active = event.MouseInput.isRightPressed(); - - if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) - { - leftclicked = true; - } - if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) - { - rightclicked = true; - } - if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) - { - leftreleased = true; - } - if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) - { - rightreleased = true; - } - if(event.MouseInput.Event == EMIE_MOUSE_WHEEL) - { - mouse_wheel += event.MouseInput.Wheel; - } - } - } - - return false; } - bool IsKeyDown(const KeyPress &keyCode) const - { - return keyIsDown[keyCode]; - } - - // Checks whether a key was down and resets the state - bool WasKeyDown(const KeyPress &keyCode) - { - bool b = keyWasDown[keyCode]; - if (b) - keyWasDown.unset(keyCode); - return b; - } + return false; +} - s32 getMouseWheel() - { - s32 a = mouse_wheel; - mouse_wheel = 0; - return a; - } - - void clearInput() - { - keyIsDown.clear(); - keyWasDown.clear(); - - leftclicked = false; - rightclicked = false; - leftreleased = false; - rightreleased = false; - - left_active = false; - middle_active = false; - right_active = false; - - mouse_wheel = 0; - } - - MyEventReceiver() - { - clearInput(); - } - - bool leftclicked; - bool rightclicked; - bool leftreleased; - bool rightreleased; - - bool left_active; - bool middle_active; - bool right_active; - - s32 mouse_wheel; - -private: - IrrlichtDevice *m_device; - - // The current state of keys - KeyList keyIsDown; - // Whether a key has been pressed or not - KeyList keyWasDown; -}; - -/* - Separated input handler -*/ - -class RealInputHandler : public InputHandler -{ -public: - RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver): - m_device(device), - m_receiver(receiver) - { - } - virtual bool isKeyDown(const KeyPress &keyCode) - { - return m_receiver->IsKeyDown(keyCode); - } - virtual bool wasKeyDown(const KeyPress &keyCode) - { - return m_receiver->WasKeyDown(keyCode); - } - virtual v2s32 getMousePos() - { - return m_device->getCursorControl()->getPosition(); - } - virtual void setMousePos(s32 x, s32 y) - { - m_device->getCursorControl()->setPosition(x, y); - } - - virtual bool getLeftState() - { - return m_receiver->left_active; - } - virtual bool getRightState() - { - return m_receiver->right_active; - } - - virtual bool getLeftClicked() - { - return m_receiver->leftclicked; - } - virtual bool getRightClicked() - { - return m_receiver->rightclicked; - } - virtual void resetLeftClicked() - { - m_receiver->leftclicked = false; - } - virtual void resetRightClicked() - { - m_receiver->rightclicked = false; - } - - virtual bool getLeftReleased() - { - return m_receiver->leftreleased; - } - virtual bool getRightReleased() - { - return m_receiver->rightreleased; - } - virtual void resetLeftReleased() - { - m_receiver->leftreleased = false; - } - virtual void resetRightReleased() - { - m_receiver->rightreleased = false; - } - - virtual s32 getMouseWheel() - { - return m_receiver->getMouseWheel(); - } - - void clear() - { - m_receiver->clearInput(); - } -private: - IrrlichtDevice *m_device; - MyEventReceiver *m_receiver; -}; - -class RandomInputHandler : public InputHandler -{ -public: - RandomInputHandler() - { - leftdown = false; - rightdown = false; - leftclicked = false; - rightclicked = false; - leftreleased = false; - rightreleased = false; - keydown.clear(); - } - virtual bool isKeyDown(const KeyPress &keyCode) - { - return keydown[keyCode]; - } - virtual bool wasKeyDown(const KeyPress &keyCode) - { - return false; - } - virtual v2s32 getMousePos() - { - return mousepos; - } - virtual void setMousePos(s32 x, s32 y) - { - mousepos = v2s32(x,y); - } - - virtual bool getLeftState() - { - return leftdown; - } - virtual bool getRightState() - { - return rightdown; - } - - virtual bool getLeftClicked() - { - return leftclicked; - } - virtual bool getRightClicked() - { - return rightclicked; - } - virtual void resetLeftClicked() - { - leftclicked = false; - } - virtual void resetRightClicked() - { - rightclicked = false; - } - - virtual bool getLeftReleased() - { - return leftreleased; - } - virtual bool getRightReleased() - { - return rightreleased; - } - virtual void resetLeftReleased() - { - leftreleased = false; - } - virtual void resetRightReleased() - { - rightreleased = false; - } - - virtual s32 getMouseWheel() - { - return 0; - } - - virtual void step(float dtime) - { - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 40); - keydown.toggle(getKeySetting("keymap_jump")); - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 40); - keydown.toggle(getKeySetting("keymap_special1")); - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 40); - keydown.toggle(getKeySetting("keymap_forward")); - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 40); - keydown.toggle(getKeySetting("keymap_left")); - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 20); - mousespeed = v2s32(Rand(-20,20), Rand(-15,20)); - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 30); - leftdown = !leftdown; - if(leftdown) - leftclicked = true; - if(!leftdown) - leftreleased = true; - } - } - { - static float counter1 = 0; - counter1 -= dtime; - if(counter1 < 0.0) - { - counter1 = 0.1*Rand(1, 15); - rightdown = !rightdown; - if(rightdown) - rightclicked = true; - if(!rightdown) - rightreleased = true; - } - } - mousepos += mousespeed; - } - - s32 Rand(s32 min, s32 max) - { - return (myrand()%(max-min+1))+min; - } -private: - KeyList keydown; - v2s32 mousepos; - v2s32 mousespeed; - bool leftdown; - bool rightdown; - bool leftclicked; - bool rightclicked; - bool leftreleased; - bool rightreleased; -}; void drawMenuBackground(video::IVideoDriver* driver) {