guiTextEditSliderBitmapCtrl.cpp 13 KB

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