From 4c5de1fb42c3f23bcb176f9c3a72d66d78a26214 Mon Sep 17 00:00:00 2001 From: Dirk Sohler Date: Mon, 18 Feb 2019 22:02:59 +0100 Subject: [PATCH] add a helper function to build the formspec frame fixes https://gitlab.com/4w/mtimer/issues/4 --- system/formspec_creation.lua | 57 ++++++++++++++++++++---------------- system/on_receive_fields.lua | 2 +- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/system/formspec_creation.lua b/system/formspec_creation.lua index 2f31317..351aa67 100644 --- a/system/formspec_creation.lua +++ b/system/formspec_creation.lua @@ -2,6 +2,29 @@ local m = mtimer local S = m.translator +local build_frame = function (width, height, title) + local formspec_frame = [[ + size[+width,+height] label[0,-0.125;+title] + container[0,+position] + button_exit[+exitpos,0;+bwidth,1;exit;]]..S('Exit')..[[] + button[+mainpos,0;+bwidth,1;main_menu;]]..S('Main Menu')..[[] + button[+defaultpos,0;+bwidth,1;default;]]..S('Default')..[[] + container_end[] + ]] + + return formspec_frame:gsub('%+%a+', { + ['+width'] = width, + ['+height'] = height, + ['+title'] = title, + ['+position'] = height-0.6, + ['+bwidth'] = width/3, + ['+exitpos'] = 0, + ['+mainpos'] = width/3, + ['+defaultpos'] = (width/3)*2 + }) +end + + mtimer.show_formspec.main_menu = function (player_name) minetest.show_formspec(player_name, 'mtimer:main_menu', [[ size[4,8.25] @@ -20,24 +43,17 @@ end mtimer.show_formspec.set_visibility = function (player_name) - minetest.show_formspec(player_name, 'mtimer:set_visibility', [[ - size[6,2.25] - label[0,-0.125;]]..S('mTimer Visibility')..[[] - - button[0,0.5;2,1;visible;]]..S('Visible')..[[] - button[2,0.5;2,1;invisible;]]..S('Invisible')..[[] - button[4,0.5;2,1;default;]]..S('Default')..[[] - - button_exit[0,1.5;3,1;exit;]]..S('Exit')..[[] - button[3,1.5;3,1;back;]]..S('Main Menu')..[[] + minetest.show_formspec(player_name, 'mtimer:set_visibility', + build_frame(6, 2.25, S('mTimer Visibility'))..[[ + button[0,0.5;3,1;visible;]]..S('Visible')..[[] + button[3,0.5;3,1;invisible;]]..S('Invisible')..[[] ]]) end mtimer.show_formspec.set_position = function (player_name) - minetest.show_formspec(player_name, 'mtimer:set_position', [[ - size[8,6.125] - label[0,-0.125;]]..S('mTimer Position')..[[] + minetest.show_formspec(player_name, 'mtimer:set_position', + build_frame(8, 6.125, S('mTimer Position'))..[[ image_button[0,0.5;8,4.5 ;mtimer_positions_orientation.png;pos_xx;;;false] @@ -54,10 +70,6 @@ mtimer.show_formspec.set_position = function (player_name) image_button[5.34,3.5;2.67,1.5;mtimer_transparent.png;pos_br;;;false] label[0,5;]]..S('Click the position you want to place the timer at.')..[[] - - button_exit[0,5.5;2,1;exit;]]..S('Exit')..[[] - button[2,5.5;2,1;back;]]..S('Main Menu')..[[] - button[4,5.5;2,1;default;]]..S('Default')..[[] ]]) end @@ -74,18 +86,11 @@ mtimer.show_formspec.set_color = function (player_name) 'AA' }) - minetest.show_formspec(player_name, 'mtimer:set_color', [[ - size[6.85,2.7] - label[0,-0.125;]]..S('mTimer Color')..[[] + minetest.show_formspec(player_name, 'mtimer:set_color', + build_frame(6, 2.7, S('mTimer Color'))..[[ field_close_on_enter[color;false] - field[0.25,0.75;3,1;color;;]]..color..[[] box[3,0.55;0.65,0.65;]]..color..[[] - button[3.85,0.45;3,1;default;]]..S('Default')..[[] - label[-0.05,1.45;]]..S('Use `@1` format only!', hexcolor)..[[] - - button_exit[-0.05,2;3.45,1;exit;]]..S('Exit')..[[] - button[3.44,2;3.4,1;back;]]..S('Main Menu')..[[] ]]) end diff --git a/system/on_receive_fields.lua b/system/on_receive_fields.lua index a0b3962..74afd7b 100644 --- a/system/on_receive_fields.lua +++ b/system/on_receive_fields.lua @@ -54,7 +54,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- Back to menu from all formspecs - if fields.back then f.main_menu(name) end + if fields.main_menu then f.main_menu(name) end -- DEBUG: Print all player meta data