aboutsummaryrefslogtreecommitdiff
path: root/display_lib
diff options
context:
space:
mode:
authorPierre-Yves Rollo <dev@pyrollo.com>2015-11-11 14:26:39 +0100
committerPierre-Yves Rollo <dev@pyrollo.com>2015-11-11 14:26:39 +0100
commitd44bf53ea20136221d605e139f2b58328b68ce9c (patch)
tree4f747301de219c9743b19efe536f340af90ce5ff /display_lib
downloaddisplay_modpack-d44bf53ea20136221d605e139f2b58328b68ce9c.tar.gz
display_modpack-d44bf53ea20136221d605e139f2b58328b68ce9c.tar.bz2
display_modpack-d44bf53ea20136221d605e139f2b58328b68ce9c.zip
First commit
Diffstat (limited to 'display_lib')
-rw-r--r--display_lib/API.md77
-rw-r--r--display_lib/LICENSE.txt13
-rw-r--r--display_lib/README.md11
-rw-r--r--display_lib/depends.txt1
-rw-r--r--display_lib/init.lua156
5 files changed, 258 insertions, 0 deletions
diff --git a/display_lib/API.md b/display_lib/API.md
new file mode 100644
index 0000000..7bfaaef
--- /dev/null
+++ b/display_lib/API.md
@@ -0,0 +1,77 @@
+# Display Lib API
+This document describes Display Lib API. Display Lib allows to add a dynamic display on a node. Node must be wallmounted and Display Lib limits its rotation to vertical positions.
+## Provided methods
+### update\_entities
+**display\_lib.update\_entities(pos)**
+
+This method triggers entities update for the display node at pos. Actual entity update is made by **on\_display\_update** callback associated to the entity.
+
+**pos**: Position of the node
+### register\_display\_entity
+**display\_lib.register\_display\_entity(entity_name)**
+
+This is a helper to register entities used for display.
+
+**entity_name**: Name of the entity to register.
+## Provided callback implementations
+### on_place
+**display\_lib.on\_place(itemstack, placer, pointed\_thing)**
+
+**On_place** node callback implementation. Display nodes should have this callback (avoid placement of horizontal display node).
+### on_construct
+**display\_lib.on\_construct(pos)**
+
+**On_construct** node callback implementation. Display nodes should have this callback (creates, places and updates display entities on node construction).
+### on_destruct
+**display\_lib.on_destruct(pos)**
+
+**On_destruct** node callback implementation. Display nodes should have this callback (removes display entities on node destruction).
+### on_rotate
+**display\_lib.on\_rotate(pos, node, user, mode, new_param2)**
+
+**On_rotate** node callback implementation. Display nodes should have this callback (restricts rotations and rotates display entities associated with node).
+### on_activate
+**display\_lib.on_activate(entity, staticdata)**
+
+**On_activate** entity callback implementation for display entities. No need of this method if display entities have been registered using **register\_display\_entity** (callback is already set).
+## Howto register a display node
+* Register display entities with **register\_display\_entity**
+* Register node with :
+ - **on\_place**, **on\_construct**, **on\_destruct** and **on\_rotate** callbacks using **display\_lib** callbacks.
+ - a **display\_entities** field in node definition containing a entity name indexed table. For each entity, two fields : **depth** indicates the entity position (-0.5 to 0.5) and **on_display_update** is a callback in charge of setting up entity texture.
+
+### Example
+
+ display_lib.register_display_entity("mymod:entity1")
+ display_lib.register_display_entity("mymod:entity2")
+
+ function my_display_update1(pos, objref)
+ objref:set_properties({ textures= {"mytexture1.png"},
+ visual_size = {x=1, y=1} })
+ end
+
+ function my_display_update2(pos, objref)
+ objref:set_properties({ textures= {"mytexture2.png"},
+ visual_size = {x=1, y=1} })
+ end
+
+ minetest.register_node("mymod:test_display_node", {
+ ...
+ paramtype2 = "wallmounted",
+ ...
+ display_entities = {
+ ["mymod:entity1"] = { depth = -0.3,
+ on_display_update = my_display_update1},
+ ["mymod:entity1"] = { depth = -0.2,
+ on_display_update = my_display_update2},
+ },
+ ...
+ on_place = display_lib.on_place,
+ on_construct = display_lib.on_construct,
+ on_destruct = display_lib.on_destruct,
+ on_rotate = display_lib.on_rotate,
+ ...
+ })
+
+
+
diff --git a/display_lib/LICENSE.txt b/display_lib/LICENSE.txt
new file mode 100644
index 0000000..bc06764
--- /dev/null
+++ b/display_lib/LICENSE.txt
@@ -0,0 +1,13 @@
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
diff --git a/display_lib/README.md b/display_lib/README.md
new file mode 100644
index 0000000..7ca97b2
--- /dev/null
+++ b/display_lib/README.md
@@ -0,0 +1,11 @@
+# Dislpay Lib
+
+This library's purpose is to ease creation of wallmounted nodes with a display on front side. For example, signs and clocks. Display can be dynamic and/or different for each node instance.
+
+**Limitations**: This lib uses entities to draw display. This means display has to be vertical. So display nodes are only wallmounted vertically.
+
+**Dependancies**:default
+
+**License**: WTFPL
+
+**API**: See API.md document please.
diff --git a/display_lib/depends.txt b/display_lib/depends.txt
new file mode 100644
index 0000000..4ad96d5
--- /dev/null
+++ b/display_lib/depends.txt
@@ -0,0 +1 @@
+default
diff --git a/display_lib/init.lua b/display_lib/init.lua
new file mode 100644
index 0000000..eaddfd3
--- /dev/null
+++ b/display_lib/init.lua
@@ -0,0 +1,156 @@
+-- Display lib mod by P.Y. Rollo
+--
+-- License: WTFPL
+
+display_lib = {}
+
+-- Miscelaneous values depending on wallmounted param2
+local wallmounted_values = {
+ [0]={dx=0, dz=0, lx=0, lz=0, yaw=0, rotate=0}, -- Should never be used
+ {dx=1, dz=0, lx=0, lz=0, yaw=0, rotate=1}, -- Should never be used
+ {dx=1, dz=0, lx=0, lz=-1, yaw=-math.pi/2, rotate=4},
+ {dx=-1, dz=0, lx=0, lz=1, yaw=math.pi/2, rotate=5},
+ {dx=0, dz=1, lx=1, lz=0, yaw=0, rotate=3},
+ {dx=0, dz=-1, lx=-1, lz=0, yaw=math.pi, rotate=2}
+}
+
+--- Gets the display entities attached with a node. Removes extra ones
+local function get_entities(pos)
+ local objrefs = {}
+ local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
+ if ndef and ndef.display_entities then
+ for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
+ local entity = objref:get_luaentity()
+ if entity and ndef.display_entities[entity.name] then
+ if objrefs[entity.name] then
+ objref:remove()
+ else
+ objrefs[entity.name] = objref
+ end
+ end
+ end
+ end
+ return objrefs
+end
+
+local function clip_pos_prop(posprop)
+ if posprop then
+ return math.max(-0.5, math.min(0.5, posprop))
+ else
+ return 0
+ end
+end
+
+--- (Create and) place display entities according to the node orientation
+local function place_entities(pos)
+ local node = minetest.get_node(pos)
+ local ndef = minetest.registered_nodes[node.name]
+ local values = wallmounted_values[node.param2]
+ local objrefs = get_entities(pos)
+
+ if ndef and ndef.display_entities then
+ for entity_name, props in pairs(ndef.display_entities) do
+ local depth = clip_pos_prop(props.depth)
+ local top = clip_pos_prop(props.top)
+ local left = clip_pos_prop(props.left)
+
+ if not objrefs[entity_name] then
+ objrefs[entity_name] = minetest.add_entity(pos, entity_name)
+ end
+
+ objrefs[entity_name]:setpos({
+ x = pos.x - values.dx * depth + values.lx * left,
+ y = pos.y + top,
+ z = pos.z - values.dz * depth + values.lz * left})
+
+ objrefs[entity_name]:setyaw(values.yaw)
+ end
+ end
+ return objrefs
+end
+
+--- Call on_display_update callback of a node for one of its display entities
+local function call_node_on_display_update(pos, objref)
+ local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
+ local entity = objref:get_luaentity()
+ if ndef and ndef.display_entities and entity and ndef.display_entities[entity.name] then
+ ndef.display_entities[entity.name].on_display_update(pos, objref)
+ end
+end
+
+--- Force entity update
+function display_lib.update_entities(pos)
+ local objrefs = place_entities(pos)
+ for _, objref in pairs(objrefs) do
+ call_node_on_display_update(pos, objref)
+ end
+end
+
+--- On_activate callback for display_lib entities. Calls on_display_update callbacks
+--- of corresponding node for each entity.
+function display_lib.on_activate(entity, staticdata)
+ if entity then
+ call_node_on_display_update(entity.object:getpos(), entity.object)
+ end
+end
+
+--- On_place callback for display_lib items. Does nothing more than preventing item
+--- from being placed on ceiling or ground
+function display_lib.on_place(itemstack, placer, pointed_thing)
+ local above = pointed_thing.above
+ local under = pointed_thing.under
+ local dir = {x = under.x - above.x,
+ y = under.y - above.y,
+ z = under.z - above.z}
+ local wdir = minetest.dir_to_wallmounted(dir)
+
+ if wdir == 0 or wdir == 1 then
+ dir = placer:get_look_dir()
+ dir.y = 0
+ wdir = minetest.dir_to_wallmounted(dir)
+ end
+ return minetest.item_place(itemstack, placer, pointed_thing, wdir)
+end
+
+--- On_construct callback for display_lib items. Creates entities and update them.
+function display_lib.on_construct(pos)
+ display_lib.update_entities(pos)
+end
+
+--- On_destruct callback for display_lib items. Removes entities.
+function display_lib.on_destruct(pos)
+ local objrefs = get_entities(pos)
+
+ for _, objref in pairs(objrefs) do
+ objref:remove()
+ end
+end
+
+
+-- On_rotate (screwdriver) callback for display_lib items. Prevents axis rotation and reorients entities.
+function display_lib.on_rotate(pos, node, user, mode, new_param2)
+ if mode ~= screwdriver.ROTATE_FACE then return false end
+
+ if wallmounted_values[node.param2] then
+ minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = wallmounted_values[node.param2].rotate})
+ place_entities(pos)
+ return true
+ else
+ return false
+ end
+end
+
+--- Creates display entity with some fields and the on_activate callback
+function display_lib.register_display_entity(entity_name)
+ if not minetest.registered_entity then
+ minetest.register_entity(':'..entity_name, {
+ collisionbox = { 0, 0, 0, 0, 0, 0 },
+ visual = "upright_sprite",
+ textures = {},
+ on_activate = display_lib.on_activate,
+ })
+ end
+end
+
+
+