UI.h 6.0 KB

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