]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/cheatMenu.cpp
c5f01b610101e4a31b2c9c41538135adf428662a
[dragonfireclient.git] / src / gui / cheatMenu.cpp
1 /*
2 Dragonfire
3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
4
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.
9
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.
14
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.
18 */
19
20 #include "cheatMenu.h"
21 #include "script/scripting_client.h"
22 #include "client/client.h"
23 #include "client/fontengine.h"
24
25 CheatMenu::CheatMenu(Client *client) : m_client(client)
26 {
27         m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
28
29         if (!m_font) {
30                 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
31         } else {
32                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
33                 m_fontsize = v2u32(dim.Width, dim.Height);
34                 m_font->grab();
35         }
36         m_fontsize.X = MYMAX(m_fontsize.X, 1);
37         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
38 }
39
40 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name, int number,
41                 bool selected, bool active, int number2, CheatMenuEntryType entry_type)
42 {
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;
48         } else {
49                 bool is_category = entry_type == CHEAT_MENU_ENTRY_TYPE_CATEGORY;
50                 x += m_gap + (is_category? number : number2) * (m_entry_width + m_gap);
51                 y += m_head_height + (is_category ? 0 : number + 1) * (m_entry_height + m_gap);
52                 if (active)
53                         bgcolor = &m_active_bg_color;
54                 if (selected)
55                         fontcolor = &m_selected_font_color;
56         }
57         driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
58         if (selected)
59                 driver->draw2DRectangleOutline(
60                                 core::rect<s32>(x - 1, y - 1, x + width, y + height),
61                                 *fontcolor);
62         int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
63         core::rect<s32> fontbounds(
64                         fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
65         m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
66 }
67
68 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
69 {
70         CHEAT_MENU_GET_SCRIPTPTR
71
72         if (!show_debug)
73                 drawEntry(driver, "Dragonfireclient", 0, false, false, 0,
74                                 CHEAT_MENU_ENTRY_TYPE_HEAD);
75         int category_count = 0;
76         for (auto category = script->m_cheat_categories.begin();
77                         category != script->m_cheat_categories.end(); category++) {
78                 bool is_selected = category_count == m_selected_category;
79                 drawEntry(driver, (*category)->m_name, category_count, is_selected, false, category_count,
80                                 CHEAT_MENU_ENTRY_TYPE_CATEGORY);
81                 if (is_selected && m_cheat_layer) {
82                         int cheat_count = 0;
83                         for (auto cheat = (*category)->m_cheats.begin();
84                                         cheat != (*category)->m_cheats.end(); cheat++) {
85                                 drawEntry(driver, (*cheat)->m_name, cheat_count,
86                                                 cheat_count == m_selected_cheat, category_count,
87                                                 (*cheat)->is_enabled());
88                                 cheat_count++;
89                         }
90                 }
91                 category_count++;
92         }
93 }
94
95 void CheatMenu::selectLeft()
96 {
97         CHEAT_MENU_GET_SCRIPTPTR
98
99         int max = script->m_cheat_categories.size() - 1;
100         int *selected = &m_selected_category;
101         --*selected;
102         if (*selected < 0)
103                 *selected = max;
104 }
105
106 void CheatMenu::selectRight()
107 {
108         CHEAT_MENU_GET_SCRIPTPTR
109
110         int max = script->m_cheat_categories.size() - 1;
111         int *selected = &m_selected_category;
112         ++*selected;
113         if (*selected > max)
114                 *selected = 0;
115 }
116
117 void CheatMenu::selectDown()
118 {
119         CHEAT_MENU_GET_SCRIPTPTR
120         
121         m_cheat_layer = true;
122
123         int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
124         int *selected = &m_selected_cheat;
125         ++*selected;
126         if (*selected > max) {
127                 *selected = 1;
128         }
129 }
130
131 void CheatMenu::selectUp()
132 {
133         if (!m_cheat_layer) {
134                 return;
135         }
136
137         CHEAT_MENU_GET_SCRIPTPTR
138
139         int *selected = &m_selected_cheat;
140         --*selected;
141
142         if (*selected < 0) {
143                 m_cheat_layer = false;
144                 *selected = 1;
145         }
146 }
147
148 void CheatMenu::selectConfirm()
149 {
150         CHEAT_MENU_GET_SCRIPTPTR
151
152         if (m_cheat_layer)
153                 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
154                                                      ->m_cheats[m_selected_cheat]);
155 }