UIWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //--player --editor-resource-paths "/Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicPlayer/Resources/CoreData!/Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicPlayer/Resources/PlayerData!/Users/josh/Dev/atomic/AtomicExamples/UIExample/Resources"
  2. #include "../IO/Log.h"
  3. #include "UIEvents.h"
  4. #include "UI.h"
  5. #include "UIWidget.h"
  6. using namespace tb;
  7. namespace Atomic
  8. {
  9. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  10. widget_(0)
  11. {
  12. AddRef();
  13. if (createWidget)
  14. {
  15. widget_ = new TBWidget();
  16. widget_->SetDelegate(this);
  17. }
  18. }
  19. UIWidget::~UIWidget()
  20. {
  21. }
  22. bool UIWidget::Load(const String& filename)
  23. {
  24. UI* ui = GetSubsystem<UI>();
  25. if (!ui->LoadResourceFile(widget_ , filename))
  26. return false;
  27. VariantMap eventData;
  28. eventData[WidgetLoaded::P_WIDGET] = this;
  29. SendEvent(E_WIDGETLOADED, eventData);
  30. return true;
  31. }
  32. UIWidget* UIWidget::GetWidget(const String& id)
  33. {
  34. if (!widget_)
  35. return 0;
  36. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  37. if (!child)
  38. return 0;
  39. UI* ui = GetSubsystem<UI>();
  40. return ui->WrapWidget(child);
  41. }
  42. void UIWidget::SetWidget(tb::TBWidget* widget)
  43. {
  44. widget_ = widget;
  45. widget_->SetDelegate(this);
  46. }
  47. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  48. {
  49. using namespace WidgetEvent;
  50. data[P_HANDLER] = handler;
  51. data[P_TARGET] = target;
  52. data[P_TYPE] = (unsigned) ev.type;
  53. data[P_X] = ev.target_x;
  54. data[P_Y] = ev.target_y;
  55. data[P_DELTAX] = ev.delta_x;
  56. data[P_DELTAY] = ev.delta_y;
  57. data[P_COUNT] = ev.count;
  58. data[P_KEY] = ev.key;
  59. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  60. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  61. data[P_REFID] = (unsigned) ev.ref_id;
  62. data[P_TOUCH] = (unsigned) ev.touch;
  63. }
  64. void UIWidget::OnDelete()
  65. {
  66. if (widget_)
  67. {
  68. // if we don't have a UI subsystem, we are exiting
  69. UI* ui = GetSubsystem<UI>();
  70. if (ui)
  71. ui->UnwrapWidget(widget_);
  72. }
  73. widget_ = 0;
  74. VariantMap eventData;
  75. eventData[WidgetDeleted::P_WIDGET] = this;
  76. SendEvent(E_WIDGETDELETED, eventData);
  77. ReleaseRef();
  78. }
  79. void UIWidget::AddChild(UIWidget* child)
  80. {
  81. if (!widget_ || !child->widget_)
  82. return;
  83. widget_->AddChild(child->widget_);
  84. }
  85. void UIWidget::SetText(const String& text)
  86. {
  87. if (!widget_)
  88. return;
  89. widget_->SetText(text.CString());
  90. }
  91. void UIWidget::SetPosition(int x, int y)
  92. {
  93. if (!widget_)
  94. return;
  95. widget_->SetPosition(TBPoint(x, y));
  96. }
  97. void UIWidget::SetSize(int width, int height)
  98. {
  99. if (!widget_)
  100. return;
  101. widget_->SetSize(width, height);
  102. }
  103. void UIWidget::Center()
  104. {
  105. if (!widget_)
  106. return;
  107. UI* ui = GetSubsystem<UI>();
  108. TBRect rect = widget_->GetRect();
  109. TBWidget* root = ui->GetRootWidget();
  110. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  111. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  112. }
  113. UIWidget* UIWidget::GetParent()
  114. {
  115. if (!widget_)
  116. return 0;
  117. TBWidget* parent = widget_->GetParent();
  118. if (!parent)
  119. return 0;
  120. UI* ui = GetSubsystem<UI>();
  121. return ui->WrapWidget(parent);
  122. }
  123. void UIWidget::RemoveChild(UIWidget* child)
  124. {
  125. if (!widget_ || !child)
  126. return;
  127. TBWidget* childw = child->GetInternalWidget();
  128. if (!childw)
  129. return;
  130. widget_->RemoveChild(childw);
  131. }
  132. const String& UIWidget::GetId()
  133. {
  134. if (!widget_ || !widget_->GetID())
  135. {
  136. if (id_.Length())
  137. id_.Clear();
  138. return id_;
  139. }
  140. if (id_.Length())
  141. return id_;
  142. UI* ui = GetSubsystem<UI>();
  143. ui->GetTBIDString(widget_->GetID(), id_);
  144. return id_;
  145. }
  146. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  147. {
  148. UI* ui = GetSubsystem<UI>();
  149. if (ev.type == EVENT_TYPE_CLICK)
  150. {
  151. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  152. {
  153. // popup menu
  154. if (JSGetHeapPtr())
  155. {
  156. VariantMap eventData;
  157. eventData[PopupMenuSelect::P_BUTTON] = this;
  158. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  159. SendEvent(E_POPUPMENUSELECT, eventData);
  160. }
  161. return true;
  162. }
  163. else
  164. {
  165. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  166. {
  167. VariantMap eventData;
  168. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  169. SendEvent(E_WIDGETEVENT, eventData);
  170. }
  171. }
  172. }
  173. return false;
  174. }
  175. }