test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "test.h"
  2. #include "oxygine-framework.h"
  3. #include "core/STDFileSystem.h"
  4. #include "WebImage.h"
  5. Resources Test::resourcesUI;
  6. file::STDFileSystem extfs(true);
  7. spTest Test::instance;
  8. void Test::init()
  9. {
  10. //Mount additional file system with inner path "ext"
  11. //Used for searching files in data/ext
  12. extfs.setPath(file::fs().getFullPath("ext").c_str());
  13. file::mount(&extfs);
  14. resourcesUI.loadXML("demo/res_ui.xml");
  15. resourcesUI.loadXML("demo/fonts.xml");
  16. HttpRequestTask::init();
  17. //Load logo from oxygine server
  18. spWebImage sp = new WebImage;
  19. sp->load("http://oxygine.org/test/logo.png");
  20. sp->setInputEnabled(false);
  21. sp->attachTo(getStage());
  22. sp->setPriority(10);
  23. sp->setAlpha(128);
  24. sp->setSize(150, 107);
  25. sp->setX(getStage()->getWidth() - sp->getWidth());
  26. sp->setY(getStage()->getHeight() - sp->getHeight());
  27. }
  28. void Test::free()
  29. {
  30. resourcesUI.free();
  31. instance->detach();
  32. instance = 0;
  33. HttpRequestTask::release();
  34. }
  35. class Toggle: public Button
  36. {
  37. public:
  38. Toggle(const Test::toggle* t, int num): _current(0)
  39. {
  40. _items.assign(t, t + num);
  41. }
  42. int _current;
  43. std::vector<Test::toggle> _items;
  44. };
  45. Color textColor(72, 61, 139, 255);
  46. spTextField createText(const std::string& txt)
  47. {
  48. spTextField text = new TextField();
  49. TextStyle style;
  50. style.font = Test::resourcesUI.getResFont("main")->getFont();
  51. style.color = textColor;
  52. style.vAlign = TextStyle::VALIGN_MIDDLE;
  53. style.hAlign = TextStyle::HALIGN_CENTER;
  54. style.multiline = true;
  55. text->setStyle(style);
  56. text->setText(txt.c_str());
  57. return text;
  58. }
  59. spButton createButtonHelper(spButton button, const std::string& txt, EventCallback cb)
  60. {
  61. button->setPriority(10);
  62. //button->setName(id);
  63. button->setResAnim(Test::resourcesUI.getResAnim("button"));
  64. button->addEventListener(TouchEvent::CLICK, cb);
  65. //Create Actor with Text and add it to button as child
  66. spTextField text = createText(txt);
  67. text->setSize(button->getSize());
  68. text->attachTo(button);
  69. return button;
  70. }
  71. Test::Test() : _color(Color::White), _txtColor(72, 61, 139, 255)
  72. {
  73. setSize(getStage()->getSize());
  74. _x = getWidth() - 100;
  75. _y = 2;
  76. ui = new Actor;
  77. content = new Content;
  78. content->setSize(getSize());
  79. addChild(content);
  80. addChild(ui);
  81. if (instance)
  82. {
  83. spButton button = createButtonHelper(new Button, "back", CLOSURE(this, &Test::back));
  84. button->setY(getHeight() - button->getHeight());
  85. ui->addChild(button);
  86. }
  87. memset(_notifies, 0, sizeof(_notifies));
  88. }
  89. Test::~Test()
  90. {
  91. }
  92. spButton Test::addButton(std::string id, std::string txt)
  93. {
  94. textColor = _txtColor;
  95. spButton button = createButtonHelper(new Button, txt, CLOSURE(this, &Test::_clicked));
  96. initActor(button.get(),
  97. arg_name = id,
  98. arg_attachTo = ui,
  99. arg_anchor = Vector2(0.5f, 0.0f),
  100. arg_pos = Vector2(_x, _y));
  101. button->setColor(_color);
  102. textColor = Color(72, 61, 139, 255);
  103. _y += button->getHeight() + 2.0f;
  104. if (_y + button->getHeight() >= getHeight())
  105. {
  106. _y = 5;
  107. _x += button->getWidth() + 70;
  108. }
  109. return button;
  110. }
  111. void Test::addToggle(std::string id, const toggle* t, int num)
  112. {
  113. spButton button = createButtonHelper(new Toggle(t, num), t[0].text, CLOSURE(this, &Test::_toggleClicked));
  114. initActor(button.get(),
  115. arg_name = id,
  116. arg_attachTo = ui,
  117. arg_anchor = Vector2(0.5f, 0.0f),
  118. arg_pos = Vector2(_x, _y));
  119. _y += button->getHeight() + 2.0f;
  120. if (_y + button->getHeight() >= getHeight())
  121. {
  122. _y = 0;
  123. _x += button->getWidth() + 70;
  124. }
  125. }
  126. void Test::updateText(std::string id, std::string txt)
  127. {
  128. spActor child = ui->getChild(id);
  129. if (!child)
  130. return;
  131. spTextField t = safeSpCast<TextField>(child->getFirstChild());
  132. if (!t)
  133. return;
  134. t->setText(txt);
  135. }
  136. void Test::_clicked(Event* event)
  137. {
  138. clicked(event->currentTarget->getName());
  139. }
  140. void Test::_toggleClicked(Event* event)
  141. {
  142. Toggle* t = safeCast<Toggle*>(event->currentTarget.get());
  143. toggleClicked(event->currentTarget->getName(), &t->_items[t->_current]);
  144. t->_current = (t->_current + 1) % t->_items.size();
  145. spTextField ta = safeSpCast<TextField>(t->getFirstChild());
  146. const toggle* data = &t->_items[t->_current];
  147. ta->setText(data->text);
  148. }
  149. void Test::back(Event* event)
  150. {
  151. detach();
  152. instance->setVisible(true);
  153. }
  154. void Test::notify(std::string txt, int time)
  155. {
  156. size_t N = 0;
  157. for (size_t i = 0; i < MAX_NOTIFIES; ++i)
  158. {
  159. if (_notifies[i])
  160. continue;
  161. N = i;
  162. break;
  163. }
  164. _notifies[N] += 1;
  165. spColorRectSprite sprite = new ColorRectSprite();
  166. sprite->setUserData((void*)N);
  167. sprite->setPriority(10);
  168. Color colors[] = {Color(0xD2691EFF), Color(0x7FFFD4FF), Color(0xDC143CFF), Color(0xADFF2FFF), };
  169. Color c = colors[rand() % 4];
  170. sprite->setColor(c);
  171. sprite->setSize(100, 30);
  172. //sprite->setAnimFrame(resourcesUI.getResAnim("button"));
  173. sprite->setAlpha(0);
  174. spTweenQueue tq = new TweenQueue;
  175. tq->add(Actor::TweenAlpha(255), 300, 1, false, 0, Tween::ease_inExpo);
  176. tq->add(Actor::TweenAlpha(0), 300, 1, false, 1200);
  177. tq->setDetachActor(true);
  178. tq->addDoneCallback(CLOSURE(this, &Test::notifyDone));
  179. sprite->addTween(tq);
  180. sprite->attachTo(ui);
  181. sprite->setPosition(2.0f, getHeight() - 100.0f - N * sprite->getHeight() * 1.1f);
  182. spTextField text = createText(txt);
  183. text->attachTo(sprite);
  184. text->setColor(Color::Black);
  185. text->setPosition(sprite->getSize() / 2);
  186. }
  187. void Test::notifyDone(Event* ev)
  188. {
  189. size_t N = size_t(ev->target->getUserData());
  190. _notifies[N] -= 1;
  191. }