guiTextEditSliderCtrl.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/guiTextEditSliderCtrl.h"
  27. IMPLEMENT_CONOBJECT(GuiTextEditSliderCtrl);
  28. GuiTextEditSliderCtrl::GuiTextEditSliderCtrl()
  29. {
  30. mRange.set(0.0f, 1.0f);
  31. mIncAmount = 1.0f;
  32. mValue = 0.0f;
  33. mMulInc = 0;
  34. mIncCounter = 0.0f;
  35. mFormat = StringTable->insert("%3.2f");
  36. mTextAreaHit = None;
  37. }
  38. GuiTextEditSliderCtrl::~GuiTextEditSliderCtrl()
  39. {
  40. }
  41. void GuiTextEditSliderCtrl::initPersistFields()
  42. {
  43. Parent::initPersistFields();
  44. addField("format", TypeString, Offset(mFormat, GuiTextEditSliderCtrl));
  45. addField("range", TypePoint2F, Offset(mRange, GuiTextEditSliderCtrl));
  46. addField("increment", TypeF32, Offset(mIncAmount, GuiTextEditSliderCtrl));
  47. }
  48. void GuiTextEditSliderCtrl::getText(char *dest)
  49. {
  50. Parent::getText(dest);
  51. }
  52. void GuiTextEditSliderCtrl::setText(const char *txt)
  53. {
  54. mValue = dAtof(txt);
  55. checkRange();
  56. setValue();
  57. }
  58. bool GuiTextEditSliderCtrl::onKeyDown(const GuiEvent &event)
  59. {
  60. return Parent::onKeyDown(event);
  61. }
  62. void GuiTextEditSliderCtrl::checkRange()
  63. {
  64. if(mValue < mRange.x)
  65. mValue = mRange.x;
  66. else if(mValue > mRange.y)
  67. mValue = mRange.y;
  68. }
  69. void GuiTextEditSliderCtrl::setValue()
  70. {
  71. char buf[20];
  72. dSprintf(buf,sizeof(buf),mFormat, mValue);
  73. Parent::setText(buf);
  74. }
  75. void GuiTextEditSliderCtrl::onTouchDown(const GuiEvent &event)
  76. {
  77. char txt[20];
  78. Parent::getText(txt);
  79. mValue = dAtof(txt);
  80. mMouseDownTime = Sim::getCurrentTime();
  81. GuiControl *parent = getParent();
  82. if(!parent)
  83. return;
  84. Point2I camPos = event.mousePoint;
  85. Point2I point = parent->localToGlobalCoord(mBounds.point);
  86. if(camPos.x > point.x + mBounds.extent.x - 14)
  87. {
  88. if(camPos.y > point.y + (mBounds.extent.y/2))
  89. {
  90. mValue -=mIncAmount;
  91. mTextAreaHit = ArrowDown;
  92. mMulInc = -0.15f;
  93. }
  94. else
  95. {
  96. mValue +=mIncAmount;
  97. mTextAreaHit = ArrowUp;
  98. mMulInc = 0.15f;
  99. }
  100. checkRange();
  101. setValue();
  102. mouseLock();
  103. return;
  104. }
  105. Parent::onTouchDown(event);
  106. }
  107. void GuiTextEditSliderCtrl::onTouchDragged(const GuiEvent &event)
  108. {
  109. if(mTextAreaHit == None || mTextAreaHit == Slider)
  110. {
  111. mTextAreaHit = Slider;
  112. GuiControl *parent = getParent();
  113. if(!parent)
  114. return;
  115. Point2I camPos = event.mousePoint;
  116. Point2I point = parent->localToGlobalCoord(mBounds.point);
  117. F32 maxDis = 100;
  118. F32 val;
  119. if(camPos.y < point.y)
  120. {
  121. if(point.y < maxDis)
  122. maxDis = (F32)point.y;
  123. val = point.y - maxDis;
  124. if(point.y > 0)
  125. mMulInc= 1.0f-(((float)camPos.y - val) / maxDis);
  126. else
  127. mMulInc = 1.0f;
  128. checkIncValue();
  129. return;
  130. }
  131. else if(camPos.y > point.y + mBounds.extent.y)
  132. {
  133. GuiCanvas *root = getRoot();
  134. val = (F32)(root->mBounds.extent.y - (point.y + mBounds.extent.y));
  135. if(val < maxDis)
  136. maxDis = val;
  137. if( val > 0)
  138. mMulInc= -(float)(camPos.y - (point.y + mBounds.extent.y))/maxDis;
  139. else
  140. mMulInc = -1.0f;
  141. checkIncValue();
  142. return;
  143. }
  144. mTextAreaHit = None;
  145. Parent::onTouchDragged(event);
  146. }
  147. }
  148. void GuiTextEditSliderCtrl::onTouchUp(const GuiEvent &event)
  149. {
  150. mMulInc = 0.0f;
  151. mouseUnlock();
  152. //if we released the mouse within this control, then the parent will call
  153. //the mConsoleCommand other wise we have to call it.
  154. Parent::onTouchUp(event);
  155. //if we didn't release the mouse within this control, then perform the action
  156. if (!cursorInControl())
  157. Con::evaluate(mConsoleCommand, false);
  158. mTextAreaHit = None;
  159. }
  160. void GuiTextEditSliderCtrl::checkIncValue()
  161. {
  162. if(mMulInc > 1.0f)
  163. mMulInc = 1.0f;
  164. else if(mMulInc < -1.0f)
  165. mMulInc = -1.0f;
  166. }
  167. void GuiTextEditSliderCtrl::timeInc(U32 elapseTime)
  168. {
  169. S32 numTimes = elapseTime / 750;
  170. if(mTextAreaHit != Slider && numTimes > 0)
  171. {
  172. if(mTextAreaHit == ArrowUp)
  173. mMulInc = 0.15f * numTimes;
  174. else
  175. mMulInc = -0.15f * numTimes;
  176. checkIncValue();
  177. }
  178. }
  179. void GuiTextEditSliderCtrl::onRender(Point2I offset, const RectI &updateRect)
  180. {
  181. if(mTextAreaHit != None)
  182. {
  183. U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime;
  184. if(elapseTime > 750 || mTextAreaHit == Slider)
  185. {
  186. timeInc(elapseTime);
  187. mIncCounter += mMulInc;
  188. if(mIncCounter >= 1.0f || mIncCounter <= -1.0f)
  189. {
  190. mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount;
  191. mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1;
  192. checkRange();
  193. setValue();
  194. }
  195. }
  196. }
  197. Parent::onRender(offset, updateRect);
  198. Point2I start(offset.x + mBounds.extent.x - 14, offset.y);
  199. Point2I midPoint(start.x + 7, start.y + (mBounds.extent.y/2));
  200. dglDrawRectFill(Point2I(start.x+1,start.y+1), Point2I(start.x+13,start.y+mBounds.extent.y-1) , mProfile->mFillColor);
  201. dglDrawLine(start, Point2I(start.x, start.y+mBounds.extent.y),mProfile->mFontColor);
  202. dglDrawLine(Point2I(start.x,midPoint.y),
  203. Point2I(start.x+14,midPoint.y),
  204. mProfile->mFontColor);
  205. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  206. glColor4f(0,0,0,255);
  207. GLfloat verts[] = {
  208. 0, 0,
  209. 0, 0,
  210. 0, 0,
  211. 0, 0,
  212. 0, 0,
  213. 0, 0,
  214. };
  215. if(mTextAreaHit == ArrowUp)
  216. {
  217. verts[0] = (GLfloat)midPoint.x;
  218. verts[1] = (GLfloat)start.y+1;
  219. verts[2] = (GLfloat)start.x+11;
  220. verts[3] = (GLfloat)midPoint.y-2;
  221. verts[4] = (GLfloat)start.x+3;
  222. verts[5] = (GLfloat)midPoint.y-2;
  223. }
  224. else
  225. {
  226. verts[0] = (GLfloat)midPoint.x;
  227. verts[1] = (GLfloat)start.y+2;
  228. verts[2] = (GLfloat)start.x+11;
  229. verts[3] = (GLfloat)midPoint.y-1;
  230. verts[4] = (GLfloat)start.x+3;
  231. verts[5] = (GLfloat)midPoint.y-1;
  232. }
  233. if(mTextAreaHit == ArrowDown)
  234. {
  235. verts[6] = (GLfloat)midPoint.x;
  236. verts[7] = (GLfloat)start.y+mBounds.extent.y-1;
  237. verts[8] = (GLfloat)start.x+11;
  238. verts[9] = (GLfloat)midPoint.y+3;
  239. verts[10] = (GLfloat)start.x+3;
  240. verts[11] = (GLfloat)midPoint.y+3;
  241. }
  242. else
  243. {
  244. verts[6] = (GLfloat)midPoint.x;
  245. verts[7] = (GLfloat)start.y+mBounds.extent.y-2;
  246. verts[8] = (GLfloat)start.x+11;
  247. verts[9] = (GLfloat)midPoint.y+2;
  248. verts[10] = (GLfloat)start.x+3;
  249. verts[11] = (GLfloat)midPoint.y+2;
  250. }
  251. glVertexPointer(2, GL_FLOAT, 0, verts);
  252. glDrawArrays(GL_TRIANGLES, 0, 6);
  253. #else
  254. glColor3i(0,0,0);
  255. glBegin(GL_TRIANGLES);
  256. if(mTextAreaHit == ArrowUp)
  257. {
  258. glVertex2i(midPoint.x, start.y+1);
  259. glVertex2i(start.x+11,midPoint.y-2);
  260. glVertex2i(start.x+3,midPoint.y-2);
  261. }
  262. else
  263. {
  264. glVertex2i(midPoint.x, start.y+2);
  265. glVertex2i(start.x+11,midPoint.y-1);
  266. glVertex2i(start.x+3,midPoint.y-1);
  267. }
  268. if(mTextAreaHit == ArrowDown)
  269. {
  270. glVertex2i(midPoint.x, start.y+mBounds.extent.y-1);
  271. glVertex2i(start.x+11,midPoint.y+3);
  272. glVertex2i(start.x+3,midPoint.y+3);
  273. }
  274. else
  275. {
  276. glVertex2i(midPoint.x, start.y+mBounds.extent.y-2);
  277. glVertex2i(start.x+11,midPoint.y+2);
  278. glVertex2i(start.x+3,midPoint.y+2);
  279. }
  280. glEnd();
  281. #endif
  282. }
  283. void GuiTextEditSliderCtrl::onPreRender()
  284. {
  285. if (isFirstResponder())
  286. {
  287. U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
  288. mNumFramesElapsed++;
  289. if ((timeElapsed > 500) && (mNumFramesElapsed > 3))
  290. {
  291. mCursorOn = !mCursorOn;
  292. mTimeLastCursorFlipped = Sim::getCurrentTime();
  293. mNumFramesElapsed = 0;
  294. setUpdate();
  295. }
  296. //update the cursor if the text is scrolling
  297. if (mDragHit)
  298. {
  299. if ((mScrollDir < 0) && (mCursorPos > 0))
  300. {
  301. mCursorPos--;
  302. }
  303. else if ((mScrollDir > 0) && (mCursorPos < (S32)dStrlen(mText)))
  304. {
  305. mCursorPos++;
  306. }
  307. }
  308. }
  309. }