2
0

DebugUI.cpp 6.2 KB

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