guiSliderCtrl.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "graphics/dgl.h"
  25. #include "graphics/TextureManager.h"
  26. #include "gui/guiSliderCtrl.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "platform/event.h"
  29. IMPLEMENT_CONOBJECT(GuiSliderCtrl);
  30. //----------------------------------------------------------------------------
  31. GuiSliderCtrl::GuiSliderCtrl(void)
  32. {
  33. mActive = true;
  34. mRange.set(0.0f, 1.0f);
  35. mTicks = 10;
  36. mValue = 0.5f;
  37. mThumbSize.set(8, 20);
  38. mShiftPoint = 5;
  39. mShiftExtent = 10;
  40. mDisplayValue = false;
  41. mMouseOver = false;
  42. mDepressed = false;
  43. mIsContainer = false;
  44. }
  45. //----------------------------------------------------------------------------
  46. void GuiSliderCtrl::initPersistFields()
  47. {
  48. Parent::initPersistFields();
  49. addGroup("Slider");
  50. addField("range", TypePoint2F, Offset(mRange, GuiSliderCtrl));
  51. addField("ticks", TypeS32, Offset(mTicks, GuiSliderCtrl));
  52. addField("value", TypeF32, Offset(mValue, GuiSliderCtrl));
  53. endGroup("Slider");
  54. }
  55. //----------------------------------------------------------------------------
  56. ConsoleMethod( GuiSliderCtrl, getValue, F32, 2, 2, "() Use the getValue method to get the current value of the slider.\n"
  57. "@return Returns current value of control (position of slider)"){
  58. return object->getValue();
  59. }
  60. //----------------------------------------------------------------------------
  61. void GuiSliderCtrl::setScriptValue(const char *val)
  62. {
  63. mValue = dAtof(val);
  64. updateThumb(mValue, false);
  65. }
  66. //----------------------------------------------------------------------------
  67. bool GuiSliderCtrl::onWake()
  68. {
  69. if (!Parent::onWake())
  70. return false;
  71. if (mThumbSize.y + mProfile->getFont(mFontSizeAdjust)->getHeight() - 4 <= (U32) mBounds.extent.y)
  72. mDisplayValue = true;
  73. else
  74. mDisplayValue = false;
  75. updateThumb(mValue, false, true);
  76. mHasTexture = mProfile->constructBitmapArray() >= NumBitmaps;
  77. if (mHasTexture)
  78. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  79. return true;
  80. }
  81. //----------------------------------------------------------------------------
  82. void GuiSliderCtrl::onTouchDown(const GuiEvent &event)
  83. {
  84. if (!mActive || !mAwake || !mVisible)
  85. return;
  86. mouseLock();
  87. setFirstResponder();
  88. mDepressed = true;
  89. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  90. F32 value;
  91. if (mBounds.extent.x >= mBounds.extent.y)
  92. value = F32(curMousePos.x - mShiftPoint) / F32(mBounds.extent.x - mShiftExtent) * (mRange.y - mRange.x) + mRange.x;
  93. else
  94. value = F32(curMousePos.y) / F32(mBounds.extent.y) * (mRange.y - mRange.x) + mRange.x;
  95. updateThumb(value, !(event.modifier & SI_SHIFT));
  96. }
  97. //----------------------------------------------------------------------------
  98. void GuiSliderCtrl::onTouchDragged(const GuiEvent &event)
  99. {
  100. if (!mActive || !mAwake || !mVisible)
  101. return;
  102. Point2I curMousePos = globalToLocalCoord(event.mousePoint);
  103. F32 value;
  104. if (mBounds.extent.x >= mBounds.extent.y)
  105. value = F32(curMousePos.x - mShiftPoint) / F32(mBounds.extent.x - mShiftExtent) * (mRange.y - mRange.x) + mRange.x;
  106. else
  107. value = F32(curMousePos.y) / F32(mBounds.extent.y) * (mRange.y - mRange.x) + mRange.x;
  108. if (value > mRange.y)
  109. value = mRange.y;
  110. else if (value < mRange.x)
  111. value = mRange.x;
  112. if (!(event.modifier & SI_SHIFT) && mTicks > 2)
  113. {
  114. // If the shift key is held, snap to the nearest tick, if any are being drawn
  115. F32 tickStep = (mRange.y - mRange.x) / F32(mTicks + 1);
  116. F32 tickSteps = (value - mRange.x) / tickStep;
  117. S32 actualTick = S32(tickSteps + 0.5);
  118. value = actualTick * tickStep + mRange.x;
  119. AssertFatal(value <= mRange.y && value >= mRange.x, "Error, out of bounds value generated from shift-snap of slider");
  120. }
  121. Con::executef(this, 1, "onMouseDragged");
  122. updateThumb(value, !(event.modifier & SI_SHIFT));
  123. }
  124. //----------------------------------------------------------------------------
  125. void GuiSliderCtrl::onTouchUp(const GuiEvent &)
  126. {
  127. if (!mActive || !mAwake || !mVisible)
  128. return;
  129. mDepressed = false;
  130. mouseUnlock();
  131. execConsoleCallback();
  132. }
  133. void GuiSliderCtrl::onTouchEnter(const GuiEvent &event)
  134. {
  135. setUpdate();
  136. if (isMouseLocked())
  137. {
  138. mDepressed = true;
  139. mMouseOver = true;
  140. }
  141. else
  142. {
  143. mMouseOver = true;
  144. }
  145. }
  146. void GuiSliderCtrl::onTouchLeave(const GuiEvent &)
  147. {
  148. setUpdate();
  149. if (isMouseLocked())
  150. mDepressed = false;
  151. mMouseOver = false;
  152. }
  153. //----------------------------------------------------------------------------
  154. void GuiSliderCtrl::updateThumb(F32 _value, bool snap, bool onWake)
  155. {
  156. if (snap && mTicks > 1)
  157. {
  158. // If the shift key is held, snap to the nearest tick, if any are being drawn
  159. F32 tickStep = (mRange.y - mRange.x) / F32(mTicks + 1);
  160. F32 tickSteps = (_value - mRange.x) / tickStep;
  161. S32 actualTick = S32(tickSteps + 0.5);
  162. _value = actualTick * tickStep + mRange.x;
  163. AssertFatal(_value <= mRange.y && _value >= mRange.x, "Error, out of bounds value generated from shift-snap of slider");
  164. }
  165. mValue = _value;
  166. // clamp the thumb to legal values
  167. if (mValue < mRange.x) mValue = mRange.x;
  168. if (mValue > mRange.y) mValue = mRange.y;
  169. Point2I ext = mBounds.extent;
  170. ext.x -= (mShiftExtent + mThumbSize.x) / 2;
  171. // update the bounding thumb rect
  172. if (mBounds.extent.x >= mBounds.extent.y)
  173. { // HORZ thumb
  174. S32 mx = (S32) ((F32(ext.x) * (mValue - mRange.x) / (mRange.y - mRange.x)));
  175. S32 my = ext.y / 2;
  176. if (mDisplayValue)
  177. my = mThumbSize.y / 2;
  178. mThumb.point.x = mx - (mThumbSize.x / 2);
  179. mThumb.point.y = my - (mThumbSize.y / 2);
  180. mThumb.extent = mThumbSize;
  181. }
  182. else
  183. { // VERT thumb
  184. S32 mx = ext.x / 2;
  185. S32 my = (S32) ((F32(ext.y) * (mValue - mRange.x) / (mRange.y - mRange.x)));
  186. mThumb.point.x = mx - (mThumbSize.y / 2);
  187. mThumb.point.y = my - (mThumbSize.x / 2);
  188. mThumb.extent.x = mThumbSize.y;
  189. mThumb.extent.y = mThumbSize.x;
  190. }
  191. setFloatVariable(mValue);
  192. setUpdate();
  193. // Use the alt console command if you want to continually update:
  194. if (!onWake)
  195. execAltConsoleCallback();
  196. }
  197. //----------------------------------------------------------------------------
  198. void GuiSliderCtrl::onRender(Point2I offset, const RectI &updateRect)
  199. {
  200. Point2I pos(offset.x + mShiftPoint, offset.y);
  201. Point2I ext(mBounds.extent.x - mShiftExtent, mBounds.extent.y);
  202. RectI thumb = mThumb;
  203. if (mHasTexture)
  204. {
  205. if (mTicks > 0)
  206. {
  207. // TODO: tick marks should be positioned based on the bitmap dimentions.
  208. Point2I mid(ext.x, ext.y / 2);
  209. Point2I oldpos = pos;
  210. pos += Point2I(1, 0);
  211. glColor4f(0, 0, 0, 1);
  212. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  213. // tick marks
  214. for (U32 t = 0; t <= (mTicks + 1); t++)
  215. {
  216. S32 x = (S32) (F32(mid.x + 1) / F32(mTicks + 1) * F32(t)) + pos.x;
  217. S32 y = pos.y + mid.y;
  218. GLfloat shiftVert[] = {
  219. (GLfloat) (x),
  220. (GLfloat) (y + mShiftPoint),
  221. (GLfloat) (x),
  222. (GLfloat) (y + mShiftPoint * 2.0f + 2.0f),
  223. };
  224. glVertexPointer(2, GL_FLOAT, 0, shiftVert);
  225. glDrawArrays(GL_LINES, 0, 2);
  226. }
  227. glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
  228. // tick marks
  229. for (U32 t = 0; t <= (mTicks + 1); t++)
  230. {
  231. S32 x = (S32) (F32(mid.x + 1) / F32(mTicks + 1) * F32(t)) + pos.x + 1;
  232. S32 y = pos.y + mid.y + 1;
  233. GLfloat shiftVert[] = {
  234. (GLfloat) (x),
  235. (GLfloat) (y + mShiftPoint),
  236. (GLfloat) (x),
  237. (GLfloat) (y + mShiftPoint * 2 + 3),
  238. };
  239. glVertexPointer(2, GL_FLOAT, 0, shiftVert);
  240. glDrawArrays(GL_LINES, 0, 2);
  241. }
  242. #else
  243. glBegin(GL_LINES);
  244. // tick marks
  245. for (U32 t = 0; t <= (mTicks+1); t++)
  246. {
  247. S32 x = (S32)(F32(mid.x+1)/F32(mTicks+1)*F32(t)) + pos.x;
  248. S32 y = pos.y + mid.y;
  249. glVertex2i(x, y + mShiftPoint);
  250. glVertex2i(x, y + mShiftPoint*2 + 2);
  251. }
  252. glEnd();
  253. glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
  254. glBegin(GL_LINES);
  255. // tick marks
  256. for (U32 t = 0; t <= (mTicks+1); t++)
  257. {
  258. S32 x = (S32)(F32(mid.x+1)/F32(mTicks+1)*F32(t)) + pos.x + 1;
  259. S32 y = pos.y + mid.y + 1;
  260. glVertex2i(x, y + mShiftPoint );
  261. glVertex2i(x, y + mShiftPoint*2 + 3);
  262. }
  263. glEnd();
  264. #endif
  265. pos = oldpos;
  266. }
  267. S32 index = SliderButtonNormal;
  268. if (mMouseOver)
  269. index = SliderButtonHighlight;
  270. dglClearBitmapModulation();
  271. //left border
  272. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(offset.x, offset.y + (mBounds.extent.y / 4)), mBitmapBounds[SliderLineLeft]);
  273. //right border
  274. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(offset.x + mBounds.extent.x - mBitmapBounds[SliderLineRight].extent.x, offset.y + (mBounds.extent.y / 4)), mBitmapBounds[SliderLineRight]);
  275. //draw our center piece to our slider control's border and stretch it
  276. RectI destRect;
  277. destRect.point.x = offset.x + mBitmapBounds[SliderLineLeft].extent.x;
  278. destRect.extent.x = mBounds.extent.x - mBitmapBounds[SliderLineLeft].extent.x - mBitmapBounds[SliderLineRight].extent.x;
  279. destRect.point.y = offset.y + (mBounds.extent.y / 4);
  280. destRect.extent.y = mBitmapBounds[SliderLineCenter].extent.y;
  281. RectI stretchRect;
  282. stretchRect = mBitmapBounds[SliderLineCenter];
  283. stretchRect.inset(1, 0);
  284. dglDrawBitmapStretchSR(mProfile->mTextureHandle, destRect, stretchRect);
  285. //draw our control slider button
  286. thumb.point += pos;
  287. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(thumb.point.x, offset.y), mBitmapBounds[index]);
  288. }
  289. else
  290. {
  291. // we're not usina a bitmap, draw procedurally.
  292. if (mBounds.extent.x >= mBounds.extent.y)
  293. {
  294. Point2I mid(ext.x, ext.y / 2);
  295. if (mDisplayValue)
  296. mid.set(ext.x, mThumbSize.y / 2);
  297. glColor4f(0, 0, 0, 1);
  298. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  299. // tick marks
  300. for (U32 t = 0; t <= (mTicks + 1); t++)
  301. {
  302. S32 x = (S32) (F32(mid.x - 1) / F32(mTicks + 1) * F32(t));
  303. GLfloat verts[] = {
  304. static_cast<GLfloat>(pos.x + x), static_cast<GLfloat>(pos.y + mid.y - mShiftPoint),
  305. static_cast<GLfloat>(pos.x + x), static_cast<GLfloat>(pos.y + mid.y + mShiftPoint)
  306. };
  307. glVertexPointer(2, GL_FLOAT, 0, verts);
  308. glDrawArrays(GL_LINES, 0, 2);
  309. }
  310. GLfloat verts[] = {
  311. static_cast<GLfloat>(pos.x), static_cast<GLfloat>(pos.y + mid.y),
  312. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y + mid.y)
  313. };
  314. glVertexPointer(2, GL_FLOAT, 0, verts);
  315. glDrawArrays(GL_LINES, 0, 2);
  316. }
  317. else
  318. {
  319. Point2I mid(ext.x / 2, ext.y);
  320. glColor4f(0, 0, 0, 1);
  321. // tick marks
  322. for (U32 t = 0; t <= (mTicks + 1); t++)
  323. {
  324. S32 y = (S32) (F32(mid.y - 1) / F32(mTicks + 1) * F32(t));
  325. GLfloat verts[] = {
  326. static_cast<GLfloat>(pos.x + mid.x - mShiftPoint), static_cast<GLfloat>(pos.y + y),
  327. static_cast<GLfloat>(pos.x + mid.x + mShiftPoint), static_cast<GLfloat>(pos.y + y)
  328. };
  329. glVertexPointer(2, GL_FLOAT, 0, verts);
  330. glDrawArrays(GL_LINES, 0, 2);
  331. }
  332. GLfloat verts[] = {
  333. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y),
  334. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y + mid.y)
  335. };
  336. //for the horizontal line
  337. glVertexPointer(2, GL_FLOAT, 0, verts);
  338. glDrawArrays(GL_LINES, 0, 2);
  339. #else
  340. glBegin(GL_LINES);
  341. // horz rule
  342. glVertex2i(pos.x, pos.y+mid.y);
  343. glVertex2i(pos.x+mid.x, pos.y+mid.y);
  344. // tick marks
  345. for (U32 t = 0; t <= (mTicks+1); t++)
  346. {
  347. S32 x = (S32)(F32(mid.x-1)/F32(mTicks+1)*F32(t));
  348. glVertex2i(pos.x+x, pos.y+mid.y-mShiftPoint);
  349. glVertex2i(pos.x+x, pos.y+mid.y+mShiftPoint);
  350. }
  351. glEnd();
  352. }
  353. else
  354. {
  355. Point2I mid(ext.x/2, ext.y);
  356. glColor4f(0, 0, 0, 1);
  357. glBegin(GL_LINES);
  358. // horz rule
  359. glVertex2i(pos.x+mid.x, pos.y);
  360. glVertex2i(pos.x+mid.x, pos.y+mid.y);
  361. // tick marks
  362. for (U32 t = 0; t <= (mTicks+1); t++)
  363. {
  364. S32 y = (S32)(F32(mid.y-1)/F32(mTicks+1)*F32(t));
  365. glVertex2i(pos.x+mid.x-mShiftPoint, pos.y+y);
  366. glVertex2i(pos.x+mid.x+mShiftPoint, pos.y+y);
  367. }
  368. glEnd();
  369. #endif
  370. mDisplayValue = false;
  371. }
  372. // draw the thumb
  373. thumb.point += pos;
  374. renderUniversalRect(thumb, mProfile, NormalState);
  375. }
  376. if (mDisplayValue)
  377. {
  378. char buf[20];
  379. dSprintf(buf, sizeof(buf), "%0.3g", mValue);
  380. Point2I textStart = thumb.point;
  381. S32 txt_w = mProfile->getFont(mFontSizeAdjust)->getStrWidth((const UTF8 *) buf);
  382. textStart.x += (S32) ((thumb.extent.x / 2.0f));
  383. textStart.y += thumb.extent.y - 2; //19
  384. textStart.x -= (txt_w / 2);
  385. if (textStart.x < offset.x)
  386. textStart.x = offset.x;
  387. else if (textStart.x + txt_w > offset.x + mBounds.extent.x)
  388. textStart.x -= ((textStart.x + txt_w) - (offset.x + mBounds.extent.x));
  389. dglSetBitmapModulation(getFontColor(mProfile));
  390. dglDrawText(mProfile->getFont(mFontSizeAdjust), textStart, buf, mProfile->mFontColors);
  391. }
  392. renderChildControls(offset, mBounds, updateRect);
  393. }