Enable providing Context and GUI via constructor in Component Inflater
This commit is contained in:
@@ -1,11 +1,20 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.BaseWidget;
|
import com.bartlomiejpluta.base.api.game.gui.base.BaseWidget;
|
||||||
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
|
|
||||||
public abstract class BaseComponent extends BaseWidget implements Component {
|
public abstract class BaseComponent extends BaseWidget implements Component {
|
||||||
protected boolean focused;
|
protected boolean focused;
|
||||||
|
protected final Context context;
|
||||||
|
protected final GUI gui;
|
||||||
|
|
||||||
|
protected BaseComponent(Context context, GUI gui) {
|
||||||
|
this.context = context;
|
||||||
|
this.gui = gui;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Component> getChildren() {
|
public Iterable<Component> getChildren() {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@@ -11,6 +13,10 @@ public abstract class BaseContainer extends BaseComponent implements Container {
|
|||||||
protected final List<Component> children = new LinkedList<>();
|
protected final List<Component> children = new LinkedList<>();
|
||||||
private final List<Component> readOnlyChildren = unmodifiableList(children);
|
private final List<Component> readOnlyChildren = unmodifiableList(children);
|
||||||
|
|
||||||
|
public BaseContainer(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Component> getChildren() {
|
public Iterable<Component> getChildren() {
|
||||||
return readOnlyChildren;
|
return readOnlyChildren;
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
|
|
||||||
public class HLayout extends BaseContainer {
|
public class HLayout extends BaseContainer {
|
||||||
|
|
||||||
|
public HLayout(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected float getContentWidth() {
|
protected float getContentWidth() {
|
||||||
return sumChildrenWidth();
|
return sumChildrenWidth();
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||||
@@ -10,6 +12,10 @@ public class HOptionChoice extends HLayout {
|
|||||||
private static final EnumSet<KeyAction> ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT);
|
private static final EnumSet<KeyAction> ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT);
|
||||||
private int selected = 0;
|
private int selected = 0;
|
||||||
|
|
||||||
|
public HOptionChoice(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void focus() {
|
public void focus() {
|
||||||
super.focus();
|
super.focus();
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.Image;
|
import com.bartlomiejpluta.base.api.game.gui.base.Image;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.Paint;
|
import com.bartlomiejpluta.base.api.game.gui.base.Paint;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
|
|
||||||
public class ImageView extends BaseComponent {
|
public class ImageView extends BaseComponent {
|
||||||
private final GUI gui;
|
|
||||||
private final Paint paint;
|
private final Paint paint;
|
||||||
|
|
||||||
private Image image;
|
private Image image;
|
||||||
@@ -17,10 +15,9 @@ public class ImageView extends BaseComponent {
|
|||||||
private float scaleX = 1;
|
private float scaleX = 1;
|
||||||
private float scaleY = 1;
|
private float scaleY = 1;
|
||||||
|
|
||||||
public ImageView(GUI gui, String imageUid) {
|
public ImageView(Context context, GUI gui) {
|
||||||
this.gui = requireNonNull(gui);
|
super(context, gui);
|
||||||
this.paint = gui.createPaint();
|
this.paint = gui.createPaint();
|
||||||
this.image = gui.getImage(imageUid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setImage(String imageUid) {
|
public void setImage(String imageUid) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.Color;
|
import com.bartlomiejpluta.base.api.game.gui.base.Color;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
@@ -7,8 +8,6 @@ import com.bartlomiejpluta.base.api.game.screen.Screen;
|
|||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
public class Label extends BaseComponent {
|
public class Label extends BaseComponent {
|
||||||
// private final GUI gui;
|
|
||||||
|
|
||||||
private String text = "";
|
private String text = "";
|
||||||
private String font;
|
private String font;
|
||||||
private float fontSize;
|
private float fontSize;
|
||||||
@@ -17,17 +16,11 @@ public class Label extends BaseComponent {
|
|||||||
|
|
||||||
private final float[] bounds = new float[4];
|
private final float[] bounds = new float[4];
|
||||||
|
|
||||||
public Label() {
|
public Label(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
this.color = gui.createColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public Label(GUI gui, String font) {
|
|
||||||
// this.gui = requireNonNull(gui);
|
|
||||||
// this.font = requireNonNull(font);
|
|
||||||
//
|
|
||||||
// this.color = this.gui.createColor();
|
|
||||||
// }
|
|
||||||
|
|
||||||
public String getText() {
|
public String getText() {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
|
|
||||||
public class VLayout extends BaseContainer {
|
public class VLayout extends BaseContainer {
|
||||||
|
|
||||||
|
public VLayout(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected float getContentWidth() {
|
protected float getContentWidth() {
|
||||||
return maxChildrenWidth();
|
return maxChildrenWidth();
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||||
@@ -10,6 +12,10 @@ public class VOptionChoice extends VLayout {
|
|||||||
private static final EnumSet<KeyAction> ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT);
|
private static final EnumSet<KeyAction> ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT);
|
||||||
private int selected = 0;
|
private int selected = 0;
|
||||||
|
|
||||||
|
public VOptionChoice(Context context, GUI gui) {
|
||||||
|
super(context, gui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void focus() {
|
public void focus() {
|
||||||
super.focus();
|
super.focus();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
|||||||
import com.bartlomiejpluta.base.engine.context.model.DefaultContext;
|
import com.bartlomiejpluta.base.engine.context.model.DefaultContext;
|
||||||
import com.bartlomiejpluta.base.engine.core.engine.GameEngine;
|
import com.bartlomiejpluta.base.engine.core.engine.GameEngine;
|
||||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||||
|
import com.bartlomiejpluta.base.engine.gui.xml.inflater.ComponentInflater;
|
||||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||||
import com.bartlomiejpluta.base.engine.project.serial.ProjectDeserializer;
|
import com.bartlomiejpluta.base.engine.project.serial.ProjectDeserializer;
|
||||||
import com.bartlomiejpluta.base.engine.util.reflection.ClassLoader;
|
import com.bartlomiejpluta.base.engine.util.reflection.ClassLoader;
|
||||||
@@ -33,6 +34,7 @@ public class DefaultContextManager implements ContextManager {
|
|||||||
private final FontManager fontManager;
|
private final FontManager fontManager;
|
||||||
private final EntityManager entityManager;
|
private final EntityManager entityManager;
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
|
private final ComponentInflater inflater;
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
@@ -59,6 +61,7 @@ public class DefaultContextManager implements ContextManager {
|
|||||||
.imageManager(imageManager)
|
.imageManager(imageManager)
|
||||||
.mapManager(mapManager)
|
.mapManager(mapManager)
|
||||||
.fontManager(fontManager)
|
.fontManager(fontManager)
|
||||||
|
.inflater(inflater)
|
||||||
.gameRunner(gameRunner)
|
.gameRunner(gameRunner)
|
||||||
.projectName(project.getName())
|
.projectName(project.getName())
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
|||||||
import com.bartlomiejpluta.base.engine.core.engine.GameEngine;
|
import com.bartlomiejpluta.base.engine.core.engine.GameEngine;
|
||||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||||
import com.bartlomiejpluta.base.engine.gui.render.NanoVGGUI;
|
import com.bartlomiejpluta.base.engine.gui.render.NanoVGGUI;
|
||||||
|
import com.bartlomiejpluta.base.engine.gui.xml.inflater.ComponentInflater;
|
||||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntityManager;
|
import com.bartlomiejpluta.base.engine.world.entity.manager.EntityManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.image.manager.ImageManager;
|
import com.bartlomiejpluta.base.engine.world.image.manager.ImageManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.map.manager.MapManager;
|
import com.bartlomiejpluta.base.engine.world.map.manager.MapManager;
|
||||||
@@ -45,6 +46,9 @@ public class DefaultContext implements Context {
|
|||||||
@NonNull
|
@NonNull
|
||||||
private final FontManager fontManager;
|
private final FontManager fontManager;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final ComponentInflater inflater;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@NonNull
|
@NonNull
|
||||||
private final GameRunner gameRunner;
|
private final GameRunner gameRunner;
|
||||||
@@ -100,7 +104,8 @@ public class DefaultContext implements Context {
|
|||||||
@Override
|
@Override
|
||||||
public GUI newGUI() {
|
public GUI newGUI() {
|
||||||
log.info("Creating new GUI");
|
log.info("Creating new GUI");
|
||||||
var gui = new NanoVGGUI(fontManager, imageManager);
|
var gui = new NanoVGGUI(this, fontManager, imageManager, inflater);
|
||||||
|
|
||||||
guis.add(gui);
|
guis.add(gui);
|
||||||
gui.init(screen);
|
gui.init(screen);
|
||||||
return gui;
|
return gui;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.engine.gui.render;
|
package com.bartlomiejpluta.base.engine.gui.render;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.*;
|
import com.bartlomiejpluta.base.api.game.gui.base.*;
|
||||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
@@ -8,6 +9,7 @@ import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
|||||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||||
import com.bartlomiejpluta.base.engine.gui.widget.ScreenWidget;
|
import com.bartlomiejpluta.base.engine.gui.widget.ScreenWidget;
|
||||||
|
import com.bartlomiejpluta.base.engine.gui.xml.inflater.ComponentInflater;
|
||||||
import com.bartlomiejpluta.base.engine.world.image.manager.ImageManager;
|
import com.bartlomiejpluta.base.engine.world.image.manager.ImageManager;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -28,10 +30,12 @@ import static org.lwjgl.system.MemoryUtil.NULL;
|
|||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
public class NanoVGGUI implements GUI {
|
public class NanoVGGUI implements GUI {
|
||||||
|
private final Context context;
|
||||||
private final FontManager fontManager;
|
private final FontManager fontManager;
|
||||||
private final ImageManager imageManager;
|
private final ImageManager imageManager;
|
||||||
|
private final ComponentInflater componentInflater;
|
||||||
|
|
||||||
private long context;
|
private long nvg;
|
||||||
private ScreenWidget screenWidget;
|
private ScreenWidget screenWidget;
|
||||||
|
|
||||||
private final List<NanoVGColor> colors = new LinkedList<>();
|
private final List<NanoVGColor> colors = new LinkedList<>();
|
||||||
@@ -40,9 +44,9 @@ public class NanoVGGUI implements GUI {
|
|||||||
private final Map<String, NanoVGImage> loadedImages = new HashMap<>();
|
private final Map<String, NanoVGImage> loadedImages = new HashMap<>();
|
||||||
|
|
||||||
public void init(Screen screen) {
|
public void init(Screen screen) {
|
||||||
context = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
|
nvg = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
|
||||||
|
|
||||||
if (context == NULL) {
|
if (nvg == NULL) {
|
||||||
throw new AppException("Could not init NanoVG");
|
throw new AppException("Could not init NanoVG");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,11 +55,11 @@ public class NanoVGGUI implements GUI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||||
nvgBeginFrame(context, screen.getWidth(), screen.getHeight(), 1);
|
nvgBeginFrame(nvg, screen.getWidth(), screen.getHeight(), 1);
|
||||||
|
|
||||||
screenWidget.draw(screen, this);
|
screenWidget.draw(screen, this);
|
||||||
|
|
||||||
nvgEndFrame(context);
|
nvgEndFrame(nvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -96,10 +100,10 @@ public class NanoVGGUI implements GUI {
|
|||||||
if (image == null) {
|
if (image == null) {
|
||||||
log.info("Loading GUI image with UID: [{}] into cache under the key: [{}]", imageUid, key);
|
log.info("Loading GUI image with UID: [{}] into cache under the key: [{}]", imageUid, key);
|
||||||
var data = imageManager.loadObjectByteBuffer(imageUid);
|
var data = imageManager.loadObjectByteBuffer(imageUid);
|
||||||
var handle = nvgCreateImageMem(context, imageFlags, data);
|
var handle = nvgCreateImageMem(nvg, imageFlags, data);
|
||||||
var width = new int[1];
|
var width = new int[1];
|
||||||
var height = new int[1];
|
var height = new int[1];
|
||||||
nvgImageSize(context, handle, width, height);
|
nvgImageSize(nvg, handle, width, height);
|
||||||
|
|
||||||
log.info("GUI image with UID: [{}], size {}x{} and flags [0b{}] has been loaded", imageUid, width[0], height[0], toBinaryString(imageFlags));
|
log.info("GUI image with UID: [{}], size {}x{} and flags [0b{}] has been loaded", imageUid, width[0], height[0], toBinaryString(imageFlags));
|
||||||
image = new NanoVGImage(handle, width[0], height[0]);
|
image = new NanoVGImage(handle, width[0], height[0]);
|
||||||
@@ -112,62 +116,62 @@ public class NanoVGGUI implements GUI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginPath() {
|
public void beginPath() {
|
||||||
nvgBeginPath(context);
|
nvgBeginPath(nvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void closePath() {
|
public void closePath() {
|
||||||
nvgClosePath(context);
|
nvgClosePath(nvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawRectangle(float x, float y, float width, float height) {
|
public void drawRectangle(float x, float y, float width, float height) {
|
||||||
nvgRect(context, x, y, width, height);
|
nvgRect(nvg, x, y, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawRoundedRectangle(float x, float y, float width, float height, float radius) {
|
public void drawRoundedRectangle(float x, float y, float width, float height, float radius) {
|
||||||
nvgRoundedRect(context, x, y, width, height, radius);
|
nvgRoundedRect(nvg, x, y, width, height, radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawCircle(float x, float y, float radius) {
|
public void drawCircle(float x, float y, float radius) {
|
||||||
nvgCircle(context, x, y, radius);
|
nvgCircle(nvg, x, y, radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawEllipse(float x, float y, float radiusX, float radiusY) {
|
public void drawEllipse(float x, float y, float radiusX, float radiusY) {
|
||||||
nvgEllipse(context, x, y, radiusX, radiusY);
|
nvgEllipse(nvg, x, y, radiusX, radiusY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawArc(float x, float y, float radius, float start, float end, WindingDirection direction) {
|
public void drawArc(float x, float y, float radius, float start, float end, WindingDirection direction) {
|
||||||
nvgArc(context, x, y, radius, start, end, direction == WindingDirection.CLOCKWISE ? NVG_CW : NVG_CCW);
|
nvgArc(nvg, x, y, radius, start, end, direction == WindingDirection.CLOCKWISE ? NVG_CW : NVG_CCW);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawArcTo(float x1, float y1, float x2, float y2, float radius) {
|
public void drawArcTo(float x1, float y1, float x2, float y2, float radius) {
|
||||||
nvgArcTo(context, x1, y1, x2, y2, radius);
|
nvgArcTo(nvg, x1, y1, x2, y2, radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawLineTo(float x, float y) {
|
public void drawLineTo(float x, float y) {
|
||||||
nvgLineTo(context, x, y);
|
nvgLineTo(nvg, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawBezierTo(float controlX1, float controlY1, float controlX2, float controlY2, float targetX, float targetY) {
|
public void drawBezierTo(float controlX1, float controlY1, float controlX2, float controlY2, float targetX, float targetY) {
|
||||||
nvgBezierTo(context, controlX1, controlY1, controlX2, controlY2, targetX, targetY);
|
nvgBezierTo(nvg, controlX1, controlY1, controlX2, controlY2, targetX, targetY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawQuadBezierTo(float controlX, float controlY, float targetX, float targetY) {
|
public void drawQuadBezierTo(float controlX, float controlY, float targetX, float targetY) {
|
||||||
nvgQuadTo(context, controlX, controlY, targetX, targetY);
|
nvgQuadTo(nvg, controlX, controlY, targetX, targetY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLineCap(LineCap cap) {
|
public void setLineCap(LineCap cap) {
|
||||||
nvgLineCap(context, lineCapToNanoVGInteger(cap));
|
nvgLineCap(nvg, lineCapToNanoVGInteger(cap));
|
||||||
}
|
}
|
||||||
|
|
||||||
private int lineCapToNanoVGInteger(LineCap cap) {
|
private int lineCapToNanoVGInteger(LineCap cap) {
|
||||||
@@ -182,58 +186,58 @@ public class NanoVGGUI implements GUI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLineJoin(LineCap join) {
|
public void setLineJoin(LineCap join) {
|
||||||
nvgLineJoin(context, lineCapToNanoVGInteger(join));
|
nvgLineJoin(nvg, lineCapToNanoVGInteger(join));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void moveTo(float x, float y) {
|
public void moveTo(float x, float y) {
|
||||||
nvgMoveTo(context, x, y);
|
nvgMoveTo(nvg, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setGlobalAlpha(float alpha) {
|
public void setGlobalAlpha(float alpha) {
|
||||||
nvgGlobalAlpha(context, alpha);
|
nvgGlobalAlpha(nvg, alpha);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPathWinding(WindingDirection direction) {
|
public void setPathWinding(WindingDirection direction) {
|
||||||
nvgPathWinding(context, direction == WindingDirection.CLOCKWISE ? NVG_CW : NVG_CCW);
|
nvgPathWinding(nvg, direction == WindingDirection.CLOCKWISE ? NVG_CW : NVG_CCW);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFillColor(Color color) {
|
public void setFillColor(Color color) {
|
||||||
nvgFillColor(context, ((NanoVGColor) color).getColor());
|
nvgFillColor(nvg, ((NanoVGColor) color).getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFillPaint(Paint paint) {
|
public void setFillPaint(Paint paint) {
|
||||||
nvgFillPaint(context, ((NanoVGPaint) paint).getPaint());
|
nvgFillPaint(nvg, ((NanoVGPaint) paint).getPaint());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fill() {
|
public void fill() {
|
||||||
nvgFill(context);
|
nvgFill(nvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStrokeColor(Color color) {
|
public void setStrokeColor(Color color) {
|
||||||
nvgStrokeColor(context, ((NanoVGColor) color).getColor());
|
nvgStrokeColor(nvg, ((NanoVGColor) color).getColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStrokePaint(Paint paint) {
|
public void setStrokePaint(Paint paint) {
|
||||||
nvgStrokePaint(context, ((NanoVGPaint) paint).getPaint());
|
nvgStrokePaint(nvg, ((NanoVGPaint) paint).getPaint());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stroke() {
|
public void stroke() {
|
||||||
nvgStroke(context);
|
nvgStroke(nvg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void boxGradient(float x, float y, float width, float height, float radius, float feather, Color inner, Color outer, Paint target) {
|
public void boxGradient(float x, float y, float width, float height, float radius, float feather, Color inner, Color outer, Paint target) {
|
||||||
nvgBoxGradient(
|
nvgBoxGradient(
|
||||||
context,
|
nvg,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
width,
|
width,
|
||||||
@@ -249,7 +253,7 @@ public class NanoVGGUI implements GUI {
|
|||||||
@Override
|
@Override
|
||||||
public void linearGradient(float x, float y, float endX, float endY, Color start, Color end, Paint target) {
|
public void linearGradient(float x, float y, float endX, float endY, Color start, Color end, Paint target) {
|
||||||
nvgLinearGradient(
|
nvgLinearGradient(
|
||||||
context,
|
nvg,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
endX,
|
endX,
|
||||||
@@ -263,7 +267,7 @@ public class NanoVGGUI implements GUI {
|
|||||||
@Override
|
@Override
|
||||||
public void radialGradient(float x, float y, float innerRadius, float outerRadius, Color start, Color end, Paint target) {
|
public void radialGradient(float x, float y, float innerRadius, float outerRadius, Color start, Color end, Paint target) {
|
||||||
nvgRadialGradient(
|
nvgRadialGradient(
|
||||||
context,
|
nvg,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
innerRadius,
|
innerRadius,
|
||||||
@@ -277,7 +281,7 @@ public class NanoVGGUI implements GUI {
|
|||||||
@Override
|
@Override
|
||||||
public void imagePattern(float x, float y, float angle, float alpha, Image image, Paint target) {
|
public void imagePattern(float x, float y, float angle, float alpha, Image image, Paint target) {
|
||||||
nvgImagePattern(
|
nvgImagePattern(
|
||||||
context,
|
nvg,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
image.getWidth(),
|
image.getWidth(),
|
||||||
@@ -292,7 +296,7 @@ public class NanoVGGUI implements GUI {
|
|||||||
@Override
|
@Override
|
||||||
public void imagePattern(float x, float y, float width, float height, float angle, float alpha, Image image, Paint target) {
|
public void imagePattern(float x, float y, float width, float height, float angle, float alpha, Image image, Paint target) {
|
||||||
nvgImagePattern(
|
nvgImagePattern(
|
||||||
context,
|
nvg,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
width,
|
width,
|
||||||
@@ -306,65 +310,65 @@ public class NanoVGGUI implements GUI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStrokeWidth(float width) {
|
public void setStrokeWidth(float width) {
|
||||||
nvgStrokeWidth(context, width);
|
nvgStrokeWidth(nvg, width);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putText(float x, float y, CharSequence text, float[] outTextBounds) {
|
public void putText(float x, float y, CharSequence text, float[] outTextBounds) {
|
||||||
nvgText(context, x, y, text);
|
nvgText(nvg, x, y, text);
|
||||||
nvgTextBounds(context, x, y, text, outTextBounds);
|
nvgTextBounds(nvg, x, y, text, outTextBounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putText(float x, float y, CharSequence text) {
|
public void putText(float x, float y, CharSequence text) {
|
||||||
nvgText(context, x, y, text);
|
nvgText(nvg, x, y, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putTextBox(float x, float y, float lineWidth, CharSequence text, float[] outTextBounds) {
|
public void putTextBox(float x, float y, float lineWidth, CharSequence text, float[] outTextBounds) {
|
||||||
nvgTextBox(context, x, y, lineWidth, text);
|
nvgTextBox(nvg, x, y, lineWidth, text);
|
||||||
nvgTextBoxBounds(context, x, y, lineWidth, text, outTextBounds);
|
nvgTextBoxBounds(nvg, x, y, lineWidth, text, outTextBounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putTextBox(float x, float y, float lineWidth, CharSequence text) {
|
public void putTextBox(float x, float y, float lineWidth, CharSequence text) {
|
||||||
nvgTextBox(context, x, y, lineWidth, text);
|
nvgTextBox(nvg, x, y, lineWidth, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFontFace(String fontUid) {
|
public void setFontFace(String fontUid) {
|
||||||
if (!loadedFonts.contains(fontUid)) {
|
if (!loadedFonts.contains(fontUid)) {
|
||||||
var fontBuffer = fontManager.loadObjectByteBuffer(fontUid);
|
var fontBuffer = fontManager.loadObjectByteBuffer(fontUid);
|
||||||
nvgCreateFontMem(context, fontUid, fontBuffer, 0);
|
nvgCreateFontMem(nvg, fontUid, fontBuffer, 0);
|
||||||
loadedFonts.add(fontUid);
|
loadedFonts.add(fontUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
nvgFontFace(context, fontUid);
|
nvgFontFace(nvg, fontUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFontSize(float size) {
|
public void setFontSize(float size) {
|
||||||
nvgFontSize(context, size);
|
nvgFontSize(nvg, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFontBlur(float blur) {
|
public void setFontBlur(float blur) {
|
||||||
nvgFontBlur(context, blur);
|
nvgFontBlur(nvg, blur);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setTextAlignment(int alignment) {
|
public void setTextAlignment(int alignment) {
|
||||||
nvgTextAlign(context, alignment);
|
nvgTextAlign(nvg, alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setTextLineHeight(float textLineHeight) {
|
public void setTextLineHeight(float textLineHeight) {
|
||||||
nvgTextLineHeight(context, textLineHeight);
|
nvgTextLineHeight(nvg, textLineHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clip(float x, float y, float width, float height) {
|
public void clip(float x, float y, float width, float height) {
|
||||||
nvgScissor(context, x, y, width, height);
|
nvgScissor(nvg, x, y, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -383,10 +387,10 @@ public class NanoVGGUI implements GUI {
|
|||||||
log.info("Disposed {} GUI paint buffers", paints.size());
|
log.info("Disposed {} GUI paint buffers", paints.size());
|
||||||
|
|
||||||
log.info("Disposing GUI images");
|
log.info("Disposing GUI images");
|
||||||
loadedImages.values().stream().map(NanoVGImage::getImageHandle).forEach(h -> nvgDeleteImage(context, h));
|
loadedImages.values().stream().map(NanoVGImage::getImageHandle).forEach(h -> nvgDeleteImage(nvg, h));
|
||||||
log.info("Disposed {} GUI images", loadedImages.size());
|
log.info("Disposed {} GUI images", loadedImages.size());
|
||||||
|
|
||||||
log.info("Disposing GUI context");
|
log.info("Disposing GUI nvg");
|
||||||
nvgDelete(context);
|
nvgDelete(nvg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.engine.gui.xml.inflater;
|
package com.bartlomiejpluta.base.engine.gui.xml.inflater;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
|
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.component.Component;
|
import com.bartlomiejpluta.base.api.game.gui.component.Component;
|
||||||
@@ -33,25 +34,25 @@ public class ComponentInflater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public Component inflate(String xml) {
|
public Component inflate(String xml, Context context, GUI gui) {
|
||||||
var document = builder.parse(xml);
|
var document = builder.parse(xml);
|
||||||
return parseNode(document.getDocumentElement());
|
return parseNode(document.getDocumentElement(), context, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public Component inflate(File file) {
|
public Component inflate(File file, Context context, GUI gui) {
|
||||||
var document = builder.parse(file);
|
var document = builder.parse(file);
|
||||||
return parseNode(document.getDocumentElement());
|
return parseNode(document.getDocumentElement(), context, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public Component inflate(InputStream is) {
|
public Component inflate(InputStream is, Context context, GUI gui) {
|
||||||
var document = builder.parse(is);
|
var document = builder.parse(is);
|
||||||
return parseNode(document.getDocumentElement());
|
return parseNode(document.getDocumentElement(), context, gui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private Component parseNode(Node node) {
|
private Component parseNode(Node node, Context context, GUI gui) {
|
||||||
var uri = node.getNamespaceURI();
|
var uri = node.getNamespaceURI();
|
||||||
var name = node.getLocalName();
|
var name = node.getLocalName();
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ public class ComponentInflater {
|
|||||||
var canonicalName = name.replaceAll("\\*", "").replaceAll("\\.+", ".");
|
var canonicalName = name.replaceAll("\\*", "").replaceAll("\\.+", ".");
|
||||||
|
|
||||||
var componentClass = loader.loadClass(canonicalName);
|
var componentClass = loader.loadClass(canonicalName);
|
||||||
var component = createComponent(componentClass, node.getAttributes());
|
var component = createComponent(componentClass, node.getAttributes(), context, gui);
|
||||||
|
|
||||||
var children = node.getChildNodes();
|
var children = node.getChildNodes();
|
||||||
|
|
||||||
@@ -84,15 +85,15 @@ public class ComponentInflater {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
component.add(parseNode(childNode));
|
component.add(parseNode(childNode, context, gui));
|
||||||
}
|
}
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private Component createComponent(Class<?> componentClass, NamedNodeMap attributes) {
|
private Component createComponent(Class<?> componentClass, NamedNodeMap attributes, Context context, GUI gui) {
|
||||||
var component = (Component) componentClass.getConstructor().newInstance();
|
var component = (Component) componentClass.getConstructor(Context.class, GUI.class).newInstance(context, gui);
|
||||||
|
|
||||||
// Set attributes via setter methods
|
// Set attributes via setter methods
|
||||||
for (int i = 0; i < attributes.getLength(); ++i) {
|
for (int i = 0; i < attributes.getLength(); ++i) {
|
||||||
|
|||||||
Reference in New Issue
Block a user