-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathEditorButton.java
More file actions
266 lines (196 loc) · 6.76 KB
/
EditorButton.java
File metadata and controls
266 lines (196 loc) · 6.76 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015-19 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.app.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import processing.app.Mode;
import processing.data.StringDict;
abstract public class EditorButton extends JComponent
implements MouseListener, MouseMotionListener, ActionListener {
static public final int DIM = Toolkit.zoom(36);
/**
* The lowercase short name/path used to load its SVG/PNG image data.
* This will usually be something like /lib/toolbar/run (to pull in
* an icon from the Processing /lib folder) or if it's a relative path,
* it will be sourced from the mode folder.
*/
protected String name;
/** Button's description. */
protected String title;
/** Description of alternate behavior when shift is down. */
protected String titleShift;
/** Description of alternate behavior when alt is down. */
protected String titleAlt;
protected boolean pressed;
protected boolean selected;
protected boolean rollover;
protected boolean shift;
protected Image enabledImage;
protected Image disabledImage;
protected Image selectedImage;
protected Image rolloverImage;
protected Image pressedImage;
protected Image gradient;
protected EditorToolbar toolbar;
public EditorButton(EditorToolbar parent, String name, String title) {
this(parent, name, title, title, title);
}
public EditorButton(EditorToolbar parent, String name,
String title, String titleShift) {
this(parent, name, title, titleShift, title);
}
public EditorButton(EditorToolbar parent, String name,
String title, String titleShift, String titleAlt) {
this.name = name;
this.toolbar = parent;
this.title = title;
this.titleShift = titleShift;
this.titleAlt = titleAlt;
updateTheme();
addMouseListener(this);
addMouseMotionListener(this);
}
protected Image renderImage(String state) {
Mode mode = toolbar.mode;
String xmlOrig = mode.loadString(name + ".svg");
// If no SVG available, load image data from PNG files
if (xmlOrig == null) {
return mode.loadImageX(name + "-" + state);
}
StringDict replacements = new StringDict(new String[][] {
{ "#fff", Theme.get("toolbar.button." + state + ".field") },
{ "#ff5757", Theme.get("toolbar.button." + state + ".glyph") },
{ "silver", Theme.get("toolbar.button." + state + ".stroke") }
});
return Toolkit.svgToImageMult(xmlOrig, DIM, DIM, replacements);
}
public void updateTheme() {
disabledImage = renderImage("disabled");
enabledImage = renderImage("enabled");
selectedImage = renderImage("selected");
pressedImage = renderImage("pressed");
rolloverImage = renderImage("rollover");
if (disabledImage == null) {
disabledImage = enabledImage;
}
if (selectedImage == null) {
selectedImage = enabledImage;
}
if (pressedImage == null) {
pressedImage = enabledImage; // could be selected image
}
if (rolloverImage == null) {
rolloverImage = enabledImage; // could be pressed image
}
}
@Override
public void paintComponent(Graphics g) {
Image image = enabledImage;
if (!isEnabled()) {
image = disabledImage;
} else if (selected) {
image = selectedImage;
} else if (pressed) {
image = pressedImage;
} else if (rollover) {
image = rolloverImage;
}
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int dim = getSize().width; // width == height
if (gradient != null) {
//g.drawImage(gradient, 0, 0, DIM, DIM, this);
g.drawImage(gradient, 0, 0, dim, dim, this);
}
//g.drawImage(image, 0, 0, DIM, DIM, this);
g.drawImage(image, 0, 0, dim, dim, this);
}
@Override
public void mouseClicked(MouseEvent e) { }
public boolean isShiftDown() {
return shift;
}
@Override
public void mousePressed(MouseEvent e) {
// Using mousePressed() (or mouseReleased()) because mouseClicked()
// won't be fired if the user nudges the mouse while clicking.
// https://github.com/processing/processing/issues/3529
setPressed(true);
shift = e.isShiftDown();
// It looks like ActionEvent expects old-style modifiers,
// so the e.getModifiers() call is actually correct.
// There's an open JDK bug for this, but it remains unresolved:
// https://bugs.openjdk.java.net/browse/JDK-8186024
// Mostly this is only used for shift, but some modes also make use of
// alt as a way to control debug stepping and whatnot. [fry 210813]
// https://github.com/processing/processing4/issues/67
//noinspection deprecation
actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
null, e.getModifiers()));
}
@Override
public void mouseReleased(MouseEvent e) {
setPressed(false);
}
public void setPressed(boolean pressed) {
if (isEnabled()) {
this.pressed = pressed;
repaint();
}
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getRolloverText(InputEvent e) {
if (e.isShiftDown()) {
return titleShift;
} else if (e.isAltDown()) {
return titleAlt;
}
return title;
}
@Override
public void mouseEntered(MouseEvent e) {
toolbar.setRollover(this, e);
rollover = true;
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
toolbar.setRollover(null, e);
rollover = false;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) { }
@Override
public void mouseMoved(MouseEvent e) { }
abstract public void actionPerformed(ActionEvent e);
@Override
public Dimension getPreferredSize() {
return new Dimension(DIM, DIM);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}