Skip to content

Commit 3868b53

Browse files
committed
[#314] Add keyboard opacity to ZX ULA
1 parent 739a32b commit 3868b53

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

application/src/main/java/net/emustudio/application/gui/dialogs/EditorPanel.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package net.emustudio.application.gui.dialogs;
2020

21-
import net.emustudio.application.Constants;
2221
import net.emustudio.application.gui.actions.CompileAction;
2322
import net.emustudio.application.gui.actions.editor.*;
2423
import net.emustudio.application.gui.editor.Editor;

plugins/device/zxspectrum-ula/src/main/java/net/emustudio/plugins/device/zxspectrum/ula/gui/DisplayWindow.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.emustudio.plugins.device.zxspectrum.ula.ULA;
2222

2323
import javax.swing.*;
24+
import javax.swing.border.BevelBorder;
2425
import java.awt.*;
2526
import java.awt.event.WindowEvent;
2627

@@ -34,10 +35,11 @@ public class DisplayWindow extends JDialog {
3435
private final static int BOUND_Y = (int) (DisplayCanvas.ZOOM * SCREEN_IMAGE_HEIGHT + 2 * MARGIN);
3536

3637
private final DisplayCanvas canvas;
38+
private final KeyboardCanvas keyboardCanvas = new KeyboardCanvas(70);
39+
private final JPanel statusBar = new JPanel();
3740

3841
public DisplayWindow(JFrame parent, ULA ula) {
3942
super(parent);
40-
KeyboardCanvas keyboardCanvas = new KeyboardCanvas(70);
4143
this.canvas = new DisplayCanvas(ula, keyboardCanvas);
4244

4345
initComponents();
@@ -70,16 +72,32 @@ private void initComponents() {
7072
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
7173
canvas.setBounds(MARGIN, MARGIN, BOUND_X, BOUND_Y);
7274

75+
statusBar.setLayout(new BoxLayout(statusBar, BoxLayout.X_AXIS));
76+
statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
77+
78+
JLabel labelOpacity = new JLabel("Keyboard opacity:");
79+
labelOpacity.setHorizontalAlignment(SwingConstants.LEFT);
80+
statusBar.add(labelOpacity);
81+
82+
JSlider sliderOpacity = new JSlider();
83+
sliderOpacity.setMinimum(0);
84+
sliderOpacity.setMaximum(100);
85+
sliderOpacity.setValue(keyboardCanvas.getAlpha());
86+
sliderOpacity.addChangeListener(e -> keyboardCanvas.setAlpha(sliderOpacity.getValue()));
87+
statusBar.add(sliderOpacity);
88+
7389
GroupLayout layout = new GroupLayout(getContentPane());
7490
getContentPane().setLayout(layout);
7591
layout.setHorizontalGroup(
7692
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
77-
.addComponent(canvas));
93+
.addComponent(canvas)
94+
.addComponent(statusBar, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE));
7895
layout.setVerticalGroup(
7996
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
8097
.addGroup(layout.createSequentialGroup()
8198
.addComponent(canvas, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
82-
.addContainerGap()));
99+
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
100+
.addComponent(statusBar, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)));
83101
pack();
84102
}
85103

plugins/device/zxspectrum-ula/src/main/java/net/emustudio/plugins/device/zxspectrum/ula/gui/KeyboardCanvas.java

+43-24
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
2525
private final static double sHalf = s / 2.0; // space between buttons
2626
private final static int arc = 15; // arc radius
2727
private final static int margin = 10;
28-
private final static int rshiftw = 3 * bw - 3 * s - margin + (int)sHalf; // right shift width
28+
private final static int rshiftw = 3 * bw - 3 * s - margin + (int) sHalf; // right shift width
2929

3030
public final static int KEYBOARD_WIDTH = 13 * (bw + s) + bsw + 10 + 10;
3131
public final static int KEYBOARD_HEIGHT = 5 * (bh + s) + 2 * margin - s;
@@ -104,12 +104,14 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
104104
private final Color usableButtonColor;
105105
private final Color outlineColor;
106106
private final Color brightColor;
107+
private int alpha;
107108

108109
private boolean symShift = false;
109110
private boolean shift = false;
110111

111112
public KeyboardCanvas(int alpha) {
112113
setDoubleBuffered(true);
114+
this.alpha = alpha;
113115
this.usableButtonColor = new Color(
114116
Color.LIGHT_GRAY.getRed(),
115117
Color.LIGHT_GRAY.getGreen(),
@@ -131,6 +133,14 @@ public boolean onKeyEvent(KeyEvent e) {
131133
return true;
132134
}
133135

136+
public int getAlpha() {
137+
return alpha;
138+
}
139+
140+
public void setAlpha(int alpha) {
141+
this.alpha = alpha;
142+
}
143+
134144
public void paint(Graphics g) {
135145
Graphics2D g2d = (Graphics2D) g;
136146
// we must set RenderingHints before any drawing
@@ -142,7 +152,7 @@ public void paint(Graphics g) {
142152

143153
g2d.setFont(new Font("SansSerif", Font.PLAIN, 11));
144154
g2d.setStroke(outlineStroke);
145-
g2d.setColor(brightColor);
155+
g2d.setColor(adjustAlpha(brightColor));
146156
g2d.translate(X_SHIFT_L, 0);
147157

148158
for (int i = 0; i < KEY_MAP.length; i++) {
@@ -161,26 +171,28 @@ public void paint(Graphics g) {
161171
int sw = g2d.getFontMetrics().stringWidth(text);
162172

163173
g2d.translate(KEY_MAP[i][0] - sw / 2.0, KEY_MAP[i][1]);
164-
g2d.setColor(outlineColor);
174+
g2d.setColor(adjustAlpha(outlineColor));
165175
g2d.fill(textShape);
166176
g2d.translate(sw / 2.0, 0);
167177
}
168178
}
169179

170180
private void drawKeyboard(Graphics2D g) {
171181
BasicStroke stroke = new BasicStroke(2.0f);
182+
Color adjUsableButtonColor = adjustAlpha(usableButtonColor);
183+
Color adjOutlineColor = adjustAlpha(outlineColor);
172184

173185
// keyboard shape
174186
g.setStroke(stroke);
175-
g.setColor(outlineColor);
187+
g.setColor(adjOutlineColor);
176188
g.drawRoundRect(X_SHIFT, -STROKE_WIDTH, KEYBOARD_WIDTH, KEYBOARD_HEIGHT, arc, arc);
177189

178190
// top row
179191
for (int i = 0; i < 13; i++) {
180192
if (i >= 1 && i <= 10) {
181-
g.setColor(usableButtonColor);
193+
g.setColor(adjUsableButtonColor);
182194
g.fillRoundRect(X_SHIFT_L + i * (bw + s), Y_SHIFT_T, bw, bh, arc, arc);
183-
g.setColor(outlineColor);
195+
g.setColor(adjOutlineColor);
184196
}
185197
g.drawRoundRect(X_SHIFT_L + i * (bw + s), Y_SHIFT_T, bw, bh, arc, arc);
186198
}
@@ -193,9 +205,9 @@ private void drawKeyboard(Graphics2D g) {
193205
g.drawRoundRect(X_SHIFT_L, y1, tabw, bh, arc, arc);
194206
for (int i = 0; i < 12; i++) {
195207
if (i < 10) {
196-
g.setColor(usableButtonColor);
208+
g.setColor(adjUsableButtonColor);
197209
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + tabw + s, y1, bw, bh, arc, arc);
198-
g.setColor(outlineColor);
210+
g.setColor(adjOutlineColor);
199211
}
200212
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + tabw + s, y1, bw, bh, arc, arc);
201213
}
@@ -209,63 +221,70 @@ private void drawKeyboard(Graphics2D g) {
209221
6
210222
);
211223

212-
g.setColor(usableButtonColor);
224+
g.setColor(adjUsableButtonColor);
213225
g.fillPolygon(enterPolygon);
214-
g.setColor(outlineColor);
226+
g.setColor(adjOutlineColor);
215227
g.drawPolygon(enterPolygon);
216228

217229
// caps lock
218230
int y2 = Y_SHIFT_T + (bh + s) * 2;
219231
g.drawRoundRect(X_SHIFT_L, y2, bsw, bh, arc, arc);
220232
for (int i = 0; i < 12; i++) {
221233
if (i < 9) {
222-
g.setColor(usableButtonColor);
234+
g.setColor(adjUsableButtonColor);
223235
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + bsw + s, y2, bw, bh, arc, arc);
224-
g.setColor(outlineColor);
236+
g.setColor(adjOutlineColor);
225237
}
226238
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + bsw + s, y2, bw, bh, arc, arc);
227239
}
228240

229241
// l shift
230242
int y3 = Y_SHIFT_T + (bh + s) * 3;
231243

232-
g.setColor(usableButtonColor);
244+
g.setColor(adjUsableButtonColor);
233245
g.fillRoundRect(X_SHIFT_L, y3, lshiftw, bh, arc, arc);
234-
g.setColor(outlineColor);
246+
g.setColor(adjOutlineColor);
235247
g.drawRoundRect(X_SHIFT_L, y3, lshiftw, bh, arc, arc);
236248
for (int i = 0; i < 11; i++) {
237249
if (i >= 1 && i < 8) {
238-
g.setColor(usableButtonColor);
250+
g.setColor(adjUsableButtonColor);
239251
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + lshiftw + s, y3, bw, bh, arc, arc);
240-
g.setColor(outlineColor);
252+
g.setColor(adjOutlineColor);
241253
}
242254
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + lshiftw + s, y3, bw, bh, arc, arc);
243255
}
244-
g.setColor(usableButtonColor);
256+
g.setColor(adjUsableButtonColor);
245257
g.fillRoundRect(X_SHIFT_L + 11 * (bw + s) + lshiftw + s, y3, rshiftw, bh, arc, arc);
246-
g.setColor(outlineColor);
258+
g.setColor(adjOutlineColor);
247259
g.drawRoundRect(X_SHIFT_L + 11 * (bw + s) + lshiftw + s, y3, rshiftw, bh, arc, arc);
248260

249261
// l ctrl
250262
int y4 = Y_SHIFT_T + (bh + s) * 4;
251-
g.setColor(usableButtonColor);
263+
g.setColor(adjUsableButtonColor);
252264
g.fillRoundRect(X_SHIFT_L, y4, tabw, bh, arc, arc);
253-
g.setColor(outlineColor);
265+
g.setColor(adjOutlineColor);
254266
g.drawRoundRect(X_SHIFT_L, y4, tabw, bh, arc, arc);
255267
g.drawRoundRect(X_SHIFT_L + tabw + s, y4, bw, bh, arc, arc);
256268
g.drawRoundRect(X_SHIFT_L + tabw + bw + 2 * s, y4, tabw, bh, arc, arc);
257269

258-
g.setColor(usableButtonColor);
270+
g.setColor(adjUsableButtonColor);
259271
g.fillRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 2 * s, y4, brakew, bh, arc, arc);
260-
g.setColor(outlineColor);
272+
g.setColor(adjOutlineColor);
261273
g.drawRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 2 * s, y4, brakew, bh, arc, arc);
262274
g.drawRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 4 * s + brakew, y4, tabw, bh, arc, arc);
263275

264-
g.setColor(usableButtonColor);
276+
g.setColor(adjUsableButtonColor);
265277
g.fillRoundRect(X_SHIFT_L + 3 * (tabw + s) + bw + 4 * s + brakew, y4, bw, bh, arc, arc);
266-
g.setColor(outlineColor);
278+
g.setColor(adjOutlineColor);
267279
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + bw + 4 * s + brakew, y4, bw, bh, arc, arc); // RCTRL
268280
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + 2 * bw + 5 * s + brakew, y4, bw, bh, arc, arc);
269281
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + 3 * bw + 6 * s + brakew, y4, tabw, bh, arc, arc);
270282
}
283+
284+
private Color adjustAlpha(Color color) {
285+
int r = color.getRed();
286+
int g = color.getGreen();
287+
int b = color.getBlue();
288+
return new Color(r, g, b, this.alpha);
289+
}
271290
}

0 commit comments

Comments
 (0)