guiTextEditSliderCtrl.cpp 12 KB

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