WarpPI/desktop/src/main/java/it/cavallium/warppi/gui/graphicengine/impl/swing/SwingWindow.java

378 lines
11 KiB
Java
Raw Normal View History

2018-09-22 10:29:40 +02:00
package it.cavallium.warppi.gui.graphicengine.impl.swing;
2017-01-16 17:57:09 +01:00
2019-11-02 23:13:19 +01:00
import it.cavallium.warppi.StaticVars;
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.device.input.Keyboard;
import it.cavallium.warppi.gui.graphicengine.RenderingLoop;
import it.cavallium.warppi.util.EventSubmitter;
2019-10-25 13:58:49 +02:00
2019-11-02 23:13:19 +01:00
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
2017-01-16 17:57:09 +01:00
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.net.URISyntaxException;
2017-01-16 17:57:09 +01:00
public class SwingWindow extends JFrame {
private static final long serialVersionUID = 2945898937634075491L;
2019-10-25 13:58:49 +02:00
private final int defaultWidth;
private final int defaultHeight;
2017-01-16 17:57:09 +01:00
public CustomCanvas c;
private RenderingLoop renderingLoop;
2018-09-22 10:29:40 +02:00
private final SwingEngine display;
2017-10-17 22:49:21 +02:00
private int mult = 1;
2019-11-01 15:23:34 +01:00
private final EventSubmitter<Integer[]> onResize;
private final EventSubmitter<Integer[]> onResize$;
public JPanel buttonsPanel;
2018-09-22 10:29:40 +02:00
private SwingAdvancedButton[][] buttons;
private int BTN_SIZE;
2019-10-25 13:58:49 +02:00
private volatile boolean windowShown;
private volatile boolean forceRepaint;
2017-01-16 17:57:09 +01:00
2019-10-25 13:58:49 +02:00
public SwingWindow(final SwingEngine disp, int defaultWidth, int defaultHeight) {
display = disp;
2019-10-25 13:58:49 +02:00
this.defaultWidth = defaultWidth;
this.defaultHeight = defaultHeight;
2018-09-22 11:17:30 +02:00
setLayout(new BorderLayout());
setBackground(Color.BLACK);
2019-10-25 13:58:49 +02:00
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
2017-01-16 17:57:09 +01:00
c = new CustomCanvas();
2019-10-25 13:58:49 +02:00
c.setMinimumSize(new Dimension(0, 0));
c.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
c.setSize(defaultWidth, defaultHeight);
2017-01-16 17:57:09 +01:00
c.setDoubleBuffered(false);
this.add(c, BorderLayout.CENTER);
try {
setupButtonsPanel();
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
2017-01-16 17:57:09 +01:00
// this.setExtendedState(Frame.MAXIMIZED_BOTH);
Toolkit.getDefaultToolkit().setDynamicLayout(false);
// Transparent 16 x 16 pixel cursor image.
final BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
mult = StaticVars.windowZoomFunction.apply(StaticVars.windowZoom.getLastValue()).intValue();
2018-09-02 12:45:51 +02:00
2019-02-27 23:29:03 +01:00
if (!WarpPI.getPlatform().getSettings().isDebugEnabled()) {
2017-01-16 17:57:09 +01:00
// Create a new blank cursor.
final Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
2017-01-16 17:57:09 +01:00
// Set the blank cursor to the JFrame.
getContentPane().setCursor(blankCursor);
setResizable(false);
2017-01-16 17:57:09 +01:00
}
2018-03-01 09:43:10 +01:00
setTitle("WarpPI Calculator by Andrea Cavalli (@Cavallium)");
2019-11-01 15:23:34 +01:00
onResize = EventSubmitter.create();
onResize$ = onResize.map((newSize) -> {
2019-10-25 13:58:49 +02:00
if (newSize[0] <= 0)
newSize[0] = 1;
if (newSize[1] <= 0)
newSize[1] = 1;
var oldSize = disp.r.size;
2019-11-02 23:13:19 +01:00
disp.r.size = new int[]{newSize[0], newSize[1]};
2019-10-25 13:58:49 +02:00
2018-09-22 10:29:40 +02:00
SwingRenderer.canvas2d = new int[disp.r.size[0] * disp.r.size[1]];
2019-10-25 13:58:49 +02:00
var oldG = disp.g;
2018-08-29 00:07:45 +02:00
disp.g = new BufferedImage(disp.r.size[0], disp.r.size[1], BufferedImage.TYPE_INT_RGB);
2019-10-25 13:58:49 +02:00
Graphics2D g = (Graphics2D) disp.g.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setColor(Color.BLACK);
g.clearRect(0, 0, disp.r.size[0], disp.r.size[1]);
double oldRatio = (double) oldSize[0] / (double) oldSize[1];
double newRatio = (double) newSize[0] / (double) newSize[1];
int newFrameWidth;
int newFrameHeight = 0;
if ((int) (oldRatio * 100) == (int) (newRatio * 100)) {
newFrameWidth = newSize[0];
newFrameHeight = newSize[1];
} else {
newFrameWidth = oldSize[0];
newFrameHeight = oldSize[1];
}
g.drawImage(oldG, 0, 0, newFrameWidth, newFrameHeight, null);
forceRepaint = true;
display.repaint();
2019-11-01 15:23:34 +01:00
return newSize;
2018-08-29 00:07:45 +02:00
});
2018-09-02 12:45:51 +02:00
addComponentListener(new ComponentListener() {
2017-01-16 17:57:09 +01:00
@Override
2018-09-22 11:17:30 +02:00
public void componentHidden(final ComponentEvent e) {
2019-10-25 13:58:49 +02:00
sendPowerOffSignal();
2017-01-16 17:57:09 +01:00
}
@Override
2019-11-02 23:13:19 +01:00
public void componentMoved(final ComponentEvent e) {
}
2017-01-16 17:57:09 +01:00
@Override
2018-09-22 11:17:30 +02:00
public void componentResized(final ComponentEvent e) {
2019-10-25 13:58:49 +02:00
if (windowShown) {
2019-11-02 23:13:19 +01:00
onResize.submit(new Integer[]{c.getWidth() / mult, c.getHeight() / mult});
2019-10-25 13:58:49 +02:00
}
2017-01-16 17:57:09 +01:00
}
@Override
2018-09-22 11:17:30 +02:00
public void componentShown(final ComponentEvent e) {
2019-10-25 13:58:49 +02:00
SwingWindow.this.windowShown = true;
2019-02-27 23:29:03 +01:00
if (WarpPI.getPlatform().getSettings().isDebugEnabled())
SwingWindow.this.centerWindow();
}
2017-01-16 17:57:09 +01:00
});
c.setFocusable(true);
c.grabFocus();
c.addKeyListener(new KeyListener() {
2017-01-16 17:57:09 +01:00
@Override
2018-09-22 11:17:30 +02:00
public void keyPressed(final KeyEvent arg0) {
Keyboard.debugKeyCode = arg0.getKeyCode();
2017-01-16 17:57:09 +01:00
}
@Override
2018-09-22 11:17:30 +02:00
public void keyReleased(final KeyEvent arg0) {
2018-04-01 01:01:57 +02:00
Keyboard.debugKeyCodeRelease = arg0.getKeyCode();
2017-01-16 17:57:09 +01:00
}
@Override
2018-09-22 11:17:30 +02:00
public void keyTyped(final KeyEvent arg0) {
2017-01-16 17:57:09 +01:00
// TODO Auto-generated method stub
}
});
2018-08-29 00:07:45 +02:00
StaticVars.windowZoom$.subscribe((newZoomValue) -> {
if (newZoomValue != mult) {
mult = (int) newZoomValue.floatValue();
2019-11-02 23:13:19 +01:00
onResize.submit(new Integer[]{getWWidth(), getWHeight()});
2019-02-27 23:29:03 +01:00
WarpPI.getPlatform().getConsoleUtils().out().println(3, "Engine", "CPU", "Zoom changed");
2018-08-29 00:07:45 +02:00
}
});
}
private void setupButtonsPanel() throws IOException, URISyntaxException {
BTN_SIZE = 32;
2018-09-22 11:17:30 +02:00
if (StaticVars.debugWindow2x)
BTN_SIZE *= 2;
2018-09-22 11:17:30 +02:00
2018-09-22 10:29:40 +02:00
buttons = new SwingAdvancedButton[8][8];
2018-09-22 11:17:30 +02:00
final JPanel buttonsPanelContainer = new JPanel();
buttonsPanelContainer.setLayout(new FlowLayout());
buttonsPanelContainer.setBackground(Color.BLACK);
this.add(buttonsPanelContainer, BorderLayout.PAGE_END);
buttonsPanel = new JPanel();
buttonsPanelContainer.add(buttonsPanel, BorderLayout.CENTER);
buttonsPanel.setLayout(new GridLayout(9, 7));
2018-09-22 14:10:36 +02:00
buttonsPanel.setBackground(Color.BLACK);
buttonsPanel.setDoubleBuffered(false);
buttonsPanel.setVisible(true);
2018-09-22 11:17:30 +02:00
for (int row = 0; row < 5; row++)
for (int col = 0; col < 7; col++)
2018-09-22 14:10:36 +02:00
if (row == 0 && col == 2 || row == 0 && col == 4 || row == 2 && col == 2) {
createBlankBox();
} else {
createBtn(row, col);
}
for (int row = 5; row < 8; row++) {
createBlankBox();
2018-09-22 11:17:30 +02:00
for (int col = 0; col < 5; col++)
createBtn(row, col);
createBlankBox();
}
2018-09-22 11:17:30 +02:00
final int b = 7;
createBlankBox();
2018-09-22 11:17:30 +02:00
for (int a = 4; a >= 0; a--)
createBtn(a, b);
createBlankBox();
2019-10-25 13:58:49 +02:00
var size = super.getSize();
super.setSize(size.width, size.height + buttonsPanelContainer.getHeight());
super.pack();
}
private void createBlankBox() {
2018-09-22 11:17:30 +02:00
final JPanel l = new JPanel();
l.setPreferredSize(new Dimension((int) (BTN_SIZE * 1.5), BTN_SIZE));
l.setBackground(Color.BLACK);
buttonsPanel.add(l);
}
2018-09-22 11:17:30 +02:00
private void createBtn(final int row, final int col) throws IOException, URISyntaxException {
2019-02-27 23:29:03 +01:00
final BufferedImage img = ImageIO.read(WarpPI.getPlatform().getStorageUtils().getResourceStream("/desktop-buttons.png"));
2018-09-22 11:17:30 +02:00
final SwingAdvancedButton b = new SwingAdvancedButton(img, new Dimension((int) (BTN_SIZE * 1.5), BTN_SIZE));
b.drawDefaultComponent = false;
b.setText(Keyboard.getKeyName(row, col));
2018-09-22 14:10:36 +02:00
b.setBasicForeground(Color.BLACK);
Font f = b.getFont();
f = f.deriveFont(Font.BOLD, BTN_SIZE / 3);
b.setFont(f);
b.setBackground(new Color(200, 200, 200));
b.setFocusable(true);
2018-09-22 14:10:36 +02:00
b.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
2018-10-06 16:37:44 +02:00
Keyboard.keyRaw(row, col, true);
c.grabFocus();
2018-09-22 14:10:36 +02:00
}
2019-11-02 23:13:19 +01:00
2018-09-22 14:10:36 +02:00
@Override
public void mousePressed(MouseEvent e) {
2018-10-06 16:37:44 +02:00
Keyboard.keyRaw(row, col, false);
c.grabFocus();
2018-09-22 14:10:36 +02:00
}
2019-11-02 23:13:19 +01:00
2018-09-22 14:10:36 +02:00
@Override
public void mouseExited(MouseEvent e) {
if (b.state == 2) {
b.setForeground(b.basicForeground.darker());
} else {
b.setForeground(b.basicForeground);
}
b.hover = false;
b.repaint();
}
2019-11-02 23:13:19 +01:00
2018-09-22 14:10:36 +02:00
@Override
public void mouseEntered(MouseEvent e) {
if (b.state == 2) {
b.setForeground(b.basicForeground);
} else {
b.setForeground(b.basicForeground.brighter());
}
b.hover = true;
b.repaint();
}
2019-11-02 23:13:19 +01:00
2018-09-22 14:10:36 +02:00
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
2019-11-02 23:13:19 +01:00
2018-09-22 14:10:36 +02:00
}
});
buttons[row][col] = b;
buttonsPanel.add(b);
}
2018-09-22 11:17:30 +02:00
public void setAlphaChanged(final boolean val) {
for (int row = 0; row < buttons.length; row++)
for (int col = 0; col < buttons[0].length; col++) {
2018-09-22 11:17:30 +02:00
final SwingAdvancedButton btn = buttons[row][col];
if (btn != null) {
btn.setText(Keyboard.getKeyName(row, col));
2018-09-22 11:17:30 +02:00
if (row == 0 && col == 1)
if (val)
btn.state = 2;
2018-09-22 11:17:30 +02:00
else
btn.state = 0;
2018-09-22 11:17:30 +02:00
if (val && Keyboard.hasKeyName(row, col))
2018-09-22 14:10:36 +02:00
btn.setBasicForeground(Color.RED);
2018-09-22 11:17:30 +02:00
else
2018-09-22 14:10:36 +02:00
btn.setBasicForeground(Color.BLACK);
}
}
}
2018-09-22 11:17:30 +02:00
public void setShiftChanged(final boolean val) {
for (int row = 0; row < buttons.length; row++)
for (int col = 0; col < buttons[0].length; col++) {
2018-09-22 11:17:30 +02:00
final SwingAdvancedButton btn = buttons[row][col];
if (btn != null) {
btn.setText(Keyboard.getKeyName(row, col));
2018-09-22 11:17:30 +02:00
if (row == 0 && col == 0)
if (val)
btn.state = 2;
2018-09-22 11:17:30 +02:00
else
btn.state = 0;
2018-09-22 11:17:30 +02:00
if (val && Keyboard.hasKeyName(row, col))
2018-09-22 14:10:36 +02:00
btn.setBasicForeground(new Color(255, 120, 0));
2018-09-22 11:17:30 +02:00
else
2018-09-22 14:10:36 +02:00
btn.setBasicForeground(Color.BLACK);
}
}
}
2018-09-22 11:17:30 +02:00
2019-11-01 15:23:34 +01:00
public EventSubmitter<Integer[]> onResize() {
2018-08-29 00:07:45 +02:00
return onResize$;
2017-01-16 17:57:09 +01:00
}
2019-10-25 13:58:49 +02:00
@Deprecated
2017-01-16 17:57:09 +01:00
@Override
2018-09-22 11:17:30 +02:00
public void setSize(final int width, final int height) {
2018-05-12 21:18:29 +02:00
c.setSize(new Dimension(width * mult, height * mult));
2018-09-22 11:17:30 +02:00
c.setPreferredSize(new Dimension(width * mult, height * mult));
2017-01-16 17:57:09 +01:00
super.pack();
}
public Dimension getWSize() {
return new Dimension(getWWidth(), getWHeight());
2017-01-16 17:57:09 +01:00
}
public int getWWidth() {
2019-10-25 13:58:49 +02:00
if (!windowShown) return defaultWidth;
2018-05-12 21:18:29 +02:00
return c.getWidth() / mult;
2017-01-16 17:57:09 +01:00
}
public int getWHeight() {
2019-10-25 13:58:49 +02:00
if (!windowShown) return defaultHeight;
2018-05-12 21:18:29 +02:00
return c.getHeight() / mult;
2017-01-16 17:57:09 +01:00
}
2018-09-22 11:17:30 +02:00
public void setRenderingLoop(final RenderingLoop renderingLoop) {
this.renderingLoop = renderingLoop;
}
2017-01-16 17:57:09 +01:00
public void centerWindow() {
2018-05-12 21:18:29 +02:00
final Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
final int x = (int) ((dimension.getWidth() - super.getWidth()) / 2);
final int y = (int) ((dimension.getHeight() - super.getHeight()) / 2);
super.setLocation(x, y);
}
2018-05-12 21:18:29 +02:00
2019-10-25 13:58:49 +02:00
public void sendPowerOffSignal() {
display.destroyEngine();
this.setVisible(false);
this.dispose();
}
// private static ObjectArrayList<Double> mediaValori = new ObjectArrayList<Double>();
2017-01-16 17:57:09 +01:00
public class CustomCanvas extends JPanel {
2017-01-16 17:57:09 +01:00
/**
2018-09-22 11:17:30 +02:00
*
2017-01-16 17:57:09 +01:00
*/
private static final long serialVersionUID = 605243927485370885L;
@Override
2018-09-22 11:17:30 +02:00
public void paintComponent(final Graphics g) {
2017-01-16 17:57:09 +01:00
// long time1 = System.nanoTime();
if (renderingLoop != null) {
2019-10-25 13:58:49 +02:00
boolean forceRepaint = SwingWindow.this.forceRepaint;
SwingWindow.this.forceRepaint = false;
renderingLoop.refresh(forceRepaint);
final int[] a = ((DataBufferInt) display.g.getRaster().getDataBuffer()).getData();
2018-09-22 10:29:40 +02:00
SwingRenderer.canvas2d = a;
2018-05-12 21:18:29 +02:00
g.clearRect(0, 0, display.r.size[0] * mult, display.r.size[1] * mult);
g.drawImage(display.g, 0, 0, display.r.size[0] * mult, display.r.size[1] * mult, null);
// long time2 = System.nanoTime();
// double timeDelta = ((double)(time2-time1))/1000000000d;
// double mediaAttuale = timeDelta;
// mediaValori.add(mediaAttuale);
// double somma = 0;
// for (Double val : mediaValori) {
// somma+=val;
// }
// System.out.println(somma/((double)mediaValori.size()));
}
2017-01-16 17:57:09 +01:00
}
}
2017-01-16 17:57:09 +01:00
}