Comment.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. -- Description: Comment system.
  10. --
  11. ----------------------------------------------------------------------------------------------------
  12. ----------------------------------------------------------------------------------------------------
  13. -- $Id$
  14. -- $DateTime$
  15. -- Description: Comment system.
  16. --
  17. ----------------------------------------------------------------------------------------------------
  18. Comment =
  19. {
  20. Properties =
  21. {
  22. Text = "This is a comment",
  23. fSize = 1.2, --[0.0, 100.0 , 0.1, ""]
  24. bHidden = 0,
  25. fMaxDist = 100, --[0.0 ,255.0 , 0.1, ""]
  26. nCharsPerLine = 30, --[1, 255, 1, ""]
  27. bFixed = 0,
  28. clrDiffuse = { x=1,y=0.5,z=0 },
  29. },
  30. Editor={
  31. Model="Editor/Objects/comment.cgf",
  32. Icon="Comment.bmp",
  33. },
  34. hidden = 0,
  35. lines = {},
  36. lineCount = 0,
  37. fMaxDistSquared = 0,
  38. bNoUpdateInGame = 1,
  39. }
  40. -------------------------------------------------------
  41. function Comment:OnLoad(table)
  42. self.hidden = table.hidden;
  43. end
  44. -------------------------------------------------------
  45. function Comment:OnSave(table)
  46. table.hidden = self.hidden;
  47. end
  48. -------------------------------------------------------
  49. function Comment:OnInit()
  50. -- Delete when not in dev-mode.
  51. if (not System.IsDevModeEnable() ) then
  52. self:DeleteThis();
  53. return;
  54. end
  55. self:OnReset();
  56. end
  57. -------------------------------------------------------
  58. function Comment:OnSpawn()
  59. end
  60. -------------------------------------------------------
  61. function Comment:OnPropertyChange()
  62. self:OnReset();
  63. end
  64. -------------------------------------------------------
  65. function Comment:OnReset()
  66. -- Comment is only Active(OnUpdate() will be called) when in Editor or when cl_comment > 0
  67. local cl_comment = System.GetCVar("cl_comment")
  68. if (System.IsEditor() or cl_comment > 0) then
  69. self:SetUpdatePolicy( ENTITY_UPDATE_VISIBLE );
  70. self:Activate(1)
  71. else
  72. self:Activate(0);
  73. end
  74. -- bNoUpdateInGame is checked in OnUpdate() to account for in-editor game mode.
  75. if (cl_comment == 0) then
  76. self.bNoUpdateInGame = 1;
  77. else
  78. self.bNoUpdateInGame = 0;
  79. end
  80. self.fMaxDistSquared = self.Properties.fMaxDist * self.Properties.fMaxDist;
  81. self.hidden = self.Properties.bHidden;
  82. self.lines = {};
  83. -- process the text and line-break it
  84. local maxLength = self.Properties.nCharsPerLine;
  85. local curLength = 0;
  86. local curText = "";
  87. local curLine = 1;
  88. for char in string.gfind(self.Properties.Text, ".") do
  89. if (char == " ") then
  90. -- word finished ... see if we are over the limit
  91. if (curLength > maxLength) then
  92. -- commit line
  93. self.lines[curLine] = curText;
  94. curLine = curLine + 1;
  95. curText = "";
  96. curLength = 0;
  97. -- "consume" the whitespace
  98. char = "";
  99. end
  100. end
  101. if (char ~= "") then
  102. -- append char
  103. curText = curText..char;
  104. curLength = curLength + 1;
  105. end
  106. end
  107. self.lines[curLine] = curText;
  108. self.lineCount = curLine;
  109. self:OnUpdate(0);
  110. end
  111. -------------------------------------------------------
  112. function Comment:OnUpdate(delta)
  113. if (self.hidden ~= 0 or self:IsHidden() or self.Properties.Text =="" or (self.bNoUpdateInGame == 1 and not System.IsEditing())) then
  114. return;
  115. end
  116. -- calculate alpha
  117. local alpha = 0;
  118. local factor = 0;
  119. if (self.fMaxDistSquared>0) then
  120. local mypos = g_Vectors.temp_v1;
  121. self:GetWorldPos(mypos);
  122. SubVectors(mypos, mypos, System.GetViewCameraPos());
  123. local distSquared = LengthSqVector(mypos);
  124. factor = distSquared;
  125. if (self.Properties.fMaxDist >= 255.0) then
  126. alpha = 1.0;
  127. elseif (distSquared<self.fMaxDistSquared) then
  128. alpha = distSquared/self.fMaxDistSquared;
  129. alpha = 1.0 - alpha*alpha;
  130. end
  131. end
  132. -- draw text
  133. local increment = System.GetViewCameraUpDir();
  134. if (alpha>0.001) then
  135. local pos = self:GetWorldPos( g_Vectors.temp_v1 );
  136. factor = math.sqrt(factor);
  137. factor = (self.Properties.fSize/60) * factor * System.GetViewCameraFov();
  138. local incrementAll = g_Vectors.temp_v4;
  139. FastScaleVector(increment, increment, factor);
  140. FastScaleVector(incrementAll, increment, (self.lineCount / 2 + 1));
  141. -- start all the way at the top
  142. FastSumVectors(pos, pos, incrementAll);
  143. local textColor = { x=0, y=1, z=0 }; -- default color is for (bFixed == 1)
  144. if (self.Properties.bFixed ~= 1) then
  145. textColor = {x=self.Properties.clrDiffuse.x, y=self.Properties.clrDiffuse.y, z=self.Properties.clrDiffuse.z};
  146. end
  147. for i,val in ipairs(self.lines) do
  148. System.DrawLabel( pos, self.Properties.fSize, val, textColor.x, textColor.y, textColor.z, alpha );
  149. -- decrement the increment
  150. FastDifferenceVectors(pos, pos, increment);
  151. end
  152. end
  153. end
  154. -------------------------------------------------------
  155. function Comment:Event_UnHide(sender)
  156. BroadcastEvent(self, "UnHide");
  157. self.hidden = 0;
  158. end
  159. -------------------------------------------------------
  160. function Comment:Event_Hide(sender)
  161. BroadcastEvent(self, "Hide");
  162. self.hidden = 1;
  163. end
  164. -------------------------------------------------------
  165. Comment.FlowEvents =
  166. {
  167. Inputs =
  168. {
  169. Hide = { Comment.Event_Hide, "bool" },
  170. UnHide = { Comment.Event_UnHide, "bool" },
  171. },
  172. Outputs =
  173. {
  174. Hide = "bool",
  175. UnHide = "bool",
  176. },
  177. }