Skip to content

Commit b87a670

Browse files
committed
more work on header/toolbar/etc
1 parent 19b6778 commit b87a670

File tree

6 files changed

+279
-42
lines changed

6 files changed

+279
-42
lines changed

app/src/processing/app/Editor.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Timer;
3939

4040
import javax.swing.*;
41+
import javax.swing.border.Border;
42+
import javax.swing.border.EmptyBorder;
4143
import javax.swing.event.*;
4244
import javax.swing.plaf.basic.BasicSplitPaneDivider;
4345
import javax.swing.plaf.basic.BasicSplitPaneUI;
@@ -172,7 +174,9 @@ public void windowDeactivated(WindowEvent e) {
172174

173175
buildMenuBar();
174176

175-
backgroundGradient = Toolkit.getLibImage("vertical-gradient.png");
177+
/*
178+
//backgroundGradient = Toolkit.getLibImage("vertical-gradient.png");
179+
backgroundGradient = mode.getGradient("editor", 400, 400);
176180
JPanel contentPain = new JPanel() {
177181
@Override
178182
public void paintComponent(Graphics g) {
@@ -183,8 +187,10 @@ public void paintComponent(Graphics g) {
183187
// g.fillRect(0, 0, dim.width, dim.height);
184188
}
185189
};
190+
*/
186191
//contentPain.setBorder(new EmptyBorder(0, 0, 0, 0));
187192
//System.out.println(contentPain.getBorder());
193+
JPanel contentPain = new JPanel();
188194

189195
// JFrame f = new JFrame();
190196
// f.setContentPane(new JPanel() {
@@ -207,6 +213,7 @@ public void paintComponent(Graphics g) {
207213
// pain.setOpaque(false);
208214
// pain.setLayout(new BorderLayout());
209215
// contentPain.add(pain, BorderLayout.CENTER);
216+
// contentPain.setBorder(new EmptyBorder(10, 10, 10, 10));
210217

211218
Box box = Box.createVerticalBox();
212219
Box upper = Box.createVerticalBox();
@@ -257,21 +264,20 @@ public void paintComponent(Graphics g) {
257264
// remove any ugly borders added by PLAFs
258265
splitPane.setBorder(null);
259266
// necessary to let the gradient show through
260-
splitPane.setOpaque(false);
261-
267+
// splitPane.setOpaque(false);
268+
269+
// remove an ugly border around anything in a SplitPane !$*&!%
270+
UIManager.getDefaults().put("SplitPane.border", BorderFactory.createEmptyBorder());
271+
// override the look of the SplitPane so that it's identical across OSes
262272
splitPane.setUI(new BasicSplitPaneUI() {
263273
public BasicSplitPaneDivider createDefaultDivider() {
264274
return new BasicSplitPaneDivider(this) {
265275
final Color dividerColor = mode.getColor("divider.color"); //new Color(204, 204, 204);
266276
final Color dotColor = mode.getColor("divider.dot.color"); //new Color(80, 80, 80);
267277
int dotSize = mode.getInteger("divider.dot.diameter"); //3;
268278

269-
//public void setBorder(Border b) { }
270-
271279
@Override
272280
public void paint(Graphics g) {
273-
//Graphics2D g2 = Toolkit.prepareGraphics(g);
274-
//Toolkit.prepareGraphics(g);
275281
Graphics2D g2 = (Graphics2D) g;
276282
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
277283
RenderingHints.VALUE_ANTIALIAS_ON);
@@ -284,10 +290,7 @@ public void paint(Graphics g) {
284290
g.setColor(dotColor);
285291
int x = w/2 - dotSize/2;
286292
int y = h/2 - dotSize/2;
287-
//g2.fillOval(x, y, width, height);
288293
g.fillOval(x, y, dotSize, dotSize);
289-
290-
super.paint(g);
291294
}
292295
};
293296
}

app/src/processing/app/EditorToolbar.java

Lines changed: 150 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,54 @@
2323

2424
package processing.app;
2525

26-
import java.awt.Component;
26+
import java.awt.*;
2727
import java.awt.event.ActionEvent;
28+
import java.awt.image.BufferedImage;
29+
import java.awt.image.WritableRaster;
30+
import java.util.Arrays;
2831

2932
import javax.swing.Box;
3033
import javax.swing.JLabel;
3134
import javax.swing.JPanel;
3235

36+
import processing.core.PApplet;
37+
3338

3439
/**
3540
* Run/Stop button plus Mode selection
3641
*/
3742
abstract public class EditorToolbar extends JPanel {
43+
static final int HIGH = 80;
44+
3845
protected Editor editor;
3946
protected Base base;
4047
protected Mode mode;
4148

4249
protected EditorButton runButton;
4350
protected EditorButton stopButton;
51+
protected EditorButton currentButton;
4452

4553
protected Box box;
4654
protected JLabel label;
4755

56+
// int GRADIENT_TOP = 192;
57+
// int GRADIENT_BOTTOM = 246;
58+
protected Image backgroundGradient;
59+
protected Image reverseGradient;
60+
4861

4962
public EditorToolbar(Editor editor) {
5063
this.editor = editor;
5164
base = editor.getBase();
5265
mode = editor.getMode();
5366

67+
//setOpaque(false);
68+
//gradient = createGradient();
69+
//System.out.println(gradient);
70+
71+
backgroundGradient = mode.getGradient("header", 400, HIGH);
72+
reverseGradient = mode.getGradient("reversed", 100, EditorButton.DIM);
73+
5474
runButton = new EditorButton(mode,
5575
"/lib/toolbar/run",
5676
Language.text("toolbar.run"),
@@ -74,34 +94,49 @@ public void actionPerformed(ActionEvent e) {
7494

7595
box = Box.createHorizontalBox();
7696
box.add(runButton);
77-
box.add(label = new JLabel());
78-
box.add(Box.createHorizontalGlue());
7997

98+
label = new JLabel();
99+
label.setFont(mode.getFont("toolbar.sketch.font"));
100+
label.setForeground(mode.getColor("toolbar.sketch.color"));
101+
box.add(label);
102+
currentButton = runButton;
103+
104+
box.add(Box.createHorizontalGlue());
80105
Component items = createModeButtons();
81106
if (items != null) {
82107
box.add(items);
83108
}
84-
box.add(createModeSelector());
85-
86-
add(box);
109+
ModeSelector ms = new ModeSelector();
110+
box.add(ms);
111+
add(box);
87112
}
88113

89114

90-
public Component createModeButtons() {
91-
return null;
115+
public void paintComponent(Graphics g) {
116+
// super.paintComponent(g);
117+
Dimension size = getSize();
118+
g.drawImage(backgroundGradient, 0, 0, size.width, size.height, this);
92119
}
93120

94121

95-
public Component createModeSelector() {
122+
public Component createModeButtons() {
96123
return null;
97124
}
98-
125+
126+
127+
// public Component createModeSelector() {
128+
// return new ModeSelector();
129+
// }
130+
99131

100132
protected void swapButton(EditorButton replacement) {
101-
box.remove(0);
102-
box.add(replacement, 0);
103-
box.revalidate();
104-
box.repaint(); // may be needed
133+
if (currentButton != replacement) {
134+
box.remove(0);
135+
box.add(replacement, 0);
136+
box.revalidate();
137+
box.repaint(); // may be needed
138+
currentButton = replacement;
139+
}
105140
}
106141

107142

@@ -129,8 +164,109 @@ public void deactivateStop() {
129164

130165
abstract public void handleRun();
131166

167+
132168
abstract public void handleStop();
169+
170+
171+
class ModeSelector extends JPanel {
172+
Image offscreen;
173+
int width, height;
174+
175+
String title;
176+
Font titleFont;
177+
Color titleColor;
178+
int titleAscent;
179+
int titleWidth;
180+
181+
final int MODE_GAP_WIDTH = 13;
182+
final int ARROW_GAP_WIDTH = 6;
183+
final int ARROW_WIDTH = 8;
184+
final int ARROW_TOP = 21;
185+
final int ARROW_BOTTOM = 29;
186+
187+
int[] triangleX = new int[3];
188+
int[] triangleY = new int[] { ARROW_TOP, ARROW_TOP, ARROW_BOTTOM };
189+
190+
191+
@SuppressWarnings("deprecation")
192+
public ModeSelector() {
193+
title = mode.getTitle(); //.toUpperCase();
194+
titleFont = mode.getFont("mode.title.font");
195+
titleColor = mode.getColor("mode.title.color");
196+
197+
// getGraphics() is null and no offscreen yet
198+
titleWidth = getToolkit().getFontMetrics(titleFont).stringWidth(title);
199+
200+
// setOpaque(false);
201+
}
202+
203+
@Override
204+
public void paintComponent(Graphics screen) {
205+
// Toolkit.debugOpacity(this);
206+
207+
Dimension size = getSize();
208+
width = 0;
209+
if (width != size.width || height != size.height) {
210+
if (Toolkit.highResDisplay()) {
211+
offscreen = createImage(size.width*2, size.height*2);
212+
} else {
213+
offscreen = createImage(size.width, size.height);
214+
}
215+
width = size.width;
216+
height = size.height;
217+
}
218+
219+
Graphics g = offscreen.getGraphics();
220+
/*Graphics2D g2 =*/ Toolkit.prepareGraphics(g);
221+
//Toolkit.clearGraphics(g, width, height);
222+
// g.clearRect(0, 0, width, height);
223+
// g.setColor(Color.GREEN);
224+
// g.fillRect(0, 0, width, height);
225+
226+
g.setFont(titleFont);
227+
if (titleAscent == 0) {
228+
titleAscent = (int) Toolkit.getAscent(g); //metrics.getAscent();
229+
}
230+
FontMetrics metrics = g.getFontMetrics();
231+
titleWidth = metrics.stringWidth(title);
232+
233+
g.drawImage(reverseGradient, 0, 0, width, height, this);
234+
235+
g.setColor(titleColor);
236+
g.drawString(title, MODE_GAP_WIDTH, (height + titleAscent) / 2);
237+
238+
int x = MODE_GAP_WIDTH + titleWidth + ARROW_GAP_WIDTH;
239+
triangleX[0] = x;
240+
triangleX[1] = x + ARROW_WIDTH;
241+
triangleX[2] = x + ARROW_WIDTH/2;
242+
g.fillPolygon(triangleX, triangleY, 3);
243+
244+
// screen.clearRect(0, 0, width, height);
245+
screen.drawImage(offscreen, 0, 0, width, height, this);
246+
// screen.setColor(Color.RED);
247+
// screen.drawRect(0, 0, width-1, height-1);
248+
}
249+
250+
@Override
251+
public Dimension getPreferredSize() {
252+
return new Dimension(MODE_GAP_WIDTH + titleWidth +
253+
ARROW_GAP_WIDTH + ARROW_WIDTH + MODE_GAP_WIDTH,
254+
EditorButton.DIM);
255+
}
256+
257+
@Override
258+
public Dimension getMinimumSize() {
259+
return getPreferredSize();
260+
}
261+
262+
@Override
263+
public Dimension getMaximumSize() {
264+
return getPreferredSize();
265+
}
266+
}
133267
}
268+
269+
134270
//public abstract class EditorToolbar extends JComponent implements MouseInputListener, KeyListener {
135271
//
136272
// /** Width of each toolbar button. */

app/src/processing/app/Mode.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.awt.*;
2727
import java.awt.event.*;
28+
import java.awt.image.BufferedImage;
29+
import java.awt.image.WritableRaster;
2830
import java.io.*;
2931
import java.util.*;
3032

@@ -40,6 +42,7 @@
4042
import processing.app.contrib.ExamplesContribution;
4143
import processing.app.syntax.*;
4244
import processing.core.PApplet;
45+
import processing.core.PConstants;
4346

4447

4548
public abstract class Mode {
@@ -1342,6 +1345,39 @@ public SyntaxStyle getStyle(String attribute) {
13421345
// return new SyntaxStyle(color, italic, bold);
13431346
return new SyntaxStyle(color, bold);
13441347
}
1348+
1349+
1350+
public Image getGradient(String attribute, int wide, int high) {
1351+
int top = getColor(attribute + ".gradient.top").getRGB();
1352+
int bot = getColor(attribute + ".gradient.bottom").getRGB();
1353+
1354+
// float r1 = (top >> 16) & 0xff;
1355+
// float g1 = (top >> 8) & 0xff;
1356+
// float b1 = top & 0xff;
1357+
// float r2 = (bot >> 16) & 0xff;
1358+
// float g2 = (bot >> 8) & 0xff;
1359+
// float b2 = bot & 0xff;
1360+
1361+
BufferedImage outgoing =
1362+
new BufferedImage(wide, high, BufferedImage.TYPE_INT_RGB);
1363+
int[] row = new int[wide];
1364+
WritableRaster wr = outgoing.getRaster();
1365+
for (int i = 0; i < high; i++) {
1366+
// Arrays.fill(row, (255 - (i + GRADIENT_TOP)) << 24);
1367+
// int r = (int) PApplet.map(i, 0, high-1, r1, r2);
1368+
int rgb = PApplet.lerpColor(top, bot, i / (float)(high-1), PConstants.RGB);
1369+
Arrays.fill(row, rgb);
1370+
// System.out.println(PApplet.hex(row[0]));
1371+
wr.setDataElements(0, i, wide, 1, row);
1372+
}
1373+
// Graphics g = outgoing.getGraphics();
1374+
// for (int i = 0; i < steps; i++) {
1375+
// g.setColor(new Color(1, 1, 1, 255 - (i + GRADIENT_TOP)));
1376+
// //g.fillRect(0, i, EditorButton.DIM, 10);
1377+
// g.drawLine(0, i, EditorButton.DIM, i);
1378+
// }
1379+
return outgoing;
1380+
}
13451381

13461382

13471383
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

0 commit comments

Comments
 (0)