File size: 7,379 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
class CrossHair
{
	protected string					m_Name;
	protected bool 						m_Shown;
	protected bool 						m_Current;

	protected Widget 					m_Widget;
	
	void CrossHair(Widget w)
	{
		m_Widget = w;
		m_Name = w.GetName();
		m_Shown = true;
		m_Current = false;
	}

	void ~CrossHair() {}

	string GetName()
	{
		return m_Name;
	}

	bool IsCurrent()
	{
		return m_Current;
	}

	bool IsShown()
	{
		return m_Shown;
	}

	void Show()
	{
		m_Shown = false;
		m_Current = true;
		m_Widget.Show(true);
	}
	
	void Hide()
	{
		m_Shown = true;
		m_Current = false;
		m_Widget.Show(false);
	}
	
	Widget GetWidget()
	{
		return m_Widget;
	}
}

class CrossHairSelector extends ScriptedWidgetEventHandler
{
	protected PlayerBase 					m_Player;
	protected ActionManagerBase 			m_AM;
	
	protected Widget					m_Root;

	protected ref set<ref CrossHair>	m_CrossHairs;

	//! Floating crosshair
	protected vector m_PreviousDirection;
	protected bool m_PreviousDirectionSet;
	
	void CrossHairSelector()
	{
		m_Player 		= null;
		m_AM 			= null;
		m_CrossHairs 	= new set<ref CrossHair>;

		GetGame().GetPostUpdateQueue(CALL_CATEGORY_GUI).Insert(Update);
	}
		
	void ~CrossHairSelector()
	{
		GetGame().GetPostUpdateQueue(CALL_CATEGORY_GUI).Remove(Update);
	}
	
	protected void Init()
	{
		m_CrossHairs.Clear();
				
		Widget child = m_Root.GetChildren();
		while(child)
		{
			m_CrossHairs.Insert(CrossHair(child));
			child = child.GetSibling();
		}
	}
	
	protected void OnWidgetScriptInit(Widget w)
	{
		m_Root = w;
		m_Root.SetHandler(this);
		Init();
		m_Root.Update();
	}
	
	protected void Update()
	{
		//! don't show crosshair if it's disabled in profile or from server
		if(!g_Game.GetProfileOption(EDayZProfilesOptions.CROSSHAIR) || g_Game.GetWorld().IsCrosshairDisabled())
		{
			if(GetCurrentCrossHair())
				ShowCrossHair(null);
			
			return;
		};

		if(m_Player && !m_Player.IsAlive()) // handle respawn
		{
			m_Player = null;
			m_AM = null;
		}
		if(!m_Player) GetPlayer();
		if(!m_AM) GetActionManager();
		
		if(m_Player && m_Player.IsPlayerSelected())
		{
			SelectCrossHair();
		}
	}
	
	// getters
 	protected void GetPlayer()
	{
		Class.CastTo(m_Player, GetGame().GetPlayer());
	}

	protected void GetActionManager()
	{
		if( m_Player && m_Player.IsPlayerSelected() )
		{
			Class.CastTo(m_AM, m_Player.GetActionManager());
		}
		else
			m_AM = null;
	}
	
	protected CrossHair GetCrossHairByName(string widgetName)
	{
		for(int i = 0; i < m_CrossHairs.Count(); i++)
		{
			if(m_CrossHairs.Get(i).GetName() == widgetName)
				return m_CrossHairs.Get(i);
		}

		return null;
	}

	protected CrossHair GetCurrentCrossHair()
	{
		for(int i = 0; i < m_CrossHairs.Count(); i++)
		{
			if(m_CrossHairs.Get(i).IsCurrent())
				return m_CrossHairs.Get(i);
		}

		return null;
	}
	
	protected void SelectCrossHair()
	{
		if(!m_AM) return;

		HumanInputController hic = m_Player.GetInputController();
		ActionBase action = m_AM.GetRunningAction();
		
		bool firearmInHands = m_Player.GetItemInHands() && m_Player.GetItemInHands().IsWeapon();

		//! firearms
		if ( firearmInHands && m_Player.IsRaised() && !m_Player.IsInIronsights() && !m_Player.IsInOptics() && !hic.CameraIsFreeLook() && !m_Player.GetCommand_Melee2() )
		{
			ShowCrossHair(GetCrossHairByName("crossT_128x128"));
		}
		//! On Continuous Actions
		else if (action && action.GetActionCategory() == AC_CONTINUOUS)
		{
			int actionState = m_AM.GetActionState(action);

			if ( actionState != UA_NONE )
				ShowCrossHair(null);
		}
		//! raised hands(bare + non-firearm) + melee cmd
		else if ( m_Player.IsRaised() || m_Player.GetCommand_Melee2() || GetGame().GetUIManager().GetMenu() != null )
		{
			ShowCrossHair(null);
		}
		//! handle unconscious state
		else if ( m_Player.GetCommand_Unconscious() )
		{
			ShowCrossHair(null);
		}
		//! default
		else
		{
			ShowCrossHair(GetCrossHairByName("dot"));
		}
	}
	
