Move painting function from myfences and add an API

master
Samuel Sieb 2016-03-20 17:34:39 -07:00
commit a36da59374
15 changed files with 314 additions and 0 deletions

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# mypaint
Use paint cans and brushes to paint items.
Licence - WTFPL

1
depends.txt Normal file
View File

@ -0,0 +1 @@
default

1
description.txt Normal file
View File

@ -0,0 +1 @@
Provides paint brushes and buckets for other mods to use to paint their items.

BIN
extras/mypaint.xcf Normal file

Binary file not shown.

Binary file not shown.

19
init.lua Normal file
View File

@ -0,0 +1,19 @@
mypaint = {}
mypaint.colors = {
red = {"Red", "ff0000"},
green = {"Green", "00ff00"},
white = {"White", "ffffff"},
}
mypaint.paintables = {}
mypaint.register = function(names, colors)
local ctable = {}
for _, color in ipairs(colors) do
ctable[color] = true
end
for _, name in ipairs(names) do
mypaint.paintables[name] = ctable
end
end
dofile(minetest.get_modpath("mypaint").."/paint.lua")

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name = mypaint

106
models/mypaint_can.obj Normal file
View File

@ -0,0 +1,106 @@
# Blender v2.77 (sub 0) OBJ File: ''
# www.blender.org
o Cylinder
v -0.000000 -0.500000 0.250000
v -0.000000 0.000000 0.250000
v -0.125000 -0.500000 0.216506
v -0.125000 0.000000 0.216506
v -0.216506 -0.500000 0.125000
v -0.216506 0.000000 0.125000
v -0.250000 -0.500000 0.000000
v -0.250000 0.000000 0.000000
v -0.216506 -0.500000 -0.125000
v -0.216506 0.000000 -0.125000
v -0.125000 -0.500000 -0.216506
v -0.125000 0.000000 -0.216506
v -0.000000 -0.500000 -0.250000
v -0.000000 0.000000 -0.250000
v 0.125000 -0.500000 -0.216506
v 0.125000 0.000000 -0.216506
v 0.216506 -0.500000 -0.125000
v 0.216506 0.000000 -0.125000
v 0.250000 -0.500000 -0.000000
v 0.250000 0.000000 -0.000000
v 0.216506 -0.500000 0.125000
v 0.216506 0.000000 0.125000
v 0.125000 -0.500000 0.216506
v 0.125000 0.000000 0.216506
vt 0.999881 0.000119
vt 0.999881 0.322018
vt 0.916568 0.322018
vt 0.916567 0.000119
vt 0.833254 0.322018
vt 0.833254 0.000119
vt 0.749941 0.322018
vt 0.749940 0.000119
vt 0.666627 0.322018
vt 0.666627 0.000119
vt 0.583314 0.322018
vt 0.583313 0.000119
vt 0.500000 0.322018
vt 0.500000 0.000119
vt 0.416687 0.322018
vt 0.416686 0.000119
vt 0.333373 0.322018
vt 0.333373 0.000119
vt 0.250060 0.322018
vt 0.250060 0.000119
vt 0.166746 0.322018
vt 0.166746 0.000119
vt 0.114068 0.633186
vt 0.041916 0.591529
vt 0.000260 0.519378
vt 0.000260 0.436064
vt 0.041916 0.363912
vt 0.114068 0.322256
vt 0.197381 0.322256
vt 0.269533 0.363912
vt 0.311190 0.436064
vt 0.311190 0.519377
vt 0.269533 0.591529
vt 0.197381 0.633186
vt 0.083433 0.322018
vt 0.083433 0.000119
vt 0.000119 0.322018
vt 0.000119 0.000119
vt 0.197241 0.322256
vt 0.269392 0.363913
vt 0.311049 0.436064
vt 0.311049 0.519378
vt 0.269392 0.591529
vt 0.197241 0.633186
vt 0.113927 0.633186
vt 0.041776 0.591529
vt 0.000119 0.519377
vt 0.000119 0.436064
vt 0.041776 0.363912
vt 0.113927 0.322256
vn -0.258800 0.000000 0.965900
vn -0.707100 0.000000 0.707100
vn -0.965900 0.000000 0.258800
vn -0.965900 0.000000 -0.258800
vn -0.707100 0.000000 -0.707100
vn -0.258800 0.000000 -0.965900
vn 0.258800 0.000000 -0.965900
vn 0.707100 0.000000 -0.707100
vn 0.965900 0.000000 -0.258800
vn 0.965900 0.000000 0.258800
vn 0.000000 1.000000 0.000000
vn 0.707100 0.000000 0.707100
vn 0.258800 0.000000 0.965900
vn 0.000000 -1.000000 0.000000
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/4/2 4/3/2 6/5/2 5/6/2
f 5/6/3 6/5/3 8/7/3 7/8/3
f 7/8/4 8/7/4 10/9/4 9/10/4
f 9/10/5 10/9/5 12/11/5 11/12/5
f 11/12/6 12/11/6 14/13/6 13/14/6
f 13/14/7 14/13/7 16/15/7 15/16/7
f 15/16/8 16/15/8 18/17/8 17/18/8
f 17/18/9 18/17/9 20/19/9 19/20/9
f 19/20/10 20/19/10 22/21/10 21/22/10
f 4/23/11 2/24/11 24/25/11 22/26/11 20/27/11 18/28/11 16/29/11 14/30/11 12/31/11 10/32/11 8/33/11 6/34/11
f 21/22/12 22/21/12 24/35/12 23/36/12
f 23/36/13 24/35/13 2/37/13 1/38/13
f 1/39/14 3/40/14 5/41/14 7/42/14 9/43/14 11/44/14 13/45/14 15/46/14 17/47/14 19/48/14 21/49/14 23/50/14

