Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92321301
HScrollbar.pde
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Nov 19, 09:43
Size
3 KB
Mime Type
text/x-c++
Expires
Thu, Nov 21, 09:43 (1 d, 22 h)
Engine
blob
Format
Raw Data
Handle
22422992
Attached To
rBAFOURPROJECT InfoVisuGit
HScrollbar.pde
View Options
class HScrollbar {
float barWidth; //Bar's width in pixels
float barHeight; //Bar's height in pixels
float xPosition; //Bar's x position in pixels
float yPosition; //Bar's y position in pixels
float sliderPosition, newSliderPosition; //Position of slider
float sliderPositionMin, sliderPositionMax; //Max and min values of slider
boolean mouseOver; //Is the mouse over the slider?
int mousePressedState = 0; // 0 = not pressed, 1 = rising edge, 2 = pressed
boolean locked; //Is the mouse clicking and dragging the slider now?
/**
* @brief Creates a new horizontal scrollbar
*
* @param x The x position of the top left corner of the bar in pixels
* @param y The y position of the top left corner of the bar in pixels
* @param w The width of the bar in pixels
* @param h The height of the bar in pixels
*/
HScrollbar (float x, float y, float w, float h) {
barWidth = w;
barHeight = h;
xPosition = x;
yPosition = y;
sliderPosition = xPosition + barWidth/2 - barHeight/2;
newSliderPosition = sliderPosition;
sliderPositionMin = xPosition;
sliderPositionMax = xPosition + barWidth - barHeight;
}
/**
* @brief Updates the state of the scrollbar according to the mouse movement
*/
void update() {
//detects rising edges of mouse (when is being clicked)
if(mousePressed){ //mouse is currently pressed, so
if (mousePressedState == 0){
mousePressedState = 1; //if it wasn't at last frame, it's a rising edge (=1)
} else {
mousePressedState = 2; //else it's not a rising edge (=2)
}
} else {
mousePressedState = 0;
}
if (isMouseOver()) {
mouseOver = true;
} else {
mouseOver = false;
}
if (mousePressedState == 1 && mouseOver) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newSliderPosition = constrain(mouseX - barHeight/2, sliderPositionMin, sliderPositionMax);
}
if (abs(newSliderPosition - sliderPosition) > 1) {
sliderPosition = sliderPosition + (newSliderPosition - sliderPosition);
}
}
/**
* @brief Clamps the value into the interval
*
* @param val The value to be clamped
* @param minVal Smallest value possible
* @param maxVal Largest value possible
*
* @return val clamped into the interval [minVal, maxVal]
*/
float constrain(float val, float minVal, float maxVal) {
return min(max(val, minVal), maxVal);
}
/**
* @brief Gets whether the mouse is hovering the scrollbar
*
* @return Whether the mouse is hovering the scrollbar
*/
boolean isMouseOver() {
if (mouseX > xPosition && mouseX < xPosition+barWidth &&
mouseY > yPosition && mouseY < yPosition+barHeight) {
return true;
} else {
return false;
}
}
/**
* @brief Draws the scrollbar in its current state
*/
void display() {
noStroke();
fill(204);
rect(xPosition, yPosition, barWidth, barHeight);
if (mouseOver || locked) {
fill(0, 0, 0);
} else {
fill(102, 102, 102);
}
rect(sliderPosition, yPosition, barHeight, barHeight);
}
/**
* @brief Gets the slider position
*
* @return The slider position in the interval [0,1]
* corresponding to [leftmost position, rightmost position]
*/
float getPos() {
return (sliderPosition - xPosition)/(barWidth - barHeight);
}
}
Event Timeline
Log In to Comment