From 028cdbf99e2e5d043b0be425fa5e94f1dafc1e2f Mon Sep 17 00:00:00 2001 From: Dirk Sohler Date: Sat, 16 Feb 2019 16:45:32 +0100 Subject: [PATCH] establish formspec system --- init.lua | 18 +++++++++++++++ locale/mtimer.de.tr | 43 ++++++++++++++++++++++++++++++++++++ mod.conf | 2 +- system/chat_command.lua | 10 +++++++++ system/formspec_creation.lua | 30 +++++++++++++++++++++++++ system/on_joinplayer.lua | 9 ++++++++ system/on_receive_fields.lua | 30 +++++++++++++++++++++++++ system/timer_update.lua | 1 + 8 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 locale/mtimer.de.tr create mode 100644 system/chat_command.lua create mode 100644 system/formspec_creation.lua create mode 100644 system/on_joinplayer.lua create mode 100644 system/on_receive_fields.lua create mode 100644 system/timer_update.lua diff --git a/init.lua b/init.lua index e69de29..a18eb03 100644 --- a/init.lua +++ b/init.lua @@ -0,0 +1,18 @@ +local modpath = minetest.get_modpath('mtimer')..DIR_DELIM +local syspath = modpath..'system'..DIR_DELIM + + +mtimer = { + translator = minetest.get_translator('mtimer'), + show_formspec = {}, + meta = { + visible = { key = 'mtimer:visible', default = 'true' }, + } +} + + +dofile(syspath..'chat_command.lua') +dofile(syspath..'formspec_creation.lua') +dofile(syspath..'timer_update.lua') +dofile(syspath..'on_receive_fields.lua') +dofile(syspath..'on_joinplayer.lua') diff --git a/locale/mtimer.de.tr b/locale/mtimer.de.tr new file mode 100644 index 0000000..b56d5a0 --- /dev/null +++ b/locale/mtimer.de.tr @@ -0,0 +1,43 @@ +# textdomain: mtimer + +# Configuration Selection Menu +Toggle Visibility=Sichtbarkeit umschalten +Set Position=Position setzen +Set Color=Farbe einstellen +Timezone Offset=Zeitzonenunterschied +Ingame Time Representation=Spielzeit-Repräsentation +Real World Time Representation=Realzeit-Repräsentation +Set Timer Text=Timertext einstellen + +# Visibility +mTimer Visibility=mTimer-Sichtbarkeit +Visible=Sichtbar +Invisible=Unsichtbar + +# Generic Formspec Strings +Default=Standard +Exit=Verlassen +Back=Zurück + +# Timer Weekdays +Monday=Montag +Tuesday=Dienstag +Wednesday=Mittwoch +Thursday=Donnerstag +Friday=Freitag +Saturday=Samstag +Sunday=Sonntag + +# Timer Months +January=Januar +February=Februar +March=März +April=April +May=Mai +June=Juni +July=Juli +August=Auggust +September=September +Oktober=Oktober +November=November +December=Dezember diff --git a/mod.conf b/mod.conf index 3f1ea88..93a31ba 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,3 @@ name = mtimer -depends = default +depends = default, sfinv description = Ingame timer for showing current playtime, current day time, ingame time, etc. diff --git a/system/chat_command.lua b/system/chat_command.lua new file mode 100644 index 0000000..afaea44 --- /dev/null +++ b/system/chat_command.lua @@ -0,0 +1,10 @@ +local m = mtimer +local S = m.translator + +minetest.register_chatcommand('mtimer', { + description = S('Configure timer display'), + func = function(name, parameters) + local action = parameters:match('%a+') + if not action then m.show_formspec.menu(name) end + end +}) diff --git a/system/formspec_creation.lua b/system/formspec_creation.lua new file mode 100644 index 0000000..41293d8 --- /dev/null +++ b/system/formspec_creation.lua @@ -0,0 +1,30 @@ +local m = mtimer +local S = m.translator + + +mtimer.show_formspec.menu = function (player_name) + minetest.show_formspec(player_name, 'mtimer:main_menu', [[ + size[4,7.75] + button[0,0;4,1;set_visibility;]]..S('Set Visibility')..[[] + button[0,1;4,1;set_position;]]..S('Set Position')..[[] + button[0,2;4,1;set_color;]]..S('Set Color')..[[] + button[0,3;4,1;timezone_offset;]]..S('Timezone Offset')..[[] + button[0,4;4,1;ingame_time;]]..S('Ingame Time Representation')..[[] + button[0,5;4,1;real_time;]]..S('Real World Time Representation')..[[] + button[0,6;4,1;timer_text;]]..S('Set Timer Text')..[[] + button_exit[0,7;4,1;exit;]]..S('Exit')..[[] + ]]) +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('Back')..[[] + ]]) +end diff --git a/system/on_joinplayer.lua b/system/on_joinplayer.lua new file mode 100644 index 0000000..f6cebfe --- /dev/null +++ b/system/on_joinplayer.lua @@ -0,0 +1,9 @@ +local m = mtimer + +minetest.register_on_joinplayer(function(player) + local meta = player:get_meta() + for _,def in pairs(m.meta) do + local current = meta:get_string(def.key) + if current == '' then meta:set_string(def.key, def.default) end + end +end) diff --git a/system/on_receive_fields.lua b/system/on_receive_fields.lua new file mode 100644 index 0000000..8deabe5 --- /dev/null +++ b/system/on_receive_fields.lua @@ -0,0 +1,30 @@ +local m = mtimer +local f = mtimer.show_formspec + +minetest.register_on_player_receive_fields(function(player, formname, fields) + local meta = player:get_meta() + local name = player:get_player_name() + + + -- Select what formspec to show basing on main menu button + if formname == 'mtimer:main_menu' then + if fields.set_visibility then f.set_visibility(name) end + end + + + -- Set timer visibility + if formname == 'mtimer:set_visibility' then + local attr = m.meta.visible + if fields.visible then meta:set_string(attr.key, 'true') end + if fields.invisible then meta:set_string(attr.key, 'false') end + if fields.default then meta:set_string(attr.key, attr.default) end + end + + + -- Back to menu from all formspecs + if fields.back then f.menu(name) end + + + -- DEBUG: Print all player meta data + print(dump(meta:to_table())) +end) diff --git a/system/timer_update.lua b/system/timer_update.lua new file mode 100644 index 0000000..463aae9 --- /dev/null +++ b/system/timer_update.lua @@ -0,0 +1 @@ +local m = mtimer