]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/cheatMenu.cpp
Fix compile error
[dragonfireclient.git] / src / gui / cheatMenu.cpp
1 /*
2 Dragonfire
3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU Lesser General Public License for more details.
12 You should have received a copy of the GNU Lesser General Public License along
13 with this program; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16
17 #include "script/scripting_client.h"
18 #include "client/client.h"
19 #include "client/fontengine.h"
20 #include "cheatMenu.h"
21 #include <cstddef>
22
23 FontMode CheatMenu::fontStringToEnum(std::string str) {
24         if (str == "FM_Standard") 
25                 return FM_Standard;
26         else if (str == "FM_Mono")
27                 return FM_Mono;
28         else if (str == "FM_Fallback")
29                 return FM_Fallback;
30         else if (str == "FM_Simple")
31                 return FM_Simple;
32         else if (str == "FM_SimpleMono")
33                 return FM_SimpleMono;
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         irr::core::vector3df bg_color;
46         irr::core::vector3df active_bg_color;
47         irr::core::vector3df font_color;
48         irr::core::vector3df selected_font_color;
49
50         g_settings->getV3FNoEx("m_bg_color", bg_color);
51         g_settings->getV3FNoEx("m_active_bg_color", active_bg_color);
52         g_settings->getV3FNoEx("m_font_color", font_color);
53         g_settings->getV3FNoEx("m_selected_font_color", selected_font_color);
54
55         m_bg_color = video::SColor(g_settings->getU32("m_bg_color_alpha"), 
56                                                            bg_color.X, bg_color.Y, bg_color.Z);
57         
58         m_active_bg_color = video::SColor(g_settings->getU32("m_active_bg_color_alpha"), 
59                                                                   active_bg_color.X, active_bg_color.Y, active_bg_color.Z);
60
61         m_font_color = video::SColor(g_settings->getU32("m_font_color_alpha"),
62                                                                  font_color.X, font_color.Y, font_color.Z);
63
64         m_selected_font_color = video::SColor(g_settings->getU32("m_selected_font_color_alpha"),
65                                                                                   selected_font_color.X, selected_font_color.Y, selected_font_color.Z);
66         
67         m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, fontMode);
68
69         if (!m_font) {
70                 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
71         } else {
72                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
73                 m_fontsize = v2u32(dim.Width, dim.Height);
74                 m_font->grab();
75         }
76         m_fontsize.X = MYMAX(m_fontsize.X, 1);
77         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
78 }
79
80 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name,
81         std::size_t column_align_index, std::size_t cheat_entry_index,
82         bool is_selected, bool is_enabled, CheatMenuEntryType entry_type)
83 {
84         int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
85         video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
86
87         // Align with correct column.
88         x += m_gap + column_align_index * (m_entry_width + m_gap);
89
90         if (is_selected)
91                 fontcolor = &m_selected_font_color;
92         if (is_enabled)
93                 bgcolor = &m_active_bg_color;
94
95         switch (entry_type)
96         {
97         case CHEAT_MENU_ENTRY_TYPE_HEAD:
98                 height = m_head_height;
99                 break;
100         case CHEAT_MENU_ENTRY_TYPE_CATEGORY:
101                 y += m_head_height + m_gap;
102                 break;
103         case CHEAT_MENU_ENTRY_TYPE_ENTRY:
104                 y += m_head_height + (cheat_entry_index + 1) * (m_entry_height + m_gap);
105                 break;
106         default:
107                 // TODO log an error or something.
108                 break;
109         }
110
111         driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
112         if (is_selected)
113                 driver->draw2DRectangleOutline(
114                                 core::rect<s32>(x - 2, y - 2, x + width + 1, y + height + 1),
115                                 *fontcolor);
116         int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
117         core::rect<s32> fontbounds(
118                         fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
119         m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
120 }
121
122 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
123 {
124         ClientScripting *script{ getScript() };
125         if (!script || !script->m_cheats_loaded)
126         return;
127
128         // Draw menu header if debug info is not being drawn.
129         if (!show_debug)
130                 drawEntry(driver, "Dragonfireclient", 0, 0, false, false,
131                         CHEAT_MENU_ENTRY_TYPE_HEAD);
132
133         int category_count = 0;
134         for (const auto &menu_item : script->m_cheat_categories) {
135                 bool is_selected = category_count == m_selected_category;
136                 drawEntry(driver, menu_item->m_name, category_count, 0, is_selected,
137                         false, CHEAT_MENU_ENTRY_TYPE_CATEGORY);
138                 if (is_selected && m_cheat_layer) {
139                         int cheat_count = 0;
140                         for (const auto &sub_menu_item : menu_item->m_cheats) {
141                                 drawEntry(driver, sub_menu_item->m_name, category_count,
142                                         cheat_count, cheat_count == m_selected_cheat,
143                                         sub_menu_item->is_enabled());
144                                 cheat_count++;
145                         }
146                 }
147                 category_count++;
148         }
149 }
150
151 void CheatMenu::selectLeft()
152 {
153         CHEAT_MENU_GET_SCRIPTPTR
154
155         int max = script->m_cheat_categories.size() - 1;
156         int *selected = &m_selected_category;
157         --*selected;
158         if (*selected < 0)
159                 *selected = max;
160 }
161
162 void CheatMenu::selectRight()
163 {
164         CHEAT_MENU_GET_SCRIPTPTR
165
166         int max = script->m_cheat_categories.size() - 1;
167         int *selected = &m_selected_category;
168         ++*selected;
169         if (*selected > max)
170                 *selected = 0;
171 }
172
173 void CheatMenu::selectDown()
174 {
175         CHEAT_MENU_GET_SCRIPTPTR
176
177         m_cheat_layer = true;
178
179         int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
180         int *selected = &m_selected_cheat;
181         ++*selected;
182         if (*selected > max) {
183                 *selected = 1;
184         }
185 }
186
187 void CheatMenu::selectUp()
188 {
189         if (!m_cheat_layer) {
190                 return;
191         }
192
193         CHEAT_MENU_GET_SCRIPTPTR
194
195         int *selected = &m_selected_cheat;
196         --*selected;
197
198         if (*selected < 0) {
199                 m_cheat_layer = false;
200                 *selected = 1;
201         }
202 }
203
204 void CheatMenu::selectConfirm()
205 {
206         CHEAT_MENU_GET_SCRIPTPTR
207
208         if (m_cheat_layer)
209                 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
210                                                      ->m_cheats[m_selected_cheat]);
211 }