-
Notifications
You must be signed in to change notification settings - Fork 2
/
imgui_textwrap.h
208 lines (155 loc) · 6.05 KB
/
imgui_textwrap.h
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
// Elmo Trolla, 2019
// License: pick one - public domain / UNLICENSE (https://www.unlicense.org) / MIT (https://opensource.org/licenses/MIT).
// NB! do not repalce these with "#pragma once"
#ifndef IMGUI_TEXTWRAP_H
#define IMGUI_TEXTWRAP_H
//
// Makes some simple uses of imgui text rendering api a bit simpler.
// (text positioning, foreground/background colors)
//
// this is a header-file library.
// include as usual, but in one file:
// #define IMGUI_TEXTWRAP_IMPLEMENTATION
// #include "imgui_textwrap.h"
//
#include "imgui.h"
#include "imgui_internal.h"
class ImguiTextwrap
{
public:
ImguiTextwrap();
virtual ~ImguiTextwrap() {};
void init(ImFont* font, ImDrawList* drawlist=NULL);
// return string size in pixels
ImVec2 size(const char* text);
void set_fgcolor(const ImVec4& color);
void set_bgcolor(const ImVec4& color);
// top
void drawtl(const char* text, float x, float y);
void drawtr(const char* text, float x, float y);
void drawtm(const char* text, float x, float y);
// bottom
void drawbl(const char* text, float x, float y);
void drawbr(const char* text, float x, float y);
void drawbm(const char* text, float x, float y);
// middle
void drawml(const char* text, float x, float y);
void drawmr(const char* text, float x, float y);
void drawmm(const char* text, float x, float y);
// baseline
void drawbll(const char* text, float x, float y);
void drawblr(const char* text, float x, float y);
void drawblm(const char* text, float x, float y);
// if fgcolor or bgcolor or z are None, previous values are used.
// assumes some kind of pixel-projection..
// you almost never want to use this method directly.
void draw(const char* text, float x, float y, int positioning);
// readonly variables
float height;
ImVec4 fgcolor;
ImVec4 bgcolor;
ImFont* font;
ImDrawList* drawlist;
private:
void m_fix_pos(float x, float y, float w, int positioning, float* x_fix, float* y_fix);
};
#endif // IMGUI_TEXTWRAP_H
#ifdef IMGUI_TEXTWRAP_IMPLEMENTATION
ImguiTextwrap::ImguiTextwrap()
{
this->font = NULL;
this->drawlist = NULL;
this->height = 0.;
this->fgcolor = ImVec4(1., 1., 1., 1.);
this->bgcolor = ImVec4(1., 1., 1., 0.);
}
// --------------------------------------------------------------------------
// ---- METHODS -------------------------------------------------------------
// --------------------------------------------------------------------------
void ImguiTextwrap::init(ImFont* font, ImDrawList* drawlist) {
IM_ASSERT(font);
this->font = font;
this->height = font->FontSize;
this->drawlist = drawlist;
}
// Calculate text size. Text can be multi-line.
ImVec2 ImguiTextwrap::size(const char* text)
{
IM_ASSERT(this->font);
ImVec2 text_size = this->font->CalcTextSizeA(this->font->FontSize, FLT_MAX, 0, text, NULL, NULL);
//if (text_size.x > 0.0f)
// text_size.x -= character_spacing_x;
return text_size;
}
void ImguiTextwrap::set_fgcolor(const ImVec4& color)
{
fgcolor = color;
}
void ImguiTextwrap::set_bgcolor(const ImVec4& color)
{
bgcolor = color;
}
// top
void ImguiTextwrap::drawtl(const char* text, float x, float y) { draw(text, x, y, 1+16); }
void ImguiTextwrap::drawtr(const char* text, float x, float y) { draw(text, x, y, 2+16); }
void ImguiTextwrap::drawtm(const char* text, float x, float y) { draw(text, x, y, 4+16); }
// bottom
void ImguiTextwrap::drawbl(const char* text, float x, float y) { draw(text, x, y, 1+32); }
void ImguiTextwrap::drawbr(const char* text, float x, float y) { draw(text, x, y, 2+32); }
void ImguiTextwrap::drawbm(const char* text, float x, float y) { draw(text, x, y, 4+32); }
// middle
void ImguiTextwrap::drawml(const char* text, float x, float y) { draw(text, x, y, 1+64); }
void ImguiTextwrap::drawmr(const char* text, float x, float y) { draw(text, x, y, 2+64); }
void ImguiTextwrap::drawmm(const char* text, float x, float y) { draw(text, x, y, 4+64); }
// baseline
void ImguiTextwrap::drawbll(const char* text, float x, float y) { draw(text, x, y, 1+8); }
void ImguiTextwrap::drawblr(const char* text, float x, float y) { draw(text, x, y, 3+8); }
void ImguiTextwrap::drawblm(const char* text, float x, float y) { draw(text, x, y, 4+8); }
void ImguiTextwrap::draw(const char* text, float x, float y, int positioning)
{
IM_ASSERT(this->font);
ImVec2 str_size = this->size(text);
// position the text according to given flags. top-left, bottom-right, ..
m_fix_pos(x, y, str_size.x, positioning, &x, &y);
if (str_size.x > 0) {
x = floor(x);
y = floor(y);
ImDrawList *dl = (this->drawlist != NULL) ? this->drawlist : ImGui::GetWindowDrawList();
if (dl) {
// draw the background
if (bgcolor.w != 0) {
dl->AddRectFilled(ImVec2(x, y), ImVec2(x + str_size.x + 1, y + this->font->FontSize),
ImColor(bgcolor));
}
// draw the text
dl->AddText(this->font, this->font->FontSize, ImVec2(x, y), ImColor(fgcolor), text, NULL, 0, NULL);
}
}
}
// --------------------------------------------------------------------------
// ---- PRIVATE -------------------------------------------------------------
// --------------------------------------------------------------------------
void ImguiTextwrap::m_fix_pos(float x, float y, float w, int positioning, float* x_fix, float* y_fix)
{
IM_ASSERT(this->font);
// positioning
// first 3 bits - left-right-middle
// next 4 bits - baseline-top-bottom-middle
if (positioning) {
if (positioning & 1) {} // left
else if (positioning & 2) x -= w; // right
else if (positioning & 4) x -= w / 2.f; // middle
if (positioning & 8) y -= this->font->Ascent; // baseline
else if (positioning & 16) {} // top
else if (positioning & 32) y -= this->height; // bottom
else if (positioning & 64) y -= this->height / 2.f; // middle
//else if (positioning & 64) y += this->height / 2.f - this->font->Descent; // middle
}
//*x_fix = floor(x + 0.5f); // round(x); // round missing in visual studio express 2008
//*y_fix = floor(y + 0.5f); // round(y);
//*x_fix = round(x);
//*y_fix = round(y);
*x_fix = x;
*y_fix = y;
}
#endif // IMGUI_TEXTWRAP_IMPLEMENTATION