UI.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 UIView;
  33. class UIPopupWindow;
  34. class MessageBox;
  35. class ATOMIC_API UI : public Object, private tb::TBWidgetListener
  36. {
  37. friend class UIView;
  38. ATOMIC_OBJECT(UI, Object)
  39. public:
  40. /// Construct.
  41. UI(Context* context);
  42. /// Destruct.
  43. virtual ~UI();
  44. tb::TBWidget* GetRootWidget() { return rootWidget_; }
  45. bool LoadResourceFile(tb::TBWidget* widget, const String& filename);
  46. void SetKeyboardDisabled(bool disabled) {keyboardDisabled_ = disabled; }
  47. void SetInputDisabled(bool disabled) { inputDisabled_ = disabled; }
  48. void Render(bool resetRenderTargets = true);
  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. /// Get the currently focused view
  92. UIView* GetFocusedView() const { return focusedView_; }
  93. private:
  94. static WeakPtr<Context> uiContext_;
  95. static void TBFileReader(const char* filename, void** data, unsigned* length);
  96. static void TBIDRegisterStringCallback(unsigned id, const char* value);
  97. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  98. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  99. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  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. /// Add a UIView to UI subsystem, happens immediately at UIView creation
  107. void AddUIView(UIView* uiView);
  108. /// Set the currently focused view
  109. void SetFocusedView(UIView* uiView);
  110. /// Removes a UIView from the UI subsystem, readding a view is not encouraged
  111. void RemoveUIView(UIView* uiView);
  112. tb::TBWidget* rootWidget_;
  113. UIRenderer* renderer_;
  114. WeakPtr<Graphics> graphics_;
  115. HashMap<tb::TBWidget*, SharedPtr<UIWidget> > widgetWrap_;
  116. HashMap<unsigned, String> tbidToString_;
  117. WeakPtr<UIPopupWindow> tooltip_;
  118. int changedEventsBlocked_;
  119. bool inputDisabled_;
  120. bool keyboardDisabled_;
  121. bool initialized_;
  122. bool skinLoaded_;
  123. bool consoleVisible_;
  124. bool exitRequested_;
  125. float tooltipHoverTime_;
  126. Vector<SharedPtr<UIView>> uiViews_;
  127. WeakPtr<UIView> focusedView_;
  128. // Events
  129. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  130. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  131. void HandleConsoleClosed(StringHash eventType, VariantMap& eventData);
  132. };
  133. }