adapt custom timer to new style

Also implement control buttons

closes https://gitlab.com/4w/mtimer/-/issues/12
This commit is contained in:
Dirk Sohler 2021-04-04 14:23:35 +02:00
parent 70760fc37f
commit f7d6025557
No known key found for this signature in database
GPG Key ID: B9751241BD7D4E1A
8 changed files with 117 additions and 64 deletions

@ -68,7 +68,7 @@ Session Duration=Sitzungsdauer
Session Start Time=Sitzungsstart-Zeit Session Start Time=Sitzungsstart-Zeit
Host Time=Hostzeit Host Time=Hostzeit
# Custom Timer Things # Custom Timer Setup
Running=Timer läuft Running=Timer läuft
Stopped=Angehalten Stopped=Angehalten
Finished=Abgeschlossen Finished=Abgeschlossen
@ -83,12 +83,22 @@ Configure the custom timer=Individuellen Timer konfigurieren
Start the custom timer=Individuellen Timer starten Start the custom timer=Individuellen Timer starten
Stop stop custom timer=Individuellen Timer anhalten Stop stop custom timer=Individuellen Timer anhalten
Restart the custom timer=Individuellen Timer neu starten Restart the custom timer=Individuellen Timer neu starten
# Custonm timer status messages
The custom timer is already running=Der individuelle Timer läuft bereits The custom timer is already running=Der individuelle Timer läuft bereits
The custom timer is not running=Der individuelle Timer läuft nicht The custom timer is not running=Der individuelle Timer läuft nicht
The custom timer was started=Der individuelle Timer wurde gestartet The custom timer was started=Der individuelle Timer wurde gestartet
The custom timer was stopped=Der individuelle Timer wurde angehalten The custom timer was stopped=Der individuelle Timer wurde angehalten
The custom timer was restarted=Der individuelle timer wurde neu gestartet The custom timer was restarted=Der individuelle timer wurde neu gestartet
# Custom Timer Controls
The timer is currently @1=Der Timer ist aktuell @1
running=aktiv
stopped=angehalten
Start=Starten
Stop=Anhalten
Restart=Neustart
# Default Timer Format # Default Timer Format
Current Date: @1=Aktuelles Datum: @1 Current Date: @1=Aktuelles Datum: @1
Ingame Time: @1=Spielzeit: @1 Ingame Time: @1=Spielzeit: @1

@ -1,8 +1,6 @@
local m = mtimer local m = mtimer
local S = m.translator local S = m.translator
local d = m.dialog local d = m.dialog
local cs = minetest.chat_send_player
local ds = minetest.deserialize
-- Colorize a command sequence -- Colorize a command sequence
@ -17,46 +15,6 @@ local command = function (command)
end end
local custom_timer_handling = function (name, action)
local player = minetest.get_player_by_name(name)
local player_meta = player:get_meta()
local current_timestamp = os.time(os.date('!*t'))
local ctv_key = m.meta.custom_timer_settings.key
local ctv = ds(player_meta:get_string(ctv_key))
if action == 'start' then
if ctv.running ~= true then
ctv.running = true
ctv.start_timestamp = current_timestamp
cs(name, S('The custom timer was started'))
else
cs(name, S('The custom timer is already running'))
end
end
if action == 'stop' then
if ctv.running ~= false then
ctv.running = false
ctv.start_timestamp = 0
cs(name, S('The custom timer was stopped'))
else
cs(name, S('The custom timer is not running'))
end
end
if action == 'restart' then
if ctv.running == true then
ctv.start_timestamp = current_timestamp
cs(name, S('The custom timer was restarted'))
else
cs(name, S('The custom timer is not running'))
end
end
player_meta:set_string(ctv_key, minetest.serialize(ctv))
end
-- Chat command -- Chat command
-- --
-- The `/mtimer` chat command opens the main menu and allows to directly open -- The `/mtimer` chat command opens the main menu and allows to directly open
@ -106,9 +64,9 @@ minetest.register_chatcommand('mtimer', {
if action == 'tf' then d.timer_format(name) end if action == 'tf' then d.timer_format(name) end
if action == 'ct' then d.custom_timer(name) end if action == 'ct' then d.custom_timer(name) end
if action == 'ctstart' then custom_timer_handling(name,'start') end if action == 'ctstart' then mtimer.update_custom_timer(name, { action = 'start' }) end
if action == 'ctstop' then custom_timer_handling(name,'stop') end if action == 'ctstop' then mtimer.update_custom_timer(name, { action = 'stop' }) end
if action == 'ctrestart' then custom_timer_handling(name,'restart') end if action == 'ctrestart' then mtimer.update_custom_timer(name, { action = 'restart' }) end
if action == 'help' then if action == 'help' then
local message = { local message = {
@ -131,7 +89,7 @@ minetest.register_chatcommand('mtimer', {
command('ctstop ')..S('Stop stop custom timer'), command('ctstop ')..S('Stop stop custom timer'),
command('ctrestart')..S('Restart the custom timer') command('ctrestart')..S('Restart the custom timer')
} }
cs(name, table.concat(message, '\n')) minetest.chat_send_player(name, table.concat(message, '\n'))
end end
end end
}) })

