2
0

DebugUI.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Application/DebugUI.h>
  6. #include <Renderer/Texture.h>
  7. #include <UI/UIManager.h>
  8. #include <UI/UIImage.h>
  9. #include <UI/UIStaticText.h>
  10. #include <UI/UICheckBox.h>
  11. #include <UI/UIHorizontalStack.h>
  12. #include <UI/UIVerticalStack.h>
  13. #include <UI/UITextButton.h>
  14. #include <Image/LoadTGA.h>
  15. #include <Utils/Log.h>
  16. #include <Utils/AssetStream.h>
  17. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  18. #include <fstream>
  19. JPH_SUPPRESS_WARNINGS_STD_END
  20. DebugUI::DebugUI(UIManager *inUIManager, const Font *inFont) :
  21. mUI(inUIManager),
  22. mFont(inFont)
  23. {
  24. // Load UI texture with commonly used UI elements
  25. AssetStream texture_stream("UI.tga", std::ios::in | std::ios::binary);
  26. Ref<Surface> texture_surface = LoadTGA(texture_stream.Get());
  27. if (texture_surface == nullptr)
  28. FatalError("Failed to load UI.tga");
  29. mUITexture = mUI->GetRenderer()->CreateTexture(texture_surface);
  30. // Install callback that pops a layer when the deactivate animation finishes
  31. mUI->SetDeactivatedAction([this]() {
  32. mUI->PopLayer();
  33. });
  34. // Don't want to draw any layers that are not active
  35. mUI->SetDrawInactiveLayers(false);
  36. }
  37. UIElement *DebugUI::CreateMenu()
  38. {
  39. mUI->PushLayer();
  40. UIImage *background = new UIImage();
  41. background->SetRelativeX(10);
  42. background->SetRelativeY(10);
  43. background->SetImage(UITexturedQuad(mUITexture, 0, 0, 33, 30, 4, 4, 24, 21));
  44. mUI->Add(background);
  45. UIVerticalStack *stack = new UIVerticalStack();
  46. stack->SetRelativeX(10);
  47. stack->SetRelativeY(10);
  48. stack->SetPaddingRight(10);
  49. stack->SetPaddingBottom(10);
  50. background->Add(stack);
  51. return stack;
  52. }
  53. UIStaticText *DebugUI::CreateStaticText(UIElement *inMenu, const string_view &inText)
  54. {
  55. UIStaticText *text = new UIStaticText();
  56. text->SetText(inText);
  57. text->SetFont(mFont);
  58. inMenu->Add(text);
  59. return text;
  60. }
  61. UITextButton *DebugUI::CreateTextButton(UIElement *inMenu, const string_view &inName, UITextButton::ClickAction inAction)
  62. {
  63. UITextButton *button = new UITextButton();
  64. button->SetText(inName);
  65. button->SetFont(mFont);
  66. button->SetClickAction(inAction);
  67. button->SetTextPadding(0, 24, 0, 0);
  68. button->SetPaddingRight(24);
  69. inMenu->Add(button);
  70. return button;
  71. }
  72. UICheckBox *DebugUI::CreateCheckBox(UIElement *inMenu, const string_view &inName, bool inInitiallyChecked, UICheckBox::ClickAction inAction)
  73. {
  74. UICheckBox *check_box = new UICheckBox();
  75. check_box->SetUncheckedStateQuad(UITexturedQuad(mUITexture, 48, 0, 16, 16));
  76. check_box->SetCheckedStateQuad(UITexturedQuad(mUITexture, 65, 0, 16, 16));
  77. check_box->SetFont(mFont);
  78. check_box->SetText(inName);
  79. check_box->SetClickAction(inAction);
  80. check_box->SetState(inInitiallyChecked? UICheckBox::STATE_CHECKED : UICheckBox::STATE_UNCHECKED);
  81. check_box->SetPaddingRight(24);
  82. inMenu->Add(check_box);
  83. return check_box;
  84. }
  85. UISlider *DebugUI::CreateSlider(UIElement *inMenu, const string_view &inName, float inInitialValue, float inMinValue, float inMaxValue, float inStepValue, UISlider::ValueChangedAction inAction)
  86. {
  87. UIHorizontalStack *horiz = new UIHorizontalStack();
  88. horiz->SetPaddingRight(24);
  89. inMenu->Add(horiz);
  90. UIStaticText *text = new UIStaticText();
  91. text->SetFont(mFont);
  92. text->SetTextPadding(0, 24, 0, 0);
  93. text->SetText(inName);
  94. text->SetPaddingRight(20);
  95. horiz->Add(text);
  96. UISlider *slider = new UISlider();
  97. slider->SetHeight(24);
  98. slider->SetWidth(250);
  99. slider->SetPaddingRight(20);
  100. slider->SetValue(inInitialValue);
  101. slider->SetRange(inMinValue, inMaxValue, inStepValue);
  102. slider->SetValueChangedAction(inAction);
  103. slider->SetSlider(UITexturedQuad(mUITexture, 44, 37, 1, 9));
  104. slider->SetThumb(UITexturedQuad(mUITexture, 31, 32, 11, 19));
  105. horiz->Add(slider);
  106. UIButton *decr_button = new UIButton();
  107. decr_button->SetRepeat(0.5f, 0.2f);
  108. decr_button->SetButtonQuad(UITexturedQuad(mUITexture, 0, 31, 17, 21));
  109. slider->Add(decr_button);
  110. slider->SetDecreaseButton(decr_button);
  111. UIButton *incr_button = new UIButton();
  112. incr_button->SetRepeat(0.5f, 0.2f);
  113. incr_button->SetButtonQuad(UITexturedQuad(mUITexture, 13, 31, 17, 21));
  114. slider->Add(incr_button);
  115. slider->SetIncreaseButton(incr_button);
  116. UIImage *image = new UIImage();
  117. image->SetImage(UITexturedQuad(mUITexture, 34, 0, 13, 24, 36, 2, 9, 20));
  118. horiz->Add(image);
  119. UIStaticText *value = new UIStaticText();
  120. value->SetWidth(75);
  121. value->SetTextPadding(0, 5, 0, 5);
  122. value->SetWrap(true);
  123. value->SetTextAlignment(UIElement::RIGHT);
  124. value->SetFont(mFont);
  125. image->Add(value);
  126. slider->SetStaticText(value);
  127. return slider;
  128. }
  129. UIComboBox *DebugUI::CreateComboBox(UIElement *inMenu, const string_view &inName, const Array<String> &inItems, int inInitialItem, UIComboBox::ItemChangedAction inAction)
  130. {
  131. UIHorizontalStack *horiz = new UIHorizontalStack();
  132. horiz->SetPaddingRight(24);
  133. inMenu->Add(horiz);
  134. UIStaticText *text = new UIStaticText();
  135. text->SetFont(mFont);
  136. text->SetTextPadding(0, 24, 0, 0);
  137. text->SetText(inName);
  138. text->SetPaddingRight(20);
  139. horiz->Add(text);
  140. UIComboBox *combo = new UIComboBox();
  141. combo->SetHeight(24);
  142. combo->SetWidth(250);
  143. combo->SetPaddingRight(20);
  144. combo->SetItems(inItems);
  145. combo->SetCurrentItem(inInitialItem);
  146. combo->SetItemChangedAction(inAction);
  147. horiz->Add(combo);
  148. UIButton *prev_button = new UIButton();
  149. prev_button->SetRepeat(0.5f, 0.2f);
  150. prev_button->SetButtonQuad(UITexturedQuad(mUITexture, 0, 31, 17, 21));
  151. combo->Add(prev_button);
  152. combo->SetPreviousButton(prev_button);
  153. UIButton *next_button = new UIButton();
  154. next_button->SetRepeat(0.5f, 0.2f);
  155. next_button->SetButtonQuad(UITexturedQuad(mUITexture, 13, 31, 17, 21));
  156. combo->Add(next_button);
  157. combo->SetNextButton(next_button);
  158. UIStaticText *value = new UIStaticText();
  159. value->SetTextPadding(0, 5, 0, 5);
  160. value->SetWrap(false);
  161. value->SetTextAlignment(UIElement::CENTER);
  162. value->SetFont(mFont);
  163. combo->Add(value);
  164. combo->SetStaticText(value);
  165. return combo;
  166. }
  167. void DebugUI::ShowMenu(UIElement *inMenu)
  168. {
  169. UIHorizontalStack::sUniformChildWidth(inMenu);
  170. mUI->AutoLayout();
  171. mUI->SwitchToState(UIManager::STATE_ACTIVATING);
  172. }
  173. void DebugUI::BackToMain()
  174. {
  175. while (mUI->GetNumLayers() > 2)
  176. mUI->PopLayer();
  177. }
  178. void DebugUI::ToggleVisibility()
  179. {
  180. if (mUI->GetNumLayers() > 2)
  181. mUI->SwitchToState(UIManager::STATE_DEACTIVATING);
  182. else
  183. mUI->SetVisible(!mUI->IsVisible());
  184. }