API cleanups
This commit is contained in:
parent
0525ccf9bc
commit
92916d8736
4
API.md
4
API.md
@ -36,6 +36,7 @@ Usable from node functions, from apps or outsite
|
||||
- `os:swap_node(new_node_name)`- Swap the node only without any changes on OS
|
||||
- `os:set_infotext(infotext)` - Set the mouseover infotext for laptop node
|
||||
- `os:save()` - Store all app-data to nodemeta. Called mostly internally so no explicit call necessary
|
||||
- `os:get_app(appname)`- Get the app instance
|
||||
- `os:set_app(appname)` - Start/Enable/navigate to appname. If no appname given the launcher is called
|
||||
- `os:receive_fields(fields, sender)` - Should be called from node.on_receive_fields to get the apps interactive
|
||||
- `os:get_theme(theme)`- Get theme data current or requested (theme parameter is optional)
|
||||
@ -51,12 +52,13 @@ Usable from node functions, from apps or outsite
|
||||
- `fullscreen` - (boolean) Do not add app-background and window buttons
|
||||
- `view` - (boolean) The definition is a view. That means the app/view is not visible in launcher
|
||||
- `formspec_func(app, os)` - Function, should return the app formspec (mandatory) During definition the "app" and the "os" are available
|
||||
- `appwindow_formspec_func(launcher_app, app, os)`- Only custom launcher app: App background / Window decorations and buttons
|
||||
- `receive_fields_func(app, os, fields, sender)` Function for input processing. The "app" and the "os" are available inside the call
|
||||
`laptop.register_view(internal_shortname, { definitiontable })` - add a new app or view
|
||||
same as register_app, but the view flag is set. app_name and app_icon not necessary
|
||||
|
||||
## App Object
|
||||
`local app = laptop.get_app(internal_shortname, os)` - Give the app object internal_shortname, connected to given os. Not necessary in formspec_func or receive_fields_func because given trough interface
|
||||
`local app = os:get_app(appname)` - Give the app object internal_shortname, connected to given os. Not necessary in formspec_func or receive_fields_func because given trough interface
|
||||
- `data = app:get_storage_ref(appname)` - Returns a "persitant" data table that means the data in this table is not lost between formspec_func, receive_fields_func, apps-switch or on/off. Appname is optional to get data from other app
|
||||
- `app:back_app() - Go back to previous app/view
|
||||
- `app:exit_app() - Delete call stack and return to launcher
|
||||
|
52
app_fw.lua
52
app_fw.lua
@ -2,37 +2,26 @@ laptop.apps = {}
|
||||
|
||||
local app_class = {}
|
||||
app_class.__index = app_class
|
||||
laptop.class_lib.app = app_class
|
||||
|
||||
-- internally used: get current app formspec
|
||||
function app_class:get_formspec()
|
||||
|
||||
local app_result
|
||||
if self.formspec_func then
|
||||
app_result = self.formspec_func(self, self.os)
|
||||
else
|
||||
app_result = ""
|
||||
end
|
||||
|
||||
if self.fullscreen then
|
||||
return app_result
|
||||
end
|
||||
|
||||
local formspec = 'size[15,10]'
|
||||
if self.os.theme.app_bg then
|
||||
formspec = formspec..'background[0,0;15,10;'..self.os.theme.app_bg..';true]'
|
||||
local launcher = self.os:get_app(self.os.custom_launcher or "launcher")
|
||||
local window_formspec = ""
|
||||
if launcher.appwindow_formspec_func then
|
||||
window_formspec = launcher.appwindow_formspec_func(launcher, self, self.os)
|
||||
end
|
||||
if #self.os.appdata.os.stack > 1 then
|
||||
formspec = formspec..'image_button[-0.29,-0.31;1.09,0.61;'..self.os.theme.back_button..';os_back;<]' --TODO: if stack exists
|
||||
end
|
||||
if self.app_info then
|
||||
if #self.os.appdata.os.stack > 1 then
|
||||
formspec = formspec.."label[0.8,-0.29;"..self.app_info.."]"
|
||||
else
|
||||
formspec = formspec.."label[-0.1,-0.29;"..self.app_info.."]"
|
||||
end
|
||||
end
|
||||
formspec = formspec..'image_button[14.2,-0.31;1.09,0.61;'..self.os.theme.exit_button..';os_exit;X]'
|
||||
return formspec..app_result
|
||||
return window_formspec..app_result
|
||||
end
|
||||
|
||||
-- internally used: process input
|
||||
@ -59,24 +48,13 @@ function app_class:get_storage_ref(app_name)
|
||||
return self.os.appdata[store_name]
|
||||
end
|
||||
|
||||
-- Back to previous app in stack
|
||||
function app_class:back_app()
|
||||
local stacksize = #self.os.appdata.os.stack
|
||||
|
||||
-- pop stack
|
||||
if stacksize > 0 then
|
||||
self.os.appdata.os.stack[stacksize] = nil
|
||||
stacksize = #self.os.appdata.os.stack
|
||||
end
|
||||
|
||||
if stacksize == 0 then
|
||||
self.os:set_app() -- launcher
|
||||
else
|
||||
self.os:set_app(self.os.appdata.os.stack[stacksize])
|
||||
end
|
||||
self.os:set_app(self.os:appstack_pop())
|
||||
end
|
||||
|
||||
-- Exit current app and back to launcher
|
||||
function app_class:exit_app()
|
||||
self.os.appdata.os.stack = {}
|
||||
self.os:set_app() -- launcher
|
||||
end
|
||||
|
||||
@ -91,18 +69,6 @@ function laptop.register_view(name, def)
|
||||
laptop.apps[name] = def
|
||||
end
|
||||
|
||||
-- Get app instance for object
|
||||
function laptop.get_app(name, os)
|
||||
local template = laptop.apps[name]
|
||||
if not template then
|
||||
return
|
||||
end
|
||||
local app = setmetatable(table.copy(template), app_class)
|
||||
app.name = name
|
||||
app.os = os
|
||||
return app
|
||||
end
|
||||
|
||||
-- load all apps
|
||||
local app_path = minetest.get_modpath('laptop')..'/apps/'
|
||||
local app_list = minetest.get_dir_list(app_path, false)
|
||||
|
@ -2,7 +2,7 @@
|
||||
laptop.register_app("launcher", {
|
||||
-- app_name = "Main launcher", -- not in launcher list
|
||||
fullscreen = true,
|
||||
formspec_func = function(app, os)
|
||||
formspec_func = function(launcher_app, os)
|
||||
local c_row_count = 4
|
||||
|
||||
local i = 0
|
||||
@ -26,7 +26,25 @@ laptop.register_app("launcher", {
|
||||
end
|
||||
return out
|
||||
end,
|
||||
receive_fields_func = function(app, os, fields, sender)
|
||||
appwindow_formspec_func = function(launcher_app, app, os)
|
||||
local formspec = 'size[15,10]'
|
||||
if os.theme.app_bg then
|
||||
formspec = formspec..'background[0,0;15,10;'..os.theme.app_bg..';true]'
|
||||
end
|
||||
if #os.appdata.os.stack > 0 then
|
||||
formspec = formspec..'image_button[-0.29,-0.31;1.09,0.61;'..os.theme.back_button..';os_back;<]' --TODO: if stack exists
|
||||
end
|
||||
if app.app_info then
|
||||
if #os.appdata.os.stack > 1 then
|
||||
formspec = formspec.."label[0.8,-0.29;"..app.app_info.."]"
|
||||
else
|
||||
formspec = formspec.."label[-0.1,-0.29;"..app.app_info.."]"
|
||||
end
|
||||
end
|
||||
formspec = formspec..'image_button[14.2,-0.31;1.09,0.61;'..os.theme.exit_button..';os_exit;X]'
|
||||
return formspec
|
||||
end,
|
||||
receive_fields_func = function(launcher_app, os, fields, sender)
|
||||
for name, descr in pairs(fields) do
|
||||
if laptop.apps[name] then
|
||||
os:set_app(name)
|
||||
|
2
init.lua
2
init.lua
@ -1,5 +1,5 @@
|
||||
laptop = {}
|
||||
|
||||
laptop.class_lib = {}
|
||||
dofile(minetest.get_modpath('laptop')..'/themes.lua')
|
||||
dofile(minetest.get_modpath('laptop')..'/app_fw.lua')
|
||||
dofile(minetest.get_modpath('laptop')..'/os.lua')
|
||||
|
64
os.lua
64
os.lua
@ -3,6 +3,7 @@
|
||||
-----------------------------------------------------
|
||||
local os_class = {}
|
||||
os_class.__index = os_class
|
||||
laptop.class_lib.os = os_class
|
||||
|
||||
-- Swap the node
|
||||
function os_class:swap_node(new_node_name)
|
||||
@ -16,7 +17,7 @@ function os_class:power_on(new_node_name)
|
||||
if new_node_name then
|
||||
self:swap_node(new_node_name)
|
||||
end
|
||||
self:set_app("launcher")
|
||||
self:set_app() --launcher
|
||||
end
|
||||
|
||||
-- Power on the system / and resume last running app
|
||||
@ -71,31 +72,61 @@ function os_class:set_theme(theme)
|
||||
end
|
||||
end
|
||||
|
||||
-- Set infotext for system
|
||||
-- Add app to stack (before starting new)
|
||||
function os_class:appstack_add(appname)
|
||||
table.insert(self.appdata.os.stack, appname)
|
||||
end
|
||||
|
||||
-- Get last app from stack
|
||||
function os_class:appstack_pop()
|
||||
local ret
|
||||
if #self.appdata.os.stack > 0 then
|
||||
ret = self.appdata.os.stack[#self.appdata.os.stack]
|
||||
table.remove(self.appdata.os.stack, #self.appdata.os.stack)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
-- Free stack
|
||||
function os_class:appstack_free()
|
||||
self.appdata.os.stack = {}
|
||||
end
|
||||
|
||||
-- Get new app instance
|
||||
function os_class:get_app(name)
|
||||
local template = laptop.apps[name]
|
||||
if not template then
|
||||
return
|
||||
end
|
||||
local app = setmetatable(table.copy(template), laptop.class_lib.app)
|
||||
app.name = name
|
||||
app.os = self
|
||||
return app
|
||||
end
|
||||
|
||||
-- Activate the app
|
||||
function os_class:set_app(appname)
|
||||
local name = appname or "launcher"
|
||||
local launcher = self.custom_launcher or "launcher"
|
||||
local newapp = appname or launcher
|
||||
|
||||
if name == "launcher" and self.custom_launcher then
|
||||
name = self.custom_launcher
|
||||
if newapp == launcher then
|
||||
self:appstack_free()
|
||||
elseif self.appdata.os.current_app and
|
||||
self.appdata.os.current_app ~= launcher and
|
||||
self.appdata.os.current_app ~= newapp then
|
||||
os_class:appstack_add(self.appdata.os.current_app)
|
||||
end
|
||||
|
||||
-- Add app to the stack
|
||||
local stacksize = #self.appdata.os.stack
|
||||
if (stacksize == 0 or self.appdata.os.stack[stacksize] ~= name) and
|
||||
name ~= (self.custom_launcher or "launcher") then
|
||||
table.insert(self.appdata.os.stack, name)
|
||||
end
|
||||
|
||||
|
||||
self.appdata.os.current_app = name
|
||||
local app = laptop.get_app(name, self)
|
||||
self.appdata.os.current_app = newapp
|
||||
local app = self:get_app(newapp)
|
||||
self.meta:set_string('formspec', app:get_formspec())
|
||||
self:save()
|
||||
end
|
||||
|
||||
-- Handle input processing
|
||||
function os_class:receive_fields(fields, sender)
|
||||
local appname = self.appdata.os.current_app or self.custom_launcher or "launcher"
|
||||
local app = laptop.get_app(appname, self)
|
||||
local app = self:get_app(appname)
|
||||
app:receive_fields(fields, sender)
|
||||
self.appdata.os.last_player = sender:get_player_name()
|
||||
if self.appdata.os.current_app == appname then
|
||||
@ -103,7 +134,6 @@ function os_class:receive_fields(fields, sender)
|
||||
end
|
||||
self:save()
|
||||
end
|
||||
|
||||
-----------------------------------------------------
|
||||
-- Get Operating system object
|
||||
-----------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user