Skip to content

Commit

Permalink
few things
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jan 18, 2024
1 parent af0c3a1 commit 0cb4fa2
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 69 deletions.
64 changes: 52 additions & 12 deletions examples/basic/assets/data/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,62 @@ local Gdx = java.import("com.badlogic.gdx.Gdx")

local renderer = ShapeRenderer.new()

--
--renderer:begin()
--
--print(ShapeRenderer)
--print(renderer)
local moveState = 1;
-- 1 = RIGHT
-- 2 = TOP
-- 3 = LEFT
-- 4 = BOTTOM


-- renderer:begin(50)
local recX = 50.0;
local recY = 50.0;
local recSize = 100.0
local recSpeed = 5.0
local halfRec = recSize / 2.0

function render()
Gdx.gl.glClearColor(1.0, 1.0, 1.0, 1.0)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
--
renderer:Begin()
-- --shapeRenderer:begin(ShapeRenderer.ShapeType.Filled)
-- --shapeRenderer:setColor(1, 0, 0 ,1)
-- --shapeRenderer:rect(2, 20, 100, 100)

moveBox()

renderer:Begin(ShapeRenderer.ShapeType.Filled)
renderer:setColor(1.0, 0.0, 0.0 ,1.0)
renderer:rect(recX - halfRec, recY - halfRec, recSize, recSize)
renderer:End()
end

function moveBox()
local width = Gdx.graphics.getWidth()
local height = Gdx.graphics.getHeight()

local borderLimitRight = width - halfRec;
local borderLimitLeft = halfRec;
local borderLimitTop = height - halfRec;
local borderLimitBottom = halfRec;

if moveState == 1 then
if recX < borderLimitRight then
recX = recX + recSpeed
else
moveState = 2
end
elseif moveState == 2 then
if recY < borderLimitTop then
recY = recY + recSpeed
else
moveState = 3
end
elseif moveState == 3 then
if recX > borderLimitLeft then
recX = recX - recSpeed
else
moveState = 4;
end
elseif moveState == 4 then
if recY > borderLimitBottom then
recY = recY - recSpeed;
else
moveState = 1;
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ public void show() {
buildScript(code);
}

int moveState = 1;
// 1 = RIGHT
// 2 = TOP
// 3 = LEFT
// 4 = BOTTOM

float recX = 50;
float recY = 50;

@Override
public void renderImGui() {
update();
renderEditText();

renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(1, 0, 0 ,1);
renderer.rect(2, 20, 100, 100);
renderer.end();
}

