guiProgressBitmapCtrl.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/game/guiProgressBitmapCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. IMPLEMENT_CONOBJECT( GuiProgressBitmapCtrl );
  29. ConsoleDocClass( GuiProgressBitmapCtrl,
  30. "@brief A horizontal progress bar rendered from a repeating image.\n\n"
  31. "This class is used give progress feedback to the user. Unlike GuiProgressCtrl which simply "
  32. "renders a filled rectangle, GuiProgressBitmapCtrl renders the bar using a bitmap.\n\n"
  33. "This bitmap can either be simple, plain image which is then stretched into the current extents of the bar "
  34. "as it fills up or it can be a bitmap array with three entries. In the case of a bitmap array, the "
  35. "first entry in the array is used to render the left cap of the bar and the third entry in the array "
  36. "is used to render the right cap of the bar. The second entry is streched in-between the two caps.\n\n"
  37. "@tsexample\n"
  38. "// This example shows one way to break down a long-running computation into phases\n"
  39. "// and incrementally update a progress bar between the phases.\n"
  40. "\n"
  41. "new GuiProgressBitmapCtrl( Progress )\n"
  42. "{\n"
  43. " bitmap = \"core/art/gui/images/loading\";\n"
  44. " extent = \"300 50\";\n"
  45. " position = \"100 100\";\n"
  46. "};\n"
  47. "\n"
  48. "// Put the control on the canvas.\n"
  49. "%wrapper = new GuiControl();\n"
  50. "%wrapper.addObject( Progress );\n"
  51. "Canvas.pushDialog( %wrapper );\n"
  52. "\n"
  53. "// Start the computation.\n"
  54. "schedule( 1, 0, \"phase1\" );\n"
  55. "\n"
  56. "function phase1()\n"
  57. "{\n"
  58. " Progress.setValue( 0 );\n"
  59. "\n"
  60. " // Perform some computation.\n"
  61. " //...\n"
  62. "\n"
  63. " // Update progress.\n"
  64. " Progress.setValue( 0.25 );\n"
  65. "\n"
  66. " // Schedule next phase. Don't call directly so engine gets a change to run refresh.\n"
  67. " schedule( 1, 0, \"phase2\" );\n"
  68. "}\n"
  69. "\n"
  70. "function phase2()\n"
  71. "{\n"
  72. " // Perform some computation.\n"
  73. " //...\n"
  74. "\n"
  75. " // Update progress.\n"
  76. " Progress.setValue( 0.7 );\n"
  77. "\n"
  78. " // Schedule next phase. Don't call directly so engine gets a change to run refresh.\n"
  79. " schedule( 1, 0, \"phase3\" );\n"
  80. "}\n"
  81. "\n"
  82. "function phase3()\n"
  83. "{\n"
  84. " // Perform some computation.\n"
  85. " //...\n"
  86. "\n"
  87. " // Update progress.\n"
  88. " Progress.setValue( 0.9 );\n"
  89. "\n"
  90. " // Schedule next phase. Don't call directly so engine gets a change to run refresh.\n"
  91. " schedule( 1, 0, \"phase4\" );\n"
  92. "}\n"
  93. "\n"
  94. "function phase4()\n"
  95. "{\n"
  96. " // Perform some computation.\n"
  97. " //...\n"
  98. "\n"
  99. " // Final update of progress.\n"
  100. " Progress.setValue( 1.0 );\n"
  101. "}\n"
  102. "@endtsexample\n\n"
  103. "@see GuiProgressCtrl\n\n"
  104. "@ingroup GuiValues"
  105. );
  106. //-----------------------------------------------------------------------------
  107. GuiProgressBitmapCtrl::GuiProgressBitmapCtrl()
  108. : mProgress( 0.f ),
  109. mBitmapName( StringTable->EmptyString() ),
  110. mUseVariable( false ),
  111. mTile( false )
  112. {
  113. }
  114. //-----------------------------------------------------------------------------
  115. void GuiProgressBitmapCtrl::initPersistFields()
  116. {
  117. addProtectedField( "bitmap", TypeFilename, Offset( mBitmapName, GuiProgressBitmapCtrl ),
  118. _setBitmap, defaultProtectedGetFn,
  119. "~Path to the bitmap file to use for rendering the progress bar.\n\n"
  120. "If the profile assigned to the control already has a bitmap assigned, this property need not be "
  121. "set in which case the bitmap from the profile is used."
  122. );
  123. Parent::initPersistFields();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void GuiProgressBitmapCtrl::setBitmap( const char* name )
  127. {
  128. bool awake = mAwake;
  129. if( awake )
  130. onSleep();
  131. mBitmapName = StringTable->insert( name );
  132. if( awake )
  133. onWake();
  134. setUpdate();
  135. }
  136. //-----------------------------------------------------------------------------
  137. const char* GuiProgressBitmapCtrl::getScriptValue()
  138. {
  139. static const U32 bufSize = 64;
  140. char * ret = Con::getReturnBuffer(bufSize);
  141. dSprintf(ret, bufSize, "%g", mProgress);
  142. return ret;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void GuiProgressBitmapCtrl::setScriptValue(const char *value)
  146. {
  147. //set the value
  148. if (! value)
  149. mProgress = 0.0f;
  150. else
  151. mProgress = dAtof(value);
  152. //validate the value
  153. mProgress = mClampF(mProgress, 0.f, 1.f);
  154. setUpdate();
  155. }
  156. //-----------------------------------------------------------------------------
  157. void GuiProgressBitmapCtrl::onPreRender()
  158. {
  159. const char * var = getVariable();
  160. if(var)
  161. {
  162. F32 value = mClampF(dAtof(var), 0.f, 1.f);
  163. if(value != mProgress)
  164. {
  165. mProgress = value;
  166. setUpdate();
  167. }
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. void GuiProgressBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  172. {
  173. RectI ctrlRect(offset, getExtent());
  174. //grab lowest dimension
  175. if(getHeight() <= getWidth())
  176. mDim = getHeight();
  177. else
  178. mDim = getWidth();
  179. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  180. drawUtil->clearBitmapModulation();
  181. if(mNumberOfBitmaps == 1)
  182. {
  183. //draw the progress with image
  184. S32 width = (S32)((F32)(getWidth()) * mProgress);
  185. if (width > 0)
  186. {
  187. //drawing stretch bitmap
  188. RectI progressRect = ctrlRect;
  189. progressRect.extent.x = width;
  190. drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRect, mProfile->mBitmapArrayRects[0]);
  191. }
  192. }
  193. else if(mNumberOfBitmaps >= 3)
  194. {
  195. //drawing left-end bitmap
  196. RectI progressRectLeft(ctrlRect.point.x, ctrlRect.point.y, mDim, mDim);
  197. drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRectLeft, mProfile->mBitmapArrayRects[0]);
  198. //draw the progress with image
  199. S32 width = (S32)((F32)(getWidth()) * mProgress);
  200. if (width > mDim)
  201. {
  202. //drawing stretch bitmap
  203. RectI progressRect = ctrlRect;
  204. progressRect.point.x += mDim;
  205. progressRect.extent.x = (width - mDim - mDim);
  206. if (progressRect.extent.x < 0)
  207. progressRect.extent.x = 0;
  208. drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRect, mProfile->mBitmapArrayRects[1]);
  209. //drawing right-end bitmap
  210. RectI progressRectRight(progressRect.point.x + progressRect.extent.x, ctrlRect.point.y, mDim, mDim );
  211. drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRectRight, mProfile->mBitmapArrayRects[2]);
  212. }
  213. }
  214. else
  215. Con::warnf("guiProgressBitmapCtrl only processes an array of bitmaps == 1 or >= 3");
  216. //if there's a border, draw it
  217. if (mProfile->mBorder)
  218. drawUtil->drawRect(ctrlRect, mProfile->mBorderColor);
  219. Parent::onRender( offset, updateRect );
  220. //render the children
  221. renderChildControls(offset, updateRect);
  222. }
  223. //-----------------------------------------------------------------------------
  224. bool GuiProgressBitmapCtrl::onWake()
  225. {
  226. if(!Parent::onWake())
  227. return false;
  228. mNumberOfBitmaps = mProfile->constructBitmapArray();
  229. return true;
  230. }
  231. //=============================================================================
  232. // Console Methods.
  233. //=============================================================================
  234. // MARK: ---- Console Methods ----
  235. //-----------------------------------------------------------------------------
  236. DefineEngineMethod( GuiProgressBitmapCtrl, setBitmap, void, ( const char* filename ),,
  237. "Set the bitmap to use for rendering the progress bar.\n\n"
  238. "@param filename ~Path to the bitmap file.\n\n"
  239. "@note Directly assign to #bitmap rather than using this method.\n\n"
  240. "@see GuiProgressBitmapCtrl::setBitmap" )
  241. {
  242. object->setBitmap( filename );
  243. }