Add config options for calendar display

master
Wuzzy 2020-08-27 21:49:53 +02:00
parent 0008ecc85f
commit 367f573738
1 changed files with 46 additions and 25 deletions

71
gui.lua
View File

@ -13,8 +13,18 @@ local COLOR_DAYBOX_TODAY = "#FFFFFF80"
local player_current_calendars = {}
local ORDINAL = true
local SHOW_WEEKDAYS = true
local SHOW_CONTROLS = true
local DEFAULT_SETTINGS = { ordinal = ORDINAL, show_weekdays = SHOW_WEEKDAYS, show_controls = SHOW_CONTROLS }
function calendar.show_calendar(player_name, ordinal, wanted_months, wanted_years)
function calendar.show_calendar(player_name, settings, wanted_months, wanted_years)
local ordinal, show_weekdays, show_controls
if not settings then
settings = DEFAULT_SETTINGS
end
ordinal = settings.ordinal
show_weekdays = settings.show_weekdays
show_controls = settings.show_controls
local days, months, years = calendar.get_date()
local total_days = minetest.get_day_count()
local ddays, dmonths, dyears = calendar.get_date(nil, ordinal)
@ -42,12 +52,16 @@ function calendar.show_calendar(player_name, ordinal, wanted_months, wanted_year
tdays = tdays + wanted_years * (calendar.MONTHS * calendar.MONTH_DAYS)
tdays = tdays + wanted_months * calendar.MONTH_DAYS
local tdays_start = tdays
local weekday = calendar.get_weekday(tdays)
local x, y = 0.75, 1.2
for w=1, #calendar.weekday_names_short do
formspec = formspec .. "label["..x..","..y..";"..calendar.weekday_names_short[w].."]"
x = x + 1.1
local weekday, x, y
if show_weekdays then
weekday = calendar.get_weekday(tdays)
x, y = 0.75, 1.2
for w=1, #calendar.weekday_names_short do
formspec = formspec .. "label["..x..","..y..";"..calendar.weekday_names_short[w].."]"
x = x + 1.1
end
else
weekday = 0
end
x, y = 0.5, 1.7
@ -83,21 +97,23 @@ function calendar.show_calendar(player_name, ordinal, wanted_months, wanted_year
tdays = tdays + 1
end
y = y + 1.1
if calendar.get_weekday(tdays_start) <= calendar.get_weekday(tdays - 1) then
if show_weekdays and calendar.get_weekday(tdays_start) <= calendar.get_weekday(tdays - 1) then
y = y + 1.1
end
y = y + 0.1
if wanted_months > 0 or wanted_years > 0 then
formspec = formspec .. "button[0.5,"..y..";1,1;prev_year;<<]"
.. "button[1.5,"..y..";1,1;prev_month;<]"
.. "tooltip[prev_month;"..F(S("Previous month")).."]"
.. "tooltip[prev_year;"..F(S("Previous year")).."]"
if show_controls then
if wanted_months > 0 or wanted_years > 0 then
formspec = formspec .. "button[0.5,"..y..";1,1;prev_year;<<]"
.. "button[1.5,"..y..";1,1;prev_month;<]"
.. "tooltip[prev_month;"..F(S("Previous month")).."]"
.. "tooltip[prev_year;"..F(S("Previous year")).."]"
end
formspec = formspec .. "button[2.5,"..y..";2,1;today;"..F(S("Today")).."]"
formspec = formspec .. "button[4.5,"..y..";1,1;next_month;>]"
.. "button[5.5,"..y..";1,1;next_year;>>]"
.. "tooltip[next_month;"..F(S("Next month")).."]"
.. "tooltip[next_year;"..F(S("Next year")).."]"
end
formspec = formspec .. "button[2.5,"..y..";2,1;today;"..F(S("Today")).."]"
formspec = formspec .. "button[4.5,"..y..";1,1;next_month;>]"
.. "button[5.5,"..y..";1,1;next_year;>>]"
.. "tooltip[next_month;"..F(S("Next month")).."]"
.. "tooltip[next_year;"..F(S("Next year")).."]"
minetest.show_formspec(player_name, "calendar:calendar", formspec)
player_current_calendars[player_name] = { years = wanted_years, months = wanted_months }
@ -115,27 +131,29 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if not player_current_calendars then
return
end
local show = false
cur_years = player_current_calendars[name].years
cur_months = player_current_calendars[name].months
if fields.today then
calendar.show_calendar(name, ORDINAL)
cur_years, cur_months = nil, nil
show = true
elseif fields.next_year then
cur_years = cur_years + 1
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
show = true
elseif fields.prev_year then
if cur_years == 0 then
cur_months = 0
else
cur_years = cur_years - 1
end
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
show = true
elseif fields.next_month then
cur_months = cur_months + 1
if cur_months > calendar.MONTHS - 1 then
cur_months = 0
cur_years = cur_years + 1
end
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
show = true
elseif fields.prev_month then
cur_months = cur_months - 1
if cur_months < 0 then
@ -144,7 +162,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
cur_years = cur_years - 1
end
end
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
show = true
end
if show then
calendar.show_calendar(name, DEFAULT_SETTINGS, cur_months, cur_years)
end
end)
@ -157,7 +178,7 @@ minetest.register_chatcommand("calendar", {
param = "",
description = S("Display calendar"),
func = function( name, param )
calendar.show_calendar(name, ORDINAL)
calendar.show_calendar(name, DEFAULT_SETTINGS)
end,
})
@ -183,7 +204,7 @@ minetest.register_node("calendar:calendar", {
if not clicker:is_player() then
return itemstack
end
calendar.show_calendar(clicker:get_player_name(), ORDINAL)
calendar.show_calendar(clicker:get_player_name(), DEFAULT_SETTINGS)
return itemstack
end,
})