meter.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #ifndef METER_H
  19. #define METER_H
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Please see the CPP file for an explanation of this API.
  22. //////////////////////////////////////////////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////
  24. // Headers.
  25. //////////////////////////////////////////////////////////////////////////////
  26. #include "System.h"
  27. // If PATHS_IN_INCLUDES macro is defined, we can utilized relative
  28. // paths to a header file. In this case we generally go off of our
  29. // RSPiX root directory. System.h MUST be included before this macro
  30. // is evaluated. System.h is the header that, based on the current
  31. // platform (or more so in this case on the compiler), defines
  32. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  33. // instead.
  34. #ifdef PATHS_IN_INCLUDES
  35. #include "ORANGE/GUI/dlg.h"
  36. #else
  37. #include "Dlg.h"
  38. #endif // PATHS_IN_INCLUDES
  39. #include <string.h>
  40. //////////////////////////////////////////////////////////////////////////////
  41. // Macros.
  42. //////////////////////////////////////////////////////////////////////////////
  43. // Number of values remembered for histogram.
  44. #define METER_HISTOGRAM_HISTORY 20 // Values.
  45. // Maximum length for the unit string.
  46. #define MAX_UNIT_LEN 128 // Characters.
  47. //////////////////////////////////////////////////////////////////////////////
  48. // Typedefs.
  49. //////////////////////////////////////////////////////////////////////////////
  50. //////////////////////////////////////////////////////////////////////////////
  51. class RMeter : public RDlg
  52. {
  53. public: // Construction/Destruction.
  54. // Default constructor.
  55. RMeter(void);
  56. // Destructor.
  57. ~RMeter(void);
  58. //////////////////////////////////////////////////////////////////////////////
  59. public: // Enums.
  60. ////////////////////////////////////////////////////////////////////////
  61. // Enums.
  62. ////////////////////////////////////////////////////////////////////////
  63. typedef enum
  64. {
  65. Needle, // Analog needle meter.
  66. Digital, // Digital meter.
  67. Bar, // Bar (like progress bar).
  68. Histogram, // Histogram.
  69. NumDisplayTypes // Ceiling.
  70. } DisplayType; // Type of meter display.
  71. typedef enum
  72. {
  73. Percentage, // Info displayed as percentage.
  74. Value, // Info displayed as value.
  75. NumInfoTypes // Ceiling.
  76. } InfoType; // Type of meter info.
  77. public: // Methods.
  78. ////////////////////////////////////////////////////////////////////////
  79. // Methods.
  80. ////////////////////////////////////////////////////////////////////////
  81. //////////////////////// Virtual Overrides /////////////////////////////
  82. // Composes the meter into the image provided. For consistency, you
  83. // should probably always draw the meter.
  84. short Draw( // Returns 0 on success.
  85. RImage* pimDst, // Destination image.
  86. short sDstX = 0, // X position in destination.
  87. short sDstY = 0, // Y position in destination.
  88. short sSrcX = 0, // X position in source.
  89. short sSrcY = 0, // Y position in source.
  90. short sW = 0, // Amount to draw.
  91. short sH = 0, // Amount to draw.
  92. RRect* prc = NULL); // Clip to.
  93. // Activate or deactivate mouse reaction.
  94. void SetActive( // Returns nothing.
  95. short sActive) // TRUE to make active, FALSE otherwise.
  96. {
  97. RDlg::SetActive(sActive);
  98. m_guiMeter.SetActive(sActive);
  99. }
  100. ////////////////////// Original to this class ///////////////////////////
  101. // Set the new value for the meter. Do this BEFORE drawing for most
  102. // accurate results.
  103. long SetNewValue( // Returns previous value.
  104. long lNewVal) // In: New value.
  105. {
  106. long lRes = m_lCurVal;
  107. // Store new val.
  108. m_lCurVal = lNewVal;
  109. // Accumulate total.
  110. m_lCurTotal += lNewVal;
  111. // Count updates.
  112. m_lNumValues++;
  113. // If the new value is less than the min . . .
  114. if (lNewVal < m_lMinValue)
  115. {
  116. m_lMinValue = lNewVal;
  117. }
  118. // If the new value is greater than the max . . .
  119. if (lNewVal > m_lMaxValue)
  120. {
  121. m_lMaxValue = lNewVal;
  122. }
  123. // Return old.
  124. return lRes;
  125. }
  126. // This begins a period to be timed with rspGetMilliseconds().
  127. // Call EndPeriod() to end the period and update the meter.
  128. void StartPeriod(void)
  129. {
  130. m_lStartPeriod = rspGetMilliseconds();
  131. }
  132. // This ends a period and updates the meter with the length of
  133. // the period in milliseconds.
  134. long EndPeriod(void) // Returns period.
  135. {
  136. SetNewValue(rspGetMilliseconds() - m_lStartPeriod);
  137. return m_lCurVal;
  138. }
  139. // Set duration between updates.
  140. void SetUpdateInterval( // Returns nothing.
  141. long lDuration) // Time in milliseconds between updates.
  142. {
  143. m_lDuration = lDuration;
  144. }
  145. // Set the type of meter you want.
  146. void SetType( // Returns nothing.
  147. DisplayType dtType, // In: Type of meter display.
  148. // See Enums above.
  149. InfoType itType) // In: Type of info display.
  150. // See Enums above.
  151. {
  152. m_dtType = dtType;
  153. m_itType = itType;
  154. }
  155. // Set the range for the meter.
  156. void SetRange( // Returns nothing.
  157. long lMin, // Minimum value.
  158. long lMax) // Maximum value.
  159. {
  160. m_lMin = lMin;
  161. m_lMax = lMax;
  162. }
  163. // Set the unit of measurement for info display.
  164. void SetUnit( // Returns nothing.
  165. char* pszUnit) // Unit string (ex: "Mbytes" or "ms").
  166. {
  167. ASSERT(strlen(pszUnit) < sizeof(m_szUnit));
  168. strcpy(m_szUnit, pszUnit);
  169. }
  170. // Set the colors.
  171. void SetColors( // Returns nothing.
  172. U32 u32Background, // Background color.
  173. U32 u32Meter, // Meter color.
  174. U32 u32Needle, // Needle, bar, etc. color.
  175. U32 u32Text, // Text foreground color.
  176. U32 u32Overflow) // Needle color for over/underflow.
  177. {
  178. m_u32BackColor = u32Background;
  179. m_u32Meter = u32Meter;
  180. m_u32Needle = u32Needle;
  181. m_u32TextColor = u32Text;
  182. m_u32Overflow = u32Overflow;
  183. }
  184. // Compose the static portions. Fills the parts of the image
  185. // that don't change.
  186. void Compose( // Returns 0 nothing.
  187. RImage* pimDst = NULL); // In: Destination. NULL == use internal m_im.
  188. // Advance to next meter type. Called by btn callback.
  189. void NextType(void)
  190. {
  191. SetType(
  192. (DisplayType)((m_dtType + 1) % NumDisplayTypes),
  193. m_itType);
  194. Compose();
  195. }
  196. ////////////////////////////////////////////////////////////////////////
  197. // Querries.
  198. ////////////////////////////////////////////////////////////////////////
  199. //////////////////////////////////////////////////////////////////////////////
  200. public: // Static
  201. //////////////////////////////////////////////////////////////////////////////
  202. public: // Querries.
  203. //////////////////////////////////////////////////////////////////////////////
  204. protected: // Internal functions.
  205. // Callbacks from m_guiMeter's Hot.
  206. static void BtnCall(RGuiItem* pgui)
  207. { ((RMeter*)pgui->m_ulUserInstance)->NextType(); }
  208. //////////////////////////////////////////////////////////////////////////////
  209. public: // Member variables.
  210. long m_lCurVal; // Value for next draw.
  211. long m_lStartPeriod; // Start period.
  212. char m_szUnit[MAX_UNIT_LEN + 1]; // Unit of measurement text.
  213. long m_lMin; // Minimum value on meter.
  214. long m_lMax; // Maximum value on meter.
  215. DisplayType m_dtType; // Type of meter display.
  216. InfoType m_itType; // Type of meter info.
  217. U32 m_u32Meter; // Meter color.
  218. U32 m_u32Needle; // Needle, bar, etc. color.
  219. U32 m_u32Overflow; // Needle color for over/underflow.
  220. long m_lDuration; // Time between updates in
  221. // milliseconds.
  222. long m_lNextUpdate; // Time of next update in
  223. // milliseconds.
  224. long m_lCurTotal; // Current total.
  225. long m_lNumValues; // Number of values since
  226. // total was last cleared.
  227. long m_lMaxValue; // Maximum value since
  228. // total was last cleared.
  229. long m_lMinValue; // Minimum value since
  230. // total was last cleared.
  231. RGuiItem m_guiMeter; // Actual meter gui.
  232. // History of values for histogram.
  233. short m_asQHistory[METER_HISTOGRAM_HISTORY];
  234. long m_lQIndex;
  235. protected: // Internal typedefs.
  236. protected: // Protected member variables.
  237. short m_sInfoY;
  238. };
  239. #endif // METER_H
  240. //////////////////////////////////////////////////////////////////////////////
  241. // EOF
  242. //////////////////////////////////////////////////////////////////////////////