Repository navigation
Expand file tree
/
Copy pathdimgui.cpp
More file actions
1462 lines (1399 loc) · 45.1 KB
/
Copy pathdimgui.cpp
File metadata and controls
1462 lines (1399 loc) · 45.1 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
/* $Copyright: $
* Copyright (c) 2022-2025 by Steve Baker (steve.baker.llc@gmail.com)
* and Thomas Moore (darktjm@gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* All IMGUI-related code is in here.
*
* TODO:
* - some sizes (e.g. button widths) hard-coded/not font-responsive
* - for game: "Load Config" checkbox, default checked
* (or Yes/No/Always/Never)
* - bindkey dialog
* - RST key is same as RESET action
* - keyboard: shifted symbols
* - tab interface should have good default size automatically
* - each tab should grow to fit total size
* - joystick should limit growth to keep circle size in check
* - switching tabs should not resize dialog
* - change displayed bound menu action key when rebound via config or GUI
*/
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "imgui.h"
#include "imgui_stdlib.h"
// Note: sdl.h was renamed to sdl2.h in 1.89.3. Very annoying.
#include "backends/imgui_impl_sdl2.h"
#include "backends/imgui_impl_opengl3.h"
#include "ImGuiFileDialog.h"
/* imgui_markdown.h has warnings, but I don't care enough to fix them */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#ifndef __clang__ // clang just warns that this warning group doesn't exist
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#include "imgui_markdown.h"
#pragma GCC diagnostic pop
#include "imgui_internal.h" // Needed for GetFocusID(). Why is that private?
// Also for drawing underlines
// Also to disable nav highlight on ImageButton
#include <GL/gl.h>
#include <regex>
extern "C" {
#include "Aq.h"
#include <fontconfig/fontconfig.h>
}
#include "gui_common.h"
using namespace ImGui;
static ImGuiID _rwinid = 0; // IMGUI ID for rendering widget (for focus detect)
static value_t *_imgui_state = NULL;
static const char *state_val(const char *key);
void imgui_glinit(void)
{
// Decide GL+GLSL versions
// From example_sdl_opengl3
// Why is this not just in the impl file?
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
#define glsl_version "#version 100"
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
// GL 3.2 Core + GLSL 150
#define glsl_version "#version 150"
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
// GL 3.0 + GLSL 130
#define glsl_version "#version 130"
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
}
static pthread_mutex_t imgui_lock = PTHREAD_MUTEX_INITIALIZER;
// In imgui-1.89.3, this was a structure. Changed to pointer because static
// structure init doesn't correclty set "automatic" context (set by construtor
// executed at program startup, before context even exists). This could've
// been designed better (e.g. init to NULL and if NULL, auto-init on use)
// c8ad25caa6bfcb786085a76e67a7f31e61a80cb7
static ImGuiFileDialog *_rdlg, *_tdlg, *_bdlg;
static ImGuiFileDialog *fdialog(const char *dir);
static ImFont *_bold, *_italic, *_small;
static ImFont *load_font(const char *name, float scale = 1.0f, bool fc_only = false);
void imgui_init(int scale)
{
pthread_mutex_lock(&imgui_lock);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
CreateContext();
ImGuiIO& io = GetIO();
io.FontGlobalScale = (double)scale / 3; // * 3 / 4? 1.0, since I added font pref?
// From imgui.h, to change:
// For correct scaling, prefer to reload font + rebuild ImFontAtlas +
// call style.ScaleAllSizes().
// On the other hand, for temporary (local to window) change:
io.FontAllowUserScaling = true; // allow ctrl+wheel scaling of any window
// Actually, I should probably disable this, since it's not really
// configurable, and is globally active.
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// In particular, while I would like gamepad navigation, it's annoying
// when you're actually using the gamepad for a game and the menu suddenly
// gets activated.
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.IniFilename = NULL;
// Load persistent state
if(_config_file) {
// XDG_STATE would be most appropriate, but nobody uses that.
// XDG_CACHE would be more appropriate, but I don't care.
const char *cf = find_xdg_file(XDG_CONFIG, "Aq", "imgui.json");
if(cf) {
_imgui_state = json_decode_file(cf);
// I don't like persistent locations, but whatever.
const char *winstate = state_val("winstate");
if(winstate && winstate[0])
LoadIniSettingsFromMemory(winstate);
}
}
_rdlg = fdialog("roms");
_tdlg = fdialog("tapes");
_bdlg = fdialog("bas");
// Setup Dear ImGui style
StyleColorsDark();
//StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(_window, _gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Load Fonts
FcInit();
add_default_str("imfont", "Sans");
const char *font = config_str("imfont");
if(!font || !*font || !load_font(font)) {
io.Fonts->AddFontDefault();
ImFontConfig cfg;
cfg.SizePixels = io.Fonts->ConfigData[0].SizePixels * 0.7;
_small = io.Fonts->AddFontDefault(&cfg);
} else
_small = load_font(font, 0.7);
add_default_type("imbold", VDT_NULL);
const char *ofont = config_str("imbold");
if(ofont && *ofont)
_bold = load_font(ofont);
else if(font && *font) {
// FIXME: there is probably a way in fontconfig to just add the
// bold attrbute without tacking it to the font name
char buf[strlen(font) + 6];
strcpy(buf, font);
strcat(buf, ":bold");
_bold = load_font(buf, 1.0f, true);
}
add_default_type("imitalic", VDT_NULL);
if((ofont = config_str("imitalic")) && *ofont)
_italic = load_font(ofont);
else if(font && *font) {
// FIXME: there is probably a way in fontconfig to just add the
// italic attrbute without tacking it to the font name
char buf[strlen(font) + 8];
strcpy(buf, font);
strcat(buf, ":italic");
_italic = load_font(buf, 1.0f, true);
}
pthread_mutex_unlock(&imgui_lock);
}
static const char *state_val(const char *key)
{
const value_t *v = json_objval(_imgui_state, key);
if(v && v->type == VDT_STRING)
return v->s;
else
return NULL;
}
static void write_imgui_state(void)
{
if(!_imgui_state)
return;
// XDG_STATE would be most appropriate, but nobody uses that.
// XDG_CACHE would be more appropriate, but I don't care.
mkpath(find_xdg_file(XDG_CONFIG, "Aq", NULL));
FILE *f = fopen(find_xdg_file(XDG_CONFIG, "Aq/imgui.json", NULL), "w");
if(!f)
return;
char *fv = json_pretty(_imgui_state);
fputs(fv, f);
fclose(f);
free(fv);
}
static void set_imgui_state(const char *key, const char *val)
{
value_t v = { .type = VDT_STRING, .s = (char *)val };
if(!_imgui_state) {
_imgui_state = (value_t *)malloc(sizeof(*_imgui_state));
_imgui_state->type = VDT_OBJECT;
_imgui_state->o = NULL;
}
json_set_objval(_imgui_state, key, &v);
write_imgui_state();
}
static struct menukey {
char pkey, key;
bool pactive : 1, active : 1;
} *_menu_hot;
static int _num_menus, _cur_menu = -1;
bool _tmp_show_menubar = false;
// Returns true if GUI consumed the event
bool imgui_event(SDL_Event *event)
{
pthread_mutex_lock(&imgui_lock);
ImGui_ImplSDL2_ProcessEvent(event);
pthread_mutex_unlock(&imgui_lock);
// Technically, some mouse events are also consumed by the GUI
// Modal dialogs definitely take focus, as they should
// I expect menus to take focus if active, since they support key nav
// QUIT is global. I should process above instead, but let z80.c do it.
if(_menu_hot && (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP)) {
if((event->key.keysym.mod & KMOD_ALT) &&
!(event->key.keysym.mod & (KMOD_SHIFT|KMOD_GUI|KMOD_CTRL))) {
char c = toupper(event->key.keysym.sym);
for(int i = 0; i < _num_menus; i++)
if(_menu_hot[i].pkey == c) {
if(event->type == SDL_KEYDOWN) {
_tmp_show_menubar = true;
_menu_hot[i].pactive = true;
}
return true;
}
}
static char mpress = 0;
if(!(event->key.keysym.mod & (KMOD_SHIFT|KMOD_GUI|KMOD_CTRL|KMOD_ALT))) {
char c = toupper(event->key.keysym.sym);
if(_cur_menu >= 0) {
for(int i = _cur_menu; i < _num_menus; i++)
if(_menu_hot[i].key == c &&
_menu_hot[i].pkey == _menu_hot[_cur_menu].pkey) {
if(event->type == SDL_KEYDOWN)
_menu_hot[i].active = true;
mpress = c;
return true;
}
} else if(c && c == mpress) {
mpress = 0;
if(event->type == SDL_KEYUP)
return true;
}
}
}
return event->type != SDL_QUIT && GetFocusID() != _rwinid;
}
static void MDLink(MarkdownLinkCallbackData data)
{
if(!data.isImage) {
string url(data.link, data.linkLength);
SDL_OpenURL(url.c_str());
}
}
static ImFont *load_font(const char *name, _U float scale, bool fc_only)
{
// FIXME: 20 is a random number.
#define fontf() fc_only ? NULL :GetIO().Fonts->AddFontFromFileTTF(name, 20);
#if 0
// This does not fail gracefully. It assert()s/kills on failure.
ImFont *ret = fontf();
if(ret)
return ret;
#endif
// Instead, load from file as last resort.
// I suppose I should at least support a -<size> suffix, but I won't
auto pat = FcNameParse((const FcChar8 *)name);
if(!pat)
return fontf();
// I have no idea what to do with pat, so I'll just
// copy what fc-match and xft do:
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
FcResult fcr;
auto match = FcFontMatch(NULL, pat, &fcr);
FcPatternDestroy(pat);
if(!match)
return fontf();
FcChar8 *fname;
if(FcPatternGetString(match, FC_FILE, 0, &fname) != FcResultMatch) {
FcPatternDestroy(match);
return fontf(); // can't open font w/o filename
}
ImFontConfig cfg; // default constructor
fcr = FcPatternGetInteger(match, FC_INDEX, 0, &cfg.FontNo);
double dsize;
fcr = FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &dsize);
if(fcr != FcResultMatch) {
float vdpi;
SDL_GetDisplayDPI(0, NULL, NULL, &vdpi);
if(!vdpi)
vdpi = 100;
fcr = FcPatternGetDouble(match, FC_SIZE, 0, &dsize);
if(fcr != FcResultMatch) {
FcPatternDestroy(match);
return fontf();
}
dsize = dsize *vdpi / 72; // 72 points per inch
}
dsize *= scale;
ImFont *ret = GetIO().Fonts->AddFontFromFileTTF((const char *)fname, (float)dsize, &cfg /*, glyph_ranges*/);
FcPatternDestroy(match);
// if(!ret)
// return fontf();
return ret;
}
void MDFormat(const MarkdownFormatInfo& info, bool start)
{
if(info.type == MarkdownFormatType::EMPHASIS && info.level == 1 &&
_italic) {
if(start)
PushFont(_italic);
else
PopFont();
return;
}
if(info.type == MarkdownFormatType::EMPHASIS && info.level > 1) {
if(_bold) {
if(start)
PushFont(_bold);
else
PopFont();
} else {
if(start)
// PushStyleColor(ImGuiCol_Text, ImGui::GetStyle().Colors[ImGuiCol_NavHighlight]);
PushStyleColor(ImGuiCol_Text, ImVec4(0.4, 1, 1, 1));
else
PopStyleColor();
}
} else
defaultMarkdownFormatCallback(info, start);
}
// Yeah, I'm pointlessly using a mostly unconfigured MD module to display this
// I'm too lazy to hand-craft the dialog
// FIXME: Esc should exit (now only Enter exits)
// Yeah, I just copied this from grok. Whatever. I wrote that, too.
// Well, actually, I converted to Markdown for ImGui
// Actually, it's pseudo-markdown. \n ends a paragraph.
static const char about_message[] =
"Mattel **Aq**uarius Emulator\n"
"Version " VERSION "\n"
"Compiled " __DATE__ "\n\n"
// Dammit, imgui_markdown doesn't support <>. Needs []().
#if 0
"Author: Steve Baker <steve.baker.llc@gmail.com>\n"
/* FIXME: the homepage should be more specific */
"Homepage: <https://oldmanprogrammer.net>\n"
"With help from\nThomas J. Moore <darktjm@gmail.com>\n"
#else
"Author: Steve Baker <[steve.baker.llc@gmail.com](mailto:steve.baker.llc@gmail.com)>\n"
/* FIXME: the homepage should be more specific */
"Homepage: <[https://oldmanprogrammer.net](https://oldmanprogrammer.net)>\n"
"With help from\nThomas J. Moore <[darktjm@gmail.com](mailto:darktjm@gmail.com)>\n"
#endif
// FIXME: add repository link & issue tracker link
;
static MarkdownConfig mdConfig = {
.linkCallback = MDLink, .formatCallback = MDFormat
};
static bool _show_about = false;
void show_about(void)
{
// This should just be OpenPopup(), but it has to be done
// below to get the ID right
_show_about = true;
}
static void process_about(int w, int h)
{
ImVec2 center = GetMainViewport()->GetCenter();
SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImVec2 dim(w, h); // It'll shrink to fit, so maximize to avoid scroll/wrap
SetNextWindowSize(dim, ImGuiCond_Appearing); // needs initial size or it will be too narrow
if(_show_about) {
// This has to be done here to get the ID right
OpenPopup("About Aq");
_show_about = false;
}
if(BeginPopupModal("About Aq", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
Markdown(about_message, strlen(about_message), mdConfig);
Dummy(ImVec2(0, 10));
ImVec2 rem = GetContentRegionAvail();
if(rem.x > 120)
Indent((rem.x - 120) / 2);
// SetKeyboardFocusHere(); // causes flashing every time mouse moves
static bool pressed_enter = false, pressed_esc = false;
if(Button("OK", ImVec2(120, 0))) {
CloseCurrentPopup();
pressed_enter = pressed_esc = false;
} else {
// since the focus methods don't work, and don't do esc anyway,
// handle ok/esc manually
bool enter = IsKeyPressed(ImGuiKey_Enter);
bool esc = IsKeyPressed(ImGuiKey_Escape);
if(enter)
pressed_enter = true;
if(esc)
pressed_esc = true;
if((pressed_enter && !IsKeyDown(ImGuiKey_Enter)) ||
(pressed_esc && !IsKeyDown(ImGuiKey_Escape))) {
pressed_enter = pressed_esc = false;
CloseCurrentPopup();
}
}
// SetItemDefaultFocus(); // does nothing
EndPopup();
}
}
/********* File popups *********/
static ImGuiFileDialog *fdialog(const char *dir)
{
ImGuiFileDialog *ret = new ImGuiFileDialog;
// set bookmarks. 4 standard locations + persistent user entries
const char *bm = state_val(dir);
if(bm && *bm)
ret->DeserializeBookmarks(bm);
ret->AddBookmark("Here", ".");
ret->AddBookmark(dir, dir);
int slen = strlen(dir);
char buf[slen + 4];
memcpy(buf, "Aq/", 3);
strcpy(buf + 3, dir);
ret->AddBookmark("User", find_xdg_file(XDG_CONFIG, buf, NULL));
char sbuf[slen + sizeof(SYS_DIR) + 1];
memcpy(sbuf, SYS_DIR, sizeof(SYS_DIR) - 1);
sbuf[sizeof(SYS_DIR) - 1] = '/';
strcpy(sbuf + sizeof(SYS_DIR), dir);
ret->AddBookmark("System", sbuf);
return ret;
}
static void finish_fdialog(ImGuiFileDialog *dlg, const char *dir)
{
// FIXME: retain path (probably need one per file type)
// filePath = dlg->GetCurrentPath();
// filter = dlg->GetCurrentFilter();
string save = dlg->SerializeBookmarks(true);
const char *oldb = state_val(dir);
if((save.size() && !oldb) || (oldb && strcmp(oldb, save.c_str())))
set_imgui_state(dir, save.c_str());
dlg->Close();
}
// FIXME: file name is mandatory
bool _askrom_noreset = false;
void askrom(romdesc *rom)
{
char *wdbuf = NULL;
// Where did current ROM come from?
// if not there, Qt defaults to cur dir
const char *wd = file_from(rom->where, "roms", &wdbuf);
string cap(rom->what), fname(rom->where ? rom->where : "");
cap += " ROM File";
// v0.6.5 case-insensitive ignores last item in group, so repeat 1st
// easy to fix/patch, but I prefer keeping it pristine
_rdlg->OpenDialog("askrom", cap, "ROMs{.rom,.bin,.rom},All{(())}", wd, fname, 1, rom,
// What an obnoxious flag naming convention
ImGuiFileDialogFlags_Modal|
ImGuiFileDialogFlags_CaseInsensitiveExtention);
if(wdbuf)
free(wdbuf);
}
static void process_askrom(int w, int h)
{
if(_rdlg && _rdlg->Display("askrom", ImGuiWindowFlags_NoCollapse,
ImVec2(w*0.6, h*0.6))) {
if(_rdlg->IsOk()) {
romdesc *rom = (romdesc *)_rdlg->GetUserDatas();
auto sel = _rdlg->GetFilePathName();
if(!rom->where != !sel.size() || (rom->where && sel != rom->where)) {
if(rom->pref) {
set_config_str(rom->pref, sel.c_str());
rom->where = config_str(rom->pref);
} else {
rom->buf = sel;
if(sel.size())
rom->where = rom->buf.c_str();
else
rom->where = NULL;
}
}
if(!_askrom_noreset)
_doreset = true;
}
_askrom_noreset = false;
finish_fdialog(_rdlg, "roms");
}
}
void asktape()
{
char *wdbuf = NULL;
const char *wd = find_xdg_file(XDG_DATA, "Aq/tapes", NULL);;
if(_tape)
wd = file_from(_tape, "tapes", &wdbuf);
if(!_tdlg)
_tdlg = new ImGuiFileDialog;
// regex pointlessly needs double paren around it.
// regex parser doesn't support internal parens. No way to really fix this.
// (should be ..|\.(tape|caq)$)
_tdlg->OpenDialog("asktape", "Tape File", "Tapes{((^tape$|\\.tape$|\\.caq$|\\.TAPE$|\\.CAQ$))},All{(())}", wd, _tape ? _tape : "", 1, &_tape,
// What an obnoxious flag naming convention
ImGuiFileDialogFlags_Modal|
ImGuiFileDialogFlags_CaseInsensitiveExtention);
if(wdbuf)
free(wdbuf);
}
static void process_asktape(int w, int h)
{
if(_tdlg && _tdlg->Display("asktape", ImGuiWindowFlags_NoCollapse,
ImVec2(w*0.6, h*0.6))) {
if(_tdlg->IsOk()) {
auto sel = _tdlg->GetFilePathName();
if(!_tape != !sel.size() || (_tape && sel != _tape))
// FIXME: leaks _tape
_tape = strdup(sel.c_str());
}
finish_fdialog(_tdlg, "tapes");
asked_tape();
}
}
void askbasic(void)
{
if(!_bdlg)
_bdlg = new ImGuiFileDialog;
// v0.6.5 case-insensitive ignores last item in group, so repeat 1st
// easy to fix/patch, but I prefer keeping it pristine
_bdlg->OpenDialog("askbas", "BASIC File", "BASIC{.bas,.txt,.bas},All{(())}", ".", "", 1, &_tape,
// What an obnoxious flag naming convention
ImGuiFileDialogFlags_Modal|
ImGuiFileDialogFlags_CaseInsensitiveExtention);
}
static void process_askbasic(int w, int h)
{
if(_bdlg && _bdlg->Display("askbas", ImGuiWindowFlags_NoCollapse,
ImVec2(w*0.6, h*0.6))) {
if(_bdlg->IsOk()) {
auto sel = _bdlg->GetFilePathName();
if(sel.size())
load_basic(sel.c_str());
}
finish_fdialog(_bdlg, "bas");
}
}
static bool _show_romdlg;
void askroms(void)
{
// This should just be OpenPopup(), but it has to be done
// below to get the ID right
// Well, actually, the file dialog quits the popup, so this isn't a popup
_show_romdlg = true;
}
static void dorom(romdesc *rom, const char *def,
const char *chk = NULL, bool *bv = NULL)
{
// For some reason, there is a wide gap between the text entry and the button.
// For that reason, I'm abandoning Table.
#define USE_TABLES 0
#if USE_TABLES
TableNextRow();
TableNextColumn();
#else
auto rem = GetContentRegionAvail();
unsigned rw = rem.x > 0 ? (unsigned)rem.x : 0;
#endif
AlignTextToFramePadding();
Text("%s ROM", rom->what);
#if USE_TABLES
TableNextColumn();
#else
SameLine();
// check lab len instead of x for smoother resize
// otherwise, there is a noticable jump of all text widgets to left before
// re-stabilizing at the correct x.
// rendered text len would be better, but strlen is probably good enough
// at least until internationalization
static unsigned max_ll = 0;
unsigned ll = strlen(rom->what);
if(ll > max_ll)
max_ll = ll;
static unsigned text_size = 0, prev_w = 0;
unsigned x = floor(GetCursorPosX());
if(ll == max_ll)
text_size = x;
Dummy(ImVec2(text_size - x, 0));
SameLine();
#endif
// blank label not supported; otherwise absorbs next widget into focus group
// Maybe adding a dummy after it would work as well, but a space works.
string lab(" ##");
lab += rom->what;
#if !USE_TABLES
static unsigned itsize = 200;
SetNextItemWidth(itsize);
#endif
InputTextWithHint(lab.c_str(), def, &rom->buf);
#if USE_TABLES
TableNextColumn();
#else
SameLine();
#endif
lab = "..." + lab.substr(1);
// This button won't respond until the text entry box is selected if the
// text entry's label is blank or just a hashval.
if(Button(lab.c_str())) {
_askrom_noreset = true;
askrom(rom); // ??? this pops down the dialog if it's a poup.
}
#if USE_TABLES
TableNextColumn();
#else
SameLine();
unsigned cbw = floor(GetCursorPosX());
#endif
if(chk)
Checkbox(chk, bv);
#if USE_TABLES
else
// If you really wanted me to use this more, you
// should've named it something shorter
// e.g. TextRaw
TextUnformatted("");
#else
if(chk)
SameLine();
static unsigned mcb;
x = floor(GetCursorPosX());
NewLine();
cbw = x - cbw;
if(rw != prev_w || cbw > mcb)
mcb = cbw;
if(cbw == mcb && rw != x)
itsize += (int)rw - x;
prev_w = rw;
#endif
}
static void process_romdlg(int w, int h)
{
static ImVec2 dim;
if(!dim.x)
dim = ImVec2(w*2/3,h/3);
static const char *grom_tmp, *mrom_tmp, *crom_tmp, *ebrom_tmp;
static romdesc grom = { .where = grom_tmp };
static romdesc mrom = { .where = mrom_tmp };
static romdesc crom = { .where = crom_tmp };
static romdesc ebrom = { .where = ebrom_tmp };
static bool showing = false;
static bool save_gr, use_eb;
if(_show_romdlg) {
// This has to be done here to get the ID right
// OpenPopup("Select ROMs");
// but file dialog closes this, so it's a window now.
if(!grom.what) {
grom.what = _gamerom_desc.what;
mrom.what = _mainrom_desc.what;
crom.what = _charrom_desc.what;
ebrom.what = _ebrom_desc.what;
}
grom.buf = _gamerom_desc.where ? _gamerom_desc.where : "";
save_gr = false;
mrom.buf = _mainrom_desc.where;
crom.buf = _charrom_desc.where;
ebrom.buf = _ebrom_desc.where;
use_eb = _extendedbasic;
_show_romdlg = false;
showing = true;
}
if(showing) {
ImVec2 center = GetMainViewport()->GetCenter();
SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
SetNextWindowSizeConstraints(ImVec2(w/3,dim.y), ImVec2(w,dim.y));
SetNextWindowSize(dim, ImGuiCond_Appearing);
}
// This can't be a popup.
// Modal is an "internal only" flag
if(showing && Begin("Select ROMs", NULL, ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse)) {
#if USE_TABLES
if(BeginTable("table1", 4)) {
TableSetupColumn("lab", ImGuiTableColumnFlags_WidthFixed);
TableSetupColumn("input", ImGuiTableColumnFlags_WidthStretch);
TableSetupColumn("but", ImGuiTableColumnFlags_WidthFixed);
TableSetupColumn("check", ImGuiTableColumnFlags_WidthFixed);
#endif
dorom(&grom, "None if blank", "Save", &save_gr);
dorom(&mrom, DEFAULT_MAINROM);
dorom(&crom, DEFAULT_CHARROM);
dorom(&ebrom, DEFAULT_EBROM, "Use", &use_eb);
#if USE_TABLES
EndTable();
}
#endif
Separator();
auto rem = GetContentRegionAvail();
// Probably not good to make Enter accept by default.
static bool /* pressed_enter = false, */ pressed_esc = false;
if(Button("Cancel", ImVec2(120, 0)))
// CloseCurrentPopup();
showing = false;
SameLine();
rem.x -= GetCursorPosX();
if(rem.x > 120)
Indent(rem.x);
// SetKeyboardFocusHere();
if(Button("OK/Reset", ImVec2(120,0))) {
// CloseCurrentPopup();
showing = false;
ud_rom(_gamerom_desc, grom, NULL);
if(save_gr)
set_config_str("cartridge", _gamerom_desc.where);
ud_rom(_mainrom_desc, mrom, DEFAULT_MAINROM);
ud_rom(_charrom_desc, crom, DEFAULT_CHARROM);
ud_rom(_ebrom_desc, ebrom, DEFAULT_EBROM);
if(use_eb != _extendedbasic)
_doreset = true;
_extendedbasic = use_eb;
set_config_bool("use_ebasic", use_eb);
}
// SetItemDefaultFocus();
if(IsWindowFocused()) {
// bool enter = IsKeyPressed(ImGuiKey_Enter);
bool esc = IsKeyPressed(ImGuiKey_Escape);
// if(enter)
// pressed_enter = true;
if(esc)
pressed_esc = true;
if(/*(pressed_enter && !IsKeyDown(ImGuiKey_Enter)) || */
(pressed_esc && !IsKeyDown(ImGuiKey_Escape))) {
/* pressed_enter = */ pressed_esc = false;
// CloseCurrentPopup();
showing = false;
}
}
rem = GetContentRegionAvail();
if(rem.y != 0) {
dim.y -= rem.y;
SetWindowSize(dim);
}
End();
}
}
/********* Main window ***********/
void win_title(const char *t)
{
SDL_SetWindowTitle(_window, t);
}
static void copy_noamp(char *d, const char *s)
{
do {
if(*s == '&' && s[1])
s++;
*d++ = *s;
} while(*s++);
}
static bool calc_ul(const char *namp, const char *raw,
ImVec2 &start, ImVec2 &end, double scale)
{
// yet more internals, just to draw the underline
auto dc = GetCurrentWindow()->DC;
const ImGuiMenuColumns* offsets = &dc.MenuColumns;
const char *keyoff = strchr(raw, '&'), *nae = namp + (keyoff - raw);;
while(keyoff && keyoff[1] == '&') {
nae--;
keyoff = strchr(keyoff + 2, '&');
}
if(!keyoff)
return false;
start = ImVec2(dc.CursorPos.x + offsets->OffsetLabel, dc.CursorPos.y);
start += CalcTextSize(namp, nae, true);
start.y += GetFont()->Descent * scale;
end = start;
ImVec2 key_size = CalcTextSize(keyoff + 1, keyoff +2, true);
end.x += key_size.x;
return true;
}
static void popup_menu(bool horiz)
{
if(!_menu_hot) {
for(_num_menus = 0; actions[_num_menus].parent; _num_menus++);
_menu_hot = (menukey *)malloc(_num_menus * sizeof(*_menu_hot));
const char *prev = NULL;
char hkey = 0;
for(int i = 0; i < _num_menus; i++) {
const char *p = actions[i].parent;
if(!prev || strcmp(p, prev)) {
prev = p;
const char *h;
while(1) {
h = strchr(p, '&');
if(h && h[1] != '&')
break;
h += 2;
}
if(h)
hkey = toupper(h[1]);
}
_menu_hot[i].pkey = hkey;
const char *h;
while(1) {
h = strchr(p, '&');
if(h && h[1] != '&')
break;
h += 2;
}
_menu_hot[i].key = h ? toupper(h[1]) : 0;
_menu_hot[i].pactive = false;
_menu_hot[i].active = false;
}
}
const char *cur = NULL;
bool is_act = false, any_act = false;
char mbuf[16], ibuf[64];
auto col = GetColorU32(ImGuiCol_Text);
double scale = GetCurrentWindow()->FontWindowScale;
auto dl = GetWindowDrawList();
for(int i = 0; i < _num_menus; i++) {
const char *p = actions[i].parent;
if(!cur || strcmp(cur, p)) {
if(is_act)
EndMenu();
copy_noamp(mbuf, cur = p);
ImVec2 start, end;
calc_ul(mbuf, p, start, end, scale);
if(horiz) {
float off = IM_FLOOR(GetStyle().ItemSpacing.x * 0.5f);
start.x += off;
end.x += off;
}
if(_menu_hot[i].pactive) {
OpenPopup(mbuf);
_menu_hot[i].pactive = false;
}
is_act = BeginMenu(mbuf);
if(is_act) {
any_act = true;
_cur_menu = i;
}
dl->AddLine(start, end, col, scale);
}
if(!is_act)
continue;
copy_noamp(ibuf, actions[i].name);
ImVec2 start, end;
bool draw_ul = calc_ul(ibuf, actions[i].name, start, end, scale);
bool act = actions[i].toggle ?
MenuItem(ibuf, actions[i].def_binding, actions[i].toggle) :
MenuItem(ibuf, actions[i].def_binding);
if(!act && _menu_hot[i].active) {
_menu_hot[i].active = false;
act = true;
CloseCurrentPopup();
if(actions[i].toggle)
*actions[i].toggle ^= true;
}
if(act) {
actions[i].f();
if(actions[i].toggle && actions[i].pref)
set_config_bool(actions[i].pref, *actions[i].toggle);
} else if(draw_ul)
GetWindowDrawList()->AddLine(start, end, col, scale);
}
if(is_act)
EndMenu();
if(!any_act) {
_tmp_show_menubar = false;
_cur_menu = -1;
}
}
void toggle_menu()
{
}
static void process_bindkey(int w, int h);
void imgui_update(unsigned w, unsigned h, unsigned long texid)
{
pthread_mutex_lock(&imgui_lock);
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
NewFrame();
SetNextWindowPos(ImVec2(0, 0));
SetNextWindowSize(ImVec2(w, h));
uint32_t bg, fg;
color(_bordercolor, &fg, &bg);
bg = (bg & 0xff00ff00) + ((bg & 0xff0000) >> 16) +
((bg & 0xff) << 16);
PushStyleColor(ImGuiCol_WindowBg, bg);
if(Begin("mainwin", NULL,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus |
(_tmp_show_menubar || _show_menubar ? ImGuiWindowFlags_MenuBar : 0))) {
if((_tmp_show_menubar || _show_menubar) && BeginMenuBar()) {
popup_menu(true);
EndMenuBar();
}
exec_actions(true);
// FIXME: aspect correct? 4:3/narrow pixels? Or square (5:3)?
auto dim = GetContentRegionAvail();
auto pos = GetCursorScreenPos();
if(dim.x > dim.y * 4 / 3) {
// Dummy(ImVec2((dim.x - dim.y * 4 / 3) / 2, 0));
SameLine((dim.x - dim.y * 4 / 3) / 2);
dim.x = dim.y * 4 / 3;
} else if(dim.y > dim.x * 3 / 4) {
Dummy(ImVec2(0, (dim.y - dim.x * 3 / 4) / 2));
dim.y = dim.x * 3 / 4;
}
// I don't want borders. It's possible to remove the frame border:
#if 0
PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
// And, using an internal, undocumented feature, the nav border:
GetCurrentWindow()->DC.NavHideHighlightOneFrame = true;
// There really ought to be a way to disable this in the real API.
ImageButton((ImTextureID)texid, dim);
PopStyleVar();
#else
// But in the end, I decided not to use ImageButton(). Instead,
// I use an InvisibleButton with the image overlayed. This should
// avoid any automatic graphical modification entirely.
// Besides, I can make it span the entire window.
Image((ImTextureID)texid, dim);
SetCursorScreenPos(pos);
InvisibleButton("##rwin", ImVec2(-FLT_MIN, -FLT_MIN));
#endif
_rwinid = GetItemID(); // shouldn't change after first time
if(BeginPopupContextItem()) {
popup_menu(false);
EndPopup();
}
}
PopStyleColor();
End();
// Dialogs must be handled separately
process_askrom(w, h);