UI.h 6.9 KB

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