2
0

PolyUIVScrollBar.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * PolyUIVScrollBar.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 3/25/10.
  6. * Copyright 2010 Ivan Safrin. All rights reserved.
  7. *
  8. */
  9. #include "PolyUIVScrollBar.h"
  10. using namespace Polycode;
  11. UIVScrollBar::UIVScrollBar(Number width, Number height, Number initialRatio) : ScreenEntity() {
  12. scrollValue = 0;
  13. Config *conf = CoreServices::getInstance()->getConfig();
  14. minHandleSize = conf->getNumericValue("Polycode", "uiScrollHandleMinSize");
  15. Number st = conf->getNumericValue("Polycode", "uiScrollBgSkinT");
  16. Number sr = conf->getNumericValue("Polycode", "uiScrollBgSkinR");
  17. Number sb = conf->getNumericValue("Polycode", "uiScrollBgSkinB");
  18. Number sl = conf->getNumericValue("Polycode", "uiScrollBgSkinL");
  19. padding = conf->getNumericValue("Polycode", "uiScrollBgSkinPadding");
  20. bgBox = new UIBox(conf->getStringValue("Polycode", "uiScrollBgSkin"),
  21. st,sr,sb,sl,
  22. width, height);
  23. addChild(bgBox);
  24. st = conf->getNumericValue("Polycode", "uiScrollHandleSkinT");
  25. sr = conf->getNumericValue("Polycode", "uiScrollHandleSkinR");
  26. sb = conf->getNumericValue("Polycode", "uiScrollHandleSkinB");
  27. sl = conf->getNumericValue("Polycode", "uiScrollHandleSkinL");
  28. if(initialRatio > 1)
  29. initialRatio = 1;
  30. scrollHandleHeight = height*initialRatio;
  31. if(scrollHandleHeight < minHandleSize)
  32. scrollHandleHeight = minHandleSize;
  33. handleBox = new UIBox(conf->getStringValue("Polycode", "uiScrollHandleSkin"),
  34. st,sr,sb,sl,
  35. width-(padding*2),scrollHandleHeight);
  36. handleBox->setPosition(padding, padding);
  37. addChild(handleBox);
  38. bgBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  39. handleBox->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  40. handleBox->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  41. handleBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  42. handleBox->blockMouseInput = true;
  43. dragRectHeight = height-(padding*2)-scrollHandleHeight;
  44. handleBox->setDragLimits(Rectangle(padding,padding,width-(padding*2)-(width-(padding*2)), dragRectHeight));
  45. lastPositionY = handleBox->getPosition().y;
  46. this->height = height;
  47. this->width = width;
  48. this->hitwidth = width;
  49. this->hitheight = height;
  50. }
  51. void UIVScrollBar::Resize(int newHeight) {
  52. bgBox->resizeBox(width, newHeight);
  53. this->height = newHeight;
  54. this->hitheight = newHeight;
  55. dragRectHeight = height-(padding*2)-scrollHandleHeight;
  56. handleBox->setDragLimits(Rectangle(padding,padding,width-(padding*2)-(width-(padding*2)), dragRectHeight));
  57. }
  58. void UIVScrollBar::Update() {
  59. if(lastPositionY != handleBox->getPosition().y) {
  60. lastPositionY = handleBox->getPosition().y;
  61. scrollValue = (lastPositionY-padding)/dragRectHeight;
  62. if(scrollValue < 0) scrollValue = 0;
  63. if(scrollValue > 1) scrollValue = 1;
  64. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  65. }
  66. }
  67. void UIVScrollBar::setHandleRatio(Number newRatio) {
  68. scrollHandleHeight = height*newRatio;
  69. if(scrollHandleHeight < minHandleSize)
  70. scrollHandleHeight = minHandleSize;
  71. dragRectHeight = height-(padding*2)-scrollHandleHeight;
  72. handleBox->resizeBox(handleBox->getWidth(), scrollHandleHeight);
  73. handleBox->setDragLimits(Rectangle(padding,padding,width-(padding*2)-(width-(padding*2)), dragRectHeight));
  74. }
  75. void UIVScrollBar::onMouseWheelUp(Number x, Number y) {
  76. scrollUpOneTick();
  77. }
  78. void UIVScrollBar::onMouseWheelDown(Number x, Number y) {
  79. scrollDownOneTick();
  80. }
  81. void UIVScrollBar::scrollUpOneTick() {
  82. Number newPos = handleBox->getPosition().y - 5;
  83. if(newPos < padding)
  84. newPos = padding;
  85. handleBox->setPositionY(newPos);
  86. }
  87. void UIVScrollBar::scrollDownOneTick() {
  88. Number newPos = handleBox->getPosition().y + 5;
  89. if(newPos > dragRectHeight)
  90. newPos = dragRectHeight;
  91. handleBox->setPositionY(newPos);
  92. }
  93. Number UIVScrollBar::getScrollValue() {
  94. return scrollValue;
  95. }
  96. void UIVScrollBar::handleEvent(Event *event) {
  97. if(event->getDispatcher() == bgBox) {
  98. InputEvent *inputEvent = (InputEvent*)event;
  99. switch(event->getEventCode()) {
  100. case InputEvent::EVENT_MOUSEDOWN:
  101. if(inputEvent->mousePosition.y < handleBox->getPosition().y) {
  102. Number newPos = handleBox->getPosition().y - scrollHandleHeight;
  103. if(newPos < padding)
  104. newPos = padding;
  105. handleBox->setPositionY(newPos);
  106. } else {
  107. Number newPos = handleBox->getPosition().y + scrollHandleHeight;
  108. if(newPos > dragRectHeight)
  109. newPos = dragRectHeight;
  110. handleBox->setPositionY(newPos);
  111. }
  112. break;
  113. }
  114. }
  115. if(event->getDispatcher() == handleBox) {
  116. InputEvent *inputEvent = (InputEvent*)event;
  117. switch(event->getEventCode()) {
  118. case InputEvent::EVENT_MOUSEUP:
  119. case InputEvent::EVENT_MOUSEUP_OUTSIDE:
  120. handleBox->stopDrag();
  121. break;
  122. case InputEvent::EVENT_MOUSEDOWN:
  123. handleBox->startDrag(inputEvent->mousePosition.x-handleBox->getPosition().x,inputEvent->mousePosition.y-handleBox->getPosition().y);
  124. break;
  125. }
  126. }
  127. }
  128. UIVScrollBar::~UIVScrollBar() {
  129. }