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 ] = LinearColorF( 1.0, 1.0, 1.0 );
  75. mGraphColor[ 1 ] = LinearColorF( 1.0, 0.0, 0.0 );
  76. mGraphColor[ 2 ] = LinearColorF( 0.0, 1.0, 0.0 );
  77. mGraphColor[ 3 ] = LinearColorF( 0.0, 0.0, 1.0 );
  78. mGraphColor[ 4 ] = LinearColorF( 0.0, 1.0, 1.0 );
  79. mGraphColor[ 5 ] = LinearColorF( 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. F32 graphOffset;
  131. switch( mGraphType[ k ] )
  132. {
  133. case Bar:
  134. {
  135. F32 prevOffset = 0;
  136. for( S32 sample = 0; sample < numSamples; ++ sample )
  137. {
  138. PrimBuild::begin( GFXTriangleStrip, 4 );
  139. PrimBuild::color( mGraphColor[ k ] );
  140. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints ) * F32( sample + 1 );
  141. PrimBuild::vertex2f( globalPos.x + prevOffset,
  142. midPointY - ( getDatum( k, sample ) * Scale ) );
  143. PrimBuild::vertex2f( globalPos.x + graphOffset,
  144. midPointY - ( getDatum( k, sample ) * Scale ) );
  145. PrimBuild::vertex2f( globalPos.x + graphOffset,
  146. midPointY );
  147. PrimBuild::vertex2f( globalPos.x + prevOffset,
  148. midPointY );
  149. prevOffset = graphOffset;
  150. PrimBuild::end();
  151. }
  152. break;
  153. }
  154. case Filled:
  155. {
  156. PrimBuild::begin( GFXTriangleStrip, numSamples * 2 ); // Max size correct? -pw
  157. PrimBuild::color( mGraphColor[ k ] );
  158. for( S32 sample = 0; sample < numSamples; ++ sample )
  159. {
  160. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  161. PrimBuild::vertex2f( globalPos.x + graphOffset,
  162. midPointY );
  163. PrimBuild::vertex2f( globalPos.x + graphOffset,
  164. midPointY - ( getDatum( k, sample ) * Scale ) );
  165. }
  166. PrimBuild::end();
  167. break;
  168. }
  169. case Point:
  170. case Polyline:
  171. {
  172. if( mGraphType[ k ] == Point )
  173. PrimBuild::begin( GFXPointList, numSamples ); // Max size correct? -pw
  174. else
  175. PrimBuild::begin( GFXLineStrip, numSamples );
  176. PrimBuild::color( mGraphColor[ k ] );
  177. for( S32 sample = 0; sample < numSamples; ++ sample )
  178. {
  179. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  180. PrimBuild::vertex2f( globalPos.x + graphOffset,
  181. midPointY - ( getDatum( k, sample ) * Scale ) );
  182. }
  183. PrimBuild::end();
  184. break;
  185. }
  186. }
  187. }
  188. if( mProfile->mBorder )
  189. {
  190. RectI rect( offset.x, offset.y, getWidth(), getHeight() );
  191. GFX->getDrawUtil()->drawRect( rect, mProfile->mBorderColor );
  192. }
  193. renderChildControls( offset, updateRect );
  194. }
  195. //-----------------------------------------------------------------------------
  196. void GuiGraphCtrl::addDatum(S32 plotID, F32 v)
  197. {
  198. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  199. // Add the data and trim the vector...
  200. mGraphData[ plotID ].push_front( v );
  201. if( mGraphData[ plotID ].size() > MaxDataPoints )
  202. mGraphData[ plotID ].pop_back();
  203. // Keep record of maximum data value for scaling purposes.
  204. if( v > mGraphMax[ plotID ] )
  205. mGraphMax[ plotID ] = v;
  206. }
  207. //-----------------------------------------------------------------------------
  208. F32 GuiGraphCtrl::getDatum( S32 plotID, S32 sample)
  209. {
  210. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  211. AssertFatal(sample > -1 && sample < MaxDataPoints, "Invalid sample specified!");
  212. return mGraphData[ plotID ][sample];
  213. }
  214. //-----------------------------------------------------------------------------
  215. void GuiGraphCtrl::addAutoPlot( S32 plotID, const char* variable, S32 update )
  216. {
  217. mAutoPlot[ plotID ] = StringTable->insert( variable );
  218. mAutoPlotDelay[ plotID ] = update;
  219. }
  220. //-----------------------------------------------------------------------------
  221. void GuiGraphCtrl::removeAutoPlot( S32 plotID )
  222. {
  223. mAutoPlot[ plotID ] = NULL;
  224. }
  225. //-----------------------------------------------------------------------------
  226. void GuiGraphCtrl::setGraphType( S32 plotID, GraphType graphType )
  227. {
  228. AssertFatal( plotID > -1 && plotID < MaxPlots, "Invalid plot specified!" );
  229. mGraphType[ plotID ] = graphType;
  230. }
  231. //=============================================================================
  232. // Console Methods.
  233. //=============================================================================
  234. // MARK: ---- Console Methods ----
  235. //-----------------------------------------------------------------------------
  236. DefineEngineMethod( GuiGraphCtrl, addDatum, void, ( S32 plotId, F32 value ),,
  237. "Add a data point to the plot's curve.\n\n"
  238. "@param plotId Index of the plotting curve to which to add the data point. Must be 0<=plotId<6.\n"
  239. "@param value Value of the data point to add to the curve.\n\n"
  240. "@note Data values are added to the @b left end of the plotting curve.\n\n"
  241. "@note A maximum number of 200 data points can be added to any single plotting curve at any one time. If "
  242. "this limit is exceeded, data points on the right end of the curve are culled." )
  243. {
  244. if( plotId > object->MaxPlots )
  245. {
  246. Con::errorf( "GuiGraphCtrl::addDatum - 'plotId' out of range: %i", plotId );
  247. return;
  248. }
  249. object->addDatum( plotId, value );
  250. }
  251. //-----------------------------------------------------------------------------
  252. DefineEngineMethod( GuiGraphCtrl, getDatum, F32, ( S32 plotId, S32 index ),,
  253. "Get a data point on the given plotting curve.\n\n"
  254. "@param plotId Index of the plotting curve from which to fetch the data point. Must be 0<=plotId<6.\n"
  255. "@param index Index of the data point on the curve.\n"
  256. "@return The value of the data point or -1 if @a plotId or @a index are out of range." )
  257. {
  258. if( plotId > object->MaxPlots )
  259. {
  260. Con::errorf( "GuiGraphCtrl::getDatum - 'plotId' out of range: %i", plotId );
  261. return -1.f;
  262. }
  263. if( index > object->MaxDataPoints)
  264. {
  265. Con::errorf( "GuiGraphCtrl::getDatum - Data point index out of range: %i", index );
  266. return -1.f;
  267. }
  268. return object->getDatum( plotId, index );
  269. }
  270. //-----------------------------------------------------------------------------
  271. DefineEngineMethod( GuiGraphCtrl, addAutoPlot, void, ( S32 plotId, const char* variable, S32 updateFrequency ),,
  272. "Sets up the given plotting curve to automatically plot the value of the @a variable with a "
  273. "frequency of @a updateFrequency.\n"
  274. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  275. "@param variable Name of the global variable.\n"
  276. "@param updateFrequency Frequency with which to add new data points to the plotting curve (in milliseconds).\n"
  277. "@tsexample\n"
  278. "// Plot FPS counter at 1 second intervals.\n"
  279. "%graph.addAutoPlot( 0, \"fps::real\", 1000 );\n"
  280. "@endtsexample" )
  281. {
  282. if( plotId > object->MaxPlots )
  283. {
  284. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  285. return;
  286. }
  287. object->addAutoPlot( plotId, variable, updateFrequency );
  288. }
  289. //-----------------------------------------------------------------------------
  290. DefineEngineMethod( GuiGraphCtrl, removeAutoPlot, void, ( S32 plotId ),,
  291. "Stop automatic variable plotting for the given curve.\n"
  292. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n" )
  293. {
  294. if( plotId > object->MaxPlots )
  295. {
  296. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  297. return;
  298. }
  299. object->removeAutoPlot( plotId );
  300. }
  301. //-----------------------------------------------------------------------------
  302. DefineEngineMethod( GuiGraphCtrl, setGraphType, void, ( S32 plotId, GuiGraphType graphType ),,
  303. "Change the charting type of the given plotting curve.\n"
  304. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  305. "@param graphType Charting type to use for the curve.\n"
  306. "@note Instead of calling this method, you can directly assign to #plotType." )
  307. {
  308. if( plotId > object->MaxPlots )
  309. {
  310. Con::errorf( "GuiGraphCtrl::setGraphType - 'plotId' out of range: %i", plotId );
  311. return;
  312. }
  313. object->setGraphType( plotId, graphType );
  314. }
  315. //-----------------------------------------------------------------------------
  316. DefineEngineStringlyVariadicMethod( GuiGraphCtrl, matchScale, void, 3, GuiGraphCtrl::MaxPlots + 2, "( int plotID1, int plotID2, ... ) "
  317. "Set the scale of all specified plots to the maximum scale among them.\n\n"
  318. "@param plotID1 Index of plotting curve.\n"
  319. "@param plotID2 Index of plotting curve." )
  320. {
  321. F32 max = 0;
  322. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  323. {
  324. S32 plotID = dAtoi( argv[ 2 + i ] );
  325. if( plotID > object->MaxPlots )
  326. {
  327. Con::errorf( "GuiGraphCtrl::matchScale - Plot ID out of range: %i", plotID );
  328. return;
  329. }
  330. max = getMax( object->getMax( plotID ), max );
  331. }
  332. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  333. object->setMax( dAtoi( argv[ 2 + i ] ), max );
  334. }