Typography.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Core/ProcessUtils.h>
  24. #include <Urho3D/Graphics/RenderPath.h>
  25. #include <Urho3D/Graphics/Zone.h>
  26. #include <Urho3D/Input/Input.h>
  27. #include <Urho3D/UI/CheckBox.h>
  28. #include <Urho3D/UI/DropDownList.h>
  29. #include <Urho3D/UI/Font.h>
  30. #include <Urho3D/UI/Text.h>
  31. #include <Urho3D/UI/UI.h>
  32. #include <Urho3D/UI/UIEvents.h>
  33. #include "Typography.h"
  34. #include <Urho3D/DebugNew.h>
  35. // Expands to this example's entry-point
  36. URHO3D_DEFINE_APPLICATION_MAIN(Typography)
  37. namespace
  38. {
  39. // Tag used to find all Text elements
  40. const String TEXT_TAG("Typography_text_tag");
  41. }
  42. Typography::Typography(Context* context) :
  43. Sample(context)
  44. {
  45. }
  46. void Typography::Start()
  47. {
  48. // Execute base class startup
  49. Sample::Start();
  50. // Enable OS cursor
  51. GetSubsystem<Input>()->SetMouseVisible(true);
  52. // Load XML file containing default UI style sheet
  53. ResourceCache* cache = GetSubsystem<ResourceCache>();
  54. XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  55. // Set the loaded style as default style
  56. UI* ui = GetSubsystem<UI>();
  57. UIElement* root = ui->GetRoot();
  58. root->SetDefaultStyle(style);
  59. // Create a UIElement to hold all our content
  60. // (Don't modify the root directly, as the base Sample class uses it)
  61. uielement_ = new UIElement(context_);
  62. uielement_->SetAlignment(HA_CENTER, VA_CENTER);
  63. uielement_->SetLayout(LM_VERTICAL, 10, IntRect(20, 40, 20, 40));
  64. root->AddChild(uielement_);
  65. // Add some sample text.
  66. CreateText();
  67. // Add a checkbox to toggle the background color.
  68. CreateCheckbox("White background", URHO3D_HANDLER(Typography, HandleWhiteBackground))
  69. ->SetChecked(false);
  70. // Add a checkbox to toggle SRGB output conversion (if available).
  71. // This will give more correct text output for FreeType fonts, as the FreeType rasterizer
  72. // outputs linear coverage values rather than SRGB values. However, this feature isn't
  73. // available on all platforms.
  74. CreateCheckbox("Graphics::SetSRGB", URHO3D_HANDLER(Typography, HandleSRGB))
  75. ->SetChecked(GetSubsystem<Graphics>()->GetSRGB());
  76. // Add a checkbox for the global ForceAutoHint setting. This affects character spacing.
  77. CreateCheckbox("UI::SetForceAutoHint", URHO3D_HANDLER(Typography, HandleForceAutoHint))
  78. ->SetChecked(ui->GetForceAutoHint());
  79. // Add a drop-down menu to control the font hinting level.
  80. const char* levels[] = {
  81. "FONT_HINT_LEVEL_NONE",
  82. "FONT_HINT_LEVEL_LIGHT",
  83. "FONT_HINT_LEVEL_NORMAL",
  84. NULL
  85. };
  86. CreateMenu("UI::SetFontHintLevel", levels, URHO3D_HANDLER(Typography, HandleFontHintLevel))
  87. ->SetSelection(ui->GetFontHintLevel());
  88. // Add a drop-down menu to control the subpixel threshold.
  89. const char* thresholds[] = {
  90. "0",
  91. "3",
  92. "6",
  93. "9",
  94. "12",
  95. "15",
  96. "18",
  97. "21",
  98. NULL
  99. };
  100. CreateMenu("UI::SetFontSubpixelThreshold", thresholds, URHO3D_HANDLER(Typography, HandleFontSubpixel))
  101. ->SetSelection(ui->GetFontSubpixelThreshold() / 3);
  102. // Add a drop-down menu to control oversampling.
  103. const char* limits[] = {
  104. "1",
  105. "2",
  106. "3",
  107. "4",
  108. "5",
  109. "6",
  110. "7",
  111. "8",
  112. NULL
  113. };
  114. CreateMenu("UI::SetFontOversampling", limits, URHO3D_HANDLER(Typography, HandleFontOversampling))
  115. ->SetSelection(ui->GetFontOversampling() - 1);
  116. // Set the mouse mode to use in the sample
  117. Sample::InitMouseMode(MM_FREE);
  118. }
  119. void Typography::CreateText()
  120. {
  121. SharedPtr<UIElement> container(new UIElement(context_));
  122. container->SetAlignment(HA_LEFT, VA_TOP);
  123. container->SetLayout(LM_VERTICAL);
  124. uielement_->AddChild(container);
  125. ResourceCache* cache = GetSubsystem<ResourceCache>();
  126. Font* font = cache->GetResource<Font>("Fonts/BlueHighway.ttf");
  127. for (float size = 1; size <= 18; size += 0.5)
  128. {
  129. SharedPtr<Text> text(new Text(context_));
  130. text->SetText(String("The quick brown fox jumps over the lazy dog (") + String(size) + String("pt)"));
  131. text->SetFont(font, size);
  132. text->AddTag(TEXT_TAG);
  133. container->AddChild(text);
  134. }
  135. }
  136. SharedPtr<CheckBox> Typography::CreateCheckbox(const String& label, EventHandler* handler)
  137. {
  138. SharedPtr<UIElement> container(new UIElement(context_));
  139. container->SetAlignment(HA_LEFT, VA_TOP);
  140. container->SetLayout(LM_HORIZONTAL, 8);
  141. uielement_->AddChild(container);
  142. SharedPtr<CheckBox> box(new CheckBox(context_));
  143. container->AddChild(box);
  144. box->SetStyleAuto();
  145. SharedPtr<Text> text(new Text(context_));
  146. container->AddChild(text);
  147. text->SetText(label);
  148. text->SetStyleAuto();
  149. text->AddTag(TEXT_TAG);
  150. SubscribeToEvent(box, E_TOGGLED, handler);
  151. return box;
  152. }
  153. SharedPtr<DropDownList> Typography::CreateMenu(const String& label, const char** items, EventHandler* handler)
  154. {
  155. SharedPtr<UIElement> container(new UIElement(context_));
  156. container->SetAlignment(HA_LEFT, VA_TOP);
  157. container->SetLayout(LM_HORIZONTAL, 8);
  158. uielement_->AddChild(container);
  159. SharedPtr<Text> text(new Text(context_));
  160. container->AddChild(text);
  161. text->SetText(label);
  162. text->SetStyleAuto();
  163. text->AddTag(TEXT_TAG);
  164. SharedPtr<DropDownList> list(new DropDownList(context_));
  165. container->AddChild(list);
  166. list->SetStyleAuto();
  167. for (int i = 0; items[i]; ++i)
  168. {
  169. SharedPtr<Text> item(new Text(context_));
  170. list->AddItem(item);
  171. item->SetText(items[i]);
  172. item->SetStyleAuto();
  173. item->SetMinWidth(item->GetRowWidth(0) + 10);
  174. item->AddTag(TEXT_TAG);
  175. }
  176. text->SetMaxWidth(text->GetRowWidth(0));
  177. SubscribeToEvent(list, E_ITEMSELECTED, handler);
  178. return list;
  179. }
  180. void Typography::HandleWhiteBackground(StringHash eventType, VariantMap& eventData)
  181. {
  182. CheckBox* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  183. bool checked = box->IsChecked();
  184. Color fg = checked ? Color::BLACK : Color::WHITE;
  185. Color bg = checked ? Color::WHITE : Color::BLACK;
  186. Renderer* renderer = GetSubsystem<Renderer>();
  187. Zone* zone = renderer->GetDefaultZone();
  188. zone->SetFogColor(bg);
  189. PODVector<UIElement*> text = uielement_->GetChildrenWithTag(TEXT_TAG, true);
  190. for (int i = 0; i < text.Size(); i++)
  191. {
  192. text[i]->SetColor(fg);
  193. }
  194. }
  195. void Typography::HandleForceAutoHint(StringHash eventType, VariantMap& eventData)
  196. {
  197. CheckBox* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  198. bool checked = box->IsChecked();
  199. GetSubsystem<UI>()->SetForceAutoHint(checked);
  200. }
  201. void Typography::HandleSRGB(StringHash eventType, VariantMap& eventData)
  202. {
  203. CheckBox* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
  204. bool checked = box->IsChecked();
  205. Graphics* graphics = GetSubsystem<Graphics>();
  206. if (graphics->GetSRGBWriteSupport())
  207. {
  208. graphics->SetSRGB(checked);
  209. }
  210. else
  211. {
  212. URHO3D_LOGWARNING("Graphics::GetSRGBWriteSupport returned false");
  213. // Note: PostProcess/GammaCorrection.xml implements SRGB conversion.
  214. // However, post-processing filters don't affect the UI layer.
  215. }
  216. }
  217. void Typography::HandleFontHintLevel(StringHash eventType, VariantMap& eventData)
  218. {
  219. DropDownList* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  220. unsigned i = list->GetSelection();
  221. GetSubsystem<UI>()->SetFontHintLevel((FontHintLevel)i);
  222. }
  223. void Typography::HandleFontSubpixel(StringHash eventType, VariantMap& eventData)
  224. {
  225. DropDownList* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  226. unsigned i = list->GetSelection();
  227. GetSubsystem<UI>()->SetFontSubpixelThreshold(i * 3);
  228. }
  229. void Typography::HandleFontOversampling(StringHash eventType, VariantMap& eventData)
  230. {
  231. DropDownList* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
  232. unsigned i = list->GetSelection();
  233. GetSubsystem<UI>()->SetFontOversampling(i + 1);
  234. }