TDME2  1.9.200
GUIStyledTextNodeController.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
8 #include <tdme/gui/fwd-tdme.h>
12 
13 using std::string;
14 using std::to_string;
15 using std::vector;
16 
23 
24 /**
25  * GUI styled text node controller
26  * @author Andreas Drewke
27  */
29 
30  : public GUINodeController
31 {
32  friend class tdme::gui::GUIParser;
33  friend class GUIStyledTextNode;
34 
35 public:
36 
37  /**
38  * Change listener interface
39  * @author Andreas Drewke
40  */
41  struct ChangeListener {
42 
43  /**
44  * Destructor
45  */
46  virtual ~ChangeListener() {}
47 
48  /**
49  * On remove text
50  * @param idx index
51  * @param count count
52  */
53  virtual void onRemoveText(int idx, int count) = 0;
54 
55  /**
56  * On remove text
57  * @param idx index
58  * @param count count
59  */
60  virtual void onInsertText(int idx, int count) = 0;
61  };
62 
63  /**
64  * Code completion listener
65  * @author Andreas Drewke
66  */
68 
69  /**
70  * Destructor
71  */
73 
74  /**
75  * On code completion requested
76  * @param idx index
77  */
78  virtual void onCodeCompletion(int idx) = 0;
79 
80  };
81 
82 private:
83  struct HistoryEntry {
86  Type type,
87  int idx,
88  const string& data,
89  bool joinable
90  ):
91  type(type),
92  idx(idx),
93  data(data),
95  {}
97  int idx;
98  string data;
99  bool joinable;
100  };
101 
102  static constexpr int64_t TIME_DOUBLECLICK { 250LL };
103  static constexpr int64_t CURSOR_MODE_DURATION { 500LL };
104  int64_t cursorModeStarted { -1LL };
107  int index { 0 };
108  int selectionIndex { -1 };
109  int lineIndex { 0 };
110  vector<ChangeListener*> changeListeners;
111  vector<CodeCompletionListener*> codeCompletionListeners;
114  bool dragging { false };
115  bool input { false };
116  int64_t timeLastClick { -1LL };
117  bool doubleClick { false };
118 
119  vector<HistoryEntry> history;
120  int historyEntryIdx { -1 };
121  int historyIdx { 0 };
122  bool typedChars { false };
123 
124  /**
125  * @return must show cursor
126  */
127  inline bool isShowCursor() {
128  return true;
129  }
130 
131  /**
132  * Reset cursor mode
133  */
134  void resetCursorMode();
135 
136  /**
137  * @return cursor mode
138  */
140 
141  /**
142  * Forward remove text
143  * @param idx index
144  * @param count count
145  */
146  void forwardRemoveText(int idx, int count);
147 
148  /**
149  * Forward insert text
150  * @param idx index
151  * @param count count
152  */
153  void forwardInsertText(int idx, int count);
154 
155  /**
156  * Forward code completion
157  * @param idx index
158  */
159  void forwardCodeCompletion(int idx);
160 
161 protected:
162  // forbid class copy
164 
165  /**
166  * Constructor
167  * @param node node
168  */
170 
171 public:
172  /**
173  * @return index
174  */
175  inline int getIndex() {
176  return index;
177  }
178 
179  /**
180  * Set index
181  * @param index index
182  */
183  inline void setIndex(int index) {
184  this->index = index;
185  }
186 
187  /**
188  * @return selection index
189  */
190  inline int getSelectionIndex() {
191  return selectionIndex;
192  }
193 
194  /**
195  * Set selection index
196  * @param selectionIndex selection index
197  */
199  this->selectionIndex = selectionIndex;
200  }
201 
202  /**
203  * Unset typing history entry index
204  */
206  typedChars = false;
207  historyEntryIdx = -1;
208  }
209 
210  /**
211  * Set typing history entry index
212  */
213  inline void setTypingHistoryEntryIdx() {
214  if (historyEntryIdx != -1) return;
215  auto index = this->index;
216  if (selectionIndex != -1) index = Math::min(index, selectionIndex);
218  }
219 
220  /**
221  * Store typing history entry
222  */
224 
225  /**
226  * Store typing history entry
227  * @param index index
228  * @param data data
229  */
230  void storeTypingHistoryEntry2(int index, const string& data);
231 
232  /**
233  * Store deletion history entry and store prior typing if we have any
234  * @param index index
235  * @param count count
236  */
240  }
241 
242  /**
243  * Store deletion history entry
244  * @param index index
245  * @param count count
246  */
247  void storeDeletionHistoryEntry(int index, int count) {
248  //
249  if (historyIdx != -1 && historyIdx < history.size()) {
250  history.erase(history.begin() + historyIdx, history.end());
251  historyIdx = history.size();
252  }
254  }
255 
256  /**
257  * Store typing history entry
258  * @param index index
259  * @param count count
260  */
261  void storeDeletionHistoryInternal(int index, int count);
262 
263  /**
264  * Redo
265  */
266  void redo();
267 
268  /**
269  * Undo
270  */
271  void undo();
272 
273  /**
274  * Select all
275  */
276  void selectAll();
277 
278  /**
279  * Cut
280  */
281  void cut();
282 
283  /**
284  * Copy
285  */
286  void copy();
287 
288  /**
289  * Paste
290  */
291  void paste();
292 
293  /**
294  * Delete
295  */
296  void delete_();
297 
298  // overridden methods
299  bool isDisabled() override;
300  void setDisabled(bool disabled) override;
301  void initialize() override;
302  void dispose() override;
303  void postLayout() override;
304  void handleMouseEvent(GUINode* node, GUIMouseEvent* event) override;
305  void handleKeyboardEvent(GUIKeyboardEvent* event) override;
306  void tick() override;
307  void onFocusGained() override;
308  void onFocusLost() override;
309  bool hasValue() override;
310  const MutableString& getValue() override;
311  void setValue(const MutableString& value) override;
312  void onSubTreeChange() override;
313 
314  /**
315  * Add change listener
316  * @param listener listener
317  */
318  void addChangeListener(ChangeListener* listener);
319 
320  /**
321  * Remove change listener
322  * @param listener listener
323  */
324  void removeChangeListener(ChangeListener* listener);
325 
326  /**
327  * Add code completion listener
328  * @param listener listener
329  */
330  void addCodeCompletionListener(CodeCompletionListener* listener);
331 
332  /**
333  * Remove code completion listener
334  * @param listener listener
335  */
336  void removeCodeCompletionListener(CodeCompletionListener* listener);
337 
338  /**
339  * Replace text from given index with given count by string by
340  * @param by string to insert
341  * @param index character index
342  * @param count character cound
343  */
344  void replace(const string& by, int index, int count);
345 
346 };
GUI parser.
Definition: GUIParser.h:40
GUI node controller base class.
GUI node base class.
Definition: GUINode.h:64
void replace(const string &by, int index, int count)
Replace text from given index with given count by string by.
void storeDeletionHistoryEntry(int index, int count)
Store deletion history entry.
void initialize() override
Initialize controller after element has been created.
void handleKeyboardEvent(GUIKeyboardEvent *event) override
Handle keyboard event.
void storeDeletionHistoryEntryStoreTypingEntry(int index, int count)
Store deletion history entry and store prior typing if we have any.
void setValue(const MutableString &value) override
Set value.
void handleMouseEvent(GUINode *node, GUIMouseEvent *event) override
Handle mouse event.
void addCodeCompletionListener(CodeCompletionListener *listener)
Add code completion listener.
void tick() override
Tick method will be executed once per frame.
void forwardInsertText(int idx, int count)
Forward insert text.
void unsetTypingHistoryEntryIdx()
Unset typing history entry index.
void setTypingHistoryEntryIdx()
Set typing history entry index.
void setDisabled(bool disabled) override
Set disabled.
void addChangeListener(ChangeListener *listener)
Add change listener.
void storeDeletionHistoryInternal(int index, int count)
Store typing history entry.
void removeCodeCompletionListener(CodeCompletionListener *listener)
Remove code completion listener.
void storeTypingHistoryEntry2(int index, const string &data)
Store typing history entry.
void removeChangeListener(ChangeListener *listener)
Remove change listener.
void forwardRemoveText(int idx, int count)
Forward remove text.
vector< CodeCompletionListener * > codeCompletionListeners
void setSelectionIndex(int selectionIndex)
Set selection index.
void forwardCodeCompletion(int idx)
Forward code completion.
Mutable utf8 aware string class.
Definition: MutableString.h:23
virtual void onRemoveText(int idx, int count)=0
On remove text.
virtual void onInsertText(int idx, int count)=0
On remove text.
virtual void onCodeCompletion(int idx)=0
On code completion requested.
HistoryEntry(Type type, int idx, const string &data, bool joinable)
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6