205 lines
6.9 KiB
Lua
Raw Normal View History

2022-02-12 17:13:16 -05:00
--local usage
local futil = computers.formspec
2022-02-12 16:36:11 -05:00
function computers.load_gui(pos, node, clicker)
2022-02-12 17:13:16 -05:00
--minetest.chat_send_all("test")
2022-02-12 19:21:39 -05:00
local function select_btn(form, btn)
--to hardcoded
for _, obtn in pairs({"terminal", "browser"}) do
local cindex = futil.get_index_by_name(form, obtn .. "_btn")
form[cindex].props.bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70"
form[cindex].props.bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff90"
cindex = futil.get_index_by_name(form, obtn .. "_ctn")
form[cindex].state = 1
end
local cindex = futil.get_index_by_name(form, btn .. "_btn")
form[cindex].props.bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff20"
form[cindex].props.bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff40"
local aindex = futil.get_index_by_name(form, btn .. "_ctn")
form[aindex].state = 0
end
2022-02-12 17:13:16 -05:00
local formspec = {
formspec_version = 4,
{
type = "size",
w = 10,
2022-02-12 19:21:39 -05:00
h = 12,
2022-02-12 17:13:16 -05:00
},
{
2022-02-12 19:21:39 -05:00
type = "no_prepend"
},
{
type = "bgcolor",
bgcolor = "black",
fullscreen = "neither"
2022-02-12 17:13:16 -05:00
},
{
type = "button",
2022-02-12 19:21:39 -05:00
x = 0,
y = 0,
w = 2,
h = 1,
name = "terminal_btn",
label = "Terminal",
2022-02-12 17:13:16 -05:00
on_event = function(form, player, element)
2022-02-12 19:21:39 -05:00
select_btn(form, "terminal")
2022-02-12 17:13:16 -05:00
return form
end,
props = {
border = false,
bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70",
bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff90",
bgimg_middle = "4,4",
}
2022-02-12 19:21:39 -05:00
},
{
type = "button",
x = 2,
y = 0,
w = 2,
h = 1,
name = "browser_btn",
label = "Browser",
on_event = function(form, player, element)
select_btn(form, "browser")
return form
end,
props = {
border = false,
bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70",
bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff90",
bgimg_middle = "4,4",
}
},
{
type = "container",
name = "terminal_ctn",
state = 0,
x = 0,
y = 1,
{
type = "background",
x = 0,
y = 0,
w = 10,
h = 11,
2022-02-12 20:52:12 -05:00
texture_name = "[combine:16x16^[noalpha"
2022-02-12 19:21:39 -05:00
},
{
2022-02-12 20:52:12 -05:00
type = "textarea",
x = 0,
y = 0,
w = 10,
h = 10,
name = "terminal_output",
read_only = 1,
--label = "test",
2022-02-13 00:31:20 -05:00
default = "welcome to kuto\nversion " .. computers.os.version .. "\n\nuser:~$ ",
2022-02-12 19:21:39 -05:00
},
{
2022-02-12 20:52:12 -05:00
type = "box",
x = 0,
y = 10,
w = 10,
h = 1,
color = "#ffffff"
},
{
type = "field",
x = 0,
y = 10,
w = 10,
h = 1,
name = "terminal_input",
close_on_enter = false,
2022-02-13 00:31:20 -05:00
pwd = "",
2022-02-12 20:52:12 -05:00
props = {
border = false,
},
on_event = function(form, player, element, value)
local cindex = futil.get_index_by_name(form, "terminal_ctn")
local eindex = futil.get_index_by_name(form[cindex], "terminal_output")
local text = form[cindex][eindex].default
2022-02-13 00:31:20 -05:00
local pass_table = {
element = element
}
2022-02-12 20:52:12 -05:00
if value == "clear" then
2022-02-13 00:31:20 -05:00
form[cindex][eindex].default = "user:~" .. element.pwd .."$" .."\n"
2022-02-12 20:52:12 -05:00
end
2022-02-12 19:21:39 -05:00
2022-02-12 21:56:17 -05:00
local cdata = value:split(" ", false, 1)
if computers.registered_commands[cdata[1]] then
2022-02-13 00:31:20 -05:00
form[cindex][eindex].default = text .. value .. "\n"
2022-02-12 21:56:17 -05:00
text = form[cindex][eindex].default
2022-02-13 00:31:20 -05:00
local output = computers.registered_commands[cdata[1]](pos, cdata[2], pass_table)
if output and type(output) == "string" then
form[cindex][eindex].default = text .. output .. "\n" .. "user:~" .. element.pwd .."$" .." "
2022-02-12 21:56:17 -05:00
end
elseif value == "clear" then
2022-02-13 00:31:20 -05:00
form[cindex][eindex].default = "user:~$ "
2022-02-12 21:56:17 -05:00
elseif value == "" then
2022-02-13 00:31:20 -05:00
form[cindex][eindex].default = text .. "user:~" .. element.pwd .."$" .. "\n"
2022-02-12 21:56:17 -05:00
else
2022-02-13 00:31:20 -05:00
form[cindex][eindex].default = text .. value ..
"\nERROR: command not found\n" .. "user:~" .. element.pwd .."$" .. " "
2022-02-12 21:56:17 -05:00
end
2022-02-12 19:21:39 -05:00
return form
end,
2022-02-12 20:52:12 -05:00
},
2022-02-12 19:21:39 -05:00
},
{
type = "container",
name = "browser_ctn",
state = 1,
x = 0,
y = 1,
{
type = "background",
x = 0,
y = 0,
w = 10,
h = 11,
texture_name = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70"
},
{
type = "label",
x = 1,
y = 1.5,
label = "browser pane",
},
2022-02-12 20:52:12 -05:00
{
type = "button",
x = 1,
y = 2,
w = 5,
h = 2,
name = "test_btn",
label = "test btn",
on_event = function(form, player, element)
local cindex = futil.get_index_by_name(form, "browser_ctn")
local eindex = futil.get_index_by_name(form[cindex], "test_btn")
form[cindex][eindex] = {type = "label", x=1, y=3, label = "test button label"}
return form
end,
props = {
border = false,
bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70",
bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff90",
bgimg_middle = "4,4",
}
}
},
2022-02-12 17:13:16 -05:00
}
2022-02-12 20:52:12 -05:00
futil.show_formspec(clicker, "computers:gui", formspec)
2022-02-12 16:36:11 -05:00
end