implement icon colorization for icon buttons

This commit is contained in:
Dirk Sohler 2021-05-08 21:32:02 +02:00
parent c2d1d1019e
commit 1444a4d8f5
No known key found for this signature in database
GPG Key ID: B9751241BD7D4E1A
2 changed files with 24 additions and 4 deletions

@ -18,6 +18,7 @@ local esc = minetest.formspec_escape
-- label = 'My Cool Button label' or '',
-- width = 5, or 3,
-- image_size = 1, or 0.5,
-- colorize = color_table or { use = false },
-- container = { left = 1, top = 1} or pos_container
-- exit_button = true or false
-- }
@ -31,12 +32,30 @@ local esc = minetest.formspec_escape
-- top = 0
-- }
--
-- Via the `colorize` sub-table it is possible to add colorization to the icon
-- shown oin the button. The table contains two entries to define the color and
-- the ratio for applying the color.
--
-- color_table = {
-- color = '#729fcf',
-- ratio = 200
-- }
--
-- If one of the values is omitted the value shown in the example is used.
--
-- @param id The ID for the button
-- @param definition The definition table as described
-- @return string The created button as formspec code
mtimer.get_icon_button = function (id, definition)
local def = definition or {}
-- Define colorization of button texture
local t_table = type(def.colorize) == 'table'
local t_values = t_table and def.colorize or { use = false }
local t_color = t_values.color or '#729fcf'
local t_ratio = t_values.ratio or 200
local t_colorize = '^[colorize:'..t_color..':'..t_ratio
-- Set button defaults
local b_width = def.width or 3
local i_size = def.image_size or 0.5
@ -53,7 +72,7 @@ mtimer.get_icon_button = function (id, definition)
return (table.concat({
'container[+containerLeft,+containerTop]',
' container[+buttonPadding,+buttonPadding]',
' image[0,0;+imageSize,+imageSize;+icon]',
' image[0,0;+imageSize,+imageSize;+icon+colorize]',
' label[+labelLeftPos,+labelTopPos;+label]',
' container_end[]',
' +buttonType[0,0;+buttonWidth,+buttonHeight;+id;]',
@ -67,6 +86,7 @@ mtimer.get_icon_button = function (id, definition)
['+buttonPadding'] = b_padding,
['+imageSize'] = i_size,
['+icon'] = 'mtimer_'..id:gsub(':', '_')..'.png',
['+colorize'] = t_values.use == false and '' or t_colorize,
['+labelLeftPos'] = l_left_pos,
['+labelTopPos'] = l_top_pos,
['+id'] = id,

@ -92,9 +92,9 @@ mtimer.dialog.custom_timer = function (player_name)
' box[0,-0.25;+contentWidth,0.04;#ffffff]',
' label[0,0.375;'..esc(S('The timer is currently @1', timer_status))..']',
' container[+contentWidth,0]',
mtimer.get_icon_button('ct_start', { width = 2.25, label = S('Start'), container = { left = -7.25 } }),
mtimer.get_icon_button('ct_stop', { width = 2.25, label = S('Stop'), container = { left = -4.75 } }),
mtimer.get_icon_button('ct_restart', { width = 2.25, label = S('Restart'), container = { left = -2.25 } }),
mtimer.get_icon_button('ct_start', { width = 2.25, label = S('Start'), colorize = { color = '#4e9a06' }, container = { left = -7.25 } }),
mtimer.get_icon_button('ct_stop', { width = 2.25, label = S('Stop'), colorize = { color = '#a40000', ratio = 128 }, container = { left = -4.75 } }),
mtimer.get_icon_button('ct_restart', { width = 2.25, label = S('Restart'), colorize = { color = '#729fcf' }, container = { left = -2.25 } }),
' container_end[]',
'container_end[]',
}