Skip to content

Commit

Permalink
[#314] Add keyboard opacity to ZX ULA
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Jun 3, 2024
1 parent 739a32b commit 3868b53
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package net.emustudio.application.gui.dialogs;

import net.emustudio.application.Constants;
import net.emustudio.application.gui.actions.CompileAction;
import net.emustudio.application.gui.actions.editor.*;
import net.emustudio.application.gui.editor.Editor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.emustudio.plugins.device.zxspectrum.ula.ULA;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.WindowEvent;

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

private final DisplayCanvas canvas;
private final KeyboardCanvas keyboardCanvas = new KeyboardCanvas(70);
private final JPanel statusBar = new JPanel();

public DisplayWindow(JFrame parent, ULA ula) {
super(parent);
KeyboardCanvas keyboardCanvas = new KeyboardCanvas(70);
this.canvas = new DisplayCanvas(ula, keyboardCanvas);

initComponents();
Expand Down Expand Up @@ -70,16 +72,32 @@ private void initComponents() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
canvas.setBounds(MARGIN, MARGIN, BOUND_X, BOUND_Y);

statusBar.setLayout(new BoxLayout(statusBar, BoxLayout.X_AXIS));
statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));

JLabel labelOpacity = new JLabel("Keyboard opacity:");
labelOpacity.setHorizontalAlignment(SwingConstants.LEFT);
statusBar.add(labelOpacity);

JSlider sliderOpacity = new JSlider();
sliderOpacity.setMinimum(0);
sliderOpacity.setMaximum(100);
sliderOpacity.setValue(keyboardCanvas.getAlpha());
sliderOpacity.addChangeListener(e -> keyboardCanvas.setAlpha(sliderOpacity.getValue()));
statusBar.add(sliderOpacity);

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(canvas));
.addComponent(canvas)
.addComponent(statusBar, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(canvas, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
.addContainerGap()));
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusBar, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)));
pack();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class KeyboardCanvas extends JComponent implements KeyboardDispatcher.OnK
private final static double sHalf = s / 2.0; // space between buttons
private final static int arc = 15; // arc radius
private final static int margin = 10;
private final static int rshiftw = 3 * bw - 3 * s - margin + (int)sHalf; // right shift width
private final static int rshiftw = 3 * bw - 3 * s - margin + (int) sHalf; // right shift width

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

private boolean symShift = false;
private boolean shift = false;

public KeyboardCanvas(int alpha) {
setDoubleBuffered(true);
this.alpha = alpha;
this.usableButtonColor = new Color(
Color.LIGHT_GRAY.getRed(),
Color.LIGHT_GRAY.getGreen(),
Expand All @@ -131,6 +133,14 @@ public boolean onKeyEvent(KeyEvent e) {
return true;
}

public int getAlpha() {
return alpha;
}

public void setAlpha(int alpha) {
this.alpha = alpha;
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// we must set RenderingHints before any drawing
Expand All @@ -142,7 +152,7 @@ public void paint(Graphics g) {

g2d.setFont(new Font("SansSerif", Font.PLAIN, 11));
g2d.setStroke(outlineStroke);
g2d.setColor(brightColor);
g2d.setColor(adjustAlpha(brightColor));
g2d.translate(X_SHIFT_L, 0);

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

g2d.translate(KEY_MAP[i][0] - sw / 2.0, KEY_MAP[i][1]);
g2d.setColor(outlineColor);
g2d.setColor(adjustAlpha(outlineColor));
g2d.fill(textShape);
g2d.translate(sw / 2.0, 0);
}
}

private void drawKeyboard(Graphics2D g) {
BasicStroke stroke = new BasicStroke(2.0f);
Color adjUsableButtonColor = adjustAlpha(usableButtonColor);
Color adjOutlineColor = adjustAlpha(outlineColor);

// keyboard shape
g.setStroke(stroke);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT, -STROKE_WIDTH, KEYBOARD_WIDTH, KEYBOARD_HEIGHT, arc, arc);

// top row
for (int i = 0; i < 13; i++) {
if (i >= 1 && i <= 10) {
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + i * (bw + s), Y_SHIFT_T, bw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
}
g.drawRoundRect(X_SHIFT_L + i * (bw + s), Y_SHIFT_T, bw, bh, arc, arc);
}
Expand All @@ -193,9 +205,9 @@ private void drawKeyboard(Graphics2D g) {
g.drawRoundRect(X_SHIFT_L, y1, tabw, bh, arc, arc);
for (int i = 0; i < 12; i++) {
if (i < 10) {
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + tabw + s, y1, bw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
}
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + tabw + s, y1, bw, bh, arc, arc);
}
Expand All @@ -209,63 +221,70 @@ private void drawKeyboard(Graphics2D g) {
6
);

g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillPolygon(enterPolygon);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawPolygon(enterPolygon);

// caps lock
int y2 = Y_SHIFT_T + (bh + s) * 2;
g.drawRoundRect(X_SHIFT_L, y2, bsw, bh, arc, arc);
for (int i = 0; i < 12; i++) {
if (i < 9) {
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + bsw + s, y2, bw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
}
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + bsw + s, y2, bw, bh, arc, arc);
}

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

g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L, y3, lshiftw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT_L, y3, lshiftw, bh, arc, arc);
for (int i = 0; i < 11; i++) {
if (i >= 1 && i < 8) {
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + i * (bw + s) + lshiftw + s, y3, bw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
}
g.drawRoundRect(X_SHIFT_L + i * (bw + s) + lshiftw + s, y3, bw, bh, arc, arc);
}
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + 11 * (bw + s) + lshiftw + s, y3, rshiftw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT_L + 11 * (bw + s) + lshiftw + s, y3, rshiftw, bh, arc, arc);

// l ctrl
int y4 = Y_SHIFT_T + (bh + s) * 4;
g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L, y4, tabw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT_L, y4, tabw, bh, arc, arc);
g.drawRoundRect(X_SHIFT_L + tabw + s, y4, bw, bh, arc, arc);
g.drawRoundRect(X_SHIFT_L + tabw + bw + 2 * s, y4, tabw, bh, arc, arc);

g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 2 * s, y4, brakew, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 2 * s, y4, brakew, bh, arc, arc);
g.drawRoundRect(X_SHIFT_L + 2 * (tabw + s) + bw + 4 * s + brakew, y4, tabw, bh, arc, arc);

g.setColor(usableButtonColor);
g.setColor(adjUsableButtonColor);
g.fillRoundRect(X_SHIFT_L + 3 * (tabw + s) + bw + 4 * s + brakew, y4, bw, bh, arc, arc);
g.setColor(outlineColor);
g.setColor(adjOutlineColor);
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + bw + 4 * s + brakew, y4, bw, bh, arc, arc); // RCTRL
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + 2 * bw + 5 * s + brakew, y4, bw, bh, arc, arc);
g.drawRoundRect(X_SHIFT_L + 3 * (tabw + s) + 3 * bw + 6 * s + brakew, y4, tabw, bh, arc, arc);
}

private Color adjustAlpha(Color color) {
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
return new Color(r, g, b, this.alpha);
}
}

0 comments on commit 3868b53

Please sign in to comment.