Skip to content

Commit

Permalink
Several Looper Fix
Browse files Browse the repository at this point in the history
+ Recerete Looper system.
+ Focus navigate.
+ Italic and bold.
+ Shader crash in windows devices
  • Loading branch information
GabrielBRDeveloper committed May 22, 2024
1 parent 892b4c0 commit 87c85aa
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 64 deletions.
14 changes: 13 additions & 1 deletion res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
width="wrap_content"
height="wrap_content"
textSize="14sp"
textStyle="bold"
text="Hello world"/>

<TextView
Expand All @@ -33,6 +32,19 @@
textSize="14sp"
text="That allow create UIs a like Material3"/>

<EditText
width="128dp"
height="128dp"/>

<EditText
width="128dp"
height="128dp"/>
<EditText
width="128dp"
height="128dp"/>
<EditText
width="128dp"
height="128dp"/>
</LinearLayout>
</CardLayout>
</FrameLayout>
2 changes: 2 additions & 0 deletions res/font/roboto.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<font>
<font bold="true" italic="true">@fonts/Roboto/Roboto-BoldItalic</font>
<font italic="true">@fonts/Roboto/Roboto-Italic</font>
<font bold="true">@fonts/Roboto/Roboto-Bold</font>
<font>@fonts/Roboto/Roboto-Regular</font>
</font>
Binary file added res/fonts/Roboto/Roboto-BoldItalic.ttf
Binary file not shown.
Binary file added res/fonts/Roboto/Roboto-Italic.ttf
Binary file not shown.
9 changes: 4 additions & 5 deletions res/raw/view_renderer.frag
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
precision mediump float;

#define ALPHA float(params[1])/255.0
#define MODE params[0]

uniform int[8] params;
#define ALPHA float(params.y)/255.0
#define MODE params.x

uniform vec4 params;
uniform sampler2D texture;

varying vec2 uv;

void main() {
vec4 diffuse = vec4(1.0);

if(MODE == 0) {
if(MODE == 0.0) {
diffuse = texture2D(texture, uv);
}

Expand Down
2 changes: 2 additions & 0 deletions res/raw/view_renderer.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
precision mediump float;

attribute vec4 vPos;
attribute vec2 vTexCoords;

Expand Down
38 changes: 29 additions & 9 deletions src/br/nullexcept/mux/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import br.nullexcept.mux.C;
import br.nullexcept.mux.core.texel.TexelAPI;
import br.nullexcept.mux.lang.Valuable;
import br.nullexcept.mux.utils.Log;
import br.nullexcept.mux.view.Window;
import org.lwjgl.egl.EGL;
import org.lwjgl.egl.EGL10;
import org.lwjgl.egl.EGL11;
import org.lwjgl.glfw.GLFWNativeEGL;
import org.lwjgl.opengles.GLES;
import org.lwjgl.system.MemoryUtil;

import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;

Expand All @@ -18,13 +25,8 @@ public class Application {

public static void initialize(Valuable<Activity> creator){
glfwInit();
glfwDefaultWindowHints();
if (C.Config.SET_WINDOW_GL_HINT) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, C.Config.WINDOW_GL_VERSION[0]);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, C.Config.WINDOW_GL_VERSION[1]);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
}
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

setupEGL();

long window = glfwCreateWindow(1,1,"[MasterUI - Core]",0, 0);
glfwMakeContextCurrent(window);
Expand All @@ -43,10 +45,28 @@ public static void initialize(Valuable<Activity> creator){
TexelAPI.destroy();
glfwTerminate();
System.gc();
Looper.sleep(2000,0); // Wait for all services stop
Looper.sleep(2000); // Wait for all services stop
System.exit(0);
}

private static void setupEGL() {
glfwDefaultWindowHints();
if (C.Config.SET_WINDOW_GL_HINT) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, C.Config.WINDOW_GL_VERSION[0]);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, C.Config.WINDOW_GL_VERSION[1]);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);

long display = GLFWNativeEGL.glfwGetEGLDisplay();
try {// Setup EGL
int[][] version = new int[2][1];
EGL10.eglInitialize(display, version[0], version[1]);
EGL.createDisplayCapabilities(display, version[0][0], version[1][0]);
} catch (Exception e) {}
}
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
}

