1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Game refactor [2/X]: Various moves (profilergraph, nodePlacementPrediction, create_formspec_menu)

* Move profilergraph to dedicated files
* Move nodePlacementPrediction to Game class
* Rename create_formspec_menu to GUIFormSpecMenu::create
This commit is contained in:
Loic Blot 2018-01-13 10:34:56 +01:00 committed by Loïc Blot
parent 362323cdc2
commit 64fe79b53b
6 changed files with 418 additions and 363 deletions

View file

@ -10,5 +10,6 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiTable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp
${CMAKE_CURRENT_SOURCE_DIR}/intlGUIEditBox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
PARENT_SCOPE
)

View file

@ -57,6 +57,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
#include "intlGUIEditBox.h"
#include "mainmenumanager.h"
#endif
#define MY_CHECKPOS(a,b) \
@ -129,6 +131,28 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
delete m_text_dst;
}
void GUIFormSpecMenu::create(GUIFormSpecMenu **cur_formspec, Client *client,
JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest)
{
if (*cur_formspec == 0) {
*cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
client, client->getTextureSource(), fs_src, txt_dest);
(*cur_formspec)->doPause = false;
/*
Caution: do not call (*cur_formspec)->drop() here --
the reference might outlive the menu, so we will
periodically check if *cur_formspec is the only
remaining reference (i.e. the menu was removed)
and delete it in that case.
*/
} else {
(*cur_formspec)->setFormSource(fs_src);
(*cur_formspec)->setTextDest(txt_dest);
}
}
void GUIFormSpecMenu::removeChildren()
{
const core::list<gui::IGUIElement*> &children = getChildren();

View file

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

169
src/gui/profilergraph.cpp Normal file
View file

@ -0,0 +1,169 @@
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
*/
#include "profilergraph.h"
#include "util/string.h"
void ProfilerGraph::put(const Profiler::GraphValues &values)
{
m_log.emplace_back(values);
while (m_log.size() > m_log_max_size)
m_log.erase(m_log.begin());
}
void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
gui::IGUIFont *font) const
{
// Do *not* use UNORDERED_MAP here as the order needs
// to be the same for each call to prevent flickering
std::map<std::string, Meta> m_meta;
for (const Piece &piece : m_log) {
for (const auto &i : piece.values) {
const std::string &id = i.first;
const float &value = i.second;
std::map<std::string, Meta>::iterator j = m_meta.find(id);
if (j == m_meta.end()) {
m_meta[id] = Meta(value);
continue;
}
if (value < j->second.min)
j->second.min = value;
if (value > j->second.max)
j->second.max = value;
}
}
// Assign colors
static const video::SColor usable_colors[] = {
video::SColor(255, 255, 100, 100),
video::SColor(255, 90, 225, 90),
video::SColor(255, 100, 100, 255),
video::SColor(255, 255, 150, 50),
video::SColor(255, 220, 220, 100)
};
static const u32 usable_colors_count =
sizeof(usable_colors) / sizeof(*usable_colors);
u32 next_color_i = 0;
for (auto &i : m_meta) {
Meta &meta = i.second;
video::SColor color(255, 200, 200, 200);
if (next_color_i < usable_colors_count)
color = usable_colors[next_color_i++];
meta.color = color;
}
s32 graphh = 50;
s32 textx = x_left + m_log_max_size + 15;
s32 textx2 = textx + 200 - 15;
s32 meta_i = 0;
for (const auto &p: m_meta) {
const std::string &id = p.first;
const Meta &meta = p.second;
s32 x = x_left;
s32 y = y_bottom - meta_i * 50;
float show_min = meta.min;
float show_max = meta.max;
if (show_min >= -0.0001 && show_max >= -0.0001) {
if (show_min <= show_max * 0.5)
show_min = 0;
}
s32 texth = 15;
char buf[10];
snprintf(buf, 10, "%.3g", show_max);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - graphh,
textx2, y - graphh + texth),
meta.color);
snprintf(buf, 10, "%.3g", show_min);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - texth,
textx2, y),
meta.color);
font->draw(utf8_to_wide(id).c_str(),
core::rect<s32>(textx, y - graphh / 2 - texth / 2,
textx2, y - graphh / 2 + texth / 2),
meta.color);
s32 graph1y = y;
s32 graph1h = graphh;
bool relativegraph = (show_min != 0 && show_min != show_max);
float lastscaledvalue = 0.0;
bool lastscaledvalue_exists = false;
for (const Piece &piece : m_log) {
float value = 0;
bool value_exists = false;
Profiler::GraphValues::const_iterator k =
piece.values.find(id);
if (k != piece.values.end()) {
value = k->second;
value_exists = true;
}
if (!value_exists) {
x++;
lastscaledvalue_exists = false;
continue;
}
float scaledvalue = 1.0;
if (show_max != show_min)
scaledvalue = (value - show_min) / (show_max - show_min);
if (scaledvalue == 1.0 && value == 0) {
x++;
lastscaledvalue_exists = false;
continue;
}
if (relativegraph) {
if (lastscaledvalue_exists) {
s32 ivalue1 = lastscaledvalue * graph1h;
s32 ivalue2 = scaledvalue * graph1h;
driver->draw2DLine(v2s32(x - 1, graph1y - ivalue1),
v2s32(x, graph1y - ivalue2), meta.color);
}
lastscaledvalue = scaledvalue;
lastscaledvalue_exists = true;
} else {
s32 ivalue = scaledvalue * graph1h;
driver->draw2DLine(v2s32(x, graph1y),
v2s32(x, graph1y - ivalue), meta.color);
}
x++;
}
meta_i++;
}
}

58
src/gui/profilergraph.h Normal file
View file

@ -0,0 +1,58 @@
/*
Minetest
Copyright (C) 2010-2018 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.
*/
#pragma once
#include <SColor.h>
#include <deque>
#include <utility>
#include <IGUIFont.h>
#include <IVideoDriver.h>
#include "profiler.h"
/* Profiler display */
class ProfilerGraph
{
private:
struct Piece {
Piece(Profiler::GraphValues v) : values(std::move(v)) {}
Profiler::GraphValues values;
};
struct Meta {
float min;
float max;
video::SColor color;
Meta(float initial = 0,
video::SColor color = video::SColor(255, 255, 255, 255)):
min(initial),
max(initial),
color(color)
{}
};
std::deque<Piece> m_log;
public:
u32 m_log_max_size = 200;
ProfilerGraph() = default;
void put(const Profiler::GraphValues &values);
void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
gui::IGUIFont *font) const;
};