From fc66c5ee9b0d8170c539874bbd34f257256f7053 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Sat, 12 Feb 2022 12:31:16 -0300 Subject: [PATCH] made the mod own painter --- automobiles_lib/README.md | 10 ++ automobiles_lib/init.lua | 103 +++++++------ automobiles_lib/painter.lua | 145 ++++++++++++++++++ .../textures/automobiles_painter.png | Bin 0 -> 5592 bytes 4 files changed, 207 insertions(+), 51 deletions(-) create mode 100755 automobiles_lib/README.md create mode 100644 automobiles_lib/painter.lua create mode 100644 automobiles_lib/textures/automobiles_painter.png diff --git a/automobiles_lib/README.md b/automobiles_lib/README.md new file mode 100755 index 0000000..a24dd4b --- /dev/null +++ b/automobiles_lib/README.md @@ -0,0 +1,10 @@ +Coupe + +This mod adds library for automobiles + +licence of code +LGPL3 +except painter.lua, licenced as MIT + +licence of the media: +licence CC0 diff --git a/automobiles_lib/init.lua b/automobiles_lib/init.lua index 85dc496..974c041 100755 --- a/automobiles_lib/init.lua +++ b/automobiles_lib/init.lua @@ -47,57 +47,6 @@ function automobiles_lib.minmax(v,m) return math.min(math.abs(v),m)*minekart.sign(v) end -function automobiles_lib.set_paint(self, puncher, itmstck) - local item_name = "" - if itmstck then item_name = itmstck:get_name() end - - if item_name == "bike:painter" then - --painting with bike painter - local meta = itmstck:get_meta() - local colstr = meta:get_string("paint_color") - automobiles_lib.paint(self, colstr) - return true - else - --painting with dyes - local split = string.split(item_name, ":") - local color, indx, _ - if split[1] then _,indx = split[1]:find('dye') end - if indx then - for clr,_ in pairs(automobiles_lib.colors) do - local _,x = split[2]:find(clr) - if x then color = clr end - end - --lets paint!!!! - --local color = item_name:sub(indx+1) - local colstr = automobiles_lib.colors[color] - --minetest.chat_send_all(color ..' '.. dump(colstr)) - if colstr then - automobiles_lib.paint(self, colstr) - itmstck:set_count(itmstck:get_count()-1) - puncher:set_wielded_item(itmstck) - return true - end - -- end painting - end - end - return false -end - ---painting -function automobiles_lib.paint(self, colstr) - if colstr then - self._color = colstr - local l_textures = self.initial_properties.textures - for _, texture in ipairs(l_textures) do - local indx = texture:find('automobiles_painting.png') - if indx then - l_textures[_] = "automobiles_painting.png^[multiply:".. colstr - end - end - self.object:set_properties({textures=l_textures}) - end -end - --returns 0 for old, 1 for new function automobiles_lib.detect_player_api(player) local player_proterties = player:get_properties() @@ -390,10 +339,62 @@ minetest.register_node("automobiles_lib:light", { }, }) +function automobiles_lib.set_paint(self, puncher, itmstck) + local item_name = "" + if itmstck then item_name = itmstck:get_name() end + + if item_name == "automobiles_lib:painter" or item_name == "bike:painter" then + --painting with bike painter + local meta = itmstck:get_meta() + local colstr = meta:get_string("paint_color") + automobiles_lib.paint(self, colstr) + return true + else + --painting with dyes + local split = string.split(item_name, ":") + local color, indx, _ + if split[1] then _,indx = split[1]:find('dye') end + if indx then + for clr,_ in pairs(automobiles_lib.colors) do + local _,x = split[2]:find(clr) + if x then color = clr end + end + --lets paint!!!! + --local color = item_name:sub(indx+1) + local colstr = automobiles_lib.colors[color] + --minetest.chat_send_all(color ..' '.. dump(colstr)) + if colstr then + automobiles_lib.paint(self, colstr) + itmstck:set_count(itmstck:get_count()-1) + puncher:set_wielded_item(itmstck) + return true + end + -- end painting + end + end + return false +end + +--painting +function automobiles_lib.paint(self, colstr) + if colstr then + self._color = colstr + local l_textures = self.initial_properties.textures + for _, texture in ipairs(l_textures) do + local indx = texture:find('automobiles_painting.png') + if indx then + l_textures[_] = "automobiles_painting.png^[multiply:".. colstr + end + end + self.object:set_properties({textures=l_textures}) + end +end + dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua") dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua") dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua") dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "ground_detection.lua") +dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "painter.lua") -- engine minetest.register_craftitem("automobiles_lib:engine",{ diff --git a/automobiles_lib/painter.lua b/automobiles_lib/painter.lua new file mode 100644 index 0000000..bfb7af4 --- /dev/null +++ b/automobiles_lib/painter.lua @@ -0,0 +1,145 @@ +--[[ +Copyright (C) 2022 Alexsandro Percy +Copyright (C) 2018 Hume2 + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +]]-- + +--[[painting functions adapted from bike mod]]-- + +local function is_hex(color) + if color:len() ~= 7 then return nil end + return color:match("#%x%x%x%x%x%x") +end + +-- hex translation +local function hex_to_rgb(hex_value) + hex_value = hex_value:gsub("#","") + local rgb = { + r = tonumber("0x"..hex_value:sub(1,2)), + g = tonumber("0x"..hex_value:sub(3,4)), + b = tonumber("0x"..hex_value:sub(5,6)), + } + return rgb +end + +local function rgb_to_hex(r, g, b) + return string.format("#%02X%02X%02X", r, g, b) +end + +-- Need to convert between 1000 units and 256 +local function from_slider_rgb(value) + value = tonumber(value) + return math.floor((255/1000*value)+0.5) +end + +-- ...and back +local function to_slider_rgb(value) + if value then + return 1000/255*value + end + return 1000/255*255 +end + +-- Painter formspec +local function painter_form(itemstack, player) + local meta = itemstack:get_meta() + local color = meta:get_string("paint_color") + if color == nil or color == "" then + color = "#FFFFFF" + meta:set_string("paint_color", color) + meta:set_string("description", "Automobiles Painter ("..color:upper()..")") + end + local rgb = hex_to_rgb(color) + minetest.show_formspec(player:get_player_name(), "automobiles_lib:painter", + -- Init formspec + "size[6,4.7;true]".. + "position[0.5, 0.45]".. + -- Preview + "label[0,0;Preview:]".. + "image[1.2,0;2,2;automobiles_painting.png^[colorize:"..color..":255]".. + -- RGB sliders + "scrollbar[0,2;5,0.3;horizontal;r;"..tostring(to_slider_rgb(rgb.r)).."]".. + "label[5.1,1.9;R: "..tostring(rgb.r).."]".. + "scrollbar[0,2.6;5,0.3;horizontal;g;"..tostring(to_slider_rgb(rgb.g)).."]".. + "label[5.1,2.5;G: "..tostring(rgb.g).."]".. + "scrollbar[0,3.2;5,0.3;horizontal;b;"..tostring(to_slider_rgb(rgb.b)).."]".. + "label[5.1,3.1;B: "..tostring(rgb.b).."]".. + -- Hex field + "field[0.3,4.5;2,0.8;hex;Hex Color;"..color.."]".. + "button[4.05,4.1;2,1;set;Set color]" + ) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "automobiles_lib:painter" then + local itemstack = player:get_wielded_item() + if fields.set then + if itemstack:get_name() == "automobiles_lib:painter" then + local meta = itemstack:get_meta() + local hex = fields.hex + if hex then + if is_hex(hex) == nil then + hex = "#FFFFFF" + end + -- Save color data to painter (rgba sliders will adjust to hex/alpha too!) + meta:set_string("paint_color", hex) + meta:set_string("description", "Automobiles Painter ("..hex:upper()..")") + player:set_wielded_item(itemstack) + painter_form(itemstack, player) + return + end + end + end + if fields.r or fields.g or fields.b then + if itemstack:get_name() == "automobiles_lib:painter" then + -- Save on slider adjustment (hex/alpha will adjust to match the rgba!) + local meta = itemstack:get_meta() + local function sval(value) + return from_slider_rgb(value:gsub(".*:", "")) + end + 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 + meta:set_string("description", "Automobiles Painter ("..meta:get_string("paint_color"):upper()..")") + player:set_wielded_item(itemstack) + painter_form(itemstack, player) + end + end + end +end) +--[[end of adaptations]]-- + + +-- Make the actual thingy +minetest.register_tool("automobiles_lib:painter", { + description = "Automobiles Painter", + inventory_image = "automobiles_painter.png", + wield_scale = {x = 2, y = 2, z = 1}, + on_place = painter_form, + on_secondary_use = painter_form, +}) + +minetest.register_craft({ + output = "automobiles_lib:painter", + recipe = { + {"", "default:steel_ingot", ""}, + {"dye:red", "dye:green", "dye:blue"}, + {"", "default:steel_ingot", ""}, + }, +}) diff --git a/automobiles_lib/textures/automobiles_painter.png b/automobiles_lib/textures/automobiles_painter.png new file mode 100644 index 0000000000000000000000000000000000000000..b67670ada03c6383775d62640c2ad43f290ec639 GIT binary patch literal 5592 zcmeHKdpJ~E8z0vkC6$V5VvLTF%zcKL$;fSD+(T~VvS((`n3(%8gQGf0Nu>+A&{do` zQYleKp(06?^wCAS=-`A(oRp$(&*=Jm=bxVEJO4G$vuE$U*6;nj@B3TtT5GROem-sn z`g8SRFqi?;-PIquPg7lby3oJb&<6kp(^;Pw7$Wxv6bP|Y#O23=2zi1SM1V>@7Y0+_ zD+rE}4%tq5e8-~8TlD7Vu7P48(t z`d^!gGU5?)=HO{@i#_h|c$sr(z_I<*)PpQ#gtM@?q{rjQD6y+$@=<*EtDMRY(*k(!ug(S^xMa|%XzW@pjP+?{Rh-XbN!Zvq$_8P4NTEhD1= zE_?=Gz|7WjXv}}0A9?QPl-vv!w=C^ycGNX(o{MI6Rg);PH#t@(&mtnk9NVp7d&}hd zR@C(xy}u29&xIWv8Jl}v-$btUXDMUG+NMTE{BWT*Vbg3AT3L*tf8l)%gmd&pj(ZI( z#6m*4T?0>pakQP%Z-?6OaE{Fq#?DxV{n<_S&RyA#ty%NgccS2RQ@kjHGitnN=!CY> zdQo@u42q0+`(>Rv#^KUubt_(+AGx?Kuh3~7`J#@QbDFu%))@kMOZ_yq*D@oNQ;WRe z=ysL|a~6Kqfm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