nngn
Loading...
Searching...
No Matches
textbox.h
Go to the documentation of this file.
1#ifndef NNGN_TEXTBOX_H
2#define NNGN_TEXTBOX_H
3
4#include <chrono>
5#include <limits>
6
7#include "math/vec2.h"
8#include "utils/def.h"
9#include "utils/flags.h"
10
11#include "text.h"
12
13namespace nngn {
14
15class Fonts;
16struct Timing;
17
18class Textbox {
19public:
20 enum Flag : u8 {
21 UPDATED = 1u << 0,
22 SCREEN_UPDATED = 1u << 1,
23 MONOSPACED = 1u << 2,
24 };
25 struct Command {
26 enum : unsigned char {
27 MIN = static_cast<unsigned char>(
28 std::numeric_limits<signed char>::min()),
30 };
31 };
32 static constexpr auto DEFAULT_SPEED = std::chrono::milliseconds(50);
33 Text str = {}, title = {};
34 std::chrono::microseconds timer = {};
35 std::chrono::milliseconds speed = DEFAULT_SPEED;
36 vec2 title_bl = {0, 0}, title_tr = {0, 0};
37 vec2 str_bl = {0, 0}, str_tr = {0, 0};
38 static bool is_character(unsigned char c);
39 static bool is_character(char c);
40 static bool is_command(unsigned char c);
41 static bool is_command(char c);
42 void init(const Fonts *f) { this->fonts = f; }
43 bool empty(void) const;
44 std::size_t text_length(void) const;
45 bool monospaced(void) const { return this->flags.is_set(Flag::MONOSPACED); }
46 bool updated(void) const;
47 bool finished(void) const { return this->str.cur == this->str.str.size(); }
48 void set_monospaced(bool m);
49 void set_screen_updated(void) { this->flags.set(Flag::SCREEN_UPDATED); }
50 void set_speed(unsigned s) { this->speed = std::chrono::milliseconds(s); }
51 void set_title(const char *s);
52 void set_text(const char *s);
53 void set_cur(std::size_t cur);
54 bool update(const Timing &t);
55 void update_size(void);
56 void update_size(const uvec2 &screen);
57 void clear_updated(void);
58private:
60 const Fonts *fonts = nullptr;
61};
62
63inline bool Textbox::is_character(unsigned char c) {
64 return c < Command::MIN && c != '\n';
65}
66
67inline bool Textbox::is_character(char c) {
68 return Textbox::is_character(static_cast<unsigned char>(c));
69}
70
71inline bool Textbox::is_command(unsigned char c) {
72 return Command::MIN <= c && c < Command::MAX;
73}
74
75inline bool Textbox::is_command(char c) {
76 return Textbox::is_command(static_cast<unsigned char>(c));
77}
78
79inline bool Textbox::empty(void) const {
80 return this->str.str.empty() && this->title.str.empty();
81}
82
83inline void Textbox::set_monospaced(bool m) {
84 this->flags.set(Flag::MONOSPACED, m);
85 this->flags.set(Flag::UPDATED);
86 this->update_size();
87}
88
89inline bool Textbox::updated(void) const {
90 return this->flags.is_set(Flag::UPDATED | Flag::SCREEN_UPDATED);
91}
92
93inline void Textbox::clear_updated(void) {
94 this->flags.clear(Flag::UPDATED | Flag::SCREEN_UPDATED);
95}
96
97}
98
99#endif
Definition: font.h:30
Definition: textbox.h:18
void update_size(void)
Definition: textbox.cpp:60
Text title
Definition: textbox.h:33
const Fonts * fonts
Definition: textbox.h:60
bool updated(void) const
Definition: textbox.h:89
vec2 str_tr
Definition: textbox.h:37
bool empty(void) const
Definition: textbox.h:79
Flags< Flag > flags
Definition: textbox.h:59
bool monospaced(void) const
Definition: textbox.h:45
void set_title(const char *s)
Definition: textbox.cpp:26
bool finished(void) const
Definition: textbox.h:47
vec2 title_bl
Definition: textbox.h:36
static bool is_command(unsigned char c)
Definition: textbox.h:71
vec2 str_bl
Definition: textbox.h:37
Text str
Definition: textbox.h:33
void init(const Fonts *f)
Definition: textbox.h:42
std::size_t text_length(void) const
Definition: textbox.cpp:16
std::chrono::microseconds timer
Definition: textbox.h:34
static constexpr auto DEFAULT_SPEED
Definition: textbox.h:32
static bool is_character(unsigned char c)
Definition: textbox.h:63
Flag
Definition: textbox.h:20
@ MONOSPACED
Definition: textbox.h:23
@ UPDATED
Definition: textbox.h:21
@ SCREEN_UPDATED
Definition: textbox.h:22
void set_text(const char *s)
Definition: textbox.cpp:31
void set_speed(unsigned s)
Definition: textbox.h:50
std::chrono::milliseconds speed
Definition: textbox.h:35
void set_cur(std::size_t cur)
Definition: textbox.cpp:37
void set_monospaced(bool m)
Definition: textbox.h:83
void set_screen_updated(void)
Definition: textbox.h:49
vec2 title_tr
Definition: textbox.h:36
void clear_updated(void)
Definition: textbox.h:93
update
Definition: img_common.lua:42
c
Definition: gamma.lua:11
m
Definition: input.lua:23
function f()) end
std::chrono::seconds s
Definition: timing.cpp:6
Definition: audio.cpp:7
std::uint8_t u8
Definition: def.h:12
Wrapper for an unsigned integral representing flags.
Definition: flags.h:18
constexpr Flags & clear(AT a)
Definition: flags.h:30
constexpr bool is_set(AT a) const
Definition: flags.h:26
constexpr Flags & set(AT a)
Definition: flags.h:28
Definition: text.h:12
std::string str
Definition: text.h:13
size_t cur
Definition: text.h:14
Definition: textbox.h:25
@ TEXT_BLUE
Definition: textbox.h:29
@ MIN
Definition: textbox.h:27
@ TEXT_RED
Definition: textbox.h:29
@ TEXT_WHITE
Definition: textbox.h:29
@ MAX
Definition: textbox.h:29
@ TEXT_GREEN
Definition: textbox.h:29
Definition: timing.h:20