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 "cheatMenu.h"
18 #include "script/scripting_client.h"
19 #include "client/client.h"
20 #include "client/fontengine.h"
23 CheatMenu::CheatMenu(Client *client) : m_client(client)
25 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
28 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
30 core::dimension2d<u32> dim = m_font->getDimension(L"M");
31 m_fontsize = v2u32(dim.Width, dim.Height);
34 m_fontsize.X = MYMAX(m_fontsize.X, 1);
35 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
38 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name,
39 std::size_t column_align_index, std::size_t cheat_entry_index,
40 bool is_selected, bool is_enabled, CheatMenuEntryType entry_type)
42 int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
43 video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
45 // Align with correct column.
46 x += m_gap + column_align_index * (m_entry_width + m_gap);
49 fontcolor = &m_selected_font_color;
51 bgcolor = &m_active_bg_color;
55 case CHEAT_MENU_ENTRY_TYPE_HEAD:
56 height = m_head_height;
58 case CHEAT_MENU_ENTRY_TYPE_CATEGORY:
59 y += m_head_height + m_gap;
61 case CHEAT_MENU_ENTRY_TYPE_ENTRY:
62 y += m_head_height + (cheat_entry_index + 1) * (m_entry_height + m_gap);
65 // TODO log an error or something.
69 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
71 driver->draw2DRectangleOutline(
72 core::rect<s32>(x - 2, y - 2, x + width + 1, y + height + 1),
74 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
75 core::rect<s32> fontbounds(
76 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
77 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
80 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
82 ClientScripting *script{ getScript() };
83 if (!script || !script->m_cheats_loaded)
86 // Draw menu header if debug info is not being drawn.
88 drawEntry(driver, "Dragonfireclient", 0, 0, false, false,
89 CHEAT_MENU_ENTRY_TYPE_HEAD);
91 int category_count = 0;
92 for (const auto &menu_item : script->m_cheat_categories) {
93 bool is_selected = category_count == m_selected_category;
94 drawEntry(driver, menu_item->m_name, category_count, 0, is_selected,
95 false, CHEAT_MENU_ENTRY_TYPE_CATEGORY);
96 if (is_selected && m_cheat_layer) {
98 for (const auto &sub_menu_item : menu_item->m_cheats) {
99 drawEntry(driver, sub_menu_item->m_name, category_count,
100 cheat_count, cheat_count == m_selected_cheat,
101 sub_menu_item->is_enabled());
109 void CheatMenu::selectLeft()
111 CHEAT_MENU_GET_SCRIPTPTR
113 int max = script->m_cheat_categories.size() - 1;
114 int *selected = &m_selected_category;
120 void CheatMenu::selectRight()
122 CHEAT_MENU_GET_SCRIPTPTR
124 int max = script->m_cheat_categories.size() - 1;
125 int *selected = &m_selected_category;
131 void CheatMenu::selectDown()
133 CHEAT_MENU_GET_SCRIPTPTR
135 m_cheat_layer = true;
137 int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
138 int *selected = &m_selected_cheat;
140 if (*selected > max) {
145 void CheatMenu::selectUp()
147 if (!m_cheat_layer) {
151 CHEAT_MENU_GET_SCRIPTPTR
153 int *selected = &m_selected_cheat;
157 m_cheat_layer = false;
162 void CheatMenu::selectConfirm()
164 CHEAT_MENU_GET_SCRIPTPTR
167 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
168 ->m_cheats[m_selected_cheat]);