From 8ffef59da38f61730f0957647b769c91c16a8b18 Mon Sep 17 00:00:00 2001
From: entuland <entuland@gmail.com>
Date: Tue, 26 Jun 2018 01:32:20 +0200
Subject: [PATCH] moved and improved notification facility

---
 init.lua   | 65 ++++++------------------------------------------
 notify.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 57 deletions(-)
 create mode 100644 notify.lua

diff --git a/init.lua b/init.lua
index 84217db..a43a5de 100644
--- a/init.lua
+++ b/init.lua
@@ -2,6 +2,7 @@ rhotator = {}
 
 local mod_path = minetest.get_modpath(minetest.get_current_modname())
 local storage = minetest.get_mod_storage()
+local notify = dofile(mod_path .. "/notify.lua")
 
 -- constants
 
@@ -26,9 +27,6 @@ rhotator.SECONDARY_BTN = SECONDARY_BTN
 local rot_matrices = {}
 local dir_matrices = {}
 
-local huds = {}
-local hud_timeout_seconds = 3
-
 local facedir_memory = {}
 
 -- ============================================================
@@ -187,52 +185,6 @@ local function vector_to_dir_index(vec)
 	return (vec.y > 0) and POS.Y or NEG.Y
 end
 
--- ============================================================
--- hud functions
-
-local function hud_remove(player)
-	local playername = player:get_player_name()
-	local hud = huds[playername]
-	if not hud then return end
-	if os.time() < hud_timeout_seconds + hud.time then
-		return
-	end
-	player:hud_remove(hud.id)
-	huds[playername] = nil
-end
-
-local function hud_create(player, message)
-	local playername = player:get_player_name()
-	local id = player:hud_add({
-		text = message,
-		hud_elem_type = "text",
-		name = "rhotator_feedback",
-		direction = 0,
-		position = { x = 0.1, y = 0.9},
-		alignment = { x = 1, y = -1},
-		number = 0xFFFFFF,
-	})
-	huds[playername] = {
-		id = id,
-		time = os.time(),
-	}
-end
-
-local function notify(player, message)
-	message = "[rhotator] " .. message
-	local playername = player:get_player_name()
-	local hud = huds[playername]
-	if not hud then
-		hud_create(player, message)
-	else
-		player:hud_change(hud.id, "text", message)
-		hud.time = os.time()
-	end
-	minetest.after(hud_timeout_seconds, function()
-		hud_remove(player)
-	end)
-end
-
 -- ============================================================
 -- rhotator main
 
@@ -402,10 +354,9 @@ local function interact(player, pointed_thing, click)
 	if pointed_thing.type ~= "node" then
 		return
 	end
-	
 	local pos = pointed_thing.under
 	if minetest.is_protected(pos, player:get_player_name()) then
-		notify(player, "You're not authorized to alter nodes in this area")
+		notify.error(player, "You're not authorized to alter nodes in this area")
 		minetest.record_protection_violation(pos, player:get_player_name())
 		return
 	end
@@ -414,12 +365,12 @@ local function interact(player, pointed_thing, click)
 	local nodedef = minetest.registered_nodes[node.name]
 
 	if not nodedef then
-		notify(player, "Unsupported node type: " .. node.name)
+		notify.error(player, "Unsupported node type: " .. node.name)
 		return
 	end
 
 	local handler = handlers[nodedef.paramtype2]
-		
+	
 	-- Node provides a handler, so let the handler decide instead if the node can be rotated
 	if nodedef.on_rotate then
 		-- Copy pos and node because callback can modify it
@@ -430,17 +381,17 @@ local function interact(player, pointed_thing, click)
 			notify(player, "Rotation reportedly performed by on_rotate()")
 			return
 		else
-			notify(player, "Rotation disallowed by on_rotate() return value")
+			notify.warning(player, "Rotation disallowed by on_rotate() return value")
 			return
 		end
 	elseif nodedef.on_rotate == false then
-		notify(player, "Rotation prevented by on_rotate == false")
+		notify.warning(player, "Rotation prevented by on_rotate == false")
 		return
 	elseif nodedef.can_dig and not nodedef.can_dig(pos, player) then
-		notify(player, "Rotation prevented by can_dig() checks")
+		notify.warning(player, "Rotation prevented by can_dig() checks")
 		return
 	elseif not handler then
-		notify(player, "Cannot rotate node with paramtype2 == " .. nodedef.paramtype2)
+		notify.warning(player, "Cannot rotate node with paramtype2 == " .. nodedef.paramtype2)
 		return
 	end
 	
diff --git a/notify.lua b/notify.lua
new file mode 100644
index 0000000..c7e1282
--- /dev/null
+++ b/notify.lua
@@ -0,0 +1,72 @@
+local mod_name = minetest.get_current_modname()
+local huds = {}
+local hud_timeout_seconds = 3
+
+-- defaults
+local position = { x = 0.1, y = 0.9}
+local alignment = { x = 1, y = -1}
+local normal_color = 0xFFFFFF
+local warning_color = 0xFFFF00
+local error_color = 0xDD0000
+local direction = 0
+
+local notify = {}
+notify.__index = notify
+setmetatable(notify, notify)
+
+local function hud_remove(player)
+	local playername = player:get_player_name()
+	local hud = huds[playername]
+	if not hud then return end
+	if os.time() < hud_timeout_seconds + hud.time then
+		return
+	end
+	player:hud_remove(hud.id)
+	huds[playername] = nil
+end
+
+local function hud_create(player, message, params)
+	local playername = player:get_player_name()
+	local def = type(params) == "table" and params or {}
+	def.position = def.position or position
+	def.alignment = def.alignment or alignment
+	def.number = def.number or def.color or normal_color
+	def.color = nil
+	def.position = def.position or position
+	def.direction = def.direction or direction
+	def.text = message or def.text
+	def.hud_elem_type = def.hud_elem_type or "text"
+	def.name = mod_name .. "_feedback"
+	local id = player:hud_add(def)
+	huds[playername] = {
+		id = id,
+		time = os.time(),
+	}
+end
+
+notify.warn = function(player, message)
+	notify(player, message, {color = warning_color })
+end
+
+notify.warning = notify.warn
+
+notify.err = function(player, message)
+	notify(player, message, {color = error_color })
+end
+
+notify.error = notify.err
+
+notify.__call = function(self, player, message, params)
+	message = "[" .. mod_name .. "] " .. message
+	local playername = player:get_player_name()
+	local hud = huds[playername]
+	if hud then
+		player:hud_remove(hud.id)
+	end
+	hud_create(player, message, params)
+	minetest.after(hud_timeout_seconds, function()
+		hud_remove(player)
+	end)
+end
+
+return notify
-- 
GitLab