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
No known key found for this signature in database
GPG Key ID: B9751241BD7D4E1A
2 changed files with 108 additions and 77 deletions

@ -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 S = m.translator
local esc = minetest.formspec_escape
@ -24,7 +17,7 @@ local esc = minetest.formspec_escape
-- definition = {
-- label = 'My Cool Button label' or '',
-- width = 5, or 3,
-- image_size = 1, or 0,5,
-- image_size = 1, or 0.5,
-- container = { left = 1, top = 1} or pos_container
-- 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.
--
-- pos_container = {
-- left = 0
-- left = 0,
-- top = 0
-- }
--
@ -68,7 +61,7 @@ mtimer.get_icon_button = function (id, definition)
}, ' '):gsub('%+%w+', {
['+containerLeft'] = c_left,
['+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,
['+buttonHeight'] = b_height,
['+buttonPadding'] = b_padding,
@ -91,14 +84,16 @@ end
-- the `def` table. The following table is an example.
--
-- {
-- title = 'Nice Title' -- Automatically prefixed for orientation
-- prefix = '[Blarb] ' -- Optional title prefix
-- width = 8, -- Optional width of the content container
-- height = 3, -- Optional height of the content container
-- show_to = 'Playername' -- Name of the player to show the formspec to
-- add_buttons = false -- Optionally hide buttons
-- content_offset = 0 -- Optionally Offset content height position
-- formspec = {} -- Table with formspec definition
-- title = 'Nice Title' -- Automatically prefixed for orientation
-- prefix = '[Blarb] ' -- Optional title prefix
-- width = 8, -- Optional width of the content container
-- height = 3, -- Optional height of the content container
-- show_to = 'Playername', -- Name of the player to show the formspec to
-- hide_buttons = true, -- Optionally hide buttons
-- hide_title = true, -- Optionally hide title
-- 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] ”
@ -106,15 +101,15 @@ end
-- 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.
--
-- 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
-- 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
-- `content_offset` attribute offsets the content vertically by the given
-- amount of units. Formspec height and button positions are fixed according
-- to the given value.
-- amount of units. Formspec height and button positions are adapted to the
-- given value.
--
-- 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
@ -124,10 +119,9 @@ end
-- you can easily start at 0,0 for your definition. The function automatically
-- 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
-- into the default size. The resulting formspec is a minimum of 9 units wide
-- and 5.85 units high (1.85 units are added for the title and the buttons).
-- into the default size.
--
-- All formspec table entries can contain the following variables. Variables
-- start with a plus sign (+) and are read until any character that is not
@ -135,59 +129,96 @@ end
--
-- Variable Name Value Type
-- --------------------------------------------------------------------------
-- +title formspecs title
-- +width width of the formspec
-- +height height of the formspec
-- +contentWidth optimal width for the content
-- +contentPosition content container vertical position (+offset)
-- +buttons default buttons vertical position
-- +width Width of the formspec
-- +height Height of the formspec
-- +iconSize Size of the title icon
-- +labelPositionLeft Position of the title label from left side
-- +labelPositionTop Position of the title label from top
-- +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 def The definition table as described
-- @return string the constructed “frame”
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 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 prefix = def.prefix or '[mTimer] '
local height = ((def.height or 0) <= 4 and 5 or def.height)+content_offset
local icon_size = def.icon_size or 0.5
local line_position = 0
local buttons = ''
local title = ''
-- Calculate height
if not add_buttons then height = height - 1.85 end
height = height + content_offset
-- Set up title
if def.hide_title ~= true then
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
local buttons = not add_buttons and '' or table.concat({
'container[0.25,+buttons]',
' box[0,0;+contentWidth,0.04;#ffffff]',
mtimer.get_icon_button('main_menu', { label = S('Main Menu'), width = 2.5, container = { top = 0.25 } }),
' 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[]',
'container_end[]'
}, ' ')
if def.hide_buttons ~= true then
height = height + 1
buttons = table.concat({
'container[0.25,+buttonsPosition]',
'box[0,0;+contentWidth,0.04;#ffffff]',
mtimer.get_icon_button('main_menu', {
label = S('Main Menu'),
width = 2.5, container = { top = 0.25 }
}),
'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
local formspec = table.concat({
'formspec_version[2]',
'size[+width,+height]',
'image[0.25,0.2;0.3,0.3;+icon]',
'label[0.65,0.35;+title]',
'box[0.25,0.6;+contentWidth,0.04;#ffffff]',
title,
'container[0.25,+contentPosition]',
table.concat(def.formspec, ' '),
'container_end[]',
buttons
}, ' '):gsub('%+%a+', {
['+width'] = width+0.25,
['+height'] = height,
['+contentWidth'] = width-0.25,
['+title'] = minetest.formspec_escape(prefix..def.title),
['+icon'] = 'mtimer_'..id:gsub('mtimer:', '')..'.png',
['+contentPosition'] = 0.9 + content_offset,
['+buttons'] = height-1.25,
-- Formspec frame
['+width'] = width + 0.5,
['+height'] = height + 0.5,
['+contentWidth'] = width,
['+contentPosition'] = content_offset + 0.25,
['+iconSize'] = icon_size,
-- 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

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