3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "script/scripting_client.h"
21 #include "client/client.h"
22 #include "client/fontengine.h"
23 #include "cheatMenu.h"
26 FontMode CheatMenu::fontStringToEnum(std::string str)
28 if (str == "FM_Standard")
30 else if (str == "FM_Mono")
32 else if (str == "FM_Fallback")
34 else if (str == "FM_Simple")
36 else if (str == "FM_SimpleMono")
38 else if (str == "FM_MaxMode")
40 else if (str == "FM_Unspecified")
41 return FM_Unspecified;
46 CheatMenu::CheatMenu(Client *client) : m_client(client)
48 FontMode fontMode = fontStringToEnum(g_settings->get("cheat_menu_font"));
49 v3f bg_color, active_bg_color, font_color, selected_font_color;
51 bg_color = g_settings->getV3F("cheat_menu_bg_color");
52 active_bg_color = g_settings->getV3F("cheat_menu_active_bg_color");
53 font_color = g_settings->getV3F("cheat_menu_font_color");
54 selected_font_color = g_settings->getV3F("cheat_menu_selected_font_color");
56 m_bg_color = video::SColor(g_settings->getU32("cheat_menu_bg_color_alpha"),
57 bg_color.X, bg_color.Y, bg_color.Z);
59 m_active_bg_color = video::SColor(
60 g_settings->getU32("cheat_menu_active_bg_color_alpha"),
61 active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
63 m_font_color = video::SColor(g_settings->getU32("cheat_menu_font_color_alpha"),
64 font_color.X, font_color.Y, font_color.Z);
66 m_selected_font_color = video::SColor(
67 g_settings->getU32("cheat_menu_selected_font_color_alpha"),
68 selected_font_color.X, selected_font_color.Y,
69 selected_font_color.Z);
71 m_head_height = g_settings->getU32("cheat_menu_head_height");
72 m_entry_height = g_settings->getU32("cheat_menu_entry_height");
73 m_entry_width = g_settings->getU32("cheat_menu_entry_width");
75 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
78 errorstream << "CheatMenu: Unable to load font" << std::endl;
80 core::dimension2d<u32> dim = m_font->getDimension(L"M");
81 m_fontsize = v2u32(dim.Width, dim.Height);
84 m_fontsize.X = MYMAX(m_fontsize.X, 1);
85 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
88 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name, int number,
89 bool selected, bool active, CheatMenuEntryType entry_type)
91 int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
92 video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
93 if (entry_type == CHEAT_MENU_ENTRY_TYPE_HEAD) {
94 bgcolor = &m_active_bg_color;
95 height = m_head_height;
97 bool is_category = entry_type == CHEAT_MENU_ENTRY_TYPE_CATEGORY;
98 y += m_gap + m_head_height +
99 (number + (is_category ? 0 : m_selected_category)) *
100 (m_entry_height + m_gap);
101 x += (is_category ? 0 : m_gap + m_entry_width);
103 bgcolor = &m_active_bg_color;
105 fontcolor = &m_selected_font_color;
107 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
109 driver->draw2DRectangleOutline(
110 core::rect<s32>(x - 1, y - 1, x + width, y + height),
112 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
113 core::rect<s32> fontbounds(
114 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
115 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
118 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
120 CHEAT_MENU_GET_SCRIPTPTR
123 drawEntry(driver, "Dragonfireclient", 0, false, false,
124 CHEAT_MENU_ENTRY_TYPE_HEAD);
125 int category_count = 0;
126 for (auto category = script->m_cheat_categories.begin();
127 category != script->m_cheat_categories.end(); category++) {
128 bool is_selected = category_count == m_selected_category;
129 drawEntry(driver, (*category)->m_name, category_count, is_selected, false,
130 CHEAT_MENU_ENTRY_TYPE_CATEGORY);
131 if (is_selected && m_cheat_layer) {
133 for (auto cheat = (*category)->m_cheats.begin();
134 cheat != (*category)->m_cheats.end(); cheat++) {
135 drawEntry(driver, (*cheat)->m_name, cheat_count,
136 cheat_count == m_selected_cheat,
137 (*cheat)->is_enabled());
145 void CheatMenu::drawHUD(video::IVideoDriver *driver, double dtime)
147 CHEAT_MENU_GET_SCRIPTPTR
149 m_rainbow_offset += dtime;
151 m_rainbow_offset = fmod(m_rainbow_offset, 6.0f);
153 std::vector<std::string> enabled_cheats;
157 for (auto category = script->m_cheat_categories.begin();
158 category != script->m_cheat_categories.end(); category++) {
159 for (auto cheat = (*category)->m_cheats.begin();
160 cheat != (*category)->m_cheats.end(); cheat++) {
161 if ((*cheat)->is_enabled()) {
162 enabled_cheats.push_back((*cheat)->m_name);
168 if (enabled_cheats.empty())
171 std::vector<video::SColor> colors;
173 for (int i = 0; i < cheat_count; i++) {
174 video::SColor color = video::SColor(255, 0, 0, 0);
175 f32 h = (f32)i * 2.0f / (f32)cheat_count - m_rainbow_offset;
178 f32 x = (1 - fabs(fmod(h, 2.0f) - 1.0f)) * 255.0f;
181 color = video::SColor(255, 255, x, 0);
184 color = video::SColor(255, x, 255, 0);
187 color = video::SColor(255, 0, 255, x);
190 color = video::SColor(255, 0, x, 255);
193 color = video::SColor(255, x, 0, 255);
196 color = video::SColor(255, 255, 0, x);
199 colors.push_back(color);
202 core::dimension2d<u32> screensize = driver->getScreenSize();
207 for (std::string cheat : enabled_cheats) {
208 core::dimension2d<u32> dim =
209 m_font->getDimension(utf8_to_wide(cheat).c_str());
210 u32 x = screensize.Width - 5 - dim.Width;
212 core::rect<s32> fontbounds(x, y, x + dim.Width, y + dim.Height);
213 m_font->draw(cheat.c_str(), fontbounds, colors[i], false, false);
220 void CheatMenu::selectUp()
222 CHEAT_MENU_GET_SCRIPTPTR
224 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
226 : script->m_cheat_categories.size()) -
228 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
234 void CheatMenu::selectDown()
236 CHEAT_MENU_GET_SCRIPTPTR
238 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
240 : script->m_cheat_categories.size()) -
242 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
248 void CheatMenu::selectRight()
252 m_cheat_layer = true;
253 m_selected_cheat = 0;
256 void CheatMenu::selectLeft()
260 m_cheat_layer = false;
263 void CheatMenu::selectConfirm()
265 CHEAT_MENU_GET_SCRIPTPTR
268 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
269 ->m_cheats[m_selected_cheat]);