From 519124b8cfc38fc41dd0c0b96dc539f7b5928784 Mon Sep 17 00:00:00 2001 From: Ammerhai Date: Mon, 11 Aug 2025 21:17:29 +0200 Subject: [PATCH] add basic calendar --- src/main.cpp | 6 ++-- src/work-calendar.cpp | 82 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 88ae792..8d4cc25 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,13 +149,13 @@ int main(int, char**) ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); + //work calendar app + per_frame(); + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); - //work calendar app - per_frame(); - // Rendering ImGui::Render(); const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w }; diff --git a/src/work-calendar.cpp b/src/work-calendar.cpp index aa498e2..808124a 100644 --- a/src/work-calendar.cpp +++ b/src/work-calendar.cpp @@ -1,6 +1,82 @@ #include + +bool is_leap_year(int year) { + return ((year % 4) == 0 && (year % 100) != 0) || year % 400 == 0; +} + +int weekday_from_day(int year, int month, int day) { + int a = (14 - month) / 12; + int y = year - a; + int m = month + (12 * a) - 2; + return (day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7; +} + +int calendar_week_from_day(int year, int month, int day){ + int days_sum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + int days = days_sum[month - 1]; + if(is_leap_year(year) && month > 2) + days++; + + return (10 + days + day - weekday_from_day(year, month, day)) / 7; +} + +int days_per_month(int year, int month){ + int days_in_month[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; + if (month == 2) { + return is_leap_year(year) ? 29 : 28; + } + return days_in_month[month - 1]; +} + void per_frame(){ - //TODO - //ImGui::Begin(); -} \ No newline at end of file + static bool use_work_area = true; + static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings; + // We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.) + // Based on your use case you may want one or the other. + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos); + ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size); + + //Begin + ImGui::Begin("Work Calendar", 0, flags); + + for (int month = 1; month <= 12; month++) { + ImGui::PushID(month); + //month + if(ImGui::BeginTable("Month", 8, ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_NoHostExtendX)) { + ImGui::TableSetupColumn("KW"); + ImGui::TableSetupColumn("Mo"); + ImGui::TableSetupColumn("Di"); + ImGui::TableSetupColumn("Mi"); + ImGui::TableSetupColumn("Do"); + ImGui::TableSetupColumn("Fr"); + ImGui::TableSetupColumn("Sa"); + ImGui::TableSetupColumn("So"); + ImGui::TableHeadersRow(); + + int offset = weekday_from_day(2025, month, 1); + //tage + 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(0); + ImGui::Text("%d", calendar_week_from_day(2025, month, day)); + } + + ImGui::TableSetColumnIndex(weekday + 1); + ImGui::Text("%d", day); + } + + ImGui::EndTable(); + if ((month % 4) != 0) + ImGui::SameLine(); + } + ImGui::PopID(); + } + + ImGui::End(); +} +