Skip to content

Commit

Permalink
NinePath implement
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Apr 26, 2024
1 parent 86a8260 commit 057870d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- dev
pull_request:

jobs:
Expand Down
12 changes: 11 additions & 1 deletion src/br/nullexcept/mux/core/texel/CanvasTexel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/br/nullexcept/mux/core/texel/VgTexel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/br/nullexcept/mux/graphics/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions src/br/nullexcept/mux/graphics/drawable/NinePathDrawable.java
Original file line number Diff line number Diff line change
@@ -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
}
}
}
29 changes: 22 additions & 7 deletions src/br/nullexcept/mux/res/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 057870d

Please sign in to comment.