UIEditField.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include <TurboBadger/tb_widgets_common.h>
  24. #include <TurboBadger/tb_editfield.h>
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UIEditField.h"
  28. #include "../Atomic/Input/Input.h"
  29. using namespace tb;
  30. namespace Atomic
  31. {
  32. UIEditField::UIEditField(Context* context, bool createWidget) : UIWidget(context, false),
  33. firstFocusFlag_(false)
  34. {
  35. if (createWidget)
  36. {
  37. widget_ = new TBEditField();
  38. widget_->SetDelegate(this);
  39. GetSubsystem<UI>()->WrapWidget(this, widget_);
  40. }
  41. }
  42. UIEditField::~UIEditField()
  43. {
  44. }
  45. void UIEditField::SetReadOnly(bool readonly)
  46. {
  47. if (!widget_)
  48. return;
  49. TBEditField* w = (TBEditField*) widget_;
  50. w->SetReadOnly(readonly);
  51. }
  52. void UIEditField::SetStyling(bool styling)
  53. {
  54. if (!widget_)
  55. return;
  56. TBEditField* w = (TBEditField*) widget_;
  57. w->SetStyling(styling);
  58. }
  59. void UIEditField::SetMultiline(bool multiline)
  60. {
  61. if (!widget_)
  62. return;
  63. TBEditField* w = (TBEditField*) widget_;
  64. w->SetMultiline(multiline);
  65. }
  66. void UIEditField::SetWrapping(bool wrap)
  67. {
  68. if (!widget_)
  69. return;
  70. TBEditField* w = (TBEditField*) widget_;
  71. w->SetWrapping(wrap);
  72. }
  73. bool UIEditField::GetWrapping()
  74. {
  75. if (!widget_)
  76. return false;
  77. TBEditField* w = (TBEditField*) widget_;
  78. return w->GetWrapping();
  79. }
  80. void UIEditField::SetEditType(UI_EDIT_TYPE type)
  81. {
  82. if (!widget_)
  83. return;
  84. // safe cast?
  85. TBEditField* w = (TBEditField*) widget_;
  86. w->SetEditType((tb::EDIT_TYPE) type);
  87. }
  88. void UIEditField::ScrollTo(int x, int y)
  89. {
  90. if (!widget_)
  91. return;
  92. // safe cast?
  93. TBEditField* w = (TBEditField*) widget_;
  94. w->ScrollTo(x, y);
  95. }
  96. void UIEditField::AppendText(const String& text)
  97. {
  98. if (!widget_)
  99. return;
  100. // safe cast?
  101. TBEditField* w = (TBEditField*) widget_;
  102. w->AppendText(text.CString());
  103. }
  104. void UIEditField::SetAdaptToContentSize(bool adapt)
  105. {
  106. if (!widget_)
  107. return;
  108. TBEditField* w = (TBEditField*) widget_;
  109. w->SetAdaptToContentSize(adapt);
  110. }
  111. bool UIEditField::GetAdaptToContentSize() const
  112. {
  113. if (!widget_)
  114. return false;
  115. // safe cast?
  116. TBEditField* w = (TBEditField*) widget_;
  117. return w->GetAdaptToContentSize();
  118. }
  119. void UIEditField::Reformat(bool update_fragments)
  120. {
  121. if (!widget_)
  122. return;
  123. TBEditField* w = (TBEditField*) widget_;
  124. w->Reformat(update_fragments);
  125. }
  126. void UIEditField::SetTextAlign(UI_TEXT_ALIGN align)
  127. {
  128. if (!widget_)
  129. return;
  130. // safe cast?
  131. TBEditField* w = (TBEditField*) widget_;
  132. switch (align)
  133. {
  134. case UI_TEXT_ALIGN_CENTER:
  135. w->SetTextAlign(TB_TEXT_ALIGN_CENTER);
  136. break;
  137. case UI_TEXT_ALIGN_LEFT:
  138. w->SetTextAlign(TB_TEXT_ALIGN_LEFT);
  139. break;
  140. case UI_TEXT_ALIGN_RIGHT:
  141. w->SetTextAlign(TB_TEXT_ALIGN_RIGHT);
  142. break;
  143. }
  144. }
  145. void UIEditField::OnFocusChanged(bool focused)
  146. {
  147. UIWidget::OnFocusChanged(focused);
  148. if (!widget_)
  149. return;
  150. // safe cast?
  151. TBEditField* w = (TBEditField*) widget_;
  152. TBStyleEdit* styleEdit = w->GetStyleEdit();
  153. if (styleEdit != NULL)
  154. {
  155. if (focused)
  156. {
  157. if (!w->GetMultiline())
  158. styleEdit->selection.SelectAll();
  159. firstFocusFlag_ = true;
  160. #if defined(ATOMIC_PLATFORM_ANDROID) || defined(ATOMIC_PLATFORM_IOS)
  161. // click on field to gain focus and bring up the onscreen keyboard to edit
  162. if ( !(w->GetReadOnly() || w->GetState(WIDGET_STATE_DISABLED)) )
  163. {
  164. Input* input = GetSubsystem<Input>();
  165. input->SetScreenKeyboardVisible(true);
  166. }
  167. #endif
  168. }
  169. else
  170. {
  171. styleEdit->selection.SelectNothing();
  172. }
  173. }
  174. }
  175. bool UIEditField::OnEvent(const tb::TBWidgetEvent &ev)
  176. {
  177. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("edit_complete"))
  178. {
  179. VariantMap eventData;
  180. eventData[UIWidgetEditComplete::P_WIDGET] = this;
  181. SendEvent(E_UIWIDGETEDITCOMPLETE, eventData);
  182. return true;
  183. }
  184. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  185. {
  186. // Select the entire value in the field when it is selected
  187. if (widget_ && firstFocusFlag_)
  188. {
  189. firstFocusFlag_ = false;
  190. // safe cast?
  191. TBEditField* w = (TBEditField*) widget_;
  192. TBStyleEdit* styleEdit = w->GetStyleEdit();
  193. if (styleEdit != NULL && !w->GetMultiline())
  194. {
  195. styleEdit->selection.SelectAll();
  196. }
  197. }
  198. #if defined(ATOMIC_PLATFORM_ANDROID) || defined(ATOMIC_PLATFORM_IOS)
  199. // triple click to get the onscreen keyboard, in case it is auto-focused
  200. else if ( ev.count == 3 )
  201. {
  202. TBEditField* w = (TBEditField*) widget_;
  203. if ( !(w->GetReadOnly() || w->GetState(WIDGET_STATE_DISABLED)) )
  204. {
  205. Input* input = GetSubsystem<Input>();
  206. input->SetScreenKeyboardVisible(true);
  207. }
  208. }
  209. #endif
  210. }
  211. return UIWidget::OnEvent(ev);
  212. }
  213. }