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