-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.js
185 lines (149 loc) · 5.88 KB
/
buttons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const STYLE_DEFAULT = {
background: '#eee',
color: '#111',
border_color: '',
border_width: 0,
border_radius: 5,
text_font: 'sans-serif',
text_size: 12,
}
const STYLE_HOVER = {
background: '#ccc',
color: '#111',
}
const STYLE_PRESSED = {
background: '#aaa',
color: '#000',
}
const STYLE_DISABLED = {
background: '#777',
color: '#333',
}
class Button {
#was_pressed = false;
#was_hovering = false;
#props = { };
#bounds = { minx: null, miny: null, maxx: null, maxy: null, centerx: null, centery: null };
#cstyles = { default: STYLE_DEFAULT, hover: STYLE_HOVER, pressed: STYLE_PRESSED, disabled: STYLE_DISABLED };
constructor(properties) {
this.#props = Object.assign({
content: '',
x: null, y: null,
w: null, h: null,
width: null,
height: null,
style_default: STYLE_DEFAULT,
style_hover: STYLE_HOVER,
style_pressed: STYLE_PRESSED,
style_disabled: STYLE_DISABLED,
on_mouse_enter: null,
on_mouse_exit: null,
on_press: null,
on_release: null,
align_x: -1,
align_y: -1,
enabled: true,
}, properties);
if ( this.#props.x === null || this.#props.y === null || this.#props.width === null || this.#props.height === null )
throw( '"x", "y", "width", and "height" must all be defined in the button properties!' );
Object.seal(this.#props);
this.#calculateStyles();
this.#calculateBounds();
}
#calculateBounds() {
const offset_x = (this.#props.align_x-1)*this.#props.width/2,
offset_y = (this.#props.align_y-1)*this.#props.height/2;
this.#bounds.minx = this.#props.x + offset_x,
this.#bounds.miny = this.#props.y + offset_y,
this.#bounds.maxx = this.#props.x + this.#props.width + offset_x,
this.#bounds.maxy = this.#props.y + this.#props.height + offset_y,
this.#bounds.centerx = this.#props.x + this.#props.width/2 + offset_x,
this.#bounds.centery = this.#props.y + this.#props.height/2 + offset_y;
}
#calculateStyles() {
this.#cstyles.default = Object.assign({}, STYLE_DEFAULT, this.#props.style_default);
this.#cstyles.hover = Object.assign({}, STYLE_DEFAULT, this.#props.style_default, this.#props.style_hover);
this.#cstyles.pressed = Object.assign({}, STYLE_DEFAULT, this.#props.style_default, this.#props.style_pressed);
this.#cstyles.disabled = Object.assign({}, STYLE_DEFAULT, this.#props.style_default, this.#props.style_disabled);
}
update(properties) {
try {
Object.assign(this.#props, properties);
} catch(e) {console.warn( `Encountered an unrecognized property! Original error: ${e.message}` )}
if ( 'x' in properties || 'y' in properties || 'width' in properties || 'height' in properties || 'align_x' in properties || 'align_y' in properties )
this.#calculateBounds();
if ( 'style_default' in properties || 'style_hover' in properties || 'style_pressed' in properties || 'style_disabled' in properties )
this.#calculateStyles();
}
/** Shorthand for .update({ x: <value>, y: <value>, ... }) */
place( x, y, width=null, height=null ) {
this.#props.x = x,
this.#props.y = y;
if ( width !== null ) this.#props.width = width;
if ( height !== null ) this.#props.height = height;
this.#calculateBounds();
}
/** Shorthand for .update({ style_<stylename>: <value> }) */
style( stylename, style ) {
if (!( 'style_'+stylename in this.#props ))
throw( `Style name must be either "default", "hover", "pressed", or "disabled". Received "${stylename}" instead.` );
this.#props['style_'+stylename] = style;
this.#calculateStyles();
}
/** Shorthand for .update({ content: <value> }) */
text( content ) { this.#props.content = content }
/** Shorthand for .update({ enabled: true }) */
enable() { this.#props.enabled = true }
/** Shorthand for .update({ enabled: false }) */
disable() { this.#props.enabled = false }
/**
* Returns whether the specified point (by default the mouse's position) is hovering over the button.
* @param {number} x (optional) x override.
* @param {number} y (optional) y override.
* @returns {boolean}
*/
isHovering(x=mouseX, y=mouseY) {
return x > this.#bounds.minx && x < this.#bounds.maxx && y > this.#bounds.miny && y < this.#bounds.maxy;
}
/**
* Returns whether the button is currently being pressed.
* @returns {boolean}
*/
isPressed() {
return this.isHovering() && mouseIsPressed;
}
#getCurrentStyle( hovering, pressed ) {
if ( !this.#props.enabled ) return this.#cstyles.disabled;
if ( pressed ) return this.#cstyles.pressed;
if ( hovering ) return this.#cstyles.hover;
return this.#cstyles.default;
}
/**
* Draws the button on the specified canvas, or the global canvas by default.
* @param context (optional) The p5 canvas to draw to.
*/
draw( context=globalThis ) {
const is_hovering = this.isHovering();
const is_pressed = mouseIsPressed && (is_hovering || this.#was_pressed);
const style = this.#getCurrentStyle( is_hovering, is_pressed );
if ( style.background ) context.fill( style.background );
else context.noFill();
if ( style.border_color && style.border_radius ) {
context.stroke( style.border_color );
context.strokeWeight( style.border_width );
} else context.noStroke();
context.rect( this.#bounds.minx, this.#bounds.miny, this.#props.width, this.#props.height, style.border_radius );
noStroke();
context.fill( style.color );
context.textAlign( CENTER, CENTER );
context.textSize( style.text_size );
context.textFont( style.text_font );
context.text( this.#props.content, this.#bounds.centerx, this.#bounds.centery );
if ( !this.#was_pressed && is_pressed && this.#props.on_press ) this.#props.on_press();
if ( this.#was_pressed && !is_pressed && this.#props.on_release ) this.#props.on_release();
if ( !this.#was_hovering && is_hovering && this.#props.on_mouse_enter ) this.#props.on_mouse_enter();
if ( this.#was_hovering && !is_hovering && this.#props.on_mouse_exit ) this.#props.on_mouse_exit();
this.#was_pressed = is_pressed;
this.#was_hovering = is_hovering;
}
}