113 lines
3.3 KiB
Lua
Raw Normal View History

-----------------------------------------------------
-- Operating System class
-----------------------------------------------------
local os_class = {}
os_class.__index = os_class
-- Swap the node
function os_class:swap_node(new_node_name)
local node = minetest.get_node(self.pos)
node.name = new_node_name
minetest.swap_node(self.pos, node)
end
-- Power on the system and start the launcher
function os_class:power_on(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
self:set_app("launcher")
end
-- Power on the system / and resume last running app
function os_class:resume(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
self:set_app(self.appdata.launcher.current_app)
end
-- Power off the system
function os_class:power_off(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
self.meta:set_string('formspec', "")
2017-11-24 22:11:45 +01:00
self:save()
end
-- Set infotext for system
function os_class:set_infotext(infotext)
self.meta:set_string('infotext', infotext)
end
2017-11-23 17:17:59 +01:00
function os_class:save()
2017-11-24 22:11:45 +01:00
self.appdata.launcher.custom = self.custom_launcher
2017-11-23 17:17:59 +01:00
self.meta:set_string('laptop_appdata', minetest.serialize(self.appdata))
end
-- Set infotext for system
function os_class:set_app(appname)
2017-11-24 22:11:45 +01:00
local name = appname or "launcher"
if name == "launcher" and self.custom_launcher then
name = self.custom_launcher
2017-11-23 17:17:59 +01:00
end
2017-11-24 22:11:45 +01:00
self.appdata.launcher.current_app = name
local app = laptop.get_app(name, self)
2017-11-23 17:17:59 +01:00
self.meta:set_string('formspec', app:get_formspec())
self:save()
end
function os_class:receive_fields(fields, sender)
2017-11-24 22:11:45 +01:00
local appname = self.appdata.launcher.current_app or self.custom_launcher or "launcher"
2017-11-23 17:17:59 +01:00
local app = laptop.get_app(appname, self)
app:receive_fields(fields, sender)
if self.appdata.launcher.current_app == appname then
self.meta:set_string('formspec', app:get_formspec())
end
self:save()
2017-11-23 17:17:59 +01:00
end
-----------------------------------------------------
-- Get Operating system object
-----------------------------------------------------
function laptop.os_get(pos)
local self = setmetatable({}, os_class)
2017-11-23 17:17:59 +01:00
self.__index = os_class
self.pos = pos
self.meta = minetest.get_meta(pos)
self.appdata = minetest.deserialize(self.meta:get_string('laptop_appdata')) or {}
self.appdata.launcher = self.appdata.launcher or {}
2017-11-24 22:11:45 +01:00
self.custom_launcher = self.appdata.launcher.custom
2017-11-23 17:17:59 +01:00
return self
end
-----------------------------------------------------
-- Hooks for node definition
-----------------------------------------------------
function laptop.after_place_node(pos, placer, itemstack, pointed_thing)
local appdata = minetest.deserialize(itemstack:get_meta():get_string("laptop_appdata"))
if appdata then
local os = laptop.os_get(pos)
os.appdata = appdata
os:save()
end
end
function laptop.after_dig_node(pos, oldnode, oldmetadata, digger)
local appdata = oldmetadata.fields['laptop_appdata']
if appdata then
local item_name = minetest.registered_items[oldnode.name].drop or oldnode.name
local inventory = digger:get_inventory()
for idx = inventory:get_size("main"), 1, -1 do
local stack = inventory:get_stack("main", idx)
if stack:get_name() == item_name and stack:get_meta():get_string("laptop_appdata") == "" then
stack:get_meta():set_string("laptop_appdata", appdata)
digger:get_inventory():set_stack("main", idx, stack)
break
end
end
end
end