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 "cheatMenu.h"
21 #include "script/scripting_client.h"
22 #include "client/client.h"
23 #include "client/fontengine.h"
25 CheatMenu::CheatMenu(Client *client) : m_client(client)
27 m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Fallback);
30 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
32 core::dimension2d<u32> dim = m_font->getDimension(L"M");
33 m_fontsize = v2u32(dim.Width, dim.Height);
36 m_fontsize.X = MYMAX(m_fontsize.X, 1);
37 m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
40 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name, int number,
41 bool selected, bool active, CheatMenuEntryType entry_type)
43 int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
44 video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
45 if (entry_type == CHEAT_MENU_ENTRY_TYPE_HEAD) {
46 bgcolor = &m_active_bg_color;
47 height = m_head_height;
49 bool is_category = entry_type == CHEAT_MENU_ENTRY_TYPE_CATEGORY;
50 y += m_gap + m_head_height +
51 (number + (is_category ? 0 : m_selected_category)) *
52 (m_entry_height + m_gap);
53 x += (is_category ? 0 : m_gap + m_entry_width);
55 bgcolor = &m_active_bg_color;
57 fontcolor = &m_selected_font_color;
59 driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
61 driver->draw2DRectangleOutline(
62 core::rect<s32>(x - 1, y - 1, x + width, y + height),
64 int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
65 core::rect<s32> fontbounds(
66 fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
67 m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
70 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
72 CHEAT_MENU_GET_SCRIPTPTR
75 drawEntry(driver, "Dragonfireclient", 0, false, false,
76 CHEAT_MENU_ENTRY_TYPE_HEAD);
77 int category_count = 0;
78 for (auto category = script->m_cheat_categories.begin();
79 category != script->m_cheat_categories.end(); category++) {
80 bool is_selected = category_count == m_selected_category;
81 drawEntry(driver, (*category)->m_name, category_count, is_selected, false,
82 CHEAT_MENU_ENTRY_TYPE_CATEGORY);
83 if (is_selected && m_cheat_layer) {
85 for (auto cheat = (*category)->m_cheats.begin();
86 cheat != (*category)->m_cheats.end(); cheat++) {
87 drawEntry(driver, (*cheat)->m_name, cheat_count,
88 cheat_count == m_selected_cheat,
89 (*cheat)->is_enabled());
97 void CheatMenu::selectUp()
99 CHEAT_MENU_GET_SCRIPTPTR
101 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
103 : script->m_cheat_categories.size()) -
105 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
111 void CheatMenu::selectDown()
113 CHEAT_MENU_GET_SCRIPTPTR
115 int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
117 : script->m_cheat_categories.size()) -
119 int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
125 void CheatMenu::selectRight()
129 m_cheat_layer = true;
130 m_selected_cheat = 0;
133 void CheatMenu::selectLeft()
137 m_cheat_layer = false;
140 void CheatMenu::selectConfirm()
142 CHEAT_MENU_GET_SCRIPTPTR
145 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
146 ->m_cheats[m_selected_cheat]);