-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetlist.cpp
305 lines (257 loc) · 9.67 KB
/
widgetlist.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
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
#include <MultiWidgets/Widget.hpp>
#include <MultiWidgets/StayInsideParentOperator.hpp>
#include <MultiWidgets/RotateTowardsHandsOperator.hpp>
#include <MultiWidgets/LimitScaleOperator.hpp>
#include <MultiWidgets/TextBox.hpp>
#include <Nimble/Random.hpp>
#include <Luminous/Utils.hpp>
#include "widgetlist.hpp"
#include "RoundTextBox.hpp"
WidgetList::WidgetList(MultiWidgets::Widget * parent) : MultiWidgets::Widget(parent),
m_type("WidgetList")
{
setName("WidgetList");
setCSSType("WidgetList");
setInputFlags(inputFlags() & ~INPUT_PASS_TO_CHILDREN);
}
WidgetList * WidgetList::clone(){
WidgetList * list = WidgetList::createNiceList(parent());
list->setStyle(style());
list->setLocation(mapToParent(Nimble::Vector2(0, 0)));
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
RoundTextBox * tb = dynamic_cast<RoundTextBox*>(*it);
RoundTextBox * tb2 = new RoundTextBox(0, 0, MultiWidgets::TextBox::HCENTER);
tb2->setCSSClass("FloatingWord_clone");
tb2->setStyle(tb->style());
tb2->setText(tb->text());
tb2->setWidth(tb->width());
tb2->setHeight(tb->height());
tb2->setAlignFlags(MultiWidgets::TextBox::HCENTER | MultiWidgets::TextBox::VCENTER);
list->addItem(tb2);
}
//layout();
list->layout();
list->setDepth(depth());
list->setScale(scale());
list->setRotation(rotation());
return list;
}
WidgetList * WidgetList::createNiceList(Widget * parent, Widget * content) {
WidgetList * list = new WidgetList(parent);
if (content)
list->addItem(content);
list->setDepth(-1);
list->raiseFlag(WidgetList::LOCK_DEPTH);
list->addOperator(new MultiWidgets::StayInsideParentOperator);
//list->addOperator(new MultiWidgets::RotateTowardsHandsOperator);
list->addOperator(new MultiWidgets::LimitScaleOperator(MultiWidgets::LimitScaleOperator::COMPARE_SCALE,
1.0f, 2.0f));
return list;
}
void WidgetList::update(float dt)
{
layout();
if (m_itemList.empty())
raiseFlag(DELETE_ME);
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
(**it).update(dt);
}
MultiWidgets::Widget::update(dt);
}
void WidgetList::layout()
{
float offset = 0.0f;
float ymax = 5.0f;
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
(*it)->setLocation(offset, 0);
ymax = Nimble::Math::Max((*it)->height(), ymax);
offset += (*it)->width();
}
setSize(offset, ymax);
}
/*
void WidgetList::processFingers(MultiWidgets::GrabManager &gm, MultiWidgets::FingerArray &fingers, float dt)
{
if (!parent()) return;
// go through all the (MyList-)siblings that don't intersect with this already
// and see if processing input makes us collide
std::set<WidgetList*> plausible;
for (ChildIterator it = parent()->childBegin(); it != parent()->childEnd(); ++it) {
WidgetList * sibling = dynamic_cast<WidgetList*>(*it);
if (!sibling || sibling == this)
continue;
if (!sibling->intersects(*this) && sibling->lastInteraction().sinceSecondsD() < 2.0) {
//Radiant::info("%lf", sibling->lastInteraction().sinceSecondsD());
plausible.insert(sibling);
}
}
MultiWidgets::Widget::processFingers(gm, fingers, dt);
// check for new collision between last item <-> first item
for (std::set<WidgetList*>::iterator it = plausible.begin();
it != plausible.end(); ++it) {
if ((*it)->intersects(*this)) {
WidgetList * ml = *it;
assert(ml != 0);
if (m_itemList.empty() || ml->m_itemList.empty())
continue;
// if last item collides with first item of this, put other list in the beginning
// of this list.
// otherwise, if first item collides with last item of this, append other list to this
Nimble::Rectangle myFront = itemRect(m_itemList.front(), -1);
Nimble::Rectangle myBack = itemRect(m_itemList.back(), 1);
Nimble::Rectangle otherFront = ml->itemRect(ml->m_itemList.front(), -1);
Nimble::Rectangle otherBack = ml->itemRect(ml->m_itemList.back(), 1);
if (myBack.intersects(otherFront)) {
m_itemList.splice(m_itemList.end(), ml->m_itemList);
ml->layout();
layout();
} else if (myFront.intersects(otherBack)) {
float offset = ml->width();
setLocation(mapToParent(Nimble::Vector2(-offset, 0)));
m_itemList.splice(m_itemList.begin(), ml->m_itemList);
layout();
ml->layout();
}
}
}
handleSplits(gm, fingers, dt);
}
void WidgetList::handleSplits(MultiWidgets::GrabManager &gm, MultiWidgets::FingerArray &fingers, float dt) {
if (fingers.size() < 2)
return;
std::map<float, MultiTouch::Finger> fings;
// Go through all the fingers touching this widget by their x-coordinates
// (in local coordinates)
// If two adjacent fingers have moved the opposites ways and are located in
// adjacent widgets, split at that position
for (unsigned int i=0; i < fingers.size(); ++i) {
fings.insert(std::make_pair(gm.project(fingers[i].tipLocation()).x, fingers[i]));
}
std::map<float, MultiTouch::Finger>::iterator prev = fings.begin();
std::map<float, MultiTouch::Finger>::iterator it = prev;
++it;
for( ; it != fings.end(); ++it) {
MultiTouch::Finger & c1 = prev->second;
MultiTouch::Finger & c2 = it->second;
Nimble::Vector2 mot1 = gm.project(c1.tipLocation())-gm.project(c1.prevTipLocation());
Nimble::Vector2 mot2 = gm.project(c2.tipLocation())-gm.project(c2.prevTipLocation());
mot1 /= dt; mot2 /= dt;
// two fingers; left one moved left and right one moved right
// (mot1.x can be zero only because of testing with mouse emulation..)
if ((mot1.x < -250.0 && mot2.x > 250.0) || mot1.x < -500.0 || mot2.x > 500.0) {
ItemList::iterator i1 = findItem(gm.project(c1.tipLocation()));
ItemList::iterator i2 = findItem(gm.project(c2.tipLocation()));
if (i1 != m_itemList.end() && i2 != m_itemList.end()) {
ItemList::iterator i1_5 = i1;
++i1_5;
// check that widgets at finger positions are neighbours
if (i1 != i2 && i1_5 == i2) {
WidgetList * list = WidgetList::createNiceList(parent());
list->setStyle(*style());
list->setLocation(mapToParent((**i2).mapToParent(Nimble::Vector2(0, 0)) + Nimble::Vector2(30, 0)));
list->m_itemList.splice(list->m_itemList.begin(), m_itemList, i2, m_itemList.end());
layout();
list->layout();
list->setDepth(depth());
list->setScale(scale());
list->setRotation(rotation());
Nimble::Vector2 vel(1000, Nimble::RandomUniform::instance().randMinMax(-200, 200));
vel.rotate(rotation());
dropAllGrabs(gm);
setHasInteraction(false);
/// @todo this has no effect, do something to force
setVelocity(-vel);
list->setVelocity(vel);
list->touch();
}
}
}
}
}
*/
// return the bounding rectangle of widget in list in (common) parent coordinates
/// @param part -1 means left half, 0 full size, 1 right half
Nimble::Rectangle WidgetList::itemRect(const Widget * w, int part) {
Nimble::Matrix3 m = transform();
Nimble::Vector2 sz = w->size();
if (part != 0)
sz.x /= 2;
m *= w->transform();
m *= Nimble::Matrix3::translate2D(0.5f * sz);
if (part > 0)
m *= Nimble::Matrix3::translate2D(0.5f*w->width(), 0);
return Nimble::Rectangle(sz, m);
}
// find item from list which contains local coordinate pos
WidgetList::ItemList::iterator WidgetList::findItem(Nimble::Vector2 pos)
{
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
if ((**it).isInside((**it).transform().inverse().project(pos)))
return it;
}
return m_itemList.end();
}
void WidgetList::addItem(Widget * w) {
m_itemList.push_back(w);
}
MultiWidgets::Widget* WidgetList::getItem(size_t idx) {
ItemList::iterator it = m_itemList.begin();
std::advance(it, idx);
return *it;
}
size_t WidgetList::itemCount() const {
return m_itemList.size();
}
void WidgetList::renderContent(Luminous::RenderContext &r)
{
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
(**it).render(r);
}
}
/*
void WidgetList::render(Luminous::RenderContext & r)
{
int m_thickness(25);
float m_arc(0.1);
// Apply the current transformation matrix to the current
rendering matrix.
r.pushTransformRightMul(transform());
// 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 - m_thickness * 0.5f);
float radius2 = scale * (outerRadius - m_thickness * 4.0f);
float radius3 = scale * (outerRadius - m_thickness * 2.0f);
// How many segments should we use for the circle.
int segments = Nimble::Math::Max(scale * outerRadius * 0.7f, 30.0f);
// Enable the typical OpenGL blending mode.
Luminous::Utils::glUsualBlend();
// Draw two fixed circles
Luminous::Utils::
glFilledSoftCircle(center.data(), radius,
m_thickness * scale, 1.0f,
segments,
color().data());
// Calculate end points
float start = Nimble::Math::HALF_PI + m_rotation - (m_arc * Nimble::Math::PI);
float end = Nimble::Math::HALF_PI + m_rotation + (m_arc * Nimble::Math::PI);
r.popTransform();
}
*/
const char * WidgetList::type() const
{
return m_type.c_str();
}
void WidgetList::recursiveSetAlpha(float alpha){
for (ItemList::iterator it = m_itemList.begin(); it != m_itemList.end(); ++it ) {
(*it)->setColor(1.0f,1.0f,1.0f,alpha);
(*it)->setForegroundColor(0.0f,0.0f,0.0f,alpha);
(*it)->setBorderColor(0.0f,0.0f,0.0f,alpha);
}
}