PolyUIScrollContainer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * PolyUIScrollContainer.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 "PolyUIScrollContainer.h"
  10. using namespace Polycode;
  11. UIScrollContainer::UIScrollContainer(ScreenEntity *scrolledEntity, bool hScroll, bool vScroll, Number width, Number height) : ScreenEntity() {
  12. scrolledEntity->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  13. scrolledEntity->rebuildTransformMatrix();
  14. Config *conf = CoreServices::getInstance()->getConfig();
  15. hasHScroll = hScroll;
  16. hasVScroll = vScroll;
  17. this->width = width;
  18. this->height = height;
  19. this->hitwidth = width;
  20. this->hitheight = height;
  21. Number uiScrollPanePadding = conf->getNumericValue("Polycode", "uiScrollPanePadding");
  22. defaultScrollSize = conf->getNumericValue("Polycode", "uiScrollDefaultSize");
  23. maskShape = new ScreenShape(ScreenShape::SHAPE_RECT, width, height);
  24. maskShape->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  25. addChild(maskShape);
  26. maskShape->setColor(1.0f,0,0,0.8f);
  27. scrollChild = scrolledEntity;
  28. addChild(scrollChild);
  29. scrollChild->setMask(maskShape);
  30. vScrollBar = new UIVScrollBar(defaultScrollSize, height, height / scrolledEntity->getHeight());
  31. addChild(vScrollBar);
  32. vScrollBar->setPosition(width+uiScrollPanePadding,0);
  33. vScrollBar->addEventListener(this, Event::CHANGE_EVENT);
  34. if(!vScroll)
  35. vScrollBar->enabled = false;
  36. hScrollBar = new UIHScrollBar(width, defaultScrollSize, width / scrolledEntity->getWidth());
  37. addChild(hScrollBar);
  38. hScrollBar->setPosition(0,height+uiScrollPanePadding);
  39. hScrollBar->addEventListener(this, Event::CHANGE_EVENT);
  40. if(!hScroll)
  41. hScrollBar->enabled = false;
  42. setContentSize(scrollChild->getWidth()*scrollChild->getScale().x, scrollChild->getHeight()*scrollChild->getScale().y);
  43. Resize(width, height);
  44. }
  45. void UIScrollContainer::Resize(int x, int y) {
  46. width = x;
  47. height = y;
  48. hitwidth = width;
  49. hitheight = height;
  50. maskShape->setShapeSize(x, y);
  51. vScrollBar->Resize(y);
  52. setContentSize(contentWidth, contentHeight);
  53. vScrollBar->setPositionY(0);
  54. }
  55. void UIScrollContainer::onMouseWheelUp(Number x, Number y) {
  56. if(vScrollBar->enabled)
  57. vScrollBar->scrollUpOneTick();
  58. }
  59. void UIScrollContainer::onMouseWheelDown(Number x, Number y) {
  60. if(vScrollBar->enabled)
  61. vScrollBar->scrollDownOneTick();
  62. }
  63. void UIScrollContainer::setContentSize(Number newContentWidth, Number newContentHeight) {
  64. contentHeight = newContentHeight;
  65. contentWidth = newContentWidth;
  66. vScrollBar->setHandleRatio(height / newContentHeight);
  67. hScrollBar->setHandleRatio(width / newContentWidth);
  68. if(hasVScroll) {
  69. if((height / newContentHeight) >= 1) {
  70. vScrollBar->enabled = false;
  71. } else {
  72. vScrollBar->enabled = true;
  73. }
  74. }
  75. if(hasHScroll) {
  76. if((width / newContentWidth) >= 1) {
  77. hScrollBar->enabled = false;
  78. } else {
  79. hScrollBar->enabled = true;
  80. }
  81. }
  82. }
  83. void UIScrollContainer::handleEvent(Event *event) {
  84. if(event->getDispatcher() == vScrollBar) {
  85. if(event->getEventCode() == Event::CHANGE_EVENT) {
  86. scrollChild->setPositionY(round(-((contentHeight-height) )*vScrollBar->getScrollValue()));
  87. }
  88. }
  89. if(event->getDispatcher() == hScrollBar) {
  90. if(event->getEventCode() == Event::CHANGE_EVENT) {
  91. scrollChild->setPositionX(round(-((contentWidth-width) )*hScrollBar->getScrollValue()));
  92. }
  93. }
  94. }
  95. UIScrollContainer::~UIScrollContainer() {
  96. }