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"
|
2017-05-13 11:05:16 +02:00
|
|
|
#include "keycode.h"
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "renderingengine.h"
|
2020-09-23 19:12:20 +02:00
|
|
|
#include <unordered_set>
|
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;
|
|
|
|
};
|
|
|
|
|
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-09-23 19:12:20 +02:00
|
|
|
bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown.count(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-09-23 19:12:20 +02:00
|
|
|
bool b = keyWasDown.count(keyCode);
|
2015-02-10 21:14:24 +01:00
|
|
|
if (b)
|
2020-09-23 19:12:20 +02:00
|
|
|
keyWasDown.erase(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-09-23 19:12:20 +02:00
|
|
|
bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed.count(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-09-23 19:12:20 +02:00
|
|
|
bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased.count(keycode); }
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-09-23 19:12:20 +02:00
|
|
|
void listenForKey(const KeyPress &keyCode) { keysListenedFor.insert(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-09-23 19:12:20 +02:00
|
|
|
//! The current state of keys
|
|
|
|
std::unordered_set<KeyPress> keyIsDown;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-09-23 19:12:20 +02:00
|
|
|
//! Whether a key was down
|
|
|
|
std::unordered_set<KeyPress> keyWasDown;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-09-23 19:12:20 +02:00
|
|
|
//! Whether a key has just been pressed
|
|
|
|
std::unordered_set<KeyPress> keyWasPressed;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-09-23 19:12:20 +02:00
|
|
|
//! Whether a key has just been released
|
|
|
|
std::unordered_set<KeyPress> keyWasReleased;
|
2020-06-05 15:06:35 +02:00
|
|
|
|
2020-09-23 19:12:20 +02:00
|
|
|
//! List of keys we listen for
|
|
|
|
std::unordered_set<KeyPress> 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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyReleased(k);
|
|
|
|
}
|
|
|
|
virtual bool wasKeyReleased(GameKeyType k)
|
|
|
|
{
|
|
|
|
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
|
|
|
|
}
|
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-09-23 19:12:20 +02:00
|
|
|
virtual bool isKeyDown(GameKeyType k) { return keydown.count(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; }
|
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-09-23 19:12:20 +02:00
|
|
|
std::unordered_set<KeyPress> keydown;
|
2015-02-10 21:14:24 +01:00
|
|
|
v2s32 mousepos;
|
|
|
|
v2s32 mousespeed;
|
|
|
|
};
|