-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundTextBox.cpp
199 lines (154 loc) · 5.57 KB
/
RoundTextBox.cpp
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
#include <MultiWidgets/TextBox.hpp>
#include <Poetic/GPUWrapperFont.hpp>
#include <Poetic/Utils.hpp>
#include <Luminous/GLContext.hpp>
#include <Luminous/Utils.hpp>
#include <Luminous/RenderContext.hpp>
#include <Luminous/Error.hpp>
#include <Radiant/FileUtils.hpp>
#include <MultiWidgets/Export.hpp>
#include <MultiWidgets/Widget.hpp>
#include <Poetic/FontManager.hpp>
#include <Valuable/ValueString.hpp>
#include <Valuable/ValueInt.hpp>
#include <Nimble/Matrix3.hpp>
#include <Nimble/Vector2.hpp>
#include <iostream>
#include "RoundTextBox.hpp"
using namespace Radiant;
using namespace Nimble;
RoundTextBox::RoundTextBox(MultiWidgets::Widget * parent, const char * text, uint32_t a) : TextBox(parent, text, a), m_type("RoundTextBox")
{
}
RoundTextBox::~RoundTextBox()
{
}
RoundTextBox * RoundTextBox::clone(){
RoundTextBox * tb2 = new RoundTextBox(parent(), 0, MultiWidgets::TextBox::HCENTER);
tb2->setCSSClass("FloatingWord");
tb2->setStyle(style());
tb2->setText(text());
tb2->setWidth(width());
tb2->setHeight(height());
tb2->setAlignFlags(MultiWidgets::TextBox::HCENTER | MultiWidgets::TextBox::VCENTER);
tb2->setDepth(depth());
tb2->setLocation(mapToParent(Nimble::Vector2(0,0)));
tb2->setScale(scale());
tb2->setRotation(rotation());
tb2->setType("clone");
tb2->setFaceSize(faceSize());
tb2->setPlayer(player());
return tb2;
}
/*
void RoundTextBox::cachedRender(Luminous::RenderContext & r)
{
std::cout << "RoundTextBox::cachedRender called" << std::endl;
Luminous::FramebufferResource & fbr = m_fbResource.ref(r.resources());
fbr.texture().bind();
// Render cached
glEnable(GL_TEXTURE_2D);
Luminous::Utils::glUsualBlend();
const float pw = m_cssPaddingWidth.asFloat();
const float bw = m_cssBorderWidth.asFloat();
r.pushTransformRightMul(Nimble::Matrix3::translate2D(Nimble::Vector2(-pw - bw, -pw - bw)));
// Get the current rendering transform, starting from the root.
Nimble::Matrix3 m = r.transform();
float scale = m.extractScale();
// The outer radius of the widget.
float outerRadius = width() * 0.5f;
// Calculate the center of the widget.
Nimble::Vector3 center = m * Nimble::Vector3(outerRadius, outerRadius, 1);
// The radius for rendering. The circle function uses the center
// of the line as the radius, while we use outer edge.
float radius = scale * (outerRadius);
// How many segments should we use for the circle.
int segments = Nimble::Math::Max(scale * outerRadius * 0.7f, 30.0f);
float color[] = {1.f, 1.f, 1.f, 1.f};
//r.drawTexRect(size() + 2.f * Nimble::Vector2(pw + bw, pw + bw), color);
r.drawCircle(center.data(), radius, color, segments);
r.popTransform();
glDisable(GL_TEXTURE_2D);
}
void RoundTextBox::renderContent(Luminous::RenderContext & r)
{
// No mutex() in SDK 1.2
Luminous::GLContext::Guard lock(r.glContext());
//Luminous::GLContext::Guard lock(r.glContext()->mutex());
// Get the basic box from standard widgets.
glDisable(GL_TEXTURE_2D);
//Widget::renderContent(r);
// Replaced with circle code:
const Nimble::Matrix3 & m = r.transform();
Radiant::Color c = color();
r.useCurrentBlendMode();
Luminous::GLSLProgramObject * shader = bindShader(r);
float scale = m.extractScale();
float outerRadius = width() * 0.5f;
Nimble::Vector2 center(width()*0.5f, height()*0.5f);
float radius = scale * (outerRadius);
int segments = Nimble::Math::Max(scale * outerRadius * 0.7f, 30.0f);
float color[] = {c.red(),c.green(),c.blue(),c.alpha()};
r.drawCircle(center, radius, color, segments);
if(shader) shader->unbind();
// Circle code ends
if(flags() & CACHED_RENDER) {
Luminous::FramebufferResource & fbr = m_fbResource.ref(r.resources());
// Update cache if needed
if((int) m_renderCacheGeneration != fbr.generation())
updateRenderCache(r);
// Render from cache
cachedRender(r);
} else {
renderText(r);
}
}
*/
void RoundTextBox::renderBorder(Luminous::RenderContext &r){
//float color[] = {0, 0, 0, 1};
//r.drawArc(0.5f * size(), width() * 0.5f, 0, Nimble::Math::PI * 2, 10, 2, color, 30);
const float pw = m_cssPaddingWidth.asFloat();
const float bw = m_cssBorderWidth.asFloat();
if(bw <= 0.01f || m_cssBorderColor.alpha() <= 0.00001f)
return;
// const Nimble::Matrix3 & m = r.transform();
// Rect outside(-pw - bw, -pw -bw, size().x + pw + bw, size().y + pw + bw);
// Rect inside(-pw, -pw, size().x + pw, size().y + pw);
glDisable(GL_TEXTURE_2D);
r.useCurrentBlendMode();
//Luminous::Utils::glRectWithHoleAA(outside, inside, m, m_cssBorderColor.asColor().data());
r.drawArc(0.5f * size(), width() * 0.5f, 0, Nimble::Math::PI * 2, bw, 1, m_cssBorderColor.asColor().data(), 30);
}
void RoundTextBox::renderContent(Luminous::RenderContext & r)
{
Radiant::Color old = color();
r.drawCircle(0.5f*size(), width()*0.5f, color().data());
setColor(1, 1, 1, 0);
TextBox::renderContent(r);
setColor(old);
}
bool RoundTextBox::isInside(Nimble::Vector2 v) const
{
/* Calculate the outer radius of the circle. */
float outerRadius = width() * 0.5f;
/* Calculate the center of circle.*/
Nimble::Vector2 center(outerRadius, outerRadius);
/* Compute how far the vector is from the cebter of the donut. */
float distance = (v - center).length();
/* If the distance exceeds the outer radius, then point v is not inside this widget. */
//std::cout << "distance : " << distance << " outerRadius: " << outerRadius << std::endl;
if(distance > outerRadius)
return false;
return true;
}
const char * RoundTextBox::type() const
{
return m_type.c_str();
}
int RoundTextBox::player() const
{
return m_playernumber;
}
void RoundTextBox::setPlayer(int i){
m_playernumber = i;
}