Torque3D Documentation / _generateds / guiProgressBitmapCtrl.cpp

guiProgressBitmapCtrl.cpp

Engine/source/gui/game/guiProgressBitmapCtrl.cpp

More...

Public Functions

ConsoleDocClass(GuiProgressBitmapCtrl , "@brief A horizontal progress bar rendered from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> repeating <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "This class is used give progress feedback <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the user. Unlike <a href="/coding/class/classguiprogressctrl/">GuiProgressCtrl</a> which simply " "renders <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> filled rectangle, <a href="/coding/class/classguiprogressbitmapctrl/">GuiProgressBitmapCtrl</a> renders the bar using <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">bitmap.\n\n</a>" "This bitmap can either be simple, plain image which is then stretched into the current extents of the bar " "as it fills up or it can be <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap array with three entries. In the case of <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap array)
DefineEngineMethod(GuiProgressBitmapCtrl , setBitmap , void , (const char *filename) , "Set the bitmap <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> rendering the progress <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">bar.\n\n</a>" "@param filename ~Path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the bitmap <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@note Directly assign <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> #bitmap rather than using this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">method.\n\n</a>" "@see <a href="/coding/class/classguiprogressbitmapctrl/#classguiprogressbitmapctrl_1ae34b404024fbd47e46fa475f2423b6c1">GuiProgressBitmapCtrl::setBitmap</a>" )

Detailed Description

Public Functions

ConsoleDocClass(GuiProgressBitmapCtrl , "@brief A horizontal progress bar rendered from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> repeating <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "This class is used give progress feedback <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the user. Unlike <a href="/coding/class/classguiprogressctrl/">GuiProgressCtrl</a> which simply " "renders <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> filled rectangle, <a href="/coding/class/classguiprogressbitmapctrl/">GuiProgressBitmapCtrl</a> renders the bar using <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">bitmap.\n\n</a>" "This bitmap can either be simple, plain image which is then stretched into the current extents of the bar " "as it fills up or it can be <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap array with three entries. In the case of <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap array)

DefineEngineMethod(GuiProgressBitmapCtrl , setBitmap , void , (const char *filename) , "Set the bitmap <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> rendering the progress <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">bar.\n\n</a>" "@param filename ~Path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the bitmap <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@note Directly assign <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> #bitmap rather than using this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">method.\n\n</a>" "@see <a href="/coding/class/classguiprogressbitmapctrl/#classguiprogressbitmapctrl_1ae34b404024fbd47e46fa475f2423b6c1">GuiProgressBitmapCtrl::setBitmap</a>" )

IMPLEMENT_CONOBJECT(GuiProgressBitmapCtrl )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "gui/game/guiProgressBitmapCtrl.h"
 26
 27#include "console/console.h"
 28#include "console/consoleTypes.h"
 29#include "console/engineAPI.h"
 30#include "gfx/gfxDrawUtil.h"
 31
 32
 33IMPLEMENT_CONOBJECT( GuiProgressBitmapCtrl );
 34
 35ConsoleDocClass( GuiProgressBitmapCtrl,
 36   "@brief A horizontal progress bar rendered from a repeating image.\n\n"
 37   
 38   "This class is used give progress feedback to the user.  Unlike GuiProgressCtrl which simply "
 39   "renders a filled rectangle, GuiProgressBitmapCtrl renders the bar using a bitmap.\n\n"
 40   
 41   "This bitmap can either be simple, plain image which is then stretched into the current extents of the bar "
 42   "as it fills up or it can be a bitmap array with three entries.  In the case of a bitmap array, the "
 43   "first entry in the array is used to render the left cap of the bar and the third entry in the array "
 44   "is used to render the right cap of the bar.  The second entry is streched in-between the two caps.\n\n"
 45   
 46   "@tsexample\n"
 47      "// This example shows one way to break down a long-running computation into phases\n"
 48      "// and incrementally update a progress bar between the phases.\n"
 49      "\n"
 50      "new GuiProgressBitmapCtrl( Progress )\n"
 51      "{\n"
 52      "   bitmap = \"core/art/gui/images/loading\";\n"
 53      "   extent = \"300 50\";\n"
 54      "   position = \"100 100\";\n"
 55      "};\n"
 56      "\n"
 57      "// Put the control on the canvas.\n"
 58      "%wrapper = new GuiControl();\n"
 59      "%wrapper.addObject( Progress );\n"
 60      "Canvas.pushDialog( %wrapper );\n"
 61      "\n"
 62      "// Start the computation.\n"
 63      "schedule( 1, 0, \"phase1\" );\n"
 64      "\n"
 65      "function phase1()\n"
 66      "{\n"
 67      "   Progress.setValue( 0 );\n"
 68      "\n"
 69      "   // Perform some computation.\n"
 70      "   //...\n"
 71      "\n"
 72      "   // Update progress.\n"
 73      "   Progress.setValue( 0.25 );\n"
 74      "\n"
 75      "   // Schedule next phase.  Don't call directly so engine gets a change to run refresh.\n"
 76      "   schedule( 1, 0, \"phase2\" );\n"
 77      "}\n"
 78      "\n"
 79      "function phase2()\n"
 80      "{\n"
 81      "   // Perform some computation.\n"
 82      "   //...\n"
 83      "\n"
 84      "   // Update progress.\n"
 85      "   Progress.setValue( 0.7 );\n"
 86      "\n"
 87      "   // Schedule next phase.  Don't call directly so engine gets a change to run refresh.\n"
 88      "   schedule( 1, 0, \"phase3\" );\n"
 89      "}\n"
 90      "\n"
 91      "function phase3()\n"
 92      "{\n"
 93      "   // Perform some computation.\n"
 94      "   //...\n"
 95      "\n"
 96      "   // Update progress.\n"
 97      "   Progress.setValue( 0.9 );\n"
 98      "\n"
 99      "   // Schedule next phase.  Don't call directly so engine gets a change to run refresh.\n"
100      "   schedule( 1, 0, \"phase4\" );\n"
101      "}\n"
102      "\n"
103      "function phase4()\n"
104      "{\n"
105      "   // Perform some computation.\n"
106      "   //...\n"
107      "\n"
108      "   // Final update of progress.\n"
109      "   Progress.setValue( 1.0 );\n"
110      "}\n"
111   "@endtsexample\n\n"
112   
113   "@see GuiProgressCtrl\n\n"
114   
115   "@ingroup GuiValues"
116);
117
118
119//-----------------------------------------------------------------------------
120
121GuiProgressBitmapCtrl::GuiProgressBitmapCtrl()
122   : mProgress( 0.f ),
123     mBitmapName( StringTable->EmptyString() ),
124     mUseVariable( false ),
125     mTile( false ),
126     mNumberOfBitmaps(0),
127     mDim(0)
128{
129}
130
131//-----------------------------------------------------------------------------
132
133void GuiProgressBitmapCtrl::initPersistFields()
134{
135   addProtectedField( "bitmap", TypeFilename, Offset( mBitmapName, GuiProgressBitmapCtrl ),
136      _setBitmap, defaultProtectedGetFn,
137      "~Path to the bitmap file to use for rendering the progress bar.\n\n"
138      "If the profile assigned to the control already has a bitmap assigned, this property need not be "
139      "set in which case the bitmap from the profile is used."
140   );
141   
142   Parent::initPersistFields();
143}
144
145//-----------------------------------------------------------------------------
146
147void GuiProgressBitmapCtrl::setBitmap( const char* name )
148{
149   bool awake = mAwake;
150   if( awake )
151      onSleep();
152
153   mBitmapName = StringTable->insert( name );
154   if( awake )
155      onWake();
156      
157   setUpdate();
158}
159
160//-----------------------------------------------------------------------------
161
162const char* GuiProgressBitmapCtrl::getScriptValue()
163{
164   static const U32 bufSize = 64;
165   char * ret = Con::getReturnBuffer(bufSize);
166   dSprintf(ret, bufSize, "%g", mProgress);
167   return ret;
168}
169
170//-----------------------------------------------------------------------------
171
172void GuiProgressBitmapCtrl::setScriptValue(const char *value)
173{
174   //set the value
175   if (! value)
176      mProgress = 0.0f;
177   else
178      mProgress = dAtof(value);
179
180   //validate the value
181   mProgress = mClampF(mProgress, 0.f, 1.f);
182   setUpdate();
183}
184
185//-----------------------------------------------------------------------------
186
187void GuiProgressBitmapCtrl::onPreRender()
188{
189   const char * var = getVariable();
190   if(var)
191   {
192      F32 value = mClampF(dAtof(var), 0.f, 1.f);
193      if(value != mProgress)
194      {
195         mProgress = value;
196         setUpdate();
197      }
198   }
199}
200
201//-----------------------------------------------------------------------------
202
203void GuiProgressBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
204{  
205   RectI ctrlRect(offset, getExtent());
206   
207   //grab lowest dimension
208   if(getHeight() <= getWidth())
209      mDim = getHeight();
210   else
211      mDim = getWidth();
212
213   GFXDrawUtil* drawUtil = GFX->getDrawUtil();
214   
215   drawUtil->clearBitmapModulation();
216
217   if(mNumberOfBitmaps == 1)
218   {
219      //draw the progress with image
220      S32 width = (S32)((F32)(getWidth()) * mProgress);
221      if (width > 0)
222      {
223         //drawing stretch bitmap
224         RectI progressRect = ctrlRect;
225         progressRect.extent.x = width;
226         drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRect, mProfile->mBitmapArrayRects[0]);
227      }
228   }
229   else if(mNumberOfBitmaps >= 3)
230   {
231      //drawing left-end bitmap
232      RectI progressRectLeft(ctrlRect.point.x, ctrlRect.point.y, mDim, mDim);
233      drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRectLeft, mProfile->mBitmapArrayRects[0]);
234
235      //draw the progress with image
236      S32 width = (S32)((F32)(getWidth()) * mProgress);
237      if (width > mDim)
238      {
239         //drawing stretch bitmap
240         RectI progressRect = ctrlRect;
241         progressRect.point.x +=  mDim;
242         progressRect.extent.x = (width - mDim - mDim);
243         if (progressRect.extent.x < 0)
244            progressRect.extent.x = 0;
245         drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRect, mProfile->mBitmapArrayRects[1]);
246      
247         //drawing right-end bitmap
248         RectI progressRectRight(progressRect.point.x + progressRect.extent.x, ctrlRect.point.y, mDim, mDim );
249         drawUtil->drawBitmapStretchSR(mProfile->mTextureObject, progressRectRight, mProfile->mBitmapArrayRects[2]);
250      }
251   }
252   else
253      Con::warnf("guiProgressBitmapCtrl only processes an array of bitmaps == 1 or >= 3");
254
255   //if there's a border, draw it
256   if (mProfile->mBorder)
257      drawUtil->drawRect(ctrlRect, mProfile->mBorderColor);
258
259   Parent::onRender( offset, updateRect );
260
261   //render the children
262   renderChildControls(offset, updateRect);
263   
264}
265
266//-----------------------------------------------------------------------------
267
268bool GuiProgressBitmapCtrl::onWake()
269{
270   if(!Parent::onWake())
271      return false;
272
273   mNumberOfBitmaps = mProfile->constructBitmapArray();
274
275   return true;
276}
277
278//=============================================================================
279//    Console Methods.
280//=============================================================================
281// MARK: ---- Console Methods ----
282
283//-----------------------------------------------------------------------------
284
285DefineEngineMethod( GuiProgressBitmapCtrl, setBitmap, void, ( const char* filename ),,
286   "Set the bitmap to use for rendering the progress bar.\n\n"
287   "@param filename ~Path to the bitmap file.\n\n"
288   "@note Directly assign to #bitmap rather than using this method.\n\n"
289   "@see GuiProgressBitmapCtrl::setBitmap" )
290{
291   object->setBitmap( filename );
292}
293