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