47_Typography.as 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Text rendering example.
  2. // Displays text at various sizes, with checkboxes to change the rendering parameters.
  3. #include "Scripts/Utilities/Sample.as"
  4. // Tag used to find all Text elements
  5. String TEXT_TAG = "Typography_text_tag";
  6. // Top-level container for this sample's UI
  7. UIElement@ uielement;
  8. void Start()
  9. {
  10. // Execute the common startup for samples
  11. SampleStart();
  12. // Enable OS cursor
  13. input.mouseVisible = true;
  14. // Load XML file containing default UI style sheet
  15. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  16. // Set the loaded style as default style
  17. ui.root.defaultStyle = style;
  18. // Create a UIElement to hold all our content
  19. // (Don't modify the root directly, as the base Sample class uses it)
  20. uielement = UIElement();
  21. uielement.SetAlignment(HA_CENTER, VA_CENTER);
  22. uielement.SetLayout(LM_VERTICAL, 10, IntRect(20, 40, 20, 40));
  23. ui.root.AddChild(uielement);
  24. // Add some sample text
  25. CreateText();
  26. // Add a checkbox to toggle the background color.
  27. CreateCheckbox("White background", "HandleWhiteBackground")
  28. .checked = false;
  29. // Add a checkbox to toggle SRGB output conversion (if available).
  30. // This will give more correct text output for FreeType fonts, as the FreeType rasterizer
  31. // outputs linear coverage values rather than SRGB values. However, this feature isn't
  32. // available on all platforms.
  33. CreateCheckbox("Graphics::SetSRGB", "HandleSRGB")
  34. .checked = graphics.sRGB;
  35. // Add a checkbox for the global ForceAutoHint setting. This affects character spacing.
  36. CreateCheckbox("UI::SetForceAutoHint", "HandleForceAutoHint")
  37. .checked = ui.forceAutoHint;
  38. // Add a drop-down menu to control the font hinting level.
  39. Array<String> levels = {
  40. "FONT_HINT_LEVEL_NONE",
  41. "FONT_HINT_LEVEL_LIGHT",
  42. "FONT_HINT_LEVEL_NORMAL"
  43. };
  44. CreateMenu("UI::SetFontHintLevel", levels, "HandleFontHintLevel")
  45. .selection = ui.fontHintLevel;
  46. // Add a drop-down menu to control the subpixel threshold.
  47. Array<String> thresholds = {
  48. "0",
  49. "3",
  50. "6",
  51. "9",
  52. "12",
  53. "15",
  54. "18",
  55. "21"
  56. };
  57. CreateMenu("UI::SetFontSubpixelThreshold", thresholds, "HandleFontSubpixel")
  58. .selection = ui.fontSubpixelThreshold / 3;
  59. // Add a drop-down menu to control oversampling.
  60. Array<String> limits = {
  61. "1",
  62. "2",
  63. "3",
  64. "4",
  65. "5",
  66. "6",
  67. "7",
  68. "8"
  69. };
  70. CreateMenu("UI::SetFontOversampling", limits, "HandleFontOversampling")
  71. .selection = ui.fontOversampling - 1;
  72. // Set the mouse mode to use in the sample
  73. SampleInitMouseMode(MM_FREE);
  74. }
  75. void CreateText()
  76. {
  77. UIElement@ container = UIElement();
  78. container.SetAlignment(HA_LEFT, VA_TOP);
  79. container.SetLayout(LM_VERTICAL);
  80. uielement.AddChild(container);
  81. Font@ font = cache.GetResource("Font", "Fonts/BlueHighway.ttf");
  82. for (float size = 1; size <= 18; size += 0.5)
  83. {
  84. Text@ text = Text();
  85. text.text = "The quick brown fox jumps over the lazy dog (" + size + "pt)";
  86. text.SetFont(font, size);
  87. text.AddTag(TEXT_TAG);
  88. container.AddChild(text);
  89. }
  90. }
  91. CheckBox@ CreateCheckbox(String label, String handler)
  92. {
  93. UIElement@ container = UIElement();
  94. container.SetAlignment(HA_LEFT, VA_TOP);
  95. container.SetLayout(LM_HORIZONTAL, 8);
  96. uielement.AddChild(container);
  97. CheckBox@ box = CheckBox();
  98. container.AddChild(box);
  99. box.SetStyleAuto();
  100. Text@ text = Text();
  101. container.AddChild(text);
  102. text.text = label;
  103. text.SetStyleAuto();
  104. text.AddTag(TEXT_TAG);
  105. SubscribeToEvent(box, "Toggled", handler);
  106. return box;
  107. }
  108. DropDownList@ CreateMenu(String label, Array<String> items, String handler)
  109. {
  110. UIElement@ container = UIElement();
  111. container.SetAlignment(HA_LEFT, VA_TOP);
  112. container.SetLayout(LM_HORIZONTAL, 8);
  113. uielement.AddChild(container);
  114. Text@ text = Text();
  115. container.AddChild(text);
  116. text.text = label;
  117. text.SetStyleAuto();
  118. text.AddTag(TEXT_TAG);
  119. DropDownList@ list = DropDownList();
  120. container.AddChild(list);
  121. list.SetStyleAuto();
  122. for (int i = 0; i < items.length; ++i)
  123. {
  124. Text@ t = Text();
  125. list.AddItem(t);
  126. t.text = items[i];
  127. t.SetStyleAuto();
  128. t.minWidth = t.rowWidths[0] + 10;
  129. t.AddTag(TEXT_TAG);
  130. }
  131. text.maxWidth = text.rowWidths[0];
  132. SubscribeToEvent(list, "ItemSelected", handler);
  133. return list;
  134. }
  135. void HandleWhiteBackground(StringHash eventType, VariantMap& eventData)
  136. {
  137. CheckBox@ box = eventData["Element"].GetPtr();
  138. bool checked = box.checked;
  139. Color fg = checked ? Color::BLACK : Color::WHITE;
  140. Color bg = checked ? Color::WHITE : Color::BLACK;
  141. renderer.defaultZone.fogColor = bg;
  142. Array<UIElement@>@ elements = uielement.GetChildrenWithTag(TEXT_TAG, true);
  143. for (int i = 0; i < elements.length; ++i)
  144. {
  145. elements[i].color = fg;
  146. }
  147. }
  148. void HandleForceAutoHint(StringHash eventType, VariantMap& eventData)
  149. {
  150. CheckBox@ box = eventData["Element"].GetPtr();
  151. bool checked = box.checked;
  152. ui.forceAutoHint = checked;
  153. }
  154. void HandleSRGB(StringHash eventType, VariantMap& eventData)
  155. {
  156. CheckBox@ box = eventData["Element"].GetPtr();
  157. bool checked = box.checked;
  158. if (graphics.sRGBWriteSupport)
  159. {
  160. graphics.sRGB = checked;
  161. }
  162. else
  163. {
  164. log.Warning("graphics.sRGBWriteSupport is false");
  165. }
  166. }
  167. void HandleFontHintLevel(StringHash eventType, VariantMap& eventData)
  168. {
  169. DropDownList@ list = eventData["Element"].GetPtr();
  170. int i = list.selection;
  171. ui.fontHintLevel = FontHintLevel(i);
  172. }
  173. void HandleFontSubpixel(StringHash eventType, VariantMap& eventData)
  174. {
  175. DropDownList@ list = eventData["Element"].GetPtr();
  176. int i = list.selection;
  177. ui.fontSubpixelThreshold = i * 3;
  178. }
  179. void HandleFontOversampling(StringHash eventType, VariantMap& eventData)
  180. {
  181. DropDownList@ list = eventData["Element"].GetPtr();
  182. int i = list.selection;
  183. ui.fontOversampling = i + 1;
  184. }
  185. // Create XML patch instructions for screen joystick layout specific to this sample app
  186. String patchInstructions =
  187. "<patch>" +
  188. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  189. " <attribute name=\"Is Visible\" value=\"false\" />" +
  190. " </add>" +
  191. "</patch>";