UIWidget.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  48. {
  49. using namespace WidgetEvent;
  50. data[P_TARGET] = target;
  51. data[P_TYPE] = (unsigned) ev.type;
  52. data[P_X] = ev.target_x;
  53. data[P_Y] = ev.target_y;
  54. data[P_DELTAX] = ev.delta_x;
  55. data[P_DELTAY] = ev.delta_y;
  56. data[P_COUNT] = ev.count;
  57. data[P_KEY] = ev.key;
  58. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  59. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  60. data[P_ID] = (unsigned) ev.ref_id;
  61. data[P_TOUCH] = (unsigned) ev.touch;
  62. }
  63. void UIWidget::OnDelete()
  64. {
  65. widget_ = 0;
  66. ReleaseRef();
  67. }
  68. void UIWidget::AddChild(UIWidget* child)
  69. {
  70. if (!widget_ || !child->widget_)
  71. return;
  72. widget_->AddChild(child->widget_);
  73. }
  74. void UIWidget::SetText(const String& text)
  75. {
  76. if (!widget_)
  77. return;
  78. widget_->SetText(text.CString());
  79. }
  80. void UIWidget::SetPosition(int x, int y)
  81. {
  82. if (!widget_)
  83. return;
  84. widget_->SetPosition(TBPoint(x, y));
  85. }
  86. void UIWidget::SetSize(int width, int height)
  87. {
  88. if (!widget_)
  89. return;
  90. widget_->SetSize(width, height);
  91. }
  92. void UIWidget::Center()
  93. {
  94. if (!widget_)
  95. return;
  96. UI* ui = GetSubsystem<UI>();
  97. TBRect rect = widget_->GetRect();
  98. TBWidget* root = ui->GetRootWidget();
  99. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  100. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  101. }
  102. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  103. {
  104. if (ev.type == EVENT_TYPE_CLICK)
  105. return false;
  106. return false;
  107. }
  108. }