From c01a5fa7344199900436372e464033ad75a72bb3 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Wed, 21 Oct 2020 18:37:38 +1300 Subject: [PATCH] Add WIP digistuff touchscreen exporting --- README.md | 2 + digistuff_ts.lua | 111 +++++++++++++++++++++++++++++++++++++++++++++++ fs51/init.lua | 2 +- index.html | 2 +- index.lua | 49 +++++++++++++-------- renderer.lua | 25 ++++++----- 6 files changed, 161 insertions(+), 30 deletions(-) create mode 100644 digistuff_ts.lua diff --git a/README.md b/README.md index e783fb4..95c723b 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ default (dynamically loaded when required). if you plan to import them again. - The ability to load existing formspecs (provided they are version 2 or above). + - The ability to export to (but not import from) digistuff touchscreen + formspecs. ## Limitations diff --git a/digistuff_ts.lua b/digistuff_ts.lua new file mode 100644 index 0000000..abbce11 --- /dev/null +++ b/digistuff_ts.lua @@ -0,0 +1,111 @@ +-- +-- Formspec AST to digistuff touchscreen converter +-- +-- Copyright © 2020 by luk3yx. +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU Affero General Public License as +-- published by the Free Software Foundation, either version 3 of the +-- License, or (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General Public License +-- along with this program. If not, see . +-- + +local basic_types = {} +for _, t in ipairs({'image', 'field', 'pwdfield', 'textarea', 'label', + 'vertlabel', 'button', 'button_exit'}) do + basic_types[t] = true +end + +local function export(tree, backport_func) + tree = formspec_ast.flatten(tree) + + -- Modify box nodes before calling backport_func() to fix alignment + for node in formspec_ast.find(tree, 'box') do + -- Boxes can be partially emulated with images. + node.type = 'image' + node.texture_name = 'halo.png^[colorize:' .. node.color + node.color = nil + end + + if backport_func then + tree = backport_func(tree) + end + + local fs_v2 = (tree.formspec_version or 1) >= 2 + tree.formspec_version = nil + if tree[1] and tree[1].type == 'size' then + table.remove(tree, 1) + end + for _, node in ipairs(tree) do + if node.type == 'dropdown' or node.type == 'textlist' then + node.selected_id, node.selected_idx = node.selected_idx, nil + node.choices, node.item = node.item, nil + -- Later versions of the digustuff mod require a height field even + -- for formspec version 1 (breaking dropdowns on older clients). + node.h = node.h or 0.81 + elseif node.type == 'image_button' or + node.type == 'image_button_exit' then + node.image, node.texture_name = node.texture_name, nil + elseif not basic_types[node.type] then + return nil, 'Unsupported node type: ' .. node.type + end + + if not node.command then + node.command = 'add' .. node.type + end + node.type = nil + for _, i in ipairs({"x", "y", "w", "h"}) do + if node[i] then + node[i:upper()] = node[i] + node[i] = nil + end + end + end + table.insert(tree, 1, {command = 'clear'}) + table.insert(tree, 2, {command = 'realcoordinates', enabled = fs_v2}) + return tree +end + +local function very_basic_dump(obj) + local obj_type = type(obj) + if obj_type == 'string' then + return ('%q'):format(obj) + elseif obj_type ~= 'table' then + return tostring(obj) + end + + local t = {} + for k, v in pairs(obj) do + if type(k) ~= 'string' or not k:match('^[A-Za-z_]+$') then + k = '[' .. very_basic_dump(k) .. ']' + end + local line = k .. ' = ' .. very_basic_dump(v) + if k == 'command' then + table.insert(t, 1, line) + else + table.insert(t, line) + end + end + + return '{' .. table.concat(t, ', ') .. '}' +end + +local function export_string(...) + local res, err = export(...) + if not res then + return nil, err + end + for k, v in ipairs(res) do + res[k] = very_basic_dump(v) + end + return '{' .. table.concat(res, ',\n') .. '}' +end + +return export, export_string diff --git a/fs51/init.lua b/fs51/init.lua index 40a49cc..ccbcdbf 100644 --- a/fs51/init.lua +++ b/fs51/init.lua @@ -74,7 +74,7 @@ fixers.item_image = fixers.image function fixers.button(elem) elem.type = 'image_' .. elem.type - elem.texture_name = '' + elem.texture_name = 'blank.png' return fixers.image_button(elem) end fixers.button_exit = fixers.button diff --git a/index.html b/index.html index 4af718f..da9c1c0 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ - +