From b80632c705c8218ce6c6ba21a377c0bbd0df6371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 11 Mar 2021 18:52:07 +0100 Subject: [PATCH] Delegate some extra methods from NanoVG to GUI --- .../base/api/game/gui/base/GUI.java | 42 ++++- .../base/api/game/gui/base/LineCap.java | 9 + .../api/game/gui/base/WindingDirection.java | 6 + .../base/engine/gui/render/NanoVGGUI.java | 159 +++++++++++++++--- 4 files changed, 186 insertions(+), 30 deletions(-) create mode 100644 api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/LineCap.java create mode 100644 api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/WindingDirection.java diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/GUI.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/GUI.java index 892add8c..bf5a7a51 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/GUI.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/GUI.java @@ -18,18 +18,44 @@ public interface GUI extends Renderable, Disposable { void beginPath(); - void drawRectangle(float x, float y, float w, float h); + void closePath(); - void fillColor(float red, float green, float blue, float alpha); + void drawRectangle(float x, float y, float width, float height); - void strokeColor(float red, float green, float blue, float alpha); + void drawRoundedRectangle(float x, float y, float width, float height, float radius); + + void drawCircle(float x, float y, float radius); + + void drawEllipse(float x, float y, float radiusX, float radiusY); + + void drawArc(float x, float y, float radius, float start, float end, WindingDirection direction); + + void drawArcTo(float x1, float y1, float x2, float y2, float radius); + + void drawLineTo(float x, float y); + + void drawBezierTo(float controlX1, float controlY1, float controlX2, float controlY2, float targetX, float targetY); + + void drawQuadBezierTo(float controlX, float controlY, float targetX, float targetY); + + void setLineCap(LineCap cap); + + void setLineJoin(LineCap join); + + void moveTo(float x, float y); + + void setPathWinding(WindingDirection direction); void setFontFace(String fontUid); void setFontSize(float size); + void setFontBlur(float blur); + void setTextAlignment(int alignment); + void setTextLineHeight(float textLineHeight); + void putText(float x, float y, CharSequence text, float[] outTextBounds); void putText(float x, float y, CharSequence text); @@ -37,4 +63,14 @@ public interface GUI extends Renderable, Disposable { void putTextBox(float x, float y, float lineWidth, CharSequence text, float[] outTextBounds); void putTextBox(float x, float y, float lineWidth, CharSequence text); + + void setGlobalAlpha(float alpha); + + void fillColor(float red, float green, float blue, float alpha); + + void setStrokeWidth(float width); + + void strokeColor(float red, float green, float blue, float alpha); + + void clip(float x, float y, float width, float height); } diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/LineCap.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/LineCap.java new file mode 100644 index 00000000..e4ca53d3 --- /dev/null +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/LineCap.java @@ -0,0 +1,9 @@ +package com.bartlomiejpluta.base.api.game.gui.base; + +public enum LineCap { + BUTT, + ROUND, + SQUARE, + BEVEL, + MITER +} diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/WindingDirection.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/WindingDirection.java new file mode 100644 index 00000000..6afb20db --- /dev/null +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/WindingDirection.java @@ -0,0 +1,6 @@ +package com.bartlomiejpluta.base.api.game.gui.base; + +public enum WindingDirection { + CLOCKWISE, + COUNTER_CLOCKWISE +} diff --git a/engine/src/main/java/com/bartlomiejpluta/base/engine/gui/render/NanoVGGUI.java b/engine/src/main/java/com/bartlomiejpluta/base/engine/gui/render/NanoVGGUI.java index e571fc31..4b9d5a49 100644 --- a/engine/src/main/java/com/bartlomiejpluta/base/engine/gui/render/NanoVGGUI.java +++ b/engine/src/main/java/com/bartlomiejpluta/base/engine/gui/render/NanoVGGUI.java @@ -2,7 +2,9 @@ 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.screen.Screen; import com.bartlomiejpluta.base.api.internal.render.ShaderManager; import com.bartlomiejpluta.base.engine.error.AppException; @@ -25,7 +27,8 @@ public class NanoVGGUI implements GUI { @Setter private Widget root; - private NVGColor color; + private NVGColor fillColor; + private NVGColor strokeColor; private final Set loadedFonts = new HashSet<>(); private final float[] boundBuffer = new float[4]; @@ -38,7 +41,8 @@ public class NanoVGGUI implements GUI { public void init(Screen screen) { context = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES); - color = NVGColor.create(); + fillColor = NVGColor.create(); + strokeColor = NVGColor.create(); if (context == NULL) { throw new AppException("Could not onCreate NanoVG"); @@ -62,8 +66,115 @@ public class NanoVGGUI implements GUI { } @Override - public void drawRectangle(float x, float y, float w, float h) { - nvgRect(context, x, y, w, h); + public void closePath() { + nvgClosePath(context); + } + + @Override + public void drawRectangle(float x, float y, float width, float height) { + nvgRect(context, x, y, width, height); + } + + @Override + public void drawRoundedRectangle(float x, float y, float width, float height, float radius) { + nvgRoundedRect(context, x, y, width, height, radius); + } + + @Override + public void drawCircle(float x, float y, float radius) { + nvgCircle(context, x, y, radius); + } + + @Override + public void drawEllipse(float x, float y, float radiusX, float radiusY) { + nvgEllipse(context, x, y, radiusX, radiusY); + } + + @Override + 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); + } + + @Override + public void drawArcTo(float x1, float y1, float x2, float y2, float radius) { + nvgArcTo(context, x1, y1, x2, y2, radius); + } + + @Override + public void drawLineTo(float x, float y) { + nvgLineTo(context, x, y); + } + + @Override + public void drawBezierTo(float controlX1, float controlY1, float controlX2, float controlY2, float targetX, float targetY) { + nvgBezierTo(context, controlX1, controlY1, controlX2, controlY2, targetX, targetY); + } + + @Override + public void drawQuadBezierTo(float controlX, float controlY, float targetX, float targetY) { + nvgQuadTo(context, controlX, controlY, targetX, targetY); + } + + @Override + public void setLineCap(LineCap cap) { + nvgLineCap(context, lineCapToNanoVGInteger(cap)); + } + + private int lineCapToNanoVGInteger(LineCap cap) { + return switch (cap) { + case BUTT -> NVG_BUTT; + case ROUND -> NVG_ROUND; + case SQUARE -> NVG_SQUARE; + case BEVEL -> NVG_BEVEL; + case MITER -> NVG_MITER; + }; + } + + @Override + public void setLineJoin(LineCap join) { + nvgLineJoin(context, lineCapToNanoVGInteger(join)); + } + + @Override + public void moveTo(float x, float y) { + nvgMoveTo(context, x, y); + } + + @Override + public void setGlobalAlpha(float alpha) { + nvgGlobalAlpha(context, alpha); + } + + @Override + public void setPathWinding(WindingDirection direction) { + nvgPathWinding(context, direction == WindingDirection.CLOCKWISE ? NVG_CW : NVG_CCW); + } + + @Override + public void fillColor(float red, float green, float blue, float alpha) { + fillColor.r(red); + fillColor.g(green); + fillColor.b(blue); + fillColor.a(alpha); + + nvgFillColor(context, fillColor); + nvgFill(context); + } + + @Override + public void strokeColor(float red, float green, float blue, float alpha) { + strokeColor.r(red); + strokeColor.g(green); + strokeColor.b(blue); + strokeColor.a(alpha); + + nvgStrokeColor(context, strokeColor); + nvgStroke(context); + } + + @Override + public void setStrokeWidth(float width) { + nvgStrokeWidth(context, width); } @Override @@ -88,28 +199,6 @@ public class NanoVGGUI implements GUI { nvgTextBox(context, x, y, lineWidth, text); } - @Override - public void fillColor(float red, float green, float blue, float alpha) { - color.r(red); - color.g(green); - color.b(blue); - color.a(alpha); - - nvgFillColor(context, color); - nvgFill(context); - } - - @Override - public void strokeColor(float red, float green, float blue, float alpha) { - color.r(red); - color.g(green); - color.b(blue); - color.a(alpha); - - nvgStrokeColor(context, color); - nvgFill(context); - } - @Override public void setFontFace(String fontUid) { if (!loadedFonts.contains(fontUid)) { @@ -126,14 +215,30 @@ public class NanoVGGUI implements GUI { nvgFontSize(context, size); } + @Override + public void setFontBlur(float blur) { + nvgFontBlur(context, blur); + } + @Override public void setTextAlignment(int alignment) { nvgTextAlign(context, alignment); } + @Override + public void setTextLineHeight(float textLineHeight) { + nvgTextLineHeight(context, textLineHeight); + } + + @Override + public void clip(float x, float y, float width, float height) { + nvgScissor(context, x, y, width, height); + } + @Override public void dispose() { - color.free(); + fillColor.free(); + strokeColor.free(); nvgDelete(context); } }