UIController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // ----------------------------------------------------------------------
  2. // File: UIController
  3. // Organisation: Virtence GmbH
  4. // Department: Simulation Development
  5. // Copyright: © 2014 Virtence GmbH. All rights reserved
  6. // Author: Silvio Lange (silvio.lange@virtence.com)
  7. // ----------------------------------------------------------------------
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using System.Collections.Generic;
  11. namespace Virtence.VText.Demo {
  12. /// <summary>
  13. /// control the main ui in the start scene
  14. /// </summary>
  15. public class UIController : MonoBehaviour
  16. {
  17. #region EXPOSED
  18. [Tooltip("The wrapper for the VTextInterface")]
  19. public VtextHandler VTextController; // the wrapper for the VTextInterface
  20. //[Tooltip("The dropdown which holds the available fonts")]
  21. //public Dropdown FontDropdown; // the dropdown which holds the available fonts
  22. [Header("Common")]
  23. [Tooltip("The label which shows the current font name")]
  24. public Text FontNameLabel; // the label which shows the current font name
  25. [Tooltip("The slider which shows the bevel value")]
  26. public Slider SizeSlider; // the slider which shows the size value
  27. [Tooltip("The slider which shows the bevel value")]
  28. public Slider DepthSlider; // the slider which shows the depth value
  29. [Tooltip("The slider which shows the bevel value")]
  30. public Slider BevelSlider; // the slider which shows the bevel value
  31. [Header("Justify")]
  32. [Tooltip("The toggle button for align the text to the left")]
  33. public Toggle MajorModeLeftToggle; // the toggle button for align the text to the left
  34. [Tooltip("The toggle button for align the text to the center")]
  35. public Toggle MajorModeCenterToggle; // the toggle button for align the text to the center
  36. [Tooltip("The toggle button for align the text to the right")]
  37. public Toggle MajorModeRightToggle; // the toggle button for align the text to the right
  38. [Tooltip("The toggle button for align the text in block style")]
  39. public Toggle MajorModeBlockToggle; // the toggle button for align the text to the right
  40. [Header("Lightprobes")]
  41. [Tooltip("The toggle button for using lightprobes")]
  42. public Toggle UseLightProbesToggle; // the toggle button for align the text to the right
  43. #endregion // EXPOSED
  44. #region CONSTANTS
  45. #endregion // CONSTANTS
  46. #region FIELDS
  47. private int _currentFontIndex; // the index of the current used font
  48. #endregion // FIELDS
  49. #region PROPERTIES
  50. #endregion // PROPERTIES
  51. #region METHODS
  52. // initialize
  53. void Awake()
  54. {
  55. VTextController.FontNameChanged += OnFontNameChanged;;
  56. VTextController.SizeValueChanged += OnSizeChanged;
  57. VTextController.DepthValueChanged += OnDepthChanged;
  58. VTextController.BevelValueChanged += OnBevelChanged;
  59. VTextController.MajorValueChanged += OnMajorLayoutChanged;
  60. }
  61. /// <summary>
  62. /// initialize
  63. /// </summary>
  64. void Start() {
  65. //FontDropdown.ClearOptions();
  66. //FontDropdown.AddOptions(VTextInterface.GetAvailableFonts());
  67. }
  68. /// <summary>
  69. /// Selects the next font.
  70. /// </summary>
  71. public void SelectNextFont() {
  72. SetFontByIndex(_currentFontIndex + 1);
  73. }
  74. /// <summary>
  75. /// Selects the previous font.
  76. /// </summary>
  77. public void SelectPreviousFont() {
  78. SetFontByIndex(_currentFontIndex - 1);
  79. }
  80. /// <summary>
  81. /// set the depth of the VText
  82. /// </summary>
  83. /// <param name="value">Value.</param>
  84. public void SetSize(float value) {
  85. VTextController.SetSize(Mathf.Clamp01(value));
  86. }
  87. /// <summary>
  88. /// set the depth of the VText
  89. /// </summary>
  90. /// <param name="value">Value.</param>
  91. public void SetDepth(float value) {
  92. VTextController.SetDepth(Mathf.Clamp01(value));
  93. }
  94. /// <summary>
  95. /// set the depth of the VText
  96. /// </summary>
  97. /// <param name="value">Value.</param>
  98. public void SetBevel(float value) {
  99. VTextController.SetBevel(Mathf.Clamp01(value));
  100. }
  101. /// <summary>
  102. /// Sets the major layout to Start
  103. /// </summary>
  104. public void SetMajorLayoutLeft(bool enabled) {
  105. if (enabled) {
  106. VTextController.SetAlignment(VTextLayout.align.Start);
  107. }
  108. }
  109. /// <summary>
  110. /// Sets the major layout to Center
  111. /// </summary>
  112. public void SetMajorLayoutCenter(bool enabled) {
  113. if (enabled) {
  114. VTextController.SetAlignment(VTextLayout.align.Center);
  115. }
  116. }
  117. /// <summary>
  118. /// Sets the major layout to End
  119. /// </summary>
  120. public void SetMajorLayoutRight(bool enabled) {
  121. if (enabled) {
  122. VTextController.SetAlignment(VTextLayout.align.End);
  123. }
  124. }
  125. /// <summary>
  126. /// Sets the major layout to block
  127. /// </summary>
  128. public void SetMajorLayoutBlock(bool enabled) {
  129. if (enabled) {
  130. VTextController.SetAlignment(VTextLayout.align.Block);
  131. }
  132. }
  133. /// <summary>
  134. /// set the font by specifying an index (the index int the AvailableFonts)
  135. /// </summary>
  136. /// <param name="index">Index.</param>
  137. private void SetFontByIndex(int index) {
  138. List<string> availableFonts = VTextInterface.GetAvailableFonts();
  139. _currentFontIndex = index;
  140. if (index < 0) {
  141. _currentFontIndex = availableFonts.Count - 1;
  142. }
  143. if (index >= availableFonts.Count) {
  144. _currentFontIndex = 0;
  145. }
  146. VTextController.SetFont(availableFonts[_currentFontIndex]);
  147. }
  148. /// <summary>
  149. /// enable or disable the usage of lightprobes
  150. /// </summary>
  151. /// <returns><c>true</c>, if light probes was enabled, <c>false</c> otherwise.</returns>
  152. /// <param name="enable">If set to <c>true</c> enable.</param>
  153. public void EnableLightProbes(bool enable) {
  154. VTextController.SetLightProbes(enable);
  155. }
  156. #endregion // METHODS
  157. #region EVENTHANDLERS
  158. /// <summary>
  159. /// this is called if the current used fontname changes
  160. /// </summary>
  161. /// <param name="sender">Sender.</param>
  162. /// <param name="e">E.</param>
  163. void OnFontNameChanged(object sender, GenericEventArgs<string> e)
  164. {
  165. _currentFontIndex = VTextInterface.GetAvailableFonts().IndexOf(e.Value);
  166. FontNameLabel.text = string.Format("{0} ({1}/{2})", e.Value, _currentFontIndex + 1, VTextInterface.GetAvailableFonts().Count);
  167. }
  168. /// <summary>
  169. /// this is called if the size value of the vtext object changes
  170. /// </summary>
  171. /// <param name="sender">Sender.</param>
  172. /// <param name="e">E.</param>
  173. void OnSizeChanged(object sender, GenericEventArgs<float> e)
  174. {
  175. SizeSlider.value = e.Value;
  176. }
  177. /// <summary>
  178. /// this is called if the depth value of the vtext object changes
  179. /// </summary>
  180. /// <param name="sender">Sender.</param>
  181. /// <param name="e">E.</param>
  182. void OnDepthChanged(object sender, GenericEventArgs<float> e)
  183. {
  184. DepthSlider.value = e.Value;
  185. BevelSlider.interactable = (e.Value > Mathf.Epsilon);
  186. }
  187. /// <summary>
  188. /// this is called if the bevel value of the vtext object changes
  189. /// </summary>
  190. /// <param name="sender">Sender.</param>
  191. /// <param name="e">E.</param>
  192. void OnBevelChanged(object sender, GenericEventArgs<float> e)
  193. {
  194. BevelSlider.value = e.Value;
  195. }
  196. /// <summary>
  197. /// this is called if the major layout mode changes
  198. /// </summary>
  199. /// <param name="sender">Sender.</param>
  200. /// <param name="e">E.</param>
  201. void OnMajorLayoutChanged(object sender, GenericEventArgs<VTextLayout.align> e)
  202. {
  203. switch (e.Value) {
  204. case VTextLayout.align.Start:
  205. case VTextLayout.align.Base:
  206. MajorModeLeftToggle.isOn = true;
  207. break;
  208. case VTextLayout.align.Center:
  209. MajorModeCenterToggle.isOn = true;
  210. break;
  211. case VTextLayout.align.End:
  212. MajorModeRightToggle.isOn = true;
  213. break;
  214. case VTextLayout.align.Block:
  215. MajorModeBlockToggle.isOn = true;
  216. break;
  217. };
  218. }
  219. #endregion // EVENTHANDLERS
  220. }
  221. }