huddebugwincharstats.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. class HudDebugWinCharStats extends HudDebugWinBase
  2. {
  3. TextListboxWidget m_WgtValues;
  4. Widget m_WgtPanel;
  5. ref array<ref Widget> m_StatWidgets = new array<ref Widget>;
  6. ref map <ref SliderWidget, string> m_SliderWidgets = new map<ref SliderWidget, string>;
  7. ref array<ref TextWidget> m_StatValues = new array<ref TextWidget>;
  8. ref map<ref EditBoxWidget, string> m_StatValuesInput = new map<ref EditBoxWidget, string>;
  9. bool m_Populated;
  10. bool m_ChangingSlider;
  11. //============================================
  12. // Constructor
  13. //============================================
  14. void HudDebugWinCharStats(Widget widget_root)
  15. {
  16. m_WgtRoot = widget_root;
  17. m_WgtPanel = Widget.Cast(m_WgtRoot.FindAnyWidget("Stats") );
  18. //FitWindow();
  19. }
  20. //============================================
  21. // Destructor
  22. //============================================
  23. void ~HudDebugWinCharStats()
  24. {
  25. SetUpdate( false );
  26. }
  27. //============================================
  28. // GetWinType
  29. //============================================
  30. override int GetType()
  31. {
  32. return HudDebug.HUD_WIN_CHAR_STATS;
  33. }
  34. //============================================
  35. // Show
  36. //============================================
  37. override void Show()
  38. {
  39. super.Show();
  40. //Print("Show()");
  41. SetUpdate( true );
  42. }
  43. //============================================
  44. // Hide
  45. //============================================
  46. override void Hide()
  47. {
  48. super.Hide();
  49. //Print("Hide()");
  50. SetUpdate( false );
  51. }
  52. //============================================
  53. // SetUpdate
  54. //============================================
  55. override void SetUpdate( bool state )
  56. {
  57. //Disable update on server (PluginDeveloperSync)
  58. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  59. PluginDeveloperSync developer_sync = PluginDeveloperSync.Cast( GetPlugin( PluginDeveloperSync ) );
  60. //if client, send RPC
  61. if ( GetGame().IsClient() )
  62. {
  63. ref Param1<bool> params = new Param1<bool>( state );
  64. if ( player )
  65. {
  66. player.RPCSingleParam( ERPCs.DEV_STATS_UPDATE, params, true );
  67. SetRPCSent();
  68. }
  69. }
  70. //else set directly
  71. else
  72. {
  73. if ( developer_sync )
  74. {
  75. developer_sync.EnableUpdate( state, ERPCs.DEV_STATS_UPDATE, player );
  76. }
  77. }
  78. }
  79. override void Update()
  80. {
  81. super.Update();
  82. if (!m_Populated)
  83. SetupValues();
  84. UpdateValues();
  85. }
  86. void SetupValues()
  87. {
  88. PluginDeveloperSync developerSync = PluginDeveloperSync.Cast(GetPlugin(PluginDeveloperSync));
  89. //clear window
  90. //ClearValues();
  91. if ( developerSync.m_PlayerStatsSynced.Count() > 0 )
  92. {
  93. foreach (SyncedValue syncedValue : developerSync.m_PlayerStatsSynced)
  94. {
  95. string name = syncedValue.GetName();
  96. string value = syncedValue.GetValue().ToString();
  97. AddValue(name, value);
  98. }
  99. FitWindow();
  100. m_Populated = true;
  101. }
  102. }
  103. void UpdateValues()
  104. {
  105. PluginDeveloperSync developerSync = PluginDeveloperSync.Cast( GetPlugin( PluginDeveloperSync ) );
  106. if ( developerSync.m_PlayerStatsSynced.Count() > 0 )
  107. {
  108. foreach (int i, SyncedValue syncedValue : developerSync.m_PlayerStatsSynced)
  109. {
  110. string statName = syncedValue.GetName();
  111. float valueNormalized = syncedValue.GetValueNorm();
  112. float value = syncedValue.GetValue();
  113. if ( statName == "BloodType" )
  114. {
  115. string type, name;
  116. bool positive;
  117. name = value.ToString();
  118. name += "("+BloodTypes.GetBloodTypeName(Math.Round(value), type, positive)+")";
  119. m_StatValues.Get(i).SetText(name);
  120. }
  121. else
  122. {
  123. if (statName == "HeatBuffer")
  124. {
  125. float heatBufferNormalized = Math.Round(Math.Lerp(-1, 1, valueNormalized) * 1000) * 0.001;
  126. m_StatValues.Get(i).SetText(string.Format("%1 (%2)", heatBufferNormalized.ToString(), value.ToString()));
  127. }
  128. else
  129. m_StatValues.Get(i).SetText(value.ToString());
  130. }
  131. if (!m_ChangingSlider)
  132. m_SliderWidgets.GetKeyByValue(statName).SetCurrent(valueNormalized * 100);
  133. }
  134. }
  135. }
  136. void AddValue(string title, string value)
  137. {
  138. Widget widget = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/debug/day_z_hud_debug_stat.layout", m_WgtPanel );
  139. TextWidget tw = TextWidget.Cast(widget.FindAnyWidget("StatName"));
  140. tw.SetText(title);
  141. m_StatWidgets.Insert(widget);
  142. TextWidget tw_output = TextWidget.Cast(widget.FindAnyWidget("OutputValue"));
  143. m_StatValues.Insert(tw_output);
  144. EditBoxWidget ebw_input = EditBoxWidget.Cast(widget.FindAnyWidget("InputValue"));
  145. m_StatValuesInput.Insert(ebw_input, title );
  146. SliderWidget sw = SliderWidget.Cast(widget.FindAnyWidget("StatSlider"));
  147. m_SliderWidgets.Insert(sw,title );
  148. AutoHeightSpacer WgtModifiersContent_panel_script;
  149. m_WgtPanel.GetScript( WgtModifiersContent_panel_script );
  150. WgtModifiersContent_panel_script.Update();
  151. }
  152. void ClearValues()
  153. {
  154. m_StatWidgets.Clear();
  155. }
  156. void FitWindow()
  157. {
  158. TextListboxWidget wgt = TextListboxWidget.Cast(m_WgtPanel);
  159. if(wgt)
  160. FitWindowByContent( wgt );
  161. }
  162. bool OnClick( Widget w, int x, int y, int button )
  163. {
  164. if ( w.GetName() == "ResetStats" )
  165. {
  166. ResetStats();
  167. return true;
  168. }
  169. return false;
  170. }
  171. bool OnChange(Widget w, int x, int y, bool finished)
  172. {
  173. if ( m_StatValuesInput.Contains(EditBoxWidget.Cast(w)) && finished )
  174. {
  175. EditBoxWidget ebw = EditBoxWidget.Cast(w);
  176. RPCChangeStat(m_StatValuesInput.Get(EditBoxWidget.Cast(w)), ebw.GetText().ToFloat());
  177. return true;
  178. }
  179. if (m_SliderWidgets.Contains(SliderWidget.Cast(w)))
  180. {
  181. m_ChangingSlider = true;
  182. string stat_name = m_SliderWidgets.Get(SliderWidget.Cast(w));
  183. SliderWidget sw = SliderWidget.Cast(w);
  184. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  185. for ( int i = 0; i < player.m_PlayerStats.GetPCO().Get().Count(); i++ )
  186. {
  187. string label = player.m_PlayerStats.GetPCO().Get().Get( i ).GetLabel();
  188. if(label == stat_name)
  189. {
  190. float stat_min = player.m_PlayerStats.GetPCO().Get().Get( i ).GetMin();
  191. float stat_max = player.m_PlayerStats.GetPCO().Get().Get( i ).GetMax();
  192. float current_value_norm = sw.GetCurrent() / 100;
  193. float current_value_abs = stat_min + (stat_max - stat_min) * current_value_norm;
  194. RPCChangeStat(label, current_value_abs);
  195. }
  196. }
  197. //Print("OnChange " + finished);
  198. if(finished)
  199. m_ChangingSlider = false;
  200. }
  201. return false;
  202. }
  203. void ResetStats()
  204. {
  205. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  206. //if client, send RPC
  207. ref Param1<bool> params = new Param1<bool>( false );
  208. if ( player )
  209. {
  210. player.RPCSingleParam( ERPCs.DEV_RPC_STATS_RESET, params, true );
  211. }
  212. }
  213. void RPCChangeStat(string stat, float value)
  214. {
  215. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  216. //if client, send RPC
  217. ref Param2<string, float> params = new Param2<string, float>( stat, value );
  218. if ( player )
  219. {
  220. player.RPCSingleParam( ERPCs.DEV_RPC_STAT_SET, params, true );
  221. }
  222. }
  223. }