Convert tabs to spaces in painter.lua

This commit is contained in:
Johannes Fritz 2022-07-31 13:32:14 -05:00
parent 6fe62c6659
commit 133261427e

View File

@ -24,127 +24,127 @@ Copyright (C) 2018 Hume2
--[[painting functions adapted from bike mod]]-- --[[painting functions adapted from bike mod]]--
local function is_hex(color) local function is_hex(color)
if color:len() ~= 7 then return nil end if color:len() ~= 7 then return nil end
return color:match("#%x%x%x%x%x%x") return color:match("#%x%x%x%x%x%x")
end end
-- hex translation -- hex translation
local function hex_to_rgb(hex_value) local function hex_to_rgb(hex_value)
hex_value = hex_value:gsub("#","") hex_value = hex_value:gsub("#","")
local rgb = { local rgb = {
r = tonumber("0x"..hex_value:sub(1,2)), r = tonumber("0x"..hex_value:sub(1,2)),
g = tonumber("0x"..hex_value:sub(3,4)), g = tonumber("0x"..hex_value:sub(3,4)),
b = tonumber("0x"..hex_value:sub(5,6)), b = tonumber("0x"..hex_value:sub(5,6)),
} }
return rgb return rgb
end end
local function rgb_to_hex(r, g, b) local function rgb_to_hex(r, g, b)
return string.format("#%02X%02X%02X", r, g, b) return string.format("#%02X%02X%02X", r, g, b)
end end
-- Painter formspec -- Painter formspec
local function painter_form(itemstack, player) local function painter_form(itemstack, player)
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local color = meta:get_string("paint_color") local color = meta:get_string("paint_color")
if color == nil or color == "" then if color == nil or color == "" then
color = "#FFFFFF" color = "#FFFFFF"
meta:set_string("paint_color", color) meta:set_string("paint_color", color)
meta:set_string("description", "Automobiles Painter ("..color:upper()..")") meta:set_string("description", "Automobiles Painter ("..color:upper()..")")
end end
local rgb = hex_to_rgb(color) local rgb = hex_to_rgb(color)
minetest.show_formspec(player:get_player_name(), "automobiles_lib:painter", minetest.show_formspec(player:get_player_name(), "automobiles_lib:painter",
-- Init formspec -- Init formspec
"size[6,4.7;true]".. "size[6,4.7;true]"..
"position[0.5, 0.45]".. "position[0.5, 0.45]"..
-- Preview -- Preview
"label[0,0;Preview:]".. "label[0,0;Preview:]"..
"image[1.2,0;2,2;automobiles_painting.png^[colorize:"..color..":255]".. "image[1.2,0;2,2;automobiles_painting.png^[colorize:"..color..":255]"..
-- RGB sliders -- RGB sliders
"scrollbaroptions[min=0;max=255]".. "scrollbaroptions[min=0;max=255]"..
"scrollbar[0,2;5,0.3;horizontal;r;"..rgb.r.."]".. "scrollbar[0,2;5,0.3;horizontal;r;"..rgb.r.."]"..
"label[5.1,1.9;R: "..rgb.r.."]".. "label[5.1,1.9;R: "..rgb.r.."]"..
"scrollbar[0,2.6;5,0.3;horizontal;g;"..rgb.g.."]".. "scrollbar[0,2.6;5,0.3;horizontal;g;"..rgb.g.."]"..
"label[5.1,2.5;G: "..rgb.g.."]".. "label[5.1,2.5;G: "..rgb.g.."]"..
"scrollbar[0,3.2;5,0.3;horizontal;b;"..rgb.b.."]".. "scrollbar[0,3.2;5,0.3;horizontal;b;"..rgb.b.."]"..
"label[5.1,3.1;B: "..rgb.b.."]".. "label[5.1,3.1;B: "..rgb.b.."]"..
-- Hex field -- Hex field
"field[0.3,4.5;2,0.8;hex;Hex Color;"..color.."]".. "field[0.3,4.5;2,0.8;hex;Hex Color;"..color.."]"..
"button[4.05,4.1;2,1;set;Set color]" "button[4.05,4.1;2,1;set;Set color]"
) )
end end
local formspec_timers = {} local formspec_timers = {}
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "automobiles_lib:painter" then if formname == "automobiles_lib:painter" then
if formspec_timers[player] then if formspec_timers[player] then
formspec_timers[player]:cancel() formspec_timers[player]:cancel()
formspec_timers[player] = nil formspec_timers[player] = nil
end end
local itemstack = player:get_wielded_item() local itemstack = player:get_wielded_item()
if fields.set or fields.quit then if fields.set or fields.quit then
if itemstack:get_name() == "automobiles_lib:painter" then if itemstack:get_name() == "automobiles_lib:painter" then
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local hex = fields.hex local hex = fields.hex
if hex then if hex then
if is_hex(hex) == nil then if is_hex(hex) == nil then
hex = "#FFFFFF" hex = "#FFFFFF"
end end
-- Save color data to painter (rgba sliders will adjust to hex/alpha too!) -- Save color data to painter (rgba sliders will adjust to hex/alpha too!)
meta:set_string("paint_color", hex) meta:set_string("paint_color", hex)
meta:set_string("description", "Automobiles Painter ("..hex:upper()..")") meta:set_string("description", "Automobiles Painter ("..hex:upper()..")")
player:set_wielded_item(itemstack) player:set_wielded_item(itemstack)
painter_form(itemstack, player) painter_form(itemstack, player)
end end
end end
elseif fields.r or fields.g or fields.b then elseif fields.r or fields.g or fields.b then
if itemstack:get_name() == "automobiles_lib:painter" then if itemstack:get_name() == "automobiles_lib:painter" then
-- Save on slider adjustment (hex/alpha will adjust to match the rgba!) -- Save on slider adjustment (hex/alpha will adjust to match the rgba!)
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local function sval(value) local function sval(value)
local num = math.floor(tonumber((value:gsub(".*:", ""))) or 0) local num = math.floor(tonumber((value:gsub(".*:", ""))) or 0)
if num > 255 or num < 0 then num = 0 end if num > 255 or num < 0 then num = 0 end
return num return num
end end
meta:set_string("paint_color", rgb_to_hex(sval(fields.r),sval(fields.g),sval(fields.b))) meta:set_string("paint_color", rgb_to_hex(sval(fields.r),sval(fields.g),sval(fields.b)))
-- Keep track of what this painter is painting -- Keep track of what this painter is painting
meta:set_string("description", "Automobiles Painter ("..meta:get_string("paint_color"):upper()..")") meta:set_string("description", "Automobiles Painter ("..meta:get_string("paint_color"):upper()..")")
formspec_timers[player] = minetest.after(0.2, function(itemstack, name) formspec_timers[player] = minetest.after(0.2, function(itemstack, name)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then if player then
local wield = player:get_wielded_item() local wield = player:get_wielded_item()
if wield:get_name() == itemstack:get_name() then if wield:get_name() == itemstack:get_name() then
player:set_wielded_item(itemstack) player:set_wielded_item(itemstack)
painter_form(itemstack, player) painter_form(itemstack, player)
end end
end end
formspec_timers[player] = nil formspec_timers[player] = nil
end, itemstack, player:get_player_name()) end, itemstack, player:get_player_name())
end end
end end
return true return true
end end
end) end)
--[[end of adaptations]]-- --[[end of adaptations]]--
-- Make the actual thingy -- Make the actual thingy
minetest.register_tool("automobiles_lib:painter", { minetest.register_tool("automobiles_lib:painter", {
description = "Automobiles Painter", description = "Automobiles Painter",
inventory_image = "automobiles_painter.png", inventory_image = "automobiles_painter.png",
wield_scale = {x = 2, y = 2, z = 1}, wield_scale = {x = 2, y = 2, z = 1},
on_place = painter_form, on_place = painter_form,
on_secondary_use = painter_form, on_secondary_use = painter_form,
}) })
minetest.register_craft({ minetest.register_craft({
output = "automobiles_lib:painter", output = "automobiles_lib:painter",
recipe = { recipe = {
{"", "default:steel_ingot", ""}, {"", "default:steel_ingot", ""},
{"dye:red", "dye:green", "dye:blue"}, {"dye:red", "dye:green", "dye:blue"},
{"", "default:steel_ingot", ""}, {"", "default:steel_ingot", ""},
}, },
}) })