add basic save and load
add menu - save - load - demo on/off - close + popup #9 #10
This commit is contained in:
+110
-2
@@ -1,9 +1,14 @@
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "inter.h"
|
||||
|
||||
extern bool should_exit;
|
||||
extern bool show_demo_window;
|
||||
|
||||
//standard year
|
||||
int year = 2025;
|
||||
|
||||
@@ -49,6 +54,8 @@ void init(){
|
||||
year = get_current_year();
|
||||
}
|
||||
|
||||
static char* savefile_path = "./calendar";
|
||||
|
||||
static const char* month_names[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
|
||||
|
||||
typedef struct Category {
|
||||
@@ -57,7 +64,7 @@ typedef struct Category {
|
||||
char name[64] = "New Category";
|
||||
} Category;
|
||||
|
||||
static size_t num_categories = 0;
|
||||
static uint32_t num_categories = 0;
|
||||
static Category categories[128];
|
||||
|
||||
static bool legend_visible = true;
|
||||
@@ -72,7 +79,7 @@ typedef struct Categorized_Day {
|
||||
} Categorized_Day;
|
||||
|
||||
static int year_min_size = 1;
|
||||
static size_t num_categorized_days = 0;
|
||||
static uint32_t num_categorized_days = 0;
|
||||
static Categorized_Day categorized_days[365*50];
|
||||
|
||||
//Colors
|
||||
@@ -82,6 +89,58 @@ ImVec4 table_sunday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.2f);
|
||||
|
||||
ImVec4 table_hover_color = ImVec4(0.0f, 0.3f, 0.6f, 0.2f);
|
||||
|
||||
void save(){
|
||||
uint32_t version = 1;
|
||||
FILE *save_file = fopen(savefile_path, "wb");
|
||||
fwrite(&version, sizeof(version), 1, save_file);
|
||||
fwrite(&num_categories, sizeof(num_categories), 1, save_file);
|
||||
fwrite(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);
|
||||
|
||||
for (int i = 0; i < num_categories; i++) {
|
||||
fwrite(&categories[i].color, sizeof(categories[i].color), 1, save_file);
|
||||
fwrite(&categories[i].name, sizeof(categories[i].name), 1, save_file);
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_categorized_days; i++) {
|
||||
fwrite(&categorized_days[i].year, sizeof(categorized_days[i].year), 1, save_file);
|
||||
fwrite(&categorized_days[i].month, sizeof(categorized_days[i].month), 1, save_file);
|
||||
fwrite(&categorized_days[i].day, sizeof(categorized_days[i].day), 1, save_file);
|
||||
fwrite(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
|
||||
}
|
||||
|
||||
fclose (save_file);
|
||||
}
|
||||
|
||||
void load(){
|
||||
FILE *save_file = fopen(savefile_path, "rb");
|
||||
uint32_t version = 0;
|
||||
fread(&version, sizeof(version), 1, save_file);
|
||||
if(version != 1) {
|
||||
return; //TODO Popup warning
|
||||
}
|
||||
|
||||
fread(&num_categories, sizeof(num_categories), 1, save_file);
|
||||
fread(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);
|
||||
|
||||
if(num_categories > IM_ARRAYSIZE(categories) || num_categorized_days > IM_ARRAYSIZE(categorized_days)){
|
||||
return; //TODO Popup warning
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_categories; i++) {
|
||||
fread(&categories[i].color, sizeof(categories[i].color), 1, save_file);
|
||||
fread(&categories[i].name, sizeof(categories[i].name), 1, save_file);
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_categorized_days; i++) {
|
||||
fread(&categorized_days[i].year, sizeof(categorized_days[i].year), 1, save_file);
|
||||
fread(&categorized_days[i].month, sizeof(categorized_days[i].month), 1, save_file);
|
||||
fread(&categorized_days[i].day, sizeof(categorized_days[i].day), 1, save_file);
|
||||
fread(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
|
||||
}
|
||||
|
||||
fclose (save_file);
|
||||
}
|
||||
|
||||
void per_frame(){
|
||||
ImGuiStyle &style = ImGui::GetStyle();
|
||||
|
||||
@@ -101,8 +160,29 @@ void per_frame(){
|
||||
}
|
||||
ImGui::DockSpaceOverViewport(main_viewport_dock, ImGui::GetMainViewport(), ImGuiDockNodeFlags_NoTabBar);
|
||||
|
||||
ImGuiID close_popup = ImGui::GetID("Close");
|
||||
ImGuiID open_popup = ImGui::GetID("Open");
|
||||
ImGuiID save_popup = ImGui::GetID("Save");
|
||||
ImGuiID logout_popup = ImGui::GetID("Logout"); //TODO
|
||||
ImGuiID about_popup = ImGui::GetID("About");
|
||||
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMenu("Datei")) {
|
||||
if (ImGui::MenuItem("Kalender öffnen", NULL)) {
|
||||
//TODO popup + filepicker
|
||||
load();
|
||||
}
|
||||
if (ImGui::MenuItem("Kalender speichern", NULL)) {
|
||||
save();
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
ImGui::MenuItem("Abmelden", NULL);
|
||||
|
||||
ImGui::Separator();
|
||||
if (ImGui::MenuItem("Beenden", NULL)) {
|
||||
ImGui::OpenPopup(close_popup);
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
@@ -111,9 +191,36 @@ void per_frame(){
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("Hilfe")) {
|
||||
ImGui::MenuItem("Demo", NULL, &show_demo_window);
|
||||
ImGui::MenuItem("Wiki", NULL);
|
||||
ImGui::Separator();
|
||||
ImGui::MenuItem("Über", NULL);
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
|
||||
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
|
||||
if (ImGui::BeginPopupModal("Close", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize)) {
|
||||
ImGui::Text("Möchten Sie wirklich beenden?");
|
||||
|
||||
ImGui::SetItemDefaultFocus();
|
||||
if (ImGui::Button("Ja", ImVec2(120, 0))) {
|
||||
should_exit = true;
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Nein", ImVec2(120, 0))) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
if (ImGui::Begin("Work Calendar", 0)) {
|
||||
ImGui::PushStyleColor(ImGuiCol_TableHeaderBg, {0,0,0,0});
|
||||
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, {0,0,0,0});
|
||||
@@ -133,6 +240,7 @@ void per_frame(){
|
||||
ImGui::TableSetColumnIndex(1);
|
||||
if (ImGui::ArrowButton("##left", ImGuiDir_Left))
|
||||
year--;
|
||||
|
||||
ImGui::TableSetColumnIndex(2);
|
||||
ImGui::TextAligned(0.5, -FLT_MIN, "%d", year);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user