-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFEBasicApplication.cpp
More file actions
804 lines (667 loc) · 18.9 KB
/
FEBasicApplication.cpp
File metadata and controls
804 lines (667 loc) · 18.9 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
#include "FEBasicApplication.h"
using namespace FocalEngine;
#ifdef FEBASICAPPLICATION_SHARED
extern "C" __declspec(dllexport) void* GetBasicApplication()
{
return FEBasicApplication::GetInstancePointer();
}
#endif
FEBasicApplication::FEBasicApplication()
{
glfwInit();
glfwSetMonitorCallback(MonitorCallback);
IMGUI_CHECKVERSION();
// Set the console control handler to intercept the close event
if (!SetConsoleCtrlHandler(APPLICATION.ConsoleHandler, TRUE))
{
LOG.Add("Failed to set console control handler", "Console");
}
}
FEBasicApplication::~FEBasicApplication()
{
for (size_t i = 0; i < Windows.size(); i++)
{
Windows[i]->InvokeTerminateCallback();
}
OnTerminate();
}
FE_DEFINE_VERSION_INFO(FE_BASIC_APPLICATION_)
std::string FEBasicApplication::GetVersion()
{
return GetFE_BASIC_APPLICATION_VersionInfo().GetVersion();
}
int FEBasicApplication::GetBuildNumber()
{
return GetFE_BASIC_APPLICATION_VersionInfo().BuildNumber;
}
std::string FEBasicApplication::GetBuildTimestamp()
{
return GetFE_BASIC_APPLICATION_VersionInfo().BuildTimestamp;
}
std::string FEBasicApplication::GetBuildInfo()
{
return GetFE_BASIC_APPLICATION_VersionInfo().GetBuildInfo();
}
std::string FEBasicApplication::GetFullVersion()
{
return "FEBasicApplication " + GetFE_BASIC_APPLICATION_VersionInfo().GetFullVersionString();
}
void FEBasicApplication::SetWindowCallbacks(FEWindow* Window)
{
if (Window == nullptr)
return;
glfwSetWindowCloseCallback(Window->GetGlfwWindow(), WindowCloseCallback);
glfwSetWindowFocusCallback(Window->GetGlfwWindow(), WindowFocusCallback);
glfwSetWindowSizeCallback(Window->GetGlfwWindow(), WindowResizeCallback);
glfwSetMouseButtonCallback(Window->GetGlfwWindow(), MouseButtonCallback);
glfwSetCursorEnterCallback(Window->GetGlfwWindow(), MouseEnterCallback);
glfwSetCursorPosCallback(Window->GetGlfwWindow(), MouseMoveCallback);
glfwSetCharCallback(Window->GetGlfwWindow(), CharCallback);
glfwSetKeyCallback(Window->GetGlfwWindow(), KeyButtonCallback);
glfwSetDropCallback(Window->GetGlfwWindow(), DropCallback);
glfwSetScrollCallback(Window->GetGlfwWindow(), ScrollCallback);
}
void FEBasicApplication::InitializeWindow(FEWindow* Window)
{
if (Window == nullptr)
return;
glfwMakeContextCurrent(Window->GetGlfwWindow());
// It should be called for each window.
glewInit();
SetWindowCallbacks(Window);
Window->InitializeImGui();
}
FEWindow* FEBasicApplication::AddWindow(const int Width, const int Height, std::string WindowTitle)
{
FEWindow* NewWindow = new FEWindow(Width, Height, WindowTitle);
if (NewWindow->GetGlfwWindow() == nullptr)
return nullptr;
Windows.push_back(NewWindow);
InitializeWindow(NewWindow);
return NewWindow;
}
FEWindow* FEBasicApplication::AddFullScreenWindow(size_t MonitorIndex)
{
std::vector<MonitorInfo> Monitors = APPLICATION.GetMonitors();
if (MonitorIndex >= Monitors.size())
return nullptr;
return AddFullScreenWindow(&Monitors[MonitorIndex]);
}
FEWindow* FEBasicApplication::AddFullScreenWindow(MonitorInfo* Monitor)
{
FEWindow* NewWindow = new FEWindow(Monitor);
if (NewWindow->GetGlfwWindow() == nullptr)
return nullptr;
Windows.push_back(NewWindow);
InitializeWindow(NewWindow);
return NewWindow;
}
FEWindow* FEBasicApplication::GetWindow(std::string WindowID)
{
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i]->GetID() == WindowID)
return Windows[i];
}
return nullptr;
}
FEWindow* FEBasicApplication::GetWindow(size_t WindowIndex)
{
if (WindowIndex < 0 || WindowIndex >= Windows.size())
return nullptr;
return Windows[WindowIndex];
}
FEWindow* FEBasicApplication::GetWindow(int WindowIndex)
{
return GetWindow(size_t(WindowIndex));
}
FEWindow* FEBasicApplication::GetWindow(GLFWwindow* GLFWwindow)
{
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i]->GetGlfwWindow() == GLFWwindow)
return Windows[i];
}
return nullptr;
}
FEWindow* FEBasicApplication::GetMainWindow()
{
if (Windows.empty())
return nullptr;
// For now, I will return the first window
return Windows[0];
}
void FEBasicApplication::BeginFrame()
{
if (APPLICATION.bHasToTerminate)
return;
THREAD_POOL.Update();
for (size_t i = 0; i < VirtualUIs.size(); i++)
{
VirtualUIs[i]->BeginFrame();
VirtualUIs[i]->Render();
VirtualUIs[i]->EndFrame();
}
}
void FEBasicApplication::EndFrame() const
{
glfwPollEvents();
if (APPLICATION.bHasToTerminate)
{
APPLICATION.OnTerminate();
return;
}
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i]->bShouldTerminate)
{
APPLICATION.CloseWindow(i);
return;
}
}
}
void FEBasicApplication::RenderWindows()
{
for (size_t i = 0; i < Windows.size(); i++)
{
Windows[i]->BeginFrame();
Windows[i]->Render();
Windows[i]->EndFrame();
}
}
bool FEBasicApplication::IsNotTerminated() const
{
if (bShouldClose)
return false;
if ((Windows.empty() && (ConsoleWindow == nullptr)) || bIsReadyToTerminate)
return false;
if (Windows.size() == 1 && (ConsoleWindow == nullptr) && APPLICATION.Windows[0]->bShouldClose)
return false;
return true;
}
void FEBasicApplication::MonitorCallback(GLFWmonitor* Monitor, int Event)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
APPLICATION.Windows[i]->InvokeMonitorCallback(Monitor, Event);
}
}
void FEBasicApplication::WindowCloseCallback(GLFWwindow* Window)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
{
APPLICATION.CloseWindow(i);
return;
}
}
}
void FEBasicApplication::WindowFocusCallback(GLFWwindow* Window, int Focused)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeOnFocusCallback(Focused);
}
}
void FEBasicApplication::WindowResizeCallback(GLFWwindow* Window, int Width, int Height)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeResizeCallback(Width, Height);
}
}
void FEBasicApplication::MouseEnterCallback(GLFWwindow* Window, int Entered)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeMouseEnterCallback(Entered);
}
}
void FEBasicApplication::MouseButtonCallback(GLFWwindow* Window, const int Button, const int Action, const int Mods)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeMouseButtonCallback(Button, Action, Mods);
}
}
void FEBasicApplication::MouseMoveCallback(GLFWwindow* Window, const double Xpos, const double Ypos)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeMouseMoveCallback(Xpos, Ypos);
}
}
void FEBasicApplication::CharCallback(GLFWwindow* Window, unsigned int Codepoint)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeCharCallback(Codepoint);
}
}
void FEBasicApplication::KeyButtonCallback(GLFWwindow* Window, const int Key, const int Scancode, const int Action, const int Mods)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeKeyCallback(Key, Scancode, Action, Mods);
}
}
void FEBasicApplication::DropCallback(GLFWwindow* Window, const int Count, const char** Paths)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeDropCallback(Count, Paths);
}
}
void FEBasicApplication::ScrollCallback(GLFWwindow* Window, const double Xoffset, const double Yoffset)
{
for (size_t i = 0; i < APPLICATION.Windows.size(); i++)
{
if (APPLICATION.Windows[i]->GetGlfwWindow() == Window)
APPLICATION.Windows[i]->InvokeScrollCallback(Xoffset, Yoffset);
}
}
std::string FEBasicApplication::GetUniqueHexID()
{
return UNIQUE_ID.GetUniqueHexID();
}
bool FEBasicApplication::SetClipboardText(const std::string Text)
{
if (OpenClipboard(nullptr))
{
EmptyClipboard();
const HGLOBAL MemoryHandle = GlobalAlloc(GMEM_MOVEABLE, Text.size() + 1);
if (MemoryHandle == nullptr)
{
LOG.Add("Failed to allocate memory for clipboard", "FE_BASIC_APPLICATION", FE_LOG_ERROR);
CloseClipboard();
return false;
}
memcpy(GlobalLock(MemoryHandle), Text.c_str(), Text.size() + 1);
GlobalUnlock(MemoryHandle);
SetClipboardData(CF_TEXT, MemoryHandle);
CloseClipboard();
return true;
}
return false;
}
std::string FEBasicApplication::GetClipboardText()
{
std::string Result;
if (OpenClipboard(nullptr))
{
HANDLE ClipboardData = nullptr;
ClipboardData = GetClipboardData(CF_TEXT);
if (ClipboardData != nullptr)
{
const char* ClipboardText = static_cast<char*>(GlobalLock(ClipboardData));
if (ClipboardText != nullptr)
Result = ClipboardText;
}
CloseClipboard();
}
return Result;
}
BOOL WINAPI FEBasicApplication::ConsoleHandler(DWORD dwType)
{
switch (dwType)
{
case CTRL_CLOSE_EVENT:
// This will be a call from different thread, so I can't call the OnTerminate() directly.
// Instead, I will use flag to indicate the termination
APPLICATION.bHasToTerminate = true;
// Here we will try to wait for the main thread to terminate
while (!APPLICATION.bIsReadyToTerminate)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
};
return TRUE;
default:
return FALSE;
}
}
bool FEBasicApplication::HasConsoleWindow() const
{
return !(APPLICATION.ConsoleWindow == nullptr);
}
FEConsoleWindow* FEBasicApplication::CreateConsoleWindow(std::function<void(void* UserData)> MainFunc, void* UserData)
{
if (MainFunc == nullptr)
return nullptr;
ConsoleWindow = new FEConsoleWindow(MainFunc, UserData);
return ConsoleWindow;
}
void FEBasicApplication::AddOnTerminateCallback(std::function<void()> UserOnTerminateCallback)
{
UserOnTerminateCallbackFunc.push_back(UserOnTerminateCallback);
}
void FEBasicApplication::OnTerminate()
{
// Call the user callbacks
for (auto& Func : UserOnTerminateCallbackFunc)
Func();
// If the console is active, then terminate it
if (ConsoleWindow != nullptr)
{
// Post a message to the console window to close it
PostMessage(ConsoleWindow->GetHandle(), WM_CLOSE, 0, 0);
}
for (size_t i = 0; i < Windows.size(); i++)
{
Windows[i]->InvokeTerminateCallback();
delete Windows[i];
if (i + 1 < Windows.size())
SwitchToImGuiContextOfWindow(i + 1);
}
Windows.clear();
glfwTerminate();
bIsReadyToTerminate = true;
}
void FEBasicApplication::AddOnCloseCallback(std::function<void()> UserOnCloseCallback)
{
UserOnCloseCallbackFuncs.push_back(UserOnCloseCallback);
}
void FEBasicApplication::Close()
{
bShouldClose = true;
TryToClose();
}
void FEBasicApplication::TryToClose()
{
// Call the user callbacks
// In these callbacks, the user can set the flag to false in order to prevent the application from closing
for (auto& Func : UserOnCloseCallbackFuncs)
Func();
}
void FEBasicApplication::CancelClose()
{
bShouldClose = false;
}
void FEBasicApplication::CloseWindow(std::string WindowID)
{
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i]->GetID() == WindowID)
CloseWindow(i);
}
}
void FEBasicApplication::CloseWindow(FEWindow* WindowToClose)
{
if (WindowToClose == nullptr)
return;
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i] == WindowToClose)
{
CloseWindow(i);
break;
}
}
}
// It could happen that ImGui_ImplGlfw_WndProc would be called in glfwPollEvents() for example;
// And if old window is destroyed, then it will cause crash.
// So, I will set ImGui context to the first window.
// It could be not the best solution, but it is the easiest one for now.
void FEBasicApplication::SwitchToImGuiContextOfWindow(size_t WindowIndex)
{
if (!Windows.empty())
{
ImGui::SetCurrentContext(Windows[WindowIndex]->GetImGuiContext());
}
}
void FEBasicApplication::CloseWindow(size_t WindowIndex)
{
if (WindowIndex < 0 || WindowIndex >= Windows.size())
return;
// Call the user callbacks
// In these callbacks, the user can cancel the close operation
Windows[WindowIndex]->InvokeCloseCallback();
// If the user callback did not cancel the close operation, then close and destroy the window
if (Windows[WindowIndex]->bShouldClose)
{
Windows[WindowIndex]->InvokeTerminateCallback();
delete Windows[WindowIndex];
Windows.erase(Windows.begin() + WindowIndex);
SwitchToImGuiContextOfWindow();
}
// If there are no windows and console is hidden or not active, then terminate the application
if (!HaveAnyVisibleWindow())
{
OnTerminate();
}
}
void FEBasicApplication::TerminateWindow(FEWindow* WindowToTerminate)
{
if (WindowToTerminate == nullptr)
return;
WindowToTerminate->InvokeTerminateCallback();
size_t WindowIndex = 0;
for (size_t i = 0; i < Windows.size(); i++)
{
if (Windows[i] == WindowToTerminate)
{
WindowIndex = i;
break;
}
}
delete WindowToTerminate;
Windows.erase(Windows.begin() + WindowIndex);
SwitchToImGuiContextOfWindow();
// If there are no windows and console is hidden or not active, then terminate the application
if (!HaveAnyVisibleWindow())
{
OnTerminate();
}
}
bool FEBasicApplication::HaveAnyVisibleWindow() const
{
if (Windows.empty() && (ConsoleWindow == nullptr || ConsoleWindow != nullptr && ConsoleWindow->IsHidden()))
return false;
return true;
}
bool FEBasicApplication::HaveAnyWindow() const
{
if (Windows.empty() && ConsoleWindow == nullptr)
return false;
return true;
}
std::vector<MonitorInfo> FEBasicApplication::GetMonitors()
{
std::vector<MonitorInfo> Result;
int MonitorCount;
GLFWmonitor** Monitors = glfwGetMonitors(&MonitorCount);
if (Monitors == nullptr || MonitorCount == 0)
return Result;
for (int i = 0; i < MonitorCount; i++)
{
if (Monitors[i] != nullptr)
{
MonitorInfo Info;
Info.Monitor = Monitors[i];
Info.VideoMode = glfwGetVideoMode(Monitors[i]);
Info.Name = glfwGetMonitorName(Monitors[i]);
glfwGetMonitorPos(Monitors[i], &Info.VirtualX, &Info.VirtualY);
Result.push_back(Info);
}
}
return Result;
}
size_t FEBasicApplication::MonitorInfoToMonitorIndex(MonitorInfo* Monitor)
{
std::vector<MonitorInfo> Monitors = GetMonitors();
for (size_t i = 0; i < Monitors.size(); i++)
{
if (Monitors[i].Monitor == Monitor->Monitor)
return i;
}
return 0;
}
std::vector<CommandLineAction> FEBasicApplication::ParseCommandLine(std::string CommandLine, const std::string ActionPrefix, const std::string SettingEqualizer)
{
auto Split = [](const std::string& S, char Delimiter) {
std::vector<std::string> Tokens;
std::string Token;
std::istringstream TokenStream(S);
bool InsideQuotes = false;
char CurrentChar;
while (TokenStream.get(CurrentChar))
{
if (CurrentChar == '\"')
{
InsideQuotes = !InsideQuotes; // Toggle the state
}
else if (CurrentChar == Delimiter && !InsideQuotes)
{
if (!Token.empty())
{
Tokens.push_back(Token);
Token.clear();
}
}
else
{
Token += CurrentChar;
}
}
if (!Token.empty())
Tokens.push_back(Token); // Add the last token
return Tokens;
};
// Remove tabs
CommandLine.erase(std::remove(CommandLine.begin(), CommandLine.end(), '\t'), CommandLine.end());
// Replace new lines with spaces
std::replace(CommandLine.begin(), CommandLine.end(), '\n', ' ');
std::vector<std::string> Tokens = Split(CommandLine, ' ');
std::vector<CommandLineAction> Actions;
CommandLineAction* CurrentAction = nullptr;
for (const auto& Token : Tokens)
{
if (Token.substr(0, ActionPrefix.length()) == ActionPrefix)
{
std::string ActionName = Token.substr(ActionPrefix.length()); // Remove ActionPrefix
Actions.push_back(CommandLineAction{ ActionName, {} });
CurrentAction = &Actions.back(); // Point to the newly added action
}
else if (CurrentAction && Token.find(SettingEqualizer) != std::string::npos)
{
auto DelimPos = Token.find(SettingEqualizer);
std::string Key = Token.substr(0, DelimPos);
std::string Value = Token.substr(DelimPos + ActionPrefix.length());
CurrentAction->Settings[Key] = Value;
}
}
return Actions;
}
FEConsoleWindow* FEBasicApplication::GetConsoleWindow()
{
return ConsoleWindow;
}
FEVirtualUI* FEBasicApplication::AddVirtualUI(GLuint FrameBuffer, int Width, int Height, std::string Name)
{
if (Width <= 0 || Height <= 0)
return nullptr;
if (FrameBuffer == GLuint(-1))
return nullptr;
FEVirtualUI* NewVirtualUI = new FEVirtualUI(Width, Height, Name);
NewVirtualUI->Initialize(FrameBuffer, Width, Height);
NewVirtualUI->SetName(Name);
APPLICATION.VirtualUIs.push_back(NewVirtualUI);
return NewVirtualUI;
}
void FEBasicApplication::RemoveVirtualUI(FEVirtualUI* VirtualUI)
{
if (VirtualUI == nullptr)
return;
for (size_t i = 0; i < APPLICATION.VirtualUIs.size(); i++)
{
if (APPLICATION.VirtualUIs[i] == VirtualUI)
{
delete VirtualUI;
APPLICATION.VirtualUIs.erase(APPLICATION.VirtualUIs.begin() + i);
break;
}
}
}
std::string FEBasicApplication::TruncateText(const std::string& Text, float MaxWidth, EllipsisPosition Position, const std::string& Ellipsis)
{
if (Text.empty())
return Text;
float TextWidth = ImGui::CalcTextSize(Text.c_str()).x;
if (TextWidth <= MaxWidth)
return Text;
float EllipsisWidth = ImGui::CalcTextSize(Ellipsis.c_str()).x;
if (MaxWidth <= EllipsisWidth)
return Ellipsis;
float AvailableWidth = MaxWidth - EllipsisWidth;
switch (Position)
{
case EllipsisPosition::End:
{
size_t Low = 0;
size_t High = Text.size();
while (Low < High)
{
size_t Mid = (Low + High + 1) / 2;
float Width = ImGui::CalcTextSize(Text.c_str(), Text.c_str() + Mid).x;
if (Width <= AvailableWidth)
Low = Mid;
else
High = Mid - 1;
}
return Text.substr(0, Low) + Ellipsis;
}
case EllipsisPosition::Start:
{
size_t Low = 0;
size_t High = Text.size();
while (Low < High)
{
size_t Mid = (Low + High) / 2;
float Width = ImGui::CalcTextSize(Text.c_str() + Mid).x;
if (Width <= AvailableWidth)
High = Mid;
else
Low = Mid + 1;
}
return Ellipsis + Text.substr(Low);
}
case EllipsisPosition::Middle:
{
float HalfAvailable = AvailableWidth / 2.0f;
// Find how much fits from the start
size_t StartLow = 0;
size_t StartHigh = Text.size();
while (StartLow < StartHigh)
{
size_t Mid = (StartLow + StartHigh + 1) / 2;
float Width = ImGui::CalcTextSize(Text.c_str(), Text.c_str() + Mid).x;
if (Width <= HalfAvailable)
StartLow = Mid;
else
StartHigh = Mid - 1;
}
// Find how much fits from the end
size_t EndLow = 0;
size_t EndHigh = Text.size();
while (EndLow < EndHigh)
{
size_t Mid = (EndLow + EndHigh) / 2;
float Width = ImGui::CalcTextSize(Text.c_str() + Mid).x;
if (Width <= HalfAvailable)
EndHigh = Mid;
else
EndLow = Mid + 1;
}
return Text.substr(0, StartLow) + Ellipsis + Text.substr(EndLow);
}
}
return Text;
}