@ -19,6 +19,7 @@ local esc = minetest.formspec_escape
mtimer.dialog.custom_timer = function (player_name) mtimer.dialog.custom_timer = function (player_name)
local player_meta = minetest.get_player_by_name(player_name):get_meta() local player_meta = minetest.get_player_by_name(player_name):get_meta()
local ctv = minetest.deserialize(player_meta:get_string(m.meta.custom_timer_settings.key)) local ctv = minetest.deserialize(player_meta:get_string(m.meta.custom_timer_settings.key))
local timer_status = (ctv.running == true) and S('running') or S('stopped')
local days = ctv.values.days or 0 local days = ctv.values.days or 0
local hours = ctv.values.hours or 0 local hours = ctv.values.hours or 0
@ -36,8 +37,8 @@ mtimer.dialog.custom_timer = function (player_name)
mtimer.show_formspec('mtimer:custom_timer', { mtimer.show_formspec('mtimer:custom_timer', {
title = S('Custom Timer'), title = S('Custom Timer'),
show_to = player_name, show_to = player_name,
height = 5.3, height = 6.25,
width = 9, width = 13,
formspec = { formspec = {
'field_close_on_enter[v_format_running;false]', 'field_close_on_enter[v_format_running;false]',
'field_close_on_enter[v_format_stopped;false]', 'field_close_on_enter[v_format_stopped;false]',
@ -47,20 +48,18 @@ mtimer.dialog.custom_timer = function (player_name)
'field_close_on_enter[v_minutes;false]', 'field_close_on_enter[v_minutes;false]',
'field_close_on_enter[v_seconds;false]', 'field_close_on_enter[v_seconds;false]',
'container[0,0]', 'container[0,0]',
' label[0,0.25;'..S('Running')..'] field[2.25,0;6.5,0.5;v_format_running;;'..esc(format_running)..']', ' label[0,0.25;'..S('Running')..'] field[2.5,0;10.5,0.5;v_format_running;;'..esc(format_running)..']',
' label[0,0.85;'..S('Stopped')..'] field[2.25,0.6;6.5,0.5;v_format_stopped;;'..esc(format_stopped)..']', ' label[0,0.85;'..S('Stopped')..'] field[2.5,0.6;10.5,0.5;v_format_stopped;;'..esc(format_stopped)..']',
' label[0,1.45;'..S('Finished')..'] field[2.25,1.2;6.5,0.5;v_format_finished;;'..esc(format_finished)..']', ' label[0,1.45;'..S('Finished')..'] field[2.5,1.2;10.5,0.5;v_format_finished;;'..esc(format_finished)..']',
' box[0,2;+contentWidth,0.04;#ffffff]', ' box[0,2;+contentWidth,0.04;#ffffff]',
'container_end[]', 'container_end[]',
'container[3.75,2.4]', 'container[3.75,2.4]',
' label[0,0;'..S('Information')..']', ' label[0,0;'..S('Information')..'] label[2.5,0;'..S('Variable')..'] label[5,0;'..S('Used Value')..']',
' label[1.75,0;'..S('Variable')..']', ' box[0,0.25;7,0.02;#ffffff]',
' label[3.25,0;'..S('Used Value')..']', ' label[0,0.5;'..S('Days')..'] label[2.5,0.5;{days}] label[5,0.5;'..days..']',
' box[0,0.25;5,0.02;#ffffff]', ' label[0,0.9;'..S('Hours')..'] label[2.5,0.9;{hours}] label[5,0.9;'..hours..']',
' label[0,0.5;'..S('Days')..'] label[1.75,0.5;{days}] label[3.25,0.5;'..days..']', ' label[0,1.3;'..S('Minutes')..'] label[2.5,1.3;{minutes}] label[5,1.3;'..minutes..']',
' label[0,0.9;'..S('Hours')..'] label[1.75,0.9;{hours}] label[3.25,0.9;'..hours..']', ' label[0,1.7;'..S('Seconds')..'] label[2.5,1.7;{seconds}] label[5,1.7;'..seconds..']',
' label[0,1.3;'..S('Minutes')..'] label[1.75,1.3;{minutes}] label[3.25,1.3;'..minutes..']',
' label[0,1.7;'..S('Seconds')..'] label[1.75,1.7;{seconds}] label[3.25,1.7;'..seconds..']',
'container_end[]', 'container_end[]',
'container[0,2.3]', 'container[0,2.3]',
' container[0,0]', ' container[0,0]',
@ -84,11 +83,20 @@ mtimer.dialog.custom_timer = function (player_name)
' button[0,0.75;0.75,0.25;c_seconds_m;-]', ' button[0,0.75;0.75,0.25;c_seconds_m;-]',
' container_end[]', ' container_end[]',
'container_end[]', 'container_end[]',
'container[0,3.6]', 'container[0,3.75]',
' checkbox[0,0;mode_countdown;'..S('Countdown')..';'..a_countdown..']', ' checkbox[0,0;mode_countdown;'..S('Countdown')..';'..a_countdown..']',
' checkbox[0,0.4;mode_timer;'..S('Timer Mode')..';'..a_timer..']', ' checkbox[0,0.4;mode_timer;'..S('Timer Mode')..';'..a_timer..']',
' checkbox[0,0.8;mode_continuous;'..S('Continuous Run')..';'..a_continuous..']', ' checkbox[0,0.8;mode_continuous;'..S('Continuous Run')..';'..a_continuous..']',
'container_end[]', 'container_end[]',
'container[0,5.55]',
' 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 } }),
' container_end[]',
'container_end[]',
} }
}) })
end end

