minetest_calendar/gui.lua

184 lines
5.3 KiB
Lua
Raw Normal View History

2020-08-26 17:24:25 -07:00
local S
if minetest.get_translator then
S = minetest.get_translator("calendar")
else
S = function(s) return s end
end
local F = minetest.formspec_escape
local COLOR_HOLIDAY = "#00FF00"
2020-08-27 14:45:21 -07:00
local COLOR_DAYBOX_HOLIDAY = "#00940FFF"
local COLOR_DAYBOX = "#1A1A1AFF"
local COLOR_DAYBOX_TODAY = "#FFF508FF"
local COLOR_TOOLTIP_TODAY = COLOR_DAYBOX_TODAY
2020-08-26 17:24:25 -07:00
local player_current_calendars = {}
2020-08-28 02:48:07 -07:00
local DEFAULT_SETTINGS = {
ordinal = true,
show_weekdays = true,
show_today = true,
show_holidays = true,
show_controls = true,
}
2020-08-26 17:24:25 -07:00
function calendar.show_calendar(player_name, settings, wanted_months, wanted_years)
if not settings then
settings = DEFAULT_SETTINGS
end
2020-08-26 17:24:25 -07:00
local days, months, years = calendar.get_date()
2020-08-26 17:47:57 -07:00
local total_days = minetest.get_day_count()
2020-08-28 02:48:07 -07:00
local ddays, dmonths, dyears = calendar.get_date(nil, settings.ordinal)
2020-08-26 17:24:25 -07:00
local wanted_dmonths, wanted_dyears
if not wanted_months then
wanted_months = months
end
if not wanted_years then
wanted_years = years
end
wanted_dmonths = wanted_months
wanted_dyears = wanted_years
2020-08-28 02:48:07 -07:00
if settings.ordinal then
2020-08-26 17:24:25 -07:00
wanted_dmonths = wanted_dmonths + 1
wanted_dyears = wanted_dyears + 1
end
local formspec = ""
2020-08-28 02:48:07 -07:00
if settings.ordinal then
2020-08-27 01:40:04 -07:00
formspec = formspec .. "label[0.5,0.5;"..F(S("Month @1, year @2", wanted_dmonths, wanted_dyears)).."]"
2020-08-26 17:24:25 -07:00
else
2020-08-27 01:40:04 -07:00
formspec = formspec .. "label[0.5,0.5;"..F(S("@1 months, @2 years", wanted_dmonths, wanted_dyears)).."]"
2020-08-26 17:24:25 -07:00
end
local start_day, end_day
local tdays = 0
tdays = tdays + wanted_years * (calendar.MONTHS * calendar.MONTH_DAYS)
tdays = tdays + wanted_months * calendar.MONTH_DAYS
2020-08-27 01:09:32 -07:00
local tdays_start = tdays
local weekday, x, y
2020-08-28 02:48:07 -07:00
if settings.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
2020-08-26 17:24:25 -07:00
end
x, y = 0.5, 1.7
for iday=0, calendar.MONTH_DAYS - 1 do
weekday = weekday + 1
if weekday > calendar.WEEK_DAYS then
weekday = 1
y = y + 1.1
end
x = (weekday*1.1)- 0.5
local pday = iday
2020-08-28 02:48:07 -07:00
if settings.ordinal then
2020-08-26 17:24:25 -07:00
pday = iday + 1
end
local day_str = tostring(pday)
local box_color = COLOR_DAYBOX
local holidays = calendar.get_holidays(tdays)
local tooltip_lines = {}
2020-08-28 02:48:07 -07:00
if settings.show_holidays and #holidays > 0 then
for h=1, #holidays do
table.insert(tooltip_lines, minetest.colorize(COLOR_HOLIDAY, holidays[h].name))
end
2020-08-26 17:24:25 -07:00
day_str = minetest.colorize(COLOR_HOLIDAY, day_str)
2020-08-27 14:18:37 -07:00
box_color = COLOR_DAYBOX_HOLIDAY
2020-08-26 17:24:25 -07:00
end
2020-08-27 14:26:47 -07:00
day_str = F(day_str)
2020-08-28 02:48:07 -07:00
if settings.show_today and tdays == total_days then
2020-08-27 14:26:47 -07:00
formspec = formspec .. "box["..(x-0.05)..","..(y-0.05)..";1.1,1.1;"..COLOR_DAYBOX_TODAY.."]"
2020-08-27 14:45:21 -07:00
table.insert(tooltip_lines, minetest.colorize(COLOR_TOOLTIP_TODAY, S("Today")))
2020-08-26 17:24:25 -07:00
end
2020-08-27 14:26:47 -07:00
formspec = formspec .. "box["..x..","..y..";1,1;"..box_color.."]" ..
"label["..(x+0.15)..","..(y+0.3)..";"..day_str.."]"
2020-08-26 17:24:25 -07:00
if #tooltip_lines > 0 then
formspec = formspec .. "tooltip["..x..","..y..";1,1;"..F(table.concat(tooltip_lines, "\n")).."]"
end
tdays = tdays + 1
end
2020-08-27 01:09:32 -07:00
y = y + 1.1
2020-08-28 02:48:07 -07:00
if settings.show_weekdays and calendar.get_weekday(tdays_start) <= calendar.get_weekday(tdays - 1) then
2020-08-27 01:09:32 -07:00
y = y + 1.1
end
y = y + 0.1
2020-08-28 02:48:07 -07:00
if settings.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")).."]"
2020-08-26 17:24:25 -07:00
end
local size_x = math.max(calendar.WEEK_DAYS+2, 7)
local size_y = y+1.5
formspec = "formspec_version[3]size["..size_x..","..size_y.."]" .. formspec
2020-08-26 17:24:25 -07:00
minetest.show_formspec(player_name, "calendar:calendar", formspec)
player_current_calendars[player_name] = { years = wanted_years, months = wanted_months }
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "calendar:calendar" then
return
end
if not player:is_player() then
return
end
local name = player:get_player_name()
local cur_years, cur_months
if not player_current_calendars then
return
end
local show = false
2020-08-26 17:24:25 -07:00
cur_years = player_current_calendars[name].years
cur_months = player_current_calendars[name].months
2020-08-27 01:13:13 -07:00
if fields.today then
cur_years, cur_months = nil, nil
show = true
2020-08-27 01:13:13 -07:00
elseif fields.next_year then
2020-08-26 17:24:25 -07:00
cur_years = cur_years + 1
show = true
2020-08-26 17:24:25 -07:00
elseif fields.prev_year then
if cur_years == 0 then
cur_months = 0
else
cur_years = cur_years - 1
end
show = true
2020-08-26 17:24:25 -07:00
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
show = true
2020-08-26 17:24:25 -07:00
elseif fields.prev_month then
cur_months = cur_months - 1
if cur_months < 0 then
if cur_years > 0 then
cur_months = calendar.MONTHS - 1
cur_years = cur_years - 1
end
end
show = true
end
if show then
calendar.show_calendar(name, DEFAULT_SETTINGS, cur_months, cur_years)
2020-08-26 17:24:25 -07:00
end
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_current_calendars[name] = nil
end)