Create support for NVG Colors
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||||
|
|
||||||
|
public interface Color {
|
||||||
|
void setRGB(float red, float green, float blue);
|
||||||
|
|
||||||
|
void setRGBA(float red, float green, float blue, float alpha);
|
||||||
|
|
||||||
|
void setRed(float value);
|
||||||
|
|
||||||
|
void setGreen(float value);
|
||||||
|
|
||||||
|
void setBlue(float value);
|
||||||
|
|
||||||
|
void setAlpha(float value);
|
||||||
|
|
||||||
|
float getRed();
|
||||||
|
|
||||||
|
float getGreen();
|
||||||
|
|
||||||
|
float getBlue();
|
||||||
|
|
||||||
|
float getAlpha();
|
||||||
|
}
|
||||||
@@ -17,6 +17,8 @@ public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
|||||||
|
|
||||||
void setRoot(Widget root);
|
void setRoot(Widget root);
|
||||||
|
|
||||||
|
Color createColor();
|
||||||
|
|
||||||
void beginPath();
|
void beginPath();
|
||||||
|
|
||||||
void closePath();
|
void closePath();
|
||||||
@@ -67,11 +69,11 @@ public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
|||||||
|
|
||||||
void setGlobalAlpha(float alpha);
|
void setGlobalAlpha(float alpha);
|
||||||
|
|
||||||
void setFillColor(float red, float green, float blue, float alpha);
|
void setFillColor(Color color);
|
||||||
|
|
||||||
void fill();
|
void fill();
|
||||||
|
|
||||||
void setStrokeColor(float red, float green, float blue, float alpha);
|
void setStrokeColor(Color color);
|
||||||
|
|
||||||
void setStrokeWidth(float width);
|
void setStrokeWidth(float width);
|
||||||
|
|
||||||
|
|||||||
@@ -1,54 +1,27 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
public class Label extends BaseComponent {
|
public class Label extends BaseComponent {
|
||||||
private String text;
|
private final GUI gui;
|
||||||
|
|
||||||
|
private String text = "";
|
||||||
private String font;
|
private String font;
|
||||||
|
|
||||||
private float fontSize;
|
private float fontSize;
|
||||||
|
private int alignment = GUI.ALIGN_LEFT;
|
||||||
private int alignment;
|
private final Color color;
|
||||||
|
|
||||||
private float red;
|
|
||||||
private float green;
|
|
||||||
private float blue;
|
|
||||||
private float alpha;
|
|
||||||
|
|
||||||
private final float[] bounds = new float[4];
|
private final float[] bounds = new float[4];
|
||||||
|
|
||||||
public Label(String font) {
|
public Label(GUI gui, String font) {
|
||||||
this(font, "", 10f, GUI.ALIGN_LEFT, 1.0f, 1.0f, 1.0f, 1.0f);
|
this.gui = requireNonNull(gui);
|
||||||
}
|
|
||||||
|
|
||||||
public Label(String font, String text) {
|
|
||||||
this(font, text, 10f, GUI.ALIGN_LEFT, 1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Label(String font, String text, float fontSize) {
|
|
||||||
this(font, text, fontSize, GUI.ALIGN_LEFT, 1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Label(String font, String text, float fontSize, int alignment) {
|
|
||||||
this(font, text, fontSize, alignment, 1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Label(String font, String text, float fontSize, int alignment, float red, float green, float blue) {
|
|
||||||
this(font, text, fontSize, alignment, red, green, blue, 1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Label(String font, String text, float fontSize, int alignment, float red, float green, float blue, float alpha) {
|
|
||||||
this.text = requireNonNull(text);
|
|
||||||
this.font = requireNonNull(font);
|
this.font = requireNonNull(font);
|
||||||
this.fontSize = fontSize;
|
|
||||||
this.alignment = alignment;
|
this.color = this.gui.createColor();
|
||||||
this.red = red;
|
|
||||||
this.green = green;
|
|
||||||
this.blue = blue;
|
|
||||||
this.alpha = alpha;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getText() {
|
public String getText() {
|
||||||
@@ -84,48 +57,43 @@ public class Label extends BaseComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public float getRed() {
|
public float getRed() {
|
||||||
return red;
|
return color.getRed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRed(float red) {
|
public void setRed(float value) {
|
||||||
this.red = red;
|
color.setRed(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getGreen() {
|
public float getGreen() {
|
||||||
return green;
|
return color.getGreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGreen(float green) {
|
public void setGreen(float value) {
|
||||||
this.green = green;
|
color.setGreen(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getBlue() {
|
public float getBlue() {
|
||||||
return blue;
|
return color.getBlue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBlue(float blue) {
|
public void setBlue(float value) {
|
||||||
this.blue = blue;
|
color.setBlue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getAlpha() {
|
public float getAlpha() {
|
||||||
return alpha;
|
return color.getAlpha();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlpha(float alpha) {
|
public void setAlpha(float value) {
|
||||||
this.alpha = alpha;
|
color.setAlpha(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(float red, float green, float blue, float alpha) {
|
public void setColor(float red, float green, float blue, float alpha) {
|
||||||
this.red = red;
|
color.setRGBA(red, green, blue, alpha);
|
||||||
this.green = green;
|
|
||||||
this.blue = blue;
|
|
||||||
this.alpha = alpha;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(float red, float green, float blue) {
|
public void setColor(float red, float green, float blue) {
|
||||||
this.red = red;
|
color.setRGB(red, green, blue);
|
||||||
this.green = green;
|
|
||||||
this.blue = blue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -144,7 +112,7 @@ public class Label extends BaseComponent {
|
|||||||
gui.setFontFace(font);
|
gui.setFontFace(font);
|
||||||
gui.setTextAlignment(alignment);
|
gui.setTextAlignment(alignment);
|
||||||
gui.setFontSize(fontSize);
|
gui.setFontSize(fontSize);
|
||||||
gui.setFillColor(red, green, blue, alpha);
|
gui.setFillColor(color);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
gui.putTextBox(x + paddingLeft, y + paddingTop, getWidth() - paddingLeft - paddingRight, text, bounds);
|
gui.putTextBox(x + paddingLeft, y + paddingTop, getWidth() - paddingLeft - paddingRight, text, bounds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.bartlomiejpluta.base.engine.gui.render;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.gui.base.Color;
|
||||||
|
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||||
|
import lombok.*;
|
||||||
|
import org.lwjgl.nanovg.NVGColor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class NanoVGColor implements Color, Disposable {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final NVGColor color;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRGB(float red, float green, float blue) {
|
||||||
|
color.r(red);
|
||||||
|
color.g(green);
|
||||||
|
color.b(blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRGBA(float red, float green, float blue, float alpha) {
|
||||||
|
color.r(red);
|
||||||
|
color.g(green);
|
||||||
|
color.b(blue);
|
||||||
|
color.a(alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRed(float value) {
|
||||||
|
color.r(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setGreen(float value) {
|
||||||
|
color.g(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBlue(float value) {
|
||||||
|
color.b(value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAlpha(float value) {
|
||||||
|
color.a(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getRed() {
|
||||||
|
return color.r();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getGreen() {
|
||||||
|
return color.g();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getBlue() {
|
||||||
|
return color.b();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getAlpha() {
|
||||||
|
return color.a();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
color.free();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +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.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.*;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.LineCap;
|
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.Widget;
|
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.WindingDirection;
|
|
||||||
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;
|
||||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||||
@@ -14,19 +11,17 @@ import com.bartlomiejpluta.base.engine.gui.widget.ScreenWidget;
|
|||||||
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;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.lwjgl.nanovg.NVGColor;
|
import org.lwjgl.nanovg.NVGColor;
|
||||||
import org.lwjgl.nanovg.NVGPaint;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.lwjgl.nanovg.NanoVG.*;
|
import static org.lwjgl.nanovg.NanoVG.*;
|
||||||
import static org.lwjgl.nanovg.NanoVGGL3.*;
|
import static org.lwjgl.nanovg.NanoVGGL3.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Getter
|
@Getter
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
public class NanoVGGUI implements GUI {
|
public class NanoVGGUI implements GUI {
|
||||||
@@ -36,11 +31,7 @@ public class NanoVGGUI implements GUI {
|
|||||||
private long context;
|
private long context;
|
||||||
private ScreenWidget screenWidget;
|
private ScreenWidget screenWidget;
|
||||||
|
|
||||||
private NVGColor fillColor;
|
private final List<NanoVGColor> colors = new LinkedList<>();
|
||||||
private NVGColor strokeColor;
|
|
||||||
private NVGPaint fillPaint;
|
|
||||||
private NVGPaint strokePaint;
|
|
||||||
|
|
||||||
private final Set<String> loadedFonts = new HashSet<>();
|
private final Set<String> loadedFonts = new HashSet<>();
|
||||||
private final Map<String, Integer> loadedImages = new HashMap<>();
|
private final Map<String, Integer> loadedImages = new HashMap<>();
|
||||||
|
|
||||||
@@ -52,11 +43,6 @@ public class NanoVGGUI implements GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
screenWidget = new ScreenWidget(screen);
|
screenWidget = new ScreenWidget(screen);
|
||||||
|
|
||||||
fillColor = NVGColor.create();
|
|
||||||
strokeColor = NVGColor.create();
|
|
||||||
fillPaint = NVGPaint.create();
|
|
||||||
strokePaint = NVGPaint.create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -79,6 +65,14 @@ public class NanoVGGUI implements GUI {
|
|||||||
root.setParent(screenWidget);
|
root.setParent(screenWidget);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Color createColor() {
|
||||||
|
log.info("Creating new GUI color");
|
||||||
|
var color = new NanoVGColor(NVGColor.create());
|
||||||
|
colors.add(color);
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beginPath() {
|
public void beginPath() {
|
||||||
nvgBeginPath(context);
|
nvgBeginPath(context);
|
||||||
@@ -170,13 +164,8 @@ public class NanoVGGUI implements GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setFillColor(float red, float green, float blue, float alpha) {
|
public void setFillColor(Color color) {
|
||||||
fillColor.r(red);
|
nvgFillColor(context, ((NanoVGColor) color).getColor());
|
||||||
fillColor.g(green);
|
|
||||||
fillColor.b(blue);
|
|
||||||
fillColor.a(alpha);
|
|
||||||
|
|
||||||
nvgFillColor(context, fillColor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -185,13 +174,8 @@ public class NanoVGGUI implements GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStrokeColor(float red, float green, float blue, float alpha) {
|
public void setStrokeColor(Color color) {
|
||||||
strokeColor.r(red);
|
nvgStrokeColor(context, ((NanoVGColor) color).getColor());
|
||||||
strokeColor.g(green);
|
|
||||||
strokeColor.b(blue);
|
|
||||||
strokeColor.a(alpha);
|
|
||||||
|
|
||||||
nvgStrokeColor(context, strokeColor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -269,10 +253,11 @@ public class NanoVGGUI implements GUI {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
fillColor.free();
|
log.info("Disposing GUI color buffers");
|
||||||
strokeColor.free();
|
colors.forEach(NanoVGColor::dispose);
|
||||||
fillPaint.free();
|
log.info("Disposed {} GUI colors", colors.size());
|
||||||
strokePaint.free();
|
|
||||||
|
log.info("Disposing GUI context");
|
||||||
nvgDelete(context);
|
nvgDelete(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user