public void update() {
Expand Down
129 changes: 100 additions & 29 deletions lua/lua-core/src/main/java/lua/LuaLibrary.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
package lua;

import java.util.List;
import lua.register.ImportListener;
import lua.register.LuaCreateClass;
import lua.register.LuaIntEnumData;

public class LuaLibrary {

public static final String LUA_J_HASH = "java_hash";
private static final String TEMPLATE = "Template";
private static final String INSTANCE = "Instance";
private static final String CLASS = "Class";
private static final String ENUM = "Enum";

private static boolean SKIP_ERROR_LOG = false;

public static Object getJavaInstance(Lua lua, LuaState luaState) {
String t = "Lua does not contains java instance";
if(luaState.lua_istable(1) != 0) {
luaState.lua_getfield(1, LuaLibrary.LUA_J_HASH);
if(luaState.lua_isnil(-1) != 0) {
luaState.lua_pop(1);
luaState.luaL_error(t);
return null;
}
else {
if(luaState.lua_isinteger(-1) == 0) {
luaState.lua_pop(1);
luaState.luaL_error(t);
return null;
}
else {
int hash = luaState.lua_tointeger(-1);
luaState.lua_pop(1);
Object o = lua.luaJavaInstances.get(hash);
if(o == null) {
luaState.luaL_error("Java object instance is null");
}
return o;
}
}
}
luaState.luaL_error(t);
return null;
}



/**
* Get or create the table and push to stack.
* If parent exist, it will create or get as a child.
Expand Down Expand Up @@ -113,8 +147,8 @@ public static boolean getMetaClassTable(Lua lua, LuaTableType type, String class
luaState.lua_pop(1); // pop nill
}
}
else if(type == LuaTableType.TEMPLATE) {
luaState.lua_getfield(-1, TEMPLATE);
else if(type == LuaTableType.CLASS_INSTANCE) {
luaState.lua_getfield(-1, INSTANCE);
if(luaState.lua_istable(-1) != 0) {
luaState.lua_remove(-2); // Remove metatable
return true;
Expand All @@ -140,17 +174,37 @@ else if(type == LuaTableType.ENUM) {
}

/**
* Add class function. MetaClass must exist
* Create enum table and push to stack.
*/
public static boolean setMetaClassFunction(Lua lua, LuaTableType type, String className, String functionName, LuaFunction function) {
public static boolean createEnumTable(Lua lua, List<LuaIntEnumData> enumList) {
if(enumList == null || enumList.isEmpty())
return false;
LuaState luaState = lua.luaState;
if(getMetaClassTable(lua, type, className)) {
if(lua.registerFunction(functionName, function)) {
luaState.lua_pop(1);
return true;
}
luaState.lua_newtable();

for(int i = 0; i < enumList.size(); i++) {
LuaIntEnumData enumData = enumList.get(i);
luaState.lua_pushinteger(enumData.value);
luaState.lua_setfield(-2, enumData.key);
}
return false;

lua.registerFunction("__newindex", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
luaState.luaL_error("Creating new Enum value is not allowed");
return 0;
}
});

lua.registerFunction("__index", new LuaFunction() {
@Override
public int onCall(LuaState luaState) {
luaState.luaL_error("Changing Enum value is not allowed");
return 0;
}
});

return true;
}

/**
Expand Down Expand Up @@ -212,10 +266,14 @@ public static boolean setMetaClassFloat(Lua lua, LuaTableType type, String class
return false;
}

public static boolean registerMetaClassTableNewIndex(Lua lua, LuaTableType type, String className, LuaFunction luaFunction) {
/**
* Add class function. MetaClass must exist
*/
public static boolean setMetaClassFunction(Lua lua, LuaTableType type, String className, String functionName, LuaFunction function) {
LuaState luaState = lua.luaState;
if(getMetaClassTable(lua, type, className)) {
if(lua.registerFunction("__newindex", luaFunction)) {
lua.luaState.lua_pop(1);
if(lua.registerFunction(functionName, function)) {
luaState.lua_pop(1);
return true;
}
}
Expand Down Expand Up @@ -246,6 +304,16 @@ public int onCall(LuaState luaState) {
return false;
}

public static boolean registerMetaClassTableNewIndex(Lua lua, LuaTableType type, String className, LuaFunction luaFunction) {
if(getMetaClassTable(lua, type, className)) {
if(lua.registerFunction("__newindex", luaFunction)) {
lua.luaState.lua_pop(1);
return true;
}
}
return false;
}

/**
* Create a lua instance. Require MetaClass template table.
*/
Expand All @@ -269,7 +337,7 @@ public static boolean createInstanceObject(Lua lua, String className, Object cla

//Put metatable to the new created table
{
if(getMetaClassTable(lua, LuaTableType.TEMPLATE, className)) {
if(getMetaClassTable(lua, LuaTableType.CLASS_INSTANCE, className)) {
luaState.lua_setmetatable(-2); // Set Template metatable to new table
}
else {
Expand All @@ -283,7 +351,7 @@ public static boolean createInstanceObject(Lua lua, String className, Object cla
* Register MetaClass. Use lua code 'MYCLASS = java.import("class full path")' to obtain lua class object.
* canInstantiate true means that this class can be created with new function or createInstanceObject.
*/
public static boolean registerClass(Lua lua, String className, boolean canInstantiate) {
public static boolean registerClass(Lua lua, String className, boolean addImport, boolean canInstantiate) {
LuaState luaState = lua.luaState;
SKIP_ERROR_LOG = true;
if(getMetaClass(lua, className)) {
Expand All @@ -300,18 +368,20 @@ public static boolean registerClass(Lua lua, String className, boolean canInstan
createMetaClassTemplate(lua, className);
}

lua.addImportListener(className, new ImportListener() {
@Override
public int onImport(LuaState luaState) {
// Return a metatable
if(!getMetaClassTable(lua, LuaTableType.CLASS, className)) {
luaState.luaL_error("Metatable don't exist");
if(addImport) {
lua.addImportListener(className, new ImportListener() {
@Override
public int onImport(LuaState luaState) {
// Return a metatable
if(!getMetaClassTable(lua, LuaTableType.CLASS, className)) {
luaState.luaL_error("Metatable don't exist");
}
luaState.lua_pushvalue(-1);
luaState.lua_setmetatable(-2); // Set Template metatable to new table
return 1;
}
luaState.lua_pushvalue(-1);
luaState.lua_setmetatable(-2); // Set Template metatable to new table
return 1;
}
});
});
}

return true;
}
Expand Down Expand Up @@ -364,8 +434,10 @@ public int onCall(LuaState luaState) {
}
});
}
luaState.lua_setfield(-2, TEMPLATE); // set Template to metatable
luaState.lua_setfield(-2, INSTANCE); // set Template to metatable
}

luaState.lua_pop(1);
}

private static void createMetaClassClass(Lua lua, String metaTable) {
Expand All @@ -390,7 +462,6 @@ private static void createMetaClassClass(Lua lua, String metaTable) {
luaState.lua_setfield(-2, CLASS); // set Class to metatable
}

String s = lua.dumpTable();
luaState.lua_pop(1);
}

Expand Down
2 changes: 1 addition & 1 deletion lua/lua-core/src/main/java/lua/LuaTableType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum LuaTableType {
CLASS,
TEMPLATE,
CLASS_INSTANCE,
ENUM
}
15 changes: 15 additions & 0 deletions lua/lua-core/src/main/java/lua/register/LuaIntEnumData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lua.register;

import java.util.List;

public class LuaIntEnumData {
public String key;
public int value;

public static void addEnum(List<LuaIntEnumData> enumDataList, String key, int value) {
LuaIntEnumData data = new LuaIntEnumData();
data.key = key;
data.value = value;
enumDataList.add(data);
}
}
17 changes: 11 additions & 6 deletions lua/lua-core/src/main/java/lua/register/gdx/LuaGdx.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
import lua.LuaLibrary;
import lua.LuaTableType;
import lua.register.gdx.graphics.LuaGL20;
import lua.register.gdx.graphics.LuaGraphics;
import lua.register.gdx.graphics.glutils.LuaShapeRenderer;

public class LuaGdx {
public static final String CLASSNAME = "com.badlogic.gdx.Gdx";

public static void register(Lua lua) {
LuaGL20.registerGL(lua);
LuaGL20.register(lua);
LuaGraphics.register(lua);
LuaShapeRenderer.register(lua);

String gdxClassName = "com.badlogic.gdx.Gdx";
LuaLibrary.registerClass(lua, gdxClassName, false);

String glClassName = "com.badlogic.gdx.graphics.GL20";
LuaLibrary.registerClass(lua, LuaGdx.CLASSNAME, true, false);

if(LuaLibrary.createInstanceObject(lua, glClassName, Gdx.gl)) {
LuaLibrary.setMetaClassTable(lua, LuaTableType.CLASS, gdxClassName, "gl");
if(LuaLibrary.getMetaClassTable(lua, LuaTableType.CLASS, LuaGL20.CLASSNAME)) {
LuaLibrary.setMetaClassTable(lua, LuaTableType.CLASS, LuaGdx.CLASSNAME, "gl");
}

if(LuaLibrary.getMetaClassTable(lua, LuaTableType.CLASS, LuaGraphics.CLASSNAME)) {
LuaLibrary.setMetaClassTable(lua, LuaTableType.CLASS, LuaGdx.CLASSNAME, "graphics");
}

// LuaLibrary.setMetaClassFloat()
Expand Down
Loading

0 comments on commit 0cb4fa2

Please sign in to comment.