1 /*
2  * This file is part of EasyRPG Player.
3  *
4  * EasyRPG Player is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * EasyRPG Player is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 // Headers
19 #include "window_command.h"
20 #include "color.h"
21 #include "bitmap.h"
22 #include "util_macro.h"
23 
CalculateWidth(const std::vector<std::string> & commands,int width)24 static int CalculateWidth(const std::vector<std::string>& commands, int width) {
25 	if (width < 0) {
26 		int max = 0;
27 		for (size_t i = 0; i < commands.size(); ++i) {
28 			max = std::max(max, Font::Default()->GetSize(commands[i]).width);
29 		}
30 		return max + 16;
31 	} else {
32 		return width;
33 	}
34 }
35 
Window_Command(std::vector<std::string> in_commands,int width,int max_item)36 Window_Command::Window_Command(std::vector<std::string> in_commands, int width, int max_item) :
37 	Window_Selectable(0, 0, CalculateWidth(in_commands, width), (max_item < 0 ? in_commands.size() : max_item) * 16 + 16)
38 {
39 	ReplaceCommands(std::move(in_commands));
40 }
41 
Refresh()42 void Window_Command::Refresh() {
43 	contents->Clear();
44 	for (int i = 0; i < item_max; i++) {
45 		DrawItem(i, Font::ColorDefault);
46 	}
47 }
48 
DrawItem(int index,Font::SystemColor color)49 void Window_Command::DrawItem(int index, Font::SystemColor color) {
50 	contents->ClearRect(Rect(0, menu_item_height * index, contents->GetWidth() - 0, menu_item_height));
51 	contents->TextDraw(0, menu_item_height * index + menu_item_height / 8, color, commands[index]);
52 }
53 
DisableItem(int i)54 void Window_Command::DisableItem(int i) {
55 	DrawItem(i, Font::ColorDisabled);
56 }
57 
EnableItem(int i)58 void Window_Command::EnableItem(int i) {
59 	DrawItem(i, Font::ColorDefault);
60 }
61 
SetItemText(unsigned index,StringView text)62 void Window_Command::SetItemText(unsigned index, StringView text) {
63 	if (index < commands.size()) {
64 		commands[index] = ToString(text);
65 		DrawItem(index, Font::ColorDefault);
66 	}
67 }
68 
ReplaceCommands(std::vector<std::string> in_commands)69 void Window_Command::ReplaceCommands(std::vector<std::string> in_commands) {
70 	commands = std::move(in_commands);
71 	index = 0;
72 	item_max = commands.size();
73 	const int num_contents = item_max > 0 ? item_max : 1;
74 	SetContents(Bitmap::Create(this->width - 16, num_contents * menu_item_height));
75 	SetTopRow(0);
76 
77 	Refresh();
78 }
79