]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/cheatMenu.cpp
Add spider
[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 "script/scripting_client.h"
21 #include "client/client.h"
22 #include "client/fontengine.h"
23 #include "cheatMenu.h"
24 #include <cstddef>
25
26 FontMode CheatMenu::fontStringToEnum(std::string str)
27 {
28         if (str == "FM_Standard")
29                 return FM_Standard;
30         else if (str == "FM_Mono")
31                 return FM_Mono;
32         else if (str == "FM_Fallback")
33                 return _FM_Fallback;
34         else if (str == "FM_Simple")
35                 return FM_Simple;
36         else if (str == "FM_SimpleMono")
37                 return FM_SimpleMono;
38         else if (str == "FM_MaxMode")
39                 return FM_MaxMode;
40         else if (str == "FM_Unspecified")
41                 return FM_Unspecified;
42         else
43                 return FM_Standard;
44 }
45
46 CheatMenu::CheatMenu(Client *client) : m_client(client)
47 {
48         FontMode fontMode = fontStringToEnum(g_settings->get("cheat_menu_font"));
49         v3f bg_color, active_bg_color, font_color, selected_font_color;
50
51         bg_color = g_settings->getV3F("cheat_menu_bg_color");
52         active_bg_color = g_settings->getV3F("cheat_menu_active_bg_color");
53         font_color = g_settings->getV3F("cheat_menu_font_color");
54         selected_font_color = g_settings->getV3F("cheat_menu_selected_font_color");
55
56         m_bg_color = video::SColor(g_settings->getU32("cheat_menu_bg_color_alpha"),
57                         bg_color.X, bg_color.Y, bg_color.Z);
58
59         m_active_bg_color = video::SColor(
60                         g_settings->getU32("cheat_menu_active_bg_color_alpha"),
61                         active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
62
63         m_font_color = video::SColor(g_settings->getU32("cheat_menu_font_color_alpha"),
64                         font_color.X, font_color.Y, font_color.Z);
65
66         m_selected_font_color = video::SColor(
67                         g_settings->getU32("cheat_menu_selected_font_color_alpha"),
68                         selected_font_color.X, selected_font_color.Y,
69                         selected_font_color.Z);
70
71         m_head_height = g_settings->getU32("cheat_menu_head_height");
72         m_entry_height = g_settings->getU32("cheat_menu_entry_height");
73         m_entry_width = g_settings->getU32("cheat_menu_entry_width");
74
75         m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
76
77         if (!m_font) {
78                 errorstream << "CheatMenu: Unable to load font" << std::endl;
79         } else {
80                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
81                 m_fontsize = v2u32(dim.Width, dim.Height);
82                 m_font->grab();
83         }
84         m_fontsize.X = MYMAX(m_fontsize.X, 1);
85         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
86 }
87
88 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name, int number,
89                 bool selected, bool active, CheatMenuEntryType entry_type)
90 {
91         int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
92         video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
93         if (entry_type == CHEAT_MENU_ENTRY_TYPE_HEAD) {
94                 bgcolor = &m_active_bg_color;
95                 height = m_head_height;
96         } else {
97                 bool is_category = entry_type == CHEAT_MENU_ENTRY_TYPE_CATEGORY;
98                 y += m_gap + m_head_height +
99                      (number + (is_category ? 0 : m_selected_category)) *
100                                      (m_entry_height + m_gap);
101                 x += (is_category ? 0 : m_gap + m_entry_width);
102                 if (active)
103                         bgcolor = &m_active_bg_color;
104                 if (selected)
105                         fontcolor = &m_selected_font_color;
106         }
107         driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
108         if (selected)
109                 driver->draw2DRectangleOutline(
110                                 core::rect<s32>(x - 1, y - 1, x + width, y + height),
111                                 *fontcolor);
112         int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
113         core::rect<s32> fontbounds(
114                         fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
115         m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
116 }
117
118 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
119 {
120         CHEAT_MENU_GET_SCRIPTPTR
121
122         if (!show_debug)
123                 drawEntry(driver, "Dragonfireclient", 0, false, false,
124                                 CHEAT_MENU_ENTRY_TYPE_HEAD);
125         int category_count = 0;
126         for (auto category = script->m_cheat_categories.begin();
127                         category != script->m_cheat_categories.end(); category++) {
128                 bool is_selected = category_count == m_selected_category;
129                 drawEntry(driver, (*category)->m_name, category_count, is_selected, false,
130                                 CHEAT_MENU_ENTRY_TYPE_CATEGORY);
131                 if (is_selected && m_cheat_layer) {
132                         int cheat_count = 0;
133                         for (auto cheat = (*category)->m_cheats.begin();
134                                         cheat != (*category)->m_cheats.end(); cheat++) {
135                                 drawEntry(driver, (*cheat)->m_name, cheat_count,
136                                                 cheat_count == m_selected_cheat,
137                                                 (*cheat)->is_enabled());
138                                 cheat_count++;
139                         }
140                 }
141                 category_count++;
142         }
143 }
144
145 void CheatMenu::drawHUD(video::IVideoDriver *driver, double dtime)
146 {
147         CHEAT_MENU_GET_SCRIPTPTR
148
149         m_rainbow_offset += dtime;
150
151         m_rainbow_offset = fmod(m_rainbow_offset, 6.0f);
152
153         std::vector<std::string> enabled_cheats;
154
155         int cheat_count = 0;
156
157         for (auto category = script->m_cheat_categories.begin();
158                         category != script->m_cheat_categories.end(); category++) {
159                 for (auto cheat = (*category)->m_cheats.begin();
160                                 cheat != (*category)->m_cheats.end(); cheat++) {
161                         if ((*cheat)->is_enabled()) {
162                                 enabled_cheats.push_back((*cheat)->m_name);
163                                 cheat_count++;
164                         }
165                 }
166         }
167
168         if (enabled_cheats.empty())
169                 return;
170
171         std::vector<video::SColor> colors;
172
173         for (int i = 0; i < cheat_count; i++) {
174                 video::SColor color = video::SColor(255, 0, 0, 0);
175                 f32 h = (f32)i * 2.0f / (f32)cheat_count - m_rainbow_offset;
176                 if (h < 0)
177                         h = 6.0f + h;
178                 f32 x = (1 - fabs(fmod(h, 2.0f) - 1.0f)) * 255.0f;
179                 switch ((int)h) {
180                 case 0:
181                         color = video::SColor(255, 255, x, 0);
182                         break;
183                 case 1:
184                         color = video::SColor(255, x, 255, 0);
185                         break;
186                 case 2:
187                         color = video::SColor(255, 0, 255, x);
188                         break;
189                 case 3:
190                         color = video::SColor(255, 0, x, 255);
191                         break;
192                 case 4:
193                         color = video::SColor(255, x, 0, 255);
194                         break;
195                 case 5:
196                         color = video::SColor(255, 255, 0, x);
197                         break;
198                 }
199                 colors.push_back(color);
200         }
201
202         core::dimension2d<u32> screensize = driver->getScreenSize();
203
204         u32 y = 5;
205
206         int i = 0;
207         for (std::string cheat : enabled_cheats) {
208                 core::dimension2d<u32> dim =
209                                 m_font->getDimension(utf8_to_wide(cheat).c_str());
210                 u32 x = screensize.Width - 5 - dim.Width;
211
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);
214
215                 y += dim.Height;
216                 i++;
217         }
218 }
219
220 void CheatMenu::selectUp()
221 {
222         CHEAT_MENU_GET_SCRIPTPTR
223
224         int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
225                                                                   ->m_cheats.size()
226                                  : script->m_cheat_categories.size()) -
227                   1;
228         int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
229         --*selected;
230         if (*selected < 0)
231                 *selected = max;
232 }
233
234 void CheatMenu::selectDown()
235 {
236         CHEAT_MENU_GET_SCRIPTPTR
237
238         int max = (m_cheat_layer ? script->m_cheat_categories[m_selected_category]
239                                                                   ->m_cheats.size()
240                                  : script->m_cheat_categories.size()) -
241                   1;
242         int *selected = m_cheat_layer ? &m_selected_cheat : &m_selected_category;
243         ++*selected;
244         if (*selected > max)
245                 *selected = 0;
246 }
247
248 void CheatMenu::selectRight()
249 {
250         if (m_cheat_layer)
251                 return;
252         m_cheat_layer = true;
253         m_selected_cheat = 0;
254 }
255
256 void CheatMenu::selectLeft()
257 {
258         if (!m_cheat_layer)
259                 return;
260         m_cheat_layer = false;
261 }
262
263 void CheatMenu::selectConfirm()
264 {
265         CHEAT_MENU_GET_SCRIPTPTR
266
267         if (m_cheat_layer)
268                 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
269                                                      ->m_cheats[m_selected_cheat]);
270 }