Merge pull request #10 from bell07/master

Themes support
This commit is contained in:
Gerold55 2017-12-03 16:53:00 -05:00 committed by GitHub
commit 0ea9cb97ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 112 additions and 78 deletions

7
API.md
View File

@ -19,19 +19,20 @@ Usable from node functions, from apps or outsite
- `os:save()` - Store all app-data to nodemeta. Called mostly internally so no explicit call necessary
- `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)
- `os:set_theme(theme)`- Activate theme
- `os.custom_launcher` - Replacement for default "launcher" app. Can be set/changed in node constructor or anytime at runtime
## App Definition
`laptop.register_app(internal_shortname, { definitiontable })` - add a new app or view
- `app_name` - App name shown in launcher. If not defined the app is just a view, not visible in launcher but can be activated. This way multi-screen apps are possible
- `app_icon` - Icon to be shown in launcher. If nothing given the default icon is used
- `app_info` - Short app info visible in launcher tooltip
- `background_img` - if set the image is added as background to formspec by framework
- `fullscreen` - Do not add app-background and window buttons
- `formspec_func(app, os)` - Function, should return the app formspec (mandatory) During definition the "app" and the "os" are available
- `receive_fields_func(app, os, fields, sender)` Function for input processing. The "app" and the "os" are available inside the call
## 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(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

@ -5,16 +5,23 @@ 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)
local formspec = 'size[15,10]'
if self.background_img then
formspec = formspec..'background[15,10;0,0;'..self.background_img..';true]'
end
return formspec..app_result
local app_result
if self.formspec_func then
app_result = self.formspec_func(self, self.os)
else
return ""
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[15,10;0,0;'..self.os.theme.app_bg..';true]'
end
return formspec..app_result
end
-- internally used: process input
@ -24,20 +31,6 @@ function app_class:receive_fields(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
elseif self.os.appdata[self.name] then
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
end
end
end
-- Get persitant storage table
function app_class:get_storage_ref(app_name)
local store_name = app_name or self.name
@ -61,9 +54,6 @@ function laptop.get_app(name, os)
local app = setmetatable(table.copy(template), app_class)
app.name = name
app.os = os
if os.appdata[name] then
app.background_img = os.appdata[name].background_img or app.background_img
end
return app
end

View File

@ -1,12 +1,15 @@
laptop.register_app("launcher", {
-- app_name = "Main launcher", -- not in launcher list
background_img = "laptop_os_main2.png",
fullscreen = true,
formspec_func = function(app, os)
local c_row_count = 4
local i = 0
local out = ""
local out = "size[15,10]"
if os.theme.launcher_bg then
out = out..'background[15,10;0,0;'..os.theme.launcher_bg..';true]'
end
local appslist_sorted = {}
for name, def in pairs(laptop.apps) do
if def.app_name then

View File

@ -1,52 +1,51 @@
-- 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)
-- Load available Themes
local themes_tab = {}
for name, _ in pairs(laptop.themes) do
if name ~= "default" then
table.insert(themes_tab, name)
end
end
table.sort(background_tab)
table.sort(themes_tab)
laptop.register_app("launcher_settings", {
app_name = "Laptop Settings",
app_name = "Settings",
app_icon = "laptop_setting_wrench.png",
app_info = "Change laptop settings",
app_info = "Change the computer's 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 current_theme_name = settings_data.selected_theme or os:get_theme().name or "default"
local current_theme = os:get_theme(current_theme_name)
local current_idx
local formspec = "label[0,0;Select background]"
local formspec = "label[0,0;Select theme]"
local formspec = formspec.."textlist[0,1;5,2;sel_bg;"
for i, img in ipairs(background_tab) do
local formspec = formspec.."textlist[0,1;5,2;sel_theme;"
for i, theme in ipairs(themes_tab) do
if i > 1 then
formspec = formspec..','
end
if img == bgr_img then
bgr_selected_idx = i
if theme == current_theme_name then
current_idx = i
end
formspec = formspec..img:sub(11,-5)
formspec = formspec..theme
end
if bgr_selected_idx then
formspec = formspec..";"..bgr_selected_idx
if current_idx then
formspec = formspec..";"..current_idx
end
formspec = formspec.."]"
if bgr_img then
formspec = formspec.."image[5.5,1;5,3.75;"..bgr_img.."]"
if current_theme then
formspec = formspec.."image[5.5,1;5,3.75;"..current_theme.launcher_bg.."]"
end
formspec = formspec.."button[1,3;3,1;bgr_apply;Apply background]"
formspec = formspec..'image_button[-0.14,3;3,1;'..current_theme.major_button..';theme_apply;Apply]'
-- Exit/Quit
formspec = formspec.."button[1,7;3,1;back;Exit settings]"
formspec = formspec..'image_button[2.36,3;3,1;'..current_theme.minor_button..';back;Cancel]'
return formspec
end,
@ -54,18 +53,18 @@ laptop.register_app("launcher_settings", {
local settings_data = app:get_storage_ref()
local launcher_data = app:get_storage_ref("launcher")
if fields.sel_bg then
if fields.sel_theme 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]
local selected_idx = tonumber(fields.sel_theme:sub(5))
settings_data.selected_theme = themes_tab[selected_idx]
end
if fields.bgr_apply then
launcher_data.background_img = settings_data.bgr_img
settings_data.bgr_img = nil
if fields.theme_apply and settings_data.selected_theme then
os:set_theme(settings_data.selected_theme)
settings_data.selected_theme = nil
os:set_app("launcher")
elseif fields.back then
settings_data.bgr_img = nil
settings_data.selected_theme = nil
os:set_app("launcher")
end
end

View File

@ -1,13 +1,13 @@
laptop.register_app("stickynote", {
app_name = "Sticky note pad",
app_name = "Notepad",
app_icon = "laptop_notes_pad.png",
app_info = "Write notes in simple text editor",
app_info = "Write notes in a text document.",
formspec_func = function(app, os)
local data = app:get_storage_ref()
data.text = data.text or ""
return "textarea[1,1;13,9;text;Sticky Note Pad;"..minetest.formspec_escape(data.text).."]"..
'button[1,9;3,1;back;Back]'
return "textarea[0,0.35;15.58,10.5;text;;"..minetest.formspec_escape(data.text).."]"..
'image_button[0.5,9.51;3,1;'..os.theme.major_button..';back;Save]'
end,
receive_fields_func = function(app, os, fields, sender)
if fields.text then

View File

@ -4,7 +4,7 @@ laptop.register_app("demo1", {
app_icon = "laptop_setting_wrench.png",
app_info = "The first and simple demo app",
formspec_func = function(app, os)
return 'button[5,5;3,1;back;Back to launcher]'
return 'image_button[5,5;3,1;'..os.theme.major_button..';back;Back to launcher]'
end,
receive_fields_func = function(app, os, fields, sender)
if fields.back then
@ -19,9 +19,6 @@ laptop.register_app("demo2", {
local data = app:get_storage_ref()
data.counter = data.counter or 1
if data.counter % 2 == 0 then
app.background_img = "laptop_background1.png"
end
return 'button[3,1;5,1;count;Click: '..data.counter..']'..
'button[3,3;5,1;back;Back to launcher]'
end,

View File

@ -1,5 +1,6 @@
laptop = {}
dofile(minetest.get_modpath('laptop')..'/themes.lua')
dofile(minetest.get_modpath('laptop')..'/app_fw.lua')
dofile(minetest.get_modpath('laptop')..'/os.lua')
dofile(minetest.get_modpath('laptop')..'/nodes.lua')

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name = laptop

View File

@ -179,10 +179,7 @@ minetest.register_node("laptop:monitor_off", {
stack_max = 1,
on_construct = function(pos)
local os = laptop.os_get(pos)
-- change lauchher background
local app = laptop.get_app("launcher", os)
app.background_img = "laptop_os_main.png"
app:sync_storage()
os:set_theme("red")
os:power_off()
os:set_infotext('MT Desktop')
end,
@ -219,9 +216,7 @@ minetest.register_node("laptop:monitor2_on", {
end,
on_construct = function(pos)
local os = laptop.os_get(pos)
local app = laptop.get_app("launcher", os)
app.background_img = "laptop_os_main.png"
app:sync_storage()
os:set_theme("red")
os:power_on()
os:set_infotext('MT Desktop')
end,
@ -484,4 +479,4 @@ minetest.register_craft({
{'default:copper_ingot', 'default:copper_ingot', 'default:copper_ingot'},
{'default:steel_ingot', 'default:glass', 'default:steel_ingot'},
}
})
})

26
os.lua
View File

@ -41,11 +41,36 @@ function os_class:set_infotext(infotext)
self.meta:set_string('infotext', infotext)
end
-- Save the data
function os_class:save()
self.appdata.launcher.custom = self.custom_launcher
self.meta:set_string('laptop_appdata', minetest.serialize(self.appdata))
end
-- Get given or current theme
function os_class:get_theme(theme)
local theme_sel = theme or self.appdata.launcher.theme
local ret = table.copy(laptop.themes.default)
if theme_sel and laptop.themes[theme_sel] then
for k,v in pairs(laptop.themes[theme_sel]) do
ret[k] = v
end
ret.name = theme_sel
else
ret.name = "default"
end
return ret
end
-- Set current theme
function os_class:set_theme(theme)
if laptop.themes[theme] then
self.appdata.launcher.theme = theme
self.theme = self:get_theme()
self:save()
end
end
-- Set infotext for system
function os_class:set_app(appname)
local name = appname or "launcher"
@ -80,6 +105,7 @@ function laptop.os_get(pos)
self.appdata = minetest.deserialize(self.meta:get_string('laptop_appdata')) or {}
self.appdata.launcher = self.appdata.launcher or {}
self.custom_launcher = self.appdata.launcher.custom
self.theme = self:get_theme()
return self
end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

View File

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 44 KiB

View File

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 KiB

21
themes.lua Normal file
View File

@ -0,0 +1,21 @@
laptop.themes = {
blue = {
launcher_bg = "laptop_theme_blue_launcher_bg.png",
app_bg = "laptop_theme_blue_app_bg.png",
major_button = "laptop_theme_blue_major_button.png",
minor_button = "laptop_theme_blue_minor_button.png",
back_button = "", --TODO
exit_button = "", --TODO
},
red = {
launcher_bg = "laptop_theme_red_launcher_bg.png",
},
freedom = {
launcher_bg = "laptop_theme_freedom_launcher_bg.png",
},
cubic = {
launcher_bg = "laptop_theme_cubic_launcher_bg.png",
},
}
laptop.themes.default = laptop.themes.blue -- default can be an complete theme only