1 local S
= calendar
._get_translator("calendar")
2 local F
= minetest
.formspec_escape
4 local player_current_calendars
= {}
7 local COLOR_HOLIDAY
= "#00FF00"
8 local COLOR_DAYBOX_HOLIDAY
= "#00940FFF"
9 local COLOR_DAYBOX
= "#1A1A1AFF"
10 local COLOR_DAYBOX_TODAY
= "#FFF508FF"
11 local COLOR_TOOLTIP_TODAY
= COLOR_DAYBOX_TODAY
15 local SHOW_WEEKDAYS
= true
16 local SHOW_TODAY
= true
17 local SHOW_HOLIDAYS
= true
18 local CHANGABLE
= "full"
19 local TODAY_BUTTON
= true
20 local DEFAULT_SETTINGS
= {
22 show_weekdays
= SHOW_WEEKDAYS
,
23 show_today
= SHOW_TODAY
,
24 show_holidays
= SHOW_HOLIDAYS
,
25 changable
= CHANGABLE
,
26 today_button
= TODAY_BUTTON
,
29 -- Offset between dayboxes
31 if minetest
.features
.formspec_version_element
then
37 -- Check if area tooltips are supported.
38 -- minetest.features, doesn't tell us that directly, but
39 -- the formspec version element was added after area tooltips,
40 -- so if those are present, area tooltips have to be supported as well.
41 local SHOW_AREA_TOOLTIPS
= minetest
.features
.formspec_version_element
43 function calendar
.show_calendar(player_name
, settings
, wanted_months
, wanted_years
, caption_format
)
44 -- Get settings, use defaults if needed
46 settings
= DEFAULT_SETTINGS
48 for k
,v
in pairs(DEFAULT_SETTINGS
) do
49 if settings
[k
] == nil then
54 local days
, months
, years
= calendar
.get_date()
55 local total_days
= minetest
.get_day_count()
57 if not wanted_months
then
58 wanted_months
= months
60 if not wanted_years
then
63 wanted_dyears
= wanted_years
64 if settings
.ordinal
then
65 wanted_dyears
= wanted_dyears
+ 1
68 tdays
= tdays
+ wanted_years
* (calendar
.MONTHS
* calendar
.MONTH_DAYS
)
69 tdays
= tdays
+ wanted_months
* calendar
.MONTH_DAYS
74 if caption_format
then
76 if caption_format
[3] then
77 for a
=3, #caption_format
do
78 table.insert(args
, calendar
.get_date_string(caption_format
[a
], tdays
))
81 caption
= calendar
._translate(caption_format
[2], caption_format
[1], unpack(args
))
83 if settings
.ordinal
then
84 caption
= S("@1, year @2", S(calendar
.month_names
[wanted_months
+1]), wanted_dyears
)
86 caption
= S("@1, @2 years", S(calendar
.month_names
[wanted_months
+1]), wanted_dyears
)
90 formspec
= formspec
.. "label[0.5,0.5;"..F(caption
).."]"
93 -- Render weekday names
94 local tdays_start
= tdays
96 if settings
.show_weekdays
then
97 weekday
= calendar
.get_weekday(tdays
)
99 for w
=1, #calendar
.weekday_names_short
do
100 formspec
= formspec
.. "label["..x
..","..y
..";"..F(S(calendar
.weekday_names_short
[w
])).."]"
101 x
= x
+ 1 + DAYBOX_OFFSET
107 -- Render day boxes, day numbers and highlights
109 for iday
=0, calendar
.MONTH_DAYS
- 1 do
110 weekday
= weekday
+ 1
111 if weekday
> calendar
.WEEK_DAYS
then
113 y
= y
+ 1 + DAYBOX_OFFSET
115 x
= (weekday
* (1 + DAYBOX_OFFSET
)) - 0.5
117 if settings
.ordinal
then
120 local day_str
= tostring(pday
)
122 local box_color
= COLOR_DAYBOX
123 local holidays
= calendar
.get_holidays(tdays
)
124 local tooltip_lines
= {}
126 local is_holiday
= false
127 if settings
.show_holidays
and #holidays
> 0 then
129 local text_color
= COLOR_HOLIDAY
130 for h
=1, #holidays
do
131 if holidays
[h
].text_color
then
132 text_color
= holidays
[h
].text_color
134 text_color
= COLOR_HOLIDAY
136 table.insert(tooltip_lines
, minetest
.colorize(text_color
, holidays
[h
].name
))
138 holiday_color
= holidays
[1].text_color
or COLOR_HOLIDAY
139 day_str
= minetest
.colorize(holiday_color
, day_str
)
140 box_color
= holidays
[1].daybox_color
or COLOR_DAYBOX_HOLIDAY
144 if settings
.show_today
and tdays
== total_days
then
145 if SHOW_AREA_TOOLTIPS
then
146 formspec
= formspec
.. "box["..(x
-0.05)..","..(y
-0.05)..";1.1,1.1;"..COLOR_DAYBOX_TODAY
.."]"
148 formspec
= formspec
.. "box["..(x
-0.075)..","..(y
-0.075)..";0.95,1.05;"..COLOR_DAYBOX_TODAY
.."]"
150 table.insert(tooltip_lines
, minetest
.colorize(COLOR_TOOLTIP_TODAY
, S("Today")))
152 if SHOW_AREA_TOOLTIPS
then
153 formspec
= formspec
.. "box["..x
..","..y
..";1,1;"..box_color
.."]"
154 .. "label["..(x
+0.15)..","..(y
+0.075)..";"..day_str
.."]"
157 if is_holiday
and settings
.show_holidays
then
158 day_num
= minetest
.colorize(holiday_color
, pday
)
160 -- Fake button to display the day box. Clicking has no effect, this
161 -- fake button is only used to support tooltips as a workaround
162 -- for lacking support of area tooltips
163 formspec
= formspec
.. "image_button["..x
..","..y
..";1,1;"..
164 F("calendar_legacy_daybox.png^[colorize:"..box_color
..":255]")..
165 ";day"..iday
..";"..F(day_num
).."]"
167 if #tooltip_lines
> 0 then
168 local tooltips
= F(table.concat(tooltip_lines
, "\n"))
169 if SHOW_AREA_TOOLTIPS
then
170 formspec
= formspec
.. "tooltip["..x
..","..y
..";1,1;"..F(tooltips
).."]"
172 formspec
= formspec
.. "tooltip[day"..iday
..";"..F(tooltips
).."]"
177 y
= y
+ 1 + DAYBOX_OFFSET
178 if settings
.show_weekdays
and calendar
.get_weekday(tdays_start
) <= calendar
.get_weekday(tdays
- 1) then
179 y
= y
+ 1 + DAYBOX_OFFSET
181 y
= y
+ DAYBOX_OFFSET
182 local chg_months
= settings
.changable
== "full" or settings
.changable
== "months"
183 local chg_years
= settings
.changable
== "full"
187 if wanted_months
> 0 or wanted_years
> 0 then
188 if (chg_years
or wanted_months
> 0) or (not chg_years
and wanted_months
> 0) then
189 formspec
= formspec
.. "button[1.5,"..y
..";1,1;prev_month;<]"
190 .. "tooltip[prev_month;"..F(S("Previous month")).."]"
193 formspec
= formspec
.. "button[0.5,"..y
..";1,1;prev_year;<<]"
194 .. "tooltip[prev_year;"..F(S("Previous year")).."]"
197 if settings
.today_button
then
198 formspec
= formspec
.. "button[2.5,"..y
..";2,1;today;"..F(S("Today")).."]"
201 if (chg_years
or wanted_months
< calendar
.MONTHS
- 1) or (not chg_years
and wanted_months
< calendar
.MONTHS
- 1) then
202 formspec
= formspec
.. "button[4.5,"..y
..";1,1;next_month;>]"
203 .. "tooltip[next_month;"..F(S("Next month")).."]"
206 formspec
= formspec
.. "button[5.5,"..y
..";1,1;next_year;>>]"
207 .. "tooltip[next_year;"..F(S("Next year")).."]"
210 local size_x
= math
.max(calendar
.WEEK_DAYS
+2, 7)
214 if minetest
.features
.formspec_version_element
then
215 formspec
= "formspec_version[3]" .. formspec
217 formspec
= "size["..size_x
..","..size_y
.."]" .. formspec
218 minetest
.show_formspec(player_name
, "calendar:calendar", formspec
)
220 player_current_calendars
[player_name
] =
221 { years
= wanted_years
, months
= wanted_months
, settings
= settings
, caption_format
= caption_format
}
224 minetest
.register_on_player_receive_fields(function(player
, formname
, fields
)
225 if formname
~= "calendar:calendar" then
228 if not player
:is_player() then
231 local name
= player
:get_player_name()
232 local cur_years
, cur_months
233 if not player_current_calendars
then
237 cur_years
= player_current_calendars
[name
].years
238 cur_months
= player_current_calendars
[name
].months
239 local settings
= player_current_calendars
[name
].settings
241 cur_years
, cur_months
= nil, nil
244 if settings
.changable
== "full" then
245 if fields
.next_year
then
246 cur_years
= cur_years
+ 1
248 elseif fields
.prev_year
then
249 if cur_years
== 0 then
252 cur_years
= cur_years
- 1
257 if fields
.next_month
then
258 cur_months
= cur_months
+ 1
259 if cur_months
> calendar
.MONTHS
- 1 then
260 if settings
.changable
== "full" then
262 cur_years
= cur_years
+ 1
264 cur_months
= calendar
.MONTHS
- 1
268 elseif fields
.prev_month
then
269 cur_months
= cur_months
- 1
270 if cur_months
< 0 and cur_years
> 0 then
271 if settings
.changable
== "full" then
272 cur_months
= calendar
.MONTHS
- 1
273 cur_years
= cur_years
- 1
281 calendar
.show_calendar(name
,
283 cur_months
, cur_years
,
284 player_current_calendars
[name
].caption_format
289 minetest
.register_on_leaveplayer(function(player
)
290 local name
= player
:get_player_name()
291 player_current_calendars
[name
] = nil