2015-02-10 21:14:24 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2015-02-10 21:14:24 +01:00
|
|
|
|
|
|
|
#include "irrlichttypes_extrabloated.h"
|
2016-05-27 08:35:07 +02:00
|
|
|
#include "joystick_controller.h"
|
2020-11-12 19:16:02 +01:00
|
|
|
#include <list>
|
2017-05-13 11:05:16 +02:00
|
|
|
#include "keycode.h"
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "renderingengine.h"
|
2015-02-10 21:14:24 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_TOUCHSCREENGUI
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "gui/touchscreengui.h"
|
2015-02-10 21:14:24 +01:00
|
|
|
#endif
|
|
|
|
|
2018-01-13 10:40:25 +01:00
|
|
|
class InputHandler;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
Fast key cache for main game loop
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is faster than using getKeySetting with the tradeoff that functions
|
|
|
|
* using it must make sure that it's initialised before using it and there is
|
|
|
|
* no error handling (for example bounds checking). This is really intended for
|
|
|
|
* use only in the main running loop of the client (the_game()) where the faster
|
|
|
|
* (up to 10x faster) key lookup is an asset. Other parts of the codebase
|
|
|
|
* (e.g. formspecs) should continue using getKeySetting().
|
|
|
|
*/
|
2018-01-13 10:54:18 +01:00
|
|
|
struct KeyCache
|
|
|
|
{
|
2018-01-13 10:40:25 +01:00
|
|
|
|
|
|
|
KeyCache()
|
|
|
|
{
|
|
|
|
handler = NULL;
|
|
|
|
populate();
|
|
|
|
populate_nonchanging();
|
|
|
|
}
|
|
|
|
|
|
|
|
void populate();
|
|
|
|
|
|
|
|
// Keys that are not settings dependent
|
|
|
|
void populate_nonchanging();
|
|
|
|
|
|
|
|
KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
|
|
|
|
InputHandler *handler;
|
|
|
|
};
|
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
class KeyList : private std::list<KeyPress>
|
|
|
|
{
|
|
|
|
typedef std::list<KeyPress> super;
|
|
|
|
typedef super::iterator iterator;
|
|
|
|
typedef super::const_iterator const_iterator;
|
|
|
|
|
|
|
|
virtual const_iterator find(const KeyPress &key) const
|
|
|
|
{
|
|
|
|
const_iterator f(begin());
|
|
|
|
const_iterator e(end());
|
|
|
|
|
|
|
|
while (f != e) {
|
|
|
|
if (*f == key)
|
|
|
|
return f;
|
|
|
|
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual iterator find(const KeyPress &key)
|
|
|
|
{
|
|
|
|
iterator f(begin());
|
|
|
|
iterator e(end());
|
|
|
|
|
|
|
|
while (f != e) {
|
|
|
|
if (*f == key)
|
|
|
|
return f;
|
|
|
|
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void clear() { super::clear(); }
|
|
|
|
|
|
|
|
void set(const KeyPress &key)
|
|
|
|
{
|
|
|
|
if (find(key) == end())
|
|
|
|
push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unset(const KeyPress &key)
|
|
|
|
{
|
|
|
|
iterator p(find(key));
|
|
|
|
|
|
|
|
if (p != end())
|
|
|
|
erase(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggle(const KeyPress &key)
|
|
|
|
{
|
|
|
|
iterator p(this->find(key));
|
|
|
|
|
|
|
|
if (p != end())
|
|
|
|
erase(p);
|
|
|
|
else
|
|
|
|
push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator[](const KeyPress &key) const { return find(key) != end(); }
|
|
|
|
};
|
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
class MyEventReceiver : public IEventReceiver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// This is the one method that we have to implement
|
|
|
|
virtual bool OnEvent(const SEvent &event);
|
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
|
2017-05-13 11:05:16 +02:00
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
// Checks whether a key was down and resets the state
|
|
|
|
bool WasKeyDown(const KeyPress &keyCode)
|
|
|
|
{
|
2020-11-12 19:16:02 +01:00
|
|
|
bool b = keyWasDown[keyCode];
|
2015-02-10 21:14:24 +01:00
|
|
|
if (b)
|
2020-11-12 19:16:02 +01:00
|
|
|
keyWasDown.unset(keyCode);
|
2015-02-10 21:14:24 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:06:35 +02:00
|
|
|
// Checks whether a key was just pressed. State will be cleared
|
|
|
|
// in the subsequent iteration of Game::processPlayerInteraction
|
2020-11-12 19:16:02 +01:00
|
|
|
bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
|
2020-06-05 15:06:35 +02:00
|
|
|
|
|
|
|
// Checks whether a key was just released. State will be cleared
|
|
|
|
// in the subsequent iteration of Game::processPlayerInteraction
|
2020-11-12 19:16:02 +01:00
|
|
|
bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
|
2017-05-13 11:05:16 +02:00
|
|
|
void dontListenForKeys() { keysListenedFor.clear(); }
|
2016-05-14 16:25:57 +02:00
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
s32 getMouseWheel()
|
|
|
|
{
|
|
|
|
s32 a = mouse_wheel;
|
|
|
|
mouse_wheel = 0;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearInput()
|
|
|
|
{
|
|
|
|
keyIsDown.clear();
|
|
|
|
keyWasDown.clear();
|
2020-06-05 15:06:35 +02:00
|
|
|
keyWasPressed.clear();
|
|
|
|
keyWasReleased.clear();
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2020-06-05 15:06:35 +02:00
|
|
|
mouse_wheel = 0;
|
|
|
|
}
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2020-06-05 15:06:35 +02:00
|
|
|
void clearWasKeyPressed()
|
|
|
|
{
|
|
|
|
keyWasPressed.clear();
|
|
|
|
}
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2020-06-05 15:06:35 +02:00
|
|
|
void clearWasKeyReleased()
|
|
|
|
{
|
|
|
|
keyWasReleased.clear();
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MyEventReceiver()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_TOUCHSCREENGUI
|
|
|
|
m_touchscreengui = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-21 08:04:45 +02:00
|
|
|
s32 mouse_wheel = 0;
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2017-06-21 08:04:45 +02:00
|
|
|
JoystickController *joystick = nullptr;
|
2016-05-27 08:35:07 +02:00
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
#ifdef HAVE_TOUCHSCREENGUI
|
2017-05-13 11:05:16 +02:00
|
|
|
TouchScreenGUI *m_touchscreengui;
|
2015-02-10 21:14:24 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2020-11-12 19:16:02 +01:00
|
|
|
// The current state of keys
|
|
|
|
KeyList keyIsDown;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2021-02-23 14:21:15 +01:00
|
|
|
// Like keyIsDown but only reset when that key is read
|
2020-11-12 19:16:02 +01:00
|
|
|
KeyList keyWasDown;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
// Whether a key has just been pressed
|
|
|
|
KeyList keyWasPressed;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
// Whether a key has just been released
|
|
|
|
KeyList keyWasReleased;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
// List of keys we listen for
|
|
|
|
// TODO perhaps the type of this is not really
|
|
|
|
// performant as KeyList is designed for few but
|
|
|
|
// often changing keys, and keysListenedFor is expected
|
|
|
|
// to change seldomly but contain lots of keys.
|
|
|
|
KeyList keysListenedFor;
|
2015-02-10 21:14:24 +01:00
|
|
|
};
|
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
class InputHandler
|
|
|
|
{
|
|
|
|
public:
|
2018-01-13 10:54:18 +01:00
|
|
|
InputHandler()
|
|
|
|
{
|
|
|
|
keycache.handler = this;
|
|
|
|
keycache.populate();
|
|
|
|
}
|
2017-08-15 20:30:30 +02:00
|
|
|
|
|
|
|
virtual ~InputHandler() = default;
|
2017-05-13 11:05:16 +02:00
|
|
|
|
2020-07-14 19:10:37 +02:00
|
|
|
virtual bool isRandom() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool isKeyDown(GameKeyType k) = 0;
|
|
|
|
virtual bool wasKeyDown(GameKeyType k) = 0;
|
2020-06-05 15:06:35 +02:00
|
|
|
virtual bool wasKeyPressed(GameKeyType k) = 0;
|
|
|
|
virtual bool wasKeyReleased(GameKeyType k) = 0;
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool cancelPressed() = 0;
|
2017-05-13 11:05:16 +02:00
|
|
|
|
2021-08-27 20:24:24 +02:00
|
|
|
virtual float getMovementSpeed() = 0;
|
|
|
|
virtual float getMovementDirection() = 0;
|
|
|
|
|
2020-06-05 15:06:35 +02:00
|
|
|
virtual void clearWasKeyPressed() {}
|
|
|
|
virtual void clearWasKeyReleased() {}
|
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
virtual void listenForKey(const KeyPress &keyCode) {}
|
|
|
|
virtual void dontListenForKeys() {}
|
|
|
|
|
|
|
|
virtual v2s32 getMousePos() = 0;
|
|
|
|
virtual void setMousePos(s32 x, s32 y) = 0;
|
|
|
|
|
|
|
|
virtual s32 getMouseWheel() = 0;
|
|
|
|
|
|
|
|
virtual void step(float dtime) {}
|
|
|
|
|
|
|
|
virtual void clear() {}
|
|
|
|
|
|
|
|
JoystickController joystick;
|
2018-01-13 10:54:18 +01:00
|
|
|
KeyCache keycache;
|
2017-05-13 11:05:16 +02:00
|
|
|
};
|
2015-02-10 21:14:24 +01:00
|
|
|
/*
|
|
|
|
Separated input handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
class RealInputHandler : public InputHandler
|
|
|
|
{
|
|
|
|
public:
|
2017-06-26 20:11:17 +02:00
|
|
|
RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
|
2015-02-10 21:14:24 +01:00
|
|
|
{
|
2016-05-27 08:35:07 +02:00
|
|
|
m_receiver->joystick = &joystick;
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool isKeyDown(GameKeyType k)
|
|
|
|
{
|
|
|
|
return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
|
|
|
|
}
|
|
|
|
virtual bool wasKeyDown(GameKeyType k)
|
2015-02-10 21:14:24 +01:00
|
|
|
{
|
2018-01-13 10:54:18 +01:00
|
|
|
return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
2020-06-05 15:06:35 +02:00
|
|
|
virtual bool wasKeyPressed(GameKeyType k)
|
|
|
|
{
|
2020-12-19 22:01:05 +01:00
|
|
|
return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
|
2020-06-05 15:06:35 +02:00
|
|
|
}
|
|
|
|
virtual bool wasKeyReleased(GameKeyType k)
|
|
|
|
{
|
|
|
|
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
|
|
|
|
}
|
2021-08-27 20:24:24 +02:00
|
|
|
virtual float getMovementSpeed()
|
|
|
|
{
|
|
|
|
bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
|
|
|
|
b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
|
|
|
|
l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
|
|
|
|
r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
|
|
|
|
if (f || b || l || r)
|
|
|
|
{
|
|
|
|
// if contradictory keys pressed, stay still
|
|
|
|
if (f && b && l && r)
|
|
|
|
return 0.0f;
|
|
|
|
else if (f && b && !l && !r)
|
|
|
|
return 0.0f;
|
|
|
|
else if (!f && !b && l && r)
|
|
|
|
return 0.0f;
|
|
|
|
return 1.0f; // If there is a keyboard event, assume maximum speed
|
|
|
|
}
|
|
|
|
return joystick.getMovementSpeed();
|
|
|
|
}
|
|
|
|
virtual float getMovementDirection()
|
|
|
|
{
|
|
|
|
float x = 0, z = 0;
|
|
|
|
|
|
|
|
/* Check keyboard for input */
|
|
|
|
if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
|
|
|
|
z += 1;
|
|
|
|
if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
|
|
|
|
z -= 1;
|
|
|
|
if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
|
|
|
|
x += 1;
|
|
|
|
if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
|
|
|
|
x -= 1;
|
|
|
|
|
|
|
|
if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
|
|
|
|
return atan2(x, z);
|
|
|
|
else
|
|
|
|
return joystick.getMovementDirection();
|
|
|
|
}
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool cancelPressed()
|
2015-02-10 21:14:24 +01:00
|
|
|
{
|
2018-01-13 10:54:18 +01:00
|
|
|
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
2020-06-05 15:06:35 +02:00
|
|
|
virtual void clearWasKeyPressed()
|
|
|
|
{
|
|
|
|
m_receiver->clearWasKeyPressed();
|
|
|
|
}
|
|
|
|
virtual void clearWasKeyReleased()
|
|
|
|
{
|
|
|
|
m_receiver->clearWasKeyReleased();
|
|
|
|
}
|
2016-05-14 16:25:57 +02:00
|
|
|
virtual void listenForKey(const KeyPress &keyCode)
|
|
|
|
{
|
|
|
|
m_receiver->listenForKey(keyCode);
|
|
|
|
}
|
2017-05-13 11:05:16 +02:00
|
|
|
virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
|
2015-02-10 21:14:24 +01:00
|
|
|
virtual v2s32 getMousePos()
|
|
|
|
{
|
2017-06-26 20:11:17 +02:00
|
|
|
if (RenderingEngine::get_raw_device()->getCursorControl()) {
|
|
|
|
return RenderingEngine::get_raw_device()
|
|
|
|
->getCursorControl()
|
|
|
|
->getPosition();
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
2017-08-15 20:30:30 +02:00
|
|
|
|
|
|
|
return m_mousepos;
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
2017-08-16 22:11:45 +02:00
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
virtual void setMousePos(s32 x, s32 y)
|
|
|
|
{
|
2017-06-26 20:11:17 +02:00
|
|
|
if (RenderingEngine::get_raw_device()->getCursorControl()) {
|
|
|
|
RenderingEngine::get_raw_device()
|
|
|
|
->getCursorControl()
|
|
|
|
->setPosition(x, y);
|
2017-05-13 11:05:16 +02:00
|
|
|
} else {
|
|
|
|
m_mousepos = v2s32(x, y);
|
2015-02-10 21:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
|
2015-02-10 21:14:24 +01:00
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2016-05-27 08:35:07 +02:00
|
|
|
joystick.clear();
|
2015-02-10 21:14:24 +01:00
|
|
|
m_receiver->clearInput();
|
|
|
|
}
|
2017-05-13 11:05:16 +02:00
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
private:
|
2017-06-21 08:04:45 +02:00
|
|
|
MyEventReceiver *m_receiver = nullptr;
|
2017-05-13 11:05:16 +02:00
|
|
|
v2s32 m_mousepos;
|
2015-02-10 21:14:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class RandomInputHandler : public InputHandler
|
|
|
|
{
|
|
|
|
public:
|
2017-08-15 20:30:30 +02:00
|
|
|
RandomInputHandler() = default;
|
|
|
|
|
2020-07-14 19:10:37 +02:00
|
|
|
bool isRandom() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-12 19:16:02 +01:00
|
|
|
virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool wasKeyDown(GameKeyType k) { return false; }
|
2020-06-05 15:06:35 +02:00
|
|
|
virtual bool wasKeyPressed(GameKeyType k) { return false; }
|
|
|
|
virtual bool wasKeyReleased(GameKeyType k) { return false; }
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual bool cancelPressed() { return false; }
|
2021-09-05 19:58:50 +02:00
|
|
|
virtual float getMovementSpeed() { return movementSpeed; }
|
|
|
|
virtual float getMovementDirection() { return movementDirection; }
|
2017-05-13 11:05:16 +02:00
|
|
|
virtual v2s32 getMousePos() { return mousepos; }
|
|
|
|
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
virtual s32 getMouseWheel() { return 0; }
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2018-01-13 10:54:18 +01:00
|
|
|
virtual void step(float dtime);
|
2015-02-10 21:14:24 +01:00
|
|
|
|
2017-05-13 11:05:16 +02:00
|
|
|
s32 Rand(s32 min, s32 max);
|
|
|
|
|
2015-02-10 21:14:24 +01:00
|
|
|
private:
|
2020-11-12 19:16:02 +01:00
|
|
|
KeyList keydown;
|
2015-02-10 21:14:24 +01:00
|
|
|
v2s32 mousepos;
|
|
|
|
v2s32 mousespeed;
|
2021-09-05 19:58:50 +02:00
|
|
|
float movementSpeed;
|
|
|
|
float movementDirection;
|
2015-02-10 21:14:24 +01:00
|
|
|
};
|