refactor api.exec => api.ext.exec + better docs
This commit is contained in:
parent
d1dae0d25e
commit
8598281836
7 changed files with 102 additions and 50 deletions
|
|
@ -18,7 +18,7 @@ The aim of this project is providing **one [XR Hypermedia browser](https://xrhf.
|
||||||
* Nintendo 3DS, Switch, and Wii via [lovebrew](https://lovebrew.org/)
|
* Nintendo 3DS, Switch, and Wii via [lovebrew](https://lovebrew.org/)
|
||||||
* Handhelds: Steamdeck, Anbernic, Gameforce, Game Kiddy, Powkiddy (..and more) via [portmaster](https://portmaster.games/supported-devices.html)
|
* Handhelds: Steamdeck, Anbernic, Gameforce, Game Kiddy, Powkiddy (..and more) via [portmaster](https://portmaster.games/supported-devices.html)
|
||||||
* Game emulators via [lutro](https://lutro.libretro.com/)
|
* Game emulators via [lutro](https://lutro.libretro.com/)
|
||||||
* Embedded devices via lua and/oror [nelua](https://nelua.org)
|
* Embedded devices via [nelua](https://nelua.io) or straightup [lua](https://lua.org)
|
||||||
|
|
||||||
## very hackable extensions
|
## very hackable extensions
|
||||||
|
|
||||||
|
|
|
||||||
46
api.md
46
api.md
|
|
@ -1,4 +1,42 @@
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<a href="">api</a>
|
||||||
|
<a href="">api.parser.json</a>
|
||||||
|
<a href="#api-parser-xml">api.parser.xml</a>
|
||||||
|
<a href="">api.url</a>
|
||||||
|
<a href="">api.util</a>
|
||||||
|
<a href="">api.protocol</a>
|
||||||
|
<a href="">api.ext.*</a>
|
||||||
|
<a href="#api-ext-exec">api.ext.exec(fn, ,..)</a>
|
||||||
|
<a href="https://bakpakin.github.io/tiny-ecs/doc">api.ecs</a>
|
||||||
|
<a href="https://bakpakin.github.io/tiny-ecs/doc">api.world</a>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
## Entity Component System (ECS)
|
||||||
|
|
||||||
|
Read the Full Documentation here <a href="https://bakpakin.github.io/tiny-ecs/doc">https://bakpakin.github.io/tiny-ecs/doc</a>
|
||||||
|
|
||||||
|
> Below are some helper functions to make things more efficient
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**commit()**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
obj = { foo = "bar", mycomponent = { a = 1 } }
|
||||||
|
api.ecs.add( api.world, obj )
|
||||||
|
|
||||||
|
obj.foo = "flop"
|
||||||
|
obj.commit('Xhappened')
|
||||||
|
-- commit notifies all systems to rebuild entitycache
|
||||||
|
-- because normally systems filter on an entitycache (=fast)
|
||||||
|
-- which would still rely on foo = "bar".
|
||||||
|
-- Only do this when you actually need a system to pick
|
||||||
|
-- up on a change.
|
||||||
|
```
|
||||||
|
|
||||||
|
> also <a href="#api-ext-exec">api.ext.exec('Xhappened')</a> is called so extensions can respond to the change.
|
||||||
|
|
||||||
## api.parser.xml
|
## api.parser.xml
|
||||||
|
|
||||||
|
|
@ -40,3 +78,11 @@ util.traverseXML( xml:ParseXmlText(testXml), function(node,raw)
|
||||||
-- }
|
-- }
|
||||||
end)
|
end)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## api.ext.exec
|
||||||
|
|
||||||
|
This allows batch-firing a function on each extension (if exist)
|
||||||
|
|
||||||
|
```lua
|
||||||
|
api.ext.exec('init', 123, "foo") -- call .init(123,"foo") on each extension
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
local api = ...
|
local api = ...
|
||||||
|
local ecs = api.ecs
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name = "skeleton",
|
name = "skeleton",
|
||||||
enabled = true,
|
enabled = true,
|
||||||
|
|
||||||
init = function() end,
|
init = function()
|
||||||
|
--api.ext.skeleton.extendECS()
|
||||||
|
end,
|
||||||
|
|
||||||
update = function()
|
update = function()
|
||||||
local iui = api.iui
|
local iui = api.iui
|
||||||
|
|
@ -20,5 +23,25 @@ return {
|
||||||
else
|
else
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- ECS API docs: https://bakpakin.github.io/tiny-ecs/doc
|
||||||
|
extendECS = function()
|
||||||
|
|
||||||
|
local exampleSystem = ecs.processingSystem({
|
||||||
|
updatethread = true,
|
||||||
|
-- drawthread = true
|
||||||
|
filter = ecs.requireAll('mycomponent'),
|
||||||
|
onAdd = function(self,obj) print_r(obj) end,
|
||||||
|
onRemove = function(self,obj) end,
|
||||||
|
process = function(self,obj) end,
|
||||||
|
})
|
||||||
|
ecs.addSystem( api.world, exampleSystem )
|
||||||
|
|
||||||
|
-- add example entity object
|
||||||
|
local obj = { foo = "bar", mycomponent = { a = 1 } }
|
||||||
|
ecs.add( api.world, obj )
|
||||||
|
obj.commit() -- notify other systems to update entitycache
|
||||||
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,24 @@
|
||||||
local api = ...
|
local api = ...
|
||||||
|
local ecs = api.ecs
|
||||||
local xrf = require("ext/xrfragments/lovr-xrf")
|
local xrf = require("ext/xrfragments/lovr-xrf")
|
||||||
local xrfsystem
|
local xrfsystem
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
init = function()
|
|
||||||
-- create a (ecs) system which detects add/remove entities
|
|
||||||
local ecs = api.ecs
|
|
||||||
xrfsystem = ecs.system({
|
|
||||||
|
|
||||||
filter = ecs.requireAll('x', 'y', 'z', 'model'),
|
init = function() end,
|
||||||
nocache = true,
|
|
||||||
filterDynamic = true,
|
|
||||||
|
|
||||||
onAdd = function(self,obj)
|
|
||||||
|
|
||||||
if lovr ~= nil then
|
|
||||||
xrf.traverseNodesContaining('href', obj,
|
|
||||||
xrf.makeClickable( api.ecs.worldPhysics,
|
|
||||||
function(obj, collider)
|
|
||||||
print(obj['name'] .. ".href => " .. obj['extras']['href'] )
|
|
||||||
api.world.add({collider = collider})
|
|
||||||
end
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
onRemove = function(self,obj)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
})
|
|
||||||
ecs.addSystem( api.world, xrfsystem )
|
|
||||||
|
|
||||||
|
on3DFile = function(obj)
|
||||||
|
if lovr ~= nil then
|
||||||
|
xrf.traverseNodesContaining('href', obj,
|
||||||
|
xrf.makeClickable( api.ecs.worldPhysics,
|
||||||
|
function(obj, collider)
|
||||||
|
print(obj['name'] .. ".href => " .. obj['extras']['href'] )
|
||||||
|
api.world.add({collider = collider})
|
||||||
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ function lovr.load()
|
||||||
|
|
||||||
iui.load(backend)
|
iui.load(backend)
|
||||||
|
|
||||||
api.exec( api.ext, "load")
|
api.ext.exec("load")
|
||||||
|
|
||||||
if iui.idiom == "vr" then
|
if iui.idiom == "vr" then
|
||||||
lovr.headset.setPassthrough("opaque")
|
lovr.headset.setPassthrough("opaque")
|
||||||
|
|
@ -50,7 +50,7 @@ function lovr.load()
|
||||||
end
|
end
|
||||||
|
|
||||||
function lovr.update(dt)
|
function lovr.update(dt)
|
||||||
|
|
||||||
ecs.update( api.world, dt, ecs.filterUpdate )
|
ecs.update( api.world, dt, ecs.filterUpdate )
|
||||||
|
|
||||||
iui.beginFrame(dt)
|
iui.beginFrame(dt)
|
||||||
|
|
@ -59,7 +59,7 @@ function lovr.update(dt)
|
||||||
-- In desktop mode, we use IUI's standard window API to fill the screen.
|
-- In desktop mode, we use IUI's standard window API to fill the screen.
|
||||||
iui.beginWindow(lovr.system.getWindowDimensions())
|
iui.beginWindow(lovr.system.getWindowDimensions())
|
||||||
|
|
||||||
api.exec( api.ext, "update", dt)
|
api.ext.exec("update", dt)
|
||||||
|
|
||||||
iui.endWindow()
|
iui.endWindow()
|
||||||
|
|
||||||
|
|
@ -69,7 +69,7 @@ function lovr.update(dt)
|
||||||
if mainWindow:beginFrame() then
|
if mainWindow:beginFrame() then
|
||||||
iui.beginWindow(mainWindow.w, mainWindow.h)
|
iui.beginWindow(mainWindow.w, mainWindow.h)
|
||||||
|
|
||||||
api.exec( api.ext, "update", dt)
|
api.ext.exec("update", dt)
|
||||||
|
|
||||||
iui.endWindow()
|
iui.endWindow()
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ function lovr.draw(pass)
|
||||||
pass:setClear(0.5, 0.5, 0.5)
|
pass:setClear(0.5, 0.5, 0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
api.exec(api.ext, "draw",pass)
|
api.ext.exec("draw",pass)
|
||||||
ecs.update( api.world, pass, ecs.filterDraw )
|
ecs.update( api.world, pass, ecs.filterDraw )
|
||||||
|
|
||||||
-- Dot
|
-- Dot
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,9 @@ api = {
|
||||||
ecs = ecs,
|
ecs = ecs,
|
||||||
world = ecs.world(),
|
world = ecs.world(),
|
||||||
protocol = {},
|
protocol = {},
|
||||||
ext = {}, -- all extensions loaded from disk at runtime
|
ext = { -- all extensions are loaded here from disk at runtime
|
||||||
exec = function(...) util.exec(...) end -- calls function on each table item
|
exec = function(...) util.exec(api.ext,...) end -- util function to call func on each extension
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local runtime
|
local runtime
|
||||||
|
|
@ -28,7 +29,7 @@ require( runtime.path .. "/main")
|
||||||
util.loaddir( "ext", api, api.ext )
|
util.loaddir( "ext", api, api.ext )
|
||||||
util.loaddir( "media", api, api.media )
|
util.loaddir( "media", api, api.media )
|
||||||
ecs.init()
|
ecs.init()
|
||||||
api.exec( api.ext, 'init')
|
api.ext.exec('init')
|
||||||
|
|
||||||
-- GLB URLS
|
-- GLB URLS
|
||||||
--local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo'
|
--local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo'
|
||||||
|
|
@ -41,5 +42,4 @@ api.exec( api.ext, 'init')
|
||||||
--local url = 'https://snips.sh/f/rHFLg-cewi?r=1' -- cube
|
--local url = 'https://snips.sh/f/rHFLg-cewi?r=1' -- cube
|
||||||
local url = 'https://snips.sh/f/_U5-XctEVE?r=1' -- cube, monkey, scene
|
local url = 'https://snips.sh/f/_U5-XctEVE?r=1' -- cube, monkey, scene
|
||||||
|
|
||||||
|
|
||||||
api.ecs.add( api.world, { URI = { url = url, method = 'GET', target = '_top' } })
|
api.ecs.add( api.world, { URI = { url = url, method = 'GET', target = '_top' } })
|
||||||
|
|
|
||||||
19
src/util.lua
19
src/util.lua
|
|
@ -12,15 +12,9 @@ util.loaddir = function( path, api, target)
|
||||||
end
|
end
|
||||||
|
|
||||||
util.exec = function(obj, fn,a,b,c,d,e,f)
|
util.exec = function(obj, fn,a,b,c,d,e,f)
|
||||||
foreach( obj,
|
foreach( obj,
|
||||||
when('enabled',true, util.call(fn,a,b,c,d,e,f) )
|
when('enabled',true, util.call(fn,a,b,c,d,e,f) )
|
||||||
)
|
)
|
||||||
end
|
|
||||||
|
|
||||||
function when(k,v,cb)
|
|
||||||
return function(kk,vv)
|
|
||||||
if vv[k] == v then cb(kk,vv) end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.merge(t1,t2)
|
function util.merge(t1,t2)
|
||||||
|
|
@ -141,10 +135,13 @@ end
|
||||||
|
|
||||||
function when(k,v,cb)
|
function when(k,v,cb)
|
||||||
return function(kk,vv)
|
return function(kk,vv)
|
||||||
if vv[k] == v then cb(kk,vv) end
|
if type(vv) == 'table' then
|
||||||
|
if vv[k] == v then cb(kk,vv) end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function util.merge(t1,t2)
|
function util.merge(t1,t2)
|
||||||
local t3 = {}
|
local t3 = {}
|
||||||
foreach( t1, function(k,v) t3[k] = v end)
|
foreach( t1, function(k,v) t3[k] = v end)
|
||||||
|
|
@ -179,7 +176,7 @@ util.commit = function(world, obj, api)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
api.exec( api.ext, extCb, obj)
|
api.ext.exec(extCb, obj)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue