crosshairselector.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. class CrossHair
  2. {
  3. protected string m_Name;
  4. protected bool m_Shown;
  5. protected bool m_Current;
  6. protected Widget m_Widget;
  7. void CrossHair(Widget w)
  8. {
  9. m_Widget = w;
  10. m_Name = w.GetName();
  11. m_Shown = true;
  12. m_Current = false;
  13. }
  14. void ~CrossHair() {}
  15. string GetName()
  16. {
  17. return m_Name;
  18. }
  19. bool IsCurrent()
  20. {
  21. return m_Current;
  22. }
  23. bool IsShown()
  24. {
  25. return m_Shown;
  26. }
  27. void Show()
  28. {
  29. m_Shown = false;
  30. m_Current = true;
  31. m_Widget.Show(true);
  32. }
  33. void Hide()
  34. {
  35. m_Shown = true;
  36. m_Current = false;
  37. m_Widget.Show(false);
  38. }
  39. Widget GetWidget()
  40. {
  41. return m_Widget;
  42. }
  43. }
  44. class CrossHairSelector extends ScriptedWidgetEventHandler
  45. {
  46. protected PlayerBase m_Player;
  47. protected ActionManagerBase m_AM;
  48. protected Widget m_Root;
  49. protected ref set<ref CrossHair> m_CrossHairs;
  50. //! Floating crosshair
  51. protected vector m_PreviousDirection;
  52. protected bool m_PreviousDirectionSet;
  53. void CrossHairSelector()
  54. {
  55. m_Player = null;
  56. m_AM = null;
  57. m_CrossHairs = new set<ref CrossHair>;
  58. GetGame().GetPostUpdateQueue(CALL_CATEGORY_GUI).Insert(Update);
  59. }
  60. void ~CrossHairSelector()
  61. {
  62. GetGame().GetPostUpdateQueue(CALL_CATEGORY_GUI).Remove(Update);
  63. }
  64. protected void Init()
  65. {
  66. m_CrossHairs.Clear();
  67. Widget child = m_Root.GetChildren();
  68. while(child)
  69. {
  70. m_CrossHairs.Insert(CrossHair(child));
  71. child = child.GetSibling();
  72. }
  73. }
  74. protected void OnWidgetScriptInit(Widget w)
  75. {
  76. m_Root = w;
  77. m_Root.SetHandler(this);
  78. Init();
  79. m_Root.Update();
  80. }
  81. protected void Update()
  82. {
  83. //! don't show crosshair if it's disabled in profile or from server
  84. if(!g_Game.GetProfileOption(EDayZProfilesOptions.CROSSHAIR) || g_Game.GetWorld().IsCrosshairDisabled())
  85. {
  86. if(GetCurrentCrossHair())
  87. ShowCrossHair(null);
  88. return;
  89. };
  90. if(m_Player && !m_Player.IsAlive()) // handle respawn
  91. {
  92. m_Player = null;
  93. m_AM = null;
  94. }
  95. if(!m_Player) GetPlayer();
  96. if(!m_AM) GetActionManager();
  97. if(m_Player && m_Player.IsPlayerSelected())
  98. {
  99. SelectCrossHair();
  100. }
  101. }
  102. // getters
  103. protected void GetPlayer()
  104. {
  105. Class.CastTo(m_Player, GetGame().GetPlayer());
  106. }
  107. protected void GetActionManager()
  108. {
  109. if( m_Player && m_Player.IsPlayerSelected() )
  110. {
  111. Class.CastTo(m_AM, m_Player.GetActionManager());
  112. }
  113. else
  114. m_AM = null;
  115. }
  116. protected CrossHair GetCrossHairByName(string widgetName)
  117. {
  118. for(int i = 0; i < m_CrossHairs.Count(); i++)
  119. {
  120. if(m_CrossHairs.Get(i).GetName() == widgetName)
  121. return m_CrossHairs.Get(i);
  122. }
  123. return null;
  124. }
  125. protected CrossHair GetCurrentCrossHair()
  126. {
  127. for(int i = 0; i < m_CrossHairs.Count(); i++)
  128. {
  129. if(m_CrossHairs.Get(i).IsCurrent())
  130. return m_CrossHairs.Get(i);
  131. }
  132. return null;
  133. }
  134. protected void SelectCrossHair()
  135. {
  136. if(!m_AM) return;
  137. HumanInputController hic = m_Player.GetInputController();
  138. ActionBase action = m_AM.GetRunningAction();
  139. bool firearmInHands = m_Player.GetItemInHands() && m_Player.GetItemInHands().IsWeapon();
  140. //! firearms
  141. if ( firearmInHands && m_Player.IsRaised() && !m_Player.IsInIronsights() && !m_Player.IsInOptics() && !hic.CameraIsFreeLook() && !m_Player.GetCommand_Melee2() )
  142. {
  143. ShowCrossHair(GetCrossHairByName("crossT_128x128"));
  144. }
  145. //! On Continuous Actions
  146. else if (action && action.GetActionCategory() == AC_CONTINUOUS)
  147. {
  148. int actionState = m_AM.GetActionState(action);
  149. if ( actionState != UA_NONE )
  150. ShowCrossHair(null);
  151. }
  152. //! raised hands(bare + non-firearm) + melee cmd
  153. else if ( m_Player.IsRaised() || m_Player.GetCommand_Melee2() || GetGame().GetUIManager().GetMenu() != null )
  154. {
  155. ShowCrossHair(null);
  156. }
  157. //! handle unconscious state
  158. else if ( m_Player.GetCommand_Unconscious() )
  159. {
  160. ShowCrossHair(null);
  161. }
  162. //! default
  163. else
  164. {
  165. ShowCrossHair(GetCrossHairByName("dot"));
  166. }
  167. }
  168. protected void ShowCrossHair(CrossHair crossHair)
  169. {
  170. //! no crosshair + clean + hide the previous
  171. if (!crossHair)
  172. {
  173. if (GetCurrentCrossHair())
  174. GetCurrentCrossHair().Hide();
  175. return;
  176. }
  177. else //! hide prev crosshair
  178. {
  179. if (GetCurrentCrossHair() && GetCurrentCrossHair() != crossHair)
  180. GetCurrentCrossHair().Hide();
  181. }
  182. //! show the new one
  183. if (!crossHair.IsCurrent() && crossHair.IsShown())
  184. crossHair.Show();
  185. #ifdef WIP_TRACKIR
  186. FloatingCrossHair(crossHair.GetWidget());
  187. #endif
  188. }
  189. //! Highly WIP, do not use
  190. void FloatingCrossHair(Widget widget)
  191. {
  192. HumanInputController hic = m_Player.GetInputController();
  193. //! Only intended to be used with track IR
  194. if (!hic.CameraIsTracking())
  195. {
  196. widget.SetPos(0, 0);
  197. widget.Update();
  198. return;
  199. }
  200. ActionBase action = m_AM.GetRunningAction();
  201. float dt = GetDayZGame().GetDeltaT();
  202. HumanCommandWeapons hcw = m_Player.GetCommandModifier_Weapons();
  203. vector transform[4];
  204. m_Player.GetTransformWS(transform);
  205. vector aimAngles = Vector(hcw.GetBaseAimingAngleLR(), hcw.GetBaseAimingAngleUD(), 0.0);
  206. vector plrAngles = Math3D.MatrixToAngles(transform);
  207. aimAngles = Vector(0.0, hcw.GetBaseAimingAngleUD(), 0.0);
  208. plrAngles = Vector(hic.GetHeadingAngle() * -Math.RAD2DEG, 0, 0);
  209. vector resAngle = aimAngles + plrAngles;
  210. vector start;
  211. MiscGameplayFunctions.GetHeadBonePos(m_Player, start);
  212. vector direction = resAngle.AnglesToVector();
  213. int layer = ObjIntersectView;
  214. float range = 1.0;
  215. Weapon_Base weapon;
  216. if (Class.CastTo(weapon, m_Player.GetItemInHands()) && m_Player.IsRaised())
  217. {
  218. layer = ObjIntersectFire;
  219. range = 10.0;
  220. vector usti_hlavne_position = weapon.GetSelectionPositionMS( "usti hlavne" );
  221. vector konec_hlavne_position = weapon.GetSelectionPositionMS( "konec hlavne" );
  222. usti_hlavne_position = m_Player.ModelToWorld(usti_hlavne_position);
  223. konec_hlavne_position = m_Player.ModelToWorld(konec_hlavne_position);
  224. vector contact_dir;
  225. int contact_component;
  226. direction = konec_hlavne_position - usti_hlavne_position;
  227. direction.Normalize();
  228. start = konec_hlavne_position;
  229. m_PreviousDirectionSet = false;
  230. }
  231. else
  232. {
  233. if (!m_PreviousDirectionSet)
  234. {
  235. m_PreviousDirectionSet = true;
  236. m_PreviousDirection = direction;
  237. }
  238. float r0[4];
  239. float r1[4];
  240. vector t[4];
  241. Math3D.DirectionAndUpMatrix(m_PreviousDirection, vector.Up, t);
  242. Math3D.MatrixToQuat(t, r0);
  243. Math3D.DirectionAndUpMatrix(direction, vector.Up, t);
  244. Math3D.MatrixToQuat(t, r1);
  245. Math3D.QuatLerp(r0, r0, r1, 0.1);
  246. Math3D.QuatToMatrix(r0, t);
  247. direction = t[2];
  248. m_PreviousDirection = direction;
  249. }
  250. vector end = start + (direction * range);
  251. vector position = end;
  252. DayZPhysics.RaycastRV(start, end, position, contact_dir, contact_component, null, m_Player, m_Player, false, false, layer);
  253. /*
  254. vector lines[2];
  255. lines[0] = start;
  256. lines[1] = end;
  257. Shape.CreateSphere(0xFFFFFF00, ShapeFlags.ONCE, usti_hlavne_position, 0.1);
  258. Shape.CreateSphere(0xFFFFFF00, ShapeFlags.ONCE, konec_hlavne_position, 0.1);
  259. Shape.CreateLines(0xFF00FF00, ShapeFlags.ONCE, lines, 2);
  260. */
  261. vector screenSpace = GetGame().GetScreenPos(position);
  262. float sx, sy;
  263. widget.GetScreenSize(sx, sy);
  264. screenSpace[0] = screenSpace[0] - (sx * 0.5);
  265. screenSpace[1] = screenSpace[1] - (sy * 0.5);
  266. widget.SetScreenPos(screenSpace[0], screenSpace[1]);
  267. widget.Update();
  268. }
  269. }