Merge pull request #8 from bell07/master

Laptop settings app to change launcher background
This commit is contained in:
Gerold55 2017-11-30 07:17:31 -05:00 committed by GitHub
commit b86857c36b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 14 deletions

2
API.md
View File

@ -32,6 +32,6 @@ Usable from node functions, from apps or outsite
## 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
- `data = app:get_storage_ref()` - 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.
- `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:sync_storage()` - Store internal data (eg. background_img) to app storage
- `app.background_img` - Background image from definition. Can be changed at runtime

View File

@ -3,6 +3,7 @@ laptop.apps = {}
local app_class = {}
app_class.__index = app_class
-- internally used: get current app formspec
function app_class:get_formspec()
if self.formspec_func then
local app_result = self.formspec_func(self, self.os)
@ -16,18 +17,20 @@ function app_class:get_formspec()
end
end
-- internally used: process input
function app_class:receive_fields(fields, sender)
if self.receive_fields_func then
return self.receive_fields_func(self, self.os, fields, sender)
end
end
-- Sync attributes to storage (save background_img)
function app_class:sync_storage()
if self.background_img then
local data = self:get_storage_ref()
data._background_img = self.background_img
data.background_img = self.background_img
elseif self.os.appdata[self.name] then
self.os.appdata[self.name]._background_img = self.background_img
self.os.appdata[self.name].background_img = self.background_img
-- remove table if empty
if not next(self.os.appdata[self.name]) then
self.os.appdata[self.name] = nil
@ -35,17 +38,21 @@ function app_class:sync_storage()
end
end
function app_class:get_storage_ref()
if not self.os.appdata[self.name] then
self.os.appdata[self.name] = {}
-- Get persitant storage table
function app_class:get_storage_ref(app_name)
local store_name = app_name or self.name
if not self.os.appdata[store_name] then
self.os.appdata[store_name] = {}
end
return self.os.appdata[self.name]
return self.os.appdata[store_name]
end
-- Register new app
function laptop.register_app(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
@ -55,7 +62,7 @@ function laptop.get_app(name, os)
app.name = name
app.os = os
if os.appdata[name] then
app.background_img = os.appdata[name]._background_img or app.background_img
app.background_img = os.appdata[name].background_img or app.background_img
end
return app
end

View File

@ -18,7 +18,7 @@ laptop.register_app("launcher", {
local x = math.floor((i-1) / c_row_count)*2 + 1
local y = ((i-1) % c_row_count)*2 + 1
out = out .. 'image_button['..x..','..y..';1,1;'..(e.def.app_icon or 'logo.png')..';'..e.name..';]'..
'label['..(x-0.5)..','..(y+1)..';'..e.def.app_name..']'..
'label['..(x-0.3)..','..(y+1)..';'..e.def.app_name..']'..
'tooltip['..e.name..';'..(e.def.app_info or e.name)..']' --;<bgcolor>;<fontcolor>]'
end
return out

View File

@ -0,0 +1,72 @@
-- Load available backgrounds
local path = minetest.get_modpath('laptop')..'/textures/'
local background_tab = {}
for _, file in ipairs(minetest.get_dir_list(path, false)) do
if file:sub(1,10) == 'laptop_os_' then
table.insert(background_tab, file)
end
end
table.sort(background_tab)
laptop.register_app("launcher_settings", {
app_name = "Laptop Settings",
app_icon = "laptop_setting_wrench.png",
app_info = "Change laptop settings",
formspec_func = function(app, os)
local settings_data = app:get_storage_ref()
local launcher_data = app:get_storage_ref("launcher")
-- Change background setting
local bgr_img = settings_data.bgr_img or launcher_data.background_img or laptop.apps.launcher.background_img
local bgr_selected_idx
local formspec = "label[0,0;Select background]"
local formspec = formspec.."textlist[0,1;5,2;sel_bg;"
for i, img in ipairs(background_tab) do
if i > 1 then
formspec = formspec..','
end
if img == bgr_img then
bgr_selected_idx = i
end
formspec = formspec..img:sub(11,-5)
end
if bgr_selected_idx then
formspec = formspec..";"..bgr_selected_idx
end
formspec = formspec.."]"
if bgr_img then
formspec = formspec.."image[5.5,1;5,3.75;"..bgr_img.."]"
end
formspec = formspec.."button[1,3;3,1;bgr_apply;Apply background]"
-- Exit/Quit
formspec = formspec.."button[1,7;3,1;back;Exit settings]"
return formspec
end,
receive_fields_func = function(app, os, fields, sender)
local settings_data = app:get_storage_ref()
local launcher_data = app:get_storage_ref("launcher")
if fields.sel_bg then
-- CHG:<idx> for selected or DCL:<idx> for double-clicked
local bgr_selected_idx = tonumber(fields.sel_bg:sub(5))
settings_data.bgr_img = background_tab[bgr_selected_idx]
end
if fields.bgr_apply then
launcher_data.background_img = settings_data.bgr_img
settings_data.bgr_img = nil
os:set_app("launcher")
elseif fields.back then
settings_data.bgr_img = nil
os:set_app("launcher")
end
end
})

View File

@ -5,4 +5,4 @@ dofile(minetest.get_modpath('laptop')..'/os.lua')
dofile(minetest.get_modpath('laptop')..'/nodes.lua')
-- uncomment this line to disable demo apps in production
dofile(minetest.get_modpath('laptop')..'/demo_apps.lua')
--dofile(minetest.get_modpath('laptop')..'/demo_apps.lua')

View File

@ -172,10 +172,6 @@ minetest.register_node("laptop:monitor_off", {
groups = {choppy=2, oddly_breakably_by_hand=2, dig_immediate = 2},
on_punch = function (pos, node, puncher)
local os = laptop.os_get(pos)
-- change lauchher background - swap background for all instances on punch
local app = laptop.get_app("launcher", os)
app.background_img = "laptop_os_main.png"
app:sync_storage()
os:power_on("laptop:monitor_on")
end,
after_place_node = laptop.after_place_node,