bool ScrollPanel::trapped(InterfaceEvent* event){ if(event->source == &scrollBar && scrollBar.visible){ //Handle scroll bar events if(event->type == InterfaceEvent::LeftPressed){ isScrolling = true; scrollBar.setBarDown(isScrolling); }else if(event->type == InterfaceEvent::LeftReleased){ isScrolling = false; scrollBar.setBarDown(isScrolling); }else if(event->type == InterfaceEvent::MouseWheelMoved){ if(scrollBar.visible){ yScroll -= (scrollArea.y / 20)*(*(int*)event->args); scrollChanged(); } } return true; }else if(event->type == InterfaceEvent::MouseWheelMoved && scrollBar.visible){ //If mouse wheel is scrolled anywhere in this panel apply scroll. yScroll -= (scrollArea.y / 20)*(*(int*)event->args); scrollChanged(); return true; } return false; }
/** * Redraw scroll image. */ void ScrollPanel::updateScrollView(){ //If a rendered scroll area texture exists, delete it. if(scrollTexture != 0){ delete scrollTexture; } //Calculate the y rectangle of scroll area to draw. int maxY = 0; for(unsigned int i = 0; i < interfaceElements.size(); i++){ if(interfaceElements.at(i)->visible){ if(interfaceElements.at(i)->rect.top + interfaceElements.at(i)->rect.height > maxY) maxY = interfaceElements.at(i)->rect.top + interfaceElements.at(i)->rect.height; } } scrollArea.y = maxY + rect.top + 3; //Setup scroll bar. scrollBar.visible = scrollArea.y > rect.height; if(!scrollBar.visible){ yScroll = 0; scrollArea.x = rect.width; }else{ scrollArea.x = rect.width - scrollBar.rect.width + 1; scrollBar.setSize(10, rect.height, ((float)rect.height / (float)scrollArea.y)*rect.height); scrollBar.setBarPosition(((float)(yScroll)/((float)scrollArea.y))*((float)rect.height)); } scrollChanged(); //Create and render scroll area image. scrollTexture = new sf::RenderTexture(); scrollTexture->create(scrollArea.x, scrollArea.y, true); scrollTexture->clear(sf::Color::Transparent); Panel::draw(*scrollTexture, sf::RenderStates::Default); scrollTexture->display(); scrollSprite.setTexture(scrollTexture->getTexture(), true); scrollSprite.setPosition(0, 0); scrollSprite.setTextureRect(sf::IntRect(0, yScroll, rect.width, rect.height)); }
InterfaceElement* ScrollPanel::collisionCheck(int x, int y){ if(!InterfaceElement::collisionCheck(x, y)) return 0; //Update scroll position if necessary. if(isScrolling){ yScroll = ((float)(y-rect.top-scrollBar.scrollBar.getSize().y/2)/((float)rect.height))*(float)scrollArea.y; scrollChanged(); } if(scrollBar.collisionCheck(x, y)){ return &scrollBar; } //Check if collision occurs with any panel elements. InterfaceElement* scrollViewCollision = Panel::collisionCheck(x, y + yScroll - rect.top); if(scrollViewCollision) return scrollViewCollision; return this; }
STSimpleEditView::STSimpleEditView(QGraphicsItem *parent) : QGraphicsObject(parent) { m_size=QSize(0,0); m_backgroundColor=QColor(255,255,255); m_textColor=QColor(0,0,0); //qSwap(m_backgroundColor, m_textColor); m_text=""; m_markPos=0; m_cursorPos=0; m_lineNavigationMode=false; m_nextActionPos=0; m_actions.clear(); m_layoutedText=""; m_layout=STFont::defaultFont()->layoutString(""); m_scroll=0; m_caretView=new STCaretView(this); m_caretView->setSize(QSizeF(2.f,12.f)); m_scrollBar=new STScrollBarView(this); m_scrollBar->setStyle(STScrollBarView::ClearBlack); setFlag(ItemIsFocusable); setFlag(ItemAcceptsInputMethod); setFlag(ItemClipsChildrenToShape); setCursor(QCursor(Qt::IBeamCursor)); setCacheMode(DeviceCoordinateCache); connect(m_scrollBar, SIGNAL(valueChanged()), this, SLOT(scrollChanged())); }