From 057870d7d8c3080174e4cca2a7ce2e57d80829d9 Mon Sep 17 00:00:00 2001 From: Gabriel <97042217+GabrielBRDeveloper@users.noreply.github.com> Date: Fri, 26 Apr 2024 14:34:57 -0400 Subject: [PATCH] NinePath implement --- .github/workflows/build-linux.yml | 1 + .../mux/core/texel/CanvasTexel.java | 12 ++++++- src/br/nullexcept/mux/core/texel/VgTexel.java | 2 +- src/br/nullexcept/mux/graphics/Canvas.java | 2 ++ .../graphics/drawable/NinePathDrawable.java | 36 +++++++++++++++++++ src/br/nullexcept/mux/res/Parser.java | 29 +++++++++++---- 6 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 src/br/nullexcept/mux/graphics/drawable/NinePathDrawable.java diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml index 7d927f7..3110c05 100644 --- a/.github/workflows/build-linux.yml +++ b/.github/workflows/build-linux.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - dev pull_request: jobs: diff --git a/src/br/nullexcept/mux/core/texel/CanvasTexel.java b/src/br/nullexcept/mux/core/texel/CanvasTexel.java index 9033bd1..0b4edf7 100644 --- a/src/br/nullexcept/mux/core/texel/CanvasTexel.java +++ b/src/br/nullexcept/mux/core/texel/CanvasTexel.java @@ -120,15 +120,25 @@ public void drawPath(Path path, int x, int y, Paint paint) { @Override public void drawBitmap(int x, int y, int width, int height, Bitmap bitmap, Paint paint) { + drawBitmap(x,y,width,height, 0,0, bitmap.getWidth(), getHeight(), bitmap,paint); + } + + @Override + public void drawBitmap(int x, int y, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight, Bitmap bitmap, Paint paint) { if (!(bitmap instanceof TexelBitmap)){ throw new IllegalArgumentException("Invalid bitmap, bitmap core and canvas core is different!"); } TexelBitmap texelBitmap = (TexelBitmap)bitmap; VgTexel.beginElement(); - VgTexel.drawImage(texelBitmap,x,y,width,height, 0,0, texelBitmap.getWidth(), texelBitmap.getHeight()); + VgTexel.drawImage(texelBitmap,x,y,width,height, srcX, srcY,srcWidth, srcHeight); VgTexel.endElement(); } + @Override + public void drawBitmap(Rect rect, Rect source, Bitmap bitmap, Paint paint) { + drawBitmap(rect.left, rect.top, rect.width(), rect.height(), source.left, source.top, source.width(), source.height(),bitmap, paint); + } + @Override public void dispose() { framebuffer.dispose(); diff --git a/src/br/nullexcept/mux/core/texel/VgTexel.java b/src/br/nullexcept/mux/core/texel/VgTexel.java index 535ae70..951d13d 100644 --- a/src/br/nullexcept/mux/core/texel/VgTexel.java +++ b/src/br/nullexcept/mux/core/texel/VgTexel.java @@ -158,7 +158,7 @@ public static void drawImage(TexelBitmap image, float destX, float destY, float float ah = image.getHeight() / srcH; float imgH = destH * ah; - float imgW = destW *= aw; + float imgW = destW * aw; float imgX = destX - ((srcX / image.getWidth()) * imgW); float imgY = destY - ((srcY / image.getHeight()) * imgH); diff --git a/src/br/nullexcept/mux/graphics/Canvas.java b/src/br/nullexcept/mux/graphics/Canvas.java index 3bf4d62..dd657f4 100644 --- a/src/br/nullexcept/mux/graphics/Canvas.java +++ b/src/br/nullexcept/mux/graphics/Canvas.java @@ -13,6 +13,8 @@ default void drawRect(float left, float top, float right, float bottom, Paint pa void drawBitmap(Rect rect, Bitmap bitmap, Paint paint); void drawBitmap(int x, int y, int width, int height, Bitmap bitmap, Paint paint); + void drawBitmap(int x, int y, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight, Bitmap bitmap, Paint paint); + void drawBitmap(Rect rect, Rect source, Bitmap bitmap, Paint paint); void drawBitmap(int x, int y, Bitmap bitmap, Paint paint); void drawEllipse(int left, int top, int right, int bottom, Paint paint); diff --git a/src/br/nullexcept/mux/graphics/drawable/NinePathDrawable.java b/src/br/nullexcept/mux/graphics/drawable/NinePathDrawable.java new file mode 100644 index 0000000..144a98e --- /dev/null +++ b/src/br/nullexcept/mux/graphics/drawable/NinePathDrawable.java @@ -0,0 +1,36 @@ +package br.nullexcept.mux.graphics.drawable; + +import br.nullexcept.mux.graphics.Bitmap; +import br.nullexcept.mux.graphics.Canvas; +import br.nullexcept.mux.graphics.Drawable; +import br.nullexcept.mux.graphics.Rect; + +public class NinePathDrawable extends Drawable { + private Bitmap bitmap; + private int size = 4; + + public void setBorder(int size) { + this.size = size; + } + + public void setBitmap(Bitmap bitmap) { + this.bitmap = bitmap; + } + + @Override + public void draw(Canvas canvas) { + if (bitmap != null && bitmap.isValid()) { + Rect bounds = getBounds(); + canvas.drawBitmap(bounds.left, bounds.top, size,size, 0,0, size,size, bitmap, paint); // LEFT TOP + canvas.drawBitmap(bounds.right-size, bounds.top, size,size, bitmap.getWidth()-size,0, size,size, bitmap, paint); // RIGHT TOP + canvas.drawBitmap(bounds.left, bounds.bottom-size, size,size, 0,bitmap.getHeight()-size, size,size, bitmap, paint); // LEFT BOTTOM + canvas.drawBitmap(bounds.right-size, bounds.bottom-size, size,size, bitmap.getWidth()-size,bitmap.getHeight()-size, size,size, bitmap, paint); // RIGHT BOTTOM + + canvas.drawBitmap(bounds.left+size,bounds.top,bounds.width()-size-size,size,size,0,bitmap.getWidth()-(size*2),size,bitmap,paint); // CENTER TOP + canvas.drawBitmap(bounds.left+size,bounds.bottom-size,bounds.width()-size-size,size,size,bitmap.getHeight()-size,bitmap.getWidth()-(size*2),size,bitmap,paint); // CENTER BOTTOM + canvas.drawBitmap(bounds.left,bounds.top+size,size,bounds.height()-size-size,0,size,size,bitmap.getHeight()-(size*2),bitmap,paint); // CENTER LEFT + canvas.drawBitmap(bounds.right-size,bounds.top+size,size,bounds.height()-size-size,bitmap.getWidth()-size,size,size,bitmap.getHeight()-(size*2),bitmap,paint); // CENTER RIGHT + canvas.drawBitmap(bounds.left+size,bounds.top+size,bounds.width()-(size*2),bounds.height()-(size*2),size,size,bitmap.getWidth()-(size*2),bitmap.getHeight()-(size*2),bitmap,paint); // CENTER INNER + } + } +} diff --git a/src/br/nullexcept/mux/res/Parser.java b/src/br/nullexcept/mux/res/Parser.java index a1d4dda..b56fc72 100644 --- a/src/br/nullexcept/mux/res/Parser.java +++ b/src/br/nullexcept/mux/res/Parser.java @@ -150,6 +150,13 @@ private static Drawable inflateXmlDrawable(Resources res, XmlElement xml) { return drawable; } + case "nine-path": { + NinePathDrawable drawable = new NinePathDrawable(); + drawable.setBitmap(openBitmap(attrs.getRawValue("src").substring(1))); + drawable.setBorder(Integer.parseInt(attrs.getRawValue("border"))); + + return drawable; + } case "color":{ ColorDrawable drawable = new ColorDrawable(0); attrs.searchColor(AttrList.color, drawable::setColor); @@ -207,6 +214,18 @@ private static Path parseVectorPath(String src, int w, int h) { return p; } + private static Bitmap openBitmap(String path) { + String[] supportedImages = new String[]{ + "png", "jpg", "jpeg" + }; + for (String format: supportedImages){ + if (Resources.Manager.exists(path+"."+format)){ + return BitmapFactory.openBitmap(Resources.Manager.openDocument(path+"."+format)); + } + } + return null; + } + public static Drawable parseDrawable(Resources resources, String value) { if (value.startsWith("#")){ return new ColorDrawable(Color.parseColor(value)); @@ -215,13 +234,9 @@ public static Drawable parseDrawable(Resources resources, String value) { if (Resources.Manager.exists(value+".xml")){ return inflateXmlDrawable(resources, resources.requestXml(value)); } else { - String[] supportedImages = new String[]{ - "png", "jpg", "jpeg" - }; - for (String format: supportedImages){ - if (Resources.Manager.exists(value+"."+format)){ - return new BitmapDrawable(BitmapFactory.openBitmap(Resources.Manager.openDocument(value+"."+format))); - } + Bitmap res = openBitmap(value); + if (res != null) { + return new BitmapDrawable(res); } } }