	protected void ShowCrossHair(CrossHair crossHair)
	{
		//! no crosshair + clean + hide the previous
		if (!crossHair)
		{
			if (GetCurrentCrossHair())
				GetCurrentCrossHair().Hide();
			
			return;
		}
		else //! hide prev crosshair
		{
			if (GetCurrentCrossHair() && GetCurrentCrossHair() != crossHair)
				GetCurrentCrossHair().Hide();
		}
		
		//! show the new one
		if (!crossHair.IsCurrent() && crossHair.IsShown())
			crossHair.Show();

#ifdef WIP_TRACKIR
		FloatingCrossHair(crossHair.GetWidget());
#endif
	}
	
	//! Highly WIP, do not use
	void FloatingCrossHair(Widget widget)
	{
		HumanInputController hic = m_Player.GetInputController();
		
		//! Only intended to be used with track IR
		if (!hic.CameraIsTracking())
		{
			widget.SetPos(0, 0);
			widget.Update();
			return;
		}
		
		ActionBase action = m_AM.GetRunningAction();
		
		float dt = GetDayZGame().GetDeltaT();
		
		HumanCommandWeapons hcw = m_Player.GetCommandModifier_Weapons();
		
		vector transform[4];
		m_Player.GetTransformWS(transform);
		
		vector aimAngles = Vector(hcw.GetBaseAimingAngleLR(), hcw.GetBaseAimingAngleUD(), 0.0);
		vector plrAngles = Math3D.MatrixToAngles(transform);
		
		aimAngles = Vector(0.0, hcw.GetBaseAimingAngleUD(), 0.0);
		plrAngles = Vector(hic.GetHeadingAngle() * -Math.RAD2DEG, 0, 0);
		
		vector resAngle = aimAngles + plrAngles;
		
		vector start;
		MiscGameplayFunctions.GetHeadBonePos(m_Player, start);
		
		vector direction = resAngle.AnglesToVector();
		
		int layer = ObjIntersectView;
		float range = 1.0;
		
		Weapon_Base weapon;
		if (Class.CastTo(weapon, m_Player.GetItemInHands()) && m_Player.IsRaised())
		{
			layer = ObjIntersectFire;
			range = 10.0;
			
			vector usti_hlavne_position = weapon.GetSelectionPositionMS( "usti hlavne" );
			vector konec_hlavne_position = weapon.GetSelectionPositionMS( "konec hlavne" );
			usti_hlavne_position = m_Player.ModelToWorld(usti_hlavne_position);
			konec_hlavne_position = m_Player.ModelToWorld(konec_hlavne_position);
			
			vector contact_dir;
			int contact_component;
			
			direction = konec_hlavne_position - usti_hlavne_position;
			direction.Normalize();
			
			start = konec_hlavne_position;
			
			m_PreviousDirectionSet = false;
		}
		else
		{
			if (!m_PreviousDirectionSet)
			{
				m_PreviousDirectionSet = true;
				m_PreviousDirection = direction;
			}
			
			float r0[4];
			float r1[4];
			
			vector t[4];
			
			Math3D.DirectionAndUpMatrix(m_PreviousDirection, vector.Up, t);
			Math3D.MatrixToQuat(t, r0);
			
			Math3D.DirectionAndUpMatrix(direction, vector.Up, t);
			Math3D.MatrixToQuat(t, r1);
			
			Math3D.QuatLerp(r0, r0, r1, 0.1);
			
			Math3D.QuatToMatrix(r0, t);
			
			direction = t[2];
		
			m_PreviousDirection = direction;
		}
		
		vector end = start + (direction * range);
		vector position = end;
		
		DayZPhysics.RaycastRV(start, end, position, contact_dir, contact_component, null, m_Player, m_Player, false, false, layer);
		
		/*
		vector lines[2];
		lines[0] = start;
		lines[1] = end;
		
		Shape.CreateSphere(0xFFFFFF00, ShapeFlags.ONCE, usti_hlavne_position, 0.1);
		Shape.CreateSphere(0xFFFFFF00, ShapeFlags.ONCE, konec_hlavne_position, 0.1);
		Shape.CreateLines(0xFF00FF00, ShapeFlags.ONCE, lines, 2);
		*/
		
		vector screenSpace = GetGame().GetScreenPos(position);

		float sx, sy;
		widget.GetScreenSize(sx, sy);
		screenSpace[0] = screenSpace[0] - (sx * 0.5);
		screenSpace[1] = screenSpace[1] - (sy * 0.5);
			
		widget.SetScreenPos(screenSpace[0], screenSpace[1]);
		widget.Update();
	}
}