summaryrefslogtreecommitdiff
path: root/src/SSVOpenHexagon/Core/HexagonDialogBox.cpp
blob: 851d36090e2691c87be5f6f9fb6928f6001de416 (plain)
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
#include "SSVOpenHexagon/Core/HexagonDialogBox.hpp"

#include "SSVOpenHexagon/Global/Assert.hpp"
#include "SSVOpenHexagon/Utils/FontHeight.hpp"
#include "SSVOpenHexagon/Data/StyleData.hpp"
#include "SSVOpenHexagon/Global/Config.hpp"
#include "SSVOpenHexagon/Utils/FastVertexVector.hpp"

#include <SSVStart/Utils/SFML.hpp>
#include <SSVStart/GameSystem/GameWindow.hpp>

#include <algorithm>
#include <string>
#include <tuple>

namespace hg {

[[nodiscard]] static Utils::FastVertexVectorQuads& getDialogFrame()
{
    thread_local Utils::FastVertexVectorQuads result;
    return result;
}

HexagonDialogBox::HexagonDialogBox(sf::Font& mFont, ssvs::GameWindow& mWindow)
    : window{mWindow}, txtDialog{"", mFont, 0}
{}

void HexagonDialogBox::create(const std::string& output, const int charSize,
    const float mFrameSize, const DBoxDraw mDrawMode, const float mXPos,
    const float mYPos, const bool mInputBox)
{
    lineHeight = Utils::getFontHeight(txtDialog, charSize);
    txtDialog.setString(output);
    dialogWidth = ssvs::getGlobalWidth(txtDialog);
    frameSize = mFrameSize;
    doubleFrameSize = 2.f * frameSize;
    drawMode = mDrawMode;
    xPos = mXPos;
    yPos = mYPos;
    inputBox = mInputBox;
    inputBoxPassword = false;
    input.clear();

    std::string temp;
    for(char c : output)
    {
        if(c == '\n')
        {
            dialogText.emplace_back(temp);
            temp.clear();
        }
        else
        {
            temp += c;
        }
    }

    const int size = dialogText.size();
    const float dialogHeight =
        lineHeight * size + (lineHeight / 2.f) * (size - 1);
    totalHeight = dialogHeight + 2.f * doubleFrameSize;

    if(inputBox)
    {
        totalHeight += lineHeight * 4;
    }
}

void HexagonDialogBox::create(const std::string& output, const int charSize,
    const float mFrameSize, const DBoxDraw mDrawMode, const KKey mKeyToClose,
    const float mXPos, const float mYPos)
{
    create(output, charSize, mFrameSize, mDrawMode, mXPos, mYPos);
    keyToClose = mKeyToClose;
}

void HexagonDialogBox::createInput(const std::string& output,
    const int charSize, const float mFrameSize, const DBoxDraw mDrawMode)
{
    create(output, charSize, mFrameSize, mDrawMode, 0.f, 0.f, true);
    keyToClose = KKey::Enter;
}

void HexagonDialogBox::draw(
    const sf::Color& txtColor, const sf::Color& backdropColor)
{
    switch(drawMode)
    {
        case DBoxDraw::topLeft:
        {
            drawTopLeft(txtColor, backdropColor);
            break;
        }

        case DBoxDraw::center:
        {
            drawCenter(txtColor, backdropColor);
            break;
        }

        default:
        {
            SSVOH_ASSERT(drawMode == DBoxDraw::centerUpperHalf);
            drawCenterUpperHalf(txtColor, backdropColor);
            break;
        }
    }
}

void HexagonDialogBox::drawBox(Utils::FastVertexVectorQuads& quads,
    const sf::Color& frameColor, const float x1, const float x2, const float y1,
    const float y2)
{
    const sf::Vector2f topLeft{x1, y1};
    const sf::Vector2f topRight{x2, y1};
    const sf::Vector2f bottomRight{x2, y2};
    const sf::Vector2f bottomLeft{x1, y2};

    quads.batch_unsafe_emplace_back(
        frameColor, topLeft, topRight, bottomRight, bottomLeft);
}

void HexagonDialogBox::drawText(
    const sf::Color& txtColor, const float xOffset, const float yOffset)
{
    float heightOffset = 0.f;
    const float interline = lineHeight * 1.5f;
    txtDialog.setFillColor(txtColor);

    for(auto& str : dialogText)
    {
        if(!str.empty())
        {
            txtDialog.setString(str);
            txtDialog.setPosition(
                {xOffset - ssvs::getGlobalWidth(txtDialog) / 2.f,
                    yOffset + heightOffset + 5.f});
            window.draw(txtDialog);
        }

        heightOffset += interline;
    }

    if(inputBox)
    {
        heightOffset += interline;

        if(inputBoxPassword)
        {
            txtDialog.setString(std::string(input.size(), '*'));
        }
        else
        {
            txtDialog.setString(input);
        }

        txtDialog.setPosition({xOffset - ssvs::getGlobalWidth(txtDialog) / 2.f,
            yOffset + heightOffset + 5.f});
        window.draw(txtDialog);
    }
}

inline constexpr float fontHeightDifferential = 0.9f;

void HexagonDialogBox::drawTopLeft(
    const sf::Color& txtColor, const sf::Color& backdropColor)
{
    Utils::FastVertexVectorQuads& dialogFrame = getDialogFrame();
    dialogFrame.clear();
    dialogFrame.reserve(8);

    // outer frame
    drawBox(dialogFrame, txtColor, xPos,
        2.f * doubleFrameSize + dialogWidth + xPos, yPos, totalHeight + yPos);

    // text backdrop
    drawBox(dialogFrame, backdropColor, frameSize + xPos,
        doubleFrameSize + frameSize + dialogWidth + xPos, frameSize + yPos,
        totalHeight - frameSize + yPos);

    window.draw(dialogFrame);

    // Text
    drawText(txtColor, xPos + doubleFrameSize + dialogWidth / 2.f,
        yPos - lineHeight * fontHeightDifferential + doubleFrameSize);
}

[[nodiscard]] static float calculateFMax(
    const float configWidth, const float configHeight)
{
    return std::max(1024.f / configWidth, 768.f / configHeight);
}

[[nodiscard]] static std::tuple<float, float, float> calculateFMaxAndWAndH(
    const float configWidth, const float configHeight, const float yPos)
{
    const float fmax = calculateFMax(configWidth, configHeight);
    const float w = configWidth * fmax;
    const float h = (configHeight * fmax) / 2.f + yPos;

    return {fmax, w, h};
}

void HexagonDialogBox::drawCenter(
    const sf::Color& txtColor, const sf::Color& backdropColor)
{
    const auto [fmax, w, h] =
        calculateFMaxAndWAndH(Config::getWidth(), Config::getHeight(), yPos);

    const float leftBorder = (w - dialogWidth) / 2.f + xPos,
                rightBorder = (w + dialogWidth) / 2.f + xPos,
                halfHeight = totalHeight / 2.f;

    Utils::FastVertexVectorQuads& dialogFrame = getDialogFrame();
    dialogFrame.clear();
    dialogFrame.reserve(8);

    // outer frame
    drawBox(dialogFrame, txtColor, leftBorder - doubleFrameSize,
        rightBorder + doubleFrameSize, h - halfHeight, h + halfHeight);

    // text backdrop
    drawBox(dialogFrame, backdropColor, leftBorder - frameSize,
        rightBorder + frameSize, h - halfHeight + frameSize,
        h + halfHeight - frameSize);

    window.draw(dialogFrame);

    // Text
    drawText(txtColor, w / 2.f,
        h - halfHeight - lineHeight * fontHeightDifferential + doubleFrameSize);
}

void HexagonDialogBox::drawCenterUpperHalf(
    const sf::Color& txtColor, const sf::Color& backdropColor)
{
    const auto [fmax, w, h] =
        calculateFMaxAndWAndH(Config::getWidth(), Config::getHeight(), yPos);

    const float leftBorder = (w - dialogWidth) / 2.f + xPos,
                rightBorder = (w + dialogWidth) / 2.f + xPos;

    Utils::FastVertexVectorQuads& dialogFrame = getDialogFrame();
    dialogFrame.clear();
    dialogFrame.reserve(8);

    // outer frame
    drawBox(dialogFrame, txtColor, leftBorder - doubleFrameSize,
        rightBorder + doubleFrameSize, h - totalHeight, h);

    // text backdrop
    drawBox(dialogFrame, backdropColor, leftBorder - frameSize,
        rightBorder + frameSize, h - totalHeight + frameSize, h - frameSize);

    window.draw(dialogFrame);

    // Text
    drawText(txtColor, w / 2.f,
        h - totalHeight - lineHeight * fontHeightDifferential +
            doubleFrameSize);
}

void HexagonDialogBox::clearDialogBox()
{
    getDialogFrame().clear();
    dialogText.clear();
    input.clear();
    inputBox = false;
    inputBoxPassword = false;
    keyToClose = KKey::Unknown;
}

[[nodiscard]] ssvs::KKey HexagonDialogBox::getKeyToClose() const noexcept
{
    return keyToClose;
}

[[nodiscard]] bool HexagonDialogBox::empty() const noexcept
{
    return dialogText.empty();
}

[[nodiscard]] bool HexagonDialogBox::isInputBox() const noexcept
{
    return inputBox;
}

[[nodiscard]] std::string& HexagonDialogBox::getInput() noexcept
{
    return input;
}

[[nodiscard]] const std::string& HexagonDialogBox::getInput() const noexcept
{
    return input;
}

void HexagonDialogBox::setInputBoxPassword(const bool x) noexcept
{
    inputBoxPassword = x;
}

[[nodiscard]] bool HexagonDialogBox::getInputBoxPassword() noexcept
{
    return inputBoxPassword;
}

} // namespace hg