make title dynamic and adapt to new style

This commit is contained in:
Dirk Sohler
2021-04-03 15:06:17 +02:00
parent 18efcb0307
commit dc0b0dc03d
2 changed files with 108 additions and 77 deletions

View File

@ -1,10 +1,3 @@
-- # vim: nowrap
--
-- Set Vim to no-wrapping mode because of some lines not fitting within the 80
-- characters width limit due to overall readability of the code.
-- Localise needed functions
local m = mtimer local m = mtimer
local S = m.translator local S = m.translator
local esc = minetest.formspec_escape local esc = minetest.formspec_escape
@ -24,7 +17,7 @@ local esc = minetest.formspec_escape
-- definition = { -- definition = {
-- label = 'My Cool Button label' or '', -- label = 'My Cool Button label' or '',
-- width = 5, or 3, -- width = 5, or 3,
-- image_size = 1, or 0,5, -- image_size = 1, or 0.5,
-- container = { left = 1, top = 1} or pos_container -- container = { left = 1, top = 1} or pos_container
-- exit_button = true or false -- exit_button = true or false
-- } -- }
@ -34,7 +27,7 @@ local esc = minetest.formspec_escape
-- top left corner of the formspec if not contained in another container. -- top left corner of the formspec if not contained in another container.
-- --
-- pos_container = { -- pos_container = {
-- left = 0 -- left = 0,
-- top = 0 -- top = 0
-- } -- }
-- --
@ -68,7 +61,7 @@ mtimer.get_icon_button = function (id, definition)
}, ' '):gsub('%+%w+', { }, ' '):gsub('%+%w+', {
['+containerLeft'] = c_left, ['+containerLeft'] = c_left,
['+containerTop'] = c_top, ['+containerTop'] = c_top,
['+buttonType'] = def.exit_button == true and 'button_exit' or 'button', ['+buttonType'] = def.exit_button==true and 'button_exit' or 'button',
['+buttonWidth'] = b_width, ['+buttonWidth'] = b_width,
['+buttonHeight'] = b_height, ['+buttonHeight'] = b_height,
['+buttonPadding'] = b_padding, ['+buttonPadding'] = b_padding,
@ -91,14 +84,16 @@ end
-- the `def` table. The following table is an example. -- the `def` table. The following table is an example.
-- --
-- { -- {
-- title = 'Nice Title' -- Automatically prefixed for orientation -- title = 'Nice Title' -- Automatically prefixed for orientation
-- prefix = '[Blarb] ' -- Optional title prefix -- prefix = '[Blarb] ' -- Optional title prefix
-- width = 8, -- Optional width of the content container -- width = 8, -- Optional width of the content container
-- height = 3, -- Optional height of the content container -- height = 3, -- Optional height of the content container
-- show_to = 'Playername' -- Name of the player to show the formspec to -- show_to = 'Playername', -- Name of the player to show the formspec to
-- add_buttons = false -- Optionally hide buttons -- hide_buttons = true, -- Optionally hide buttons
-- content_offset = 0 -- Optionally Offset content height position -- hide_title = true, -- Optionally hide title
-- formspec = {} -- Table with formspec definition -- icon_size = 0.5, -- Optionally set title icon size
-- content_offset = 0, -- Optionally Offset content height position
-- formspec = {} -- Table with formspec definition
-- } -- }
-- --
-- When set the title is prefixed with the prefix value. If omitted “[mTimer] ” -- When set the title is prefixed with the prefix value. If omitted “[mTimer] ”
@ -106,15 +101,15 @@ end
-- strings can be used here. The Title and prefix are “formspec-safe” so -- strings can be used here. The Title and prefix are “formspec-safe” so
-- strings that are formspec elements can be used to show them literal. -- strings that are formspec elements can be used to show them literal.
-- --
-- The default buttons can be hidden by adding `add_buttons = false` to the -- The default buttons can be hidden by adding `hide_buttons = true` to the
-- definition table. If omitted the buttons are shown. When not shown the -- definition table. If omitted the buttons are shown. When not shown the
-- formspec size will be reduced by the amout of units the buttons would -- formspec size will be reduced by the amout of units the buttons would
-- have taken place. -- have taken place. Same for the title with `hide_title = true`.
-- --
-- Some formspec elements do not properly start at 0,0 even if set so. The -- Some formspec elements do not properly start at 0,0 even if set so. The
-- `content_offset` attribute offsets the content vertically by the given -- `content_offset` attribute offsets the content vertically by the given
-- amount of units. Formspec height and button positions are fixed according -- amount of units. Formspec height and button positions are adapted to the
-- to the given value. -- given value.
-- --
-- The table entries for `formspec` are the usual formspec elements that -- The table entries for `formspec` are the usual formspec elements that
-- define what a formspec looks like. You can write all definition in one entry -- define what a formspec looks like. You can write all definition in one entry
@ -124,10 +119,9 @@ end
-- you can easily start at 0,0 for your definition. The function automatically -- you can easily start at 0,0 for your definition. The function automatically
-- places everything in relation to the formspec frame and default buttons. -- places everything in relation to the formspec frame and default buttons.
-- --
-- The minimum formspec width and height are 9 units in width and 4 units in -- The minimum formspec width and height are 10 units in width and 5 units in
-- height. So `width` and `height` can be omitted when all of your content fits -- height. So `width` and `height` can be omitted when all of your content fits
-- into the default size. The resulting formspec is a minimum of 9 units wide -- into the default size.
-- and 5.85 units high (1.85 units are added for the title and the buttons).
-- --
-- All formspec table entries can contain the following variables. Variables -- All formspec table entries can contain the following variables. Variables
-- start with a plus sign (+) and are read until any character that is not -- start with a plus sign (+) and are read until any character that is not
@ -135,59 +129,96 @@ end
-- --
-- Variable Name Value Type -- Variable Name Value Type
-- -------------------------------------------------------------------------- -- --------------------------------------------------------------------------
-- +title formspecs title -- +width Width of the formspec
-- +width width of the formspec -- +height Height of the formspec
-- +height height of the formspec -- +iconSize Size of the title icon
-- +contentWidth optimal width for the content -- +labelPositionLeft Position of the title label from left side
-- +contentPosition content container vertical position (+offset) -- +labelPositionTop Position of the title label from top
-- +buttons default buttons vertical position -- +linePosition Position of the title separator line
-- +titleText The text of the title label (the dialog title)
-- +titleIcon The icon that is used in the dialog title
-- +contentPosition Position of the actual content of the dialog
-- +buttonsPosition The position where the buttons are
-- --
-- @param id The ID of the formspec -- @param id The ID of the formspec
-- @param def The definition table as described -- @param def The definition table as described
-- @return string the constructed “frame” -- @return string the constructed “frame”
mtimer.show_formspec = function (id, def) mtimer.show_formspec = function (id, def)
local add_buttons = def.add_buttons == true or def.add_buttons == nil local title_text = def.title or ''
local title_prefix = def.prefix or '[mTimer] '
local content_offset = def.content_offset or 0 local content_offset = def.content_offset or 0
local width = (def.width or 0) <= 10 and 10 or def.width local width = (def.width or 0) <= 10 and 10 or def.width
local height = (def.height or 0) <= 4 and 5.85 or def.height + 1.85 local height = ((def.height or 0) <= 4 and 5 or def.height)+content_offset
local prefix = def.prefix or '[mTimer] ' local icon_size = def.icon_size or 0.5
local line_position = 0
local buttons = ''
local title = ''
-- Calculate height -- Set up title
if not add_buttons then height = height - 1.85 end if def.hide_title ~= true then
height = height + content_offset line_position = icon_size + 0.25
content_offset = content_offset + line_position + (icon_size / 2) + 0.1
height = height + content_offset + (icon_size / 2)
title = table.concat({
'container[0.25,0.25]',
' image[0,0;+iconSize,+iconSize;+titleIcon]',
' label[+labelPositionLeft,+labelPositionTop;+titleText]',
' box[0,+linePosition;+contentWidth,0.04;#ffffff]',
'container_end[]'
}, ' ')
end
-- Set up buttons -- Set up buttons
local buttons = not add_buttons and '' or table.concat({ if def.hide_buttons ~= true then
'container[0.25,+buttons]', height = height + 1
' box[0,0;+contentWidth,0.04;#ffffff]', buttons = table.concat({
mtimer.get_icon_button('main_menu', { label = S('Main Menu'), width = 2.5, container = { top = 0.25 } }), 'container[0.25,+buttonsPosition]',
' container[+contentWidth,0.25]', 'box[0,0;+contentWidth,0.04;#ffffff]',
mtimer.get_icon_button('exit', { label = S('Exit'), exit_button = true, width = 2.5, container = { left = -2.5 } }), mtimer.get_icon_button('main_menu', {
mtimer.get_icon_button('default', { label = S('Default'), width = 2.5, container = { left = -5.25 } }), label = S('Main Menu'),
' container_end[]', width = 2.5, container = { top = 0.25 }
'container_end[]' }),
}, ' ') 'container[+contentWidth,0.25]',
mtimer.get_icon_button('exit', {
label = S('Exit'),
exit_button = true,
width = 2.5,
container = { left = -2.5 }
}),
mtimer.get_icon_button('default', {
label = S('Default'),
width = 2.5,
container = { left = -5.25 }
}),
'container_end[]', -- right side buttons
'container_end[]' -- buttons
}, ' ')
end
-- Build formspec -- Build formspec
local formspec = table.concat({ local formspec = table.concat({
'formspec_version[2]', 'formspec_version[2]',
'size[+width,+height]', 'size[+width,+height]',
'image[0.25,0.2;0.3,0.3;+icon]', title,
'label[0.65,0.35;+title]',
'box[0.25,0.6;+contentWidth,0.04;#ffffff]',
'container[0.25,+contentPosition]', 'container[0.25,+contentPosition]',
table.concat(def.formspec, ' '), table.concat(def.formspec, ' '),
'container_end[]', 'container_end[]',
buttons buttons
}, ' '):gsub('%+%a+', { }, ' '):gsub('%+%a+', {
['+width'] = width+0.25, -- Formspec frame
['+height'] = height, ['+width'] = width + 0.5,
['+contentWidth'] = width-0.25, ['+height'] = height + 0.5,
['+title'] = minetest.formspec_escape(prefix..def.title), ['+contentWidth'] = width,
['+icon'] = 'mtimer_'..id:gsub('mtimer:', '')..'.png', ['+contentPosition'] = content_offset + 0.25,
['+contentPosition'] = 0.9 + content_offset, ['+iconSize'] = icon_size,
['+buttons'] = height-1.25, -- Title-related settings
['+labelPositionLeft'] = icon_size + (icon_size / 4),
['+labelPositionTop'] = (icon_size / 2),
['+linePosition'] = line_position,
['+titleText'] = minetest.formspec_escape(title_prefix..title_text),
['+titleIcon'] = 'mtimer_'..id:gsub('mtimer:', '')..'.png',
-- Buttons-related settings
['+buttonsPosition'] = height - 0.75,
}) })
-- Show formspec to the plauyer -- Show formspec to the plauyer

View File

@ -21,7 +21,7 @@ local esc = minetest.formspec_escape
-- @param id The buttons ID -- @param id The buttons ID
-- @param label The buttons label -- @param label The buttons label
-- @return string The parsed main menu button -- @return string The parsed main menu button
local button = function (column, row, id, label) local menu_button = function (column, row, id, label)
local b_width = 5 local b_width = 5
local i_size = 0.5 local i_size = 0.5
@ -53,29 +53,29 @@ end
-- @see https://dev.minetest.net/formspec -- @see https://dev.minetest.net/formspec
mtimer.dialog.main_menu = function (player_name) mtimer.dialog.main_menu = function (player_name)
mtimer.show_formspec('mtimer:main_menu', { mtimer.show_formspec('mtimer:main_menu', {
title = S('mTimer'), width = 15.75,
width = 16, height = 5.75,
height = 6.9, hide_buttons = true,
prefix = '', hide_title = true,
add_buttons = false,
show_to = player_name, show_to = player_name,
formspec = { formspec = {
-- Visuals -- Visuals
button(1, 1, 'set_visibility', S('Visibility')), menu_button(1, 1, 'set_visibility', S('Visibility')),
button(1, 2, 'set_position', S('Position')), menu_button(1, 2, 'set_position', S('Position')),
button(1, 3, 'set_color', S('Color')), menu_button(1, 3, 'set_color', S('Color')),
button(1, 4, 'hud_element_size', S('HUD Element Size')), menu_button(1, 4, 'hud_element_size', S('HUD Element Size')),
button(1, 5, 'hud_element_offset', S('HUD Element Offset')), menu_button(1, 5, 'hud_element_offset', S('HUD Element Offset')),
-- Time Representation -- Time Representation
button(2, 1, 'ingame_time_format', S('Ingame Time Format')), menu_button(2, 1, 'ingame_time_format', S('Ingame Time Format')),
button(2, 2, 'real_world_time_format', S('Real-World Time Format')), menu_button(2, 2, 'real_world_time_format', S('Real-World Time Format')),
button(2, 3, 'session_start_time_format', S('Session Start Time Format')), menu_button(2, 3, 'session_start_time_format', S('Session Start Time Format')),
button(2, 4, 'session_duration_format', S('Session Duration Format')), menu_button(2, 4, 'session_duration_format', S('Session Duration Format')),
button(2, 5, 'host_time_format', S('Host Time Format')), menu_button(2, 5, 'host_time_format', S('Host Time Format')),
-- Timer configuration -- Timer configuration
button(3, 1, 'timer_format', S('Timer Format')), menu_button(3, 1, 'timer_format', S('Timer Format')),
button(3, 2, 'timezone_offset', S('Timezone Offset')), menu_button(3, 2, 'timezone_offset', S('Timezone Offset')),
button(3, 3, 'custom_timer', S('Custom Timer')), menu_button(3, 3, 'custom_timer', S('Custom Timer')),
-- Custom buttons
'container[0,4.75]', 'container[0,4.75]',
' box[0,0;+contentWidth,0.04;#ffffff]', ' box[0,0;+contentWidth,0.04;#ffffff]',
' container[+contentWidth,0]', ' container[+contentWidth,0]',