2
0

guiTextEditSliderCtrl.cpp 12 KB

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