VtextHandler.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. namespace Virtence.VText.Demo {
  7. /// <summary>
  8. ///handle vtext changes in the start scene.
  9. /// </summary>
  10. public class VtextHandler : MonoBehaviour {
  11. #region EVENTS
  12. public event EventHandler<GenericEventArgs<string>> FontNameChanged; // this will be raised if the used fontname changes
  13. public event EventHandler<GenericEventArgs<float>> SizeValueChanged; // this will be raised if the size value of the VTextInterface changes
  14. public event EventHandler<GenericEventArgs<float>> DepthValueChanged; // this will be raised if the size value of the VTextInterface changes
  15. public event EventHandler<GenericEventArgs<float>> BevelValueChanged; // this will be raised if the bevel value of the VTextInterface changes
  16. public event EventHandler<GenericEventArgs<VTextLayout.align>> MajorValueChanged; // this will be raised if the major alignment value of the VTextInterface changes
  17. public event EventHandler<GenericEventArgs<bool>> UseLightProbesChanged; // this will be raised if the usage of lightprobes of the VTextInterface changes
  18. #endregion
  19. public VTextInterface[] vti_time = null;
  20. public VTextInterface vti_textOptions = null;
  21. public VTextInterface vti_textured = null;
  22. //heading
  23. private int old_headingValue;
  24. // size
  25. private float _minSize = 0.45f;
  26. private float _maxSize = 1.0f;
  27. //depth
  28. private float _minDepth = 0.0f;
  29. private float _maxDepth = 3.0f;
  30. //bevel
  31. private float _minBevel = 0.0f;
  32. private float _maxBevel = 0.1f;
  33. /// <summary>
  34. /// Awake this instance.
  35. /// </summary>
  36. void Start() {
  37. if (Loom.Current == null)
  38. Loom.Initialize();
  39. vti_textOptions.layout.SizeChanged += OnSizeChanged;
  40. vti_textOptions.layout.MajorChanged += OnMajorLayoutChanged;
  41. vti_textOptions.parameter.BevelChanged += OnBevelChanged;
  42. vti_textOptions.parameter.DepthChanged += OnDepthChanged;
  43. vti_textOptions.parameter.UseLightProbesChanged += OnUseLightProbesChanged;
  44. vti_textOptions.parameter.FontNameChanged += OnFontNameChanged;
  45. foreach (VTextInterface vi in vti_time) {
  46. vi.parameter.UseLightProbes = true;
  47. }
  48. vti_textOptions.parameter.UseLightProbes = true;
  49. vti_textured.parameter.UseLightProbes = true;
  50. // init alignment
  51. old_headingValue = (int) VTextLayout.align.Center;
  52. SetAlignment(VTextLayout.align.Center);
  53. SetSize(0.4f); // init size
  54. SetDepth(0.1f); // init depth
  55. SetBevel(0.6f); // init bevel
  56. SetFont(vti_textOptions.parameter.Fontname); // init font type
  57. if (FontNameChanged != null) {
  58. FontNameChanged.Invoke(this, new GenericEventArgs<string>(vti_textOptions.parameter.Fontname));
  59. }
  60. }
  61. /// <summary>
  62. /// Enable or disable light probes both VText odbjects in scene
  63. /// </summary>
  64. /// <value>The handle lightprobes.</value>
  65. void MessageLightprobes(bool lp) {
  66. if (vti_time != null) {
  67. foreach(VTextInterface vi in vti_time){
  68. vi.parameter.UseLightProbes = lp;
  69. }
  70. vti_textOptions.parameter.UseLightProbes = lp;
  71. vti_textured.parameter.UseLightProbes = lp;
  72. }
  73. }
  74. /// <summary>
  75. /// enable or disable the usage of lightprobes for the vtext objects
  76. /// </summary>
  77. /// <param name="enableLightProbes">If set to <c>true</c> enable light probes.</param>
  78. public void SetLightProbes(bool enableLightProbes) {
  79. foreach (VTextInterface vi in vti_time) {
  80. vi.parameter.UseLightProbes = enableLightProbes;
  81. }
  82. vti_textOptions.parameter.UseLightProbes = enableLightProbes;
  83. vti_textured.parameter.UseLightProbes = enableLightProbes;
  84. }
  85. /// <summary>
  86. /// change font of vti_textOptions
  87. /// </summary>
  88. public void SetFont(string fontName) {
  89. vti_textOptions.parameter.Fontname = fontName;
  90. // TransformTxt(vti_textOptions.layout.Major);
  91. }
  92. /// <summary>
  93. /// change size of vti_textOptions
  94. /// </summary>
  95. public void SetSize(float sizeValue) {
  96. if (vti_textOptions != null) {
  97. vti_textOptions.layout.Size = _minSize + sizeValue * (_maxSize - _minSize);
  98. }
  99. }
  100. /// <summary>
  101. /// change depth of vti_textOptions
  102. /// </summary>
  103. public void SetDepth(float depthValue) {
  104. if (vti_textOptions != null) {
  105. vti_textOptions.parameter.Depth = _minDepth + depthValue * (_maxDepth - _minDepth);
  106. }
  107. }
  108. /// <summary>
  109. /// change bevel of vti_textOptions
  110. /// </summary>
  111. public void SetBevel(float bevelValue) {
  112. if (vti_textOptions != null) {
  113. vti_textOptions.parameter.Bevel = _minBevel + bevelValue * (_maxBevel - _minBevel);
  114. }
  115. }
  116. /// <summary>
  117. /// Sets the alignment of the vti_textOptions text
  118. /// </summary>
  119. /// <param name="alignment">Alignment.</param>
  120. public void SetAlignment(VTextLayout.align alignment) {
  121. vti_textOptions.layout.Major = alignment;
  122. TransformTxt(alignment);
  123. }
  124. /// <summary>
  125. /// change transformation of vti_textOptions in dependence of the current alignment to avoid shifts
  126. /// </summary>
  127. /// <param name="layout">Layout.</param>
  128. void TransformTxt(VTextLayout.align alignment) {
  129. float width = vti_textOptions.GetBounds().size.x;
  130. switch (alignment) {
  131. case VTextLayout.align.Base:
  132. case VTextLayout.align.Start:
  133. case VTextLayout.align.Block:
  134. vti_textOptions.transform.localPosition = new Vector3(-width * 0.25f, vti_textOptions.transform.localPosition.y, vti_textOptions.transform.localPosition.z);
  135. break;
  136. case VTextLayout.align.Center:
  137. vti_textOptions.transform.localPosition = Vector3.zero;
  138. break;
  139. case VTextLayout.align.End:
  140. vti_textOptions.transform.localPosition = new Vector3(width * 0.25f, vti_textOptions.transform.localPosition.y, vti_textOptions.transform.localPosition.z);
  141. break;
  142. }
  143. }
  144. #region EVENT HANDLERS
  145. /// <summary>
  146. /// this is called if the size value of the vtext interface changes
  147. /// </summary>
  148. /// <param name="sender">Sender.</param>
  149. /// <param name="e">E.</param>
  150. void OnSizeChanged (object sender, GenericEventArgs<float> e)
  151. {
  152. if (SizeValueChanged != null) {
  153. float normalizedValue = (e.Value - _minSize) / (_maxSize - _minSize);
  154. SizeValueChanged.Invoke(this, new GenericEventArgs<float>(normalizedValue));
  155. }
  156. }
  157. /// <summary>
  158. /// this is called if the depth value of the vtext interface changes
  159. /// </summary>
  160. /// <param name="sender">Sender.</param>
  161. /// <param name="e">E.</param>
  162. void OnDepthChanged (object sender, GenericEventArgs<float> e)
  163. {
  164. if (DepthValueChanged != null) {
  165. float normalizedValue = (e.Value - _minDepth) / (_maxDepth - _minDepth);
  166. DepthValueChanged.Invoke(this, new GenericEventArgs<float>(normalizedValue));
  167. }
  168. }
  169. /// <summary>
  170. /// this is called if the bevel value of the vtext interface changes
  171. /// </summary>
  172. /// <param name="sender">Sender.</param>
  173. /// <param name="e">E.</param>
  174. void OnBevelChanged (object sender, GenericEventArgs<float> e)
  175. {
  176. if (BevelValueChanged != null) {
  177. float normalizedValue = (e.Value - _minBevel) / (_maxBevel - _minBevel);
  178. BevelValueChanged.Invoke(this, new GenericEventArgs<float>(normalizedValue));
  179. }
  180. }
  181. /// <summary>
  182. /// this is called if the major layout changes
  183. /// </summary>
  184. /// <param name="sender">Sender.</param>
  185. /// <param name="e">E.</param>
  186. void OnMajorLayoutChanged (object sender, GenericEventArgs<VTextLayout.align> e)
  187. {
  188. if (MajorValueChanged != null) {
  189. MajorValueChanged.Invoke(this, e);
  190. }
  191. }
  192. /// <summary>
  193. /// this is called if the usage of lightprobe changes
  194. /// </summary>
  195. /// <param name="sender">Sender.</param>
  196. /// <param name="e">E.</param>
  197. void OnUseLightProbesChanged (object sender, GenericEventArgs<bool> e)
  198. {
  199. if (UseLightProbesChanged != null) {
  200. UseLightProbesChanged.Invoke(this, e);
  201. }
  202. }
  203. /// <summary>
  204. /// this is called if the current used fontname changes
  205. /// </summary>
  206. /// <param name="sender">Sender.</param>
  207. /// <param name="e">E.</param>
  208. void OnFontNameChanged (object sender, GenericEventArgs<string> e)
  209. {
  210. if (FontNameChanged != null) {
  211. FontNameChanged.Invoke(this, e);
  212. }
  213. }
  214. #endregion // EVENT HANDLERS
  215. }
  216. }