minetest_calendar/gui.lua

190 lines
5.6 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"
local COLOR_DAYBOX = "#00000080"
local COLOR_DAYBOX_TODAY = "#FFFFFF80"
local player_current_calendars = {}
2020-08-27 01:40:04 -07:00
local ORDINAL = true
2020-08-26 17:24:25 -07:00
2020-08-27 10:47:08 -07:00
function calendar.show_calendar(player_name, ordinal, wanted_months, wanted_years)
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-26 17:24:25 -07:00
local ddays, dmonths, dyears = calendar.get_date(nil, ordinal)
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
if ordinal then
wanted_dmonths = wanted_dmonths + 1
wanted_dyears = wanted_dyears + 1
end
2020-08-27 00:42:22 -07:00
local formspec = "formspec_version[3]size["..(calendar.WEEK_DAYS+2)..",10]"
2020-08-26 17:24:25 -07:00
if 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
2020-08-27 00:42:22 -07:00
local weekday = calendar.get_weekday(tdays)
2020-08-26 17:24:25 -07:00
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
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
if ordinal then
pday = iday + 1
end
local day_str = tostring(pday)
local box_color = COLOR_DAYBOX
local holidays = calendar.get_holidays(tdays)
local tooltip_lines = {}
if #holidays > 0 then
table.insert(tooltip_lines, minetest.colorize(COLOR_HOLIDAY, holidays[1].name))
day_str = minetest.colorize(COLOR_HOLIDAY, day_str)
end
2020-08-26 17:47:57 -07:00
if tdays == total_days then
2020-08-26 17:24:25 -07:00
box_color = COLOR_DAYBOX_TODAY
table.insert(tooltip_lines, S("Today"))
end
if #tooltip_lines > 0 then
formspec = formspec .. "tooltip["..x..","..y..";1,1;"..F(table.concat(tooltip_lines, "\n")).."]"
end
day_str = F(day_str)
formspec = formspec .. "box["..x..","..y..";1,1;"..box_color.."]" ..
"label["..(x+0.15)..","..(y+0.3)..";"..day_str.."]"
tdays = tdays + 1
end
2020-08-27 01:09:32 -07:00
y = y + 1.1
if calendar.get_weekday(tdays_start) <= calendar.get_weekday(tdays - 1) then
y = y + 1.1
end
y = y + 0.1
2020-08-26 17:24:25 -07:00
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;<]"
2020-08-27 01:13:13 -07:00
.. "tooltip[prev_month;"..F(S("Previous month")).."]"
.. "tooltip[prev_year;"..F(S("Previous year")).."]"
2020-08-26 17:24:25 -07:00
end
2020-08-27 01:13:13 -07:00
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
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
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
2020-08-27 12:26:16 -07:00
calendar.show_calendar(name, ORDINAL)
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
2020-08-27 12:26:16 -07:00
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
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
2020-08-27 12:26:16 -07:00
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
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
2020-08-27 12:26:16 -07:00
calendar.show_calendar(name, ORDINAL, cur_months, cur_years)
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
2020-08-27 12:26:16 -07:00
calendar.show_calendar(name, ORDINAL, 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)
minetest.register_chatcommand("calendar", {
param = "",
description = S("Display calendar"),
func = function( name, param )
2020-08-27 10:47:08 -07:00
calendar.show_calendar(name, ORDINAL)
end,
})
minetest.register_node("calendar:calendar", {
drawtype = "signlike",
description = S("Calendar"),
tiles = { "calendar_calendar.png" },
inventory_image = "calendar_calendar.png",
wield_image = "calendar_calendar.png",
paramtype = "light",
paramtype2 = "wallmounted",
is_ground_content = false,
walkable = false,
groups = { dig_immediate = 2, attached_node = 1, },
selection_box = {
type = "wallmounted",
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Calendar"))
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if not clicker:is_player() then
return itemstack
end
calendar.show_calendar(clicker:get_player_name(), ORDINAL)
return itemstack
2020-08-26 17:24:25 -07:00
end,
})