forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_overlay.cpp
143 lines (116 loc) · 3.42 KB
/
message_overlay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include "message_overlay.h"
#include "player.h"
#include "bitmap.h"
#include "game_message.h"
#include "drawable_mgr.h"
#include "baseui.h"
MessageOverlay::MessageOverlay() : Drawable(Priority_Overlay, Drawable::Flags::Global)
{
// Graphics::RegisterDrawable is in the Update function
}
void MessageOverlay::Draw(Bitmap& dst) {
if (!IsAnyMessageVisible() && !show_all) {
// Don't render overlay when no message visible
return;
}
dst.Blit(ox, oy, *bitmap, bitmap->GetRect(), 255);
if (!dirty) return;
bitmap->Clear();
int i = 0;
for (auto& message : messages) {
if (!message.hidden || show_all) {
bitmap->Blit(0, i * text_height, *black, black->GetRect(), 128);
std::string text = message.text;
if (message.repeat_count > 0) {
text += " [" + std::to_string(message.repeat_count + 1) + "x]";
}
Text::Draw(*bitmap, 2, i * text_height, *Font::DefaultBitmapFont(), message.color, text);
++i;
}
}
dirty = false;
}
void MessageOverlay::AddMessage(const std::string& message, Color color) {
if (message.empty()) {
return;
}
if (message == last_message) {
// The message matches the previous message -> increase counter
messages.back().repeat_count++;
messages.back().hidden = false;
// Keep the old message (with a new counter) on the screen
counter = 0;
dirty = true;
return;
}
last_message = message;
Game_Message::WordWrap(
message,
Player::screen_width - 6, // hardcoded to screen width because the bitmap is not initialized early enough
[&](StringView line) {
messages.emplace_back(std::string(line), color);
}, *Font::DefaultBitmapFont()
);
while (messages.size() > (unsigned)message_max) {
messages.pop_front();
}
dirty = true;
}
void MessageOverlay::Update() {
if (!DisplayUi) {
return;
}
if (!bitmap) {
// Initialisation is delayed because the display is not ready on startup
OnResolutionChange();
}
if (IsAnyMessageVisible()) {
++counter;
if (counter > 150) {
counter = 0;
for (auto& message : messages) {
if (!message.hidden) {
message.hidden = true;
break;
}
}
dirty = true;
}
}
}
void MessageOverlay::SetShowAll(bool show_all) {
this->show_all = show_all;
dirty = true;
}
void MessageOverlay::OnResolutionChange() {
if (!bitmap) {
DrawableMgr::Register(this);
}
black = Bitmap::Create(Player::screen_width, text_height, Color(0, 0, 0, 255));
bitmap = Bitmap::Create(Player::screen_width, text_height * message_max, true);
}
bool MessageOverlay::IsAnyMessageVisible() const {
return std::any_of(messages.cbegin(), messages.cend(), [](const MessageOverlayItem& m) { return !m.hidden; });
}
MessageOverlayItem::MessageOverlayItem(const std::string& text, Color color) :
text(text), color(color)
{
// no-op
}