guiTextEditSliderBitmapCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/controls/guiTextEditSliderBitmapCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. IMPLEMENT_CONOBJECT(GuiTextEditSliderBitmapCtrl);
  30. ConsoleDocClass( GuiTextEditSliderBitmapCtrl,
  31. "@brief GUI Control which displays a numerical value which can be increased "
  32. "or decreased using a pair of bitmap up/down buttons. \n\n"
  33. "This control uses the bitmap specified in it's profile "
  34. "(GuiControlProfile::bitmapName). It takes this image and breaks up aspects "
  35. "of it to render the up and down arrows. It is also important to set "
  36. "GuiControlProfile::hasBitmapArray to true on the profile as well.\n\n"
  37. "The bitmap referenced should be broken up into a 1 x 4 grid (using the top "
  38. "left color pixel as a border color between each of the images) in which it "
  39. "will map to the following places:\n"
  40. "<ol>\n"
  41. "<li>Up arrow active</li>\n"
  42. "<li>Up arrow inactive</li>\n"
  43. "<li>Down arrow active</li>\n"
  44. "<li>Down arrow inactive</li>\n"
  45. "</ol>\n\n"
  46. "<pre>\n"
  47. "1\n"
  48. "2\n"
  49. "3\n"
  50. "4</pre>\n\n"
  51. "@tsexample\n"
  52. "singleton GuiControlProfile (SliderBitmapGUIProfile)\n"
  53. "{\n"
  54. " bitmap = \"core/art/gui/images/sliderArray\";\n"
  55. " hasBitmapArray = true;\n"
  56. " opaque = false;\n"
  57. "};\n\n"
  58. "new GuiTextEditSliderBitmapCtrl()\n"
  59. "{\n"
  60. " profile = \"SliderBitmapGUIProfile\";\n"
  61. " format = \"%3.2f\";\n"
  62. " range = \"-1e+03 1e+03\";\n"
  63. " increment = \"0.1\";\n"
  64. " focusOnMouseWheel = \"0\";\n"
  65. " bitmap = \"\";\n"
  66. " //Properties not specific to this control have been omitted from this example.\n"
  67. "};\n"
  68. "@endtsexample\n\n"
  69. "@see GuiTextEditSliderCtrl\n\n"
  70. "@see GuiTextEditCtrl\n\n"
  71. "@ingroup GuiCore\n"
  72. );
  73. GuiTextEditSliderBitmapCtrl::GuiTextEditSliderBitmapCtrl()
  74. {
  75. mRange.set(0.0f, 1.0f);
  76. mIncAmount = 1.0f;
  77. mValue = 0.0f;
  78. mMulInc = 0;
  79. mIncCounter = 0.0f;
  80. mFormat = StringTable->insert("%3.2f");
  81. mTextAreaHit = None;
  82. mFocusOnMouseWheel = false;
  83. mBitmapName = StringTable->insert( "" );
  84. }
  85. GuiTextEditSliderBitmapCtrl::~GuiTextEditSliderBitmapCtrl()
  86. {
  87. }
  88. void GuiTextEditSliderBitmapCtrl::initPersistFields()
  89. {
  90. addField("format", TypeString, Offset(mFormat, GuiTextEditSliderBitmapCtrl), "Character format type to place in the control.\n");
  91. addField("range", TypePoint2F, Offset(mRange, GuiTextEditSliderBitmapCtrl), "Maximum vertical and horizontal range to allow in the control.\n");
  92. addField("increment", TypeF32, Offset(mIncAmount, GuiTextEditSliderBitmapCtrl), "How far to increment the slider on each step.\n");
  93. addField("focusOnMouseWheel", TypeBool, Offset(mFocusOnMouseWheel, GuiTextEditSliderBitmapCtrl), "If true, the control will accept giving focus to the user when the mouse wheel is used.\n");
  94. addField("bitmap", TypeFilename,Offset(mBitmapName, GuiTextEditSliderBitmapCtrl), "Unused" );
  95. Parent::initPersistFields();
  96. }
  97. void GuiTextEditSliderBitmapCtrl::getText(char *dest)
  98. {
  99. Parent::getText(dest);
  100. }
  101. void GuiTextEditSliderBitmapCtrl::setText(const char *txt)
  102. {
  103. mValue = dAtof(txt);
  104. checkRange();
  105. setValue();
  106. }
  107. bool GuiTextEditSliderBitmapCtrl::onKeyDown(const GuiEvent &event)
  108. {
  109. return Parent::onKeyDown(event);
  110. }
  111. void GuiTextEditSliderBitmapCtrl::checkRange()
  112. {
  113. if(mValue < mRange.x)
  114. mValue = mRange.x;
  115. else if(mValue > mRange.y)
  116. mValue = mRange.y;
  117. }
  118. void GuiTextEditSliderBitmapCtrl::setValue()
  119. {
  120. char buf[20];
  121. // For some reason this sprintf is failing to convert
  122. // a floating point number to anything with %d, so cast it.
  123. if( dStricmp( mFormat, "%d" ) == 0 )
  124. dSprintf(buf,sizeof(buf),mFormat, (S32)mValue);
  125. else
  126. dSprintf(buf,sizeof(buf),mFormat, mValue);
  127. Parent::setText(buf);
  128. }
  129. void GuiTextEditSliderBitmapCtrl::onMouseDown(const GuiEvent &event)
  130. {
  131. // If we're not active then skip out.
  132. if ( !mActive || !mAwake || !mVisible )
  133. {
  134. Parent::onMouseDown(event);
  135. return;
  136. }
  137. char txt[20];
  138. Parent::getText(txt);
  139. mValue = dAtof(txt);
  140. mMouseDownTime = Sim::getCurrentTime();
  141. GuiControl *parent = getParent();
  142. if(!parent)
  143. return;
  144. Point2I camPos = event.mousePoint;
  145. Point2I point = parent->localToGlobalCoord(getPosition());
  146. if(camPos.x > point.x + getExtent().x - 14)
  147. {
  148. if(camPos.y > point.y + (getExtent().y/2))
  149. {
  150. mValue -=mIncAmount;
  151. mTextAreaHit = ArrowDown;
  152. mMulInc = -0.15f;
  153. }
  154. else
  155. {
  156. mValue +=mIncAmount;
  157. mTextAreaHit = ArrowUp;
  158. mMulInc = 0.15f;
  159. }
  160. checkRange();
  161. setValue();
  162. mouseLock();
  163. // We should get the focus and set the
  164. // cursor to the start of the text to
  165. // mimic the standard Windows behavior.
  166. setFirstResponder();
  167. mCursorPos = mBlockStart = mBlockEnd = 0;
  168. setUpdate();
  169. return;
  170. }
  171. Parent::onMouseDown(event);
  172. }
  173. void GuiTextEditSliderBitmapCtrl::onMouseDragged(const GuiEvent &event)
  174. {
  175. // If we're not active then skip out.
  176. if ( !mActive || !mAwake || !mVisible )
  177. {
  178. Parent::onMouseDragged(event);
  179. return;
  180. }
  181. if(mTextAreaHit == None || mTextAreaHit == Slider)
  182. {
  183. mTextAreaHit = Slider;
  184. GuiControl *parent = getParent();
  185. if(!parent)
  186. return;
  187. Point2I camPos = event.mousePoint;
  188. Point2I point = parent->localToGlobalCoord(getPosition());
  189. F32 maxDis = 100;
  190. F32 val;
  191. if(camPos.y < point.y)
  192. {
  193. if((F32)point.y < maxDis)
  194. maxDis = (F32)point.y;
  195. val = point.y - maxDis;
  196. if(point.y > 0)
  197. mMulInc= 1.0f-(((float)camPos.y - val) / maxDis);
  198. else
  199. mMulInc = 1.0f;
  200. checkIncValue();
  201. return;
  202. }
  203. else if(camPos.y > point.y + getExtent().y)
  204. {
  205. GuiCanvas *root = getRoot();
  206. val = (F32)(root->getHeight() - (point.y + getHeight()));
  207. if(val < maxDis)
  208. maxDis = val;
  209. if( val > 0)
  210. mMulInc= -(F32)(camPos.y - (point.y + getHeight()))/maxDis;
  211. else
  212. mMulInc = -1.0f;
  213. checkIncValue();
  214. return;
  215. }
  216. mTextAreaHit = None;
  217. Parent::onMouseDragged(event);
  218. }
  219. }
  220. void GuiTextEditSliderBitmapCtrl::onMouseUp(const GuiEvent &event)
  221. {
  222. // If we're not active then skip out.
  223. if ( !mActive || !mAwake || !mVisible )
  224. {
  225. Parent::onMouseUp(event);
  226. return;
  227. }
  228. mMulInc = 0.0f;
  229. mouseUnlock();
  230. if ( mTextAreaHit != None )
  231. //if we released the mouse within this control, then the parent will call
  232. //the mConsoleCommand other wise we have to call it.
  233. Parent::onMouseUp(event);
  234. //if we didn't release the mouse within this control, then perform the action
  235. // if (!cursorInControl())
  236. execConsoleCallback();
  237. execAltConsoleCallback();
  238. mTextAreaHit = None;
  239. }
  240. bool GuiTextEditSliderBitmapCtrl::onMouseWheelUp(const GuiEvent &event)
  241. {
  242. if ( !mActive || !mAwake || !mVisible )
  243. return Parent::onMouseWheelUp(event);
  244. if ( !isFirstResponder() && !mFocusOnMouseWheel )
  245. return false;
  246. mValue += mIncAmount;
  247. checkRange();
  248. setValue();
  249. setFirstResponder();
  250. mCursorPos = mBlockStart = mBlockEnd = 0;
  251. setUpdate();
  252. return true;
  253. }
  254. bool GuiTextEditSliderBitmapCtrl::onMouseWheelDown(const GuiEvent &event)
  255. {
  256. if ( !mActive || !mAwake || !mVisible )
  257. return Parent::onMouseWheelDown(event);
  258. if ( !isFirstResponder() && !mFocusOnMouseWheel )
  259. return false;
  260. mValue -= mIncAmount;
  261. checkRange();
  262. setValue();
  263. setFirstResponder();
  264. mCursorPos = mBlockStart = mBlockEnd = 0;
  265. setUpdate();
  266. return true;
  267. }
  268. void GuiTextEditSliderBitmapCtrl::checkIncValue()
  269. {
  270. if(mMulInc > 1.0f)
  271. mMulInc = 1.0f;
  272. else if(mMulInc < -1.0f)
  273. mMulInc = -1.0f;
  274. }
  275. void GuiTextEditSliderBitmapCtrl::timeInc(U32 elapseTime)
  276. {
  277. S32 numTimes = elapseTime / 750;
  278. if(mTextAreaHit != Slider && numTimes > 0)
  279. {
  280. if(mTextAreaHit == ArrowUp)
  281. mMulInc = 0.15f * numTimes;
  282. else
  283. mMulInc = -0.15f * numTimes;
  284. checkIncValue();
  285. }
  286. }
  287. bool GuiTextEditSliderBitmapCtrl::onWake()
  288. {
  289. if(!Parent::onWake())
  290. return false;
  291. mNumberOfBitmaps = mProfile->constructBitmapArray();
  292. return true;
  293. }
  294. void GuiTextEditSliderBitmapCtrl::onPreRender()
  295. {
  296. if (isFirstResponder())
  297. {
  298. U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
  299. mNumFramesElapsed++;
  300. if ((timeElapsed > 500) && (mNumFramesElapsed > 3))
  301. {
  302. mCursorOn = !mCursorOn;
  303. mTimeLastCursorFlipped = Sim::getCurrentTime();
  304. mNumFramesElapsed = 0;
  305. setUpdate();
  306. }
  307. //update the cursor if the text is scrolling
  308. if (mDragHit)
  309. {
  310. if ((mScrollDir < 0) && (mCursorPos > 0))
  311. {
  312. mCursorPos--;
  313. }
  314. else if ((mScrollDir > 0) && (mCursorPos < (S32)dStrlen(mText)))
  315. {
  316. mCursorPos++;
  317. }
  318. }
  319. }
  320. }
  321. void GuiTextEditSliderBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  322. {
  323. if(mTextAreaHit != None)
  324. {
  325. U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime;
  326. if(elapseTime > 750 || mTextAreaHit == Slider)
  327. {
  328. timeInc(elapseTime);
  329. mIncCounter += mMulInc;
  330. if(mIncCounter >= 1.0f || mIncCounter <= -1.0f)
  331. {
  332. mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount;
  333. mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1;
  334. checkRange();
  335. setValue();
  336. mCursorPos = 0;
  337. }
  338. }
  339. }
  340. Parent::onRender(offset, updateRect);
  341. // Arrow placement coordinates
  342. Point2I arrowUpStart(offset.x + getWidth() - 14, offset.y + 1 );
  343. Point2I arrowUpEnd(13, getExtent().y/2);
  344. Point2I arrowDownStart(offset.x + getWidth() - 14, offset.y + 1 + getExtent().y/2);
  345. Point2I arrowDownEnd(13, getExtent().y/2);
  346. // Draw the line that splits the number and bitmaps
  347. GFX->getDrawUtil()->drawLine(Point2I(offset.x + getWidth() - 14 -2, offset.y + 1 ),
  348. Point2I(arrowUpStart.x -2, arrowUpStart.y + getExtent().y),
  349. mProfile->mBorderColor);
  350. GFX->getDrawUtil()->clearBitmapModulation();
  351. if(mNumberOfBitmaps == 0)
  352. Con::warnf("No image provided for GuiTextEditSliderBitmapCtrl; do not render");
  353. else
  354. {
  355. // This control needs 4 images in order to render correctly
  356. if(mTextAreaHit == ArrowUp)
  357. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[0] );
  358. else
  359. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[1] );
  360. if(mTextAreaHit == ArrowDown)
  361. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[2] );
  362. else
  363. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[3] );
  364. }
  365. }
  366. void GuiTextEditSliderBitmapCtrl::setBitmap(const char *name)
  367. {
  368. bool awake = mAwake;
  369. if(awake)
  370. onSleep();
  371. mBitmapName = StringTable->insert(name);
  372. if(awake)
  373. onWake();
  374. setUpdate();
  375. }