Contextualized methods

This commit is contained in:
Andrea Cavalli 2019-10-25 13:58:49 +02:00
parent 9e1e751a59
commit 5c7411d44c
29 changed files with 470 additions and 247 deletions

View File

@ -1,12 +1,14 @@
package it.cavallium.warppi.device.input;
import java.util.Objects;
public class InputManager {
private final KeyboardInputDevice keyboard;
private final TouchInputDevice touchDevice;
public InputManager(KeyboardInputDevice keyboard, TouchInputDevice touchscreen) {
this.keyboard = keyboard;
this.touchDevice = touchscreen;
this.keyboard = Objects.requireNonNull(keyboard);
this.touchDevice = Objects.requireNonNull(touchscreen);
}
public KeyboardInputDevice getKeyboard() {

View File

@ -339,7 +339,9 @@ public final class DisplayManager implements RenderingLoop {
}
if (!screen.graphicInitialized) {
try {
screen.initializeGraphic();
var displaySize = display.getDisplaySize();
var fullCtx = new RenderContext(graphicEngine, renderer, displaySize[0], displaySize[1]);
screen.initializeGraphic(fullCtx);
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -348,6 +350,12 @@ public final class DisplayManager implements RenderingLoop {
}
private void draw_world() {
var displaySize = display.getDisplaySize();
var scrWidth = displaySize[0] - hud.getMarginLeft() - hud.getMarginRight();
var scrHeight = displaySize[1] - hud.getMarginTop() - hud.getMarginBottom();
var scrCtx = new RenderContext(graphicEngine, renderer.getBoundedInstance(hud.getMarginLeft(), hud.getMarginTop(), scrWidth, scrHeight), scrWidth, scrHeight);
var fullCtdx = new RenderContext(graphicEngine, renderer, displaySize[0], displaySize[1]);
renderer.glColor3i(255, 255, 255);
if (error != null) {
@ -378,14 +386,14 @@ public final class DisplayManager implements RenderingLoop {
}
if (hud.visible)
hud.renderBackground();
screen.render();
screen.render(scrCtx);
if (hud.visible) {
hud.render();
hud.render(fullCtdx);
hud.renderTopmostBackground();
}
screen.renderTopmost();
screen.renderTopmost(scrCtx);
if (hud.visible)
hud.renderTopmost();
hud.renderTopmost(fullCtdx);
}
}
@ -397,8 +405,8 @@ public final class DisplayManager implements RenderingLoop {
private long precTime = -1;
@Override
public void refresh() {
if (supportsPauses == false || Keyboard.popRefreshRequest() || forceRefresh || screen.mustBeRefreshed()) {
public void refresh(boolean force) {
if (force || supportsPauses == false || Keyboard.popRefreshRequest() || forceRefresh || screen.mustBeRefreshed()) {
forceRefresh = false;
draw();
}
@ -446,7 +454,11 @@ public final class DisplayManager implements RenderingLoop {
display.getDisplaySize()[1] = windowSize[1];
}
screen.beforeRender((float) (dt / 1000d));
var displaySize = display.getDisplaySize();
var scrWidth = displaySize[0] - hud.getMarginLeft() - hud.getMarginRight();
var scrHeight = displaySize[1] - hud.getMarginTop() - hud.getMarginBottom();
var scrCtx = new ScreenContext(graphicEngine, scrWidth, scrHeight);
screen.beforeRender(scrCtx, (float) (dt / 1000d));
});
graphicEngine.start(getDrawable());

View File

@ -5,13 +5,13 @@ public interface GraphicalInterface {
void initialize() throws InterruptedException;
void initializeGraphic() throws InterruptedException;
void initializeGraphic(ScreenContext ctx) throws InterruptedException;
void render();
void render(RenderContext ctx);
void renderTopmost();
void renderTopmost(RenderContext ctx);
void beforeRender(float dt);
void beforeRender(ScreenContext ctx, float dt);
boolean mustBeRefreshed();
}

View File

@ -1,5 +1,7 @@
package it.cavallium.warppi.gui;
import it.cavallium.warppi.gui.screens.Screen;
public abstract class HUD implements GraphicalInterface {
public DisplayManager d;
public boolean created = false;
@ -18,7 +20,7 @@ public abstract class HUD implements GraphicalInterface {
}
@Override
public void initializeGraphic() throws InterruptedException {
public void initializeGraphic(ScreenContext ctx) throws InterruptedException {
if (!graphicInitialized) {
graphicInitialized = true;
graphicInitialized();
@ -42,15 +44,15 @@ public abstract class HUD implements GraphicalInterface {
public abstract void renderBackground();
@Override
public abstract void render();
public abstract void render(RenderContext ctx);
public abstract void renderTopmostBackground();
@Override
public abstract void renderTopmost();
public abstract void renderTopmost(RenderContext ctx);
@Override
public abstract void beforeRender(float dt);
public abstract void beforeRender(ScreenContext ctx, float dt);
@Override
public boolean mustBeRefreshed() {
@ -65,4 +67,11 @@ public abstract class HUD implements GraphicalInterface {
visible = true;
}
public abstract int getMarginLeft();
public abstract int getMarginTop();
public abstract int getMarginRight();
public abstract int getMarginBottom();
}

View File

@ -0,0 +1,17 @@
package it.cavallium.warppi.gui;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
public class RenderContext extends ScreenContext {
private final Renderer renderer;
public RenderContext(GraphicEngine graphicEngine, Renderer renderer, int width, int height) {
super(graphicEngine, width, height);
this.renderer = renderer;
}
public Renderer getRenderer() {
return renderer;
}
}

View File

@ -0,0 +1,27 @@
package it.cavallium.warppi.gui;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
public class ScreenContext {
private final GraphicEngine graphicEngine;
private final int width;
private final int height;
public ScreenContext(GraphicEngine graphicEngine, int width, int height) {
this.graphicEngine = graphicEngine;
this.width = width;
this.height = height;
}
public GraphicEngine getGraphicEngine() {
return graphicEngine;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}

View File

@ -3,7 +3,6 @@ package it.cavallium.warppi.gui.graphicengine;
import java.io.IOException;
import java.util.List;
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.flow.Observable;
public interface GraphicEngine {
@ -46,8 +45,6 @@ public interface GraphicEngine {
Skin loadSkin(String file) throws IOException;
void waitForExit();
boolean doesRefreshPauses();
default boolean supportsFontRegistering() {

View File

@ -42,4 +42,6 @@ public interface Renderer {
void glClearSkin();
BinaryFont getCurrentFont();
Renderer getBoundedInstance(int dx, int dy, int width, int height);
}

View File

@ -1,5 +1,5 @@
package it.cavallium.warppi.gui.graphicengine;
public interface RenderingLoop {
void refresh();
void refresh(boolean force);
}

View File

@ -15,7 +15,6 @@ import it.cavallium.warppi.gui.graphicengine.Skin;
public class NoGuiEngine implements GraphicEngine {
private boolean initialized;
public Semaphore exitSemaphore = WarpPI.getPlatform().newSemaphore(0);
@Override
public int[] getSize() {
@ -62,7 +61,6 @@ public class NoGuiEngine implements GraphicEngine {
@Override
public void destroy() {
initialized = false;
exitSemaphore.release();
}
@Override
@ -267,13 +265,6 @@ public class NoGuiEngine implements GraphicEngine {
};
}
@Override
public void waitForExit() {
try {
exitSemaphore.acquire();
} catch (final InterruptedException e) {}
}
@Override
public boolean isSupported() {
return true;

View File

@ -3,6 +3,8 @@ package it.cavallium.warppi.gui.screens;
import java.io.IOException;
import java.util.HashMap;
import it.cavallium.warppi.gui.RenderContext;
import it.cavallium.warppi.gui.ScreenContext;
import org.apache.commons.lang3.SerializationUtils;
import it.cavallium.warppi.WarpPI;
@ -66,7 +68,6 @@ public class MathInputScreen extends Screen {
/**
* Create a copy of this element
* @param mathInputScreen
*/
private MathInputScreen(MathInputScreen old) {
this.calc = new MathContext(old.calc);
@ -87,13 +88,6 @@ public class MathInputScreen extends Screen {
ic = new InputContext();
calc = new MathContext();
try {
BlockContainer.initializeFonts(WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display.getGraphicEngine().loadFont("norm"), WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display.getGraphicEngine().loadFont("smal"));
} catch (final IOException e) {
e.printStackTrace();
WarpPI.getPlatform().exit(1);
}
userInput = new NormalInputContainer(ic);
result = new NormalOutputContainer();
@ -106,16 +100,22 @@ public class MathInputScreen extends Screen {
}
@Override
public void graphicInitialized() throws InterruptedException {
public void graphicInitialized(ScreenContext ctx) throws InterruptedException {
/* Fine caricamento */
try {
BlockContainer.initializeFonts(ctx.getGraphicEngine().loadFont("norm"), ctx.getGraphicEngine().loadFont("smal"));
} catch (final IOException e) {
e.printStackTrace();
WarpPI.getPlatform().exit(1);
}
}
@Override
public void beforeRender(final float dt) {
public void beforeRender(final ScreenContext ctx, final float dt) {
if (WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().error == null) {
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glClearColor(0xFFc5c2af);
ctx.getGraphicEngine().getRenderer().glClearColor(0xFFc5c2af);
} else {
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().renderer.glClearColor(0xFFDC3C32);
ctx.getGraphicEngine().getRenderer().glClearColor(0xFFDC3C32);
}
if (userInput.beforeRender(dt)) {
mustRefresh = true;
@ -140,8 +140,8 @@ public class MathInputScreen extends Screen {
}
@Override
public void render() {
final Renderer renderer = WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().renderer;
public void render(RenderContext ctx) {
final Renderer renderer = ctx.getRenderer();
MathInputScreen.fontBig.use(WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display);
final int textColor = 0xFF000000;
final int padding = 4;
@ -168,8 +168,8 @@ public class MathInputScreen extends Screen {
}
@Override
public void renderTopmost() {
final Renderer renderer = WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().renderer;
public void renderTopmost(RenderContext ctx) {
final Renderer renderer = ctx.getRenderer();
renderer.glColor3f(1, 1, 1);
final int pos = 2;
final int spacersNumb = 1;

View File

@ -2,9 +2,7 @@ package it.cavallium.warppi.gui.screens;
import it.cavallium.warppi.event.KeyboardEventListener;
import it.cavallium.warppi.event.TouchEventListener;
import it.cavallium.warppi.gui.DisplayManager;
import it.cavallium.warppi.gui.GraphicalInterface;
import it.cavallium.warppi.gui.HistoryBehavior;
import it.cavallium.warppi.gui.*;
public abstract class Screen implements KeyboardEventListener, TouchEventListener, GraphicalInterface {
public DisplayManager d;
@ -21,10 +19,10 @@ public abstract class Screen implements KeyboardEventListener, TouchEventListene
}
@Override
public void initializeGraphic() throws InterruptedException {
public void initializeGraphic(ScreenContext ctx) throws InterruptedException {
if (!graphicInitialized) {
graphicInitialized = true;
graphicInitialized();
graphicInitialized(ctx);
}
}
@ -66,18 +64,18 @@ public abstract class Screen implements KeyboardEventListener, TouchEventListene
* Called before initialized()
* @throws InterruptedException
*/
public abstract void graphicInitialized() throws InterruptedException;
public abstract void graphicInitialized(ScreenContext ctx) throws InterruptedException;
@Override
public abstract void render();
public abstract void render(RenderContext ctx);
@Override
public void renderTopmost() {
public void renderTopmost(RenderContext ctx) {
}
@Override
public abstract void beforeRender(float dt);
public abstract void beforeRender(ScreenContext ctx, float dt);
@Override
public abstract boolean mustBeRefreshed();

View File

@ -6,11 +6,12 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLDisplayOutputDevice;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLEngine;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingDeviceState;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingTouchInputDevice;
import org.apache.commons.io.FileUtils;
import it.cavallium.warppi.WarpPI;
@ -23,12 +24,8 @@ import it.cavallium.warppi.device.display.NullBacklightOutputDevice;
import it.cavallium.warppi.device.input.KeyboardInputDevice;
import it.cavallium.warppi.device.input.TouchInputDevice;
import it.cavallium.warppi.Platform;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLDisplayOutputDevice;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLEngine;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingDisplayOutputDevice;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingEngine;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingSkin;
import it.cavallium.warppi.util.CacheUtils;
import it.cavallium.warppi.util.Error;
import net.lingala.zip4j.core.ZipFile;
@ -47,6 +44,8 @@ public class DesktopPlatform implements Platform {
private StartupArguments args;
private DisplayOutputDevice displayOutputDevice;
private DeviceStateDevice deviceStateDevice;
private TouchInputDevice touchInputDevice;
private KeyboardInputDevice keyboardInputDevice;
public DesktopPlatform() {
cu = new DesktopConsoleUtils();
@ -239,14 +238,12 @@ public class DesktopPlatform implements Platform {
@Override
public TouchInputDevice getTouchInputDevice() {
// TODO Auto-generated method stub
return null;
return touchInputDevice;
}
@Override
public KeyboardInputDevice getKeyboardInputDevice() {
// TODO Auto-generated method stub
return null;
return keyboardInputDevice;
}
@Override
@ -300,8 +297,26 @@ public class DesktopPlatform implements Platform {
}
if (this.displayOutputDevice == null) this.displayOutputDevice = availableDevices.get(0);
this.deviceStateDevice = null; //TODO: Implement device state that listen exit signal from swing
if (displayOutputDevice instanceof SwingDisplayOutputDevice) {
this.touchInputDevice = new SwingTouchInputDevice((SwingEngine) displayOutputDevice.getGraphicEngine());
//TODO: implement a keyboard input device
this.keyboardInputDevice = new KeyboardInputDevice() {
@Override
public void initialize() {
}
};
this.deviceStateDevice = new SwingDeviceState((SwingEngine) displayOutputDevice.getGraphicEngine());
} else if (displayOutputDevice instanceof JOGLDisplayOutputDevice) {
this.touchInputDevice = null;
this.keyboardInputDevice = null;
this.deviceStateDevice = null; //TODO: Implement
}
}
}

View File

@ -0,0 +1,33 @@
package it.cavallium.warppi.gui.graphicengine.impl.swing;
import it.cavallium.warppi.device.DeviceStateDevice;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
public class SwingDeviceState implements DeviceStateDevice {
private final SwingEngine graphicEngine;
private final CompletableFuture<Void> exitWait;
public SwingDeviceState(SwingEngine graphicEngine) {
this.graphicEngine = graphicEngine;
this.exitWait = new CompletableFuture<>();
}
@Override
public void initialize() {
graphicEngine.subscribeExit(() -> {
exitWait.complete(null);
});
}
@Override
public Future<?> waitForExit() {
return exitWait;
}
@Override
public void powerOff() {
graphicEngine.sendPowerOffSignal();
}
}

View File

@ -8,7 +8,7 @@ public class SwingDisplayOutputDevice implements DisplayOutputDevice {
private final SwingEngine engine;
public SwingDisplayOutputDevice() {
this.engine = new SwingEngine();
this.engine = new SwingEngine(480, 320);
}
@Override

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.graphicengine.impl.swing;
import java.awt.GraphicsEnvironment;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.Semaphore;
@ -15,11 +16,17 @@ import it.cavallium.warppi.gui.graphicengine.Skin;
public class SwingEngine implements GraphicEngine {
private final int defaultWidth;
private final int defaultHeight;
private SwingWindow INSTANCE;
public SwingRenderer r;
public volatile BufferedImage g;
public volatile boolean initialized;
public Semaphore exitSemaphore;
public SwingEngine(int defaultWidth, int defaultHeight) {
this.defaultWidth = defaultWidth;
this.defaultHeight = defaultHeight;
}
@Override
public void setTitle(final String title) {
@ -51,10 +58,8 @@ public class SwingEngine implements GraphicEngine {
r = new SwingRenderer();
g = new BufferedImage(r.size[0], r.size[1], BufferedImage.TYPE_INT_RGB);
initialized = false;
exitSemaphore = new Semaphore(0);
INSTANCE = new SwingWindow(this);
INSTANCE = new SwingWindow(this, defaultWidth, defaultHeight);
setResizable(WarpPI.getPlatform().getSettings().isDebugEnabled());
setDisplayMode((int) (StaticVars.screenSize[0] / StaticVars.windowZoom.getLastValue()), (int) (StaticVars.screenSize[1] / StaticVars.windowZoom.getLastValue()));
INSTANCE.setVisible(true);
initialized = true;
if (onInitialized != null)
@ -68,20 +73,21 @@ public class SwingEngine implements GraphicEngine {
@Override
public int getWidth() {
return INSTANCE.getWWidth() - StaticVars.screenPos[0];
return this.getSize()[0];
}
@Override
public int getHeight() {
return INSTANCE.getWHeight() - StaticVars.screenPos[1];
return this.getSize()[1];
}
@Override
public void destroy() {
sendPowerOffSignal();
}
protected void destroyEngine() {
initialized = false;
exitSemaphore.release();
INSTANCE.setVisible(false);
INSTANCE.dispose();
}
@Override
@ -121,6 +127,49 @@ public class SwingEngine implements GraphicEngine {
INSTANCE.c.repaint();
}
public void subscribeExit(Runnable subscriber) {
INSTANCE.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
subscriber.run();
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
public void sendPowerOffSignal() {
INSTANCE.sendPowerOffSignal();
}
public abstract class Startable {
public Startable() {
force = false;
@ -137,7 +186,7 @@ public class SwingEngine implements GraphicEngine {
@Override
public int[] getSize() {
return r.size;
return r.size.clone();
}
@Override
@ -165,13 +214,6 @@ public class SwingEngine implements GraphicEngine {
return new SwingSkin(file);
}
@Override
public void waitForExit() {
try {
exitSemaphore.acquire();
} catch (final InterruptedException e) {}
}
@Override
public boolean isSupported() {
if (StaticVars.startupArguments.isEngineForced() && StaticVars.startupArguments.isCPUEngineForced() == false)
@ -191,4 +233,13 @@ public class SwingEngine implements GraphicEngine {
public void setShiftChanged(final boolean val) {
INSTANCE.setShiftChanged(val);
}
public Insets getInsets() {
return INSTANCE.getInsets();
}
public void subscribeTouchDevice(MouseMotionListener mouseMotionListener, MouseListener mouseListener) {
INSTANCE.c.addMouseListener(mouseListener);
INSTANCE.c.addMouseMotionListener(mouseMotionListener);
}
}

View File

@ -17,7 +17,7 @@ public class SwingFont extends RFTFont {
@Override
public void use(final DisplayOutputDevice d) {
if (d.getRenderer() instanceof SwingRenderer)
((SwingRenderer) d.getRenderer()).currentFont = this;
if (d.getGraphicEngine().getRenderer() instanceof SwingRenderer)
((SwingRenderer) d.getGraphicEngine().getRenderer()).currentFont = this;
}
}

View File

@ -15,6 +15,10 @@ public class SwingRenderer implements Renderer {
public int[] size = new int[] { 1, 1 };
static int[] canvas2d = new int[1];
public SwingRenderer() {
}
@Override
public void glColor3i(final int r, final int gg, final int b) {
glColor4i(r, gg, b, 255);
@ -57,8 +61,6 @@ public class SwingRenderer implements Renderer {
private void glDrawSkin(int x0, int y0, final int x1, final int y1, int s0, int t0, int s1, int t1,
final boolean transparent) {
x0 += StaticVars.screenPos[0];
y0 += StaticVars.screenPos[1];
final double incrementX = Math.abs((double) (x1 - x0) / (double) (s1 - s0));
final double incrementY = Math.abs((double) (y1 - y0) / (double) (t1 - t0));
final boolean flippedX = (x1 - x0) / (s1 - s0) < 0;
@ -185,10 +187,6 @@ public class SwingRenderer implements Renderer {
@Override
public void glDrawLine(float x0, float y0, float x1, float y1) {
x0 += StaticVars.screenPos[0];
x1 += StaticVars.screenPos[0];
y0 += StaticVars.screenPos[1];
y1 += StaticVars.screenPos[1];
final int ix0 = (int) x0;
final int ix1 = (int) x1;
final int iy0 = (int) y0;
@ -223,8 +221,6 @@ public class SwingRenderer implements Renderer {
@Override
public void glFillColor(float x, float y, final float width, final float height) {
x += StaticVars.screenPos[0];
y += StaticVars.screenPos[1];
final int ix = (int) x;
final int iy = (int) y;
@ -256,8 +252,6 @@ public class SwingRenderer implements Renderer {
@Override
public void glDrawStringLeft(float x, float y, final String textString) {
x += StaticVars.screenPos[0];
y += StaticVars.screenPos[1];
final int ix = (int) x;
final int iy = (int) y;

View File

@ -13,7 +13,7 @@ public class SwingSkin extends PngSkin {
@Override
public void use(final DisplayOutputDevice d) {
if (d.getRenderer() instanceof SwingRenderer)
((SwingRenderer) d.getRenderer()).currentSkin = this;
if (d.getGraphicEngine().getRenderer() instanceof SwingRenderer)
((SwingRenderer) d.getGraphicEngine().getRenderer()).currentSkin = this;
}
}

View File

@ -0,0 +1,106 @@
package it.cavallium.warppi.gui.graphicengine.impl.swing;
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.device.input.TouchInputDevice;
import it.cavallium.warppi.event.*;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.concurrent.SubmissionPublisher;
import java.util.function.Consumer;
public class SwingTouchInputDevice implements TouchInputDevice {
private final SubmissionPublisher<TouchEvent> touchEventPublisher = new SubmissionPublisher<>();
private final SwingEngine graphicEngine;
public SwingTouchInputDevice(SwingEngine graphicEngine) {
this.graphicEngine = graphicEngine;
}
@Override
public boolean getSwappedAxes() {
return false;
}
@Override
public boolean getInvertedX() {
return false;
}
@Override
public boolean getInvertedY() {
return false;
}
@Override
public void listenTouchEvents(Consumer<TouchEvent> touchEventListener) {
touchEventPublisher.consume(touchEventListener);
}
@Override
public void initialize() {
graphicEngine.subscribeTouchDevice(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
final Insets wp = graphicEngine.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
touches.add(p);
changedTouches.add(p);
final TouchMoveEvent tse = new TouchMoveEvent(changedTouches, touches);
SwingTouchInputDevice.this.touchEventPublisher.submit(tse);
}
@Override
public void mouseMoved(MouseEvent e) {
}
}, new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
final Insets wp = graphicEngine.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
touches.add(p);
changedTouches.add(p);
final TouchStartEvent tse = new TouchStartEvent(changedTouches, touches);
SwingTouchInputDevice.this.touchEventPublisher.submit(tse);
}
@Override
public void mouseReleased(MouseEvent e) {
final Insets wp = graphicEngine.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
changedTouches.add(p);
final TouchEndEvent tse = new TouchEndEvent(changedTouches, touches);
SwingTouchInputDevice.this.touchEventPublisher.submit(tse);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
}

View File

@ -1,22 +1,13 @@
package it.cavallium.warppi.gui.graphicengine.impl.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
@ -41,6 +32,8 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class SwingWindow extends JFrame {
private static final long serialVersionUID = 2945898937634075491L;
private final int defaultWidth;
private final int defaultHeight;
public CustomCanvas c;
private RenderingLoop renderingLoop;
private final SwingEngine display;
@ -50,12 +43,20 @@ public class SwingWindow extends JFrame {
public JPanel buttonsPanel;
private SwingAdvancedButton[][] buttons;
private int BTN_SIZE;
private volatile boolean windowShown;
private volatile boolean forceRepaint;
public SwingWindow(final SwingEngine disp) {
public SwingWindow(final SwingEngine disp, int defaultWidth, int defaultHeight) {
display = disp;
this.defaultWidth = defaultWidth;
this.defaultHeight = defaultHeight;
setLayout(new BorderLayout());
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
c = new CustomCanvas();
c.setMinimumSize(new Dimension(0, 0));
c.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
c.setSize(defaultWidth, defaultHeight);
c.setDoubleBuffered(false);
this.add(c, BorderLayout.CENTER);
try {
@ -84,19 +85,41 @@ public class SwingWindow extends JFrame {
onResize = BehaviorSubject.create();
onResize$ = onResize.doOnNext((newSize) -> {
disp.r.size = new int[] { newSize[0], newSize[1] };
if (disp.r.size[0] <= 0)
disp.r.size[0] = 1;
if (disp.r.size[1] <= 0)
disp.r.size[1] = 1;
if (newSize[0] <= 0)
newSize[0] = 1;
if (newSize[1] <= 0)
newSize[1] = 1;
var oldSize = disp.r.size;
disp.r.size = new int[] { newSize[0], newSize[1] };
SwingRenderer.canvas2d = new int[disp.r.size[0] * disp.r.size[1]];
var oldG = disp.g;
disp.g = new BufferedImage(disp.r.size[0], disp.r.size[1], BufferedImage.TYPE_INT_RGB);
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();
});
addComponentListener(new ComponentListener() {
@Override
public void componentHidden(final ComponentEvent e) {
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display.destroy();
sendPowerOffSignal();
}
@Override
@ -104,11 +127,14 @@ public class SwingWindow extends JFrame {
@Override
public void componentResized(final ComponentEvent e) {
onResize.onNext(new Integer[] { c.getWidth() / mult, c.getHeight() / mult });
if (windowShown) {
onResize.onNext(new Integer[]{c.getWidth() / mult, c.getHeight() / mult});
}
}
@Override
public void componentShown(final ComponentEvent e) {
SwingWindow.this.windowShown = true;
if (WarpPI.getPlatform().getSettings().isDebugEnabled())
SwingWindow.this.centerWindow();
}
@ -132,55 +158,6 @@ public class SwingWindow extends JFrame {
}
});
c.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(final MouseEvent e) {
final Insets wp = SwingWindow.this.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
touches.add(p);
changedTouches.add(p);
final TouchMoveEvent tse = new TouchMoveEvent(changedTouches, touches);
WarpPI.INSTANCE.getHardwareDevice().getInputManager().getTouchDevice().onTouchMove(tse);
}
@Override
public void mouseMoved(final MouseEvent e) {}
});
c.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(final MouseEvent e) {}
@Override
public void mousePressed(final MouseEvent e) {
final Insets wp = SwingWindow.this.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
touches.add(p);
changedTouches.add(p);
final TouchStartEvent tse = new TouchStartEvent(changedTouches, touches);
WarpPI.INSTANCE.getHardwareDevice().getInputManager().getTouchDevice().onTouchStart(tse);
}
@Override
public void mouseReleased(final MouseEvent e) {
final Insets wp = SwingWindow.this.getInsets();
final TouchPoint p = new TouchPoint(0, e.getX() - wp.left, e.getY() - wp.top, 5, 5, 1, 0);
final ObjectArrayList<TouchPoint> touches = new ObjectArrayList<>();
final ObjectArrayList<TouchPoint> changedTouches = new ObjectArrayList<>();
changedTouches.add(p);
final TouchEndEvent tse = new TouchEndEvent(changedTouches, touches);
WarpPI.INSTANCE.getHardwareDevice().getInputManager().getTouchDevice().onTouchEnd(tse);
}
@Override
public void mouseEntered(final MouseEvent e) {}
@Override
public void mouseExited(final MouseEvent e) {}
});
StaticVars.windowZoom$.subscribe((newZoomValue) -> {
if (newZoomValue != mult) {
mult = (int) newZoomValue.floatValue();
@ -224,6 +201,10 @@ public class SwingWindow extends JFrame {
for (int a = 4; a >= 0; a--)
createBtn(a, b);
createBlankBox();
var size = super.getSize();
super.setSize(size.width, size.height + buttonsPanelContainer.getHeight());
super.pack();
}
private void createBlankBox() {
@ -327,6 +308,7 @@ public class SwingWindow extends JFrame {
return onResize$;
}
@Deprecated
@Override
public void setSize(final int width, final int height) {
c.setSize(new Dimension(width * mult, height * mult));
@ -339,10 +321,12 @@ public class SwingWindow extends JFrame {
}
public int getWWidth() {
if (!windowShown) return defaultWidth;
return c.getWidth() / mult;
}
public int getWHeight() {
if (!windowShown) return defaultHeight;
return c.getHeight() / mult;
}
@ -357,6 +341,12 @@ public class SwingWindow extends JFrame {
super.setLocation(x, y);
}
public void sendPowerOffSignal() {
display.destroyEngine();
this.setVisible(false);
this.dispose();
}
// private static ObjectArrayList<Double> mediaValori = new ObjectArrayList<Double>();
public class CustomCanvas extends JPanel {
@ -370,7 +360,9 @@ public class SwingWindow extends JFrame {
public void paintComponent(final Graphics g) {
// long time1 = System.nanoTime();
if (renderingLoop != null) {
renderingLoop.refresh();
boolean forceRepaint = SwingWindow.this.forceRepaint;
SwingWindow.this.forceRepaint = false;
renderingLoop.refresh(forceRepaint);
final int[] a = ((DataBufferInt) display.g.getRaster().getDataBuffer()).getData();

View File

@ -1,22 +1,21 @@
package it.cavallium.warppi.gui.graphicengine.impl.jogl;
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
public class JOGLDisplayOutputDevice implements DisplayOutputDevice {
private JOGLEngine engine;
private JOGLEngine engine;
public JOGLDisplayOutputDevice() {
this.engine = new JOGLEngine();
}
@Override
public int[] getDisplaySize() {
return engine.getSize();
}
@Override
public JOGLEngine getGraphicEngine() {
return engine;
}
public JOGLDisplayOutputDevice() {
this.engine = new JOGLEngine();
}
@Override
public int[] getDisplaySize() {
return engine.getSize();
}
@Override
public JOGLEngine getGraphicEngine() {
return engine;
}
}

View File

@ -13,7 +13,6 @@ import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.util.texture.Texture;
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.StaticVars;
import it.cavallium.warppi.flow.Observable;
import it.cavallium.warppi.gui.graphicengine.BinaryFont;
@ -23,7 +22,6 @@ import it.cavallium.warppi.gui.graphicengine.Skin;
public class JOGLEngine implements GraphicEngine {
private final JOGLDisplayOutputDevice display;
private volatile boolean initialized;
private volatile boolean created;
private NEWTWindow wnd;
@ -36,13 +34,12 @@ public class JOGLEngine implements GraphicEngine {
protected LinkedList<Texture> registeredTextures;
protected LinkedList<Texture> unregisteredTextures;
public JOGLEngine(JOGLDisplayOutputDevice display) {
this.display = display;
public JOGLEngine() {
}
@Override
public int[] getSize() {
return size;
return size.clone();
}
@Override
@ -78,14 +75,15 @@ public class JOGLEngine implements GraphicEngine {
public void create(final Runnable onInitialized) {
initialized = false;
created = false;
size = new int[] { display.getDisplaySize()[0], display.getDisplaySize()[1] };
this.getSize();
size = new int[] { this.getSize()[0], this.getSize()[1] };
created = true;
registeredTextures = new LinkedList<>();
unregisteredTextures = new LinkedList<>();
r = new JOGLRenderer();
wnd = new NEWTWindow(display);
wnd = new NEWTWindow(this);
wnd.create();
setDisplayMode(display.getDisplaySize()[0], display.getDisplaySize()[1]);
setDisplayMode(this.getSize()[0], this.getSize()[1]);
setResizable(WarpPI.getPlatform().getSettings().isDebugEnabled());
initialized = true;
wnd.onInitialized = onInitialized;
@ -133,7 +131,7 @@ public class JOGLEngine implements GraphicEngine {
@Override
public void repaint() {
if (d != null & r != null && JOGLRenderer.gl != null)
d.refresh();
d.refresh(false);
}
@Override
@ -146,7 +144,8 @@ public class JOGLEngine implements GraphicEngine {
for (final Entry<String, JOGLFont> entry : fontCache.entrySet())
if (entry.getKey().equals(name))
return entry.getValue();
final JOGLFont font = new JOGLFont(display, name);
final JOGLFont font = new JOGLFont(name);
this.registerFont(font);
fontCache.put(name, font);
return font;
}
@ -156,21 +155,15 @@ public class JOGLEngine implements GraphicEngine {
for (final Entry<String, JOGLFont> entry : fontCache.entrySet())
if (entry.getKey().equals(name))
return entry.getValue();
final JOGLFont font = new JOGLFont(display, path, name);
final JOGLFont font = new JOGLFont(path, name);
this.registerFont(font);
fontCache.put(name, font);
return font;
}
@Override
public Skin loadSkin(final String file) throws IOException {
return new JOGLSkin(display, file);
}
@Override
public void waitForExit() {
try {
exitSemaphore.acquire();
} catch (final InterruptedException e) {}
return new JOGLSkin(file);
}
@Override
@ -195,8 +188,12 @@ public class JOGLEngine implements GraphicEngine {
return false;
}
public void registerFont(final JOGLFont gpuFont) {
registeredFonts.add(gpuFont);
private void registerFont(final BinaryFont gpuFont) {
if (gpuFont instanceof JOGLFont || gpuFont == null) {
registeredFonts.add(gpuFont);
} else {
throw new IllegalArgumentException("Can't handle font type " + gpuFont.getClass().getSimpleName());
}
}
@Override
@ -212,5 +209,4 @@ public class JOGLEngine implements GraphicEngine {
public void registerTexture(final Texture t) {
unregisteredTextures.addLast(t);
}
}

View File

@ -35,13 +35,12 @@ public class JOGLFont implements BinaryFont {
private boolean initialized = false;
private File tmpFont;
JOGLFont(final JOGLDisplayOutputDevice g, final String name) throws IOException {
this(g, null, name);
JOGLFont(final String name) throws IOException {
this(null, name);
}
public JOGLFont(final JOGLDisplayOutputDevice g, final String path, final String name) throws IOException {
public JOGLFont(final String path, final String name) throws IOException {
load(path, name);
g.getGraphicEngine().registerFont(this);
}
@Override

View File

@ -23,7 +23,7 @@ public class JOGLSkin implements Skin {
private boolean initialized = false;
private boolean isResource;
JOGLSkin(final JOGLDisplayOutputDevice d, final String file) throws IOException {
JOGLSkin(final String file) throws IOException {
load(file);
}
@ -55,7 +55,7 @@ public class JOGLSkin implements Skin {
public void use(final DisplayOutputDevice d) {
if (!initialized)
initialize(d);
final JOGLRenderer r = (JOGLRenderer) d.getRenderer();
final JOGLRenderer r = (JOGLRenderer) d.getGraphicEngine().getRenderer();
r.useTexture(t, w, h);
}

View File

@ -28,12 +28,8 @@
package it.cavallium.warppi.gui.graphicengine.impl.jogl;
import java.util.List;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.KeyListener;
import com.jogamp.newt.event.MouseEvent;
import com.jogamp.newt.event.MouseListener;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.event.WindowListener;
import com.jogamp.newt.event.WindowUpdateEvent;
@ -55,15 +51,9 @@ import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.device.input.Keyboard;
import it.cavallium.warppi.StaticVars;
import it.cavallium.warppi.event.Key;
import it.cavallium.warppi.event.TouchEndEvent;
import it.cavallium.warppi.event.TouchMoveEvent;
import it.cavallium.warppi.event.TouchPoint;
import it.cavallium.warppi.event.TouchStartEvent;
import it.cavallium.warppi.flow.BehaviorSubject;
import it.cavallium.warppi.flow.SimpleSubject;
import it.cavallium.warppi.flow.Subject;
import it.cavallium.warppi.gui.DisplayManager;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
*
@ -73,7 +63,6 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
class NEWTWindow implements GLEventListener {
private final DisplayOutputDevice display;
private final JOGLEngine engine;
private final JOGLRenderer renderer;
public GLWindow window;
@ -87,15 +76,14 @@ class NEWTWindow implements GLEventListener {
private final BehaviorSubject<Float> onZoom = BehaviorSubject.create();
private final Subject<GL2ES1> onGLContext = SimpleSubject.create();
public NEWTWindow(final JOGLDisplayOutputDevice display) {
this.display = display;
this.engine = display.getGraphicEngine();
public NEWTWindow(final JOGLEngine engine) {
this.engine = engine;
renderer = engine.getRenderer();
engine.size[0] = display.getDisplaySize()[0];
engine.size[1] = display.getDisplaySize()[1];
realWindowSize = new int[] { display.getDisplaySize()[0], display.getDisplaySize()[1] };
engine.size[0] = engine.getSize()[0];
engine.size[1] = engine.getSize()[1];
realWindowSize = new int[] { engine.getSize()[0], engine.getSize()[1] };
windowZoom = StaticVars.windowZoomFunction.apply(StaticVars.windowZoom.getLastValue());
onRealResize = BehaviorSubject.create(new Integer[] { (int) (display.getDisplaySize()[0] * windowZoom), (int) (display.getDisplaySize()[1] * windowZoom) });
onRealResize = BehaviorSubject.create(new Integer[] { (int) (engine.getSize()[0] * windowZoom), (int) (engine.getSize()[1] * windowZoom) });
onRealResize.subscribe((realSize) -> {
realWindowSize[0] = realSize[0];
@ -105,16 +93,14 @@ class NEWTWindow implements GLEventListener {
onResizeEvent.onNext(new Integer[] { engine.size[0], engine.size[1] });
refreshViewport = true;
});
StaticVars.windowZoom$.subscribe((zoom) -> {
onZoom.onNext(zoom);
});
StaticVars.windowZoom$.subscribe(onZoom::onNext);
onZoom.subscribe((z) -> {
if (windowZoom != 0) {
windowZoom = z;
engine.size[0] = (int) (realWindowSize[0] / windowZoom);
engine.size[1] = (int) (realWindowSize[1] / windowZoom);
display.getDisplaySize()[0] = engine.size[0];
display.getDisplaySize()[1] = engine.size[1];
engine.getSize()[0] = engine.size[0];
engine.getSize()[1] = engine.size[1];
refreshViewport = true;
}
});

View File

@ -1,23 +1,19 @@
package it.cavallium.warppi.device.input;
import java.util.List;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.SubmissionPublisher;
import java.util.function.Consumer;
import com.jogamp.newt.event.MouseEvent;
import com.jogamp.newt.event.MouseListener;
import com.jogamp.newt.opengl.GLWindow;
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.device.input.TouchInputDevice;
import it.cavallium.warppi.event.TouchCancelEvent;
import it.cavallium.warppi.event.TouchEndEvent;
import it.cavallium.warppi.event.TouchEvent;
import it.cavallium.warppi.event.TouchMoveEvent;
import it.cavallium.warppi.event.TouchPoint;
import it.cavallium.warppi.event.TouchStartEvent;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLEngine;
import it.cavallium.warppi.gui.screens.Screen;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class PIHardwareTouchDevice implements TouchInputDevice {
@ -126,7 +122,7 @@ public class PIHardwareTouchDevice implements TouchInputDevice {
}
@Override
public void listenTouchEvents(Subscriber<TouchEvent> touchEventListener) {
touchEventPublisher.subscribe(touchEventListener);
public void listenTouchEvents(Consumer<TouchEvent> touchEventListener) {
touchEventPublisher.consume(touchEventListener);
}
}

View File

@ -11,6 +11,7 @@ import java.util.Map;
import it.cavallium.warppi.Platform;
import it.cavallium.warppi.boot.StartupArguments;
import it.cavallium.warppi.device.DeviceStateDevice;
import it.cavallium.warppi.device.display.BacklightOutputDevice;
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.device.display.NoDisplaysAvailableException;

View File

@ -48,8 +48,8 @@ public class HtmlFont extends RFTFont {
@Override
public void use(final DisplayOutputDevice d) {
if (d.getRenderer() instanceof HtmlRenderer)
((HtmlRenderer) d.getRenderer()).f = this;
if (d.getGraphicEngine().getRenderer() instanceof HtmlRenderer)
((HtmlRenderer) d.getGraphicEngine().getRenderer()).f = this;
}
}