-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathModeSelector.java
More file actions
151 lines (120 loc) · 4.57 KB
/
ModeSelector.java
File metadata and controls
151 lines (120 loc) · 4.57 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-22 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.ui;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import processing.app.Messages;
public class ModeSelector extends JPanel {
// corner radius for the dropdown
static final int RADIUS = Toolkit.zoom(3);
String title;
Font titleFont;
Color titleColor;
int titleAscent;
int titleWidth;
final int MODE_GAP_WIDTH = Toolkit.zoom(10);
final int ARROW_GAP_WIDTH = Toolkit.zoom(6);
final int ARROW_WIDTH = Toolkit.zoom(6);
final int ARROW_TOP = Toolkit.zoom(16);
final int ARROW_BOTTOM = Toolkit.zoom(22);
int[] triangleX = new int[3];
int[] triangleY = new int[] { ARROW_TOP, ARROW_TOP, ARROW_BOTTOM };
Color backgroundColor;
Color outlineColor;
public ModeSelector(Editor editor) {
title = editor.getMode().getTitle(); //.toUpperCase();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
JPopupMenu popup = editor.getModePopup();
popup.show(ModeSelector.this, event.getX(), event.getY());
}
});
updateTheme();
}
public void updateTheme() {
titleFont = Theme.getFont("mode.title.font");
titleColor = Theme.getColor("mode.title.color");
// getGraphics() is null (even for editor) and no offscreen yet
//titleWidth = getToolkit().getFontMetrics(titleFont).stringWidth(title);
//titleWidth = editor.getGraphics().getFontMetrics(titleFont).stringWidth(title);
// Theme for mode popup is handled inside Editor.handleTheme()
// because Editor owns the parent object.
backgroundColor = Theme.getColor("mode.background.color");
outlineColor = Theme.getColor("mode.outline.color");
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = Toolkit.prepareGraphics(g);
g.setFont(titleFont);
if (titleAscent == 0) {
titleAscent = (int) processing.app.ui.Toolkit.getAscent(g); //metrics.getAscent();
}
FontMetrics metrics = g.getFontMetrics();
titleWidth = metrics.stringWidth(title);
final int width = getWidth();
final int height = getHeight();
final int inset = Toolkit.zoom(4);
final int outline = Toolkit.zoom(1);
// clear the background
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// draw the outline for this feller
g.setColor(outlineColor);
//Toolkit.dpiStroke(g2);
g2.draw(Toolkit.createRoundRect(outline, outline + inset, width - outline, height - outline - inset,
RADIUS, RADIUS, RADIUS, RADIUS));
g.setColor(titleColor);
g.drawString(title, MODE_GAP_WIDTH, (height + titleAscent) / 2 + 1);
int x = MODE_GAP_WIDTH + titleWidth + ARROW_GAP_WIDTH;
triangleX[0] = x;
triangleX[1] = x + ARROW_WIDTH;
triangleX[2] = x + ARROW_WIDTH/2;
g.fillPolygon(triangleX, triangleY, 3);
}
@Override
public Dimension getPreferredSize() {
int tempWidth = titleWidth; // with any luck, this is set
if (tempWidth == 0) {
Graphics g = getGraphics();
// the Graphics object may not be ready yet, being careful
if (g != null) {
tempWidth = getFontMetrics(titleFont).stringWidth(title);
} else {
Messages.err("null Graphics in EditorToolbar.getPreferredSize()");
}
}
return new Dimension(MODE_GAP_WIDTH + tempWidth +
ARROW_GAP_WIDTH + ARROW_WIDTH + MODE_GAP_WIDTH,
EditorButton.DIM);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}