Skip to content

Commit b78457e

Browse files
Re-implement alpha based in shaders
1 parent c4e78e4 commit b78457e

9 files changed

Lines changed: 64 additions & 21 deletions

File tree

res/layout/example_list.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
width="200dp"
88
height="200dp"
99
textSize="24dp"
10-
background="@drawable/test_path"
1110
text="MasterUI"/>
1211
</LinearLayout>

res/raw/view_renderer.frag

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
precision mediump float;
22

33
#define ALPHA float(params[1])/255.0
4+
#define MODE params[0]
45

56
uniform int[8] params;
67

@@ -10,6 +11,16 @@ varying vec2 uv;
1011

1112
void main() {
1213
vec4 color = texture2D(texture, uv);
14+
color.a *= ALPHA;
15+
if(color.a <= 0.0) discard;
16+
17+
if(MODE == 0) {
18+
color.rgb *= color.a;
19+
} else if(ALPHA == 1.0){
20+
if(color.a >= 1.0)discard;
21+
} else {
22+
discard;
23+
}
1324

1425
gl_FragColor = color;
1526
}

src/br/nullexcept/mux/app/applets/ClipboardApplet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
public abstract class ClipboardApplet extends Applet {
66
public abstract String getContent();
7+
public abstract boolean hasContent();
78
public abstract void setContent(String content);
89
}

