void ScrollBar::onPress(fw::Mouse::Button button,fm::vec2 p) { if (button == fw::Mouse::Left) { if (!inDragArea(p)) { fm::vec2 s = getSize(); fm::vec2 sprS = s * getScrollSize(); if (getScrollDirection() == ScrollHorizontal) { float inp = p.x - getPosition().x - sprS.w/2; setScrollState(inp / (s.w - sprS.w)); } else { float inp = p.y - getPosition().y - sprS.h/2; setScrollState(1 - inp / (s.h - sprS.h)); } setupDraws(); } m_grabState = getScrollState(); m_grabbed = true; m_grabp = p; } }
void ScrollBar::onMouseMove(fm::vec2 p,fm::vec2 /* prevP */ ) { if (m_grabbed) { fm::vec2 s = getSize(); float state = 0; if (getScrollDirection() == ScrollHorizontal) { state = m_grabState - (m_grabp.x - p.x) / (s.w - s.w * getScrollSize()); } else { state = m_grabState - (p.y - m_grabp.y) / (s.h - s.h * getScrollSize()); } setScrollState(state); setupDraws(); } }
void ScrollBar::setupDraws() { fm::vec3 p = getPosition(); fm::vec2 s = getSize(); float scrollSize = getScrollSize(); fm::vec2 sprS = s * scrollSize; float scrollState = getScrollState(); fm::vec2 pts[12]; fm::vec2 tpt[] = {fm::vec2(0,0),fm::vec2(1,0),fm::vec2(1,1),fm::vec2(0,0),fm::vec2(1,1),fm::vec2(0,1)}; auto mixF = [&](float a,float b,float s,bool fst) -> float { if (a > b) return fst ? a : b; return a * (1-s) + b * s; }; if (getScrollDirection() == ScrollHorizontal) { m_handle.setPosition(p + fm::vec2i((s.w-sprS.w)*scrollState,0)); m_handle.setSize(fm::vec2i(sprS.w,s.h)); C(6) pts[i+0] = fm::vec2(mixF(p.x + sprS.w/2,m_handle.getPosition().x,tpt[i].x,false), p.y + s.h/2 - 2 + 4 * tpt[i].y); C(6) pts[i+6] = fm::vec2(mixF(m_handle.getPosition().x + m_handle.getSize().w, p.x + s.w - sprS.w/2, tpt[i].x,true), p.y + s.h/2 - 2 + 4 * tpt[i].y); } else { m_handle.setPosition(p + fm::vec2i(0,(s.h-sprS.h)*(1 - scrollState))); m_handle.setSize(fm::vec2(s.w,sprS.h)); C(6) pts[i+0] = fm::vec2(p.x + s.w/2 - 2 + 4 * tpt[i].x, mixF(p.y + sprS.h/2,m_handle.getPosition().y,tpt[i].y,false)); C(6) pts[i+6] = fm::vec2(p.x + s.w/2 - 2 + 4 * tpt[i].x, mixF(m_handle.getPosition().y + m_handle.getSize().h, p.y + s.h - sprS.h/2, tpt[i].y,true)); } m_railDraw.positions = pts; }
/////////////////////////////////////////////////////////// // // Calculate the rectancle to use to draw the image // /////////////////////////////////////////////////////////// void view::updateImageRect(imbxInt32 centerPointX, imbxInt32 centerPointY) { imbxInt32 tempCenterPointX = 0; imbxInt32 tempCenterPointY = 0; getCenterPoint(&tempCenterPointX, &tempCenterPointY); if(centerPointX < 0) centerPointX = tempCenterPointX; if(centerPointY < 0) centerPointY = tempCenterPointY; imbxInt32 leftPosition, topPosition, rightPosition, bottomPosition; leftPosition= topPosition= rightPosition= bottomPosition = 0; // Get the window's size /////////////////////////////////////////////////////////// imbxUint32 windowSizeX = 0; imbxUint32 windowSizeY = 0; getWindowSize(&windowSizeX, &windowSizeY); // Get the scroll size /////////////////////////////////////////////////////////// imbxUint32 scrollSizeX = 0; imbxUint32 scrollSizeY = 0; getScrollSize(&scrollSizeX, &scrollSizeY); // Get the scroll position /////////////////////////////////////////////////////////// imbxInt32 scrollPosX = 0; imbxInt32 scrollPosY = 0; getScrollPosition(&scrollPosX, &scrollPosY); // For now, the new scroll size and position are the same // as the old ones /////////////////////////////////////////////////////////// imbxUint32 newScrollSizeX=scrollSizeX; imbxUint32 newScrollSizeY=scrollSizeY; if(m_originalImage != 0) { imbxUint32 imageSizeX(0), imageSizeY(0); m_originalImage->getSize(&imageSizeX, &imageSizeY); // Retrieve the screen's resolution /////////////////////////////////////////////////////////// imbxUint32 screenHorzDPI, screenVertDPI; screenHorzDPI=screenVertDPI=75; getScreenDPI(&screenHorzDPI, &screenVertDPI); // Get the image's size (in mms and pixels) /////////////////////////////////////////////////////////// double imageSizeMmX = 0; double imageSizeMmY = 0; m_originalImage->getSizeMm(&imageSizeMmX, &imageSizeMmY); if(imageSizeMmX == 0) { imageSizeMmX = (double)imageSizeX * 25.4 / (double)screenHorzDPI; } if(imageSizeMmY == 0) { imageSizeMmY = (double)imageSizeY * 25.4 / (double)screenVertDPI; } // Calculate the area occupied by the image, in screen's // pixels /////////////////////////////////////////////////////////// imbxUint32 displayAreaWidth=(imbxUint32)((double)imageSizeMmX*m_zoom*(double)screenHorzDPI/25.4+0.5); imbxUint32 displayAreaHeight=(imbxUint32)((double)imageSizeMmY*m_zoom*(double)screenVertDPI/25.4+0.5); if(displayAreaWidth>windowSizeX) { rightPosition = displayAreaWidth; } else { leftPosition = (windowSizeX-displayAreaWidth)>>1; rightPosition = leftPosition+displayAreaWidth; } if(displayAreaHeight>windowSizeY) { bottomPosition = displayAreaHeight; } else { topPosition = (windowSizeY-displayAreaHeight)>>1; bottomPosition = topPosition+displayAreaHeight; } newScrollSizeX = displayAreaWidth; newScrollSizeY = displayAreaHeight; }