hexcol_colour_picker/init.lua
2024-05-20 11:01:46 -04:00

63 lines
No EOL
1.7 KiB
Lua

hexcol_picker = {}
hexcol_picker.temp = {}
function hexcol_picker.show_menu(name,red)
local fs = "formspec_version[6]size[10.5,11]list[current_player;main;0.4,5.9;8,4;0]container[1,1]"
local px,py = 0,0
for i = 0,4095,1 do
fs = fs..string.format("item_image_button[%f,%f;0.255,0.255;hexcol:%01x%02x;c_%01x%02x;]",px,py,red,i,red,i)
px = px+0.25
if px == 4 then
px = 0
py = py+0.25
if py == 4 then break end
end
end
fs = fs.."container_end[]scrollbaroptions[max=15;smallstep=1]scrollbar[5,1;0.5,4;vertical;R;"..red.."]"
minetest.show_formspec(name, "hexcol_picker", fs)
end
minetest.register_chatcommand("cpicker", {
params = "",
description = "Show the colour picker",
func = function (name,params)
hexcol_picker.show_menu(name,tonumber(params) or 0)
end,
})
if unified_inventory then
unified_inventory.register_button("hexcol_picker", {
type = "image",
image = "cwheel.png",
tooltip = "Colour Picker",
action = function (player)
local name = player:get_player_name()
hexcol_picker.show_menu(name,0)
end,
hide_lite = false,
})
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "hexcol_picker" then return end
local inv = player:get_inventory()
local name = player:get_player_name()
if fields.R then
local r = minetest.explode_scrollbar_event(fields.R)
if r.type == "CHG" then
hexcol_picker.show_menu(name,r.value)
end
end
for key, value in pairs(fields) do
if string.sub(key,1,2) == "c_" then
print(string.sub(key,3))
inv:add_item("main", "hexcol:"..string.sub(key,3).." 99")
end
end
end)