guiGraphCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/editor/guiGraphCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "gfx/primBuilder.h"
  30. IMPLEMENT_CONOBJECT( GuiGraphCtrl );
  31. ConsoleDocClass( GuiGraphCtrl,
  32. "@brief A control that plots one or more curves in a chart.\n\n"
  33. "Up to 6 individual curves can be plotted in the graph. Each plotted curve can have its own display style including its "
  34. "own charting style (#plotType) and color (#plotColor).\n\n"
  35. "The data points on each curve can be added in one of two ways:\n\n"
  36. "- Manually by calling addDatum(). This causes new data points to be added to the left end of the plotting curve.\n"
  37. "- Automatically by letting the graph plot the values of a variable over time. This is achieved by calling addAutoPlot "
  38. "and specifying the variable and update frequency.\n\n"
  39. "@tsexample\n"
  40. "// Create a graph that plots a red polyline graph of the FPS counter in a 1 second (1000 milliseconds) interval.\n"
  41. "new GuiGraphCtrl( FPSGraph )\n"
  42. "{\n"
  43. " plotType[ 0 ] = \"PolyLine\";\n"
  44. " plotColor[ 0 ] = \"1 0 0\";\n"
  45. " plotVariable[ 0 ] = \"fps::real\";\n"
  46. " plotInterval[ 0 ] = 1000;\n"
  47. "};\n"
  48. "@endtsexample\n\n"
  49. "@note Each curve has a maximum number of 200 data points it can have at any one time. Adding more data points to a curve "
  50. "will cause older data points to be removed.\n\n"
  51. "@ingroup GuiValues"
  52. );
  53. ImplementEnumType( GuiGraphType,
  54. "The charting style of a single plotting curve in a GuiGraphCtrl.\n\n"
  55. "@ingroup GuiValues" )
  56. { GuiGraphCtrl::Bar, "Bar", "Plot the curve as a bar chart." },
  57. { GuiGraphCtrl::Filled, "Filled", "Plot a filled poly graph that connects the data points on the curve." },
  58. { GuiGraphCtrl::Point, "Point", "Plot each data point on the curve as a single dot." },
  59. { GuiGraphCtrl::Polyline , "PolyLine", "Plot straight lines through the data points of the curve." },
  60. EndImplementEnumType;
  61. //-----------------------------------------------------------------------------
  62. GuiGraphCtrl::GuiGraphCtrl()
  63. : mCenterY( 1.f )
  64. {
  65. dMemset( mAutoPlot, 0, sizeof( mAutoPlot ) );
  66. dMemset( mAutoPlotDelay, 0, sizeof( mAutoPlotDelay ) );
  67. dMemset( mGraphMax, 0, sizeof( mGraphMax ) );
  68. for( S32 i = 0; i < MaxPlots; ++ i )
  69. {
  70. mGraphType[ i ] = Polyline;
  71. VECTOR_SET_ASSOCIATION( mGraphData[ i ] );
  72. }
  73. AssertWarn( MaxPlots == 6, "Only 6 plot colors initialized. Update following code if you change MaxPlots." );
  74. mGraphColor[ 0 ] = ColorF( 1.0, 1.0, 1.0 );
  75. mGraphColor[ 1 ] = ColorF( 1.0, 0.0, 0.0 );
  76. mGraphColor[ 2 ] = ColorF( 0.0, 1.0, 0.0 );
  77. mGraphColor[ 3 ] = ColorF( 0.0, 0.0, 1.0 );
  78. mGraphColor[ 4 ] = ColorF( 0.0, 1.0, 1.0 );
  79. mGraphColor[ 5 ] = ColorF( 0.0, 0.0, 0.0 );
  80. }
  81. //-----------------------------------------------------------------------------
  82. void GuiGraphCtrl::initPersistFields()
  83. {
  84. addGroup( "Graph" );
  85. addField( "centerY", TypeF32, Offset( mCenterY, GuiGraphCtrl ),
  86. "Ratio of where to place the center coordinate of the graph on the Y axis. 0.5=middle height of control.\n\n"
  87. "This allows to account for graphs that have only positive or only negative data points, for example." );
  88. addField( "plotColor", TypeColorF, Offset( mGraphColor, GuiGraphCtrl ), MaxPlots,
  89. "Color to use for the plotting curves in the graph." );
  90. addField( "plotType", TYPEID< GuiGraphType >(), Offset( mGraphType, GuiGraphCtrl ), MaxPlots,
  91. "Charting type of the plotting curves." );
  92. addField( "plotVariable", TypeString, Offset( mAutoPlot, GuiGraphCtrl ), MaxPlots,
  93. "Name of the variable to automatically plot on the curves. If empty, auto-plotting "
  94. "is disabled for the respective curve." );
  95. addField( "plotInterval", TypeS32, Offset( mAutoPlotDelay, GuiGraphCtrl ), MaxPlots,
  96. "Interval between auto-plots of #plotVariable for the respective curve (in milliseconds)." );
  97. endGroup( "Graph" );
  98. Parent::initPersistFields();
  99. }
  100. //-----------------------------------------------------------------------------
  101. void GuiGraphCtrl::onRender(Point2I offset, const RectI &updateRect)
  102. {
  103. if (mBlendSB.isNull())
  104. {
  105. GFXStateBlockDesc desc;
  106. desc.setBlend(true, GFXBlendSrcColor, GFXBlendInvSrcColor);
  107. mBlendSB = GFX->createStateBlock(desc);
  108. desc.setBlend(false, GFXBlendOne, GFXBlendZero);
  109. mSolidSB = GFX->createStateBlock(desc);
  110. }
  111. GFX->setStateBlock( mBlendSB );
  112. GFX->getDrawUtil()->drawRectFill( updateRect, mProfile->mFillColor );
  113. GFX->setStateBlock( mSolidSB );
  114. const Point2I globalPos = getGlobalBounds().point;
  115. const F32 midPointY = F32( globalPos.y ) + F32( getExtent().y ) * mCenterY;
  116. for( S32 k = 0; k < MaxPlots; ++ k )
  117. {
  118. // Check if there is an autoplot and the proper amount of time has passed, if so add datum.
  119. if( mAutoPlot[ k ] && mAutoPlotDelay[ k ] < (Sim::getCurrentTime() - mAutoPlotLastDisplay[ k ] ) )
  120. {
  121. mAutoPlotLastDisplay[ k ] = Sim::getCurrentTime();
  122. addDatum( k, Con::getFloatVariable( mAutoPlot[ k ] ) );
  123. }
  124. // Nothing to graph
  125. if( mGraphData[ k ].size() == 0 )
  126. continue;
  127. // Adjust scale to max value + 5% so we can see high values
  128. F32 Scale = F32( getExtent().y ) / F32( mGraphMax[ k ] * 1.05 );
  129. const S32 numSamples = mGraphData[ k ].size();
  130. switch( mGraphType[ k ] )
  131. {
  132. case Bar:
  133. {
  134. F32 prevOffset = 0;
  135. for( S32 sample = 0; sample < numSamples; ++ sample )
  136. {
  137. PrimBuild::begin( GFXTriangleStrip, 4 );
  138. PrimBuild::color( mGraphColor[ k ] );
  139. F32 offset = F32( getExtent().x ) / F32( MaxDataPoints ) * F32( sample + 1 );
  140. PrimBuild::vertex2f( globalPos.x + prevOffset,
  141. midPointY - ( getDatum( k, sample ) * Scale ) );
  142. PrimBuild::vertex2f( globalPos.x + offset,
  143. midPointY - ( getDatum( k, sample ) * Scale ) );
  144. PrimBuild::vertex2f( globalPos.x + offset,
  145. midPointY );
  146. PrimBuild::vertex2f( globalPos.x + prevOffset,
  147. midPointY );
  148. prevOffset = offset;
  149. PrimBuild::end();
  150. }
  151. break;
  152. }
  153. case Filled:
  154. {
  155. PrimBuild::begin( GFXTriangleStrip, numSamples * 2 ); // Max size correct? -pw
  156. PrimBuild::color( mGraphColor[ k ] );
  157. for( S32 sample = 0; sample < numSamples; ++ sample )
  158. {
  159. F32 offset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  160. PrimBuild::vertex2f( globalPos.x + offset,
  161. midPointY );
  162. PrimBuild::vertex2f( globalPos.x + offset,
  163. midPointY - ( getDatum( k, sample ) * Scale ) );
  164. }
  165. PrimBuild::end();
  166. break;
  167. }
  168. case Point:
  169. case Polyline:
  170. {
  171. if( mGraphType[ k ] == Point )
  172. PrimBuild::begin( GFXPointList, numSamples ); // Max size correct? -pw
  173. else
  174. PrimBuild::begin( GFXLineStrip, numSamples );
  175. PrimBuild::color( mGraphColor[ k ] );
  176. for( S32 sample = 0; sample < numSamples; ++ sample )
  177. {
  178. F32 offset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  179. PrimBuild::vertex2f( globalPos.x + offset,
  180. midPointY - ( getDatum( k, sample ) * Scale ) );
  181. }
  182. PrimBuild::end();
  183. break;
  184. }
  185. }
  186. }
  187. if( mProfile->mBorder )
  188. {
  189. RectI rect( offset.x, offset.y, getWidth(), getHeight() );
  190. GFX->getDrawUtil()->drawRect( rect, mProfile->mBorderColor );
  191. }
  192. renderChildControls( offset, updateRect );
  193. }
  194. //-----------------------------------------------------------------------------
  195. void GuiGraphCtrl::addDatum(S32 plotID, F32 v)
  196. {
  197. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  198. // Add the data and trim the vector...
  199. mGraphData[ plotID ].push_front( v );
  200. if( mGraphData[ plotID ].size() > MaxDataPoints )
  201. mGraphData[ plotID ].pop_back();
  202. // Keep record of maximum data value for scaling purposes.
  203. if( v > mGraphMax[ plotID ] )
  204. mGraphMax[ plotID ] = v;
  205. }
  206. //-----------------------------------------------------------------------------
  207. F32 GuiGraphCtrl::getDatum( S32 plotID, S32 sample)
  208. {
  209. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  210. AssertFatal(sample > -1 && sample < MaxDataPoints, "Invalid sample specified!");
  211. return mGraphData[ plotID ][sample];
  212. }
  213. //-----------------------------------------------------------------------------
  214. void GuiGraphCtrl::addAutoPlot( S32 plotID, const char* variable, S32 update )
  215. {
  216. mAutoPlot[ plotID ] = StringTable->insert( variable );
  217. mAutoPlotDelay[ plotID ] = update;
  218. }
  219. //-----------------------------------------------------------------------------
  220. void GuiGraphCtrl::removeAutoPlot( S32 plotID )
  221. {
  222. mAutoPlot[ plotID ] = NULL;
  223. }
  224. //-----------------------------------------------------------------------------
  225. void GuiGraphCtrl::setGraphType( S32 plotID, GraphType graphType )
  226. {
  227. AssertFatal( plotID > -1 && plotID < MaxPlots, "Invalid plot specified!" );
  228. mGraphType[ plotID ] = graphType;
  229. }
  230. //=============================================================================
  231. // Console Methods.
  232. //=============================================================================
  233. // MARK: ---- Console Methods ----
  234. //-----------------------------------------------------------------------------
  235. DefineEngineMethod( GuiGraphCtrl, addDatum, void, ( S32 plotId, F32 value ),,
  236. "Add a data point to the plot's curve.\n\n"
  237. "@param plotId Index of the plotting curve to which to add the data point. Must be 0<=plotId<6.\n"
  238. "@param value Value of the data point to add to the curve.\n\n"
  239. "@note Data values are added to the @b left end of the plotting curve.\n\n"
  240. "@note A maximum number of 200 data points can be added to any single plotting curve at any one time. If "
  241. "this limit is exceeded, data points on the right end of the curve are culled." )
  242. {
  243. if( plotId > object->MaxPlots )
  244. {
  245. Con::errorf( "GuiGraphCtrl::addDatum - 'plotId' out of range: %i", plotId );
  246. return;
  247. }
  248. object->addDatum( plotId, value );
  249. }
  250. //-----------------------------------------------------------------------------
  251. DefineEngineMethod( GuiGraphCtrl, getDatum, F32, ( S32 plotId, S32 index ),,
  252. "Get a data point on the given plotting curve.\n\n"
  253. "@param plotId Index of the plotting curve from which to fetch the data point. Must be 0<=plotId<6.\n"
  254. "@param index Index of the data point on the curve.\n"
  255. "@return The value of the data point or -1 if @a plotId or @a index are out of range." )
  256. {
  257. if( plotId > object->MaxPlots )
  258. {
  259. Con::errorf( "GuiGraphCtrl::getDatum - 'plotId' out of range: %i", plotId );
  260. return -1.f;
  261. }
  262. if( index > object->MaxDataPoints)
  263. {
  264. Con::errorf( "GuiGraphCtrl::getDatum - Data point index out of range: %i", index );
  265. return -1.f;
  266. }
  267. return object->getDatum( plotId, index );
  268. }
  269. //-----------------------------------------------------------------------------
  270. DefineEngineMethod( GuiGraphCtrl, addAutoPlot, void, ( S32 plotId, const char* variable, S32 updateFrequency ),,
  271. "Sets up the given plotting curve to automatically plot the value of the @a variable with a "
  272. "frequency of @a updateFrequency.\n"
  273. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  274. "@param variable Name of the global variable.\n"
  275. "@param updateFrequency Frequency with which to add new data points to the plotting curve (in milliseconds).\n"
  276. "@tsexample\n"
  277. "// Plot FPS counter at 1 second intervals.\n"
  278. "%graph.addAutoPlot( 0, \"fps::real\", 1000 );\n"
  279. "@endtsexample" )
  280. {
  281. if( plotId > object->MaxPlots )
  282. {
  283. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  284. return;
  285. }
  286. object->addAutoPlot( plotId, variable, updateFrequency );
  287. }
  288. //-----------------------------------------------------------------------------
  289. DefineEngineMethod( GuiGraphCtrl, removeAutoPlot, void, ( S32 plotId ),,
  290. "Stop automatic variable plotting for the given curve.\n"
  291. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n" )
  292. {
  293. if( plotId > object->MaxPlots )
  294. {
  295. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  296. return;
  297. }
  298. object->removeAutoPlot( plotId );
  299. }
  300. //-----------------------------------------------------------------------------
  301. DefineEngineMethod( GuiGraphCtrl, setGraphType, void, ( S32 plotId, GuiGraphType graphType ),,
  302. "Change the charting type of the given plotting curve.\n"
  303. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  304. "@param graphType Charting type to use for the curve.\n"
  305. "@note Instead of calling this method, you can directly assign to #plotType." )
  306. {
  307. if( plotId > object->MaxPlots )
  308. {
  309. Con::errorf( "GuiGraphCtrl::setGraphType - 'plotId' out of range: %i", plotId );
  310. return;
  311. }
  312. object->setGraphType( plotId, graphType );
  313. }
  314. //-----------------------------------------------------------------------------
  315. ConsoleMethod( GuiGraphCtrl, matchScale, void, 3, GuiGraphCtrl::MaxPlots + 2, "( int plotID1, int plotID2, ... ) "
  316. "Set the scale of all specified plots to the maximum scale among them.\n\n"
  317. "@param plotID1 Index of plotting curve.\n"
  318. "@param plotID2 Index of plotting curve." )
  319. {
  320. F32 max = 0;
  321. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  322. {
  323. S32 plotID = dAtoi( argv[ 2 + i ] );
  324. if( plotID > object->MaxPlots )
  325. {
  326. Con::errorf( "GuiGraphCtrl::matchScale - Plot ID out of range: %i", plotID );
  327. return;
  328. }
  329. max = getMax( object->getMax( plotID ), max );
  330. }
  331. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  332. object->setMax( dAtoi( argv[ 2 + i ] ), max );
  333. }