3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU Lesser General Public License for more details.
12 You should have received a copy of the GNU Lesser General Public License along
13 with this program; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #include "script/scripting_client.h"
18 #include "client/client.h"
19 #include "client/fontengine.h"
20 #include "cheatMenu.h"
23 FontMode CheatMenu::fontStringToEnum(std::string str) {
24 if (str == "FM_Standard")
26 else if (str == "FM_Mono")
28 else if (str == "FM_Fallback")
30 else if (str == "FM_Simple")
32 else if (str == "FM_SimpleMono")
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 irr::core::vector3df bg_color;
46 irr::core::vector3df active_bg_color;
47 irr::core::vector3df font_color;
48 irr::core::vector3df selected_font_color;
50 g_settings->getV3FNoEx("m_bg_color", bg_color);
51 g_settings->getV3FNoEx("m_active_bg_color", active_bg_color);
52 g_settings->getV3FNoEx("m_font_color", font_color);
53 g_settings->getV3FNoEx("m_selected_font_color", selected_font_color);
55 m_bg_color = video::SColor(g_settings->getU32("m_bg_color_alpha"),
56 bg_color.X, bg_color.Y, bg_color.Z);
58 m_active_bg_color = video::SColor(g_settings->getU32("m_active_bg_color_alpha"),
59 active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
61 m_font_color = video::SColor(g_settings->getU32("m_font_color_alpha"),
62 font_color.X, font_color.Y, font_color.Z);
64 m_selected_font_color = video::SColor(g_settings->getU32("m_selected_font_color_alpha"),
65 selected_font_color.X, selected_font_color.Y, selected_font_color.Z);
67 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
70 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
72 core::dimension2d<u32> dim = m_font->getDimension(L"M");
73 m_fontsize = v2u32(dim.Width, dim.Height);
76 m_fontsize.X = MYMAX(m_fontsize.X, 1);
77 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
80 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name,
81 std::size_t column_align_index, std::size_t cheat_entry_index,
82 bool is_selected, bool is_enabled, CheatMenuEntryType entry_type)
84 int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
85 video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
87 // Align with correct column.
88 x += m_gap + column_align_index * (m_entry_width + m_gap);
91 fontcolor = &m_selected_font_color;
93 bgcolor = &m_active_bg_color;
97 case CHEAT_MENU_ENTRY_TYPE_HEAD:
98 height = m_head_height;
100 case CHEAT_MENU_ENTRY_TYPE_CATEGORY:
101 y += m_head_height + m_gap;
103 case CHEAT_MENU_ENTRY_TYPE_ENTRY:
104 y += m_head_height + (cheat_entry_index + 1) * (m_entry_height + m_gap);
107 // TODO log an error or something.
111 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
113 driver->draw2DRectangleOutline(
114 core::rect<s32>(x - 2, y - 2, x + width + 1, y + height + 1),
116 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
117 core::rect<s32> fontbounds(
118 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
119 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
122 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
124 ClientScripting *script{ getScript() };
125 if (!script || !script->m_cheats_loaded)
128 // Draw menu header if debug info is not being drawn.
130 drawEntry(driver, "Dragonfireclient", 0, 0, false, false,
131 CHEAT_MENU_ENTRY_TYPE_HEAD);
133 int category_count = 0;
134 for (const auto &menu_item : script->m_cheat_categories) {
135 bool is_selected = category_count == m_selected_category;
136 drawEntry(driver, menu_item->m_name, category_count, 0, is_selected,
137 false, CHEAT_MENU_ENTRY_TYPE_CATEGORY);
138 if (is_selected && m_cheat_layer) {
140 for (const auto &sub_menu_item : menu_item->m_cheats) {
141 drawEntry(driver, sub_menu_item->m_name, category_count,
142 cheat_count, cheat_count == m_selected_cheat,
143 sub_menu_item->is_enabled());
151 void CheatMenu::selectLeft()
153 CHEAT_MENU_GET_SCRIPTPTR
155 int max = script->m_cheat_categories.size() - 1;
156 int *selected = &m_selected_category;
162 void CheatMenu::selectRight()
164 CHEAT_MENU_GET_SCRIPTPTR
166 int max = script->m_cheat_categories.size() - 1;
167 int *selected = &m_selected_category;
173 void CheatMenu::selectDown()
175 CHEAT_MENU_GET_SCRIPTPTR
177 m_cheat_layer = true;
179 int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
180 int *selected = &m_selected_cheat;
182 if (*selected > max) {
187 void CheatMenu::selectUp()
189 if (!m_cheat_layer) {
193 CHEAT_MENU_GET_SCRIPTPTR
195 int *selected = &m_selected_cheat;
199 m_cheat_layer = false;
204 void CheatMenu::selectConfirm()
206 CHEAT_MENU_GET_SCRIPTPTR
209 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
210 ->m_cheats[m_selected_cheat]);