From 57a9284bcaac3423bc7d64edcf227c5148b528ae Mon Sep 17 00:00:00 2001 From: Dirk Sohler Date: Sat, 15 May 2021 01:03:59 +0200 Subject: [PATCH] move helper function out of fs creation function --- system/formspecs/set_position.lua | 48 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/system/formspecs/set_position.lua b/system/formspecs/set_position.lua index 63062fc..59ba0e9 100644 --- a/system/formspecs/set_position.lua +++ b/system/formspecs/set_position.lua @@ -2,6 +2,13 @@ local m = mtimer local S = m.translator +-- Image width and height for setting the screenshot in position and do the +-- button calculations +local i_width = 10 +local i_height = 6 + + +-- Position names for the current position information string local p_names = { ['t'] = S('top'), ['m'] = S('middle'), @@ -12,31 +19,40 @@ local p_names = { } +-- Return parsed position button +-- +-- This function shows a button on the given position that sets the position +-- of the GUI element accordingly. The `position` is one of the following. +-- +-- tl, tc, tr = top left, center, right +-- ml, mc, mr = middle left, center, right +-- bl, bc, br = bottom left, center right +-- +-- @param top The position of the button from the top +-- @param left The position of the button from the left +-- @param position GUI element position to set with this button +local b_define = function(top, left, position) + return ('image_button[+l,+t;+w,+h;+i;pos_+p;d;;false]'):gsub('%+%w', { + ['+l'] = (left - 1) * (i_width / 3), + ['+t'] = (top - 1) * (i_height / 3), + ['+w'] = i_width / 3, + ['+h'] = i_height / 3, + ['+i'] = 'mtimer_transparent.png', + ['+p'] = position, + }) +end + + mtimer.dialog.set_position = function (player_name) local player = minetest.get_player_by_name(player_name) local howto = S('Click the position you want to place the timer at.') + local image = 'mtimer_positions_orientation.png' -- Get current position name local p_value = player:get_meta():get_string(m.meta.position.key) local p_name = p_names[p_value:sub(1,1)]..' '..p_names[p_value:sub(2,2)] local p_info = S('Current position: @1', p_name) - -- Set up image - local image = 'mtimer_positions_orientation.png' - local i_width = 10 - local i_height = 6 - - -- Return the parsed button - local b_define = function(t, l, p) - return ('image_button[+l,+t;+w,+h;+i;pos_+p;d;;false]'):gsub('%+%w', { - ['+l'] = (l - 1) * (i_width / 3), - ['+t'] = (t - 1) * (i_height / 3), - ['+w'] = i_width / 3, - ['+h'] = i_height / 3, - ['+i'] = 'mtimer_transparent.png', - ['+p'] = p, - }) - end mtimer.show_formspec('mtimer:set_position', { title = S('Position'),