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_MaxMode")
36 else if (str == "FM_Unspecified")
37 return FM_Unspecified;
42 CheatMenu::CheatMenu(Client *client) : m_client(client)
44 FontMode fontMode = fontStringToEnum(g_settings->get("cheat_menu_font"));
45 v3f bg_color, active_bg_color, font_color, selected_font_color;
47 bg_color = g_settings->getV3F("cheat_menu_bg_color");
48 active_bg_color = g_settings->getV3F("cheat_menu_active_bg_color");
49 font_color = g_settings->getV3F("cheat_menu_font_color");
50 selected_font_color = g_settings->getV3F("cheat_menu_selected_font_color");
52 m_bg_color = video::SColor(g_settings->getU32("cheat_menu_bg_color_alpha"),
53 bg_color.X, bg_color.Y, bg_color.Z);
55 m_active_bg_color = video::SColor(
56 g_settings->getU32("cheat_menu_active_bg_color_alpha"),
57 active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
59 m_font_color = video::SColor(g_settings->getU32("cheat_menu_font_color_alpha"),
60 font_color.X, font_color.Y, font_color.Z);
62 m_selected_font_color = video::SColor(
63 g_settings->getU32("cheat_menu_selected_font_color_alpha"),
64 selected_font_color.X, selected_font_color.Y,
65 selected_font_color.Z);
67 m_head_height = g_settings->getU32("cheat_menu_head_height");
68 m_entry_height = g_settings->getU32("cheat_menu_entry_height");
69 m_entry_width = g_settings->getU32("cheat_menu_entry_width");
71 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
74 errorstream << "CheatMenu: Unable to load font" << std::endl;
76 core::dimension2d<u32> dim = m_font->getDimension(L"M");
77 m_fontsize = v2u32(dim.Width, dim.Height);
80 m_fontsize.X = MYMAX(m_fontsize.X, 1);
81 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
84 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name, int number,
85 bool selected, bool active, CheatMenuEntryType entry_type)
87 int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
88 video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
89 if (entry_type == CHEAT_MENU_ENTRY_TYPE_HEAD) {
90 bgcolor = &m_active_bg_color;
91 height = m_head_height;
93 bool is_category = entry_type == CHEAT_MENU_ENTRY_TYPE_CATEGORY;
94 y += m_gap + m_head_height +
95 (number + (is_category ? 0 : m_selected_category)) *
96 (m_entry_height + m_gap);
97 x += (is_category ? 0 : m_gap + m_entry_width);
99 bgcolor = &m_active_bg_color;
101 fontcolor = &m_selected_font_color;
103 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
105 driver->draw2DRectangleOutline(
106 core::rect<s32>(x - 1, y - 1, x + width, y + height),
108 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
109 core::rect<s32> fontbounds(
110 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
111 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
114 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
116 CHEAT_MENU_GET_SCRIPTPTR
119 drawEntry(driver, "Dragonfireclient", 0, false, false,
120 CHEAT_MENU_ENTRY_TYPE_HEAD);
121 int category_count = 0;
122 for (auto category = script->m_cheat_categories.begin();
123 category != script->m_cheat_categories.end(); category++) {
124 bool is_selected = category_count == m_selected_category;
125 drawEntry(driver, (*category)->m_name, category_count, is_selected, false,
126 CHEAT_MENU_ENTRY_TYPE_CATEGORY);
127 if (is_selected && m_cheat_layer) {
129 for (auto cheat = (*category)->m_cheats.begin();
130 cheat != (*category)->m_cheats.end(); cheat++) {
131 drawEntry(driver, (*cheat)->m_name, cheat_count,
132 cheat_count == m_selected_cheat,
133 (*cheat)->is_enabled());
141 void CheatMenu::drawHUD(video::IVideoDriver *driver, double dtime)
143 CHEAT_MENU_GET_SCRIPTPTR
145 m_rainbow_offset += dtime;
147 m_rainbow_offset = fmod(m_rainbow_offset, 6.0f);
149 std::vector<std::string> enabled_cheats;
153 for (auto category = script->m_cheat_categories.begin();
154 category != script->m_cheat_categories.end(); category++) {
155 for (auto cheat = (*category)->m_cheats.begin();
156 cheat != (*category)->m_cheats.end(); cheat++) {
157 if ((*cheat)->is_enabled()) {
158 enabled_cheats.push_back((*cheat)->m_name);
164 if (enabled_cheats.empty())
167 std::vector<video::SColor> colors;
169 for (int i = 0; i < cheat_count; i++) {
170 video::SColor color = video::SColor(255, 0, 0, 0);
171 f32 h = (f32)i * 2.0f / (f32)cheat_count - m_rainbow_offset;
174 f32 x = (1 - fabs(fmod(h, 2.0f) - 1.0f)) * 255.0f;
177 color = video::SColor(255, 255, x, 0);
180 color = video::SColor(255, x, 255, 0);
183 color = video::SColor(255, 0, 255, x);
186 color = video::SColor(255, 0, x, 255);
189 color = video::SColor(255, x, 0, 255);
192 color = video::SColor(255, 255, 0, x);
195 colors.push_back(color);
198 core::dimension2d<u32> screensize = driver->getScreenSize();
203 for (std::string cheat : enabled_cheats) {
204 core::dimension2d<u32> dim =
205 m_font->getDimension(utf8_to_wide(cheat).c_str());
206 u32 x = screensize.Width - 5 - dim.Width;
208 core::rect<s32> fontbounds(x, y, x + dim.Width, y + dim.Height);
209 m_font->draw(cheat.c_str(), fontbounds, colors[i], false, false);
216 void CheatMenu::selectUp()
218 CHEAT_MENU_GET_SCRIPTPTR
220 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
222 : script->m_cheat_categories.size()) -
224 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
230 void CheatMenu::selectDown()
232 CHEAT_MENU_GET_SCRIPTPTR
234 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
236 : script->m_cheat_categories.size()) -
238 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
244 void CheatMenu::selectRight()
248 m_cheat_layer = true;
249 m_selected_cheat = 0;
252 void CheatMenu::selectLeft()
256 m_cheat_layer = false;
259 void CheatMenu::selectConfirm()
261 CHEAT_MENU_GET_SCRIPTPTR
264 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
265 ->m_cheats[m_selected_cheat]);