src/br/nullexcept/mux/core/texel/GLTexel.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ public static void drawViewLayers(float[][] vertices, int[] textures, float[] al
6161
*
6262
* dstColor = src.rgb * X + dest.rgb * Y;
6363
*/
64+
glBlendEquation(GL_FUNC_ADD);
6465
glBlendFuncSeparate(
6566
GL_ONE,
6667
GL_ONE_MINUS_SRC_ALPHA,
6768
GL_ONE,
68-
GL_ONE_MINUS_SRC_ALPHA
69+
GL_ONE
6970
);
7071

7172

@@ -78,31 +79,28 @@ public static void drawViewLayers(float[][] vertices, int[] textures, float[] al
7879
for (int i = 0; i < vertices.length; i++) {
7980
bufferRect.put(vertices[i]);
8081
bufferRect.position(0);
81-
8282
glBindTexture(GL_TEXTURE_2D, textures[i]);
8383

8484
glVertexAttribPointer(program.position, 2, GL_FLOAT, false, 0, bufferRect);
8585
glVertexAttribPointer(program.uv, 2, GL_FLOAT, false, 0, bufferUV);
86-
87-
glUniform1iv(program.params, new int[]{});
88-
8986
glUniform1i(program.texture, GL_TEXTURE_2D);
90-
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
91-
glFinish();
87+
88+
/**
89+
* @TODO: Need better implement for fix dark border + alpha, with low gpu cost
90+
* MODE 0 = FILL
91+
* MODE 1 = "ANTILIAZING""
92+
*/
93+
for (int x = 0; x < 2; x++) {
94+
glUniform1iv(program.params, new int[]{
95+
x, Math.round(alphas[i] * 255)
96+
});
97+
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
98+
}
9299
}
93100

94101
program.unbind();
95102
glBindTexture(GL_TEXTURE_2D, 0);
96-
}
97-
98-
99-
private static FloatBuffer allocateVertices(float[][] vectors) {
100-
FloatBuffer vertx = BufferUtils.createFloatBuffer(vectors.length*vectors[0].length);
101-
vertx.position(0);
102-
for (int i = 0; i< vectors.length; i++)
103-
vertx.put(vectors[i]);
104-
vertx.position(0);
105-
return vertx;
103+
glDisable(GL_BLEND);
106104
}
107105

108106
public static void drawTexture(float x, float y, float width, float height, GLProgram program, int texture){

src/br/nullexcept/mux/core/texel/GlfwApplets.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public String getContent() {
1717
return GLFW.glfwGetClipboardString(C.GLFW_CONTEXT);
1818
}
1919

20+
@Override
21+
public boolean hasContent() {
22+
String content = getContent();
23+
return content != null && content.length() > 0;
24+
}
25+
2026
@Override
2127
public void setContent(String content) {
2228
GLFW.glfwSetClipboardString(C.GLFW_CONTEXT, content);

src/br/nullexcept/mux/core/texel/ViewRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package br.nullexcept.mux.core.texel;
22

33
import br.nullexcept.mux.graphics.Rect;
4+
import br.nullexcept.mux.utils.Log;
45
import br.nullexcept.mux.view.View;
56
import br.nullexcept.mux.view.ViewGroup;
67

@@ -26,7 +27,6 @@ public void drawInternal(CanvasTexel canvas, View view){
2627
canvas.getFramebuffer().resize(bounds.width(), bounds.height());
2728
}
2829
canvas.begin();
29-
canvas.alpha(view.getAlpha());
3030
canvas.reset();
3131
view.onDraw(canvas);
3232
if (view instanceof ViewGroup){

src/br/nullexcept/mux/core/texel/WindowContainer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void performInputEvent(Event event) {
184184
performHover((MouseEvent)event);
185185
if(event.getTarget() == -1){
186186
dispatchEvent(event);
187-
} else {
187+
} else if (renders.containsKey(event.getTarget())){
188188
View target = renders.get(event.getTarget()).view;
189189
Rect visibleBounds = target.getVisibleBounds();
190190
((MouseEvent) event).transform(-visibleBounds.left, -visibleBounds.top);
@@ -242,6 +242,7 @@ private void showMenu() {
242242
for (Menu.Group group: currentMenu.getGroups()) {
243243
for (Menu.Item item: group.getItems()) {
244244
ViewGroup v = inflater.inflate("default_widget_menu_item");
245+
v.setAlpha(item.isEnable() ? 1.0f : 0.5f );
245246
ImageView icon = v.findViewByTag("icon");
246247
if (item.getIcon() == null) {
247248
icon.getParent().removeChild(icon);

src/br/nullexcept/mux/view/Menu.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public List<Item> getItems() {
4949
return items;
5050
}
5151

52+
public Item findItemById(String id) {
53+
for (Item item: items) {
54+
if (item.id == id) {
55+
return item;
56+
}
57+
}
58+
return null;
59+
}
60+
61+
5262
public String getTitle() {
5363
return title;
5464
}
@@ -58,11 +68,25 @@ public static class Item {
5868
private final String id;
5969
private final String title;
6070
private final Drawable icon;
71+
private boolean enable;
6172

6273
public Item(String id, String title, Drawable icon) {
74+
this(id, title, icon, true);
75+
}
76+
77+
public Item(String id, String title, Drawable icon, boolean enable) {
6378
this.id = id;
6479
this.title = title;
6580
this.icon = icon;
81+
this.enable = enable;
82+
}
83+
84+
public void setEnable(boolean enable) {
85+
this.enable = enable;
86+
}
87+
88+
public boolean isEnable() {
89+
return enable;
6690
}
6791

6892
public String getTitle() {

src/br/nullexcept/mux/widget/EditText.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public void setText(CharSequence text) {
172172

173173
@Override
174174
public boolean onCreateContextMenu(Menu menu) {
175+
ClipboardApplet clipboard = getContext().getApplet(Context.CLIPBOARD_APPLET);
175176
menu.setOnClickListener(item -> {
176-
ClipboardApplet clipboard = getContext().getApplet(Context.CLIPBOARD_APPLET);
177177
switch (item.getId()) {
178178
case "paste": {
179179
pasteFromClipboard();
@@ -189,6 +189,7 @@ public boolean onCreateContextMenu(Menu menu) {
189189
}
190190
});
191191

192+
this.menu.findItemById("paste").setEnable(clipboard.hasContent());
192193
menu.add(this.menu);
193194
return true;
194195
}
@@ -213,6 +214,8 @@ private void copyToClipboard() {
213214

214215
private void pasteFromClipboard() {
215216
String content = ((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent();
217+
if (content == null)return;
218+
216219
if(selection.length() > 0){
217220
text.delete();
218221
}

0 commit comments

Comments
 (0)