private static void loop(){
glfwPollEvents();
if (System.currentTimeMillis() - lastGc > 5000){
Expand Down Expand Up @@ -124,7 +144,7 @@ public static void stop(){
Looper.getMainLooper().stop();
}

static <T extends Service> T beginService(Launch<T> launch) {
static synchronized <T extends Service> T beginService(Launch<T> launch) {
String name = launch.getLaunchClass().getName();

if (services.containsKey(name)) {
Expand Down
68 changes: 43 additions & 25 deletions src/br/nullexcept/mux/app/Looper.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package br.nullexcept.mux.app;

import br.nullexcept.mux.utils.Log;

import java.util.HashMap;
import java.util.ArrayList;

public class Looper {
private static final String LOG_TAG = "Looper";

private final HashMap<Long, Runnable> executions = new HashMap<>();
private final ArrayList<Callback> executions = new ArrayList<>();
static Looper mainLooper;
private boolean stop = false;

Expand All @@ -21,8 +17,9 @@ public void initialize(){
}

public void postDelayed(Runnable runnable, long msTime){
msTime = Math.max(0, msTime);
synchronized (executions) {
executions.put(System.nanoTime() + (msTime * 1000000L), runnable);
executions.add(new Callback(runnable,System.currentTimeMillis()+msTime));
}
}

Expand All @@ -32,28 +29,24 @@ public void post(Runnable runnable){

public void loop(){
while (!stop) {
long time = System.nanoTime();
Long[] keys;
Callback[] list;
int i = 0;
synchronized (executions) {
keys = executions.keySet().toArray(new Long[0]);
}
for (long key : keys) {
if (key <= time) {
Runnable runnable;
synchronized (executions) {
runnable = executions.get(key);
executions.remove(key);
}
try {
runnable.run();
} catch (Throwable e) {
Log.error(LOG_TAG, "[ERROR ON LOOPER]",e);
stop = true;
list = executions.toArray(new Callback[0]);
for (Callback call: list) {
if (call.time <= System.currentTimeMillis()) {
executions.remove(call);
} else {
list[i] = null;
}
i++;
}
}

sleep(0, (int) (Math.random()*400));
for (Callback call: list) {
if (call == null) continue;
call.handle.run();
}
sleep(0, (int) (Math.random()*100));
}
}

Expand All @@ -70,4 +63,29 @@ public static void sleep(int ms, int nano) {
public void stop(){
this.stop = true;
}

private static class Callback {
private static long current;
private final Runnable handle;
private final long time;
private final long id = hash();

private Callback(Runnable handle, long time) {
this.handle = handle;
this.time = time;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Callback) {
return ((Callback) obj).id == id;
}
return false;
}

public static synchronized long hash() {
current++;
return current;
}
}
}
15 changes: 9 additions & 6 deletions src/br/nullexcept/mux/core/texel/GLProgramTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ class GLProgramTexture extends GLProgram {
protected String fragment() {
return ("precision mediump float;\n" +
"uniform sampler2D __texture__;\n" +
"varying vec2 xuv;" +
"varying vec2 xuv\n\n;" +
"void main(){\n" +
" gl_FragColor = texture2D(__texture__, xuv);\n" +
"};\n").replaceAll("__texture__", UNIFORM_TEXTURE);
"}\n").replaceAll("__texture__", UNIFORM_TEXTURE)
.replaceAll("\n","\n\r");
}

@Override
protected String vertex() {
return ("attribute vec4 __position__;\n" +
return ("precision mediump float;\n" +
"attribute vec4 __position__;\n" +
"attribute vec2 __uv__;\n" +
"varying vec2 xuv;" +
"varying vec2 xuv;\n\n" +
"void main(){\n" +
" gl_Position = __position__;\n" +
" xuv = __uv__;\n" +
"};\n").replaceAll("__position__", ATTRIBUTE_POSITION)
.replaceAll("__uv__", ATTRIBUTE_UV);
"}\n").replaceAll("__position__", ATTRIBUTE_POSITION)
.replaceAll("__uv__", ATTRIBUTE_UV)
.replaceAll("\n","\n\r");
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions src/br/nullexcept/mux/core/texel/GLTexel.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public static void drawViewLayers(float[][] vertices, int[] textures, float[] al
glVertexAttribPointer(program.uv, 2, GL_FLOAT, false, 0, bufferUV);
glUniform1i(program.texture, GL_TEXTURE_2D);

glUniform1iv(program.params, new int[]{
0,
Math.round(alphas[i] * 255)
glUniform1fv(program.params, new float[]{
0.0f,
Math.round(alphas[i] * 255) / 255.0f,
0.0f,
0.0f
});
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
Expand Down
2 changes: 1 addition & 1 deletion src/br/nullexcept/mux/core/texel/GlesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static int compileShader(String source, int type){
int[] res = new int[1];
glGetShaderiv(shader, GL_COMPILE_STATUS,res);
if (res[0] != GL_TRUE){
throw new RuntimeException("Error on compile gl shader: "+ glGetShaderInfoLog(shader));
throw new RuntimeException("Error on compile gl shader: "+ glGetShaderInfoLog(shader)+"\nSource: \n"+source);
}
return shader;
}
Expand Down
39 changes: 27 additions & 12 deletions src/br/nullexcept/mux/core/texel/GlfwWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public GlfwWindow() {
window = GLFW.glfwCreateWindow(512, 512, title = "[MasterUI:Window]", 0, C.GLFW_CONTEXT);
eventManager = new GlfwEventManager(this);
GLFW.glfwIconifyWindow(window);
GLFW.glfwSetWindowSizeCallback(window, (l, w,h) -> {
windowSize.set(w,h);
Looper.getMainLooper().post(this::drawSurface);
});
GLFW.glfwSetWindowPosCallback(window, (l, x, y) -> {
p.set(x,y);
});
setMinimumSize(256,256);
center();
}
Expand Down Expand Up @@ -85,6 +92,21 @@ private void refresh() {

private long[] times = new long[8];
private long last = System.currentTimeMillis();
private Point p = new Point();
private Point pd = new Point();

private void drawSurface() {
if (destroyed)return;
GLFW.glfwSwapBuffers(C.GLFW_CONTEXT);
GLFW.glfwMakeContextCurrent(window);
glfwSwapInterval(0); //NEED THAT FOR NOT SLOW MAIN LOOP
GLES.glViewport(0, 0, windowSize.width, windowSize.height);
GLES.glClearColor(0,0,0,1);
GLES.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_STENCIL_BUFFER_BIT);
GLTexel.drawTexture(0, 0, windowSize.width, windowSize.height, container.getCanvas().getFramebuffer().getTexture());
GLFW.glfwSwapBuffers(window);
GLFW.glfwMakeContextCurrent(C.GLFW_CONTEXT);
}

private void update() {
if(destroyed)
Expand All @@ -109,22 +131,14 @@ private void update() {
container.resize(windowSize.width, windowSize.height);

container.drawFrame();
GLFW.glfwSwapBuffers(C.GLFW_CONTEXT);
GLFW.glfwMakeContextCurrent(window);
glfwSwapInterval(0); //NEED THAT FOR NOT SLOW MAIN LOOP
GLES.glViewport(0, 0, windowSize.width, windowSize.height);
GLES.glClearColor(0,0,0,1);
GLES.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_STENCIL_BUFFER_BIT);
GLTexel.drawTexture(0, 0, windowSize.width, windowSize.height, container.getCanvas().getFramebuffer().getTexture());

drawSurface();
times[1] += System.currentTimeMillis() - begin;
if (C.Flags.DEBUG_OVERLAY) {
drawDebug();
}

GLFW.glfwSwapBuffers(window);
GLFW.glfwMakeContextCurrent(C.GLFW_CONTEXT);
}


if (GLFW.glfwWindowShouldClose(window)) {
destroy();
}
Expand Down Expand Up @@ -220,7 +234,7 @@ public void setSize(int width, int height) {

@Override
public void setMinimumSize(int width, int height) {
GLFW.glfwSetWindowSizeLimits(window, width,height,Integer.MAX_VALUE, Integer.MAX_VALUE);
//GLFW.glfwSetWindowSizeLimits(window, width,height,Integer.MAX_VALUE, Integer.MAX_VALUE);
}

public void onMouseEvent(MouseEvent event) {
Expand Down Expand Up @@ -252,6 +266,7 @@ public void setVisible(boolean visible) {
if (visible) {
align();
GLFW.glfwShowWindow(window);
GLFW.glfwFocusWindow(window);
} else {
refresh();
align();
Expand Down
9 changes: 9 additions & 0 deletions src/br/nullexcept/mux/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ public final void requestFocus() {
}
}

protected void requestNextFocus() {
if (parent != null) {
parent.findNextFocus(this);
}
}

public void setFocusable(boolean focusable) {
if (parent != null && this.focusable && !focusable) {
parent.requestFocus(null);
Expand Down Expand Up @@ -322,6 +328,9 @@ protected void onCharEvent(CharEvent charEvent) {
}

protected void onKeyEvent(KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP && keyEvent.getKeyCode() == KeyEvent.KEY_TAB && isFocused()) {
requestNextFocus();
}
}

protected void onMouseMoved(MotionEvent event) {
Expand Down
Loading

0 comments on commit 87c85aa

Please sign in to comment.