debugmonitor.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. class DebugMonitor
  2. {
  3. protected bool m_IsUsingKBM;
  4. private Widget m_WidgetRoot;
  5. private TextWidget m_WindowLabelText;
  6. private TextWidget m_VersionValue;
  7. private TextWidget m_HealthValue;
  8. private TextWidget m_BloodValue;
  9. private TextWidget m_DmgSourceValue;
  10. private TextWidget m_MapTileValue;
  11. private TextWidget m_PositionValue;
  12. private TextWidget m_CopyPositionInfo;
  13. private TextWidget m_FPSValue;
  14. private TextWidget m_FPSMinValue;
  15. private TextWidget m_FPSMaxValue;
  16. private TextWidget m_FPSAvgValue;
  17. private int m_FPSTextDefaultColor;
  18. void DebugMonitor()
  19. {
  20. m_WidgetRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/debug/day_z_debug_monitor.layout");
  21. m_WidgetRoot.Show(false);
  22. m_VersionValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("VersionValue"));
  23. m_HealthValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("HealthValue"));
  24. m_BloodValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("BloodValue"));
  25. m_DmgSourceValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("DmgSourceValue"));
  26. m_MapTileValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("MapTileValue"));
  27. m_PositionValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("PositionValue"));
  28. m_CopyPositionInfo = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("CopyPositionInfo"));
  29. m_FPSValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("FPSCurrentValue"));
  30. m_FPSMinValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("FPSMinValue"));
  31. m_FPSMaxValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("FPSMaxValue"));
  32. m_FPSAvgValue = TextWidget.Cast(m_WidgetRoot.FindAnyWidget("FPSAvgValue"));
  33. m_FPSTextDefaultColor = m_FPSValue.GetColor();
  34. }
  35. void Init()
  36. {
  37. string version;
  38. g_Game.GetVersion(version);
  39. m_VersionValue.SetText(" " + version);
  40. GetGame().GetMission().GetOnInputDeviceChanged().Insert(OnInputDeviceChanged);
  41. if (GetGame().GetInput().IsKeyboardConnected())
  42. m_IsUsingKBM = true;
  43. m_WidgetRoot.Show(true);
  44. }
  45. void SetHealth(float value)
  46. {
  47. string health = string.Format(" %1", value.ToString());
  48. m_HealthValue.SetText(health);
  49. }
  50. void SetBlood(float value)
  51. {
  52. string blood = string.Format(" %1", value.ToString());
  53. m_BloodValue.SetText(blood);
  54. }
  55. void SetLastDamage(string value)
  56. {
  57. if (value != "")
  58. m_DmgSourceValue.SetText(" " + value);
  59. else
  60. m_DmgSourceValue.SetText(" -");
  61. }
  62. /*!
  63. Update the framerate values displayed within the debug monitor with new ones.
  64. */
  65. void SetFramerate(float current, float min, float max, float avg)
  66. {
  67. SetFramerateText(m_FPSValue, current);
  68. SetFramerateText(m_FPSMinValue, min);
  69. SetFramerateText(m_FPSMaxValue, max);
  70. SetFramerateText(m_FPSAvgValue, avg);
  71. }
  72. /*!
  73. Sets the provided text `widget` to display framerate `value`.
  74. Formats and colors the widget according to the value.
  75. */
  76. protected void SetFramerateText(TextWidget widget, float value)
  77. {
  78. // Ideally we would poll the refresh rate and base the values as
  79. // percentage thereof, but there is no such API in scripts.
  80. #ifdef PLATFORM_CONSOLE
  81. // default [30, inf] ; orange [20, 29] ; red [0, 19]
  82. if (value > 29)
  83. widget.SetColor(m_FPSTextDefaultColor);
  84. else if (value > 19)
  85. widget.SetColor(0xFFFF8000); // COLOR_ORANGE
  86. else
  87. widget.SetColor(COLOR_RED);
  88. #else
  89. // default [60, inf] ; yellow [40, 59] ; orange [30, 39] ; red [0, 29]
  90. if (value > 59)
  91. widget.SetColor(m_FPSTextDefaultColor);
  92. else if (value > 39)
  93. widget.SetColor(COLOR_YELLOW);
  94. else if (value > 29)
  95. widget.SetColor(0xFFFF8000); // COLOR_ORANGE
  96. else
  97. widget.SetColor(COLOR_RED);
  98. #endif
  99. widget.SetTextFormat("%1", Math.Round(value));
  100. }
  101. void SetPosition(vector value)
  102. {
  103. m_MapTileValue.SetText(" " + CalculateMapTile(value));
  104. string position = string.Format(" %1 %2 %3", value[0].ToString(), value[1].ToString(), value[2].ToString());
  105. m_PositionValue.SetText(position);
  106. if (GetUApi().GetInputByID(UAUICopyDebugMonitorPos).LocalPress())
  107. {
  108. string adjusted = (value[0] + 200000).ToString() + " " + value[2].ToString();
  109. GetGame().CopyToClipboard(adjusted);
  110. }
  111. if (m_IsUsingKBM)
  112. m_CopyPositionInfo.SetText(" (P to clipboard)");
  113. else
  114. m_CopyPositionInfo.SetText("");
  115. }
  116. void Show()
  117. {
  118. m_WidgetRoot.Show(true);
  119. }
  120. void Hide()
  121. {
  122. m_WidgetRoot.Show(false);
  123. }
  124. string CalculateMapTile(vector pos)
  125. {
  126. string tile;
  127. float worldSize = GetGame().GetWorld().GetWorldSize();
  128. float tileX = Math.InverseLerp(0, worldSize, pos[0]);
  129. float tileY = Math.InverseLerp(0, worldSize, pos[2]);
  130. tile = GetTileFomFraction(tileX).ToString() + GetTileFomFraction(tileY).ToString();
  131. return tile;
  132. }
  133. int GetTileFomFraction(float fraction)
  134. {
  135. if (fraction < 0.25)
  136. return 0;
  137. else if (fraction < 0.5)
  138. return 1;
  139. else if (fraction < 0.75)
  140. return 2;
  141. else
  142. return 3;
  143. }
  144. void OnInputDeviceChanged(EInputDeviceType pInputDeviceType)
  145. {
  146. if (pInputDeviceType == EInputDeviceType.MOUSE_AND_KEYBOARD)
  147. m_IsUsingKBM = true;
  148. else
  149. m_IsUsingKBM = false;
  150. }
  151. bool IsVisible()
  152. {
  153. return m_WidgetRoot.IsVisible();
  154. }
  155. };