2020-01-01 21:29:24 +01:00
|
|
|
#include "Textbox.h"
|
2020-07-19 21:43:29 +02:00
|
|
|
|
2022-12-30 22:57:24 +01:00
|
|
|
#include <SDL.h>
|
2020-01-31 19:25:37 +01:00
|
|
|
#include <utf8/unchecked.h>
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2023-01-12 05:27:52 +01:00
|
|
|
#include "Font.h"
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
textboxclass::textboxclass(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
w = 0;
|
|
|
|
h = 0;
|
|
|
|
tl = 0;
|
2020-04-29 06:49:15 +02:00
|
|
|
prev_tl = 0;
|
2020-01-01 21:29:24 +01:00
|
|
|
tm = 0;
|
|
|
|
timer = 0;
|
2020-07-06 22:04:34 +02:00
|
|
|
|
|
|
|
xp = 0;
|
|
|
|
yp = 0;
|
|
|
|
r = 0;
|
|
|
|
g = 0;
|
|
|
|
b = 0;
|
2021-03-20 03:51:36 +01:00
|
|
|
|
|
|
|
flipme = false;
|
2021-03-23 22:29:32 +01:00
|
|
|
|
|
|
|
rand = 0;
|
2023-01-13 05:11:39 +01:00
|
|
|
|
|
|
|
print_flags = PR_FONT_LEVEL;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::centerx(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
resize();
|
|
|
|
xp = 160 - (w / 2);
|
|
|
|
resize();
|
|
|
|
}
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::centery(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
resize();
|
|
|
|
yp = 120 - (h / 2);
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::adjust(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
resize();
|
|
|
|
if (xp < 10) xp = 10;
|
|
|
|
if (yp < 10) yp = 10;
|
|
|
|
if (xp + w > 310) xp = 310 - w;
|
|
|
|
if (yp + h > 230) yp = 230 - h;
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void textboxclass::initcol(int rr, int gg, int bb)
|
|
|
|
{
|
|
|
|
r = rr;
|
|
|
|
g = gg;
|
|
|
|
b = bb;
|
Textboxes: Don't use separate RGB variables
Text boxes have `r`, `g`, and `b`, and `tr`, `tg`, and `tb`. `tr`, `tg`,
and `tb` are the real colors of the text box, and `r`, `g`, and `b` are
merely the colors of the text box as the text box's alpha value is
applied to them.
Compare this with, say, activity zones (which are drawn like text boxes
but aren't text boxes): There is `activity_r`, `activity_g`, and
`activity_b`, and when they're drawn they're all multiplied by
`act_alpha`.
So just do the same thing here. Ditch the `tr`, `tg`, and `tb`
variables, and make `r`, `g`, and `b` the new `tr`, `tg`, and `tb`
variables. That way, there's simply less state to have to update
separately. So we can get rid of `textboxclass::setcol()` as well.
2021-09-06 09:48:42 +02:00
|
|
|
tl = 0.5;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::update(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2020-04-29 06:49:15 +02:00
|
|
|
prev_tl = tl;
|
2020-01-01 21:29:24 +01:00
|
|
|
if (tm == 0)
|
|
|
|
{
|
|
|
|
tl += .1f;
|
|
|
|
if (tl >= 1)
|
|
|
|
{
|
|
|
|
tl = 1;
|
|
|
|
tm = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tm == 2)
|
|
|
|
{
|
|
|
|
tl -= .1f;
|
|
|
|
if (tl <= 0.5)
|
|
|
|
{
|
|
|
|
tl = 0.5;
|
2020-04-29 03:38:43 +02:00
|
|
|
//this textbox will be removed by updatetextboxes() later
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer > 0)
|
|
|
|
{
|
|
|
|
timer--;
|
|
|
|
if (timer == 0) tm = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::remove(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
tm = 2;
|
|
|
|
tl = 1.0f; //Remove mode
|
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::removefast(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
tm = 2;
|
|
|
|
tl = 0.4f; //Remove mode
|
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void textboxclass::resize(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
//Set the width and height to the correct sizes
|
2021-09-06 05:46:46 +02:00
|
|
|
int max = 0;
|
2021-09-13 06:02:15 +02:00
|
|
|
for (size_t iter = 0; iter < lines.size(); iter++)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2023-01-13 05:11:39 +01:00
|
|
|
int len = font::len(print_flags, lines[iter]);
|
2023-01-15 01:31:02 +01:00
|
|
|
if (len > max) max = len;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
2022-12-30 22:57:24 +01:00
|
|
|
// 16 for the borders
|
2023-01-12 05:27:52 +01:00
|
|
|
w = max + 16;
|
2023-01-13 05:11:39 +01:00
|
|
|
h = lines.size()*font::height(print_flags) + 16;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 00:41:49 +02:00
|
|
|
void textboxclass::addline(const std::string& t)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-13 06:02:15 +02:00
|
|
|
lines.push_back(t);
|
2020-01-01 21:29:24 +01:00
|
|
|
resize();
|
2021-09-13 06:02:15 +02:00
|
|
|
if ((int) lines.size() >= 12) lines.clear();
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
2022-12-30 22:57:24 +01:00
|
|
|
|
|
|
|
void textboxclass::pad(size_t left_pad, size_t right_pad)
|
|
|
|
{
|
|
|
|
// Pad the current text with a certain number of spaces on the left and right
|
|
|
|
for (size_t iter = 0; iter < lines.size(); iter++)
|
|
|
|
{
|
|
|
|
lines[iter] = std::string(left_pad, ' ') + lines[iter] + std::string(right_pad, ' ');
|
|
|
|
}
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void textboxclass::padtowidth(size_t new_w)
|
|
|
|
{
|
|
|
|
/* Pad the current text so that each line is new_w pixels wide.
|
|
|
|
* Each existing line is centered in that width. */
|
|
|
|
resize();
|
|
|
|
size_t chars_w = SDL_max(w-16, new_w) / 8;
|
|
|
|
for (size_t iter = 0; iter < lines.size(); iter++)
|
|
|
|
{
|
|
|
|
size_t n_glyphs = utf8::unchecked::distance(lines[iter].begin(), lines[iter].end());
|
|
|
|
signed int padding_needed = chars_w - n_glyphs;
|
|
|
|
if (padding_needed < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t left_pad = padding_needed / 2;
|
|
|
|
size_t right_pad = padding_needed - left_pad;
|
|
|
|
|
|
|
|
lines[iter] = std::string(left_pad, ' ') + lines[iter] + std::string(right_pad, ' ');
|
|
|
|
}
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void textboxclass::centertext()
|
|
|
|
{
|
|
|
|
padtowidth(w-16);
|
|
|
|
}
|