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_help.h"
20 #include "bitmap.h"
21 #include "font.h"
22 
Window_Help(int ix,int iy,int iwidth,int iheight,Drawable::Flags flags)23 Window_Help::Window_Help(int ix, int iy, int iwidth, int iheight, Drawable::Flags flags) :
24 	Window_Base(ix, iy, iwidth, iheight, flags),
25 	align(Text::AlignLeft) {
26 
27 	SetContents(Bitmap::Create(width - 16, height - 16));
28 
29 	contents->Clear();
30 }
31 
SetText(std::string text,int color,Text::Alignment align,bool halfwidthspace)32 void Window_Help::SetText(std::string text, int color, Text::Alignment align, bool halfwidthspace) {
33 	if (this->text != text || this->color != color || this->align != align) {
34 		contents->Clear();
35 
36 		text_x_offset = 0;
37 		AddText(text, color, align, halfwidthspace);
38 
39 		this->text = std::move(text);
40 		this->align = align;
41 		this->color = color;
42 	}
43 }
44 
Clear()45 void Window_Help::Clear() {
46 	this->text = "";
47 	this->color = Font::ColorDefault;
48 	this->align = Text::AlignLeft;
49 	text_x_offset = 0;
50 	contents->Clear();
51 }
52 
AddText(std::string text,int color,Text::Alignment align,bool halfwidthspace)53 void Window_Help::AddText(std::string text, int color, Text::Alignment align, bool halfwidthspace) {
54 	std::string::size_type pos = 0;
55 	std::string::size_type nextpos = 0;
56 	while (nextpos != std::string::npos) {
57 		nextpos = text.find(' ', pos);
58 		auto segment = ToStringView(text).substr(pos, nextpos - pos);
59 		contents->TextDraw(text_x_offset, 2, color, segment, align);
60 		text_x_offset += Font::Default()->GetSize(segment).width;
61 
62 		if (nextpos != decltype(text)::npos) {
63 			if (halfwidthspace) {
64 				text_x_offset += Font::Default()->GetSize(" ").width / 2;
65 			} else {
66 				text_x_offset += Font::Default()->GetSize(" ").width;
67 			}
68 			pos = nextpos + 1;
69 		}
70 	}
71 }
72