save time tints into imgui.ini and allow changing the number of tint points

This commit is contained in:
Sven Balzer 2025-03-23 14:16:22 +01:00
parent 2292c049b7
commit a7635a0abf

View File

@ -66,14 +66,16 @@ static SDL_DateTime calendar_time;
static V2 mouse_pos; static V2 mouse_pos;
static int time_tints_times[4][3] = { #define MAX_TINT_TIMES 32
static int num_used_tint_times = 4;
static int time_tints_times[MAX_TINT_TIMES][3] = {
{ 4, 0, 0 }, { 4, 0, 0 },
{ 9, 0, 0 }, { 9, 0, 0 },
{ 19, 0, 0 }, { 19, 0, 0 },
{ 21, 0, 0 }, { 21, 0, 0 },
}; };
static V3 time_tints[4] = { static V3 time_tints[MAX_TINT_TIMES] = {
V3_(0.314f, 0.369f, 0.455f), V3_(0.314f, 0.369f, 0.455f),
V3_(1.0f, 0.891f, 0.868f), V3_(1.0f, 0.891f, 0.868f),
V3_(1.0f, 0.465f, 0.373f), V3_(1.0f, 0.465f, 0.373f),
@ -1375,6 +1377,39 @@ int main(int argc, char **argv) {
}; };
ImGui_ImplSDLGPU3_Init(&imgui_init_info); ImGui_ImplSDLGPU3_Init(&imgui_init_info);
ImGuiSettingsHandler time_tints_settings_handler = {};
time_tints_settings_handler.TypeName = "TimeTints";
time_tints_settings_handler.TypeHash = ImHashStr("TimeTints");
time_tints_settings_handler.ReadOpenFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, const char *name) -> void * {
if (strcmp(name, "num") == 0)
return (void *)-1;
int num = atoi(name) + 1;
return (void *)(Sint64)num;
};
time_tints_settings_handler.ReadLineFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, void *entry, const char *line) {
if (entry == (void *)-1) {
sscanf(line, "num=%d", &num_used_tint_times);
return;
}
if (strncmp(line, "time", 4) == 0) {
sscanf(line, "time=%d %d %d", &time_tints_times[(size_t)entry - 1][0], &time_tints_times[(size_t)entry - 1][1], &time_tints_times[(size_t)entry - 1][2]);
} else if(strncmp(line, "color", 5) == 0) {
sscanf(line, "color=%g %g %g", &time_tints[(size_t)entry - 1][0], &time_tints[(size_t)entry - 1][1], &time_tints[(size_t)entry - 1][2]);
}
};
time_tints_settings_handler.WriteAllFn = [](ImGuiContext *context, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buffer) {
buffer->append("[TimeTints][num]\n");
buffer->appendf("num=%d\n\n", num_used_tint_times);
for (int i = 0; i < num_used_tint_times; i++) {
buffer->appendf("[TimeTints][%d]\n", i);
buffer->appendf("time=%d %d %d\n", time_tints_times[i][0], time_tints_times[i][1], time_tints_times[i][2]);
buffer->appendf("color=%g %g %g\n\n", time_tints[i][0], time_tints[i][1], time_tints[i][2]);
}
};
ImGui::AddSettingsHandler(&time_tints_settings_handler);
bool first_frame = true; bool first_frame = true;
bool show_demo_window = false; bool show_demo_window = false;
bool show_tile_picker = false; bool show_tile_picker = false;
@ -1455,22 +1490,46 @@ int main(int argc, char **argv) {
ImGui::Checkbox("use actual time", &use_actual_time); ImGui::Checkbox("use actual time", &use_actual_time);
ImGui::NewLine(); ImGui::NewLine();
imgui_time_picker("time0", time_tints_times[0]); for (int i = 0; i < num_used_tint_times; i++) {
imgui_time_picker("time1", time_tints_times[1]); ImGui::PushID(i);
imgui_time_picker("time2", time_tints_times[2]); imgui_time_picker("##time", time_tints_times[i]);
imgui_time_picker("time3", time_tints_times[3]); ImGui::PopID();
}
if (ImGui::Button("Add")) num_used_tint_times = clamp(1, num_used_tint_times + 1, MAX_TINT_TIMES);
ImGui::SameLine();
if (ImGui::Button("Remove")) num_used_tint_times = clamp(1, num_used_tint_times - 1, MAX_TINT_TIMES);
ImGui::NewLine(); ImGui::NewLine();
ImGui::ColorEdit3("time0_color", time_tints[0].E); for (int i = 0; i < num_used_tint_times; i++) {
ImGui::ColorEdit3("time1_color", time_tints[1].E); ImGui::PushID(i);
ImGui::ColorEdit3("time2_color", time_tints[2].E); ImGui::ColorEdit3("##color", time_tints[i].E);
ImGui::ColorEdit3("time3_color", time_tints[3].E); ImGui::PopID();
}
if (selected_tile != -1 && ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_R, false)) { if (!ImGui::IsAnyItemActive()) {
if (ImGui::IsKeyDown(ImGuiKey_LeftShift)) { for (int j = 0; j < num_used_tint_times; j++) {
selected_rotation = (selected_rotation - 1) & 3; for (int i = 0; i < num_used_tint_times - 1; i++) {
} else { if (time_tints_times[i][0] > time_tints_times[i + 1][0] ||
selected_rotation = (selected_rotation + 1) & 3; time_tints_times[i][0] == time_tints_times[i + 1][0] && time_tints_times[i][1] > time_tints_times[i + 1][1] ||
time_tints_times[i][0] == time_tints_times[i + 1][0] && time_tints_times[i][1] == time_tints_times[i + 1][1] && time_tints_times[i][2] > time_tints_times[i + 1][2]) {
int temp_time[3] = { time_tints_times[i][0], time_tints_times[i][1], time_tints_times[i][2] };
V3 temp_color = time_tints[i];
time_tints_times[i][0] = time_tints_times[i + 1][0];
time_tints_times[i][1] = time_tints_times[i + 1][1];
time_tints_times[i][2] = time_tints_times[i + 1][2];
time_tints[i] = time_tints[i + 1];
time_tints_times[i + 1][0] = temp_time[0];
time_tints_times[i + 1][1] = temp_time[1];
time_tints_times[i + 1][2] = temp_time[2];
time_tints[i + 1] = temp_color;
}
}
} }
} }
} }
@ -1529,6 +1588,14 @@ int main(int argc, char **argv) {
if (ImGui::Selectable("Random", selected_rotation == -1)) if (ImGui::Selectable("Random", selected_rotation == -1))
selected_rotation = -1; selected_rotation = -1;
} }
if (selected_tile != -1 && ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_R, false)) {
if (ImGui::IsKeyDown(ImGuiKey_LeftShift)) {
selected_rotation = (selected_rotation - 1) & 3;
} else {
selected_rotation = (selected_rotation + 1) & 3;
}
}
} }
ImGui::End(); ImGui::End();
} else { } else {
@ -1805,18 +1872,18 @@ int main(int argc, char **argv) {
{ {
ZoneScopedN("tint color"); ZoneScopedN("tint color");
Sint64 tint_times_ns[5]; Sint64 tint_times_ns[MAX_TINT_TIMES];
for (int i = 0; i < 4; i++) for (int i = 0; i < num_used_tint_times; i++)
tint_times_ns[i] = (time_tints_times[i][0] * 60 * 60 + time_tints_times[i][1] * 60 + time_tints_times[i][2]) * SDL_NS_PER_SECOND; tint_times_ns[i] = (time_tints_times[i][0] * 60 * 60 + time_tints_times[i][1] * 60 + time_tints_times[i][2]) * SDL_NS_PER_SECOND;
tint_times_ns[4] = (24 * 60 * 60 + 60 * 60 + 60) * SDL_NS_PER_SECOND + tint_times_ns[0]; tint_times_ns[num_used_tint_times] = (24 * 60 * 60 + 60 * 60 + 60) * SDL_NS_PER_SECOND + tint_times_ns[0];
Sint64 calendar_time_ns = (calendar_time.hour * 60 * 60 + calendar_time.minute * 60 + calendar_time.second) * SDL_NS_PER_SECOND + (Sint64)calendar_time.nanosecond; Sint64 calendar_time_ns = (calendar_time.hour * 60 * 60 + calendar_time.minute * 60 + calendar_time.second) * SDL_NS_PER_SECOND + (Sint64)calendar_time.nanosecond;
int last_time_index = 3; int last_time_index = num_used_tint_times - 1;
if (calendar_time_ns > tint_times_ns[0]) last_time_index = 0; for (int i = 0; i < num_used_tint_times; i++) {
if (calendar_time_ns > tint_times_ns[1]) last_time_index = 1; if (calendar_time_ns > tint_times_ns[i])
if (calendar_time_ns > tint_times_ns[2]) last_time_index = 2; last_time_index = i;
if (calendar_time_ns > tint_times_ns[3]) last_time_index = 3; }
if (calendar_time_ns <= tint_times_ns[0]) calendar_time_ns += (24 * 60 * 60 + 60 * 60 + 60) * SDL_NS_PER_SECOND; if (calendar_time_ns <= tint_times_ns[0]) calendar_time_ns += (24 * 60 * 60 + 60 * 60 + 60) * SDL_NS_PER_SECOND;
Sint64 v = calendar_time_ns - tint_times_ns[last_time_index]; Sint64 v = calendar_time_ns - tint_times_ns[last_time_index];
@ -1824,7 +1891,7 @@ int main(int argc, char **argv) {
double t = v / (double)time_between; double t = v / (double)time_between;
V3 tint_color = lerp(time_tints[last_time_index], time_tints[(last_time_index + 1) % 4], t); V3 tint_color = lerp(time_tints[last_time_index], time_tints[(last_time_index + 1) % num_used_tint_times], t);
SDL_PushGPUFragmentUniformData(command_buffer, 0, &tint_color, sizeof(tint_color)); SDL_PushGPUFragmentUniformData(command_buffer, 0, &tint_color, sizeof(tint_color));
} }
} }