-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
762 lines (678 loc) · 25.7 KB
/
Copy pathMain.cpp
File metadata and controls
762 lines (678 loc) · 25.7 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include <MultiWidgets/ImageWidget.hpp>
#include <MultiWidgets/StayInsideParentOperator.hpp>
#include <MultiWidgets/LimitScaleOperator.hpp>
#include <MultiWidgets/Plugins.hpp>
#include <MultiWidgets/TextBox.hpp>
#include <Radiant/Directory.hpp>
#include <Radiant/FileUtils.hpp>
#include <Radiant/TimeStamp.hpp>
#include <Nimble/Random.hpp>
#include <Nimble/Math.hpp>
#define USE_THREADED
#ifdef USE_THREADED
#include <ThreadedRendering/SimpleThreadedApplication.hpp>
typedef ThreadedRendering::SimpleThreadedApplication GfxApp;
#else
#include <MultiWidgets/SimpleSDLApplication.hpp>
typedef MultiWidgets::SimpleSDLApplication GfxApp;
#endif
#define VACUUM_MODE 0
#define DISTORT_MODE 1
#define START_RIGHT 0
#define START_LEFT 1
#include "ReachingWidget.hpp"
#include "DistortWidget.hpp"
#include "VacuumReachingWidget.hpp"
#include "widgetlist.hpp"
#include "VacuumWidget.hpp"
#include "wordreader.hpp"
#include "RoundTextBox.hpp"
#include "logger.hpp"
#include <vector>
namespace {
bool decomposeTransformation(Nimble::Matrix3& transform,
Nimble::Vector2& translation,
float& rotation,
Nimble::Vector2& scale)
{
scale.x = sqrt(transform[0][0]*transform[0][0] + transform[1][0]*transform[1][0]);
scale.y = sqrt(transform[1][0]*transform[0][0] + transform[0][1]*transform[0][1]);
rotation = atan2(-transform[0][1], transform[1][1]);
translation.x = transform[0][2];
translation.y = transform[1][2];
return true;
}
void moveWidgetPreservingTransformation(MultiWidgets::Widget* w, MultiWidgets::Widget* to) {
Nimble::Matrix3 m = to->sceneTransform().inverse() * w->sceneTransform();
Nimble::Vector2 location, scale;
float rotation;
decomposeTransformation(m, location, rotation, scale);
to->addChild(w);
w->setLocation(location);
w->setRotation(rotation);
// assuming uniform scaling
w->setScale(scale.x);
}
}
template <class T>
void callWidgetTree(MultiWidgets::Widget * parent, T fn)
{
fn(parent);
MultiWidgets::Widget::ChildIterator it = parent->childBegin();
MultiWidgets::Widget::ChildIterator end = parent->childEnd();
while (it != end) {
callWidgetTree(*it++, fn);
}
}
struct Distractor {
int width;
int x;
int y;
};
class MyApplication : public GfxApp
{
typedef void (*WidgetCallBack)(MultiWidgets::Widget*);
typedef void (*CallBack)(void);
typedef GfxApp Parent;
public:
static MyApplication * me;
MyApplication() : Parent(),
reachingMode(VACUUM_MODE),
startSide(START_RIGHT)
{
assert(!me);
me = this;
}
void update()
{
std::list<CallBack>::iterator it;
// std::for_each(m_preUpdate.begin(), m_preUpdate.end(), boost::apply<void>());
for (it = m_preUpdate.begin(); it != m_preUpdate.end(); ++it) {
(*it)();
}
Parent::update();
// std::for_each(m_postUpdate.begin(), m_postUpdate.end(), boost::apply<void>());
for (it = m_postUpdate.begin(); it != m_postUpdate.end(); ++it) {
(*it)();
}
}
virtual bool keyPressEvent (int ascii, int special, int modifiers, bool foo) {
//Radiant::info("Keypress: %d (%c)", ascii, ascii);
if (ascii == 's') {
int width = size().x;
int height = size().y;
Luminous::Image img;
img.allocate(width, height, Luminous::PixelFormat(Luminous::PixelFormat::LAYOUT_RGB, Luminous::PixelFormat::TYPE_UBYTE));
unsigned char *pb = img.bytes();
assert(pb);
glFlush();
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pb);
img.flipVertical();
char fname[64];
time_t t;
struct tm *time_now;
t = time(NULL);
time_now = localtime(&t);
strftime(fname, 64, "screenshot-%Y%m%d-%H%M%S.tga", time_now);
img.write(fname);
return true;
}
else {
return GfxApp::keyPressEvent(ascii, special, modifiers, foo);
}
}
ReachingWidget * overlay() {
return m_overlay;
}
ReachingWidget * m_overlay;
std::list<CallBack> m_preUpdate;
std::list<CallBack> m_postUpdate;
int reachingMode;
int startSide;
};
MyApplication * MyApplication::me = 0;
void backgroundify(MultiWidgets::Widget * w)
{
ReachingWidget * d = MyApplication::me->overlay();
float IDLE_TIMEOUT = d->getValue("idle-timeout")->asFloat();
float IDLE_OK = d->getValue("idle-delay")->asFloat();
float BG_ALPHA = d->getValue("background-alpha")->asFloat();
if (w != MyApplication::me->overlay() && w->parent() == MyApplication::me->root()) {
float idle = w->lastInteraction().sinceSecondsD();
if (idle > IDLE_TIMEOUT && !w->inputTransparent()) {
w->setColor(1, 1, 1, 1);
MyApplication::me->overlay()->addChild(w);
} else {
float t = 0.0f;
if (idle > IDLE_OK)
t = (idle-IDLE_OK) / (IDLE_TIMEOUT-IDLE_OK);
float a = Nimble::Math::Clamp(t*BG_ALPHA + (1-t)*1, 0.0f, 1.0f);
w->setColor(1, 1, 1, a);
}
}
}
void bg()
{
callWidgetTree(MyApplication::me->root(), backgroundify);
}
class CollectedWordsWidget : public MultiWidgets::Widget{
public:
CollectedWordsWidget(MultiWidgets::Widget * p=0): MultiWidgets::Widget(p), m_yloc(0), m_xloc(30)
{
setName("CollectedWordsWidget");
setCSSType("CollectedWordsWidget");
}
void addWord(const std::string & word)
{
MultiWidgets::TextBox * tb = new MultiWidgets::TextBox(this, word.c_str(), MultiWidgets::TextBox::HCENTER | MultiWidgets::TextBox::VCENTER);
tb->setCSSType("CollectedWord");
tb->setStyle(style());
tb->setInputTransparent(true);
//tb->setRotation(-1 * Nimble::Math::HALF_PI);
tb->setHeight(50);
tb->setFaceSize(36);
tb->setWidth(tb->totalTextAdvance() + 10);
if((m_yloc + tb->totalTextAdvance() + 10) > width()){
m_yloc = 0;
m_xloc += 40;
}
tb->setLocation(m_yloc, m_xloc);
m_yloc += tb->width();
}
void clear(){
deleteChildren();
m_yloc = 0;
m_xloc = 0;
}
int m_yloc;
int m_xloc;
};
class WordGameWidget : public MultiWidgets::Widget {
typedef MultiWidgets::Widget Parent;
typedef std::vector<std::string> Line;
public:
WordGameWidget(int players, MultiWidgets::Widget * p=0, double physicalWidth = 0, std::string filename = "targetwords", float density = 0.0) :
Parent(p),
m_currentsentence(1),
m_currentword(0),
m_players(players),
m_playersPassed(0),
m_density(density),
m_physicalWidth(physicalWidth),
wordReader(players, filename),
logger(MyApplication::me->reachingMode)
{
setName("WordGame");
setCSSType("WordGame");
setAutoBringToTop(false);
// Name events this class will handle
// Needed in API 1.2
eventAddIn("clear-widget");
eventAddIn("start-button-pressed-0");
eventAddIn("start-button-pressed-1");
eventAddIn("word-acquired-0");
eventAddIn("word-acquired-1");
eventAddOut("clear-widget");
m_collectedWords.resize(players);
m_startButtons.resize(players);
m_scoreWidgets.resize(players);
for(int i = 0; i < players; i++){
m_playerReadyToStart[i] = false;
m_playerScore[i] = 0;
}
Nimble::Vector2 sz = MyApplication::me->size();
int preH = 200;
int preW = 800;
Nimble::Vector2 previewLocs[] = {
Nimble::Vector2(0, sz.y - preH),
Nimble::Vector2(sz.x - preW, sz.y - preH)
};
if(players >= 2){
for (int i=0; i < players; ++i) {
m_collectedWords[i] = new CollectedWordsWidget(this);
m_collectedWords[i]->setWidth(preW);
m_collectedWords[i]->setHeight(preH);
//m_collectedWords[i]->setRotationAboutCenter(-Nimble::Math::PI/2 + i*Nimble::Math::PI);
m_collectedWords[i]->setLocation(previewLocs[i]);
m_collectedWords[i]->setInputTransparent(true);
m_collectedWords[i]->addWord("Press start");
std::string buttonName = std::string("P") + Radiant::StringUtils::stringify(i+1) + std::string(" Start");
std::string buttonEvent = std::string("start-button-pressed-") + Radiant::StringUtils::stringify(i);
m_startButtons[i] = new RoundTextBox(this, buttonName.c_str(), MultiWidgets::TextBox::VCENTER | MultiWidgets::TextBox::HCENTER);
m_startButtons[i]->eventAddListener("interactionbegin", buttonEvent.c_str(), this);
m_startButtons[i]->setCSSType("StartButton");
m_startButtons[i]->setStyle(style());
/* Set width to 20 mm */
int startWidth = Nimble::Math::Round(sz.x / m_physicalWidth * 30);
m_startButtons[i]->setWidth(startWidth);
m_startButtons[i]->setHeight(startWidth);
m_startButtons[i]->setLocation(sz.x/2 - startWidth - 50 + (100 + startWidth) * i, sz.y/2 - startWidth/2);
m_startButtons[i]->setInputFlags(MultiWidgets::Widget::INPUT_USE_TAPS);
m_startButtons[i]->setDepth(0);
m_startButtons[i]->setVisible(false);
m_scoreWidgets[i] = new MultiWidgets::TextBox(this, 0, MultiWidgets::TextBox::HCENTER);
m_scoreWidgets[i]->setCSSType("ScoreWidget");
m_scoreWidgets[i]->setStyle(style());
m_scoreWidgets[i]->setText(std::string("0"));
int scoreWidth = 200;
m_scoreWidgets[i]->setWidth(scoreWidth);
m_scoreWidgets[i]->setInputTransparent(true);
m_scoreWidgets[i]->setColor(0, 0, 0, 0);
m_scoreWidgets[i]->setLocation(sz.x/2 - scoreWidth -50 + (100 + scoreWidth) * i, 100);
}
}
else{
preW = 1100;
preH = 200;
m_collectedWords[0] = new CollectedWordsWidget(this);
m_collectedWords[0]->setWidth(preW);
m_collectedWords[0]->setHeight(preH);
m_collectedWords[0]->setLocation(Nimble::Vector2(sz.x/2 - preW/2, sz.y - preH));
m_collectedWords[0]->setInputTransparent(true);
m_collectedWords[0]->addWord("Press start");
std::string buttonName = std::string("Start");
std::string buttonEvent = std::string("start-button-pressed-0");
m_startButtons[0] = new RoundTextBox(this, buttonName.c_str(), MultiWidgets::TextBox::VCENTER | MultiWidgets::TextBox::HCENTER);
m_startButtons[0]->eventAddListener("interactionbegin", buttonEvent.c_str(), this);
m_startButtons[0]->setCSSType("StartButton");
m_startButtons[0]->setStyle(style());
/* Set width to 30 mm */
int startWidth = Nimble::Math::Round(sz.x / m_physicalWidth * 30);
m_startButtons[0]->setWidth(startWidth);
m_startButtons[0]->setHeight(startWidth);
if (MyApplication::me->startSide == START_RIGHT){
m_startButtons[0]->setLocation(sz.x - startWidth - 10, sz.y/2 - startWidth/2);
}
else{
m_startButtons[0]->setLocation(100, sz.y/2 - startWidth/2);
}
m_startButtons[0]->setInputFlags(MultiWidgets::Widget::INPUT_USE_TAPS);
m_startButtons[0]->setDepth(0);
m_startButtons[0]->setVisible(false);
m_scoreWidgets[0] = new MultiWidgets::TextBox(this, 0);
m_scoreWidgets[0]->setCSSType("ScoreWidget");
m_scoreWidgets[0]->setStyle(style());
m_scoreWidgets[0]->setText(std::string("0"));
int scoreWidth = 200;
m_scoreWidgets[0]->setWidth(scoreWidth);
m_scoreWidgets[0]->setInputTransparent(true);
m_scoreWidgets[0]->setColor(0, 0, 0, 0);
m_scoreWidgets[0]->setLocation(sz.x - 100, 100);
}
setInputFlags(MultiWidgets::Widget::INPUT_PASS_TO_CHILDREN);
eventAddListener("clear-widget", "clear-widget", this);
}
virtual void update(float dt) {
Parent::update(dt);
}
static bool is_special(char c) {
return !std::isalnum(c);
}
static bool cmp_string_ascii_only(std::string s1, std::string s2)
{
std::string::iterator i1 = std::remove_if(s1.begin(), s1.end(), is_special);
std::string::iterator i2 = std::remove_if(s2.begin(), s2.end(), is_special);
s1.erase(i1, s1.end());
s2.erase(i2, s2.end());
// lowercase compare
std::transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
return s1 == s2;
}
void initializeLevel(){
MyApplication& app = *MyApplication::me;
m_currentWordWidgets.clear();
//app.overlay()->resetVectorField();
app.overlay()->isReachingActive(false);
app.overlay()->resetAndClear();
++m_currentword;
if(m_currentword > wordReader.sentenceLength(m_currentsentence)){
++m_currentsentence;
if(m_currentsentence > wordReader.maxSentence()){
// End
logger.endGame(m_playerScore, m_players);
int winningPlayer = 0;
int winningScore = m_playerScore[0];
for(int i = 1; i < m_players; i++){
if(m_playerScore[i] > winningScore){
winningPlayer = i;
winningScore = m_playerScore[i];
}
}
std::string btnText;
if(m_players == 1){
btnText = std::string("Thank you!");
}
else{
btnText = std::string("Player ") + Radiant::StringUtils::stringify(winningPlayer+1) + std::string(" wins!");
}
MultiWidgets::TextBox * winnerLabel = new MultiWidgets::TextBox(this, btnText.c_str(), MultiWidgets::TextBox::VCENTER | MultiWidgets::TextBox::HCENTER);
winnerLabel->setCSSType("WinnerLabel");
winnerLabel->setStyle(style());
winnerLabel->setWidth(winnerLabel->totalTextAdvance() + 300);
winnerLabel->setLocation(app.size().maximum() * 0.5f - winnerLabel->width()/2, app.size().minimum() * 0.5f - winnerLabel->height()/2);
winnerLabel->setInputTransparent(true);
return;
}
// Clear collected words from "answerboards"
/*for(int i = 0; i < m_players; ++i){
m_collectedWords[i]->clear();
}*/
m_currentword = 1;
}
for(int i = 0; i < m_players; ++i){
m_playerReadyToStart[i] = false;
m_startButtons[i]->setVisible(true);
}
m_playersPassed = 0;
}
virtual void processMessage(const char* msg, Radiant::BinaryData& bd)
{
std::string s(msg);
if (Radiant::StringUtils::beginsWith(s, "start-button-pressed")) {
int player = s[s.size()-1] - '0';
if(m_currentword == 1){
// Clear answer board
m_collectedWords[player]->clear();
}
m_playerReadyToStart[player] = true;
for(int i = 0; i < m_players; ++i){
if(!m_playerReadyToStart[i]){
return;
}
}
for (int i=0; i < m_players; ++i) {
m_startButtons[i]->setVisible(false);
}
gotoLevel(m_currentsentence, m_currentword);
}
else if(Radiant::StringUtils::beginsWith(s, "word-acquired")){
int player = s[s.size()-1] - '0';
TargetWord currentWord = wordReader.getWord(player, m_currentsentence, m_currentword);
logger.logAcquire(player, m_currentsentence, m_currentword, currentWord.word, currentWord.width, currentWord.distance);
//std::cout << "word acquired by player " << player << std::endl;
++m_playersPassed;
if(m_playersPassed == 1){
++m_playerScore[player];
m_scoreWidgets[player]->setText(Radiant::StringUtils::stringify(m_playerScore[player]));
//m_scoreWidgets[player]->setText(Radiant::StringUtils::stringify(130));
}
Radiant::BinaryData data;
data.writeInt32(player);
eventSend("clear-widget", data);
}
else if(Radiant::StringUtils::beginsWith(s, "clear-widget")){
logger.endRound();
int player = bd.readInt32();
m_collectedWords[player]->addWord((wordReader.getWord(player, m_currentsentence, m_currentword)).word);
m_currentWordWidgets[player]->raiseFlag(RoundTextBox::DELETE_ME);
if(m_playersPassed == m_players){
// Delete possible other widgets from the overlay
MyApplication& app = *MyApplication::me;
app.overlay()->resetAndClear();
// Delete widgets from root
MultiWidgets::Widget::ChildIterator it;
for (it = app.root()->childBegin(); it != app.root()->childEnd(); ++it) {
if (dynamic_cast<RoundTextBox*>(*it))
(*it)->raiseFlag(RoundTextBox::DELETE_ME);
}
// Level clear, go to next
initializeLevel();
}
}
else {
Parent::processMessage(msg, bd);
}
}
std::vector<Distractor> generateDistractorLayout(float R, double A, double W){
MyApplication & app = * MyApplication::me;
double MAX_A = app.size().maximum();
double MIN_A = 200;
std::vector<Distractor> vec;
using namespace Nimble::Math;
double SHRINK_T = Sin(PI/3) * PI/4;
double r_t = Sqrt(R/SHRINK_T);
double r_r = R/r_t;
double alpha = ASin((double)W/A/2);
double k = Sqrt(PI/(r_r/Tan(alpha) + alpha - PI/2) + 1);
int i_max = int( floor( log(MAX_A/A) / log(k) ));
int i_min = int( ceil ( log(MIN_A/A) / log(k) ));
double id = Log2((A/W)+1);
int n = int(ceil(r_t*PI/alpha/4));
for(int i = i_min; i <= i_max; i++){
double ki0 = Pow(k, i);
double ki1 = Pow(k, i+0.5);
double a0 = ki0 * A;
//double w0 = ki0 * W;
double a1 = ki1 * A;
//double w1 = ki1 * W;
for(int j = -n; j <= n; j++){
double angle = 2 * alpha / r_t * j;
if(j % 2 == 0){
//Distractor dw = {w0/2, a0 * Cos(angle), a0 * Sin(angle)};
Distractor dw = {a0/(Pow(2,id)-1), a0 * Cos(angle), a0 * Sin(angle)};
vec.push_back(dw);
}
else{
Distractor dw = {a1/(Pow(2,id)-1), a1 * Cos(angle), a1 * Sin(angle)};
vec.push_back(dw);
}
}
}
return vec;
}
void gotoLevel(unsigned int sentenceid, unsigned int wordid) {
Nimble::RandomUniform rnd(55);
m_playersPassed = 0;
// delete everything, create again
MyApplication& app = *MyApplication::me;
// Reset forces
app.overlay()->resetAndClear();
if (sentenceid > wordReader.maxSentence())
return;
// Create words
for (int i=0; i < m_players; ++i) {
TargetWord word = wordReader.getWord(i, sentenceid, wordid);
// Word width and distance is in millimeters, have to calculate pixel width and location
int pixelWidth = Nimble::Math::Round((double)app.size().maximum() / m_physicalWidth * word.width);
int x = Nimble::Math::Round((double)app.size().maximum() / m_physicalWidth * word.distance);
int pixelDistance = x;
if(i == 0 && (m_players >= 2 || app.startSide == START_RIGHT)){
// To left side, calculate from start button left edge to word widget right edge
int edge = m_startButtons[i]->location().x;
x = edge - x - pixelWidth;
if(x < 0){
x = 0;
std::cout << "WARNING: Requested distance " << word.distance << " mm does not fit on current screen (width " << m_physicalWidth <<" mm)." << std::endl;
}
}
else{
// To right side, calculate from start button right edge to word widget left edge
int edge = m_startButtons[i]->location().x + m_startButtons[i]->size().x;
x = edge + x;
if((x + pixelWidth) > app.size().maximum()){
x = app.size().maximum() - pixelWidth;
std::cout << "WARNING: Requested distance " << word.distance << " mm does not fit on current screen width (" << m_physicalWidth <<" mm)." << std::endl;
}
}
if(m_players == 2 || m_density == 0.0){
RoundTextBox * tb = new RoundTextBox(app.overlay(), 0, MultiWidgets::TextBox::HCENTER);
tb->setCSSClass("FloatingWord");
tb->setStyle(app.style());
tb->setText(word.word);
tb->setWidth(pixelWidth);
tb->setHeight(pixelWidth);
tb->setAlignFlags(MultiWidgets::TextBox::HCENTER | MultiWidgets::TextBox::VCENTER);
tb->setLocation(Nimble::Vector2(x, app.size().minimum()/2 - pixelWidth/2));
tb->raiseFlag(RoundTextBox::LOCK_DEPTH);
tb->addOperator(new MultiWidgets::StayInsideParentOperator());
std::string eventname = std::string("word-acquired-") + Radiant::StringUtils::stringify(i);
tb->eventAddListener("interactionbegin", eventname.c_str(), this);
while(tb->lineCount() > 1){
int faceSize = tb->faceSize()-1;
tb->setFaceSize(faceSize);
if(faceSize == 6) break;
}
tb->setType("RoundTextBox");
tb->setPlayer(i);
// Add widget to vector to find it later
m_currentWordWidgets.push_back(tb);
}
else if(m_players == 1 && m_density > 0.0){
// Generate distractors
std::vector<Distractor> distractors = generateDistractorLayout(m_density, pixelDistance, pixelWidth);
std::vector<Distractor>::iterator it;
bool targetWordInserted = false;
for(it = distractors.begin(); it < distractors.end(); it++){
//std::cout << "Distractor w: " << (*it).width << " x: " << (*it).x << " y: " << (*it).y << std::endl;
RoundTextBox * dist = new RoundTextBox(app.overlay(), 0, MultiWidgets::TextBox::HCENTER);
dist->setWidth((*it).width);
dist->setHeight((*it).width);
int distx;
if(app.startSide == START_RIGHT){
distx = m_startButtons[i]->location().x - (*it).x - (*it).width/2;
}
else{
distx = m_startButtons[i]->location().x + (*it).x - (*it).width/2;
}
int disty = app.size().y/2 - (*it).y - (*it).width/2;
dist->setLocation(distx, disty);
dist->setAlignFlags(MultiWidgets::TextBox::HCENTER | MultiWidgets::TextBox::VCENTER);
dist->setType("RoundTextBox");
if(!targetWordInserted && (*it).y == 0 && ((app.startSide == START_RIGHT && distx <= app.size().x/5)
|| (app.startSide == START_LEFT && distx >= (app.size().x*(4.0f/5.0f))))){
// Insert target word into leftmost fifth
dist->setText(word.word);
dist->setCSSClass("FloatingWord_target");
dist->setStyle(app.style());
dist->addOperator(new MultiWidgets::StayInsideParentOperator());
std::string eventname = std::string("word-acquired-") + Radiant::StringUtils::stringify(i);
dist->eventAddListener("interactionbegin", eventname.c_str(), this);
while(dist->lineCount() > 1){
int faceSize = dist->faceSize()-1;
dist->setFaceSize(faceSize);
if(faceSize == 6) break;
}
dist->setPlayer(i);
m_currentWordWidgets.push_back(dist);
targetWordInserted = true;
}
else{
dist->setCSSClass("FloatingWord");
dist->setStyle(app.style());
dist->setPlayer(-1);
}
}
}
}
app.overlay()->isReachingActive(true);
logger.startRound();
}
std::vector<std::vector<Line> > m_lines;
std::vector<std::vector<std::string> > m_sentences;
std::vector<std::vector<std::string> > m_wordLists;
unsigned int m_currentsentence;
unsigned int m_currentword;
std::vector<CollectedWordsWidget*> m_collectedWords;
std::vector<RoundTextBox*> m_startButtons;
bool m_playerReadyToStart[2];
int m_playerScore[2];
//bool m_playerFinished[2];
std::vector<RoundTextBox*> m_currentWordWidgets;
std::vector<MultiWidgets::TextBox*> m_scoreWidgets;
Valuable::ValueInt m_duplicateCount;
int m_players;
int m_playersPassed;
float m_density;
double m_physicalWidth;
WordReader wordReader;
Logger logger;
};
int main(int argc, char ** argv)
{
#ifndef USE_THREADED
SDL_Init(SDL_INIT_VIDEO);
#endif
MyApplication app;
if(!app.simpleInit(argc, argv))
return 1;
const char * dirpath = "Images";
uint32_t featureFlags = 0;
int players = 1;
int levelCount = 2;
// Default is the width of Multitouch Cell Advanced in millimeters
double displayWidth = 1018;
float distractor_density = 0.0;
std::string filename = "targetwords";
// Scan command line arguments for directory name
for(int i = 0; i < argc; i++) {
std::string r = argv[i];
if (r == "dir" && (i+1) < argc) {
dirpath = argv[i+1];
++i;
} else if (r == "--arrows") {
featureFlags |= ReachingWidget::FEATURE_ARROWS;
} else if (r == "--particles") {
featureFlags |= ReachingWidget::FEATURE_PARTICLES;
} else if (r == "--players" && (i+1) < argc) {
players = atoi(argv[++i]);
} else if (r == "--levels" && (i+1) < argc) {
levelCount = atoi(argv[++i]);
} else if (r == "--displaywidth" && (i+1) < argc) {
// Should be millimeters
displayWidth = atof(argv[++i]);
} else if (r == "--mode" && (i+1) < argc) {
if ((std::string("distort")).compare(std::string(argv[++i])) == 0)
{
app.reachingMode = DISTORT_MODE;
}
}
else if (r == "--start" && (i+1) < argc) {
if ((std::string("left")).compare(std::string(argv[++i])) == 0)
{
app.startSide = START_LEFT;
}
}
else if (r == "--wordfile" && (i+1) < argc){
filename = std::string(argv[++i]);
}
else if (r == "--density" && (i+1) < argc){
distractor_density = atof(argv[++i]);
}
}
if(app.reachingMode == DISTORT_MODE){
app.m_postUpdate.push_back(bg);
}
// Introduce generic distant reaching widget
ReachingWidget * d;
if(app.reachingMode == DISTORT_MODE){
d = new DistortWidget(app.root());
std::cout << "Selected distant reaching mode: Distort" << std::endl;
}
else{
// Default to vacuum
d = new VacuumReachingWidget(app.root());
std::cout << "Selected distant reaching mode: Vacuum" << std::endl;
}
d->setFeatureFlags(featureFlags);
d->setSize(app.root()->size());
d->setStyle(app.style());
//d->resize(d->width(), d->height());
// every 32 pixels
const int density = 28;
d->resize(Nimble::Math::Round(d->width()/density), Nimble::Math::Round(d->height()/density));
d->setDepth(-30);
d->setInputTransparent(true);
//MultiWidgets::Widget * root = d;
app.m_overlay = d;
WordGameWidget * wg = new WordGameWidget(players, app.root(), displayWidth, filename, distractor_density);
wg->setDepth(-25);
wg->raiseFlag(WordGameWidget::LOCK_DEPTH);
wg->setAutoBringToTop(false);
wg->initializeLevel();
wg->setStyle(app.style());
wg->setInputTransparent(true);
wg->setSize(0,0);
d->wordGameWidget = wg;
return app.run();
}