Game refactor [4/X]: keycache is now owned by InputHandler

* Make InputHandler own the key cache
* Add a helper function InputHandler::cancelPressed to avoid multiple similar calls in game.cpp
* Move RandomInputHandler::step definition into cpp file
master
Loic Blot 2018-01-13 10:54:18 +01:00 committed by Loïc Blot
parent f5a006dce7
commit 99c9e7a986
8 changed files with 198 additions and 186 deletions

View File

@ -158,6 +158,7 @@ LOCAL_SRC_FILES := \
jni/src/gui/guiTable.cpp \ jni/src/gui/guiTable.cpp \
jni/src/guiscalingfilter.cpp \ jni/src/guiscalingfilter.cpp \
jni/src/gui/guiVolumeChange.cpp \ jni/src/gui/guiVolumeChange.cpp \
jni/src/gui/profilergraph.cpp \
jni/src/httpfetch.cpp \ jni/src/httpfetch.cpp \
jni/src/hud.cpp \ jni/src/hud.cpp \
jni/src/imagefilters.cpp \ jni/src/imagefilters.cpp \

View File

@ -29,57 +29,51 @@ void KeyCache::populate_nonchanging()
void KeyCache::populate() void KeyCache::populate()
{ {
key[KeyType::FORWARD] = getKeySetting("keymap_forward"); key[KeyType::FORWARD] = getKeySetting("keymap_forward");
key[KeyType::BACKWARD] = getKeySetting("keymap_backward"); key[KeyType::BACKWARD] = getKeySetting("keymap_backward");
key[KeyType::LEFT] = getKeySetting("keymap_left"); key[KeyType::LEFT] = getKeySetting("keymap_left");
key[KeyType::RIGHT] = getKeySetting("keymap_right"); key[KeyType::RIGHT] = getKeySetting("keymap_right");
key[KeyType::JUMP] = getKeySetting("keymap_jump"); key[KeyType::JUMP] = getKeySetting("keymap_jump");
key[KeyType::SPECIAL1] = getKeySetting("keymap_special1"); key[KeyType::SPECIAL1] = getKeySetting("keymap_special1");
key[KeyType::SNEAK] = getKeySetting("keymap_sneak"); key[KeyType::SNEAK] = getKeySetting("keymap_sneak");
key[KeyType::AUTOFORWARD] = getKeySetting("keymap_autoforward"); key[KeyType::AUTOFORWARD] = getKeySetting("keymap_autoforward");
key[KeyType::DROP] = getKeySetting("keymap_drop"); key[KeyType::DROP] = getKeySetting("keymap_drop");
key[KeyType::INVENTORY] = getKeySetting("keymap_inventory"); key[KeyType::INVENTORY] = getKeySetting("keymap_inventory");
key[KeyType::CHAT] = getKeySetting("keymap_chat"); key[KeyType::CHAT] = getKeySetting("keymap_chat");
key[KeyType::CMD] = getKeySetting("keymap_cmd"); key[KeyType::CMD] = getKeySetting("keymap_cmd");
key[KeyType::CMD_LOCAL] = getKeySetting("keymap_cmd_local"); key[KeyType::CMD_LOCAL] = getKeySetting("keymap_cmd_local");
key[KeyType::CONSOLE] = getKeySetting("keymap_console"); key[KeyType::CONSOLE] = getKeySetting("keymap_console");
key[KeyType::MINIMAP] = getKeySetting("keymap_minimap"); key[KeyType::MINIMAP] = getKeySetting("keymap_minimap");
key[KeyType::FREEMOVE] = getKeySetting("keymap_freemove"); key[KeyType::FREEMOVE] = getKeySetting("keymap_freemove");
key[KeyType::FASTMOVE] = getKeySetting("keymap_fastmove"); key[KeyType::FASTMOVE] = getKeySetting("keymap_fastmove");
key[KeyType::NOCLIP] = getKeySetting("keymap_noclip"); key[KeyType::NOCLIP] = getKeySetting("keymap_noclip");
key[KeyType::HOTBAR_PREV] = getKeySetting("keymap_hotbar_previous"); key[KeyType::HOTBAR_PREV] = getKeySetting("keymap_hotbar_previous");
key[KeyType::HOTBAR_NEXT] = getKeySetting("keymap_hotbar_next"); key[KeyType::HOTBAR_NEXT] = getKeySetting("keymap_hotbar_next");
key[KeyType::MUTE] = getKeySetting("keymap_mute"); key[KeyType::MUTE] = getKeySetting("keymap_mute");
key[KeyType::INC_VOLUME] = getKeySetting("keymap_increase_volume"); key[KeyType::INC_VOLUME] = getKeySetting("keymap_increase_volume");
key[KeyType::DEC_VOLUME] = getKeySetting("keymap_decrease_volume"); key[KeyType::DEC_VOLUME] = getKeySetting("keymap_decrease_volume");
key[KeyType::CINEMATIC] = getKeySetting("keymap_cinematic"); key[KeyType::CINEMATIC] = getKeySetting("keymap_cinematic");
key[KeyType::SCREENSHOT] = getKeySetting("keymap_screenshot"); key[KeyType::SCREENSHOT] = getKeySetting("keymap_screenshot");
key[KeyType::TOGGLE_HUD] = getKeySetting("keymap_toggle_hud"); key[KeyType::TOGGLE_HUD] = getKeySetting("keymap_toggle_hud");
key[KeyType::TOGGLE_CHAT] = getKeySetting("keymap_toggle_chat"); key[KeyType::TOGGLE_CHAT] = getKeySetting("keymap_toggle_chat");
key[KeyType::TOGGLE_FORCE_FOG_OFF] key[KeyType::TOGGLE_FORCE_FOG_OFF] = getKeySetting("keymap_toggle_force_fog_off");
= getKeySetting("keymap_toggle_force_fog_off"); key[KeyType::TOGGLE_UPDATE_CAMERA] = getKeySetting("keymap_toggle_update_camera");
key[KeyType::TOGGLE_UPDATE_CAMERA] key[KeyType::TOGGLE_DEBUG] = getKeySetting("keymap_toggle_debug");
= getKeySetting("keymap_toggle_update_camera"); key[KeyType::TOGGLE_PROFILER] = getKeySetting("keymap_toggle_profiler");
key[KeyType::TOGGLE_DEBUG] key[KeyType::CAMERA_MODE] = getKeySetting("keymap_camera_mode");
= getKeySetting("keymap_toggle_debug"); key[KeyType::INCREASE_VIEWING_RANGE] =
key[KeyType::TOGGLE_PROFILER] getKeySetting("keymap_increase_viewing_range_min");
= getKeySetting("keymap_toggle_profiler"); key[KeyType::DECREASE_VIEWING_RANGE] =
key[KeyType::CAMERA_MODE] getKeySetting("keymap_decrease_viewing_range_min");
= getKeySetting("keymap_camera_mode"); key[KeyType::RANGESELECT] = getKeySetting("keymap_rangeselect");
key[KeyType::INCREASE_VIEWING_RANGE]
= getKeySetting("keymap_increase_viewing_range_min");
key[KeyType::DECREASE_VIEWING_RANGE]
= getKeySetting("keymap_decrease_viewing_range_min");
key[KeyType::RANGESELECT]
= getKeySetting("keymap_rangeselect");
key[KeyType::ZOOM] = getKeySetting("keymap_zoom"); key[KeyType::ZOOM] = getKeySetting("keymap_zoom");
key[KeyType::QUICKTUNE_NEXT] = getKeySetting("keymap_quicktune_next"); key[KeyType::QUICKTUNE_NEXT] = getKeySetting("keymap_quicktune_next");
key[KeyType::QUICKTUNE_PREV] = getKeySetting("keymap_quicktune_prev"); key[KeyType::QUICKTUNE_PREV] = getKeySetting("keymap_quicktune_prev");
key[KeyType::QUICKTUNE_INC] = getKeySetting("keymap_quicktune_inc"); key[KeyType::QUICKTUNE_INC] = getKeySetting("keymap_quicktune_inc");
key[KeyType::QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec"); key[KeyType::QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec");
for (int i = 0; i < 23; i++) { for (int i = 0; i < 23; i++) {
std::string slot_key_name = "keymap_slot" + std::to_string(i + 1); std::string slot_key_name = "keymap_slot" + std::to_string(i + 1);
@ -191,3 +185,72 @@ s32 RandomInputHandler::Rand(s32 min, s32 max)
{ {
return (myrand() % (max - min + 1)) + min; return (myrand() % (max - min + 1)) + min;
} }
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;
}

View File

@ -42,7 +42,8 @@ class InputHandler;
* (up to 10x faster) key lookup is an asset. Other parts of the codebase * (up to 10x faster) key lookup is an asset. Other parts of the codebase
* (e.g. formspecs) should continue using getKeySetting(). * (e.g. formspecs) should continue using getKeySetting().
*/ */
struct KeyCache { struct KeyCache
{
KeyCache() KeyCache()
{ {
@ -210,12 +211,17 @@ private:
class InputHandler class InputHandler
{ {
public: public:
InputHandler() = default; InputHandler()
{
keycache.handler = this;
keycache.populate();
}
virtual ~InputHandler() = default; virtual ~InputHandler() = default;
virtual bool isKeyDown(const KeyPress &keyCode) = 0; virtual bool isKeyDown(GameKeyType k) = 0;
virtual bool wasKeyDown(const KeyPress &keyCode) = 0; virtual bool wasKeyDown(GameKeyType k) = 0;
virtual bool cancelPressed() = 0;
virtual void listenForKey(const KeyPress &keyCode) {} virtual void listenForKey(const KeyPress &keyCode) {}
virtual void dontListenForKeys() {} virtual void dontListenForKeys() {}
@ -243,6 +249,7 @@ public:
virtual void clear() {} virtual void clear() {}
JoystickController joystick; JoystickController joystick;
KeyCache keycache;
}; };
/* /*
Separated input handler Separated input handler
@ -255,13 +262,17 @@ public:
{ {
m_receiver->joystick = &joystick; m_receiver->joystick = &joystick;
} }
virtual bool isKeyDown(const KeyPress &keyCode) virtual bool isKeyDown(GameKeyType k)
{ {
return m_receiver->IsKeyDown(keyCode); return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
} }
virtual bool wasKeyDown(const KeyPress &keyCode) virtual bool wasKeyDown(GameKeyType k)
{ {
return m_receiver->WasKeyDown(keyCode); return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
}
virtual bool cancelPressed()
{
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
} }
virtual void listenForKey(const KeyPress &keyCode) virtual void listenForKey(const KeyPress &keyCode)
{ {
@ -301,11 +312,13 @@ public:
virtual bool getLeftClicked() virtual bool getLeftClicked()
{ {
return m_receiver->leftclicked || joystick.getWasKeyDown(KeyType::MOUSE_L); return m_receiver->leftclicked ||
joystick.getWasKeyDown(KeyType::MOUSE_L);
} }
virtual bool getRightClicked() virtual bool getRightClicked()
{ {
return m_receiver->rightclicked || joystick.getWasKeyDown(KeyType::MOUSE_R); return m_receiver->rightclicked ||
joystick.getWasKeyDown(KeyType::MOUSE_R);
} }
virtual void resetLeftClicked() virtual void resetLeftClicked()
@ -313,18 +326,21 @@ public:
m_receiver->leftclicked = false; m_receiver->leftclicked = false;
joystick.clearWasKeyDown(KeyType::MOUSE_L); joystick.clearWasKeyDown(KeyType::MOUSE_L);
} }
virtual void resetRightClicked() { virtual void resetRightClicked()
{
m_receiver->rightclicked = false; m_receiver->rightclicked = false;
joystick.clearWasKeyDown(KeyType::MOUSE_R); joystick.clearWasKeyDown(KeyType::MOUSE_R);
} }
virtual bool getLeftReleased() virtual bool getLeftReleased()
{ {
return m_receiver->leftreleased || joystick.wasKeyReleased(KeyType::MOUSE_L); return m_receiver->leftreleased ||
joystick.wasKeyReleased(KeyType::MOUSE_L);
} }
virtual bool getRightReleased() virtual bool getRightReleased()
{ {
return m_receiver->rightreleased || joystick.wasKeyReleased(KeyType::MOUSE_R); return m_receiver->rightreleased ||
joystick.wasKeyReleased(KeyType::MOUSE_R);
} }
virtual void resetLeftReleased() virtual void resetLeftReleased()
@ -356,8 +372,9 @@ class RandomInputHandler : public InputHandler
public: public:
RandomInputHandler() = default; RandomInputHandler() = default;
virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; } virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
virtual bool wasKeyDown(const KeyPress &keyCode) { return false; } virtual bool wasKeyDown(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; }
virtual v2s32 getMousePos() { return mousepos; } virtual v2s32 getMousePos() { return mousepos; }
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); } virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
@ -376,74 +393,7 @@ public:
virtual s32 getMouseWheel() { return 0; } virtual s32 getMouseWheel() { return 0; }
virtual void step(float dtime) 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); s32 Rand(s32 min, s32 max);

View File

@ -757,11 +757,11 @@ protected:
inline bool isKeyDown(GameKeyType k) inline bool isKeyDown(GameKeyType k)
{ {
return input->isKeyDown(keycache.key[k]) || input->joystick.isKeyDown(k); return input->isKeyDown(k);
} }
inline bool wasKeyDown(GameKeyType k) inline bool wasKeyDown(GameKeyType k)
{ {
return input->wasKeyDown(keycache.key[k]) || input->joystick.wasKeyDown(k); return input->wasKeyDown(k);
} }
#ifdef __ANDROID__ #ifdef __ANDROID__
@ -859,8 +859,6 @@ private:
*/ */
int crack_animation_length; int crack_animation_length;
KeyCache keycache;
IntervalLimiter profiler_interval; IntervalLimiter profiler_interval;
/* /*
@ -1008,8 +1006,7 @@ bool Game::startup(bool *kill,
this->chat_backend = chat_backend; this->chat_backend = chat_backend;
this->simple_singleplayer_mode = simple_singleplayer_mode; this->simple_singleplayer_mode = simple_singleplayer_mode;
keycache.handler = input; input->keycache.populate();
keycache.populate();
driver = device->getVideoDriver(); driver = device->getVideoDriver();
smgr = RenderingEngine::get_scene_manager(); smgr = RenderingEngine::get_scene_manager();
@ -1521,7 +1518,7 @@ bool Game::connectToServer(const std::string &playername,
break; break;
} }
if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { if (input->cancelPressed()) {
*connection_aborted = true; *connection_aborted = true;
infostream << "Connect aborted [Escape]" << std::endl; infostream << "Connect aborted [Escape]" << std::endl;
break; break;
@ -1593,7 +1590,7 @@ bool Game::getServerContent(bool *aborted)
return false; return false;
} }
if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { if (input->cancelPressed()) {
*aborted = true; *aborted = true;
infostream << "Connect aborted [Escape]" << std::endl; infostream << "Connect aborted [Escape]" << std::endl;
return false; return false;
@ -1706,7 +1703,7 @@ inline bool Game::handleCallbacks()
} }
if (g_gamecallback->keyconfig_changed) { if (g_gamecallback->keyconfig_changed) {
keycache.populate(); // update the cache with new settings input->keycache.populate(); // update the cache with new settings
g_gamecallback->keyconfig_changed = false; g_gamecallback->keyconfig_changed = false;
} }
@ -1865,7 +1862,7 @@ void Game::processKeyInput()
toggleAutoforward(); toggleAutoforward();
} else if (wasKeyDown(KeyType::INVENTORY)) { } else if (wasKeyDown(KeyType::INVENTORY)) {
openInventory(); openInventory();
} else if (wasKeyDown(KeyType::ESC) || input->wasKeyDown(CancelKey)) { } else if (input->cancelPressed()) {
if (!gui_chat_console->isOpenInhibited()) { if (!gui_chat_console->isOpenInhibited()) {
showPauseMenu(); showPauseMenu();
} }
@ -2030,7 +2027,7 @@ void Game::openInventory()
if (!client->moddingEnabled() if (!client->moddingEnabled()
|| !client->getScript()->on_inventory_open(fs_src->m_client->getInventory(inventoryloc))) { || !client->getScript()->on_inventory_open(fs_src->m_client->getInventory(inventoryloc))) {
TextDest *txt_dst = new TextDestPlayerInventory(client); TextDest *txt_dst = new TextDestPlayerInventory(client);
GUIFormSpecMenu::create(&current_formspec, client, &input->joystick, fs_src, GUIFormSpecMenu::create(current_formspec, client, &input->joystick, fs_src,
txt_dst); txt_dst);
cur_formname = ""; cur_formname = "";
current_formspec->setFormSpec(fs_src->getForm(), inventoryloc); current_formspec->setFormSpec(fs_src->getForm(), inventoryloc);
@ -2396,10 +2393,10 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
// distinguish between the two in order to know when to use joysticks. // distinguish between the two in order to know when to use joysticks.
PlayerControl control( PlayerControl control(
input->isKeyDown(keycache.key[KeyType::FORWARD]), input->isKeyDown(KeyType::FORWARD),
input->isKeyDown(keycache.key[KeyType::BACKWARD]), input->isKeyDown(KeyType::BACKWARD),
input->isKeyDown(keycache.key[KeyType::LEFT]), input->isKeyDown(KeyType::LEFT),
input->isKeyDown(keycache.key[KeyType::RIGHT]), input->isKeyDown(KeyType::RIGHT),
isKeyDown(KeyType::JUMP), isKeyDown(KeyType::JUMP),
isKeyDown(KeyType::SPECIAL1), isKeyDown(KeyType::SPECIAL1),
isKeyDown(KeyType::SNEAK), isKeyDown(KeyType::SNEAK),
@ -2535,7 +2532,7 @@ void Game::handleClientEvent_ShowFormSpec(ClientEvent *event, CameraOrientation
TextDestPlayerInventory *txt_dst = TextDestPlayerInventory *txt_dst =
new TextDestPlayerInventory(client, *(event->show_formspec.formname)); new TextDestPlayerInventory(client, *(event->show_formspec.formname));
GUIFormSpecMenu::create(&current_formspec, client, &input->joystick, GUIFormSpecMenu::create(current_formspec, client, &input->joystick,
fs_src, txt_dst); fs_src, txt_dst);
cur_formname = *(event->show_formspec.formname); cur_formname = *(event->show_formspec.formname);
} }
@ -2549,7 +2546,7 @@ void Game::handleClientEvent_ShowLocalFormSpec(ClientEvent *event, CameraOrienta
FormspecFormSource *fs_src = new FormspecFormSource(*event->show_formspec.formspec); FormspecFormSource *fs_src = new FormspecFormSource(*event->show_formspec.formspec);
LocalFormspecHandler *txt_dst = LocalFormspecHandler *txt_dst =
new LocalFormspecHandler(*event->show_formspec.formname, client); new LocalFormspecHandler(*event->show_formspec.formname, client);
GUIFormSpecMenu::create(&current_formspec, client, &input->joystick, fs_src, txt_dst); GUIFormSpecMenu::create(current_formspec, client, &input->joystick, fs_src, txt_dst);
delete event->show_formspec.formspec; delete event->show_formspec.formspec;
delete event->show_formspec.formname; delete event->show_formspec.formname;
@ -3213,7 +3210,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
&client->getEnv().getClientMap(), nodepos); &client->getEnv().getClientMap(), nodepos);
TextDest *txt_dst = new TextDestNodeMetadata(nodepos, client); TextDest *txt_dst = new TextDestNodeMetadata(nodepos, client);
GUIFormSpecMenu::create(&current_formspec, client, &input->joystick, fs_src, GUIFormSpecMenu::create(current_formspec, client, &input->joystick, fs_src,
txt_dst); txt_dst);
cur_formname.clear(); cur_formname.clear();
@ -4110,7 +4107,7 @@ void Game::showPauseMenu()
FormspecFormSource *fs_src = new FormspecFormSource(os.str()); FormspecFormSource *fs_src = new FormspecFormSource(os.str());
LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_PAUSE_MENU"); LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_PAUSE_MENU");
GUIFormSpecMenu::create(&current_formspec, client, &input->joystick, fs_src, txt_dst); GUIFormSpecMenu::create(current_formspec, client, &input->joystick, fs_src, txt_dst);
current_formspec->setFocus("btn_continue"); current_formspec->setFocus("btn_continue");
current_formspec->doPause = true; current_formspec->doPause = true;
} }

View File

@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gettime.h" #include "gettime.h"
#include "gettext.h" #include "gettext.h"
#include "scripting_server.h" #include "scripting_server.h"
#include "mainmenumanager.h"
#include "porting.h" #include "porting.h"
#include "settings.h" #include "settings.h"
#include "client.h" #include "client.h"
@ -131,13 +132,13 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
delete m_text_dst; delete m_text_dst;
} }
void GUIFormSpecMenu::create(GUIFormSpecMenu **cur_formspec, Client *client, void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest) JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest)
{ {
if (*cur_formspec == 0) { if (cur_formspec == nullptr) {
*cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr, cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
client, client->getTextureSource(), fs_src, txt_dest); client, client->getTextureSource(), fs_src, txt_dest);
(*cur_formspec)->doPause = false; cur_formspec->doPause = false;
/* /*
Caution: do not call (*cur_formspec)->drop() here -- Caution: do not call (*cur_formspec)->drop() here --
@ -148,8 +149,8 @@ void GUIFormSpecMenu::create(GUIFormSpecMenu **cur_formspec, Client *client,
*/ */
} else { } else {
(*cur_formspec)->setFormSource(fs_src); cur_formspec->setFormSource(fs_src);
(*cur_formspec)->setTextDest(txt_dest); cur_formspec->setTextDest(txt_dest);
} }
} }

View File

@ -291,7 +291,7 @@ public:
~GUIFormSpecMenu(); ~GUIFormSpecMenu();
static void create(GUIFormSpecMenu **cur_formspec, Client *client, static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest); JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest);
void setFormSpec(const std::string &formspec_string, void setFormSpec(const std::string &formspec_string,

View File

@ -30,7 +30,7 @@ void ProfilerGraph::put(const Profiler::GraphValues &values)
} }
void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
gui::IGUIFont *font) const gui::IGUIFont *font) const
{ {
// Do *not* use UNORDERED_MAP here as the order needs // Do *not* use UNORDERED_MAP here as the order needs
// to be the same for each call to prevent flickering // to be the same for each call to prevent flickering
@ -56,15 +56,13 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
} }
// Assign colors // Assign colors
static const video::SColor usable_colors[] = { static const video::SColor usable_colors[] = {video::SColor(255, 255, 100, 100),
video::SColor(255, 255, 100, 100), video::SColor(255, 90, 225, 90),
video::SColor(255, 90, 225, 90), video::SColor(255, 100, 100, 255),
video::SColor(255, 100, 100, 255), video::SColor(255, 255, 150, 50),
video::SColor(255, 255, 150, 50), video::SColor(255, 220, 220, 100)};
video::SColor(255, 220, 220, 100)
};
static const u32 usable_colors_count = static const u32 usable_colors_count =
sizeof(usable_colors) / sizeof(*usable_colors); sizeof(usable_colors) / sizeof(*usable_colors);
u32 next_color_i = 0; u32 next_color_i = 0;
for (auto &i : m_meta) { for (auto &i : m_meta) {
@ -82,7 +80,7 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
s32 textx2 = textx + 200 - 15; s32 textx2 = textx + 200 - 15;
s32 meta_i = 0; s32 meta_i = 0;
for (const auto &p: m_meta) { for (const auto &p : m_meta) {
const std::string &id = p.first; const std::string &id = p.first;
const Meta &meta = p.second; const Meta &meta = p.second;
s32 x = x_left; s32 x = x_left;
@ -99,18 +97,16 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
char buf[10]; char buf[10];
snprintf(buf, 10, "%.3g", show_max); snprintf(buf, 10, "%.3g", show_max);
font->draw(utf8_to_wide(buf).c_str(), font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - graphh, core::rect<s32>(textx, y - graphh, textx2,
textx2, y - graphh + texth), y - graphh + texth),
meta.color); meta.color);
snprintf(buf, 10, "%.3g", show_min); snprintf(buf, 10, "%.3g", show_min);
font->draw(utf8_to_wide(buf).c_str(), font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - texth, core::rect<s32>(textx, y - texth, textx2, y), meta.color);
textx2, y),
meta.color);
font->draw(utf8_to_wide(id).c_str(), font->draw(utf8_to_wide(id).c_str(),
core::rect<s32>(textx, y - graphh / 2 - texth / 2, core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
textx2, y - graphh / 2 + texth / 2), y - graphh / 2 + texth / 2),
meta.color); meta.color);
s32 graph1y = y; s32 graph1y = y;
s32 graph1h = graphh; s32 graph1h = graphh;
bool relativegraph = (show_min != 0 && show_min != show_max); bool relativegraph = (show_min != 0 && show_min != show_max);
@ -120,8 +116,7 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
for (const Piece &piece : m_log) { for (const Piece &piece : m_log) {
float value = 0; float value = 0;
bool value_exists = false; bool value_exists = false;
Profiler::GraphValues::const_iterator k = Profiler::GraphValues::const_iterator k = piece.values.find(id);
piece.values.find(id);
if (k != piece.values.end()) { if (k != piece.values.end()) {
value = k->second; value = k->second;
@ -149,8 +144,10 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
if (lastscaledvalue_exists) { if (lastscaledvalue_exists) {
s32 ivalue1 = lastscaledvalue * graph1h; s32 ivalue1 = lastscaledvalue * graph1h;
s32 ivalue2 = scaledvalue * graph1h; s32 ivalue2 = scaledvalue * graph1h;
driver->draw2DLine(v2s32(x - 1, graph1y - ivalue1), driver->draw2DLine(
v2s32(x, graph1y - ivalue2), meta.color); v2s32(x - 1, graph1y - ivalue1),
v2s32(x, graph1y - ivalue2),
meta.color);
} }
lastscaledvalue = scaledvalue; lastscaledvalue = scaledvalue;
@ -158,7 +155,7 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
} else { } else {
s32 ivalue = scaledvalue * graph1h; s32 ivalue = scaledvalue * graph1h;
driver->draw2DLine(v2s32(x, graph1y), driver->draw2DLine(v2s32(x, graph1y),
v2s32(x, graph1y - ivalue), meta.color); v2s32(x, graph1y - ivalue), meta.color);
} }
x++; x++;

View File

@ -30,22 +30,25 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class ProfilerGraph class ProfilerGraph
{ {
private: private:
struct Piece { struct Piece
{
Piece(Profiler::GraphValues v) : values(std::move(v)) {} Piece(Profiler::GraphValues v) : values(std::move(v)) {}
Profiler::GraphValues values; Profiler::GraphValues values;
}; };
struct Meta { struct Meta
{
float min; float min;
float max; float max;
video::SColor color; video::SColor color;
Meta(float initial = 0, Meta(float initial = 0,
video::SColor color = video::SColor(255, 255, 255, 255)): video::SColor color = video::SColor(255, 255, 255, 255)) :
min(initial), min(initial),
max(initial), max(initial), color(color)
color(color) {
{} }
}; };
std::deque<Piece> m_log; std::deque<Piece> m_log;
public: public:
u32 m_log_max_size = 200; u32 m_log_max_size = 200;
@ -54,5 +57,5 @@ public:
void put(const Profiler::GraphValues &values); void put(const Profiler::GraphValues &values);
void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
gui::IGUIFont *font) const; gui::IGUIFont *font) const;
}; };