PolyUIHSlider.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * PolyUIHSlider.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 8/7/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyUIHSlider.h"
  10. using namespace Polycode;
  11. UIHSlider::UIHSlider(Font *font, float start, float end, float width) {
  12. bgRect = new ScreenShape(ScreenShape::SHAPE_RECT, width,8,0,0);
  13. bgRect->setPosition(0,6);
  14. bgRect->setColor(0.11f, 0.11f, 0.11f, 1.0f);
  15. // bgRect->strokeEnabled = true;
  16. // bgRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  17. addChild(bgRect);
  18. sliderWidth = width-10;
  19. sliderValue = start;
  20. startValue = start;
  21. endValue = end;
  22. shadowRect = new ScreenShape(ScreenShape::SHAPE_RECT,10,18,0,0);
  23. shadowRect->setColor(0.0f, 0.0f, 0.0f, 0.2f);
  24. shadowRect->setPosition(2, 3);
  25. addChild(shadowRect);
  26. gripRect = new ScreenShape(ScreenShape::SHAPE_RECT, 10,18,0,0);
  27. gripRect->setColor(0.13f, 0.13f, 0.13f, 1.0f);
  28. gripRect->strokeEnabled = true;
  29. gripRect->lineSmooth = false;
  30. gripRect->setStrokeColor(1.0f, 1.0f, 1.0f, 0.1f);
  31. addChild(gripRect);
  32. gripRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  33. gripRect->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  34. gripRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  35. gripRect->setDragLimits(Rectangle(0,0,width-10,0));
  36. shadowRect->setDragLimits(Rectangle(2,3,width-10,0));
  37. gripPos = 0;
  38. }
  39. UIHSlider::~UIHSlider() {
  40. }
  41. ScreenShape *UIHSlider::getBgRect() {
  42. return bgRect;
  43. }
  44. void UIHSlider::setSliderValue(float val) {
  45. if(val >= startValue && val <= endValue) {
  46. gripRect->getPosition().x = sliderWidth * ((val-startValue)/(endValue-startValue));
  47. shadowRect->getPosition().x = gripRect->getPosition().x;
  48. }
  49. }
  50. float UIHSlider::getSliderValue() {
  51. return sliderValue;
  52. }
  53. void UIHSlider::handleEvent(Event *event) {
  54. if(event->getDispatcher() == gripRect) {
  55. InputEvent *inputEvent = (InputEvent*)event;
  56. switch(event->getEventCode()) {
  57. case InputEvent::EVENT_MOUSEDOWN:
  58. gripRect->startDrag(inputEvent->mousePosition.x-gripRect->getPosition().x,inputEvent->mousePosition.y-gripRect->getPosition().y);
  59. shadowRect->startDrag(inputEvent->mousePosition.x-2-gripRect->getPosition().x,inputEvent->mousePosition.y-3-gripRect->getPosition().y);
  60. break;
  61. case InputEvent::EVENT_MOUSEUP:
  62. case InputEvent::EVENT_MOUSEUP_OUTSIDE:
  63. gripRect->stopDrag();
  64. shadowRect->stopDrag();
  65. break;
  66. }
  67. }
  68. }
  69. void UIHSlider::Update() {
  70. if(gripRect->getPosition().x != gripPos) {
  71. gripPos = gripRect->getPosition().x;
  72. sliderValue = startValue+((endValue - startValue) * (gripPos/sliderWidth));
  73. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  74. }
  75. }