Typography.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/ProcessUtils.h>
  5. #include <Urho3D/Graphics/RenderPath.h>
  6. #include <Urho3D/Graphics/Zone.h>
  7. #include <Urho3D/Input/Input.h>
  8. #include <Urho3D/UI/CheckBox.h>
  9. #include <Urho3D/UI/DropDownList.h>
  10. #include <Urho3D/UI/Font.h>
  11. #include <Urho3D/UI/Text.h>
  12. #include <Urho3D/UI/UI.h>
  13. #include <Urho3D/UI/UIEvents.h>
  14. #include "Typography.h"
  15. #include <Urho3D/DebugNew.h>
  16. // Expands to this example's entry-point
  17. URHO3D_DEFINE_APPLICATION_MAIN(Typography)
  18. namespace
  19. {
  20. // Tag used to find all Text elements
  21. const char* TEXT_TAG = "Typography_text_tag";
  22. }
  23. Typography::Typography(Context* context) :
  24. Sample(context)
  25. {
  26. }
  27. void Typography::Start()
  28. {
  29. // Execute base class startup
  30. Sample::Start();
  31. // Enable OS cursor
  32. GetSubsystem<Input>()->SetMouseVisible(true);
  33. // Load XML file containing default UI style sheet
  34. auto* cache = GetSubsystem<ResourceCache>();
  35. auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  36. // Set the loaded style as default style
  37. auto* ui = GetSubsystem<UI>();
  38. UIElement* root = ui->GetRoot();
  39. root->SetDefaultStyle(style);
  40. // Create a UIElement to hold all our content
  41. // (Don't modify the root directly, as the base Sample class uses it)
  42. uielement_ = new UIElement(context_);
  43. uielement_->SetAlignment(HA_CENTER, VA_CENTER);
  44. uielement_->SetLayout(LM_VERTICAL, 10, IntRect(20, 40, 20, 40));
  45. root->AddChild(uielement_);
  46. // Add some sample text.
  47. CreateText();
  48. // Add a checkbox to toggle the background color.
  49. CreateCheckbox("White background", URHO3D_HANDLER(Typography, HandleWhiteBackground))
  50. ->SetChecked(false);
  51. // Add a checkbox to toggle SRGB output conversion (if available).
  52. // This will give more correct text output for FreeType fonts, as the FreeType rasterizer
  53. // outputs linear coverage values rather than SRGB values. However, this feature isn't
  54. // available on all platforms.
  55. CreateCheckbox("Graphics::SetSRGB", URHO3D_HANDLER(Typography, HandleSRGB))
  56. ->SetChecked(GetSubsystem<Graphics>()->GetSRGB());
  57. // Add a checkbox for the global ForceAutoHint setting. This affects character spacing.
  58. CreateCheckbox("UI::SetForceAutoHint", URHO3D_HANDLER(Typography, HandleForceAutoHint))
  59. ->SetChecked(ui->GetForceAutoHint());
  60. // Add a drop-down menu to control the font hinting level.
  61. const char* levels[] = {
  62. "FONT_HINT_LEVEL_NONE",
  63. "FONT_HINT_LEVEL_LIGHT",
  64. "FONT_HINT_LEVEL_NORMAL",
  65. nullptr
  66. };
  67. CreateMenu("UI::SetFontHintLevel", levels, URHO3D_HANDLER(Typography, HandleFontHintLevel))
  68. ->SetSelection(ui->GetFontHintLevel());
  69. // Add a drop-down menu to control the subpixel threshold.
  70. const char* thresholds[] = {
  71. "0",
  72. "3",
  73. "6",
  74. "9",
  75. "12",
  76. "15",
  77. "18",
  78. "21",
  79. nullptr
  80. };
  81. CreateMenu("UI::SetFontSubpixelThreshold", thresholds, URHO3D_HANDLER(Typography, HandleFontSubpixel))
  82. ->SetSelection(ui->GetFontSubpixelThreshold() / 3);
  83. // Add a drop-down menu to control oversampling.
  84. const char* limits[] = {
  85. "1",
  86. "2",
  87. "3",
  88. "4",
  89. "5",
  90. "6",
  91. "7",
  92. "8",
  93. nullptr
  94. };
  95. CreateMenu("UI::SetFontOversampling", limits, URHO3D_HANDLER(Typography, HandleFontOversampling))
  96. ->SetSelection(ui->GetFontOversampling() - 1);
  97. // Set the mouse mode to use in the sample
  98. Sample::InitMouseMode(MM_FREE);
  99. }
  100. void Typography::CreateText()
  101. {
  102. SharedPtr<UIElement> container(new UIElement(context_));
  103. container->SetAlignment(HA_LEFT, VA_TOP);
  104. container->SetLayout(LM_VERTICAL);
  105. uielement_->AddChild(container);
  106. auto* cache = GetSubsystem<ResourceCache>();
  107. auto* font = cache->GetResource<Font>("Fonts/BlueHighway.ttf");
  108. for (auto size2x = 2; size2x <= 36; ++size2x)
  109. {
  110. auto size = size2x / 2.f;
  111. SharedPtr<Text> text(new Text(context_));
  112. text->SetText(String("The quick brown fox jumps over the lazy dog (") + String(size) + String("pt)"));
  113. text->SetFont(font, size);
  114. text->AddTag(TEXT_TAG);
  115. container->AddChild(text);
  116. }
  117. }
  118. SharedPtr<CheckBox> Typography::CreateCheckbox(const String& label, EventHandler* handler)
  119. {
  120. SharedPtr<UIElement> container(new UIElement(context_));
  121. container->SetAlignment(HA_LEFT, VA_TOP);
  122. container->SetLayout(LM_HORIZONTAL, 8);
  123. uielement_->AddChild(container);
  124. SharedPtr<CheckBox> box(new CheckBox(context_));
  125. container->AddChild(box);
  126. box->SetStyleAuto();
  127. SharedPtr<Text> text(new Text(context_));
  128. container->AddChild(text);
  129. text->SetText(label);
  130. text->SetStyleAuto();
  131. text->AddTag(TEXT_TAG);
  132. SubscribeToEvent(box, E_TOGGLED, handler);
  133. return box;
  134. }
  135. SharedPtr<DropDownList> Typography::CreateMenu(const String& label, const char** items, EventHandler* handler)
  136. {
  137. SharedPtr<UIElement> container(new UIElement(context_));
  138. container->SetAlignment(HA_LEFT, VA_TOP);
  139. container->SetLayout(LM_HORIZONTAL, 8);
  140. uielement_->AddChild(container);
  141. SharedPtr<Text> text(new Text(context_));
  142. container->AddChild(text);
  143. text->SetText(label);
  144. text->SetStyleAuto();
  145. text->AddTag(TEXT_TAG);
  146. SharedPtr<DropDownList> list(new DropDownList(context_));
  147. container->AddChild(list);
  148. list->SetStyleAuto();
  149. for (int i = 0; items[i]; ++i)
  150. {
  151. SharedPtr<Text> item(new Text(context_));
  152. list->AddItem(item);
  153. item->SetText(items[i]);
  154. item->SetStyleAuto();
  155. item->SetMinWidth(item->GetRowWidth(0) + 10);
  156. item->AddTag(TEXT_TAG);
  157. }
  158. text->SetMaxWidth(text->GetRowWidth(0));
  159. SubscribeToEvent(list, E_ITEMSELECTED, handler);
  160. return list;
  161. }
  162. void Typography::HandleWhiteBackground(StringHash eventType, VariantMap& eventData)
  163. {
  164. auto* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  165. bool checked = box->IsChecked();
  166. Color fg = checked ? Color::BLACK : Color::WHITE;
  167. Color bg = checked ? Color::WHITE : Color::BLACK;
  168. auto* renderer = GetSubsystem<Renderer>();
  169. Zone* zone = renderer->GetDefaultZone();
  170. zone->SetFogColor(bg);
  171. Vector<UIElement*> text = uielement_->GetChildrenWithTag(TEXT_TAG, true);
  172. for (int i = 0; i < text.Size(); i++)
  173. {
  174. text[i]->SetColor(fg);
  175. }
  176. }
  177. void Typography::HandleForceAutoHint(StringHash eventType, VariantMap& eventData)
  178. {
  179. auto* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  180. bool checked = box->IsChecked();
  181. GetSubsystem<UI>()->SetForceAutoHint(checked);
  182. }
  183. void Typography::HandleSRGB(StringHash eventType, VariantMap& eventData)
  184. {
  185. auto* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  186. bool checked = box->IsChecked();
  187. auto* graphics = GetSubsystem<Graphics>();
  188. if (graphics->GetSRGBWriteSupport())
  189. {
  190. graphics->SetSRGB(checked);
  191. }
  192. else
  193. {
  194. URHO3D_LOGWARNING("Graphics::GetSRGBWriteSupport returned false");
  195. // Note: PostProcess/GammaCorrection.xml implements SRGB conversion.
  196. // However, post-processing filters don't affect the UI layer.
  197. }
  198. }
  199. void Typography::HandleFontHintLevel(StringHash eventType, VariantMap& eventData)
  200. {
  201. auto* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  202. unsigned i = list->GetSelection();
  203. GetSubsystem<UI>()->SetFontHintLevel((FontHintLevel)i);
  204. }
  205. void Typography::HandleFontSubpixel(StringHash eventType, VariantMap& eventData)
  206. {
  207. auto* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  208. unsigned i = list->GetSelection();
  209. GetSubsystem<UI>()->SetFontSubpixelThreshold(i * 3);
  210. }
  211. void Typography::HandleFontOversampling(StringHash eventType, VariantMap& eventData)
  212. {
  213. auto* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  214. unsigned i = list->GetSelection();
  215. GetSubsystem<UI>()->SetFontOversampling(i + 1);
  216. }