guiTextEditSliderCtrl.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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::setText(const char *txt)
  49. {
  50. mValue = dAtof(txt);
  51. checkRange();
  52. setValue();
  53. }
  54. bool GuiTextEditSliderCtrl::onKeyDown(const GuiEvent &event)
  55. {
  56. return Parent::onKeyDown(event);
  57. }
  58. void GuiTextEditSliderCtrl::checkRange()
  59. {
  60. if(mValue < mRange.x)
  61. mValue = mRange.x;
  62. else if(mValue > mRange.y)
  63. mValue = mRange.y;
  64. }
  65. void GuiTextEditSliderCtrl::setValue()
  66. {
  67. char buf[20];
  68. dSprintf(buf,sizeof(buf),mFormat, mValue);
  69. Parent::setText(buf);
  70. }
  71. void GuiTextEditSliderCtrl::onTouchDown(const GuiEvent &event)
  72. {
  73. mValue = dAtof(mText);
  74. mMouseDownTime = Sim::getCurrentTime();
  75. GuiControl *parent = getParent();
  76. if(!parent)
  77. return;
  78. Point2I camPos = event.mousePoint;
  79. Point2I point = parent->localToGlobalCoord(mBounds.point);
  80. if(camPos.x > point.x + mBounds.extent.x - 14)
  81. {
  82. if(camPos.y > point.y + (mBounds.extent.y/2))
  83. {
  84. mValue -=mIncAmount;
  85. mTextAreaHit = ArrowDown;
  86. mMulInc = -0.15f;
  87. }
  88. else
  89. {
  90. mValue +=mIncAmount;
  91. mTextAreaHit = ArrowUp;
  92. mMulInc = 0.15f;
  93. }
  94. checkRange();
  95. setValue();
  96. mouseLock();
  97. return;
  98. }
  99. Parent::onTouchDown(event);
  100. }
  101. void GuiTextEditSliderCtrl::onTouchDragged(const GuiEvent &event)
  102. {
  103. if(mTextAreaHit == None || mTextAreaHit == Slider)
  104. {
  105. mTextAreaHit = Slider;
  106. GuiControl *parent = getParent();
  107. if(!parent)
  108. return;
  109. Point2I camPos = event.mousePoint;
  110. Point2I point = parent->localToGlobalCoord(mBounds.point);
  111. F32 maxDis = 100;
  112. F32 val;
  113. if(camPos.y < point.y)
  114. {
  115. if(point.y < maxDis)
  116. maxDis = (F32)point.y;
  117. val = point.y - maxDis;
  118. if(point.y > 0)
  119. mMulInc= 1.0f-(((float)camPos.y - val) / maxDis);
  120. else
  121. mMulInc = 1.0f;
  122. checkIncValue();
  123. return;
  124. }
  125. else if(camPos.y > point.y + mBounds.extent.y)
  126. {
  127. GuiCanvas *root = getRoot();
  128. val = (F32)(root->mBounds.extent.y - (point.y + mBounds.extent.y));
  129. if(val < maxDis)
  130. maxDis = val;
  131. if( val > 0)
  132. mMulInc= -(float)(camPos.y - (point.y + mBounds.extent.y))/maxDis;
  133. else
  134. mMulInc = -1.0f;
  135. checkIncValue();
  136. return;
  137. }
  138. mTextAreaHit = None;
  139. Parent::onTouchDragged(event);
  140. }
  141. }
  142. void GuiTextEditSliderCtrl::onTouchUp(const GuiEvent &event)
  143. {
  144. mMulInc = 0.0f;
  145. mouseUnlock();
  146. //if we released the mouse within this control, then the parent will call
  147. //the mConsoleCommand other wise we have to call it.
  148. Parent::onTouchUp(event);
  149. //if we didn't release the mouse within this control, then perform the action
  150. if (!cursorInControl())
  151. Con::evaluate(mConsoleCommand, false);
  152. mTextAreaHit = None;
  153. }
  154. void GuiTextEditSliderCtrl::checkIncValue()
  155. {
  156. if(mMulInc > 1.0f)
  157. mMulInc = 1.0f;
  158. else if(mMulInc < -1.0f)
  159. mMulInc = -1.0f;
  160. }
  161. void GuiTextEditSliderCtrl::timeInc(U32 elapseTime)
  162. {
  163. S32 numTimes = elapseTime / 750;
  164. if(mTextAreaHit != Slider && numTimes > 0)
  165. {
  166. if(mTextAreaHit == ArrowUp)
  167. mMulInc = 0.15f * numTimes;
  168. else
  169. mMulInc = -0.15f * numTimes;
  170. checkIncValue();
  171. }
  172. }
  173. void GuiTextEditSliderCtrl::onRender(Point2I offset, const RectI &updateRect)
  174. {
  175. if(mTextAreaHit != None)
  176. {
  177. U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime;
  178. if(elapseTime > 750 || mTextAreaHit == Slider)
  179. {
  180. timeInc(elapseTime);
  181. mIncCounter += mMulInc;
  182. if(mIncCounter >= 1.0f || mIncCounter <= -1.0f)
  183. {
  184. mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount;
  185. mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1;
  186. checkRange();
  187. setValue();
  188. }
  189. }
  190. }
  191. Parent::onRender(offset, updateRect);
  192. Point2I start(offset.x + mBounds.extent.x - 14, offset.y);
  193. Point2I midPoint(start.x + 7, start.y + (mBounds.extent.y/2));
  194. dglDrawRectFill(Point2I(start.x+1,start.y+1), Point2I(start.x+13,start.y+mBounds.extent.y-1) , mProfile->mFillColor);
  195. dglDrawLine(start, Point2I(start.x, start.y+mBounds.extent.y),mProfile->mFontColor);
  196. dglDrawLine(Point2I(start.x,midPoint.y),
  197. Point2I(start.x+14,midPoint.y),
  198. mProfile->mFontColor);
  199. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  200. glColor4f(0,0,0,255);
  201. GLfloat verts[] = {
  202. 0, 0,
  203. 0, 0,
  204. 0, 0,
  205. 0, 0,
  206. 0, 0,
  207. 0, 0,
  208. };
  209. if(mTextAreaHit == ArrowUp)
  210. {
  211. verts[0] = (GLfloat)midPoint.x;
  212. verts[1] = (GLfloat)start.y+1;
  213. verts[2] = (GLfloat)start.x+11;
  214. verts[3] = (GLfloat)midPoint.y-2;
  215. verts[4] = (GLfloat)start.x+3;
  216. verts[5] = (GLfloat)midPoint.y-2;
  217. }
  218. else
  219. {
  220. verts[0] = (GLfloat)midPoint.x;
  221. verts[1] = (GLfloat)start.y+2;
  222. verts[2] = (GLfloat)start.x+11;
  223. verts[3] = (GLfloat)midPoint.y-1;
  224. verts[4] = (GLfloat)start.x+3;
  225. verts[5] = (GLfloat)midPoint.y-1;
  226. }
  227. if(mTextAreaHit == ArrowDown)
  228. {
  229. verts[6] = (GLfloat)midPoint.x;
  230. verts[7] = (GLfloat)start.y+mBounds.extent.y-1;
  231. verts[8] = (GLfloat)start.x+11;
  232. verts[9] = (GLfloat)midPoint.y+3;
  233. verts[10] = (GLfloat)start.x+3;
  234. verts[11] = (GLfloat)midPoint.y+3;
  235. }
  236. else
  237. {
  238. verts[6] = (GLfloat)midPoint.x;
  239. verts[7] = (GLfloat)start.y+mBounds.extent.y-2;
  240. verts[8] = (GLfloat)start.x+11;
  241. verts[9] = (GLfloat)midPoint.y+2;
  242. verts[10] = (GLfloat)start.x+3;
  243. verts[11] = (GLfloat)midPoint.y+2;
  244. }
  245. glVertexPointer(2, GL_FLOAT, 0, verts);
  246. glDrawArrays(GL_TRIANGLES, 0, 6);
  247. #else
  248. glColor3i(0,0,0);
  249. glBegin(GL_TRIANGLES);
  250. if(mTextAreaHit == ArrowUp)
  251. {
  252. glVertex2i(midPoint.x, start.y+1);
  253. glVertex2i(start.x+11,midPoint.y-2);
  254. glVertex2i(start.x+3,midPoint.y-2);
  255. }
  256. else
  257. {
  258. glVertex2i(midPoint.x, start.y+2);
  259. glVertex2i(start.x+11,midPoint.y-1);
  260. glVertex2i(start.x+3,midPoint.y-1);
  261. }
  262. if(mTextAreaHit == ArrowDown)
  263. {
  264. glVertex2i(midPoint.x, start.y+mBounds.extent.y-1);
  265. glVertex2i(start.x+11,midPoint.y+3);
  266. glVertex2i(start.x+3,midPoint.y+3);
  267. }
  268. else
  269. {
  270. glVertex2i(midPoint.x, start.y+mBounds.extent.y-2);
  271. glVertex2i(start.x+11,midPoint.y+2);
  272. glVertex2i(start.x+3,midPoint.y+2);
  273. }
  274. glEnd();
  275. #endif
  276. }