DebugUI.cpp 6.3 KB

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