Here’s another concept for your consideration. Arrows with min/max/init values are used to control direction of animation. Could probably use keyboard arrows if you didn’t want to use separate classes.
color BLUE = color(64, 124, 188);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
color GREEN = color(32, 175, 47);
ForwardArrow _fwdArrw;
BackArrow _backArrw;
ValueField _valueFld;
final int _txtSize = 22;
final int _initValue = 20;
final int _maxValue = 480;
final int _minValue = 0;
int x = 0;
int y = 0;
int value = _initValue;
class ValueField {
float x, y, w, h;
String title;
color fldColor;
color txtColor;
// Constructor
ValueField(int xpos, int ypos, float wt, float ht, String valueStr, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = valueStr;
fldColor = background;
txtColor = foreground;
}
void display(int val) {
// **** Value Field **** //
fill(fldColor); // erase old value
rect(x, y, w, h);
fill(txtColor); // text color
textSize(_txtSize);
textAlign(CENTER);
text(str(val), x, y, w, h);
// **** Animation Rectangle **** //
fill(255);
rect(val, 100, 20, 20);
}
}
class ForwardArrow {
float x, y, w, h;
color arrwColor;
// Constructor
ForwardArrow(int xpos, int ypos, float wt, float ht, color background) {
x = xpos;
y = ypos;
w = wt;
h = ht;
arrwColor = background;
}
void display() {
fill(arrwColor); // arrow color
noStroke();
triangle(x, y, x, y + h, x + w, y + h/2 );
}
}
class BackArrow {
float x, y, w, h;
color arrwColor;
// Constructor
BackArrow(int xpos, int ypos, float wt, float ht, color background) {
x = xpos;
y = ypos;
w = wt;
h = ht;
arrwColor = background;
}
void display() {
fill(arrwColor); // arrow color
noStroke();
triangle(x, y + h/2, x + w, y, x + w, y + h );
}
}
void setup() {
size(500, 250);
background(BLUE);
_backArrw = new BackArrow(300, 40, 30, 30, GREEN);
_fwdArrw = new ForwardArrow(340, 40, 30, 30, GREEN);
_valueFld = new ValueField(380, 40, 50, 30, str(_initValue), WHITE, BLACK);
}
void draw() {
background(BLUE);
_valueFld.display(value);
_fwdArrw.display();
_backArrw.display();
// FwdArrw Long Press
if ((mouseX >= _fwdArrw.x) && (mouseX <= _fwdArrw.x + _fwdArrw.w) && (mouseY >= _fwdArrw.y) && (mouseY <= _fwdArrw.y + _fwdArrw.h)) {
if (mousePressed == true) {
value++;
if (value > _maxValue) {
value = _maxValue;
}
_valueFld.display(value);
}
}
// BackArrw Long Press
if ((mouseX >= _backArrw.x) && (mouseX <= _backArrw.x + _backArrw.w) && (mouseY >= _backArrw.y) && (mouseY <= _backArrw.y + _backArrw.h)) {
if (mousePressed == true) {
value--;
if (value < _minValue) {
value = _minValue;
}
_valueFld.display(value);
}
}
}
Another example of the same principle using keyboard arrows:
final int _initX = 20;
final int _minX = 0;
final int _maxX = 480;
final int _initY = 100;
final int _minY = 0;
final int _maxY = 180;
int x = _initX;
int y = _initY;
int xSpeed = 3;
int ySpeed = 3;
void setup(){
size(500,200);
}
void draw(){
background(0);
fill(255);
rect(x, y, 20, 20);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == RIGHT){
x += xSpeed;
if (x > _maxX) {
x = _maxX;
}
println("x = " + x);
}
if (keyCode == LEFT){
x -= xSpeed;
if (x < _minX) {
x = _minX;
}
println("x = " + x);
}
if (keyCode == UP){
y -= ySpeed;
if (y < _minY) {
y = _minY;
}
println("y = " + y);
}
if (keyCode == DOWN){
y += ySpeed;
if (y > _maxY) {
y = _maxY;
}
println("y = " + y);
}
}
}