TestInputText.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include "test.h"
  3. #include "InputText.h"
  4. DECLARE_SMART(TextWithBackground, spTextWithBackground);
  5. class TextWithBackground: public ColorRectSprite
  6. {
  7. public:
  8. TextWithBackground(const string& defText)
  9. {
  10. text = new TextField;
  11. //Don't handle touch events on this Actor
  12. text->setTouchEnabled(false);
  13. TextStyle style;
  14. style.color = Color::Black;
  15. style.hAlign = TextStyle::HALIGN_MIDDLE;
  16. style.vAlign = TextStyle::VALIGN_MIDDLE;
  17. style.multiline = true;
  18. style.font = Test::resourcesUI.getResFont("big");
  19. text->setStyle(style);
  20. text->setText(defText);
  21. addChild(text);
  22. }
  23. spTextField text;
  24. void sizeChanged(const Vector2& size)
  25. {
  26. text->setSize(size);
  27. }
  28. };
  29. class TestInputText: public Test
  30. {
  31. public:
  32. spInputText _input;
  33. spTextWithBackground _current;
  34. TestInputText()
  35. {
  36. _input = new InputText;
  37. //_input->setAllowedSymbols("1234567890");
  38. //_input->setDisallowedSymbols("0");
  39. _input->addEventListener(Event::COMPLETE, CLOSURE(this, &TestInputText::onComplete));
  40. spTextWithBackground t = new TextWithBackground("click and edit me 1");
  41. t->setSize(200, 60);
  42. t->setPosition(content->getWidth() / 2 - t->getWidth() / 2, 100);
  43. t->attachTo(this);
  44. t->addEventListener(TouchEvent::CLICK, CLOSURE(this, &TestInputText::onClick));
  45. t = new TextWithBackground("click and edit me 2");
  46. t->setSize(200, 60);
  47. t->setPosition(content->getWidth() / 2 - t->getWidth() / 2, 170);
  48. t->attachTo(this);
  49. t->addEventListener(TouchEvent::CLICK, CLOSURE(this, &TestInputText::onClick));
  50. }
  51. void onClick(Event* ev)
  52. {
  53. if (_current)
  54. {
  55. _current->setColor(Color::White);
  56. }
  57. _current = safeSpCast<TextWithBackground>(ev->currentTarget);
  58. _input->start(_current->text);
  59. _current->setColor(Color::Red);
  60. }
  61. void onComplete(Event* ev)
  62. {
  63. if (_current)
  64. {
  65. _current->setColor(Color::White);
  66. notify(_current->text->getText());
  67. }
  68. _current = 0;
  69. InputText::stopAnyInput();
  70. }
  71. ~TestInputText()
  72. {
  73. InputText::stopAnyInput();
  74. }
  75. void clicked(string id)
  76. {
  77. }
  78. };