guiTextEditSliderCtrl.cpp 12 KB

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