guiGraphCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. docsURL;
  85. addGroup( "Graph" );
  86. addField( "centerY", TypeF32, Offset( mCenterY, GuiGraphCtrl ),
  87. "Ratio of where to place the center coordinate of the graph on the Y axis. 0.5=middle height of control.\n\n"
  88. "This allows to account for graphs that have only positive or only negative data points, for example." );
  89. addField( "plotColor", TypeColorF, Offset( mGraphColor, GuiGraphCtrl ), MaxPlots,
  90. "Color to use for the plotting curves in the graph." );
  91. addField( "plotType", TYPEID< GuiGraphType >(), Offset( mGraphType, GuiGraphCtrl ), MaxPlots,
  92. "Charting type of the plotting curves." );
  93. addField( "plotVariable", TypeString, Offset( mAutoPlot, GuiGraphCtrl ), MaxPlots,
  94. "Name of the variable to automatically plot on the curves. If empty, auto-plotting "
  95. "is disabled for the respective curve." );
  96. addField( "plotInterval", TypeS32, Offset( mAutoPlotDelay, GuiGraphCtrl ), MaxPlots,
  97. "Interval between auto-plots of #plotVariable for the respective curve (in milliseconds)." );
  98. endGroup( "Graph" );
  99. Parent::initPersistFields();
  100. }
  101. //-----------------------------------------------------------------------------
  102. void GuiGraphCtrl::onRender(Point2I offset, const RectI &updateRect)
  103. {
  104. if (mBlendSB.isNull())
  105. {
  106. GFXStateBlockDesc desc;
  107. desc.setBlend(true, GFXBlendSrcColor, GFXBlendInvSrcColor);
  108. mBlendSB = GFX->createStateBlock(desc);
  109. desc.setBlend(false, GFXBlendOne, GFXBlendZero);
  110. mSolidSB = GFX->createStateBlock(desc);
  111. }
  112. GFX->setStateBlock( mBlendSB );
  113. GFX->getDrawUtil()->drawRectFill( updateRect, mProfile->mFillColor );
  114. GFX->setStateBlock( mSolidSB );
  115. const Point2I globalPos = getGlobalBounds().point;
  116. const F32 midPointY = F32( globalPos.y ) + F32( getExtent().y ) * mCenterY;
  117. for( S32 k = 0; k < MaxPlots; ++ k )
  118. {
  119. // Check if there is an autoplot and the proper amount of time has passed, if so add datum.
  120. if( mAutoPlot[ k ] && mAutoPlotDelay[ k ] < (Sim::getCurrentTime() - mAutoPlotLastDisplay[ k ] ) )
  121. {
  122. mAutoPlotLastDisplay[ k ] = Sim::getCurrentTime();
  123. addDatum( k, Con::getFloatVariable( mAutoPlot[ k ] ) );
  124. }
  125. // Nothing to graph
  126. if( mGraphData[ k ].size() == 0 )
  127. continue;
  128. // Adjust scale to max value + 5% so we can see high values
  129. F32 Scale = F32( getExtent().y ) / F32( mGraphMax[ k ] * 1.05 );
  130. const S32 numSamples = mGraphData[ k ].size();
  131. F32 graphOffset;
  132. switch( mGraphType[ k ] )
  133. {
  134. case Bar:
  135. {
  136. F32 prevOffset = 0;
  137. for( S32 sample = 0; sample < numSamples; ++ sample )
  138. {
  139. PrimBuild::begin( GFXTriangleStrip, 4 );
  140. PrimBuild::color( mGraphColor[ k ] );
  141. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints ) * F32( sample + 1 );
  142. PrimBuild::vertex2f( globalPos.x + prevOffset,
  143. midPointY - ( getDatum( k, sample ) * Scale ) );
  144. PrimBuild::vertex2f( globalPos.x + graphOffset,
  145. midPointY - ( getDatum( k, sample ) * Scale ) );
  146. PrimBuild::vertex2f( globalPos.x + graphOffset,
  147. midPointY );
  148. PrimBuild::vertex2f( globalPos.x + prevOffset,
  149. midPointY );
  150. prevOffset = graphOffset;
  151. PrimBuild::end();
  152. }
  153. break;
  154. }
  155. case Filled:
  156. {
  157. PrimBuild::begin( GFXTriangleStrip, numSamples * 2 ); // Max size correct? -pw
  158. PrimBuild::color( mGraphColor[ k ] );
  159. for( S32 sample = 0; sample < numSamples; ++ sample )
  160. {
  161. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  162. PrimBuild::vertex2f( globalPos.x + graphOffset,
  163. midPointY );
  164. PrimBuild::vertex2f( globalPos.x + graphOffset,
  165. midPointY - ( getDatum( k, sample ) * Scale ) );
  166. }
  167. PrimBuild::end();
  168. break;
  169. }
  170. case Point:
  171. case Polyline:
  172. {
  173. if( mGraphType[ k ] == Point )
  174. PrimBuild::begin( GFXPointList, numSamples ); // Max size correct? -pw
  175. else
  176. PrimBuild::begin( GFXLineStrip, numSamples );
  177. PrimBuild::color( mGraphColor[ k ] );
  178. for( S32 sample = 0; sample < numSamples; ++ sample )
  179. {
  180. graphOffset = F32( getExtent().x ) / F32( MaxDataPoints - 1 ) * F32( sample );
  181. PrimBuild::vertex2f( globalPos.x + graphOffset,
  182. midPointY - ( getDatum( k, sample ) * Scale ) );
  183. }
  184. PrimBuild::end();
  185. break;
  186. }
  187. }
  188. }
  189. if( mProfile->mBorder )
  190. {
  191. RectI rect( offset.x, offset.y, getWidth(), getHeight() );
  192. GFX->getDrawUtil()->drawRect( rect, mProfile->mBorderColor );
  193. }
  194. renderChildControls( offset, updateRect );
  195. }
  196. //-----------------------------------------------------------------------------
  197. void GuiGraphCtrl::addDatum(S32 plotID, F32 v)
  198. {
  199. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  200. // Add the data and trim the vector...
  201. mGraphData[ plotID ].push_front( v );
  202. if( mGraphData[ plotID ].size() > MaxDataPoints )
  203. mGraphData[ plotID ].pop_back();
  204. // Keep record of maximum data value for scaling purposes.
  205. if( v > mGraphMax[ plotID ] )
  206. mGraphMax[ plotID ] = v;
  207. }
  208. //-----------------------------------------------------------------------------
  209. F32 GuiGraphCtrl::getDatum( S32 plotID, S32 sample)
  210. {
  211. AssertFatal(plotID > -1 && plotID < MaxPlots, "Invalid plot specified!");
  212. AssertFatal(sample > -1 && sample < MaxDataPoints, "Invalid sample specified!");
  213. return mGraphData[ plotID ][sample];
  214. }
  215. //-----------------------------------------------------------------------------
  216. void GuiGraphCtrl::addAutoPlot( S32 plotID, const char* variable, S32 update )
  217. {
  218. mAutoPlot[ plotID ] = StringTable->insert( variable );
  219. mAutoPlotDelay[ plotID ] = update;
  220. }
  221. //-----------------------------------------------------------------------------
  222. void GuiGraphCtrl::removeAutoPlot( S32 plotID )
  223. {
  224. mAutoPlot[ plotID ] = NULL;
  225. }
  226. //-----------------------------------------------------------------------------
  227. void GuiGraphCtrl::setGraphType( S32 plotID, GraphType graphType )
  228. {
  229. AssertFatal( plotID > -1 && plotID < MaxPlots, "Invalid plot specified!" );
  230. mGraphType[ plotID ] = graphType;
  231. }
  232. //=============================================================================
  233. // Console Methods.
  234. //=============================================================================
  235. // MARK: ---- Console Methods ----
  236. //-----------------------------------------------------------------------------
  237. DefineEngineMethod( GuiGraphCtrl, addDatum, void, ( S32 plotId, F32 value ),,
  238. "Add a data point to the plot's curve.\n\n"
  239. "@param plotId Index of the plotting curve to which to add the data point. Must be 0<=plotId<6.\n"
  240. "@param value Value of the data point to add to the curve.\n\n"
  241. "@note Data values are added to the @b left end of the plotting curve.\n\n"
  242. "@note A maximum number of 200 data points can be added to any single plotting curve at any one time. If "
  243. "this limit is exceeded, data points on the right end of the curve are culled." )
  244. {
  245. if( plotId > object->MaxPlots )
  246. {
  247. Con::errorf( "GuiGraphCtrl::addDatum - 'plotId' out of range: %i", plotId );
  248. return;
  249. }
  250. object->addDatum( plotId, value );
  251. }
  252. //-----------------------------------------------------------------------------
  253. DefineEngineMethod( GuiGraphCtrl, getDatum, F32, ( S32 plotId, S32 index ),,
  254. "Get a data point on the given plotting curve.\n\n"
  255. "@param plotId Index of the plotting curve from which to fetch the data point. Must be 0<=plotId<6.\n"
  256. "@param index Index of the data point on the curve.\n"
  257. "@return The value of the data point or -1 if @a plotId or @a index are out of range." )
  258. {
  259. if( plotId > object->MaxPlots )
  260. {
  261. Con::errorf( "GuiGraphCtrl::getDatum - 'plotId' out of range: %i", plotId );
  262. return -1.f;
  263. }
  264. if( index > object->MaxDataPoints)
  265. {
  266. Con::errorf( "GuiGraphCtrl::getDatum - Data point index out of range: %i", index );
  267. return -1.f;
  268. }
  269. return object->getDatum( plotId, index );
  270. }
  271. //-----------------------------------------------------------------------------
  272. DefineEngineMethod( GuiGraphCtrl, addAutoPlot, void, ( S32 plotId, const char* variable, S32 updateFrequency ),,
  273. "Sets up the given plotting curve to automatically plot the value of the @a variable with a "
  274. "frequency of @a updateFrequency.\n"
  275. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  276. "@param variable Name of the global variable.\n"
  277. "@param updateFrequency Frequency with which to add new data points to the plotting curve (in milliseconds).\n"
  278. "@tsexample\n"
  279. "// Plot FPS counter at 1 second intervals.\n"
  280. "%graph.addAutoPlot( 0, \"fps::real\", 1000 );\n"
  281. "@endtsexample" )
  282. {
  283. if( plotId > object->MaxPlots )
  284. {
  285. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  286. return;
  287. }
  288. object->addAutoPlot( plotId, variable, updateFrequency );
  289. }
  290. //-----------------------------------------------------------------------------
  291. DefineEngineMethod( GuiGraphCtrl, removeAutoPlot, void, ( S32 plotId ),,
  292. "Stop automatic variable plotting for the given curve.\n"
  293. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n" )
  294. {
  295. if( plotId > object->MaxPlots )
  296. {
  297. Con::errorf( "GuiGraphCtrl::removeAutoPlot - 'plotId' out of range: %i", plotId );
  298. return;
  299. }
  300. object->removeAutoPlot( plotId );
  301. }
  302. //-----------------------------------------------------------------------------
  303. DefineEngineMethod( GuiGraphCtrl, setGraphType, void, ( S32 plotId, GuiGraphType graphType ),,
  304. "Change the charting type of the given plotting curve.\n"
  305. "@param plotId Index of the plotting curve. Must be 0<=plotId<6.\n"
  306. "@param graphType Charting type to use for the curve.\n"
  307. "@note Instead of calling this method, you can directly assign to #plotType." )
  308. {
  309. if( plotId > object->MaxPlots )
  310. {
  311. Con::errorf( "GuiGraphCtrl::setGraphType - 'plotId' out of range: %i", plotId );
  312. return;
  313. }
  314. object->setGraphType( plotId, graphType );
  315. }
  316. //-----------------------------------------------------------------------------
  317. DefineEngineStringlyVariadicMethod( GuiGraphCtrl, matchScale, void, 3, GuiGraphCtrl::MaxPlots + 2, "( int plotID1, int plotID2, ... ) "
  318. "Set the scale of all specified plots to the maximum scale among them.\n\n"
  319. "@param plotID1 Index of plotting curve.\n"
  320. "@param plotID2 Index of plotting curve." )
  321. {
  322. F32 max = 0;
  323. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  324. {
  325. S32 plotID = dAtoi( argv[ 2 + i ] );
  326. if( plotID > object->MaxPlots )
  327. {
  328. Con::errorf( "GuiGraphCtrl::matchScale - Plot ID out of range: %i", plotID );
  329. return;
  330. }
  331. max = getMax( object->getMax( plotID ), max );
  332. }
  333. for( S32 i = 0; i < ( argc - 2 ); ++ i )
  334. object->setMax( dAtoi( argv[ 2 + i ] ), max );
  335. }