From 2886017721558f8ee304cfc964697452defc6803 Mon Sep 17 00:00:00 2001 From: Ammerhai Date: Tue, 18 Nov 2025 18:07:08 +0100 Subject: [PATCH] add daycoloring with categories (legend) change default window size #6 --- src/main_win32.cpp | 2 +- src/work-calendar.cpp | 97 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/main_win32.cpp b/src/main_win32.cpp index d3f1626..7d4a1f9 100644 --- a/src/main_win32.cpp +++ b/src/main_win32.cpp @@ -39,7 +39,7 @@ int main(int, char**) // Create application window WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; ::RegisterClassExW(&wc); - HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr); + HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1600 * main_scale), (int)(900 * main_scale), nullptr, nullptr, wc.hInstance, nullptr); // Initialize Direct3D if (!CreateDeviceD3D(hwnd)) diff --git a/src/work-calendar.cpp b/src/work-calendar.cpp index 5ee5c21..f58dc31 100644 --- a/src/work-calendar.cpp +++ b/src/work-calendar.cpp @@ -51,16 +51,32 @@ void init(){ static const char* month_names[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}; -typedef struct { - bool editing = false; - float color[3]; +typedef struct Category { + bool editing; + ImColor color; char name[64] = "New Category"; } Category; -size_t num_categories = 0; -Category categories[128]; +static size_t num_categories = 0; +static Category categories[128]; + +static bool legend_visible = true; +static int selected_category = -1; + +typedef struct Categorized_Day { + int year; + int month; + int day; + + int category; +} Categorized_Day; + +static size_t num_categorized_days = 0; +static Categorized_Day categorized_days[365*50]; void per_frame(){ + ImGuiStyle &style = ImGui::GetStyle(); + ImGuiID main_viewport_dock = ImGui::GetID("main_viewport_dock"); if (!ImGui::DockBuilderGetNode(main_viewport_dock)) { ImGui::DockBuilderAddNode (main_viewport_dock, ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_NoTabBar); @@ -68,13 +84,26 @@ void per_frame(){ ImGui::DockBuilderSetNodeSize(main_viewport_dock, ImGui::GetMainViewport()->WorkSize); ImGuiID left_dock = 0; - ImGuiID right_dock = ImGui::DockBuilderSplitNode(main_viewport_dock, ImGuiDir_Right, 0.15f, NULL, &left_dock); + ImGuiID right_dock = ImGui::DockBuilderSplitNode(main_viewport_dock, ImGuiDir_Right, 0.2f, NULL, &left_dock); ImGui::DockBuilderDockWindow("Work Calendar", left_dock); ImGui::DockBuilderDockWindow("Legende", right_dock); ImGui::DockBuilderFinish(main_viewport_dock); } ImGui::DockSpaceOverViewport(main_viewport_dock, ImGui::GetMainViewport(), ImGuiDockNodeFlags_NoTabBar); + if (ImGui::BeginMainMenuBar()) { + if (ImGui::BeginMenu("Datei")) { + ImGui::EndMenu(); + } + + if (ImGui::BeginMenu("Ansicht")) { + ImGui::MenuItem("Legende", NULL, &legend_visible); + ImGui::EndMenu(); + } + + ImGui::EndMainMenuBar(); + } + if (ImGui::Begin("Work Calendar", 0)) { ImGui::PushStyleColor(ImGuiCol_TableHeaderBg, {0,0,0,0}); ImGui::PushStyleColor(ImGuiCol_HeaderHovered, {0,0,0,0}); @@ -178,8 +207,52 @@ void per_frame(){ ImU32 cell_bg_color_sun = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 0.2f)); ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sun); } + + for (int i = 0; i < num_categorized_days; i++) { + if (categorized_days[i].year != year) continue; + if (categorized_days[i].month != month) continue; + if (categorized_days[i].day != day) continue; + + ImColor color = categories[categorized_days[i].category].color; + color.Value.w = 0.5; + ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImU32(color)); + } ImGui::TextAligned(0.5, -FLT_MIN, "%d", day); + + if (selected_category != -1 && ImGui::IsItemClicked()) { + for (int i = 0; i < num_categorized_days; i++) { + if (categorized_days[i].year != year) continue; + if (categorized_days[i].month != month) continue; + if (categorized_days[i].day != day) continue; + + if (categorized_days[i].category == selected_category) { + categorized_days[i] = categorized_days[num_categorized_days-1]; + num_categorized_days--; + } else { + categorized_days[i] = { + .year = year, + .month = month, + .day = day, + + .category = selected_category, + }; + num_categorized_days++; + } + + goto color_changed; + } + + categorized_days[num_categorized_days] = { + .year = year, + .month = month, + .day = day, + + .category = selected_category, + }; + num_categorized_days++; + } + color_changed: } ImGui::EndTable(); @@ -203,7 +276,7 @@ void per_frame(){ ImGui::End(); // Legende - if (ImGui::Begin("Legende", 0)) { + if (legend_visible && ImGui::Begin("Legende", 0)) { if(ImGui::BeginTable("Legend", 4, ImGuiTableFlags_SizingStretchProp)) { ImGui::TableSetupColumn("Color", ImGuiTableColumnFlags_WidthFixed); ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); @@ -216,8 +289,11 @@ void per_frame(){ ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); - ImGuiColorEditFlags color_edit_flags = ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoOptions; - ImGui::ColorEdit3(category.name, category.color, ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoInputs | (category.editing ? 0 : color_edit_flags)); + if (ImGui::Selectable("##Category", selected_category == i, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap, { 0, style.FramePadding.y * 2 + ImGui::CalcTextSize("M").y })) { + selected_category = selected_category == i ? -1 : i; + } + ImGui::SameLine(); + ImGui::ColorEdit3(category.name, &category.color.Value.x, ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoInputs); ImGui::TableSetColumnIndex(1); if (category.editing) { @@ -251,6 +327,7 @@ void per_frame(){ ImGui::EndTable(); } } - ImGui::End(); + if (legend_visible) + ImGui::End(); }