Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

915 lines
23 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #define PROTECTED_THINGS_DISABLE
  9. #include <vgui/MouseCode.h>
  10. #include <keyvalues.h>
  11. #include <vgui/IBorder.h>
  12. #include <vgui/IInput.h>
  13. #include <vgui/ISystem.h>
  14. #include <vgui/IScheme.h>
  15. #include <vgui/ISurface.h>
  16. #include <vgui/ILocalize.h>
  17. #include <vgui_controls/Slider.h>
  18. #include <vgui_controls/Controls.h>
  19. #include <vgui_controls/TextImage.h>
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include <tier0/memdbgon.h>
  22. using namespace vgui;
  23. DECLARE_BUILD_FACTORY( Slider );
  24. static const float NOB_SIZE = 8.0f;
  25. //-----------------------------------------------------------------------------
  26. // Purpose: Create a slider bar with ticks underneath it
  27. //-----------------------------------------------------------------------------
  28. Slider::Slider(Panel *parent, const char *panelName ) : BaseClass(parent, panelName)
  29. {
  30. m_bIsDragOnRepositionNob = false;
  31. _dragging = false;
  32. _value = 0;
  33. _range[0] = 0;
  34. _range[1] = 0;
  35. _buttonOffset = 0;
  36. _sliderBorder = NULL;
  37. _insetBorder = NULL;
  38. m_nNumTicks = 10;
  39. _leftCaption = NULL;
  40. _rightCaption = NULL;
  41. SetThumbWidth( 8 );
  42. RecomputeNobPosFromValue();
  43. AddActionSignalTarget(this);
  44. SetBlockDragChaining( true );
  45. _subrange[ 0 ] = 0;
  46. _subrange[ 1 ] = 0;
  47. m_bUseSubRange = false;
  48. m_bInverted = false;
  49. }
  50. // This allows the slider to behave like it's larger than what's actually being drawn
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. // Input : bEnable -
  54. // 0 -
  55. // 100 -
  56. //-----------------------------------------------------------------------------
  57. void Slider::SetSliderThumbSubRange( bool bEnable, int nMin /*= 0*/, int nMax /*= 100*/ )
  58. {
  59. m_bUseSubRange = bEnable;
  60. _subrange[ 0 ] = nMin;
  61. _subrange[ 1 ] = nMax;
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Set the size of the slider bar.
  65. // Warning less than 30 pixels tall and everything probably won't fit.
  66. //-----------------------------------------------------------------------------
  67. void Slider::OnSizeChanged(int wide,int tall)
  68. {
  69. BaseClass::OnSizeChanged(wide,tall);
  70. RecomputeNobPosFromValue();
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: Set the value of the slider to one of the ticks.
  74. //-----------------------------------------------------------------------------
  75. void Slider::SetValue(int value, bool bTriggerChangeMessage)
  76. {
  77. int oldValue=_value;
  78. if ( _range[0] < _range[1] )
  79. {
  80. if(value<_range[0])
  81. {
  82. value=_range[0];
  83. }
  84. if(value>_range[1])
  85. {
  86. value=_range[1];
  87. }
  88. }
  89. else
  90. {
  91. if(value<_range[1])
  92. {
  93. value=_range[1];
  94. }
  95. if(value>_range[0])
  96. {
  97. value=_range[0];
  98. }
  99. }
  100. _value = value;
  101. RecomputeNobPosFromValue();
  102. if (_value != oldValue && bTriggerChangeMessage)
  103. {
  104. SendSliderMovedMessage();
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose: Return the value of the slider
  109. //-----------------------------------------------------------------------------
  110. int Slider::GetValue()
  111. {
  112. return _value;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose: Layout the slider before drawing it on screen.
  116. //-----------------------------------------------------------------------------
  117. void Slider::PerformLayout()
  118. {
  119. BaseClass::PerformLayout();
  120. RecomputeNobPosFromValue();
  121. if (_leftCaption)
  122. {
  123. _leftCaption->ResizeImageToContent();
  124. }
  125. if (_rightCaption)
  126. {
  127. _rightCaption->ResizeImageToContent();
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Move the nob on the slider in response to changing its value.
  132. //-----------------------------------------------------------------------------
  133. void Slider::RecomputeNobPosFromValue()
  134. {
  135. //int wide,tall;
  136. //GetPaintSize(wide,tall);
  137. int x, y, wide, tall;
  138. GetTrackRect( x, y, wide, tall );
  139. float usevalue = _value;
  140. int *userange = &_range[ 0 ];
  141. if ( m_bUseSubRange )
  142. {
  143. userange = &_subrange[ 0 ];
  144. usevalue = clamp( _value, _subrange[ 0 ], _subrange[ 1 ] );
  145. }
  146. float fwide=(float)wide;
  147. float frange=(float)(userange[1] -userange[0]);
  148. float fvalue=(float)(usevalue -userange[0]);
  149. float fper = (frange != 0.0f) ? fvalue / frange : 0.0f;
  150. if ( m_bInverted )
  151. fper = 1.0f - fper;
  152. float freepixels = fwide - _nobSize;
  153. float leftpixel = (float)x;
  154. float firstpixel = leftpixel + freepixels * fper + 0.5f;
  155. _nobPos[0]=(int)( firstpixel );
  156. _nobPos[1]=(int)( firstpixel + _nobSize );
  157. int rightEdge = x + wide;
  158. if(_nobPos[1]> rightEdge )
  159. {
  160. _nobPos[0]=rightEdge-((int)_nobSize);
  161. _nobPos[1]=rightEdge;
  162. }
  163. Repaint();
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose: Sync the slider's value up with the nob's position.
  167. //-----------------------------------------------------------------------------
  168. void Slider::RecomputeValueFromNobPos()
  169. {
  170. int value = EstimateValueAtPos( _nobPos[ 0 ], 0 );
  171. SetValue( value );
  172. }
  173. int Slider::EstimateValueAtPos( int localMouseX, int /*localMouseY*/ )
  174. {
  175. int x, y, wide, tall;
  176. GetTrackRect( x, y, wide, tall );
  177. int *userange = &_range[ 0 ];
  178. if ( m_bUseSubRange )
  179. {
  180. userange = &_subrange[ 0 ];
  181. }
  182. float fwide = (float)wide;
  183. float fvalue = (float)( _value - userange[0] );
  184. float fnob = (float)( localMouseX - x );
  185. float freepixels = fwide - _nobSize;
  186. // Map into reduced range
  187. fvalue = freepixels != 0.0f ? fnob / freepixels : 0.0f;
  188. return (int) (RemapVal( fvalue, 0.0, 1.0, userange[0], userange[1] ));
  189. }
  190. void Slider::SetInverted( bool bInverted )
  191. {
  192. m_bInverted = bInverted;
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose: Send a message to interested parties when the slider moves
  196. //-----------------------------------------------------------------------------
  197. void Slider::SendSliderMovedMessage()
  198. {
  199. // send a changed message
  200. KeyValues *pParams = new KeyValues("SliderMoved", "position", _value);
  201. pParams->SetPtr( "panel", this );
  202. PostActionSignal( pParams );
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Purpose: Send a message to interested parties when the user begins dragging the slider
  206. //-----------------------------------------------------------------------------
  207. void Slider::SendSliderDragStartMessage()
  208. {
  209. // send a message
  210. KeyValues *pParams = new KeyValues("SliderDragStart", "position", _value);
  211. pParams->SetPtr( "panel", this );
  212. PostActionSignal( pParams );
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Purpose: Send a message to interested parties when the user ends dragging the slider
  216. //-----------------------------------------------------------------------------
  217. void Slider::SendSliderDragEndMessage()
  218. {
  219. // send a message
  220. KeyValues *pParams = new KeyValues("SliderDragEnd", "position", _value);
  221. pParams->SetPtr( "panel", this );
  222. PostActionSignal( pParams );
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Purpose:
  226. //-----------------------------------------------------------------------------
  227. void Slider::ApplySchemeSettings(IScheme *pScheme)
  228. {
  229. BaseClass::ApplySchemeSettings(pScheme);
  230. SetFgColor(GetSchemeColor("Slider.NobColor", pScheme));
  231. // this line is useful for debugging
  232. //SetBgColor(GetSchemeColor("0 0 0 255"));
  233. m_TickColor = pScheme->GetColor( "Slider.TextColor", GetFgColor() );
  234. m_TrackColor = pScheme->GetColor( "Slider.TrackColor", GetFgColor() );
  235. #ifdef _GAMECONSOLE
  236. m_DepressedBgColor = GetSchemeColor("Slider.NobFocusColor", pScheme);
  237. #endif
  238. m_DisabledTextColor1 = pScheme->GetColor( "Slider.DisabledTextColor1", GetFgColor() );
  239. m_DisabledTextColor2 = pScheme->GetColor( "Slider.DisabledTextColor2", GetFgColor() );
  240. _sliderBorder = pScheme->GetBorder("ButtonBorder");
  241. _insetBorder = pScheme->GetBorder("ButtonDepressedBorder");
  242. if ( _leftCaption )
  243. {
  244. _leftCaption->SetFont(pScheme->GetFont("DefaultVerySmall", IsProportional() ));
  245. }
  246. if ( _rightCaption )
  247. {
  248. _rightCaption->SetFont(pScheme->GetFont("DefaultVerySmall", IsProportional() ));
  249. }
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Purpose:
  253. //-----------------------------------------------------------------------------
  254. void Slider::GetSettings(KeyValues *outResourceData)
  255. {
  256. BaseClass::GetSettings(outResourceData);
  257. char buf[256];
  258. if (_leftCaption)
  259. {
  260. _leftCaption->GetUnlocalizedText(buf, sizeof(buf));
  261. outResourceData->SetString("leftText", buf);
  262. }
  263. if (_rightCaption)
  264. {
  265. _rightCaption->GetUnlocalizedText(buf, sizeof(buf));
  266. outResourceData->SetString("rightText", buf);
  267. }
  268. }
  269. //-----------------------------------------------------------------------------
  270. // Purpose:
  271. //-----------------------------------------------------------------------------
  272. void Slider::ApplySettings(KeyValues *inResourceData)
  273. {
  274. BaseClass::ApplySettings(inResourceData);
  275. const char *left = inResourceData->GetString("leftText", NULL);
  276. const char *right = inResourceData->GetString("rightText", NULL);
  277. int thumbWidth = inResourceData->GetInt("thumbwidth", 0);
  278. if (thumbWidth != 0)
  279. {
  280. SetThumbWidth(thumbWidth);
  281. }
  282. SetTickCaptions(left, right);
  283. }
  284. //-----------------------------------------------------------------------------
  285. // Purpose:
  286. //-----------------------------------------------------------------------------
  287. const char *Slider::GetDescription()
  288. {
  289. static char buf[1024];
  290. Q_snprintf(buf, sizeof(buf), "%s, string leftText, string rightText", BaseClass::GetDescription());
  291. return buf;
  292. }
  293. //-----------------------------------------------------------------------------
  294. // Purpose: Get the rectangle to draw the slider track in.
  295. //-----------------------------------------------------------------------------
  296. void Slider::GetTrackRect( int& x, int& y, int& w, int& h )
  297. {
  298. int wide, tall;
  299. GetPaintSize( wide, tall );
  300. x = 0;
  301. y = 8;
  302. w = wide - (int)_nobSize;
  303. h = 4;
  304. }
  305. //-----------------------------------------------------------------------------
  306. // Purpose: Draw everything on screen
  307. //-----------------------------------------------------------------------------
  308. void Slider::Paint()
  309. {
  310. DrawTicks();
  311. DrawTickLabels();
  312. // Draw nob last so it draws over ticks.
  313. DrawNob();
  314. }
  315. //-----------------------------------------------------------------------------
  316. // Purpose: Draw the ticks below the slider.
  317. //-----------------------------------------------------------------------------
  318. void Slider::DrawTicks()
  319. {
  320. int x, y;
  321. int wide,tall;
  322. GetTrackRect( x, y, wide, tall );
  323. // Figure out how to draw the ticks
  324. // GetPaintSize( wide, tall );
  325. float fwide = (float)wide;
  326. float freepixels = fwide - _nobSize;
  327. float leftpixel = _nobSize / 2.0f;
  328. float pixelspertick = freepixels / ( m_nNumTicks );
  329. y += (int)_nobSize;
  330. int tickHeight = 5;
  331. if (IsEnabled())
  332. {
  333. surface()->DrawSetColor( m_TickColor ); //vgui::Color( 127, 140, 127, 255 ) );
  334. for ( int i = 0; i <= m_nNumTicks; i++ )
  335. {
  336. int xpos = (int)( leftpixel + i * pixelspertick );
  337. surface()->DrawFilledRect( xpos, y, xpos + 1, y + tickHeight );
  338. }
  339. }
  340. else
  341. {
  342. surface()->DrawSetColor( m_DisabledTextColor1 ); //vgui::Color( 127, 140, 127, 255 ) );
  343. for ( int i = 0; i <= m_nNumTicks; i++ )
  344. {
  345. int xpos = (int)( leftpixel + i * pixelspertick );
  346. surface()->DrawFilledRect( xpos+1, y+1, xpos + 2, y + tickHeight + 1 );
  347. }
  348. surface()->DrawSetColor( m_DisabledTextColor2 ); //vgui::Color( 127, 140, 127, 255 ) );
  349. for ( int i = 0; i <= m_nNumTicks; i++ )
  350. {
  351. int xpos = (int)( leftpixel + i * pixelspertick );
  352. surface()->DrawFilledRect( xpos, y, xpos + 1, y + tickHeight );
  353. }
  354. }
  355. }
  356. //-----------------------------------------------------------------------------
  357. // Purpose: Draw Tick labels under the ticks.
  358. //-----------------------------------------------------------------------------
  359. void Slider::DrawTickLabels()
  360. {
  361. int x, y;
  362. int wide,tall;
  363. GetTrackRect( x, y, wide, tall );
  364. // Figure out how to draw the ticks
  365. // GetPaintSize( wide, tall );
  366. y += (int)NOB_SIZE + 4;
  367. // Draw Start and end range values
  368. if (IsEnabled())
  369. surface()->DrawSetTextColor( m_TickColor ); //vgui::Color( 127, 140, 127, 255 ) );
  370. else
  371. surface()->DrawSetTextColor( m_DisabledTextColor1 ); //vgui::Color( 127, 140, 127, 255 ) );
  372. if ( _leftCaption != NULL )
  373. {
  374. _leftCaption->SetPos(0, y);
  375. if (IsEnabled())
  376. {
  377. _leftCaption->SetColor( m_TickColor );
  378. }
  379. else
  380. {
  381. _leftCaption->SetColor( m_DisabledTextColor1 );
  382. }
  383. _leftCaption->Paint();
  384. }
  385. if ( _rightCaption != NULL)
  386. {
  387. int rwide, rtall;
  388. _rightCaption->GetSize(rwide, rtall);
  389. _rightCaption->SetPos((int)(wide - rwide) , y);
  390. if (IsEnabled())
  391. {
  392. _rightCaption->SetColor( m_TickColor );
  393. }
  394. else
  395. {
  396. _rightCaption->SetColor( m_DisabledTextColor1 );
  397. }
  398. _rightCaption->Paint();
  399. }
  400. }
  401. //-----------------------------------------------------------------------------
  402. // Purpose: Draw the nob part of the slider.
  403. //-----------------------------------------------------------------------------
  404. void Slider::DrawNob()
  405. {
  406. // horizontal nob
  407. int x, y;
  408. int wide,tall;
  409. GetTrackRect( x, y, wide, tall );
  410. Color col = GetFgColor();
  411. #ifdef _GAMECONSOLE
  412. if(HasFocus())
  413. {
  414. col = m_DepressedBgColor;
  415. }
  416. #endif
  417. surface()->DrawSetColor(col);
  418. int nobheight = 16;
  419. surface()->DrawFilledRect(
  420. _nobPos[0],
  421. y + tall / 2 - nobheight / 2,
  422. _nobPos[1],
  423. y + tall / 2 + nobheight / 2);
  424. // border
  425. if (_sliderBorder)
  426. {
  427. _sliderBorder->Paint(
  428. _nobPos[0],
  429. y + tall / 2 - nobheight / 2,
  430. _nobPos[1],
  431. y + tall / 2 + nobheight / 2);
  432. }
  433. }
  434. //-----------------------------------------------------------------------------
  435. // Purpose: Set the text labels of the Start and end ticks.
  436. //-----------------------------------------------------------------------------
  437. void Slider::SetTickCaptions( const char *left, const char *right )
  438. {
  439. if (left)
  440. {
  441. if (_leftCaption)
  442. {
  443. _leftCaption->SetText(left);
  444. }
  445. else
  446. {
  447. _leftCaption = new TextImage(left);
  448. }
  449. }
  450. if (right)
  451. {
  452. if (_rightCaption)
  453. {
  454. _rightCaption->SetText(right);
  455. }
  456. else
  457. {
  458. _rightCaption = new TextImage(right);
  459. }
  460. }
  461. InvalidateLayout();
  462. }
  463. //-----------------------------------------------------------------------------
  464. // Purpose: Set the text labels of the Start and end ticks.
  465. //-----------------------------------------------------------------------------
  466. void Slider::SetTickCaptions( const wchar_t *left, const wchar_t *right )
  467. {
  468. if (left)
  469. {
  470. if (_leftCaption)
  471. {
  472. _leftCaption->SetText(left);
  473. }
  474. else
  475. {
  476. _leftCaption = new TextImage(left);
  477. }
  478. }
  479. if (right)
  480. {
  481. if (_rightCaption)
  482. {
  483. _rightCaption->SetText(right);
  484. }
  485. else
  486. {
  487. _rightCaption = new TextImage(right);
  488. }
  489. }
  490. InvalidateLayout();
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Purpose: Draw the slider track
  494. //-----------------------------------------------------------------------------
  495. void Slider::PaintBackground()
  496. {
  497. BaseClass::PaintBackground();
  498. int x, y;
  499. int wide,tall;
  500. GetTrackRect( x, y, wide, tall );
  501. surface()->DrawSetColor( m_TrackColor );
  502. surface()->DrawFilledRect( x, y, x + wide, y + tall );
  503. if (_insetBorder)
  504. {
  505. _insetBorder->Paint( x, y, x + wide, y + tall );
  506. }
  507. }
  508. //-----------------------------------------------------------------------------
  509. // Purpose: Set the range of the slider.
  510. //-----------------------------------------------------------------------------
  511. void Slider::SetRange(int min,int max)
  512. {
  513. _range[0]=min;
  514. _range[1]=max;
  515. if ( _range[0] < _range[1] )
  516. {
  517. if(_value<_range[0])
  518. {
  519. SetValue( _range[0] );
  520. }
  521. else if( _value>_range[1])
  522. {
  523. SetValue( _range[1] );
  524. }
  525. }
  526. else
  527. {
  528. if(_value<_range[1])
  529. {
  530. SetValue( _range[1] );
  531. }
  532. else if( _value>_range[0])
  533. {
  534. SetValue( _range[0] );
  535. }
  536. }
  537. }
  538. //-----------------------------------------------------------------------------
  539. // Purpose: Get the max and min values of the slider
  540. //-----------------------------------------------------------------------------
  541. void Slider::GetRange(int& min,int& max)
  542. {
  543. min=_range[0];
  544. max=_range[1];
  545. }
  546. //-----------------------------------------------------------------------------
  547. // Purpose: Respond when the cursor is moved in our window if we are clicking
  548. // and dragging.
  549. //-----------------------------------------------------------------------------
  550. void Slider::OnCursorMoved(int x,int y)
  551. {
  552. if(!_dragging)
  553. {
  554. return;
  555. }
  556. // input()->GetCursorPos(x,y);
  557. input()->GetCursorPosition( x, y );
  558. ScreenToLocal(x,y);
  559. // int wide,tall;
  560. // GetPaintSize(wide,tall);
  561. int _x, _y, wide, tall;
  562. GetTrackRect( _x, _y, wide, tall );
  563. _nobPos[0]=_nobDragStartPos[0]+(x-_dragStartPos[0]);
  564. _nobPos[1]=_nobDragStartPos[1]+(x-_dragStartPos[0]);
  565. int rightEdge = _x +wide;
  566. int unclamped = _nobPos[ 0 ];
  567. if(_nobPos[1]>rightEdge)
  568. {
  569. _nobPos[0]=rightEdge-(_nobPos[1]-_nobPos[0]);
  570. _nobPos[1]=rightEdge;
  571. }
  572. if(_nobPos[0]<_x)
  573. {
  574. int offset = _x - _nobPos[0];
  575. _nobPos[1]=_nobPos[1]-offset;
  576. _nobPos[0]=0;
  577. }
  578. int value = EstimateValueAtPos( unclamped, 0 );
  579. SetValue( value );
  580. // RecomputeValueFromNobPos();
  581. Repaint();
  582. SendSliderMovedMessage();
  583. }
  584. //-----------------------------------------------------------------------------
  585. // Purpose: If you click on the slider outside of the nob, the nob jumps
  586. // to the click position, and if this setting is enabled, the nob
  587. // is then draggable from the new position until the mouse is released
  588. // Input : state -
  589. //-----------------------------------------------------------------------------
  590. void Slider::SetDragOnRepositionNob( bool state )
  591. {
  592. m_bIsDragOnRepositionNob = state;
  593. }
  594. bool Slider::IsDragOnRepositionNob() const
  595. {
  596. return m_bIsDragOnRepositionNob;
  597. }
  598. bool Slider::IsDragged( void ) const
  599. {
  600. return _dragging;
  601. }
  602. //-----------------------------------------------------------------------------
  603. // Purpose: Respond to mouse presses. Trigger Record staring positon.
  604. //-----------------------------------------------------------------------------
  605. void Slider::OnMousePressed(MouseCode code)
  606. {
  607. int x,y;
  608. if (!IsEnabled())
  609. return;
  610. // input()->GetCursorPos(x,y);
  611. input()->GetCursorPosition( x, y );
  612. ScreenToLocal(x,y);
  613. RequestFocus();
  614. bool startdragging = false, bPostDragStartSignal = false;
  615. if ((x >= _nobPos[0]) && (x < _nobPos[1]))
  616. {
  617. startdragging = true;
  618. bPostDragStartSignal = true;
  619. }
  620. else
  621. {
  622. // we clicked elsewhere on the slider; move the nob to that position
  623. int min, max;
  624. GetRange(min, max);
  625. if ( m_bUseSubRange )
  626. {
  627. min = _subrange[ 0 ];
  628. max = _subrange[ 1 ];
  629. }
  630. // int wide = GetWide();
  631. int _x, _y, wide, tall;
  632. GetTrackRect( _x, _y, wide, tall );
  633. if ( wide > 0 )
  634. {
  635. float frange = ( float )( max - min );
  636. float clickFrac = clamp( ( float )( x - _x ) / (float)( wide - 1 ), 0.0f, 1.0f );
  637. float value = (float)min + clickFrac * frange;
  638. startdragging = IsDragOnRepositionNob();
  639. if ( startdragging )
  640. {
  641. _dragging = true; // Required when as
  642. SendSliderDragStartMessage();
  643. }
  644. SetValue( ( int )( value + 0.5f ) );
  645. }
  646. }
  647. if ( startdragging )
  648. {
  649. // drag the nob
  650. _dragging = true;
  651. input()->SetMouseCapture(GetVPanel());
  652. _nobDragStartPos[0] = _nobPos[0];
  653. _nobDragStartPos[1] = _nobPos[1];
  654. _dragStartPos[0] = x;
  655. _dragStartPos[1] = y;
  656. }
  657. if ( bPostDragStartSignal )
  658. SendSliderDragStartMessage();
  659. }
  660. //-----------------------------------------------------------------------------
  661. // Purpose: Just handle double presses like mouse presses
  662. //-----------------------------------------------------------------------------
  663. void Slider::OnMouseDoublePressed(MouseCode code)
  664. {
  665. OnMousePressed(code);
  666. }
  667. //-----------------------------------------------------------------------------
  668. // Purpose:
  669. //-----------------------------------------------------------------------------
  670. #ifdef _GAMECONSOLE
  671. void Slider::OnKeyCodePressed(KeyCode code)
  672. {
  673. switch ( GetBaseButtonCode( code ) )
  674. {
  675. case KEY_XBUTTON_LEFT:
  676. case KEY_XSTICK1_LEFT:
  677. case KEY_XSTICK2_LEFT:
  678. SetValue(GetValue() - 1);
  679. break;
  680. case KEY_XBUTTON_RIGHT:
  681. case KEY_XSTICK1_RIGHT:
  682. case KEY_XSTICK2_RIGHT:
  683. SetValue(GetValue() + 1);
  684. break;
  685. default:
  686. BaseClass::OnKeyCodePressed(code);
  687. break;
  688. }
  689. }
  690. #endif
  691. //-----------------------------------------------------------------------------
  692. // Purpose: Handle key presses
  693. //-----------------------------------------------------------------------------
  694. void Slider::OnKeyCodeTyped(KeyCode code)
  695. {
  696. switch (code)
  697. {
  698. // for now left and right arrows just open or close submenus if they are there.
  699. case KEY_LEFT:
  700. case KEY_DOWN:
  701. {
  702. int val = GetValue();
  703. SetValue(val-1);
  704. break;
  705. }
  706. case KEY_RIGHT:
  707. case KEY_UP:
  708. {
  709. int val = GetValue();
  710. SetValue(val+1);
  711. break;
  712. }
  713. case KEY_PAGEDOWN:
  714. {
  715. int min, max;
  716. GetRange(min, max);
  717. float range = (float) max-min;
  718. float pertick = range/m_nNumTicks;
  719. int val = GetValue();
  720. SetValue(val - (int) pertick);
  721. break;
  722. }
  723. case KEY_PAGEUP:
  724. {
  725. int min, max;
  726. GetRange(min, max);
  727. float range = (float) max-min;
  728. float pertick = range/m_nNumTicks;
  729. int val = GetValue();
  730. SetValue(val + (int) pertick);
  731. break;
  732. }
  733. case KEY_HOME:
  734. {
  735. int min, max;
  736. GetRange(min, max);
  737. SetValue(min);
  738. break;
  739. }
  740. case KEY_END:
  741. {
  742. int min, max;
  743. GetRange(min, max);
  744. SetValue(max);
  745. break;
  746. }
  747. default:
  748. BaseClass::OnKeyCodeTyped(code);
  749. break;
  750. }
  751. }
  752. //-----------------------------------------------------------------------------
  753. // Purpose: Stop dragging when the mouse is released.
  754. //-----------------------------------------------------------------------------
  755. void Slider::OnMouseReleased(MouseCode code)
  756. {
  757. if ( _dragging )
  758. {
  759. _dragging=false;
  760. input()->SetMouseCapture(0);
  761. SendSliderDragEndMessage();
  762. }
  763. }
  764. //-----------------------------------------------------------------------------
  765. // Purpose: Get the nob's position (the ends of each side of the nob)
  766. //-----------------------------------------------------------------------------
  767. void Slider::GetNobPos(int& min, int& max)
  768. {
  769. min=_nobPos[0];
  770. max=_nobPos[1];
  771. }
  772. //-----------------------------------------------------------------------------
  773. // Purpose:
  774. //-----------------------------------------------------------------------------
  775. void Slider::SetButtonOffset(int buttonOffset)
  776. {
  777. _buttonOffset=buttonOffset;
  778. }
  779. void Slider::SetThumbWidth( int width )
  780. {
  781. _nobSize = (float)width;
  782. }
  783. //-----------------------------------------------------------------------------
  784. // Purpose: Set the number of ticks that appear under the slider.
  785. //-----------------------------------------------------------------------------
  786. void Slider::SetNumTicks( int ticks )
  787. {
  788. m_nNumTicks = ticks;
  789. }