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) {
27 if (str == "FM_Standard")
29 else if (str == "FM_Mono")
31 else if (str == "FM_Fallback")
33 else if (str == "FM_Simple")
35 else if (str == "FM_SimpleMono")
37 else if (str == "FM_MaxMode")
39 else if (str == "FM_Unspecified")
40 return FM_Unspecified;
45 CheatMenu::CheatMenu(Client *client) : m_client(client)
47 FontMode fontMode = fontStringToEnum(g_settings->get("cheat_menu_font"));
48 irr::core::vector3df bg_color;
49 irr::core::vector3df active_bg_color;
50 irr::core::vector3df font_color;
51 irr::core::vector3df selected_font_color;
53 g_settings->getV3FNoEx("m_bg_color", bg_color);
54 g_settings->getV3FNoEx("m_active_bg_color", active_bg_color);
55 g_settings->getV3FNoEx("m_font_color", font_color);
56 g_settings->getV3FNoEx("m_selected_font_color", selected_font_color);
58 m_bg_color = video::SColor(g_settings->getU32("m_bg_color_alpha"),
59 bg_color.X, bg_color.Y, bg_color.Z);
61 m_active_bg_color = video::SColor(g_settings->getU32("m_active_bg_color_alpha"),
62 active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
64 m_font_color = video::SColor(g_settings->getU32("m_font_color_alpha"),
65 font_color.X, font_color.Y, font_color.Z);
67 m_selected_font_color = video::SColor(g_settings->getU32("m_selected_font_color_alpha"),
68 selected_font_color.X, selected_font_color.Y, selected_font_color.Z);
70 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
73 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
75 core::dimension2d<u32> dim = m_font->getDimension(L"M");
76 m_fontsize = v2u32(dim.Width, dim.Height);
79 m_fontsize.X = MYMAX(m_fontsize.X, 1);
80 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
83 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name,
84 std::size_t column_align_index, std::size_t cheat_entry_index,
85 bool is_selected, bool is_enabled, 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;
90 // Align with correct column.
91 x += m_gap + column_align_index * (m_entry_width + m_gap);
94 fontcolor = &m_selected_font_color;
96 bgcolor = &m_active_bg_color;
100 case CHEAT_MENU_ENTRY_TYPE_HEAD:
101 height = m_head_height;
103 case CHEAT_MENU_ENTRY_TYPE_CATEGORY:
104 y += m_head_height + m_gap;
106 case CHEAT_MENU_ENTRY_TYPE_ENTRY:
107 y += m_head_height + (cheat_entry_index + 1) * (m_entry_height + m_gap);
110 // TODO log an error or something.
114 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
116 driver->draw2DRectangleOutline(
117 core::rect<s32>(x - 2, y - 2, x + width + 1, y + height + 1),
119 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
120 core::rect<s32> fontbounds(
121 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
122 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
125 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
127 ClientScripting *script{ getScript() };
128 if (!script || !script->m_cheats_loaded)
131 // Draw menu header if debug info is not being drawn.
133 drawEntry(driver, "Dragonfireclient", 0, 0, false, false,
134 CHEAT_MENU_ENTRY_TYPE_HEAD);
136 int category_count = 0;
137 for (const auto &menu_item : script->m_cheat_categories) {
138 bool is_selected = category_count == m_selected_category;
139 drawEntry(driver, menu_item->m_name, category_count, 0, is_selected,
140 false, CHEAT_MENU_ENTRY_TYPE_CATEGORY);
141 if (is_selected && m_cheat_layer) {
143 for (const auto &sub_menu_item : menu_item->m_cheats) {
144 drawEntry(driver, sub_menu_item->m_name, category_count,
145 cheat_count, cheat_count == m_selected_cheat,
146 sub_menu_item->is_enabled());
154 void CheatMenu::drawHUD(video::IVideoDriver *driver, double dtime)
156 CHEAT_MENU_GET_SCRIPTPTR
158 m_rainbow_offset += dtime;
160 m_rainbow_offset = fmod(m_rainbow_offset, 6.0f);
162 std::vector<std::string> enabled_cheats;
166 for (auto category = script->m_cheat_categories.begin(); category != script->m_cheat_categories.end(); category++) {
167 for (auto cheat = (*category)->m_cheats.begin(); cheat != (*category)->m_cheats.end(); cheat++) {
168 if ((*cheat)->is_enabled()) {
169 enabled_cheats.push_back((*cheat)->m_name);
175 if (enabled_cheats.empty())
178 std::vector<video::SColor> colors;
180 for (int i = 0; i < cheat_count; i++) {
182 f32 h = (f32)i * 2.0f / (f32)cheat_count - m_rainbow_offset;
185 f32 x = (1 - fabs(fmod(h, 2.0f) - 1.0f)) * 255.0f;
188 color = video::SColor(255, 255, x, 0); break;
190 color = video::SColor(255, x, 255, 0); break;
192 color = video::SColor(255, 0, 255, x); break;
194 color = video::SColor(255, 0, x, 255); break;
196 color = video::SColor(255, x, 0, 255); break;
198 color = video::SColor(255, 255, 0, x); break;
200 colors.push_back(color);
203 core::dimension2d<u32> screensize = driver->getScreenSize();
208 for (std::string cheat : enabled_cheats) {
209 core::dimension2d<u32> dim = 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::selectLeft()
222 CHEAT_MENU_GET_SCRIPTPTR
224 int max = script->m_cheat_categories.size() - 1;
225 int *selected = &m_selected_category;
231 void CheatMenu::selectRight()
233 CHEAT_MENU_GET_SCRIPTPTR
235 int max = script->m_cheat_categories.size() - 1;
236 int *selected = &m_selected_category;
242 void CheatMenu::selectDown()
244 CHEAT_MENU_GET_SCRIPTPTR
246 m_cheat_layer = true;
248 int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
249 int *selected = &m_selected_cheat;
251 if (*selected > max) {
256 void CheatMenu::selectUp()
258 if (!m_cheat_layer) {
262 CHEAT_MENU_GET_SCRIPTPTR
264 int *selected = &m_selected_cheat;
268 m_cheat_layer = false;
273 void CheatMenu::selectConfirm()
275 CHEAT_MENU_GET_SCRIPTPTR
278 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
279 ->m_cheats[m_selected_cheat]);