-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgui.cpp
More file actions
3552 lines (3004 loc) · 113 KB
/
Copy pathgui.cpp
File metadata and controls
3552 lines (3004 loc) · 113 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gui.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_set>
#include "utl_settings.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include "third_party/tinyfiledialogs/tinyfiledialogs.h"
#include "utl_geom.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "utl_log.h"
#include "scr_lua_console.h"
#include "gui_occt_view.h"
#include "utl_io.h"
#include "scr_python_console.h"
#include "shp_info.h"
#include "sketch.h"
#include "utl.h"
#include "version.h"
#include <Standard_Version.hxx>
#include <GLFW/glfw3.h>
using namespace glm;
GUI* gui_instance = nullptr;
GUI::GUI()
{
EZY_ASSERT(!gui_instance);
m_view = std::make_unique<Occt_view>(*this);
gui_instance = this;
}
Length_dimension_style GUI::length_dimension_style() const
{
Length_dimension_style s{};
s.line_width = m_edge_dim_line_width;
s.arrow_size = m_edge_dim_arrow_size;
s.color_rgb[0] = m_edge_dim_color[0];
s.color_rgb[1] = m_edge_dim_color[1];
s.color_rgb[2] = m_edge_dim_color[2];
s.text_height_scale = m_edge_dim_text_scale;
s.label_h = m_edge_dim_label_h;
s.arrow_style = m_edge_dim_arrow_style;
s.arrow_orientation = m_edge_dim_arrow_orientation;
s.text_render_mode = m_edge_dim_text_render_mode;
return s;
}
void GUI::set_show_sketch_dimensions(const bool show)
{
if (m_show_sketch_dimensions == show)
return;
m_show_sketch_dimensions = show;
apply_sketch_dimensions_visibility();
}
void GUI::apply_sketch_dimensions_visibility()
{
if (m_view)
m_view->apply_sketch_dimensions_visibility();
}
ImFont* GUI::console_font() const
{
if (!m_console_font || !ImGui::GetCurrentContext())
return nullptr;
ImFontAtlas* atlas = ImGui::GetIO().Fonts;
if (!atlas)
return nullptr;
for (int i = 0; i < atlas->Fonts.Size; ++i)
if (atlas->Fonts[i] == m_console_font)
return m_console_font;
return nullptr;
}
GUI::~GUI()
{
cleanup_log_redirection_(); // Clean up stream redirection
}
ImVec4 GUI::get_clear_color() const
{
if (m_dark_mode)
return ImVec4(0.10f, 0.10f, 0.12f, 1.00f);
return ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
}
#ifdef __EMSCRIPTEN__
GUI& GUI::instance()
{
EZY_ASSERT(gui_instance);
return *gui_instance;
}
#endif
void GUI::render_gui()
{
// Underlay transform sliders use sketch_plane_view_aabb_2d -> pt_on_plane -> view projection.
// FlushViewEvents must run before ImGui so the camera matches the latest pan/zoom/rotate (do_frame() runs later).
m_view->flush_view_events();
update_window_title_();
if (m_dark_mode)
ImGui::StyleColorsDark();
else
ImGui::StyleColorsLight();
apply_imgui_style_from_members_();
menu_bar_();
dock_space_();
toolbar_();
dist_edit_();
angle_edit_();
sketch_origin_set_edit_();
sketch_list_();
sketch_properties_dialog_();
shape_list_();
shape_info_dialog_();
options_();
message_status_window_();
about_dialog_();
add_box_dialog_();
add_pyramid_dialog_();
add_sphere_dialog_();
add_cylinder_dialog_();
add_cone_dialog_();
add_torus_dialog_();
add_sketch_dialog_();
log_window_();
lua_console_();
python_console_();
settings_();
dbg_();
}
void GUI::render_occt() { m_view->do_frame(); }
// Initialize toolbar buttons
void GUI::initialize_toolbar_()
{
m_toolbar_buttons = {
// clang-format off
{load_texture("res/icons/User.png"), true, "Inspection mode", Mode::Normal},
{load_texture("res/icons/Workbench_Sketcher_none.png"), false, "Sketch inspection mode", Mode::Sketch_inspection_mode},
{load_texture("res/icons/Assembly_AxialMove.png"), false, "Shape move (g)", Mode::Move},
{load_texture("res/icons/Draft_Rotate.png"), false, "Shape rotate (r)", Mode::Rotate},
{load_texture("res/icons/Part_Scale.png"), false, "Shape Scale (s)", Mode::Scale},
{load_texture("res/icons/Macro_FaceToSketch_48.png"), false, "Create a sketch from planar face", Mode::Sketch_from_planar_face},
{load_texture("res/icons/Sketcher_MirrorSketch.png"), false, "Operational axis", Mode::Sketch_operation_axis},
{load_texture("res/icons/Sketcher_CreatePoint.png"), false, "Add node", Mode::Sketch_add_node},
{load_texture("res/icons/Sketcher_Element_Line_Edge.png"), false, "Add line edge", Mode::Sketch_add_edge},
{load_texture("res/icons/ls.png"), false, "Add multi-line edge", Mode::Sketch_add_multi_edges},
{load_texture("res/icons/Sketcher_Element_Arc_Edge.png"), false, "Add arc circle", Mode::Sketch_add_seg_circle_arc},
{load_texture("res/icons/Sketcher_CreateSquare.png"), false, "Add square", Mode::Sketch_add_square},
{load_texture("res/icons/Sketcher_CreateRectangle.png"), false, "Add rectangle from two points", Mode::Sketch_add_rectangle},
{load_texture("res/icons/Sketcher_CreateRectangle_Center.png"), false, "Add rectangle with center point", Mode::Sketch_add_rectangle_center_pt},
{load_texture("res/icons/Sketcher_CreateCircle.png"), false, "Add circle", Mode::Sketch_add_circle},
{load_texture("res/icons/Sketcher_Create3PointCircle.png"), false, "Add circle from three points", Mode::Sketch_add_circle_3_pts},
{load_texture("res/icons/Sketcher_CreateSlot.png"), false, "Add slot", Mode::Sketch_add_slot},
{load_texture("res/icons/TechDraw_LengthDimension.png"), false, "Length dimension (d)", Mode::Sketch_dim_anno},
{load_texture("res/icons/Design456_Extrude.png"), false, "Extrude sketch face (e)", Mode::Sketch_face_extrude},
{load_texture("res/icons/PartDesign_Chamfer.png"), false, "Chamfer (c)", Mode::Shape_chamfer},
{load_texture("res/icons/PartDesign_Fillet.png"), false, "Fillet (f)", Mode::Shape_fillet},
{load_texture("res/icons/Draft_PolarArray.png"), false, "Shape polar duplicate", Mode::Shape_polar_duplicate},
{load_texture("res/icons/Part_Cut.png"), false, "Shape cut", Command::Shape_cut},
{load_texture("res/icons/Part_Fuse.png"), false, "Shape fuse", Command::Shape_fuse},
{load_texture("res/icons/Part_Common.png"), false, "Shape common", Command::Shape_common},
// clang-format on
};
}
void GUI::load_examples_list_()
{
m_example_files.clear();
#ifdef __EMSCRIPTEN__
const std::filesystem::path examples_dir("/res/examples");
#else
const std::filesystem::path examples_dir("res/examples");
#endif
if (!std::filesystem::is_directory(examples_dir))
return;
for (const auto& entry : std::filesystem::directory_iterator(examples_dir))
{
if (!entry.is_regular_file())
continue;
const auto& p = entry.path();
if (p.extension() != ".ezy")
continue;
std::string path = p.string();
std::string label = p.filename().string();
m_example_files.push_back(Example_file{std::move(label), std::move(path)});
}
std::sort(m_example_files.begin(), m_example_files.end(),
[](const Example_file& a, const Example_file& b) { return a.label < b.label; });
}
void GUI::seed_default_dock_layout_(ImGuiID dockspace_id)
{
ImGui::DockBuilderRemoveNode(dockspace_id);
// Cast private DockSpace flag: mixing ImGuiDockNodeFlagsPrivate_ with ImGuiDockNodeFlags_
// triggers -Wdeprecated-enum-enum-conversion (e.g. emscripten/clang).
ImGui::DockBuilderAddNode(dockspace_id, static_cast<ImGuiDockNodeFlags>(ImGuiDockNodeFlags_DockSpace) |
ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetMainViewport()->WorkSize);
ImGuiID dock_main = dockspace_id;
ImGuiID dock_left = ImGui::DockBuilderSplitNode(dock_main, ImGuiDir_Left, 0.20f, nullptr, &dock_main);
ImGuiID dock_right = ImGui::DockBuilderSplitNode(dock_main, ImGuiDir_Right, 0.22f, nullptr, &dock_main);
ImGuiID dock_bottom = ImGui::DockBuilderSplitNode(dock_main, ImGuiDir_Down, 0.12f, nullptr, &dock_main);
ImGuiID dock_left_bottom = 0;
ImGuiID dock_left_top = ImGui::DockBuilderSplitNode(dock_left, ImGuiDir_Down, 0.52f, &dock_left_bottom, &dock_left);
ImGui::DockBuilderDockWindow("Shape List", dock_left_top);
ImGui::DockBuilderDockWindow("Sketch List", dock_left_bottom);
ImGui::DockBuilderDockWindow("Options", dock_right);
ImGui::DockBuilderDockWindow("Log", dock_bottom);
ImGui::DockBuilderFinish(dockspace_id);
}
bool GUI::occt_wants_mouse_at(const float x, const float y) const
{
if (!m_occt_passthrough_valid)
return false;
return x >= m_occt_passthrough_min[0] && x < m_occt_passthrough_max[0] && y >= m_occt_passthrough_min[1] &&
y < m_occt_passthrough_max[1];
}
ScreenCoords GUI::cursor_screen_coords() const
{
EZY_ASSERT(m_glfw_window != nullptr);
double x = 0.0;
double y = 0.0;
glfwGetCursorPos(m_glfw_window, &x, &y);
return ScreenCoords(dvec2(x, y));
}
void GUI::dock_space_()
{
m_occt_passthrough_valid = false;
const ImGuiID dockspace_id = ImGui::DockSpaceOverViewport(ImGui::GetID("EzyCadMainDockSpace"), ImGui::GetMainViewport(),
ImGuiDockNodeFlags_PassthruCentralNode);
if (m_seed_default_dock_layout)
{
seed_default_dock_layout_(dockspace_id);
m_seed_default_dock_layout = false;
}
if (ImGuiDockNode* node = ImGui::DockBuilderGetNode(dockspace_id))
if (ImGuiDockNode* central = node->CentralNode; central && central->IsEmpty())
{
m_occt_passthrough_min[0] = central->Pos.x;
m_occt_passthrough_min[1] = central->Pos.y;
m_occt_passthrough_max[0] = central->Pos.x + central->Size.x;
m_occt_passthrough_max[1] = central->Pos.y + central->Size.y;
m_occt_passthrough_valid = true;
const ImVec2 mouse = ImGui::GetIO().MousePos;
if (occt_wants_mouse_at(mouse.x, mouse.y))
ImGui::SetNextFrameWantCaptureMouse(false);
}
}
void GUI::menu_bar_()
{
if (!ImGui::BeginMainMenuBar())
return;
if (ImGui::BeginMenu("File"))
{
// Use separate `if` (not `else if`) for each entry. `BeginMenu` stays true while the submenu
// is open; chaining with `else if` would skip later items (e.g. Examples hidden while Export open).
if (ImGui::MenuItem("New", "Ctrl+N"))
new_project_();
if (ImGui::MenuItem("Open", "Ctrl+O"))
open_file_dialog_();
if (ImGui::MenuItem("Save", "Ctrl+S"))
save_file_dialog_();
if (ImGui::MenuItem("Save as"))
{
m_last_saved_path.clear(); // Force save as dialog
save_file_dialog_();
}
if (ui_show_feature(2))
{
if (ImGui::MenuItem("Import"))
import_file_dialog_();
if (ImGui::MenuItem("Sketch underlay image..."))
{
m_underlay_import_sketch_target.reset();
sketch_underlay_import_dialog_();
}
}
if (ImGui::BeginMenu("Export"))
{
if (ImGui::MenuItem("STEP (.step)..."))
export_file_dialog_(Export_format::Step);
if (ImGui::MenuItem("IGES (.igs)..."))
export_file_dialog_(Export_format::Iges);
if (ImGui::MenuItem("STL (binary)..."))
export_file_dialog_(Export_format::Stl);
if (ImGui::MenuItem("PLY (binary)..."))
export_file_dialog_(Export_format::Ply);
ImGui::EndMenu();
}
if (ui_show_feature(2) && ImGui::BeginMenu("Examples"))
{
for (const Example_file& ex : m_example_files)
if (ImGui::MenuItem(ex.label.c_str()))
{
std::ifstream file(ex.path, std::ios::binary);
const std::string file_bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
if (file.good() && !file_bytes.empty())
on_file(ex.path, file_bytes);
else
show_message("Error opening example: " + ex.label);
}
ImGui::EndMenu();
}
#ifdef __EMSCRIPTEN__
if (ImGui::MenuItem("Save settings"))
{
save_occt_view_settings();
show_message("Settings saved");
}
#endif
if (ImGui::MenuItem("Exit"))
exit(0);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit"))
{
if (ImGui::MenuItem("Undo", "Ctrl+Z", false, m_view->can_undo()))
m_view->undo();
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, m_view->can_redo()))
m_view->redo();
add_menu_items_();
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View"))
{
bool save_panes = false;
if (ImGui::MenuItem("Settings", nullptr, m_show_settings_dialog))
{
m_show_settings_dialog = !m_show_settings_dialog;
save_panes = true;
}
if (ui_show_feature(1))
{
if (ImGui::MenuItem("Options", nullptr, m_show_options))
{
m_show_options = !m_show_options;
save_panes = true;
}
if (ImGui::MenuItem("Sketch List", nullptr, m_show_sketch_list))
{
m_show_sketch_list = !m_show_sketch_list;
save_panes = true;
}
if (ImGui::MenuItem("Shape List", nullptr, m_show_shape_list))
{
m_show_shape_list = !m_show_shape_list;
save_panes = true;
}
if (ImGui::MenuItem("Log", nullptr, m_log_window_visible))
{
m_log_window_visible = !m_log_window_visible;
save_panes = true;
}
}
if (ui_show_feature(2))
{
if (ImGui::MenuItem("Lua Console", nullptr, m_show_lua_console))
{
m_show_lua_console = !m_show_lua_console;
save_panes = true;
}
if (ui_show_contextual_help() && ImGui::IsItemHovered())
ImGui::SetTooltip("Interactive Lua prompt and res/scripts/lua editors.");
if (ImGui::MenuItem("Python Console", nullptr, m_show_python_console))
{
m_show_python_console = !m_show_python_console;
save_panes = true;
}
}
#ifndef NDEBUG
if (ImGui::MenuItem("Debug", nullptr, m_show_dbg))
{
m_show_dbg = !m_show_dbg;
save_panes = true;
}
#endif
if (save_panes)
save_occt_view_settings();
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help"))
{
if (ImGui::MenuItem("About"))
m_open_about_popup = true;
if (ImGui::MenuItem("Usage Guide"))
open_url_("https://ezycad.readthedocs.io/en/latest/usage.html");
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
void GUI::about_dialog_()
{
if (m_open_about_popup)
{
m_about_popup_open = true;
ImGui::SetNextWindowSize(ImVec2(520.0f, 520.0f), ImGuiCond_Appearing);
ImGui::OpenPopup("About");
m_open_about_popup = false;
}
if (!ImGui::BeginPopupModal("About", &m_about_popup_open, ImGuiWindowFlags_None))
return;
ensure_about_assets_();
ImFont* font = ImGui::GetFont();
ImGui::MarkdownConfig md;
md.linkCallback = about_markdown_link_cb_;
md.imageCallback = about_markdown_image_cb_;
md.tooltipCallback = ImGui::defaultMarkdownTooltipCallback;
md.linkIcon = "";
md.headingFormats[0] = {font, true};
md.headingFormats[1] = {font, true};
md.headingFormats[2] = {font, false};
md.userData = this;
float const fs = ImGui::GetFontSize();
md.headingFormats[0].fontSize = fs * 1.15f;
md.headingFormats[1].fontSize = fs * 1.05f;
md.headingFormats[2].fontSize = fs;
ImGui::BeginChild("AboutMd", ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags_HorizontalScrollbar);
ImGui::Markdown(m_about_markdown.c_str(), m_about_markdown.size(), md);
ImGui::EndChild();
ImGui::EndPopup();
}
std::string GUI::project_title_segment_() const
{
if (m_last_saved_path.empty() || m_last_saved_path == "(startup)")
return "untitled";
try
{
const std::string fn = std::filesystem::path(m_last_saved_path).filename().string();
if (fn.empty())
return "untitled";
return fn;
}
catch (...)
{
return "untitled";
}
}
void GUI::update_window_title_()
{
EZY_ASSERT(m_glfw_window != nullptr);
const std::string title = std::string("EzyCad ") + EZYCAD_VERSION_STRING + " - " + project_title_segment_();
if (title == m_cached_window_title)
return;
m_cached_window_title = title;
glfwSetWindowTitle(m_glfw_window, m_cached_window_title.c_str());
}
void GUI::open_url_(const std::string& url)
{
#ifdef __EMSCRIPTEN__
// For Emscripten, use JavaScript's window.open()
// Use EM_ASM for safer execution
EM_ASM_({ window.open(UTF8ToString($0), '_blank'); }, url.c_str());
#else
// For native builds, use platform-specific system commands
std::string cmd;
#ifdef _WIN32
// Windows: Use 'start' command
cmd = "start \"\" \"";
cmd += url;
cmd += "\"";
#elif defined(__APPLE__)
// TODO test on macOS
// macOS: Use 'open' command
cmd = "open \"";
cmd += url;
cmd += "\"";
#else
// TODO test on Linux
// Linux and other Unix-like systems: Use 'xdg-open'
cmd = "xdg-open \"";
cmd += url;
cmd += "\"";
#endif
system(cmd.c_str());
#endif
}
void GUI::doc_help_button_(const char* scope, int line, const char* tooltip, const char* doc_url, bool trailing_same_line)
{
if (!ui_show_contextual_help())
return;
ImGui::PushID(scope);
ImGui::PushID(line);
if (ImGui::SmallButton("?"))
if (doc_url && doc_url[0] != '\0')
open_url_(doc_url);
ImGui::PopID();
ImGui::PopID();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", tooltip);
if (trailing_same_line)
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
}
void GUI::ensure_about_assets_()
{
if (m_about_assets_loaded)
return;
m_about_assets_loaded = true;
const char* md_paths[] = {
#ifdef __EMSCRIPTEN__
"/res/about.md",
#endif
"res/about.md",
};
for (const char* p : md_paths)
{
std::ifstream f(p);
if (!f)
continue;
m_about_markdown.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
break;
}
if (m_about_markdown.empty())
m_about_markdown = "Could not load res/about.md.\n";
else
m_about_markdown = std::string("**EzyCad ") + EZYCAD_VERSION_STRING + "**\n\n" + m_about_markdown;
m_about_markdown += "\n\nOpen CASCADE ";
m_about_markdown += OCC_VERSION_STRING_EXT;
m_about_markdown += "\nDear ImGui ";
m_about_markdown += IMGUI_VERSION;
m_about_markdown += "\nnlohmann/json ";
m_about_markdown += std::to_string(NLOHMANN_JSON_VERSION_MAJOR);
m_about_markdown += '.';
m_about_markdown += std::to_string(NLOHMANN_JSON_VERSION_MINOR);
m_about_markdown += '.';
m_about_markdown += std::to_string(NLOHMANN_JSON_VERSION_PATCH);
m_about_markdown += "\ntinyfiledialogs ";
m_about_markdown += tinyfd_version;
m_about_markdown += "\nImGuiColorTextEdit ";
m_about_markdown += EZYCAD_IMGUI_COLOR_TEXT_EDIT_REF;
m_about_markdown += "\n";
const char* png_paths[] = {
#ifdef __EMSCRIPTEN__
"/res/AI-gen-splashscreen_05_01_2026_512.png",
#endif
"res/AI-gen-splashscreen_05_01_2026_512.png",
};
for (const char* p : png_paths)
{
if (!std::filesystem::exists(p))
continue;
std::ifstream fi(p, std::ios::binary);
if (!fi)
continue;
std::string bytes((std::istreambuf_iterator<char>(fi)), std::istreambuf_iterator<char>());
if (auto dec = decode_image_bytes(bytes))
{
m_about_splash_size.x = std::get<1>(*dec);
m_about_splash_size.y = std::get<2>(*dec);
}
m_about_splash_gl = load_texture(p);
break;
}
}
ImGui::MarkdownImageData GUI::about_markdown_image_(ImGui::MarkdownLinkCallbackData data)
{
ImGui::MarkdownImageData out{};
if (!data.isImage)
return out;
static constexpr char k_splash_id[] = "AI-gen-splashscreen_05_01_2026_512.png";
std::string id(data.link, data.linkLength);
if (id != k_splash_id || m_about_splash_gl == 0)
return out;
out.isValid = true;
out.useLinkCallback = false;
out.user_texture_id = (ImTextureID)(intptr_t)m_about_splash_gl;
out.size = ImVec2((float)m_about_splash_size.x, (float)m_about_splash_size.y);
ImVec2 const avail = ImGui::GetContentRegionAvail();
if (out.size.x > avail.x && avail.x > 1.0f)
{
float const ratio = out.size.y / out.size.x;
out.size.x = avail.x;
out.size.y = avail.x * ratio;
}
return out;
}
void GUI::about_markdown_link_cb_(ImGui::MarkdownLinkCallbackData data)
{
if (data.isImage || !data.userData)
return;
std::string url(data.link, data.linkLength);
static_cast<GUI*>(data.userData)->open_url_(url);
}
ImGui::MarkdownImageData GUI::about_markdown_image_cb_(ImGui::MarkdownLinkCallbackData data)
{
if (!data.userData)
return {};
return static_cast<GUI*>(data.userData)->about_markdown_image_(data);
}
// Render toolbar with ImGui
void GUI::toolbar_()
{
ImGui::Begin("Toolbar", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDocking);
ImVec2 button_size(32, 32);
for (int i = 0; i < m_toolbar_buttons.size(); i++)
{
ImGui::PushID(i);
bool was_active = false;
if (m_toolbar_buttons[i].is_active)
{
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.26f, 0.59f, 0.98f, 1.00f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.26f, 0.59f, 0.98f, 1.00f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.06f, 0.53f, 0.98f, 1.00f));
was_active = true;
}
// Add a unique string ID (e.g., "button0", "button1", etc.)
char button_id[16];
snprintf(button_id, sizeof(button_id), "button%d", i);
if (ImGui::ImageButton(button_id, (ImTextureID)(intptr_t)m_toolbar_buttons[i].texture_id, button_size))
{
if (m_toolbar_buttons[i].data.index() == 1)
switch (std::get<Command>(m_toolbar_buttons[i].data))
{
case Command::Shape_cut:
if (Status s = m_view->shp_cut().selected_cut(); !s.is_ok())
show_message(s.message());
break;
case Command::Shape_fuse:
if (Status s = m_view->shp_fuse().selected_fuse(); !s.is_ok())
show_message(s.message());
break;
case Command::Shape_common:
if (Status s = m_view->shp_common().selected_common(); !s.is_ok())
show_message(s.message());
break;
default:
EZY_ASSERT(false);
}
else
set_mode(std::get<Mode>(m_toolbar_buttons[i].data));
}
if (ui_show_help(1) && ImGui::IsItemHovered())
ImGui::SetTooltip("%s", m_toolbar_buttons[i].tooltip);
if (was_active)
ImGui::PopStyleColor(3);
ImGui::SameLine();
ImGui::PopID();
}
ImGui::End();
}
// Distance edit related
void GUI::set_dist_edit(float dist, std::function<void(float, bool)>&& callback,
const std::optional<ScreenCoords> screen_coords)
{
// Sketch calls this every mousemove while TAB length mode is on; do not reset value/position each frame
// or typed distance is replaced by the rubber-band length at the cursor.
const bool already_editing = m_dist_callback != nullptr;
if (!already_editing)
{
m_dist_val = dist;
std::snprintf(m_dist_text_buf.data(), m_dist_text_buf.size(), "%.9g", static_cast<double>(dist));
if (screen_coords.has_value())
m_dist_edit_loc = *screen_coords;
else
m_dist_edit_loc = cursor_screen_coords();
}
m_dist_callback = std::move(callback);
if (!already_editing)
m_dist_edit_focus_pending = true;
}
void GUI::hide_dist_edit(bool apply)
{
if (!m_dist_callback)
return;
float parsed{};
if (parse_dist_text_to_float_(m_dist_text_buf.data(), parsed))
m_dist_val = parsed;
std::function<void(float, bool)> callback;
std::swap(callback, m_dist_callback);
if (apply)
callback(m_dist_val, true);
}
void GUI::dist_edit_()
{
if (!m_dist_callback)
return;
// Set the position of the next window
ImGui::SetNextWindowPos(ImVec2(float(m_dist_edit_loc.unsafe_get_x()), float(m_dist_edit_loc.unsafe_get_y())),
ImGuiCond_Always);
// Set a small size (optional)
ImGui::SetNextWindowSize(ImVec2(120.0f, 25.0f), ImGuiCond_Once);
// Begin a window with minimal flags
ImGui::Begin("FloatEdit##unique_id", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMouseInputs);
ImGui::SetNextItemWidth(100.0f);
// Focusing every frame prevents IsItemDeactivatedAfterEdit (click away / Tab) from ever committing.
if (m_dist_edit_focus_pending)
{
ImGui::SetKeyboardFocusHere(0);
m_dist_edit_focus_pending = false;
}
// Text field: InputFloat applies printf rounding so typed digits can disagree with m_dist_val.
const bool text_changed = ImGui::InputText("##dist_edit_text", m_dist_text_buf.data(), m_dist_text_buf.size(),
ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific);
if (text_changed && parse_dist_text_to_float_(m_dist_text_buf.data(), m_dist_val))
m_dist_callback(m_dist_val, false);
if (ImGui::IsItemDeactivatedAfterEdit() && m_dist_callback)
{
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
{
hide_dist_edit(false);
return;
}
float parsed{};
if (parse_dist_text_to_float_(m_dist_text_buf.data(), parsed))
m_dist_val = parsed;
std::function<void(float, bool)> callback;
std::swap(callback, m_dist_callback);
callback(m_dist_val, true);
}
ImGui::End();
}
// Angle edit related
void GUI::set_angle_edit(float angle, std::function<void(float, bool)>&& callback,
const std::optional<ScreenCoords> screen_coords)
{
const bool already_editing = m_angle_callback != nullptr;
if (!already_editing)
{
m_angle_val = angle;
std::snprintf(m_angle_text_buf.data(), m_angle_text_buf.size(), "%.9g", static_cast<double>(angle));
if (screen_coords.has_value())
m_angle_edit_loc = *screen_coords;
else
m_angle_edit_loc = cursor_screen_coords();
}
m_angle_callback = std::move(callback);
if (!already_editing)
m_angle_edit_focus_pending = true;
}
void GUI::hide_angle_edit(bool apply)
{
if (!m_angle_callback)
return;
float parsed{};
if (parse_dist_text_to_float_(m_angle_text_buf.data(), parsed))
m_angle_val = parsed;
std::function<void(float, bool)> callback;
std::swap(callback, m_angle_callback);
if (apply)
callback(m_angle_val, true);
}
bool GUI::is_dist_or_angle_edit_active() const { return m_dist_callback != nullptr || m_angle_callback != nullptr; }
bool GUI::is_sketch_origin_set_edit_active() const { return m_sketch_origin_set_plane_idx >= 0; }
void GUI::open_sketch_origin_set_edit_(const Sketch::sptr& sk, int plane_idx, double v_min, double v_max)
{
EZY_ASSERT(sk);
EZY_ASSERT(plane_idx == 0 || plane_idx == 1);
m_sketch_origin_set_sketch = sk;
m_sketch_origin_set_plane_idx = plane_idx;
m_sketch_origin_set_v_min = v_min;
m_sketch_origin_set_v_max = v_max;
m_sketch_origin_set_loc = ImGui::GetItemRectMin();
m_sketch_origin_set_focus_pending = true;
std::snprintf(m_sketch_origin_set_text_buf.data(), m_sketch_origin_set_text_buf.size(), "%.9g",
m_sketch_origin_xy[plane_idx]);
}
void GUI::hide_sketch_origin_set_edit(bool apply)
{
if (m_sketch_origin_set_plane_idx < 0)
return;
const Sketch::sptr sk = m_sketch_origin_set_sketch.lock();
const int plane_idx = m_sketch_origin_set_plane_idx;
const double v_min = m_sketch_origin_set_v_min;
const double v_max = m_sketch_origin_set_v_max;
m_sketch_origin_set_plane_idx = -1;
m_sketch_origin_set_sketch.reset();
if (!sk || !apply)
return;
float parsed{};
if (!parse_dist_text_to_float_(m_sketch_origin_set_text_buf.data(), parsed))
return;
m_sketch_origin_xy[plane_idx] = std::clamp(static_cast<double>(parsed), v_min, v_max);
sk->set_origin_pt(gp_Pnt2d(m_sketch_origin_xy[0], m_sketch_origin_xy[1]));
m_view->ctx().UpdateCurrentViewer();
}
void GUI::sketch_origin_set_edit_()
{
if (m_sketch_origin_set_plane_idx < 0)
return;
if (!m_sketch_origin_set_sketch.lock())
{
m_sketch_origin_set_plane_idx = -1;
return;
}
ImGui::SetNextWindowPos(m_sketch_origin_set_loc, ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(120.0f, 25.0f), ImGuiCond_Once);
ImGui::Begin("OriginSetEdit##unique_id", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings);
ImGui::SetNextItemWidth(100.0f);
if (m_sketch_origin_set_focus_pending)
{
ImGui::SetKeyboardFocusHere(0);
m_sketch_origin_set_focus_pending = false;
}
ImGui::InputText("##origin_set_text", m_sketch_origin_set_text_buf.data(), m_sketch_origin_set_text_buf.size(),
ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific);
if (ImGui::IsItemDeactivatedAfterEdit())
hide_sketch_origin_set_edit(true);
ImGui::End();
}
void GUI::angle_edit_()
{
if (!m_angle_callback)
return;
// Set the position of the next window
ImGui::SetNextWindowPos(ImVec2(float(m_angle_edit_loc.unsafe_get_x()), float(m_angle_edit_loc.unsafe_get_y())),
ImGuiCond_Always);
// Set a small size (optional)
ImGui::SetNextWindowSize(ImVec2(120.0f, 25.0f), ImGuiCond_Once);
// Begin a window with minimal flags
ImGui::Begin("AngleEdit##unique_id", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMouseInputs);
ImGui::SetNextItemWidth(100.0f);
if (m_angle_edit_focus_pending)
{
ImGui::SetKeyboardFocusHere(0);
m_angle_edit_focus_pending = false;
}
const bool text_changed = ImGui::InputText("##angle_edit_text", m_angle_text_buf.data(), m_angle_text_buf.size(),
ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific);
if (text_changed && parse_dist_text_to_float_(m_angle_text_buf.data(), m_angle_val))