Refactor graphics handling by renaming Graphics to GraphicsContext and introducing WindowService for menubar management
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package be.seeseemelk.diceos.system;
|
package be.seeseemelk.diceos.system;
|
||||||
|
|
||||||
|
import be.seeseemelk.diceos.system.gfx.GraphicsContext;
|
||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
import com.badlogic.gdx.utils.ScreenUtils;
|
import com.badlogic.gdx.utils.ScreenUtils;
|
||||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||||
@@ -21,22 +21,22 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
private final DisplayService display;
|
private final DisplayService display;
|
||||||
private final CursorService cursorService;
|
private final CursorService cursorService;
|
||||||
|
private final WindowService windowService;
|
||||||
private final List<OnStartup> startupTasks;
|
private final List<OnStartup> startupTasks;
|
||||||
private Texture clouds;
|
private Texture clouds;
|
||||||
private Texture border;
|
private Texture border;
|
||||||
private NinePatch menubar;
|
|
||||||
private Viewport vmViewport;
|
private Viewport vmViewport;
|
||||||
private Viewport screenViewport;
|
private Viewport screenViewport;
|
||||||
private int scaling = 1;
|
private int scaling = 1;
|
||||||
private int offsetX = 0;
|
private int offsetX = 0;
|
||||||
private int offsetY = 0;
|
private int offsetY = 0;
|
||||||
|
private GraphicsContext gc;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create() {
|
public void create() {
|
||||||
log.info("DiceOS starting...");
|
log.info("DiceOS starting...");
|
||||||
clouds = resourceLoader.loadTexture("backgrounds/clouds.png");
|
clouds = resourceLoader.loadTexture("backgrounds/clouds.png");
|
||||||
border = resourceLoader.loadTexture("system/border.png");
|
border = resourceLoader.loadTexture("system/border.png");
|
||||||
loadMenubar();
|
|
||||||
|
|
||||||
screenViewport = new ScreenViewport();
|
screenViewport = new ScreenViewport();
|
||||||
vmViewport = new ScreenViewport();
|
vmViewport = new ScreenViewport();
|
||||||
@@ -50,11 +50,6 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
log.info("DiceOS started!");
|
log.info("DiceOS started!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadMenubar() {
|
|
||||||
var texture = resourceLoader.loadTexture("system/menubar.png");
|
|
||||||
menubar = new NinePatch(texture, 9, 9, 15, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resize(int width, int height) {
|
public void resize(int width, int height) {
|
||||||
screenViewport.update(width, height, true);
|
screenViewport.update(width, height, true);
|
||||||
@@ -66,6 +61,8 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
offsetY = (height - (displayHeight * scaling)) / 2;
|
offsetY = (height - (displayHeight * scaling)) / 2;
|
||||||
|
|
||||||
cursorService.setScale(scaling);
|
cursorService.setScale(scaling);
|
||||||
|
|
||||||
|
gc = new GraphicsContext(display.getBatch(), displayHeight, displayWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,8 +80,7 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
// Render background
|
// Render background
|
||||||
display.draw(clouds, 0, 0);
|
display.draw(clouds, 0, 0);
|
||||||
|
|
||||||
// Render menubar
|
windowService.paint(gc);
|
||||||
menubar.draw(display.getBatch(), 0, display.getHeight() - 16, display.getWidth(), 16);
|
|
||||||
|
|
||||||
// Render borders
|
// Render borders
|
||||||
var param = new DisplayService.Parameters();
|
var param = new DisplayService.Parameters();
|
||||||
|
|||||||
30
src/main/java/be/seeseemelk/diceos/system/WindowService.java
Normal file
30
src/main/java/be/seeseemelk/diceos/system/WindowService.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package be.seeseemelk.diceos.system;
|
||||||
|
|
||||||
|
import be.seeseemelk.diceos.system.gfx.GraphicsContext;
|
||||||
|
import be.seeseemelk.diceos.system.toolkit.Menubar;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||||
|
import io.avaje.inject.Component;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Component
|
||||||
|
public class WindowService implements OnStartup {
|
||||||
|
private final ResourceLoader resourceLoader;
|
||||||
|
private NinePatch menubar;
|
||||||
|
private Menubar systemMenubar;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartup() {
|
||||||
|
var texture = resourceLoader.loadTexture("system/menubar.png");
|
||||||
|
menubar = new NinePatch(texture, 9, 9, 15, 1);
|
||||||
|
|
||||||
|
systemMenubar = new Menubar()
|
||||||
|
.addItem("Dice")
|
||||||
|
.addItem("System");
|
||||||
|
}
|
||||||
|
|
||||||
|
void paint(GraphicsContext gc) {
|
||||||
|
// Render menubar
|
||||||
|
menubar.draw(gc.getBatch(), 0, gc.getHeight() - 16, gc.getWidth(), 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package be.seeseemelk.diceos.system.gfx;
|
|
||||||
|
|
||||||
import io.avaje.inject.AssistFactory;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Graphics context and rendering utilities.
|
|
||||||
*/
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
@AssistFactory(GraphicsFactory.class)
|
|
||||||
public class Graphics {
|
|
||||||
// private final
|
|
||||||
//
|
|
||||||
// public void begin() {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package be.seeseemelk.diceos.system.gfx;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Graphics context and rendering utilities.
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class GraphicsContext {
|
||||||
|
private final Batch batch;
|
||||||
|
private final int height;
|
||||||
|
private final int width;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
package be.seeseemelk.diceos.system.gfx;
|
package be.seeseemelk.diceos.system.gfx;
|
||||||
|
|
||||||
public interface GraphicsFactory {
|
public interface GraphicsFactory {
|
||||||
Graphics create();
|
GraphicsContext create();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package be.seeseemelk.diceos.system.toolkit;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Menubar {
|
||||||
|
@Getter
|
||||||
|
private final List<String> items = new ArrayList<>();
|
||||||
|
|
||||||
|
public Menubar addItem(String item) {
|
||||||
|
items.add(item);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/main/resources/system/dice.png
Normal file
BIN
src/main/resources/system/dice.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 240 B |
Reference in New Issue
Block a user