Skip to content

Commit 6dc63d8

Browse files
author
monkstone
committed
refactor slider library
1 parent 03c620c commit 6dc63d8

9 files changed

Lines changed: 643 additions & 9 deletions

File tree

library/slider/slider.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# Here's a little library for quickly hooking up in sketch sliders.
44
# Copyright (c) 2016 Martin Prout.
55

6-
java_import 'monkstone.slider.HorizontalSliderBar'
7-
java_import 'monkstone.slider.VerticalSliderBar'
6+
java_import 'monkstone.slider.CustomHorizontalSlider'
7+
java_import 'monkstone.slider.CustomVerticalSlider'
88

99
module Slider
1010
def self.slider(app:, x:, y:, name:, **opts)
1111
options = default.merge opts
1212
if options[:vertical]
13-
slider = VerticalSliderBar.new(
13+
slider = CustomVerticalSlider.new(
1414
app,
1515
x,
1616
y,
@@ -20,7 +20,7 @@ def self.slider(app:, x:, y:, name:, **opts)
2020
name
2121
)
2222
else
23-
slider = HorizontalSliderBar.new(
23+
slider = CustomHorizontalSlider.new(
2424
app,
2525
x,
2626
y,

src/monkstone/slider/HorizontalSliderBar.java renamed to src/monkstone/slider/CustomHorizontalSlider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import processing.core.PApplet;
2323
import processing.core.PConstants;
2424

25-
public class HorizontalSliderBar extends SliderBar {
25+
public class CustomHorizontalSlider extends SliderBar {
2626

2727
/**
2828
*
@@ -34,7 +34,7 @@ public class HorizontalSliderBar extends SliderBar {
3434
* @param endRange end range
3535
* @param label widget label/ID
3636
*/
37-
public HorizontalSliderBar(final PApplet outer, int x, int y, int length, float beginRange, float endRange, String label) {
37+
public CustomHorizontalSlider(final PApplet outer, int x, int y, int length, float beginRange, float endRange, String label) {
3838
this.applet = outer;
3939
this.scrollWheelHandler = (short delta) -> {
4040
changeWithWheel(delta);

src/monkstone/slider/VerticalSliderBar.java renamed to src/monkstone/slider/CustomVerticalSlider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import processing.core.PConstants;
2424
import static processing.core.PConstants.*;
2525

26-
public class VerticalSliderBar extends SliderBar {
26+
public class CustomVerticalSlider extends SliderBar {
2727

2828
/**
2929
*
@@ -35,7 +35,7 @@ public class VerticalSliderBar extends SliderBar {
3535
* @param endRange end range
3636
* @param label widget label/ID
3737
*/
38-
public VerticalSliderBar(final PApplet outer, int x, int y, int length, float beginRange, float endRange, String label) {
38+
public CustomVerticalSlider(final PApplet outer, int x, int y, int length, float beginRange, float endRange, String label) {
3939
this.applet = outer;
4040
this.scrollWheelHandler = (short delta) -> {
4141
changeWithWheel(delta);
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2016 Martin Prout
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* http://creativecommons.org/licenses/LGPL/2.1/
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
package monkstone.slider;
21+
22+
import processing.core.PApplet;
23+
import processing.core.PConstants;
24+
25+
public class SimpleHorizontalSlider extends SimpleSlider {
26+
27+
final int SPACING = 20;
28+
final int LEFT_SPC = SPACING * 2;
29+
final int RIGHT_SPC = SPACING * 4;
30+
31+
/**
32+
*
33+
* @param outer
34+
* @param beginRange start range
35+
* @param endRange end range
36+
* @param initial
37+
* @param count
38+
*/
39+
public SimpleHorizontalSlider(final PApplet outer, float beginRange, float endRange, float initial, int count) {
40+
this.applet = outer;
41+
setActive(true);
42+
pX = LEFT_SPC;
43+
pY = outer.height - SPACING - (count * SPACING);
44+
pW = outer.width - RIGHT_SPC;
45+
pH = 10;
46+
ID = Integer.toString(count + 1);
47+
limits(beginRange, endRange);
48+
setValue(initial);
49+
}
50+
51+
@Override
52+
boolean mouseOver() {
53+
return (applet.mouseX >= pX && applet.mouseX <= pX + pW && applet.mouseY >= pY && applet.mouseY <= pY + pH);
54+
}
55+
56+
private void setActive(boolean active) {
57+
if (active) {
58+
applet.registerMethod("dispose", this);
59+
applet.registerMethod("draw", this);
60+
} else {
61+
applet.unregisterMethod("draw", this);
62+
}
63+
}
64+
65+
@Override
66+
void displayText() {
67+
String lFormat = "%d";
68+
if (displayLabel) {
69+
applet.fill(labelColor);
70+
applet.textSize(labelSize);
71+
applet.textAlign(PConstants.BOTTOM);
72+
applet.text(Integer.toString((int) pValue), pX + pW / 2, pY + pH);
73+
}
74+
if (displayValue) {
75+
applet.textSize(numberSize);
76+
applet.fill(numbersColor);
77+
applet.textAlign(PConstants.LEFT);
78+
applet.text(String.format(lFormat, (int) vMin), pX, pY );
79+
applet.textAlign(PConstants.RIGHT);
80+
applet.text(String.format(lFormat, (int) vMax), pX + pW, pY );
81+
}
82+
}
83+
84+
@Override
85+
void drawGui() {
86+
if (backgroundVisible) {
87+
applet.stroke(sliderBack);
88+
applet.line(pX, pY + pH / 2, pX + pW, pY + pH / 2);
89+
}
90+
applet.noStroke();
91+
applet.fill(255);
92+
applet.ellipse(pX + pScaled, pY + pH / 2, 10, 10);
93+
}
94+
95+
/**
96+
*
97+
* @param value
98+
*/
99+
@Override
100+
public final void setValue(float value) {
101+
if (value > vMax) {
102+
value = vMax;
103+
}
104+
if (value < vMin) {
105+
value = vMin;
106+
}
107+
pValue = value;
108+
pScaled = map(pValue, vMin, vMax, 0, pW);
109+
}
110+
111+
@Override
112+
void checkKeyboard() {
113+
if (mouseOver()) {
114+
if (applet.mousePressed && applet.mouseButton == PConstants.LEFT) {
115+
pValue = constrainMap(applet.mouseX - pX, 0, pW, vMin, vMax);
116+
}
117+
if (applet.keyPressed && pressOnlyOnce) {
118+
if (applet.keyCode == PConstants.LEFT || applet.keyCode == PConstants.DOWN) {
119+
pValue--;
120+
}
121+
if (applet.keyCode == PConstants.RIGHT || applet.keyCode == PConstants.UP) {
122+
pValue++;
123+
}
124+
if (pValue > vMax) {
125+
pValue = vMax;
126+
} else {
127+
pValue = (pValue < vMin) ? vMin : pValue;
128+
}
129+
pressOnlyOnce = false;
130+
}
131+
deBounce(5);
132+
pScaled = map(pValue, vMin, vMax, 0, pW);
133+
}
134+
}
135+
136+
/**
137+
*
138+
* @return
139+
*/
140+
@Override
141+
public String toString() {
142+
String geomF = "SimpleHSliderBar.new(%d, %d, %d, %.2f, %.2f, \"%s\")";
143+
return String.format(geomF, pX, pY, pW, vMin, vMax, ID);
144+
}
145+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2016 Martin Prout
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* http://creativecommons.org/licenses/LGPL/2.1/
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
package monkstone.slider;
22+
23+
import processing.core.PApplet;
24+
25+
/**
26+
*
27+
* @author Martin Prout
28+
*/
29+
public abstract class SimpleSlider implements Slider {
30+
int MIN_BAR_WIDTH = 10;
31+
int MAX_BAR_WIDTH = 30;
32+
int sliderBack = 0xff909090;
33+
int sliderFill = 0xff696969;
34+
int borderColor = 0;
35+
int numbersColor = 0;
36+
int labelColor = 0;
37+
int externalBorder = 0;
38+
boolean backgroundVisible = true;
39+
int labelSize = 18;
40+
int numberSize = 14;
41+
boolean displayLabel = false;
42+
boolean displayValue = true;
43+
int pX;
44+
int pY;
45+
int pW;
46+
int pH;
47+
float pScaled = 0;
48+
float pValue = 0;
49+
String ID;
50+
boolean pressOnlyOnce = true;
51+
int deb = 0;
52+
short wheelCount = 0;
53+
float vMin = 0;
54+
float vMax = 15;
55+
PApplet applet;
56+
57+
abstract void displayText();
58+
59+
abstract void drawGui();
60+
61+
62+
@Override
63+
public void draw() {
64+
applet.pushStyle();
65+
applet.noStroke();
66+
drawGui();
67+
displayText();
68+
applet.popStyle();
69+
change();
70+
}
71+
72+
/**
73+
*
74+
*/
75+
76+
@Override
77+
public void showLabel() {
78+
displayLabel = true;
79+
}
80+
81+
/**
82+
*
83+
*/
84+
85+
@Override
86+
public void hideLabel() {
87+
displayLabel = false;
88+
}
89+
90+
/**
91+
*
92+
*/
93+
94+
@Override
95+
public void showNumbers() {
96+
displayValue = true;
97+
}
98+
99+
/**
100+
*
101+
*/
102+
@Override
103+
public void hideNumbers() {
104+
displayValue = false;
105+
}
106+
107+
void change() {
108+
checkKeyboard();
109+
}
110+
111+
/**
112+
*
113+
*/
114+
115+
@Override
116+
public void hideBackground() {
117+
backgroundVisible = false;
118+
}
119+
120+
/**
121+
*
122+
* @return
123+
*/
124+
125+
@Override
126+
public float readValue() {
127+
return pValue;
128+
}
129+
130+
abstract boolean mouseOver();
131+
132+
protected float map(float val, float begIn, float endIn, float beginOut, float endOut) {
133+
return (beginOut + (endOut - beginOut) * ((val - begIn) / (endIn - begIn)));
134+
}
135+
136+
final void limits(float iv, float fv) {
137+
vMin = iv;
138+
vMax = fv;
139+
setValue(iv);
140+
}
141+
142+
/**
143+
*
144+
* @param s
145+
*/
146+
147+
@Override
148+
public void labelSize(int s) {
149+
labelSize = (s < 4) ? 4 : s;
150+
}
151+
152+
void deBounce(int n) {
153+
if (pressOnlyOnce) {
154+
} else if (deb++ > n) {
155+
deb = 0;
156+
pressOnlyOnce = true;
157+
}
158+
}
159+
160+
abstract void checkKeyboard();
161+
162+
163+
protected int constrainMap(double val, double begIn, double endIn, double beginOut, double endOut) {
164+
double max = Math.max(begIn, endIn);
165+
double min = Math.min(begIn, endIn);
166+
if (val < min) {
167+
val = min;
168+
}
169+
if (val > max) {
170+
val = max;
171+
}
172+
return (int) ((beginOut + (endOut - beginOut) * ((val - begIn) / (endIn - begIn))));
173+
}
174+
175+
private void setActive(boolean active) {
176+
if (active) {
177+
applet.registerMethod("dispose", this);
178+
applet.registerMethod("draw", this);
179+
} else {
180+
applet.unregisterMethod("draw", this);
181+
}
182+
}
183+
184+
185+
@Override
186+
public void dispose() {
187+
setActive(false);
188+
}
189+
}

0 commit comments

Comments
 (0)