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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
#include "StarChatBubbleManager.hpp"
#include "StarJson.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarConfiguration.hpp"
#include "StarWorldClient.hpp"
#include "StarChattyEntity.hpp"
#include "StarAssets.hpp"
#include "StarAssetTextureGroup.hpp"
#include "StarImageMetadataDatabase.hpp"
#include "StarGuiContext.hpp"
namespace Star {
ChatBubbleManager::ChatBubbleManager()
: m_textTemplate(Vec2F()), m_portraitTextTemplate(Vec2F()) {
auto assets = Root::singleton().assets();
m_guiContext = GuiContext::singletonPtr();
auto jsonData = assets->json("/interface/windowconfig/chatbubbles.config");
m_textStyle.color = jsonToColor(jsonData.get("textColor")).toRgba();
m_textStyle.loadJson(jsonData.get("textStyle"));
m_textPadding = jsonToVec2F(jsonData.get("textPadding"));
m_zoom = jsonData.getInt("textZoom");
m_bubbleOffset = jsonToVec2F(jsonData.get("bubbleOffset"));
m_maxAge = jsonData.getFloat("maxAge");
m_portraitMaxAge = jsonData.getFloat("portraitMaxAge");
unsigned textWrapWidth = jsonData.getUInt("textWrapWidth");
m_textTemplate = TextPositioning{Vec2F(), HorizontalAnchor::HMidAnchor, VerticalAnchor::TopAnchor, textWrapWidth * m_zoom};
m_interBubbleMargin = jsonData.getFloat("interBubbleMargin");
m_maxMessagePerEntity = jsonData.getInt("maxMessagePerEntity");
m_bubbles.setTweenFactor(jsonData.getFloat("tweenFactor"));
m_bubbles.setMovementThreshold(jsonData.getFloat("movementThreshold"));
m_portraitBackgroundImage = jsonData.getString("portraitBackgroundImage");
m_portraitMoreImage = jsonData.getString("portraitMoreImage");
m_portraitMorePosition = jsonToVec2I(jsonData.get("portraitMorePosition"));
m_portraitBackgroundSize = jsonToVec2I(jsonData.get("portraitBackgroundSize"));
m_portraitPosition = jsonToVec2I(jsonData.get("portraitPosition"));
m_portraitSize = jsonToVec2I(jsonData.get("portraitSize"));
m_portraitTextPosition = jsonToVec2I(jsonData.get("portraitTextPosition"));
m_portraitTextWidth = jsonData.getUInt("portraitTextWidth");
m_portraitChatterFramerate = jsonData.getFloat("portraitChatterFramerate");
m_portraitChatterDuration = jsonData.getFloat("portraitChatterDuration");
m_portraitTextTemplate = TextPositioning{Vec2F(m_portraitTextPosition), HorizontalAnchor::LeftAnchor, VerticalAnchor::TopAnchor, m_portraitTextWidth * m_zoom};
// This is a factor(0.0 - 1.0) based on the window size.
// 0.0 is directly over the player, 1.0 is the edge of the window
m_furthestVisibleTextDistance = jsonData.getFloat("furthestTextDistance");
String textFadeFunctionName = jsonData.getString("textFadeFunction");
m_textFadeFunction = Root::singleton().functionDatabase()->function(textFadeFunctionName);
String bubbleFadeFunctionName = jsonData.getString("bubbleFadeFunction");
m_bubbleFadeFunction = Root::singleton().functionDatabase()->function(bubbleFadeFunctionName);
}
void ChatBubbleManager::setCamera(WorldCamera const& camera) {
float oldPixelRatio = m_camera.pixelRatio();
m_camera = camera;
if (m_camera.pixelRatio() != oldPixelRatio) {
List<ChatAction> actions;
m_bubbles.forEach([&actions](BubbleState<Bubble> const& state, Bubble& bubble) {
actions.append(SayChatAction{bubble.entity, bubble.text, state.idealDestination, bubble.config});
});
m_bubbles.clear();
for (auto& portraitBubble : m_portraitBubbles)
actions.append(PortraitChatAction{
portraitBubble.entity,
portraitBubble.portrait,
portraitBubble.text,
portraitBubble.position,
portraitBubble.config
});
m_portraitBubbles.clear();
addChatActions(actions, true);
}
}
void ChatBubbleManager::update(float dt, WorldClientPtr world) {
m_bubbles.forEach([this, dt, &world](BubbleState<Bubble>& bubbleState, Bubble& bubble) {
bubble.age += dt;
if (auto entity = world->get<ChattyEntity>(bubble.entity)) {
bubble.onscreen = m_camera.worldGeometry().rectIntersectsRect(
m_camera.worldScreenRect(), entity->metaBoundBox().translated(entity->position()));
bubbleState.idealDestination = m_camera.worldToScreen(entity->mouthPosition() + m_bubbleOffset);
}
});
for (auto& portraitBubble : m_portraitBubbles) {
portraitBubble.age += dt;
if (auto entity = world->entity(portraitBubble.entity)) {
portraitBubble.onscreen = m_camera.worldGeometry().rectIntersectsRect(m_camera.worldScreenRect(), entity->metaBoundBox().translated(entity->position()));
if (auto chatter = as<ChattyEntity>(entity))
portraitBubble.position = chatter->mouthPosition();
else
portraitBubble.position = entity->position();
}
}
Map<EntityId, int> count;
filter(m_portraitBubbles, [&](PortraitBubble const& portraitBubble) -> bool {
count[portraitBubble.entity] += m_maxMessagePerEntity;
if (count[portraitBubble.entity] > m_maxMessagePerEntity)
return false;
if (world->get<ChattyEntity>(portraitBubble.entity))
return portraitBubble.age < m_portraitMaxAge;
return false;
});
m_bubbles.filter([&](BubbleState<Bubble> const&, Bubble const& bubble) -> bool {
if (++count[bubble.entity] > m_maxMessagePerEntity)
return false;
if (world->get<ChattyEntity>(bubble.entity))
return bubble.age < m_maxAge;
return false;
});
m_bubbles.update(dt);
}
uint8_t ChatBubbleManager::calcDistanceFadeAlpha(Vec2F bubbleScreenPosition, StoredFunctionPtr fadeFunction) const {
// first calculate bubble position as a factor, distance from center to edge
// of screen (0.0-1.0)
float halfScreenwidth = m_camera.screenSize()[0] * 0.5f;
float distanceFactor = (fabsf(bubbleScreenPosition[0] - halfScreenwidth)) / halfScreenwidth;
// that distance factor is divided by the max allowable distance
// to re-space the distance as a 0 - 1 over the max allowable distance
distanceFactor = clamp(distanceFactor / m_furthestVisibleTextDistance, 0.0f, 1.0f);
int alpha = fadeFunction->evaluate(distanceFactor);
return clamp(alpha, 0, 255);
}
void ChatBubbleManager::render() {
if (m_bubbles.empty() && m_portraitBubbles.empty())
return;
if (!Root::singleton().configuration()->get("speechBubbles").toBool())
return;
m_bubbles.forEach([this](BubbleState<Bubble> const& state, Bubble& bubble) {
if (bubble.onscreen) {
int alpha = calcDistanceFadeAlpha(state.currentPosition, m_bubbleFadeFunction);
if (alpha) {
for (auto const& bubbleImage : bubble.backgroundImages)
drawBubbleImage(state.currentPosition, bubbleImage, m_zoom, alpha);
for (auto const& bubbleText : bubble.bubbleText)
drawBubbleText(state.currentPosition, bubbleText, m_zoom, alpha, false);
}
}
});
for (auto portraitBubble : m_portraitBubbles) {
if (portraitBubble.onscreen) {
Vec2F screenPos = m_camera.worldToScreen(portraitBubble.position + m_bubbleOffset);
int frame = 0;
if (portraitBubble.age <= m_portraitChatterDuration)
frame = int((portraitBubble.age / m_portraitChatterFramerate) * 2) % 2;
// 255 here because portrait bubbles are always full opacity
for (auto const& bubbleImage : portraitBubble.backgroundImages)
drawBubbleImage(screenPos, make_tuple(get<0>(bubbleImage).replace("<frame>", toString(frame)), get<1>(bubbleImage)), m_zoom, 255);
// 255 here because portrait bubbles are always full opacity
for (auto const& bubbleText : portraitBubble.bubbleText)
drawBubbleText(screenPos, bubbleText, m_zoom, 255, true);
}
}
}
void ChatBubbleManager::addChatActions(List<ChatAction> chatActions, bool silent) {
auto assets = Root::singleton().assets();
auto config = assets->json("/interface/windowconfig/chatbubbles.config");
float partSize = config.getFloat("partSize");
for (auto action : chatActions) {
Json config = JsonObject{};
Vec2F position;
if (action.is<SayChatAction>()) {
auto sayAction = action.get<SayChatAction>();
config = sayAction.config.optObject().value(JsonObject{});
position = sayAction.position;
// TODO: Get rid of this stupid fucking bullshit, this is the ugliest
// fragilest pointlessest horseshit code in the codebase. It wouldn't
// bother me so bad if it weren't so fucking easy to do right.
// yea I agree
m_guiContext->setTextStyle(m_textStyle, m_zoom);
auto result = m_guiContext->determineTextSize(sayAction.text, m_textTemplate);
float textWidth = result.width() / m_zoom + m_textPadding[0];
float textHeight = result.height() / m_zoom + m_textPadding[1];
Vec2I innerTiles = Vec2I::ceil(Vec2F((textWidth + 4) / partSize, (textHeight + 3) / partSize));
if (innerTiles[0] % 2 == 0)
innerTiles[0] += 1;
if (innerTiles[0] < 3)
innerTiles[0] = 3;
int middleIdx = (innerTiles[0] - 1) / 2;
List<BubbleImage> backgroundImages;
if (config.getBool("drawBorder", true)) {
for (int y = 0; y < innerTiles[1]; y++) {
for (int x = 0; x < innerTiles[0]; x++) {
auto partPosition = [partSize](int x, int y) {
return Vec2F(x * partSize, y * partSize);
};
if (y == 0) {
if (x == 0) {
backgroundImages.append(make_tuple("/interface/chatbubbles/cornerBottomLeft.png", partPosition(x, y)));
} else if (x == innerTiles[0] - 1) {
backgroundImages.append(make_tuple("/interface/chatbubbles/cornerBottomRight.png", partPosition(x, y)));
} else {
if (middleIdx == x)
backgroundImages.append(make_tuple("/interface/chatbubbles/point.png", partPosition(x, y - 1)));
else
backgroundImages.append(make_tuple("/interface/chatbubbles/sideDown.png", partPosition(x, y)));
}
} else if (y == innerTiles[1] - 1) {
if (x == 0)
backgroundImages.append(make_tuple("/interface/chatbubbles/cornerTopLeft.png", partPosition(x, y)));
else if (x == innerTiles[0] - 1)
backgroundImages.append(make_tuple("/interface/chatbubbles/cornerTopRight.png", partPosition(x, y)));
else
backgroundImages.append(make_tuple("/interface/chatbubbles/sideUp.png", partPosition(x, y)));
} else {
if (x == 0)
backgroundImages.append(make_tuple("/interface/chatbubbles/sideLeft.png", partPosition(x, y)));
else if (x == innerTiles[0] - 1)
backgroundImages.append(make_tuple("/interface/chatbubbles/sideRight.png", partPosition(x, y)));
else
backgroundImages.append(make_tuple("/interface/chatbubbles/center.png", partPosition(x, y)));
}
}
}
}
float textMultiLineShift = textHeight;
float horizontalCenter = partSize * innerTiles[0] * 0.5f;
float verticalShift = (partSize * innerTiles[1] - textMultiLineShift) * 0.5f + textMultiLineShift;
Vec2F position = Vec2F(horizontalCenter, verticalShift);
List<BubbleText> bubbleTexts;
TextStyle textStyle = m_textStyle;
textStyle.fontSize = config.getUInt("fontSize", textStyle.fontSize);
if (auto jColor = config.opt("color"))
textStyle.color = jsonToColor(*jColor).toRgba();
textStyle.loadJson(config.get("style", Json()));
bubbleTexts.append(make_tuple(sayAction.text, textStyle, true, position));
for (auto& backgroundImage : backgroundImages)
get<1>(backgroundImage) += Vec2F(-horizontalCenter, partSize);
for (auto& bubbleText : bubbleTexts)
get<3>(bubbleText) += Vec2F(-horizontalCenter, partSize);
auto pos = m_camera.worldToScreen(sayAction.position + m_bubbleOffset);
RectF boundBox = fold(backgroundImages, RectF::null(), [pos, this](RectF const& boundBox, BubbleImage const& bubbleImage) {
return boundBox.combined(bubbleImageRect(pos, bubbleImage, m_zoom));
});
Bubble bubble = {sayAction.entity, sayAction.text, sayAction.config, 0, std::move(backgroundImages), std::move(bubbleTexts), false};
List<BubbleState<Bubble>> oldBubbles = m_bubbles.filtered([&sayAction](BubbleState<Bubble> const&, Bubble const& bubble) {
return bubble.entity == sayAction.entity;
});
m_bubbles.filter([&sayAction](BubbleState<Bubble> const&, Bubble const& bubble) { return bubble.entity != sayAction.entity; });
m_bubbles.addBubble(pos, boundBox, std::move(bubble), m_interBubbleMargin * m_zoom);
oldBubbles.sort([](BubbleState<Bubble> const& a, BubbleState<Bubble> const& b) { return a.contents.age < b.contents.age; });
for (auto& bubble : oldBubbles.slice(0, m_maxMessagePerEntity - 1))
m_bubbles.addBubble(bubble.idealDestination, bubble.boundBox, bubble.contents, 0);
} else if (action.is<PortraitChatAction>()) {
auto portraitAction = action.get<PortraitChatAction>();
config = portraitAction.config.optObject().value(JsonObject{});
position = portraitAction.position;
List<BubbleImage> backgroundImages;
backgroundImages.append(make_tuple(m_portraitBackgroundImage, Vec2F()));
if (config.getBool("drawMoreIndicator", false))
backgroundImages.append(make_tuple(m_portraitMoreImage, Vec2F(m_portraitMorePosition)));
backgroundImages.append(make_tuple(portraitAction.portrait, Vec2F(m_portraitPosition)));
List<BubbleText> bubbleTexts;
bubbleTexts.append(make_tuple(portraitAction.text, m_textStyle, false, Vec2F(m_portraitTextPosition)));
for (auto& backgroundImage : backgroundImages)
get<1>(backgroundImage) += Vec2F(-m_portraitBackgroundSize[0] / 2, 0);
for (auto& bubbleText : bubbleTexts)
get<3>(bubbleText) += Vec2F(-m_portraitBackgroundSize[0] / 2, 0);
m_portraitBubbles.prepend({
portraitAction.entity,
portraitAction.portrait,
portraitAction.text,
portraitAction.position,
portraitAction.config,
0,
std::move(backgroundImages),
std::move(bubbleTexts),
false
});
}
if (!silent) {
if (auto sound = config.optString("sound")) {
auto assets = Root::singleton().assets();
AudioInstancePtr audioInstance = make_shared<AudioInstance>(*assets->audio(*sound));
audioInstance->setPosition(position);
audioInstance->setVolume(config.getFloat("volume", 1.0f));
audioInstance->setPitchMultiplier(config.getFloat("pitch", 1.0f));
m_guiContext->playAudio(audioInstance);
}
}
}
}
RectF ChatBubbleManager::bubbleImageRect(Vec2F screenPos, BubbleImage const& bubbleImage, int pixelRatio) {
auto imgMetadata = Root::singleton().imageMetadataDatabase();
auto& image = get<0>(bubbleImage);
return RectF::withSize(screenPos + get<1>(bubbleImage) * pixelRatio, Vec2F(imgMetadata->imageSize(image)) * pixelRatio);
}
void ChatBubbleManager::drawBubbleImage(Vec2F screenPos, BubbleImage const& bubbleImage, int pixelRatio, int alpha) {
auto& image = get<0>(bubbleImage);
auto offset = get<1>(bubbleImage) * pixelRatio;
m_guiContext->drawQuad(image, screenPos + offset, pixelRatio, {255, 255, 255, alpha});
}
void ChatBubbleManager::drawBubbleText(Vec2F screenPos, BubbleText const& bubbleText, int pixelRatio, int alpha, bool isPortrait) {
m_guiContext->setTextStyle(get<1>(bubbleText), m_zoom);
auto offset = get<3>(bubbleText) * pixelRatio;
TextPositioning tp = isPortrait ? m_portraitTextTemplate : m_textTemplate;
tp.pos = screenPos + offset;
m_guiContext->renderText(get<0>(bubbleText), tp);
}
}
|