UI.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/UIBatch.h"
  26. namespace Atomic
  27. {
  28. class VertexBuffer;
  29. class UIRenderer;
  30. class UIWidget;
  31. class UIPopupWindow;
  32. namespace SystemUI
  33. {
  34. class MessageBox;
  35. }
  36. class UI : public Object, private tb::TBWidgetListener
  37. {
  38. OBJECT(UI)
  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 GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  50. void SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData);
  51. void Initialize(const String& languageFile);
  52. void Shutdown();
  53. void LoadSkin(const String& skin, const String& overrideSkin = String::EMPTY);
  54. bool GetSkinLoaded() { return skinLoaded_; }
  55. /// Load the default skin, will also look in resoures for UI/Skin/skin.ui.txt and
  56. /// UI/Skin/Override/skin.ui.txt for base skin and possible override (TODO: baked in UI setting for load from project)
  57. void LoadDefaultPlayerSkin();
  58. void AddFont(const String& fontFile, const String &name);
  59. void SetDefaultFont(const String& name, int size);
  60. bool IsWidgetWrapped(tb::TBWidget* widget);
  61. // wrap an existing widget we new'd from script
  62. void WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget);
  63. // given a TB widget, creating a UIWidget
  64. UIWidget* WrapWidget(tb::TBWidget* widget);
  65. bool UnwrapWidget(tb::TBWidget* widget);
  66. unsigned DebugGetWrappedWidgetCount() { return widgetWrap_.Size(); }
  67. void PruneUnreachableWidgets();
  68. void GetTBIDString(unsigned id, String& value);
  69. SystemUI::MessageBox *ShowSystemMessageBox(const String& title, const String& message);
  70. void ShowDebugHud(bool value);
  71. void ToggleDebugHud();
  72. void ShowConsole(bool value);
  73. void ToggleConsole();
  74. bool GetFocusedWidget();
  75. /// request exit on next frame
  76. void RequestExit() { exitRequested_ = true; inputDisabled_ = true; }
  77. UIRenderer* GetRenderer() { return renderer_; }
  78. UIWidget* GetWidgetAt(int x, int y, bool include_children);
  79. bool GetBlockChangedEvents() const { return changedEventsBlocked_ > 0; }
  80. void SetBlockChangedEvents(bool blocked = true);
  81. UIWidget* GetHoveredWidget();
  82. // Debugging
  83. static void DebugShowSettingsWindow(UIWidget* parent);
  84. private:
  85. static WeakPtr<Context> uiContext_;
  86. static void TBFileReader(const char* filename, void** data, unsigned* length);
  87. static void TBIDRegisterStringCallback(unsigned id, const char* value);
  88. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  89. void HandleExitRequested(StringHash eventType, VariantMap& eventData);
  90. void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  91. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  92. // TBWidgetListener
  93. void OnWidgetDelete(tb::TBWidget *widget);
  94. bool OnWidgetDying(tb::TBWidget *widget);
  95. void OnWidgetFocusChanged(tb::TBWidget *widget, bool focused);
  96. bool OnWidgetInvokeEvent(tb::TBWidget *widget, const tb::TBWidgetEvent &ev);
  97. tb::TBWidget* rootWidget_;
  98. UIRenderer* renderer_;
  99. /// UI rendering batches.
  100. PODVector<UIBatch> batches_;
  101. /// UI rendering vertex data.
  102. PODVector<float> vertexData_;
  103. SharedPtr<VertexBuffer> vertexBuffer_;
  104. WeakPtr<Graphics> graphics_;
  105. HashMap<tb::TBWidget*, SharedPtr<UIWidget> > widgetWrap_;
  106. HashMap<unsigned, String> tbidToString_;
  107. WeakPtr<UIPopupWindow> tooltip_;
  108. int changedEventsBlocked_;
  109. bool inputDisabled_;
  110. bool keyboardDisabled_;
  111. bool initialized_;
  112. bool skinLoaded_;
  113. bool consoleVisible_;
  114. bool exitRequested_;
  115. float tooltipHoverTime_;
  116. // Events
  117. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  118. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  119. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  120. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  121. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  122. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  123. void HandleKeyUp(StringHash eventType, VariantMap& eventData);
  124. void HandleKey(bool keydown, int keycode, int scancode);
  125. void HandleTextInput(StringHash eventType, VariantMap& eventData);
  126. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  127. void HandleConsoleClosed(StringHash eventType, VariantMap& eventData);
  128. //Touch Input
  129. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  130. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  131. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  132. };
  133. }