2323
2424package processing .app ;
2525
26- import java .awt .Component ;
26+ import java .awt .* ;
2727import java .awt .event .ActionEvent ;
28+ import java .awt .image .BufferedImage ;
29+ import java .awt .image .WritableRaster ;
30+ import java .util .Arrays ;
2831
2932import javax .swing .Box ;
3033import javax .swing .JLabel ;
3134import javax .swing .JPanel ;
3235
36+ import processing .core .PApplet ;
37+
3338
3439/**
3540 * Run/Stop button plus Mode selection
3641 */
3742abstract 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. */
0 commit comments