181
paint.lua Normal file
View File

@ -0,0 +1,181 @@
local BRUSH_USES = 3
local CAN_USES = 100
function check_paintcan(pos, node)
local name = node.name
if string.sub(name, 1, 14) ~= "mypaint:paint_" then
return
end
local color = string.sub(name, 15)
local meta = minetest.get_meta(pos)
local uses = meta:get_int("mypaint:uses") - 1
meta:set_int("mypaint:uses", uses)
if uses <= 0 then
minetest.dig_node(pos)
else
local info = meta:get_string("infotext")
info = string.gsub(info, "%(.*%)", "("..uses.." uses)")
meta:set_string("infotext", info)
end
return ItemStack("mypaint:brush_"..color)
end
function paint_node(pos, node, col, itemstack)
local s, e
local nname = node.name
minetest.log("error", "checking node "..nname)
s, e = string.find(nname, "_[^_]+$")
if s and e then
local color = string.sub(nname, s + 1, e)
minetest.log("error", "found color "..color)
if mypaint.colors[color] then
nname = string.sub(nname, 1, s - 1)
minetest.log("error", "color ok, base name is "..nname)
if color == col then
return
end
end
end
for name, colors in pairs(mypaint.paintables) do
if (nname == name) then
if colors[col] then
minetest.set_node(pos,{name = name.."_"..col, param2 = node.param2})
if not minetest.setting_getbool("creative_mode") then
local wear = itemstack:get_wear() + 65535 / BRUSH_USES
if wear < 65535 then
itemstack:set_wear(wear)
else
itemstack = ItemStack("mypaint:brush")
end
end
end
return itemstack
end
end
end
minetest.register_tool("mypaint:brush", {
description = "Paint Brush",
inventory_image = "mypaint_brush.png",
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
if string.sub(node.name, 1, 8) ~= "mypaint:" then
return
end
return check_paintcan(pos, node)
end
})
for color, entry in pairs(mypaint.colors) do
local desc = entry[1]
local cstring = entry[2]
minetest.register_tool("mypaint:brush_"..color, {
description = "Paint Brush ("..desc.." Paint)",
inventory_image = "mypaint_brush.png^(mypaint_brush_color.png^[colorize:#"..cstring..")",
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local ret = check_paintcan(pos, node)
if ret then
return ret
end
return paint_node(pos, node, color, itemstack)
end,
})
minetest.register_node("mypaint:paint_"..color, {
description = desc.." Paint",
drawtype = "mesh",
paramtype = "light",
paramtype2 = "facedir",
mesh = "mypaint_can.obj",
tiles = {"(mypaint_can_color.png^[colorize:#"..cstring..")^mypaint_can_base.png"},
stack_max = 1,
drop = "",
groups = {oddly_breakable_by_hand = 3, dig_immediate = 3, not_in_creative_inventory = 1},
selection_box = {
type = "fixed",
fixed = {
{-0.25, -0.5, -0.25, 0.25, 0., 0.25},
}
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if not digger then
return
end
local inv = digger:get_inventory()
if not inv then
return
end
local itemstack = ItemStack("mypaint:paintcan_"..color)
local uses = tonumber(oldmetadata.fields["mypaint:uses"])
if not uses then
uses = 100
end
if uses <= 0 then
return
end
itemstack:set_wear((CAN_USES - uses) * (65535 / CAN_USES))
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
minetest.add_item(pos, itemstack)
end
end
})
minetest.register_tool("mypaint:paintcan_"..color, {
description = desc.." Paint",
inventory_image = "mypaint_inv_can_base.png^(mypaint_inv_can_color.png^[colorize:#"..cstring.."^[mask:mypaint_inv_can_color.png)",
on_place = function(itemstack, user, pointed_thing)
local pname = "mypaint:paint_"..color
local paint = ItemStack(pname)
paint = minetest.item_place_node(paint, user, pointed_thing)
if not paint or (paint:get_count() > 0) then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local meta = minetest.get_meta(pos)
if node.name ~= pname or (meta:get_int("mypaint:uses") > 0) then
pos = pointed_thing.above
node = minetest.get_node(pos)
meta = minetest.get_meta(pos)
if node.name ~= pname or (meta:get_int("mypaint:uses") > 0) then
return
end
end
local uses = math.floor(CAN_USES - itemstack:get_wear() / (65535 / CAN_USES))
meta:set_int("mypaint:uses", uses)
meta:set_string("infotext", desc.." Paint ("..uses.." uses)")
itemstack:take_item()
return itemstack
end
})
minetest.register_craft({
output = "mypaint:paintcan_"..color,
recipe = {
{"bucket:bucket_water","dye:"..color}
},
replacements = {{"bucket:bucket_water","bucket:bucket_empty"}},
})
end
minetest.register_craft({
output = 'mypaint:brush',
recipe = {
{'wool:white'},
{'group:stick'},
}
})

BIN
textures/mypaint_brush.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB