File size: 7,396 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
class HudDebugWinCharStats extends HudDebugWinBase
{
	TextListboxWidget						m_WgtValues;
	Widget									m_WgtPanel;
	ref array<ref Widget> 					m_StatWidgets = new array<ref Widget>;
	ref map <ref SliderWidget, string> 		m_SliderWidgets = new map<ref SliderWidget, string>;
	ref array<ref TextWidget> 				m_StatValues = new array<ref TextWidget>;
	ref map<ref EditBoxWidget, string> 		m_StatValuesInput = new map<ref EditBoxWidget, string>;
	bool									m_Populated;
	bool									m_ChangingSlider;
	
	//============================================
	// Constructor
	//============================================
	void HudDebugWinCharStats(Widget widget_root)
	{
		m_WgtRoot = widget_root;
		m_WgtPanel = Widget.Cast(m_WgtRoot.FindAnyWidget("Stats") );
		//FitWindow();
	}

	//============================================
	// Destructor
	//============================================
	void ~HudDebugWinCharStats()
	{	
		SetUpdate( false );
	}


	//============================================
	// GetWinType
	//============================================
	override int GetType()
	{
		return HudDebug.HUD_WIN_CHAR_STATS;
	}
	
	//============================================
	// Show 
	//============================================
	override void Show()
	{
		super.Show();
		
		//Print("Show()");
		
		SetUpdate( true );
	}

	//============================================
	// Hide 
	//============================================
	override void Hide()
	{
		super.Hide();
		
		//Print("Hide()");
		
		SetUpdate( false );
	}

	//============================================
	// SetUpdate
	//============================================
	override void SetUpdate( bool state )
	{
		//Disable update on server (PluginDeveloperSync)
		PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
		
		PluginDeveloperSync developer_sync = PluginDeveloperSync.Cast( GetPlugin( PluginDeveloperSync ) );
		
		//if client, send RPC
		if ( GetGame().IsClient() )
		{
			ref Param1<bool> params = new Param1<bool>( state );
			if ( player )
			{
				player.RPCSingleParam( ERPCs.DEV_STATS_UPDATE, params, true );
				SetRPCSent();
			}
		}
		//else set directly
		else
		{
			if ( developer_sync )
			{
				developer_sync.EnableUpdate( state, ERPCs.DEV_STATS_UPDATE, player );	
			}
		}
	}


	override void Update()
	{
		super.Update();
		//Print("panel:" + m_WgtPanel);
		//Print("Update()");
		
		//refresh notifiers
		
		
		if(!m_Populated)
			SetupValues();
		
		UpdateValues();
	}
	
	void SetupValues()
	{
		PluginDeveloperSync developer_sync = PluginDeveloperSync.Cast( GetPlugin( PluginDeveloperSync ) );
		
		//clear window
		//ClearValues();
			
		if ( developer_sync.m_PlayerStatsSynced.Count() > 0 )
		{
			//set
			for ( int i = 0; i < developer_sync.m_PlayerStatsSynced.Count(); i++ )
			{
				AddValue( developer_sync.m_PlayerStatsSynced.Get( i ).GetName(), developer_sync.m_PlayerStatsSynced.Get( i ).GetValue().ToString() );
			}
					//fit to screen
			FitWindow();
			m_Populated = true;
		}
	}
	
	
	void UpdateValues()
	{
		PluginDeveloperSync developer_sync = PluginDeveloperSync.Cast( GetPlugin( PluginDeveloperSync ) );
		if ( developer_sync.m_PlayerStatsSynced.Count() > 0 )
		{
			for ( int i = 0; i < developer_sync.m_PlayerStatsSynced.Count(); i++ )
			{
				string stat_name = developer_sync.m_PlayerStatsSynced.Get( i ).GetName();
				float val_norm = developer_sync.m_PlayerStatsSynced.Get( i ).GetValueNorm();
				float value = developer_sync.m_PlayerStatsSynced.Get( i ).GetValue();
				
				if( stat_name == "BloodType" )
				{
					string type, name;
					bool positive;
					
					name = value.ToString();
					name += "("+BloodTypes.GetBloodTypeName( Math.Round(value), type, positive)+")";
					m_StatValues.Get(i).SetText(name);
				}
				else
				{
					m_StatValues.Get(i).SetText(value.ToString());
					
				}
				if(!m_ChangingSlider)
					m_SliderWidgets.GetKeyByValue(stat_name).SetCurrent(val_norm * 100);
				/*
				EditBoxWidget w;
				w.
				AddValue( developer_sync.m_PlayerStatsSynced.Get( i ).GetName(), developer_sync.m_PlayerStatsSynced.Get( i ).GetValue().ToString() );
				*/
			}
		}
		
	}
	
