-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCChatBubble.cpp
More file actions
executable file
·145 lines (116 loc) · 4.32 KB
/
CChatBubble.cpp
File metadata and controls
executable file
·145 lines (116 loc) · 4.32 KB
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
//
// CChatBubble.cpp
// Third
//
// Created by Didrik Munther on 22/04/15.
// Copyright (c) 2015 Didrik Munther. All rights reserved.
//
#include <sstream>
#include "CChatBubble.h"
#include "CAssetManager.h"
#include "CEntity.h"
#include "NSurface.h"
#include "CCamera.h"
CChatBubble::CChatBubble(std::string text, CEntity* target, std::string fontKey, ChatBubbleType type) :
_target(target), _type(type), CGuiText(0, 0, text, fontKey), _widestLine(0), _totalHeight(0),
_r(0), _g(0), _b(0), _rB(220), _gB(220), _bB(220) {
if(CAssetManager::getFont(fontKey) == nullptr) {
return;
}
int textSize = 20;
int letterPerSecond = 5;
bool instantText = false;
switch(type) {
case ChatBubbleType::SAY:
_r = _g = _b = 0; // Black
break;
case ChatBubbleType::YELL:
_r = 255; // Red
break;
case ChatBubbleType::WHISPER:
_g = _b = 255; // Cyan
break;
case ChatBubbleType::INSTANT_TALK:
instantText = true;
break;
}
std::vector<std::string> splittedText; // All this basically splits the text up so that it becomes several lines instead of a long one
std::stringstream ss(text);
std::string item;
const char* delim = " ";
while (std::getline(ss, item, *delim)) {
splittedText.push_back(item);
}
int currentSize = 0;
std::string currentString = "";
for(int i = 0; i < splittedText.size(); i++) {
if(currentSize > 10) {
_TextVector.push_back(CText(currentString, textSize, fontKey, Color{(Uint8)_r, (Uint8)_g, (Uint8)_b, 255}));
currentString = "";
currentSize = 0;
}
currentSize += (int)splittedText[i].length();
currentString += splittedText[i] + " ";
}
if(currentSize > 0) // For when the loop quits but there is still text that should be added
_TextVector.push_back(CText(currentString, textSize, fontKey, Color{(Uint8)_r, (Uint8)_g, (Uint8)_b, 255}));
if(!instantText)
_livingTime = (int)text.length() / letterPerSecond + 3;
else
_livingTime = 0;
int width, height;
auto i = _TextVector.begin();
while(i != _TextVector.end()) {
TTF_SizeText(i->getFont(), i->getText()->c_str(), &width, &height);
if(width > _widestLine)
_widestLine = width;
_totalHeight += height;
i++;
}
}
void CChatBubble::onLoop() {
if(SDL_GetTicks() > _creationTime + _livingTime * 1000)
_toRemove = true;
}
void CChatBubble::onRender(CWindow* window, CCamera* camera, RenderFlags renderFlags) {
if(_target == nullptr)
return;
if(_TextVector.size() <= 0)
return;
if(_TextVector[0].getFont() == nullptr)
return;
int marginX = 4;
int marginY = 2;
int floatOverHead = 20;
int boxX = _target->body->getX() + _target->body->getW() / 2 - _widestLine / 2;
int boxY = _target->body->getY() - _totalHeight - floatOverHead;
int boxW = _widestLine;
int boxH = _totalHeight;
if(camera->collision(boxX,
boxY,
boxW,
boxH)) {
NSurface::renderRect(boxX - camera->offsetX(),
boxY - camera->offsetY(),
boxW,
boxH,
window, _rB, _gB, _bB, 100);
int currentLine = 0;
auto i = _TextVector.begin();
while(i != _TextVector.end()) {
int width, height;
TTF_SizeText(i->getFont(), i->getText()->c_str(), &width, &height);
int posX = _target->body->getX() + _target->body->getW() / 2 - width / 2 + marginX;
int posY = _target->body->getY() - _totalHeight + height * currentLine - floatOverHead + marginY * 2;
if(!camera->collision(posX, posY, width + marginX, height + marginY * 2)) {
currentLine++;
i++;
continue;
}
i++->onRender(posX - camera->offsetX(),
posY - camera->offsetY(),
window);
currentLine++;
}
}
}