void ScrollArea::finishUpdate(void) { mScrollbarVisible = (mTotalArea.height > mRenderView.getSize().y); if (mScrollbarVisible) { const float scrollRatio = 1.f - ((mTotalArea.height - mScrollTop - mRenderView.getSize().y) / (mTotalArea.height - mRenderView.getSize().y)); const float scrollbarLeft = mRenderView.getCenter().x + .5f * mRenderView.getSize().x - mScrollbarWidth; const float scrollbarHeight = mRenderView.getSize().y * mRenderView.getSize().y / mTotalArea.height; const float scrollbarTop = scrollRatio * (mRenderView.getSize().y - scrollbarHeight); sf::FloatRect scrollbarRect(mTotalArea.left + scrollbarLeft, mTotalArea.top + scrollbarTop, mScrollbarWidth, scrollbarHeight); if (mMouseDown) scrollAreaVertical(mMousePos.y - mLastMousePos.y); bool mouseOverScrollbar = scrollbarRect.contains(mMousePos); if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { if (mouseOverScrollbar) { mMouseDown = true; mLastMousePos = mMousePos; } } else { mMouseDown = false; } mScrollbarSprite.setScale(mScrollbarWidth / mScrollbarTexture.getSize().x, scrollbarHeight / mScrollbarTexture.getSize().y); mScrollbarSprite.setPosition(scrollbarRect.left, scrollbarRect.top); mScrollbarSprite.setColor(sf::Color(255U, 255U, 255U, (mouseOverScrollbar || mMouseDown) ? 224U : 160U)); } mContentsSprite.setTexture(mRenderTexture.getTexture()); }
void HierarchyPanel::displayScrollbars() { Rect container(position.x + position.width - 20, position.y + (ITEM_HEIGHT * 3), 20, position.height - (ITEM_HEIGHT * 4)); Rect scrollbarRect(container.x, container.y + scrollPos, container.width, 20); Gui::box(container, ""); Rect upRect(container.x, container.y - ITEM_HEIGHT, ITEM_HEIGHT, ITEM_HEIGHT); Gui::button(upRect, ""); if(Input::getMouseButton(0) == true && upRect.contains(Input::getMousePosition()) == true) { scrollPos -= 100 * Time::getDeltaTime(); } Rect downRect(container.x, container.y + container.height, ITEM_HEIGHT, ITEM_HEIGHT); Gui::button(downRect, ""); if(Input::getMouseButton(0) == true && downRect.contains(Input::getMousePosition()) == true) { scrollPos += 100 * Time::getDeltaTime(); } scrollbarRect.height = ((float)maxDisplay / (float)totalDisplay) * (float)container.height; //Gui::drawTexture(scrollbarRect, scrollbarTexture); //Gui::button(scrollbarRect, ""); Graphics::drawTexture(scrollbarRect, scrollbarTexture, Rect(0, 0, 1, 1), 5, 5, 5, 5); if(scrolling == false) { if(Input::getMouseButtonDown(0) == true && scrollbarRect.contains(Input::getMousePosition()) == true) { mousePos = Input::getMousePosition().y; scrolling = true; } } else { scrollPos += Input::getMousePosition().y - mousePos; mousePos = Input::getMousePosition().y; if(Input::getMouseButtonUp(0) == true) { scrolling = false; } } if(scrollPos + scrollbarRect.height > container.height) { scrollPos = container.height - scrollbarRect.height; } if(scrollPos < 0) { scrollPos = 0; } float percent = 0; if(scrollPos == 0) { percent = 0; } else { percent = scrollPos / (container.height - scrollbarRect.height); } //std::cout << percent << std::endl; startDisplay = (totalDisplay - maxDisplay) * percent; }