	/*
	
	void AddValue( string title, string value )
	{
		int index = m_WgtValues.AddItem( title, NULL, 0 );
		m_WgtValues.SetItem( index, value, NULL, 1 );
	}	
	*/
	
	void AddValue( string title, string value )
	{
		Widget widget = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/debug/day_z_hud_debug_stat.layout", m_WgtPanel );
		
		TextWidget tw = TextWidget.Cast(widget.FindAnyWidget("StatName"));
		tw.SetText(title);
		m_StatWidgets.Insert(widget);
		
		TextWidget tw_output = TextWidget.Cast(widget.FindAnyWidget("OutputValue"));
		m_StatValues.Insert(tw_output);
		
		EditBoxWidget ebw_input = EditBoxWidget.Cast(widget.FindAnyWidget("InputValue"));
		m_StatValuesInput.Insert(ebw_input, title );
		
		SliderWidget sw = SliderWidget.Cast(widget.FindAnyWidget("StatSlider"));
		m_SliderWidgets.Insert(sw,title );
		
		
		//int index = m_WgtValues.AddItem( title, NULL, 0 );
		//m_WgtValues.SetItem( index, value, NULL, 1 );
		
		AutoHeightSpacer WgtModifiersContent_panel_script;
		m_WgtPanel.GetScript( WgtModifiersContent_panel_script );
		WgtModifiersContent_panel_script.Update();
	}

	void ClearValues()
	{
		m_StatWidgets.Clear();
		//m_WgtValues.ClearItems();
	}

	void FitWindow()
	{
		TextListboxWidget wgt = TextListboxWidget.Cast(m_WgtPanel);
		if(wgt)
			FitWindowByContent( wgt );
	}
	
	bool OnClick( Widget w, int x, int y, int button )
	{	
		if ( w.GetName() == "ResetStats" )
		{
			ResetStats();
			return true;
		}
		return false;
	}
	
	bool OnChange(Widget w, int x, int y, bool finished)
	{
		if ( m_StatValuesInput.Contains(EditBoxWidget.Cast(w)) && finished )
		{
			EditBoxWidget ebw = EditBoxWidget.Cast(w);
			RPCChangeStat(m_StatValuesInput.Get(EditBoxWidget.Cast(w)), ebw.GetText().ToFloat());
			return true;
		}
		if (m_SliderWidgets.Contains(SliderWidget.Cast(w)))
		{
			m_ChangingSlider = true;
			string stat_name = m_SliderWidgets.Get(SliderWidget.Cast(w));
			SliderWidget sw = SliderWidget.Cast(w);
			PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
			for ( int i = 0; i < player.m_PlayerStats.GetPCO().Get().Count(); i++ ) 
			{
				string label = player.m_PlayerStats.GetPCO().Get().Get( i ).GetLabel();
				if(label == stat_name)
				{
					float stat_min = player.m_PlayerStats.GetPCO().Get().Get( i ).GetMin();
					float stat_max = player.m_PlayerStats.GetPCO().Get().Get( i ).GetMax();
					float current_value_norm = sw.GetCurrent() / 100;
					float current_value_abs = stat_min + (stat_max - stat_min) * current_value_norm;
					
					RPCChangeStat(label, current_value_abs);
				}
			}
			//Print("OnChange " + finished);
			if(finished)
				m_ChangingSlider = false;
		}
		
		return false;
	}
	
	
	void ResetStats()
	{
		PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
		
		//if client, send RPC

		ref Param1<bool> params = new Param1<bool>( false );
		if ( player )
		{
			player.RPCSingleParam( ERPCs.DEV_RPC_STATS_RESET, params, true );
		}
	}
	
	void RPCChangeStat(string stat, float value)
	{
		PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
		
		//if client, send RPC

		ref Param2<string, float> params = new Param2<string, float>( stat, value );
		if ( player )
		{
			player.RPCSingleParam( ERPCs.DEV_RPC_STAT_SET, params, true );
		}
	}
	
	
}