@ -39,7 +39,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.set_visible then meta:set_string(attr.key, 'true') end if fields.set_visible then meta:set_string(attr.key, 'true') end
if fields.set_invisible then meta:set_string(attr.key, 'false') end if fields.set_invisible then meta:set_string(attr.key, 'false') end
if fields.default then meta:set_string(attr.key, attr.default) end if fields.default then meta:set_string(attr.key, attr.default) end
if not fields.quit then d.set_visibility(name) end
end end
@ -232,7 +231,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
seconds = seconds seconds = seconds
} }
-- Set default values if requested -- Set default values if requested and instantly return to prevent the
-- rest of the configuration to be executed. At this point only
-- resetting all values is desired.
if fields.default then if fields.default then
meta:set_string(attr.key, attr.default) meta:set_string(attr.key, attr.default)
d.custom_timer(name) d.custom_timer(name)
@ -243,8 +244,19 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- Set values if not quitting -- Set values if not quitting
if not fields.quit then if not fields.quit then
meta:set_string(attr.key, minetest.serialize(ctv)) meta:set_string(attr.key, minetest.serialize(ctv))
d.custom_timer(name)
end end
-- Control timer if one of the control buttons was pressed. This is run
-- after the values safing in order to use the new values instead of
-- the values that were stored before.
local ct_update = false
if fields.ct_start then ct_update = { action = 'start' } end
if fields.ct_stop then ct_update = { action = 'stop' } end
if fields.ct_restart then ct_update = { action = 'restart' } end
if ct_update~=false then mtimer.update_custom_timer(name,ct_update) end
-- Show the timer formspec if not quitting
if not fields.quit then d.custom_timer(name) end
end end

@ -1,5 +1,7 @@
local m = mtimer local m = mtimer
local deserialize = minetest.deserialize local deserialize = minetest.deserialize
local cs = minetest.chat_send_player
local S = m.translator
-- Calculate HUD positions and offsets -- Calculate HUD positions and offsets
@ -117,3 +119,66 @@ mtimer.update_timer = function (player_name)
player:hud_change(hud_id, 'size', {x=size, y=size}) player:hud_change(hud_id, 'size', {x=size, y=size})
player:hud_change(hud_id, 'offset', orientation.offset) player:hud_change(hud_id, 'offset', orientation.offset)
end end
-- Update the custom timer
--
-- This function handles updates for the custom timer for the player referenced
-- by the provided `name` parameter. This needs to be a player name string.
--
-- The update is performed based on the provided table.
--
-- update_parameters = {
-- action = 'the_action'
-- }
--
-- Currently the only actions are `start`, `stop`, and `restart`.
--
-- @param player_name The name of the player to update the custom timer for
-- @param update_parameters The update parameters table as described
mtimer.update_custom_timer = function (player_name, update_parameters)
local up = update_parameters or {}
local player = minetest.get_player_by_name(player_name)
local player_meta = player:get_meta()
local current_timestamp = os.time(os.date('!*t'))
local ctv_key = m.meta.custom_timer_settings.key
local ctv = minetest.deserialize(player_meta:get_string(ctv_key))
print(dump(player_name))
-- Start timer if not running
if up.action == 'start' then
if ctv.running ~= true then
ctv.running = true
ctv.start_timestamp = current_timestamp
cs(player_name, S('The custom timer was started'))
else
cs(player_name, S('The custom timer is already running'))
end
end
-- Stop timer if running
if up.action == 'stop' then
if ctv.running ~= false then
ctv.running = false
ctv.start_timestamp = 0
cs(player_name, S('The custom timer was stopped'))
else
cs(player_name, S('The custom timer is not running'))
end
end
-- Restart timer
if up.action == 'restart' then
if ctv.running == true then
ctv.start_timestamp = current_timestamp
cs(player_name, S('The custom timer was restarted'))
else
cs(player_name, S('The custom timer is not running'))
end
end
-- Write timer update to player meta data
player_meta:set_string(ctv_key, minetest.serialize(ctv))
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB