guiTextEditSliderCtrl.cc 8.8 KB

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