2017-11-23 15:51:31 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Operating System class
|
|
|
|
-----------------------------------------------------
|
|
|
|
local os_class = {}
|
|
|
|
os_class.__index = os_class
|
|
|
|
|
2017-11-24 21:54:13 +01:00
|
|
|
-- 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
|
2017-11-23 15:51:31 +01:00
|
|
|
function os_class:power_on(new_node_name)
|
|
|
|
if new_node_name then
|
2017-11-24 21:54:13 +01:00
|
|
|
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)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
2017-11-24 21:54:13 +01:00
|
|
|
self:set_app(self.appdata.launcher.current_app)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Power off the system
|
|
|
|
function os_class:power_off(new_node_name)
|
|
|
|
if new_node_name then
|
2017-11-24 21:54:13 +01:00
|
|
|
self:swap_node(new_node_name)
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
|
|
|
self.meta:set_string('formspec', "")
|
2017-11-24 22:11:45 +01:00
|
|
|
self:save()
|
2017-11-23 15:51:31 +01:00
|
|
|
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)
|
2017-11-23 18:08:07 +01:00
|
|
|
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
|
|
|
|
|
2017-11-23 15:51:31 +01:00
|
|
|
-----------------------------------------------------
|
|
|
|
-- Get Operating system object
|
|
|
|
-----------------------------------------------------
|
|
|
|
function laptop.os_get(pos)
|
2017-11-23 23:40:41 +01:00
|
|
|
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
|
2017-11-23 15:51:31 +01:00
|
|
|
end
|
2017-11-23 22:50:51 +01:00
|
|
|
|
|
|
|
-----------------------------------------------------
|
|
|
|
-- 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
|