DebugUI.cpp 6.2 KB

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