2
0

guiTextEditSliderBitmapCtrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. mMouseDownTime = 0;
  85. mNumberOfBitmaps = 0;
  86. }
  87. GuiTextEditSliderBitmapCtrl::~GuiTextEditSliderBitmapCtrl()
  88. {
  89. }
  90. void GuiTextEditSliderBitmapCtrl::initPersistFields()
  91. {
  92. addField("format", TypeString, Offset(mFormat, GuiTextEditSliderBitmapCtrl), "Character format type to place in the control.\n");
  93. addField("range", TypePoint2F, Offset(mRange, GuiTextEditSliderBitmapCtrl), "Maximum vertical and horizontal range to allow in the control.\n");
  94. addField("increment", TypeF32, Offset(mIncAmount, GuiTextEditSliderBitmapCtrl), "How far to increment the slider on each step.\n");
  95. addField("focusOnMouseWheel", TypeBool, Offset(mFocusOnMouseWheel, GuiTextEditSliderBitmapCtrl), "If true, the control will accept giving focus to the user when the mouse wheel is used.\n");
  96. addField("bitmap", TypeFilename,Offset(mBitmapName, GuiTextEditSliderBitmapCtrl), "Unused", AbstractClassRep::FIELD_HideInInspectors );
  97. Parent::initPersistFields();
  98. }
  99. void GuiTextEditSliderBitmapCtrl::getText(char *dest)
  100. {
  101. Parent::getText(dest);
  102. }
  103. void GuiTextEditSliderBitmapCtrl::setText(const char *txt)
  104. {
  105. mValue = dAtof(txt);
  106. checkRange();
  107. setValue();
  108. }
  109. bool GuiTextEditSliderBitmapCtrl::onKeyDown(const GuiEvent &event)
  110. {
  111. return Parent::onKeyDown(event);
  112. }
  113. void GuiTextEditSliderBitmapCtrl::checkRange()
  114. {
  115. if(mValue < mRange.x)
  116. mValue = mRange.x;
  117. else if(mValue > mRange.y)
  118. mValue = mRange.y;
  119. }
  120. void GuiTextEditSliderBitmapCtrl::setValue()
  121. {
  122. char buf[20];
  123. // For some reason this sprintf is failing to convert
  124. // a floating point number to anything with %d, so cast it.
  125. if( dStricmp( mFormat, "%d" ) == 0 )
  126. dSprintf(buf,sizeof(buf),mFormat, (S32)mValue);
  127. else
  128. dSprintf(buf,sizeof(buf),mFormat, mValue);
  129. Parent::setText(buf);
  130. }
  131. void GuiTextEditSliderBitmapCtrl::onMouseDown(const GuiEvent &event)
  132. {
  133. // If we're not active then skip out.
  134. if ( !mActive || !mAwake || !mVisible )
  135. {
  136. Parent::onMouseDown(event);
  137. return;
  138. }
  139. char txt[20];
  140. Parent::getText(txt);
  141. mValue = dAtof(txt);
  142. mMouseDownTime = Sim::getCurrentTime();
  143. GuiControl *parent = getParent();
  144. if(!parent)
  145. return;
  146. Point2I camPos = event.mousePoint;
  147. Point2I point = parent->localToGlobalCoord(getPosition());
  148. if(camPos.x > point.x + getExtent().x - 14)
  149. {
  150. if(camPos.y > point.y + (getExtent().y/2))
  151. {
  152. mValue -=mIncAmount;
  153. mTextAreaHit = ArrowDown;
  154. mMulInc = -0.15f;
  155. }
  156. else
  157. {
  158. mValue +=mIncAmount;
  159. mTextAreaHit = ArrowUp;
  160. mMulInc = 0.15f;
  161. }
  162. checkRange();
  163. setValue();
  164. mouseLock();
  165. // We should get the focus and set the
  166. // cursor to the start of the text to
  167. // mimic the standard Windows behavior.
  168. setFirstResponder();
  169. mCursorPos = mBlockStart = mBlockEnd = 0;
  170. setUpdate();
  171. return;
  172. }
  173. Parent::onMouseDown(event);
  174. }
  175. void GuiTextEditSliderBitmapCtrl::onMouseDragged(const GuiEvent &event)
  176. {
  177. // If we're not active then skip out.
  178. if ( !mActive || !mAwake || !mVisible )
  179. {
  180. Parent::onMouseDragged(event);
  181. return;
  182. }
  183. if(mTextAreaHit == None || mTextAreaHit == Slider)
  184. {
  185. mTextAreaHit = Slider;
  186. GuiControl *parent = getParent();
  187. if(!parent)
  188. return;
  189. Point2I camPos = event.mousePoint;
  190. Point2I point = parent->localToGlobalCoord(getPosition());
  191. F32 maxDis = 100;
  192. F32 val;
  193. if(camPos.y < point.y)
  194. {
  195. if((F32)point.y < maxDis)
  196. maxDis = (F32)point.y;
  197. val = point.y - maxDis;
  198. if(point.y > 0)
  199. mMulInc= 1.0f-(((float)camPos.y - val) / maxDis);
  200. else
  201. mMulInc = 1.0f;
  202. checkIncValue();
  203. return;
  204. }
  205. else if(camPos.y > point.y + getExtent().y)
  206. {
  207. GuiCanvas *root = getRoot();
  208. val = (F32)(root->getHeight() - (point.y + getHeight()));
  209. if(val < maxDis)
  210. maxDis = val;
  211. if( val > 0)
  212. mMulInc= -(F32)(camPos.y - (point.y + getHeight()))/maxDis;
  213. else
  214. mMulInc = -1.0f;
  215. checkIncValue();
  216. return;
  217. }
  218. mTextAreaHit = None;
  219. Parent::onMouseDragged(event);
  220. }
  221. }
  222. void GuiTextEditSliderBitmapCtrl::onMouseUp(const GuiEvent &event)
  223. {
  224. // If we're not active then skip out.
  225. if ( !mActive || !mAwake || !mVisible )
  226. {
  227. Parent::onMouseUp(event);
  228. return;
  229. }
  230. mMulInc = 0.0f;
  231. mouseUnlock();
  232. if ( mTextAreaHit != None )
  233. //if we released the mouse within this control, then the parent will call
  234. //the mConsoleCommand other wise we have to call it.
  235. Parent::onMouseUp(event);
  236. //if we didn't release the mouse within this control, then perform the action
  237. // if (!cursorInControl())
  238. execConsoleCallback();
  239. execAltConsoleCallback();
  240. mTextAreaHit = None;
  241. }
  242. bool GuiTextEditSliderBitmapCtrl::onMouseWheelUp(const GuiEvent &event)
  243. {
  244. if ( !mActive || !mAwake || !mVisible )
  245. return Parent::onMouseWheelUp(event);
  246. if ( !isFirstResponder() && !mFocusOnMouseWheel )
  247. return false;
  248. mValue += mIncAmount;
  249. checkRange();
  250. setValue();
  251. setFirstResponder();
  252. mCursorPos = mBlockStart = mBlockEnd = 0;
  253. setUpdate();
  254. return true;
  255. }
  256. bool GuiTextEditSliderBitmapCtrl::onMouseWheelDown(const GuiEvent &event)
  257. {
  258. if ( !mActive || !mAwake || !mVisible )
  259. return Parent::onMouseWheelDown(event);
  260. if ( !isFirstResponder() && !mFocusOnMouseWheel )
  261. return false;
  262. mValue -= mIncAmount;
  263. checkRange();
  264. setValue();
  265. setFirstResponder();
  266. mCursorPos = mBlockStart = mBlockEnd = 0;
  267. setUpdate();
  268. return true;
  269. }
  270. void GuiTextEditSliderBitmapCtrl::checkIncValue()
  271. {
  272. if(mMulInc > 1.0f)
  273. mMulInc = 1.0f;
  274. else if(mMulInc < -1.0f)
  275. mMulInc = -1.0f;
  276. }
  277. void GuiTextEditSliderBitmapCtrl::timeInc(U32 elapseTime)
  278. {
  279. S32 numTimes = elapseTime / 750;
  280. if(mTextAreaHit != Slider && numTimes > 0)
  281. {
  282. if(mTextAreaHit == ArrowUp)
  283. mMulInc = 0.15f * numTimes;
  284. else
  285. mMulInc = -0.15f * numTimes;
  286. checkIncValue();
  287. }
  288. }
  289. bool GuiTextEditSliderBitmapCtrl::onWake()
  290. {
  291. if(!Parent::onWake())
  292. return false;
  293. mNumberOfBitmaps = mProfile->constructBitmapArray();
  294. return true;
  295. }
  296. void GuiTextEditSliderBitmapCtrl::onPreRender()
  297. {
  298. if (isFirstResponder())
  299. {
  300. U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
  301. mNumFramesElapsed++;
  302. if ((timeElapsed > 500) && (mNumFramesElapsed > 3))
  303. {
  304. mCursorOn = !mCursorOn;
  305. mTimeLastCursorFlipped = Sim::getCurrentTime();
  306. mNumFramesElapsed = 0;
  307. setUpdate();
  308. }
  309. //update the cursor if the text is scrolling
  310. if (mDragHit)
  311. {
  312. if ((mScrollDir < 0) && (mCursorPos > 0))
  313. {
  314. mCursorPos--;
  315. }
  316. else if ((mScrollDir > 0) && (mCursorPos < (S32)dStrlen(mText)))
  317. {
  318. mCursorPos++;
  319. }
  320. }
  321. }
  322. }
  323. void GuiTextEditSliderBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  324. {
  325. if(mTextAreaHit != None)
  326. {
  327. U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime;
  328. if(elapseTime > 750 || mTextAreaHit == Slider)
  329. {
  330. timeInc(elapseTime);
  331. mIncCounter += mMulInc;
  332. if(mIncCounter >= 1.0f || mIncCounter <= -1.0f)
  333. {
  334. mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount;
  335. mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1;
  336. checkRange();
  337. setValue();
  338. mCursorPos = 0;
  339. }
  340. }
  341. }
  342. Parent::onRender(offset, updateRect);
  343. // Arrow placement coordinates
  344. Point2I arrowUpStart(offset.x + getWidth() - 14, offset.y + 1 );
  345. Point2I arrowUpEnd(13, getExtent().y/2);
  346. Point2I arrowDownStart(offset.x + getWidth() - 14, offset.y + 1 + getExtent().y/2);
  347. Point2I arrowDownEnd(13, getExtent().y/2);
  348. // Draw the line that splits the number and bitmaps
  349. GFX->getDrawUtil()->drawLine(Point2I(offset.x + getWidth() - 14 -2, offset.y + 1 ),
  350. Point2I(arrowUpStart.x -2, arrowUpStart.y + getExtent().y),
  351. mProfile->mBorderColor);
  352. GFX->getDrawUtil()->clearBitmapModulation();
  353. if(mNumberOfBitmaps == 0)
  354. Con::warnf("No image provided for GuiTextEditSliderBitmapCtrl; do not render");
  355. else
  356. {
  357. // This control needs 4 images in order to render correctly
  358. if(mTextAreaHit == ArrowUp)
  359. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mBitmap, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[0] );
  360. else
  361. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mBitmap, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[1] );
  362. if(mTextAreaHit == ArrowDown)
  363. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mBitmap, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[2] );
  364. else
  365. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mBitmap, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[3] );
  366. }
  367. }
  368. void GuiTextEditSliderBitmapCtrl::setBitmap(const char *name)
  369. {
  370. bool awake = mAwake;
  371. if(awake)
  372. onSleep();
  373. mBitmapName = StringTable->insert(name);
  374. if(awake)
  375. onWake();
  376. setUpdate();
  377. }