From 9daff74e9e1186c6f945cc00a83a802c107e3319 Mon Sep 17 00:00:00 2001 From: Ammerhai Date: Sat, 23 Aug 2025 11:17:12 +0200 Subject: [PATCH] add year and buttons to switch year change last week of previous week to 53 instead of 1 #3 #4 --- src/work-calendar.cpp | 188 ++++++++++++++++++++++++------------------ 1 file changed, 109 insertions(+), 79 deletions(-) diff --git a/src/work-calendar.cpp b/src/work-calendar.cpp index b12c740..46e3335 100644 --- a/src/work-calendar.cpp +++ b/src/work-calendar.cpp @@ -3,6 +3,7 @@ #include "inter.h" +int year = 2025; bool is_leap_year(int year) { return ((year % 4) == 0 && (year % 100) != 0) || year % 400 == 0; @@ -57,103 +58,132 @@ void per_frame(){ ImGui::PushStyleColor(ImGuiCol_HeaderHovered, {0,0,0,0}); ImGui::PushStyleColor(ImGuiCol_HeaderActive, {0,0,0,0}); - for (int month = 1; month <= 12; month++) { - ImGui::PushID(month); - ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {0,0,0,0}); - ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {0,0,0,0}); + if(ImGui::BeginTable("Calendar", 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) { + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); - if(ImGui::BeginTable("Month", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) { - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::TextAligned(0.5, -FLT_MIN, month_names[month - 1]); + ImGui::BeginGroup(); + if (ImGui::ArrowButton("##left", ImGuiDir_Left)) + year--; + ImGui::SameLine(0, 0); - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); + if (ImGui::ArrowButton("##right", ImGuiDir_Right)) + year++; + ImGui::SameLine(0, 0); + + ImGui::TextAligned(0.5, -FLT_MIN, "%d", year); + ImGui::EndGroup(); - //calendar week table - if(ImGui::BeginTable("CalendarWeek", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){ + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + + for (int month = 1; month <= 12; month++) { + ImGui::PushID(month); + ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {0,0,0,0}); + ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {0,0,0,0}); + + if(ImGui::BeginTable("Month", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) { ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); - ImGui::TextAligned(0.5, -FLT_MIN, "KW"); + ImGui::TextAligned(0.5, -FLT_MIN, month_names[month - 1]); - for (int day = 1; day <= days_per_month(2025, month); day++) { - int weekday = weekday_from_day(2025, month, day); + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); - if(weekday == 0 || day == 1){ - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::TextAligned(0.5, -FLT_MIN, "%d", calendar_week_from_day(2025, month, day)); + //calendar week table + if(ImGui::BeginTable("CalendarWeek", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){ + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + ImGui::TextAligned(0.5, -FLT_MIN, "KW"); + + for (int day = 1; day <= days_per_month(year, month); day++) { + int weekday = weekday_from_day(year, month, day); + + if(weekday == 0 || day == 1){ + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + + //if first week has 3 or less days, last week from previous year + int calender_week = calendar_week_from_day(year, month, day); + if(calender_week == 0) { + ImGui::TextAligned(0.5, -FLT_MIN, "%d", 53); + } else { + ImGui::TextAligned(0.5, -FLT_MIN, "%d", calender_week); + } + } } + + ImGui::EndTable(); } + ImGui::SameLine(0, 0); - ImGui::EndTable(); - } - ImGui::SameLine(0, 0); + ImGui::BeginGroup(); + ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0); - ImGui::BeginGroup(); - ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0); + if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){ - if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){ + ImGui::TableSetupColumn("Mo"); + ImGui::TableSetupColumn("Di"); + ImGui::TableSetupColumn("Mi"); + ImGui::TableSetupColumn("Do"); + ImGui::TableSetupColumn("Fr"); + ImGui::TableSetupColumn("Sa"); + ImGui::TableSetupColumn("So"); + ImGui::TableHeadersRow(); - ImGui::TableSetupColumn("Mo"); - ImGui::TableSetupColumn("Di"); - ImGui::TableSetupColumn("Mi"); - ImGui::TableSetupColumn("Do"); - ImGui::TableSetupColumn("Fr"); - ImGui::TableSetupColumn("Sa"); - ImGui::TableSetupColumn("So"); - ImGui::TableHeadersRow(); - - ImGui::EndTable(); - } - ImGui::PopStyleColor(2); - - ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {119/255.0f,119/255.0f,119/255.0f,83/255.0f}); - ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {119/255.0f,119/255.0f,119/255.0f,83/255.0f}); - - //days of month table - if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)) { - int offset = weekday_from_day(2025, month, 1); - - for (int day = 1; day <= days_per_month(2025, month); day++) { - int weekday = weekday_from_day(2025, month, day); - - if(weekday == 0 || day == 1){ - ImGui::TableNextRow(); - } - - ImGui::TableSetColumnIndex(weekday); - ImU32 cell_bg_color_workweek = ImGui::GetColorU32(ImVec4(255/255.0f, 255/255.0f, 255/255.0f, 1.0f)); - ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_workweek); - - //weekend coloring - if(weekday == 5){ - ImU32 cell_bg_color_sat = ImGui::GetColorU32(ImVec4(178/255.0f, 178/255.0f, 178/255.0f, 100/255.0f)); - ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sat); - } - - if(weekday == 6){ - ImU32 cell_bg_color_sun = ImGui::GetColorU32(ImVec4(100/255.0f, 100/255.0f, 100/255.0f, 100/255.0f)); - ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sun); - } - - ImGui::TextAligned(0.5, -FLT_MIN, "%d", day); + ImGui::EndTable(); } + ImGui::PopStyleColor(2); + ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {119/255.0f,119/255.0f,119/255.0f,83/255.0f}); + ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {119/255.0f,119/255.0f,119/255.0f,83/255.0f}); + + //days of month table + if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)) { + int offset = weekday_from_day(year, month, 1); + + for (int day = 1; day <= days_per_month(2025, month); day++) { + int weekday = weekday_from_day(year, month, day); + + if(weekday == 0 || day == 1){ + ImGui::TableNextRow(); + } + + ImGui::TableSetColumnIndex(weekday); + ImU32 cell_bg_color_workweek = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); + ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_workweek); + + //weekend coloring + if(weekday == 5){ + ImU32 cell_bg_color_sat = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 0.1f)); + ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sat); + } + + if(weekday == 6){ + ImU32 cell_bg_color_sun = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 0.2f)); + ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sun); + } + + ImGui::TextAligned(0.5, -FLT_MIN, "%d", day); + } + + ImGui::EndTable(); + } + ImGui::PopStyleColor(2); + ImGui::PopStyleVar(); + //weekday header table + days of month table + ImGui::EndGroup(); + //month table ImGui::EndTable(); } - ImGui::PopStyleColor(2); - ImGui::PopStyleVar(); - //weekday header table + days of month table - ImGui::EndGroup(); - //month table - ImGui::EndTable(); + + if ((month % 4) != 0) + ImGui::SameLine(); + + ImGui::PopID(); } - - if ((month % 4) != 0) - ImGui::SameLine(); - - ImGui::PopID(); + //calendar table + ImGui::EndTable(); } ImGui::PopStyleColor(3);