From 24a9ce31acf489b56682efdefed15c10dd4c308a Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 24 Jan 2023 22:40:41 +0100 Subject: lua-bindings: assigned reserved attr depending on type of object --- lua-bindings/diaphragm.lua | 93 ++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/lua-bindings/diaphragm.lua b/lua-bindings/diaphragm.lua index 57f065f..8632e51 100644 --- a/lua-bindings/diaphragm.lua +++ b/lua-bindings/diaphragm.lua @@ -72,39 +72,6 @@ function M.util.tbl_contains(t, value) return false end --- TODO: depending on objects -function M.util.is_reserved(s) - return M.util.tbl_contains({ - "top", - "left", - "width", - "height", - "right", - "bottom", - "vert_center", - "horiz_center", - - "top_left", - "top_right", - "bottom_left", - "bottom_right", - "middle_left", - "middle_right", - "top_middle", - "bottom_middle", - "center", - - "stroke_color", - "stroke_width", - "fill_color", - - -- "x", - -- "y", - -- - "size", - }, s) -end - function M.util.tbl_filter_by_key(func, t) local result = {} for k, v in pairs(t) do @@ -121,9 +88,39 @@ function M.util.tbl_filter_reserved(t) end, t) end -function M.util.tbl_assign_reserved(t, rhs) +local bounds_reserved = { + "top", + "bottom", + "left", + "right", + "width", + "height", + "vert_center", + "horiz_center", + + "top_left", + "top_right", + "bottom_left", + "bottom_right", + "middle_left", + "middle_right", + "top_middle", + "bottom_middle", + "center", +} + +local shape_reserved = { + "stroke_color", + "stroke_width", + "fill_color", +} + +local rectangle_reserved = M.util.tbl_extend(bounds_reserved, shape_reserved) +local text_reserved = M.util.tbl_extend(bounds_reserved, { "size" }) + +function M.util.tbl_assign_reserved(t, rhs, whitelist) for k, v in pairs(t) do - if M.util.is_reserved(k) then + if M.util.tbl_contains(whitelist, k) then rhs[k] = v end end @@ -159,7 +156,7 @@ M.text.font = M.core.font function M.text.new(params) params = params or {} local result = M.core.text(params) - M.util.tbl_assign_reserved(params, result) + M.util.tbl_assign_reserved(params, result, text_reserved) return result end @@ -168,7 +165,7 @@ M.rectangle = {} function M.rectangle.new(params) params = params or {} local result = M.core.rectangle() - M.util.tbl_assign_reserved(params, result) + M.util.tbl_assign_reserved(params, result, rectangle_reserved) return result end @@ -183,23 +180,37 @@ M.straight_path = {} function M.straight_path.new(params) params = params or {} local result = M.core.straight_path(params) - M.util.tbl_assign_reserved(params, result) + M.util.tbl_assign_reserved(params, result, rectangle_reserved) return result end function M.shape(params) params = params or {} - local result = M.util.tbl_filter_reserved(params) + + local function is_reserved(k) + return M.util.tbl_contains(bounds_reserved, k) + end + + local result = M.util.tbl_filter_by_key(function(k) + return not is_reserved(k) + end, params) local shape = M.core.complex_shape() setmetatable(result, { __index = function(_, k) - if M.util.is_reserved(k) then + if is_reserved(k) then return shape[k] end end, + __newindex = function(self, k, v) + if is_reserved(k) then + shape[k] = v + else + rawset(self, k, v) + end + end, }) - M.util.tbl_assign_reserved(params, shape) + M.util.tbl_assign_reserved(params, shape, bounds_reserved) return result end -- cgit v1.2.3