Skip to content

Commit

Permalink
Clip
Browse files Browse the repository at this point in the history
+ Canvas context fix.
+ CardLayout
+ Customize 4 sides of rounded rect.
  • Loading branch information
GabrielBRDeveloper committed May 19, 2024
1 parent 3486931 commit 28e7d0b
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 71 deletions.
28 changes: 24 additions & 4 deletions res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@
background="?colorSurface"
padding="20dp"
orientation="horizontal">
<LinearLayout
width="match_parent"
height="match_parent">
<CardLayout
width="match_parent"
height="match_parent"
radius="20dp">
<LinearLayout
width="match_parent"
height="match_parent"
background="#F00"
id="content">

<TextView
width="wrap_content"
height="wrap_content"
textSize="64sp"
text="Hello world"/>

<TextView
width="wrap_content"
height="wrap_content"
textSize="32sp"
text="That is a example of draw with card layout"/>

<TextView
width="wrap_content"
height="wrap_content"
textSize="32sp"
text="That allow create UIs a like Material3"/>

</LinearLayout>
</LinearLayout>
</CardLayout>
</FrameLayout>
66 changes: 50 additions & 16 deletions src/br/nullexcept/mux/core/texel/CanvasTexel.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package br.nullexcept.mux.core.texel;

import br.nullexcept.mux.graphics.*;
import br.nullexcept.mux.graphics.shape.ShapeList;
import br.nullexcept.mux.hardware.GLES;
import br.nullexcept.mux.utils.Log;
import org.lwjgl.opengles.GLES20;

class CanvasTexel implements Canvas {
Expand All @@ -15,16 +17,23 @@ public CanvasTexel(int width, int height) {
}

public void begin() {
VgTexel.endFrame();
VgTexel.setAlpha(alpha);
framebuffer.bind();
VgTexel.beginFrame(framebuffer.getWidth(), framebuffer.getHeight());
CURRENT_CANVAS = id;
}

private void check() {
if (CURRENT_CANVAS != id) {
VgTexel.endFrame();
VgTexel.setAlpha(alpha);
framebuffer.bind();
VgTexel.beginFrame(framebuffer.getWidth(), framebuffer.getHeight());
Log.error("CanvasTexel", "Switching canvas!");
begin();
}
}

@Override
public void reset() {
check();
this.alpha = 1.0f;
GLES.glClearColor(0, 0, 0, 0);
GLES.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT | GLES.GL_STENCIL_BUFFER_BIT);
Expand All @@ -42,14 +51,27 @@ public void alpha(float alpha){
VgTexel.setAlpha(this.alpha);
}

@Override
public void clip(ShapeList list) {
check();
VgTexel.beginClip();
VgTexel.getPaint().setColor(Color.WHITE);
list.asArray().forEach(shape -> {
shape.draw(this, VgTexel.getPaint());
});
VgTexel.endClip(framebuffer.getFramebuffer());
}

@Override
public void drawColor(int color) {
check();
VgTexel.setColor(color);
drawRect(0, 0, getWidth(), getHeight(), VgTexel.getPaint());
}

@Override
public void drawRect(int left, int top, int right, int bottom, Paint paint) {
check();
VgTexel.beginElement();
VgTexel.applyPaint(paint);
VgTexel.drawRect(left, top, right - left, bottom - top);
Expand All @@ -63,6 +85,7 @@ public void drawRect(Rect rect, Paint paint) {

@Override
public void drawText(CharSequence text, int x, int y, Paint paint) {
check();
if (text == null) {
text = "[NULL]";
}
Expand All @@ -74,26 +97,19 @@ public void drawText(CharSequence text, int x, int y, Paint paint) {

@Override
public void translate(int x, int y) {
check();
VgTexel.move(x,y);
}

@Override
public void rotate(float angle) {
check();
VgTexel.rotate(angle);
}

@Override
public void drawBitmap(Rect rect, Bitmap bitmap, Paint paint) {
drawBitmap(rect.left, rect.top, rect.width(), rect.height(), bitmap, paint);
}

@Override
public void drawBitmap(int x, int y, Bitmap bitmap, Paint paint) {
drawBitmap(x, y, bitmap.getWidth(), bitmap.getHeight(), bitmap, paint);
}

@Override
public void drawEllipse(int left, int top, int right, int bottom, Paint paint) {
check();
VgTexel.beginElement();
VgTexel.applyPaint(paint);
VgTexel.drawEllipse(left, top, right-left, bottom-top);
Expand All @@ -102,14 +118,21 @@ public void drawEllipse(int left, int top, int right, int bottom, Paint paint) {

@Override
public void drawRoundRect(int left, int top, int right, int bottom, int radius, Paint paint) {
drawRoundRect(left, top, right, bottom, radius,radius,radius,radius, paint);
}

@Override
public void drawRoundRect(int left, int top, int right, int bottom, int radiusLeft, int radiusTop, int radiusRight, int radiusBottom, Paint paint) {
check();
VgTexel.beginElement();
VgTexel.applyPaint(paint);
VgTexel.drawRoundedRect(left,top, right-left, bottom-top, radius);
VgTexel.drawRoundedRect(left,top,right-left, bottom-top, radiusLeft, radiusTop, radiusRight, radiusBottom);
VgTexel.endElement();
}

@Override
public void drawPath(Path path, int x, int y, Paint paint) {
check();
VgTexel.beginElement();
VgTexel.applyPaint(paint);
translate(x,y);
Expand All @@ -118,13 +141,24 @@ public void drawPath(Path path, int x, int y, Paint paint) {
VgTexel.endElement();
}

@Override
public void drawBitmap(Rect rect, Bitmap bitmap, Paint paint) {
drawBitmap(rect.left, rect.top, rect.width(), rect.height(), bitmap, paint);
}

@Override
public void drawBitmap(int x, int y, Bitmap bitmap, Paint paint) {
drawBitmap(x, y, bitmap.getWidth(), bitmap.getHeight(), bitmap, 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);
drawBitmap(x,y,width,height, 0,0, bitmap.getWidth(), bitmap.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) {
check();
if (!(bitmap instanceof TexelBitmap)){
throw new IllegalArgumentException("Invalid bitmap, bitmap core and canvas core is different!");
}
Expand Down
29 changes: 29 additions & 0 deletions src/br/nullexcept/mux/core/texel/GLTexel.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,33 @@ public static void drawTexture(float x, float y, float width, float height, GLPr
program.unbind();
glBindTexture(GL_TEXTURE_2D,0);
}

public static void drawTextureClip(int x, int y, int width, int height, int texture) {
GLProgram program = GLShaderList.TEXTURE;
glEnable(GL_BLEND);
glBlendFuncSeparate(
GL_ZERO, GL_SRC_ALPHA,
GL_ZERO, GL_SRC_ALPHA
);
prepareRect(x, y, width, height);
program.bind();

int vPosition = program.attribute(GLProgram.ATTRIBUTE_POSITION);
int vTextureCoords = program.attribute(GLProgram.ATTRIBUTE_UV);
int uTexture = program.uniform(GLProgram.UNIFORM_TEXTURE);

glBindTexture(GL_TEXTURE_2D, texture);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, false, 0, bufferRect);
glVertexAttribPointer(vTextureCoords, 2, GL_FLOAT, false, 0, bufferUV);

glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(vTextureCoords);

glUniform1i(uTexture, GL_TEXTURE_2D);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
program.unbind();
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_BLEND);
}
}
Loading

0 comments on commit 28e7d0b

Please sign in to comment.