Remove canvas visual

This commit is contained in:
2020-07-20 09:33:34 +02:00
parent 889764d496
commit 61eeac50a1
6 changed files with 0 additions and 381 deletions

View File

@@ -1,191 +0,0 @@
package de.siphalor.was.visual;
import de.siphalor.was.WhatAStorage;
import de.siphalor.was.assets.AssetsManager;
import de.siphalor.was.content.product.Product;
import de.siphalor.was.content.quest.Quest;
import de.siphalor.was.game.Transaction;
import de.siphalor.was.visual.canvas.layout.FixedAspectLayout;
import de.siphalor.was.visual.canvas.layout.FulfillingLayout;
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CanvasVisual implements Visual {
@NotNull
public static final Image MISSINGNO = getImage("textures/missingno.png");
private static final double ASPECT_RATIO = 16.0 / 9.0;
private static final Map<String, Image> imageCache = new HashMap<>();
private WhatAStorage main;
private final Frame frame = new Frame(WhatAStorage.TITLE);
private final Canvas canvas = new Canvas();
private BufferStrategy bufferStrategy;
private long minTickTime = 1000L / 60L;
private boolean fullScreen = false;
@NotNull
public static Image getImage(@NotNull String path) {
Image image = imageCache.get(path);
if (image == null) {
image = AssetsManager.getStream(path).map(inputStream -> {
try {
return (Image) ImageIO.read(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}).orElse(MISSINGNO);
imageCache.put(path, image);
}
return image;
}
@Override
public void setup(WhatAStorage whatAStorage) {
main = whatAStorage;
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
main.scheduleStop();
}
});
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
}
});
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getExtendedKeyCode() == KeyEvent.VK_F11) {
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
frame.dispose();
if (fullScreen) {
System.out.println("End fullscreen");
frame.setUndecorated(false);
frame.setVisible(true);
device.setFullScreenWindow(null);
fullScreen = false;
} else {
System.out.println("Start fullscreen");
frame.setUndecorated(true);
frame.setVisible(true);
device.setFullScreenWindow(frame);
fullScreen = true;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
Panel panel = new Panel();
panel.setBackground(Color.BLACK);
frame.add(panel);
frame.setLayout(new FulfillingLayout(panel));
panel.add(canvas);
panel.setLayout(new FixedAspectLayout(ASPECT_RATIO, canvas));
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
}
@Override
public void run() {
long time, timeDelta, tickBegin = System.nanoTime();
while (!main.isStopScheduled()) {
time = System.nanoTime();
timeDelta = time - tickBegin;
tickBegin = System.nanoTime();
do {
do {
Graphics graphics = bufferStrategy.getDrawGraphics();
graphics.dispose();
} while(bufferStrategy.contentsRestored());
bufferStrategy.show();
} while(bufferStrategy.contentsLost());
time = System.nanoTime();
timeDelta = time - tickBegin;
if (timeDelta < minTickTime) {
try {
Thread.sleep((minTickTime - timeDelta) / 1_000_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
frame.setVisible(false);
frame.dispose();
}
@Override
public void onGameStart() {
}
@Override
public void onScheduleStop() {
}
@Override
public void invalidateI18n() {
}
@Override
public void onBalanceChanged(int budget, Transaction transaction, int totalIncome, int totalLoss) {
}
@Override
public void onQuestAdded(Quest newQuest, boolean canCreateMore) {
}
@Override
public void onQuestRemoved(int index) {
}
@Override
public void onProductSet(int x, int y, int z, Product product) {
}
@Override
public void onProductCleared(int x, int y, int z, Product product) {
}
}

View File

@@ -1,61 +0,0 @@
package de.siphalor.was.visual.canvas.layout;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
public class FixedAspectLayout implements LayoutManager {
/**
* Width through height
*/
private final double aspectRatio;
@Nullable
private Component component;
public FixedAspectLayout(double aspectRatio) {
this(aspectRatio, null);
}
public FixedAspectLayout(double aspectRatio, @Nullable Component component) {
this.aspectRatio = aspectRatio;
this.component = component;
}
@Override
public void addLayoutComponent(String name, Component comp) {
component = comp;
}
@Override
public void removeLayoutComponent(Component comp) {
if (component == comp)
component = null;
}
@Override
public Dimension preferredLayoutSize(Container parent) {
return parent.getSize();
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return parent.getSize();
}
@Override
public void layoutContainer(Container parent) {
if (component != null) {
Insets insets = parent.getInsets();
int width = parent.getWidth() - (insets.left + insets.right);
int height = parent.getHeight() - (insets.top + insets.bottom);
int calc = (int) (height * aspectRatio);
if (calc <= width) {
component.setBounds(insets.left + (width - calc) / 2, insets.top, calc, height);
} else {
calc = (int) (width / aspectRatio);
component.setBounds(insets.left, insets.top + (height - calc) / 2, width, calc);
}
}
}
}

View File

@@ -1,49 +0,0 @@
package de.siphalor.was.visual.canvas.layout;
import java.awt.*;
public class FulfillingLayout implements LayoutManager {
private Component component;
public FulfillingLayout() {
this(null);
}
public FulfillingLayout(Component component) {
this.component = component;
}
@Override
public void addLayoutComponent(String name, Component comp) {
component = comp;
}
@Override
public void removeLayoutComponent(Component comp) {
component = null;
}
@Override
public Dimension preferredLayoutSize(Container parent) {
Insets insets = parent.getInsets();
return new Dimension(parent.getWidth() - (insets.left + insets.right), parent.getHeight() - (insets.top + insets.bottom));
}
@Override
public Dimension minimumLayoutSize(Container parent) {
Insets insets = parent.getInsets();
return new Dimension(parent.getWidth() - (insets.left + insets.right), parent.getHeight() - (insets.top + insets.bottom));
}
@Override
public void layoutContainer(Container parent) {
if (component != null) {
Insets insets = parent.getInsets();
component.setBounds(insets.left, insets.top,
parent.getWidth() - (insets.left + insets.right),
parent.getHeight() - (insets.top + insets.bottom)
);
}
}
}

View File

@@ -1,31 +0,0 @@
package de.siphalor.was.visual.canvas.render;
import java.awt.*;
public class AnimatedTextureObject extends TextureObject {
private final int frameTime;
private final Image[] frames;
private int time = 0;
public AnimatedTextureObject(int x, int y, int width, int height, int frameTime, Image... frames) {
super(x, y, width, height);
this.frameTime = frameTime;
this.frames = frames;
}
@Override
public void tick(int delta) {
super.tick(delta);
time += delta;
if (time >= frames.length * frameTime) {
time = 0;
}
}
@Override
public Image getTexture() {
return frames[time / frameTime];
}
}

View File

@@ -1,8 +0,0 @@
package de.siphalor.was.visual.canvas.render;
import java.awt.*;
public interface Renderable {
void tick(int delta);
void render(Graphics graphics, int width, int height);
}

View File

@@ -1,41 +0,0 @@
package de.siphalor.was.visual.canvas.render;
import de.siphalor.was.util.Util;
import java.awt.*;
public abstract class TextureObject implements Renderable, Cloneable {
private int x;
private int y;
private int width;
private int height;
protected TextureObject(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void mirrorX() {
x += width;
width = -width;
}
public void mirrorY() {
y += height;
height = -height;
}
public abstract Image getTexture();
@Override
public void tick(int delta) {
}
@Override
public void render(Graphics graphics, int width, int height) {
graphics.drawImage(getTexture(), x * width / 1600, y * height / 900, this.width * width / 1600, this.height * height / 900, Util.dummyImageObserver());
}
}