]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/cheatMenu.cpp
Merge pull request #3 from JosiahWI/ui_revamp
[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 "cheatMenu.h"
18 #include "script/scripting_client.h"
19 #include "client/client.h"
20 #include "client/fontengine.h"
21 #include <cstddef>
22
23 CheatMenu::CheatMenu(Client *client) : m_client(client)
24 {
25         m_font = g_fontengine->getFont(FONT_SIZE_UNSPECIFIED, FM_Mono);
26
27         if (!m_font) {
28                 errorstream << "CheatMenu: Unable to load fallback font" << std::endl;
29         } else {
30                 core::dimension2d<u32> dim = m_font->getDimension(L"M");
31                 m_fontsize = v2u32(dim.Width, dim.Height);
32                 m_font->grab();
33         }
34         m_fontsize.X = MYMAX(m_fontsize.X, 1);
35         m_fontsize.Y = MYMAX(m_fontsize.Y, 1);
36 }
37
38 void CheatMenu::drawEntry(video::IVideoDriver *driver, std::string name,
39         std::size_t column_align_index, std::size_t cheat_entry_index,
40         bool is_selected, bool is_enabled, CheatMenuEntryType entry_type)
41 {
42         int x = m_gap, y = m_gap, width = m_entry_width, height = m_entry_height;
43         video::SColor *bgcolor = &m_bg_color, *fontcolor = &m_font_color;
44
45         // Align with correct column.
46         x += m_gap + column_align_index * (m_entry_width + m_gap);
47
48         if (is_selected)
49                 fontcolor = &m_selected_font_color;
50         if (is_enabled)
51                 bgcolor = &m_active_bg_color;
52
53         switch (entry_type)
54         {
55         case CHEAT_MENU_ENTRY_TYPE_HEAD:
56                 height = m_head_height;
57                 break;
58         case CHEAT_MENU_ENTRY_TYPE_CATEGORY:
59                 y += m_head_height + m_gap;
60                 break;
61         case CHEAT_MENU_ENTRY_TYPE_ENTRY:
62                 y += m_head_height + (cheat_entry_index + 1) * (m_entry_height + m_gap);
63                 break;
64         default:
65                 // TODO log an error or something.
66                 break;
67         }
68
69         driver->draw2DRectangle(*bgcolor, core::rect<s32>(x, y, x + width, y + height));
70         if (is_selected)
71                 driver->draw2DRectangleOutline(
72                                 core::rect<s32>(x - 2, y - 2, x + width + 1, y + height + 1),
73                                 *fontcolor);
74         int fx = x + 5, fy = y + (height - m_fontsize.Y) / 2;
75         core::rect<s32> fontbounds(
76                         fx, fy, fx + m_fontsize.X * name.size(), fy + m_fontsize.Y);
77         m_font->draw(name.c_str(), fontbounds, *fontcolor, false, false);
78 }
79
80 void CheatMenu::draw(video::IVideoDriver *driver, bool show_debug)
81 {
82         ClientScripting *script{ getScript() };
83         if (!script || !script->m_cheats_loaded)
84         return;
85
86         // Draw menu header if debug info is not being drawn.
87         if (!show_debug)
88                 drawEntry(driver, "Dragonfireclient", 0, 0, false, false,
89                         CHEAT_MENU_ENTRY_TYPE_HEAD);
90
91         int category_count = 0;
92         for (const auto &menu_item : script->m_cheat_categories) {
93                 bool is_selected = category_count == m_selected_category;
94                 drawEntry(driver, menu_item->m_name, category_count, 0, is_selected,
95                         false, CHEAT_MENU_ENTRY_TYPE_CATEGORY);
96                 if (is_selected && m_cheat_layer) {
97                         int cheat_count = 0;
98                         for (const auto &sub_menu_item : menu_item->m_cheats) {
99                                 drawEntry(driver, sub_menu_item->m_name, category_count,
100                                         cheat_count, cheat_count == m_selected_cheat,
101                                         sub_menu_item->is_enabled());
102                                 cheat_count++;
103                         }
104                 }
105                 category_count++;
106         }
107 }
108
109 void CheatMenu::selectLeft()
110 {
111         CHEAT_MENU_GET_SCRIPTPTR
112
113         int max = script->m_cheat_categories.size() - 1;
114         int *selected = &m_selected_category;
115         --*selected;
116         if (*selected < 0)
117                 *selected = max;
118 }
119
120 void CheatMenu::selectRight()
121 {
122         CHEAT_MENU_GET_SCRIPTPTR
123
124         int max = script->m_cheat_categories.size() - 1;
125         int *selected = &m_selected_category;
126         ++*selected;
127         if (*selected > max)
128                 *selected = 0;
129 }
130
131 void CheatMenu::selectDown()
132 {
133         CHEAT_MENU_GET_SCRIPTPTR
134
135         m_cheat_layer = true;
136
137         int max = script->m_cheat_categories[m_selected_category]->m_cheats.size();
138         int *selected = &m_selected_cheat;
139         ++*selected;
140         if (*selected > max) {
141                 *selected = 1;
142         }
143 }
144
145 void CheatMenu::selectUp()
146 {
147         if (!m_cheat_layer) {
148                 return;
149         }
150
151         CHEAT_MENU_GET_SCRIPTPTR
152
153         int *selected = &m_selected_cheat;
154         --*selected;
155
156         if (*selected < 0) {
157                 m_cheat_layer = false;
158                 *selected = 1;
159         }
160 }
161
162 void CheatMenu::selectConfirm()
163 {
164         CHEAT_MENU_GET_SCRIPTPTR
165
166         if (m_cheat_layer)
167                 script->toggle_cheat(script->m_cheat_categories[m_selected_category]
168                                                      ->m_cheats[m_selected_cheat]);
169 }