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