UI.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #pragma once
  23. #include <ThirdParty/TurboBadger/tb_widgets_listener.h>
  24. #include "../Core/Object.h"
  25. #include "../UI/UIEnums.h"
  26. #include "../UI/UIBatch.h"
  27. namespace Atomic
  28. {
  29. class VertexBuffer;
  30. class UIRenderer;
  31. class UIWidget;
  32. class UIPopupWindow;
  33. namespace SystemUI
  34. {
  35. class MessageBox;
  36. }
  37. class UI : public Object, private tb::TBWidgetListener
  38. {
  39. ATOMIC_OBJECT(UI, Object)
  40. public:
  41. /// Construct.
  42. UI(Context* context);
  43. /// Destruct.
  44. virtual ~UI();
  45. tb::TBWidget* GetRootWidget() { return rootWidget_; }
  46. bool LoadResourceFile(tb::TBWidget* widget, const String& filename);
  47. void SetKeyboardDisabled(bool disabled) {keyboardDisabled_ = disabled; }
  48. void SetInputDisabled(bool disabled) { inputDisabled_ = disabled; }
  49. void Render(bool resetRenderTargets = true);
  50. void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  51. void SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData);
  52. void Initialize(const String& languageFile);
  53. void Shutdown();
  54. void LoadSkin(const String& skin, const String& overrideSkin = String::EMPTY);
  55. bool GetSkinLoaded() { return skinLoaded_; }
  56. /// Load the default skin, will also look in resoures for UI/Skin/skin.ui.txt and
  57. /// UI/Skin/Override/skin.ui.txt for base skin and possible override (TODO: baked in UI setting for load from project)
  58. void LoadDefaultPlayerSkin();
  59. void AddFont(const String& fontFile, const String &name);
  60. void SetDefaultFont(const String& name, int size);
  61. bool IsWidgetWrapped(tb::TBWidget* widget);
  62. // wrap an existing widget we new'd from script
  63. void WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget);
  64. // given a TB widget, creating a UIWidget
  65. UIWidget* WrapWidget(tb::TBWidget* widget);
  66. bool UnwrapWidget(tb::TBWidget* widget);
  67. unsigned DebugGetWrappedWidgetCount() { return widgetWrap_.Size(); }
  68. void PruneUnreachableWidgets();
  69. void GetTBIDString(unsigned id, String& value);
  70. SystemUI::MessageBox *ShowSystemMessageBox(const String& title, const String& message);
  71. // Debug HUD
  72. void ShowDebugHud(bool value);
  73. void ToggleDebugHud();
  74. /// Cycle debug HUD between showing primitive stats, current mode, profiler data, all three or none
  75. void CycleDebugHudMode();
  76. void SetDebugHudProfileMode(DebugHudProfileMode mode);
  77. void SetDebugHudExtents(bool useRootExtents = true, const IntVector2& position = IntVector2::ZERO, const IntVector2& size = IntVector2::ZERO);
  78. /// Set the DebugHud refresh interval for performance and metrics in seconds
  79. void SetDebugHudRefreshInterval(float seconds);
  80. void ShowConsole(bool value);
  81. void ToggleConsole();
  82. bool GetFocusedWidget();
  83. /// request exit on next frame
  84. void RequestExit() { exitRequested_ = true; inputDisabled_ = true; }
  85. UIRenderer* GetRenderer() { return renderer_; }
  86. UIWidget* GetWidgetAt(int x, int y, bool include_children);
  87. bool GetBlockChangedEvents() const { return changedEventsBlocked_ > 0; }
  88. void SetBlockChangedEvents(bool blocked = true);
  89. UIWidget* GetHoveredWidget();
  90. // Debugging
  91. static void DebugShowSettingsWindow(UIWidget* parent);
  92. private:
  93. static WeakPtr<Context> uiContext_;
  94. static void TBFileReader(const char* filename, void** data, unsigned* length);
  95. static void TBIDRegisterStringCallback(unsigned id, const char* value);
  96. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  97. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  98. void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  99. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  100. // TBWidgetListener
  101. void OnWidgetDelete(tb::TBWidget *widget);
  102. bool OnWidgetDying(tb::TBWidget *widget);
  103. void OnWidgetFocusChanged(tb::TBWidget *widget, bool focused);
  104. bool OnWidgetInvokeEvent(tb::TBWidget *widget, const tb::TBWidgetEvent &ev);
  105. void OnWindowClose(tb::TBWindow *window);
  106. tb::TBWidget* rootWidget_;
  107. UIRenderer* renderer_;
  108. /// UI rendering batches.
  109. PODVector<UIBatch> batches_;
  110. /// UI rendering vertex data.
  111. PODVector<float> vertexData_;
  112. SharedPtr<VertexBuffer> vertexBuffer_;
  113. WeakPtr<Graphics> graphics_;
  114. HashMap<tb::TBWidget*, SharedPtr<UIWidget> > widgetWrap_;
  115. HashMap<unsigned, String> tbidToString_;
  116. WeakPtr<UIPopupWindow> tooltip_;
  117. int changedEventsBlocked_;
  118. bool inputDisabled_;
  119. bool keyboardDisabled_;
  120. bool initialized_;
  121. bool skinLoaded_;
  122. bool consoleVisible_;
  123. bool exitRequested_;
  124. float tooltipHoverTime_;
  125. // Events
  126. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  127. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  128. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  129. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  130. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  131. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  132. void HandleKeyUp(StringHash eventType, VariantMap& eventData);
  133. void HandleKey(bool keydown, int keycode, int scancode);
  134. void HandleTextInput(StringHash eventType, VariantMap& eventData);
  135. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  136. void HandleConsoleClosed(StringHash eventType, VariantMap& eventData);
  137. //Touch Input
  138. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  139. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  140. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  141. };
  142. }