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 <string>
20 #include <iomanip>
21 #include <sstream>
22 #include "window_savefile.h"
23 #include "game_system.h"
24 #include "input.h"
25 #include "bitmap.h"
26 #include "font.h"
27 #include "player.h"
28 
Window_SaveFile(int ix,int iy,int iwidth,int iheight)29 Window_SaveFile::Window_SaveFile(int ix, int iy, int iwidth, int iheight) :
30 	Window_Base(ix, iy, iwidth, iheight) {
31 
32 	SetBorderX(4);
33 	SetContents(Bitmap::Create(width - 8, height - 16));
34 
35 	Refresh();
36 	UpdateCursorRect();
37 }
38 
UpdateCursorRect()39 void Window_SaveFile::UpdateCursorRect() {
40 	Rect rect = Rect();
41 
42 	if (GetActive()) {
43 		if (override_index > 0) {
44 			rect = Rect(0, 0, Font::Default()->GetSize(GetSaveFileName()).width + 6, 16);
45 		} else {
46 			rect = Rect(0, 0, Font::Default()->GetSize(GetSaveFileName()).width + Font::Default()->GetSize(" ").width * 5 / 2 + 8, 16);
47 		}
48 	}
49 
50 	SetCursorRect(rect);
51 }
52 
GetSaveFileName() const53 std::string Window_SaveFile::GetSaveFileName() const {
54 	std::ostringstream out;
55 	if (!override_name.empty()) {
56 		if (override_name.size() > 14 && has_party) {
57 			out << override_name.substr(0, 11) << "...";
58 		} else {
59 			out << override_name;
60 		}
61 	} else {
62 		out << lcf::Data::terms.file;
63 	}
64 	return out.str();
65 }
66 
SetIndex(int id)67 void Window_SaveFile::SetIndex(int id) {
68 	index = id;
69 }
70 
SetDisplayOverride(const std::string & name,int index)71 void Window_SaveFile::SetDisplayOverride(const std::string& name, int index) {
72 	override_name = name;
73 	override_index = index;
74 }
75 
SetParty(lcf::rpg::SaveTitle title)76 void Window_SaveFile::SetParty(lcf::rpg::SaveTitle title) {
77 	data = std::move(title);
78 	has_party = true;
79 }
80 
SetCorrupted(bool corrupted)81 void Window_SaveFile::SetCorrupted(bool corrupted) {
82 	this->corrupted = corrupted;
83 }
84 
IsValid()85 bool Window_SaveFile::IsValid() {
86 	return has_save && !corrupted;
87 }
88 
SetHasSave(bool valid)89 void Window_SaveFile::SetHasSave(bool valid) {
90 	this->has_save = valid;
91 }
92 
Refresh()93 void Window_SaveFile::Refresh() {
94 	contents->Clear();
95 
96 	Font::SystemColor fc = has_save ? Font::ColorDefault : Font::ColorDisabled;
97 
98 	contents->TextDraw(4, 2, fc, GetSaveFileName());
99 	contents->TextDraw(4 + Font::Default()->GetSize(GetSaveFileName()).width, 2, fc, " ");
100 
101 	std::stringstream out;
102 	out << std::setw(2) << std::setfill(' ') << index + 1;
103 	contents->TextDraw(4 + Font::Default()->GetSize(GetSaveFileName()).width + Font::Default()->GetSize(" ").width / 2, 2, fc, out.str());
104 
105 	if (corrupted) {
106 		contents->TextDraw(4, 16 + 2, Font::ColorKnockout, "Savegame corrupted");
107 		return;
108 	}
109 
110 	if (!has_party) {
111 		return;
112 	}
113 
114 	out.str("");
115 	if (override_index > 0) {
116 		out << lcf::Data::terms.file << std::setw(3) << std::setfill(' ') << override_index;
117 		contents->TextDraw(4, 16+2, fc, out.str());
118 	} else {
119 		contents->TextDraw(4, 16 + 2, fc, data.hero_name);
120 	}
121 
122 	auto lvl_short = ToString(lcf::Data::terms.lvl_short);
123 	if (lvl_short.size() != 2) {
124 		lvl_short.resize(2, ' ');
125 	}
126 
127 	contents->TextDraw(4, 32 + 2, 1, lvl_short);
128 
129 	int lx = Font::Default()->GetSize(lvl_short).width;
130 	out.str("");
131 	out << std::setw(2) << std::setfill(' ') << data.hero_level;
132 	contents->TextDraw(4 + lx, 32 + 2, fc, out.str());
133 
134 	auto hp_short = ToString(lcf::Data::terms.hp_short);
135 	if (hp_short.size() != 2) {
136 		hp_short.resize(2, ' ');
137 	}
138 
139 	contents->TextDraw(46, 32 + 2, 1, hp_short);
140 
141 	int hx = Font::Default()->GetSize(hp_short).width;
142 	out.str("");
143 	out << std::setw(Player::IsRPG2k3() ? 4 : 3) << std::setfill(' ') << data.hero_hp;
144 	contents->TextDraw(46 + hx, 32 + 2, fc, out.str());
145 
146 	int i = 0;
147 	DrawFace(data.face1_name, data.face1_id, 92 + i++ * 56, 0, false);
148 	DrawFace(data.face2_name, data.face2_id, 92 + i++ * 56, 0, false);
149 	DrawFace(data.face3_name, data.face3_id, 92 + i++ * 56, 0, false);
150 	DrawFace(data.face4_name, data.face4_id, 92 + i++ * 56, 0, false);
151 }
152 
Update()153 void Window_SaveFile::Update() {
154 	Window_Base::Update();
155 	UpdateCursorRect();
156 }
157