rag
graphic 2d engine
TextInput.h
1 //
2 // TextInput.h
3 //
4 
5 #ifndef __rag__TextInput__
6 #define __rag__TextInput__
7 
8 #include <rag/Timer.h>
9 #include <rag/TextEvent.h>
10 #include <rag/DisplayObject.h>
11 #include "Keyboard.h"
12 #include <rag/TextField.h>
13 
14 namespace events
15 {
17  static const std::string TEXTINPUT_START_EDITION = "TextInputStartEdition";
18 
20  static const std::string TEXTINPUT_FINISH_EDITION = "TextInputFinishEdition";
21 
23  static const std::string TEXTINPUT_ENTER_TEXT = "TextInputEnterText";
24 }
25 
26 namespace rag
27 {
28 
30 
47 {
48 public:
49 
50  TextInput(std::string defaultText = "", int maxLines = 0, int maxCharacters = 0, bool useCaptureLayer = true, Keyboard::KeyboardType keyboardType = Keyboard::KeyboardTypeDefault);
51 
52  ~TextInput();
53 
54  // returns the text exter by the user (note that can differ from what it's shown, i.e., the default text can be shown even when the user haven't wrote any text yet).
55  std::string getText();
56 
57  // Force the text to some value. Note that this is different than defaultText, this is like actually type this text.
58  void setText(const std::string& text);
59 
60  void openKeyboard();
61  void closeKeyboard();
62  void clearText();
63 
64 private:
65 
66  class CaptureLayer : public rag::DisplayObject, events::EventListener
67  {
68  public:
69  CaptureLayer(int width = 0, int height = 0)
70  {
71  captureInput = true;
72 
73  if (width == 0 || height == 0)
74  checkHitPoint = false;
75  else
76  {
77  this->width = (float)width;
78  this->height = (float)height;
79  checkHitPoint = true;
80  }
81 
82  addEventListener(events::TOUCH_BEGIN, this);
83  addEventListener(events::TOUCH_END, this);
84  addEventListener(events::TOUCH_MOVE, this);
85  addEventListener(events::TOUCH_OUT, this);
86  addEventListener(events::TOUCH_OVER, this);
87  addEventListener(events::TOUCH_TAP, this);
88  }
89 
90  // Inherited via EventListener
91  virtual void onEvent(const std::string & type, events::Event & event) override {}
92 
93  };
94 
95  virtual void onEvent(const std::string& type, events::Event& event);
96  virtual void logicUpdate();
97 
98  void startEdition();
99  void finishEdition();
100 
101  std::string getString();
102 
103  void updateTextField();
104 
105  rag::Timer timer;
106  Keyboard* keyboard;
107 
108  rag::TextField* textField;
109  std::vector<std::string> text;
110 
111  CaptureLayer* captureLayer;
112 
113  bool editing;
114 
115  std::string defaultText;
116  int maxLines;
117  int maxCharacters;
118 
119  Color defaultColor;
120 
121  float originalTextX;
122 
123  bool useCaptureLayer;
124 
125  DisplayObject* button;
126 
127  bool stageMoved;
128 
129  Keyboard::KeyboardType keyboardType;
130 
131  bool openKeyboardRequested;
132 
133 };
134 
135 } // rag
136 
137 #endif
Provides time-related functionality.
Definition: Timer.h:10
Base class for event system.
Definition: Event.h:24
bool checkHitPoint
When captureInput, checkHitPoint makes capture input only when hitTest is true. Defaults to false...
Definition: DisplayObject.h:199
bool captureInput
When true, input events are captured and propagation stops.
Definition: DisplayObject.h:198
Interface that allows to listen events.
Definition: EventListener.h:10
Definition: Bitmap.h:8
Helper object to add input to a TextField.
Definition: TextInput.h:46
Multiplatform keyboard abstraction.
Definition: Keyboard.h:14
Core object used to display things in screen.
Definition: DisplayObject.h:23
High level abstraction to render texts in display list.
Definition: TextField.h:22
Represents RGBA color.
Definition: Color.h:11
Definition: Event.h:11