guiSliderCtrl.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. if (mActive && mProfile->mSoundButtonOver)
  144. {
  145. //F32 pan = (F32(event.mousePoint.x)/F32(Canvas->mBounds.extent.x)*2.0f-1.0f)*0.8f;
  146. AUDIOHANDLE handle = alxCreateSource(mProfile->mSoundButtonOver);
  147. alxPlay(handle);
  148. }
  149. mMouseOver = true;
  150. }
  151. }
  152. void GuiSliderCtrl::onTouchLeave(const GuiEvent &)
  153. {
  154. setUpdate();
  155. if (isMouseLocked())
  156. mDepressed = false;
  157. mMouseOver = false;
  158. }
  159. //----------------------------------------------------------------------------
  160. void GuiSliderCtrl::updateThumb(F32 _value, bool snap, bool onWake)
  161. {
  162. if (snap && mTicks > 1)
  163. {
  164. // If the shift key is held, snap to the nearest tick, if any are being drawn
  165. F32 tickStep = (mRange.y - mRange.x) / F32(mTicks + 1);
  166. F32 tickSteps = (_value - mRange.x) / tickStep;
  167. S32 actualTick = S32(tickSteps + 0.5);
  168. _value = actualTick * tickStep + mRange.x;
  169. AssertFatal(_value <= mRange.y && _value >= mRange.x, "Error, out of bounds value generated from shift-snap of slider");
  170. }
  171. mValue = _value;
  172. // clamp the thumb to legal values
  173. if (mValue < mRange.x) mValue = mRange.x;
  174. if (mValue > mRange.y) mValue = mRange.y;
  175. Point2I ext = mBounds.extent;
  176. ext.x -= (mShiftExtent + mThumbSize.x) / 2;
  177. // update the bounding thumb rect
  178. if (mBounds.extent.x >= mBounds.extent.y)
  179. { // HORZ thumb
  180. S32 mx = (S32) ((F32(ext.x) * (mValue - mRange.x) / (mRange.y - mRange.x)));
  181. S32 my = ext.y / 2;
  182. if (mDisplayValue)
  183. my = mThumbSize.y / 2;
  184. mThumb.point.x = mx - (mThumbSize.x / 2);
  185. mThumb.point.y = my - (mThumbSize.y / 2);
  186. mThumb.extent = mThumbSize;
  187. }
  188. else
  189. { // VERT thumb
  190. S32 mx = ext.x / 2;
  191. S32 my = (S32) ((F32(ext.y) * (mValue - mRange.x) / (mRange.y - mRange.x)));
  192. mThumb.point.x = mx - (mThumbSize.y / 2);
  193. mThumb.point.y = my - (mThumbSize.x / 2);
  194. mThumb.extent.x = mThumbSize.y;
  195. mThumb.extent.y = mThumbSize.x;
  196. }
  197. setFloatVariable(mValue);
  198. setUpdate();
  199. // Use the alt console command if you want to continually update:
  200. if (!onWake)
  201. execAltConsoleCallback();
  202. }
  203. //----------------------------------------------------------------------------
  204. void GuiSliderCtrl::onRender(Point2I offset, const RectI &updateRect)
  205. {
  206. Point2I pos(offset.x + mShiftPoint, offset.y);
  207. Point2I ext(mBounds.extent.x - mShiftExtent, mBounds.extent.y);
  208. RectI thumb = mThumb;
  209. if (mHasTexture)
  210. {
  211. if (mTicks > 0)
  212. {
  213. // TODO: tick marks should be positioned based on the bitmap dimentions.
  214. Point2I mid(ext.x, ext.y / 2);
  215. Point2I oldpos = pos;
  216. pos += Point2I(1, 0);
  217. glColor4f(0, 0, 0, 1);
  218. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  219. // tick marks
  220. for (U32 t = 0; t <= (mTicks + 1); t++)
  221. {
  222. S32 x = (S32) (F32(mid.x + 1) / F32(mTicks + 1) * F32(t)) + pos.x;
  223. S32 y = pos.y + mid.y;
  224. GLfloat shiftVert[] = {
  225. (GLfloat) (x),
  226. (GLfloat) (y + mShiftPoint),
  227. (GLfloat) (x),
  228. (GLfloat) (y + mShiftPoint * 2.0f + 2.0f),
  229. };
  230. glVertexPointer(2, GL_FLOAT, 0, shiftVert);
  231. glDrawArrays(GL_LINES, 0, 2);
  232. }
  233. glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
  234. // tick marks
  235. for (U32 t = 0; t <= (mTicks + 1); t++)
  236. {
  237. S32 x = (S32) (F32(mid.x + 1) / F32(mTicks + 1) * F32(t)) + pos.x + 1;
  238. S32 y = pos.y + mid.y + 1;
  239. GLfloat shiftVert[] = {
  240. (GLfloat) (x),
  241. (GLfloat) (y + mShiftPoint),
  242. (GLfloat) (x),
  243. (GLfloat) (y + mShiftPoint * 2 + 3),
  244. };
  245. glVertexPointer(2, GL_FLOAT, 0, shiftVert);
  246. glDrawArrays(GL_LINES, 0, 2);
  247. }
  248. #else
  249. glBegin(GL_LINES);
  250. // tick marks
  251. for (U32 t = 0; t <= (mTicks+1); t++)
  252. {
  253. S32 x = (S32)(F32(mid.x+1)/F32(mTicks+1)*F32(t)) + pos.x;
  254. S32 y = pos.y + mid.y;
  255. glVertex2i(x, y + mShiftPoint);
  256. glVertex2i(x, y + mShiftPoint*2 + 2);
  257. }
  258. glEnd();
  259. glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
  260. glBegin(GL_LINES);
  261. // tick marks
  262. for (U32 t = 0; t <= (mTicks+1); t++)
  263. {
  264. S32 x = (S32)(F32(mid.x+1)/F32(mTicks+1)*F32(t)) + pos.x + 1;
  265. S32 y = pos.y + mid.y + 1;
  266. glVertex2i(x, y + mShiftPoint );
  267. glVertex2i(x, y + mShiftPoint*2 + 3);
  268. }
  269. glEnd();
  270. #endif
  271. pos = oldpos;
  272. }
  273. S32 index = SliderButtonNormal;
  274. if (mMouseOver)
  275. index = SliderButtonHighlight;
  276. dglClearBitmapModulation();
  277. //left border
  278. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(offset.x, offset.y + (mBounds.extent.y / 4)), mBitmapBounds[SliderLineLeft]);
  279. //right border
  280. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(offset.x + mBounds.extent.x - mBitmapBounds[SliderLineRight].extent.x, offset.y + (mBounds.extent.y / 4)), mBitmapBounds[SliderLineRight]);
  281. //draw our center piece to our slider control's border and stretch it
  282. RectI destRect;
  283. destRect.point.x = offset.x + mBitmapBounds[SliderLineLeft].extent.x;
  284. destRect.extent.x = mBounds.extent.x - mBitmapBounds[SliderLineLeft].extent.x - mBitmapBounds[SliderLineRight].extent.x;
  285. destRect.point.y = offset.y + (mBounds.extent.y / 4);
  286. destRect.extent.y = mBitmapBounds[SliderLineCenter].extent.y;
  287. RectI stretchRect;
  288. stretchRect = mBitmapBounds[SliderLineCenter];
  289. stretchRect.inset(1, 0);
  290. dglDrawBitmapStretchSR(mProfile->mTextureHandle, destRect, stretchRect);
  291. //draw our control slider button
  292. thumb.point += pos;
  293. dglDrawBitmapSR(mProfile->mTextureHandle, Point2I(thumb.point.x, offset.y), mBitmapBounds[index]);
  294. }
  295. else
  296. {
  297. // we're not usina a bitmap, draw procedurally.
  298. if (mBounds.extent.x >= mBounds.extent.y)
  299. {
  300. Point2I mid(ext.x, ext.y / 2);
  301. if (mDisplayValue)
  302. mid.set(ext.x, mThumbSize.y / 2);
  303. glColor4f(0, 0, 0, 1);
  304. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  305. // tick marks
  306. for (U32 t = 0; t <= (mTicks + 1); t++)
  307. {
  308. S32 x = (S32) (F32(mid.x - 1) / F32(mTicks + 1) * F32(t));
  309. GLfloat verts[] = {
  310. static_cast<GLfloat>(pos.x + x), static_cast<GLfloat>(pos.y + mid.y - mShiftPoint),
  311. static_cast<GLfloat>(pos.x + x), static_cast<GLfloat>(pos.y + mid.y + mShiftPoint)
  312. };
  313. glVertexPointer(2, GL_FLOAT, 0, verts);
  314. glDrawArrays(GL_LINES, 0, 2);
  315. }
  316. GLfloat verts[] = {
  317. static_cast<GLfloat>(pos.x), static_cast<GLfloat>(pos.y + mid.y),
  318. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y + mid.y)
  319. };
  320. glVertexPointer(2, GL_FLOAT, 0, verts);
  321. glDrawArrays(GL_LINES, 0, 2);
  322. }
  323. else
  324. {
  325. Point2I mid(ext.x / 2, ext.y);
  326. glColor4f(0, 0, 0, 1);
  327. // tick marks
  328. for (U32 t = 0; t <= (mTicks + 1); t++)
  329. {
  330. S32 y = (S32) (F32(mid.y - 1) / F32(mTicks + 1) * F32(t));
  331. GLfloat verts[] = {
  332. static_cast<GLfloat>(pos.x + mid.x - mShiftPoint), static_cast<GLfloat>(pos.y + y),
  333. static_cast<GLfloat>(pos.x + mid.x + mShiftPoint), static_cast<GLfloat>(pos.y + y)
  334. };
  335. glVertexPointer(2, GL_FLOAT, 0, verts);
  336. glDrawArrays(GL_LINES, 0, 2);
  337. }
  338. GLfloat verts[] = {
  339. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y),
  340. static_cast<GLfloat>(pos.x + mid.x), static_cast<GLfloat>(pos.y + mid.y)
  341. };
  342. //for the horizontal line
  343. glVertexPointer(2, GL_FLOAT, 0, verts);
  344. glDrawArrays(GL_LINES, 0, 2);
  345. #else
  346. glBegin(GL_LINES);
  347. // horz rule
  348. glVertex2i(pos.x, pos.y+mid.y);
  349. glVertex2i(pos.x+mid.x, pos.y+mid.y);
  350. // tick marks
  351. for (U32 t = 0; t <= (mTicks+1); t++)
  352. {
  353. S32 x = (S32)(F32(mid.x-1)/F32(mTicks+1)*F32(t));
  354. glVertex2i(pos.x+x, pos.y+mid.y-mShiftPoint);
  355. glVertex2i(pos.x+x, pos.y+mid.y+mShiftPoint);
  356. }
  357. glEnd();
  358. }
  359. else
  360. {
  361. Point2I mid(ext.x/2, ext.y);
  362. glColor4f(0, 0, 0, 1);
  363. glBegin(GL_LINES);
  364. // horz rule
  365. glVertex2i(pos.x+mid.x, pos.y);
  366. glVertex2i(pos.x+mid.x, pos.y+mid.y);
  367. // tick marks
  368. for (U32 t = 0; t <= (mTicks+1); t++)
  369. {
  370. S32 y = (S32)(F32(mid.y-1)/F32(mTicks+1)*F32(t));
  371. glVertex2i(pos.x+mid.x-mShiftPoint, pos.y+y);
  372. glVertex2i(pos.x+mid.x+mShiftPoint, pos.y+y);
  373. }
  374. glEnd();
  375. #endif
  376. mDisplayValue = false;
  377. }
  378. // draw the thumb
  379. thumb.point += pos;
  380. renderUniversalRect(thumb, mProfile, NormalState);
  381. }
  382. if (mDisplayValue)
  383. {
  384. char buf[20];
  385. dSprintf(buf, sizeof(buf), "%0.3g", mValue);
  386. Point2I textStart = thumb.point;
  387. S32 txt_w = mProfile->getFont(mFontSizeAdjust)->getStrWidth((const UTF8 *) buf);
  388. textStart.x += (S32) ((thumb.extent.x / 2.0f));
  389. textStart.y += thumb.extent.y - 2; //19
  390. textStart.x -= (txt_w / 2);
  391. if (textStart.x < offset.x)
  392. textStart.x = offset.x;
  393. else if (textStart.x + txt_w > offset.x + mBounds.extent.x)
  394. textStart.x -= ((textStart.x + txt_w) - (offset.x + mBounds.extent.x));
  395. dglSetBitmapModulation(getFontColor(mProfile));
  396. dglDrawText(mProfile->getFont(mFontSizeAdjust), textStart, buf, mProfile->mFontColors);
  397. }
  398. renderChildControls(offset, mBounds, updateRect);
  399. }