-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventRecorder.cpp
More file actions
758 lines (696 loc) · 23 KB
/
EventRecorder.cpp
File metadata and controls
758 lines (696 loc) · 23 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
#include "EventRecorder.h"
#include "common.h"
#include "MemoryManager.h"
#include "CycleCounter.h"
#include "A2VideoManager.h"
#include "MockingboardManager.h"
#include "extras/MemoryLoader.h"
#include "LogTextManager.h"
#include "imgui.h"
#include "imgui_internal.h" // for PushItemFlag
#include "extras/ImGuiFileDialog.h"
#include <string>
#include <fstream>
#include <sstream>
#include <chrono>
constexpr uint32_t MAXRECORDING_SECONDS = 30; // Max number of seconds to record
// below because "The declaration of a static data member in its class definition is not a definition"
EventRecorder* EventRecorder::s_instance;
// Mem snapshot cycles may be different in saved recordings
static size_t m_current_snapshot_cycles = RECORDER_MEM_SNAPSHOT_CYCLES;
//////////////////////////////////////////////////////////////////////////
// Basic singleton methods
//////////////////////////////////////////////////////////////////////////
void EventRecorder::Initialize()
{
ClearRecording();
slowdownMultiplier = 1;
}
EventRecorder::~EventRecorder()
{
}
//////////////////////////////////////////////////////////////////////////
// Serialization and data transfer methods
//////////////////////////////////////////////////////////////////////////
void EventRecorder::MakeRAMSnapshot(size_t cycle)
{
(void)cycle; // mark as unused
auto _memsize = _A2_MEMORY_SHADOW_END;
ByteBuffer buffer(RECORDER_TOTALMEMSIZE);
// Get the MAIN chunk
buffer.copyFrom(MemoryManager::GetInstance()->GetApple2MemPtr(), 0, _memsize);
// Get the AUX chunk
buffer.copyFrom(MemoryManager::GetInstance()->GetApple2MemAuxPtr(), 0x10000, _memsize);
v_memSnapshots.push_back(std::move(buffer));
}
void EventRecorder::ApplyRAMSnapshot(size_t snapshot_index)
{
if (snapshot_index > (v_memSnapshots.size() - 1)) {
std::cerr << "ERROR: Requested to apply nonexistent memory snapshot at index " << snapshot_index << std::endl;
return;
}
auto _memsize = _A2_MEMORY_SHADOW_END;
// Set the MAIN chunk
v_memSnapshots.at(snapshot_index).copyTo(MemoryManager::GetInstance()->GetApple2MemPtr(), 0, _memsize);
// Set the AUX chunk
v_memSnapshots.at(snapshot_index).copyTo(MemoryManager::GetInstance()->GetApple2MemAuxPtr(), 0x10000, _memsize);
}
void EventRecorder::WriteRecordingFile(std::ofstream& file)
{
// TODO: Start with a version
// Then write PAL/NTSC
// Then write VBL state for event 0
// First store the RAM snapshot interval
file.write(reinterpret_cast<const char*>(&m_current_snapshot_cycles), sizeof(m_current_snapshot_cycles));
// Next store the event vector size
auto _size = v_events.size();
file.write(reinterpret_cast<const char*>(&_size), sizeof(_size));
// Next store the RAM states
for (const auto& snapshot : v_memSnapshots) {
file.write(reinterpret_cast<const char*>(snapshot.data()), (RECORDER_TOTALMEMSIZE) / sizeof(uint8_t));
}
std::cout << "Writing " << _size << " events to file" << std::endl;
// And finally the events
for (const auto& event : v_events) {
WriteEvent(event, file);
}
}
void EventRecorder::ReadRecordingFile(std::ifstream& file)
{
StopReplay();
ClearRecording();
v_events.reserve(1000000 * MAXRECORDING_SECONDS);
auto logTextManager = LogTextManager::GetInstance();
// ==== HEADER: version, total cycles, current cycle, snapshot interval, event vector size
// read the version
uint32_t _version = 1;
file.read(reinterpret_cast<char*>(&_version), sizeof(_version));
if (_version != 1)
logTextManager->AddLog("Unknown Recording File version!", glm::vec4(1.f, 0.f, 0.f, 1.f));
// read the total cycles
uint32_t _cyclesTotal = CYCLES_TOTAL_NTSC;
file.read(reinterpret_cast<char*>(&_cyclesTotal), sizeof(_cyclesTotal));
if (bIsPAL && _cyclesTotal == CYCLES_TOTAL_NTSC)
CycleCounter::GetInstance()->SetVideoRegion(VideoRegion_e::NTSC);
else if (!bIsPAL && _cyclesTotal == CYCLES_TOTAL_PAL)
CycleCounter::GetInstance()->SetVideoRegion(VideoRegion_e::PAL);
if (!(_cyclesTotal == CYCLES_TOTAL_NTSC || _cyclesTotal == CYCLES_TOTAL_PAL))
logTextManager->AddLog("Unknown Recording Video Region, reverting to NTSC", glm::vec4(1.f, 0.f, 0.f, 1.f));
// read the current cycle
uint32_t _cycleCurrent = 0;
file.read(reinterpret_cast<char*>(&_cycleCurrent), sizeof(_cycleCurrent));
// read the ram snapshot interval
file.read(reinterpret_cast<char*>(&m_current_snapshot_cycles), sizeof(m_current_snapshot_cycles));
// read the event vector size
size_t _size;
file.read(reinterpret_cast<char*>(&_size), sizeof(_size));
// ==== END HEADER
// Then all the RAM states
if (_size > 0)
{
for (size_t i = 0; i <= (_size / m_current_snapshot_cycles); ++i) {
auto snapshot = ByteBuffer(RECORDER_TOTALMEMSIZE);
file.read(reinterpret_cast<char*>(snapshot.data()), (RECORDER_TOTALMEMSIZE) / sizeof(uint8_t));
v_memSnapshots.push_back(std::move(snapshot));
}
}
std::cout << "Reading " << _size << " events from file" << std::endl;
// And finally the events
if (_size > 0)
{
for (size_t i = 0; i < _size; ++i) {
ReadEvent(file);
}
}
bHasRecording = true;
}
void EventRecorder::ReadTextEventsFromFile(std::ifstream& file)
{
StopReplay();
ClearRecording();
MakeRAMSnapshot(0); // Just make a snapshot of what is now
v_events.reserve(1000000 * MAXRECORDING_SECONDS);
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') {
continue; // Skip empty lines or lines starting with '#'
}
std::stringstream ss(line);
std::string count_str, is_iigs_str, m2b0_str, m2sel_str, rw_str, addr_str, data_str;
std::getline(ss, count_str, ',');
std::getline(ss, is_iigs_str, ',');
std::getline(ss, m2b0_str, ',');
std::getline(ss, m2sel_str, ',');
std::getline(ss, rw_str, ',');
std::getline(ss, addr_str, ',');
std::getline(ss, data_str, ',');
auto count = std::stoul(count_str);
bool is_iigs = std::stoi(is_iigs_str);
bool m2b0 = std::stoi(m2b0_str);
bool m2sel = std::stoi(m2sel_str);
bool rw = std::stoi(rw_str);
uint16_t addr = static_cast<uint16_t>(std::stoul(addr_str, nullptr, 16));
uint8_t data = static_cast<uint8_t>(std::stoul(data_str, nullptr, 16));
SDHREvent event(is_iigs, m2b0, m2sel, rw, addr, data);
for (size_t i = 0; i < count; ++i) {
v_events.push_back(event);
}
}
std::cout << "Read " << v_events.size() << " text events from file" << std::endl;
bHasRecording = true;
}
// Reading an animation file locally
// The PaintWorks animations format is:
// 0x0000-0x7FFF: first SHR animation frame, standard SHR
// 0x8000-0x8003: length of animation data block (starting at 0x8008)
// 0x8004-0x8007: delay time, in number of VBLs
// 0x8008-EOF : frames data block
// For each frame:
// 4 bytes of frame data size (including these 4 bytes)
// Then a series 2 byte offset, 2 byte value
// If offset is zero, it's the end of the frame
// Notes:
// - PWA data is loaded just like SHR in 0x2000 in AUX mem
// - A frame size of 00000004 means that there's a single frame that spans the whole data block
//
// NOTE: The new PWA format extends the above to allow for any type of SHR as the first frame,
// including SHR, SHR4, SHR3200 or any combination of those in interlace or page flip mode
void EventRecorder::ReadPaintWorksAnimationsFile(std::ifstream& file)
{
StopReplay();
ClearRecording();
v_events.reserve(1000000 * MAXRECORDING_SECONDS);
auto logManager = LogTextManager::GetInstance();
// First parse the SHR data at the start of the file
SHRFileContent_e typeE1 = SHRFileContent_e::UNKNOWN;
SHRFileContent_e typeE0 = SHRFileContent_e::UNKNOWN;
uint32_t parsedCount = 0;
if (file)
parsedCount = ParseSHRData(file, 0, &typeE1, &typeE0);
if (parsedCount == 0) {
logManager->AddLog(std::string("Error! SHR animation unknown file size"));
return;
}
if (typeE1 == SHRFileContent_e::UNKNOWN) {
logManager->AddLog(std::string("Error! Unknown SHR type"));
return;
}
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
if (fileSize - parsedCount < 8) { // animation data is not there!
logManager->AddLog(std::string("Error! Not enough animation data"));
return;
}
std::string logText = "Loaded base SHR";
switch (typeE1)
{
case SHRFileContent_e::SHR:
// standard SHR
break;
case SHRFileContent_e::SHR4:
logText += "4";
break;
case SHRFileContent_e::SHR3200:
logText += "3200";
break;
default:
// this shouldn't happen, no data should be parsed in this case
logText += " (UNKNOWN!?)";
break;
}
switch (typeE0)
{
case SHRFileContent_e::UNKNOWN:
// Single image, not interlaced or page flipped
break;
case SHRFileContent_e::SHR:
// standard SHR
logText += " + SHR";
break;
case SHRFileContent_e::SHR4:
logText += " + SHR4";
break;
case SHRFileContent_e::SHR3200:
logText += " + SHR3200";
break;
case SHRFileContent_e::SHR_BYTES:
logText += " + SHR interlace bytes (no SCB+palettes)";
break;
default:
break;
}
logText += " and animation data";
logManager->AddLog(logText);
file.seekg(parsedCount);
// Now read the animations block length
uint32_t dbaLength = 0;
file.read(reinterpret_cast<char*>(&dbaLength), 4);
// Then the delay
uint32_t frameDelay = 0;
file.read(reinterpret_cast<char*>(&frameDelay), 4);
MakeRAMSnapshot(0); // Make a snapshot now, before doing the animations events
if (dbaLength > 4)
{
uint32_t _currFrameSize = 0; // size of the current frame, including this 4-byte value
uint32_t _currFramePtr = 0; // where are we in the current frame?
uint16_t _off = 0;
uint8_t _valHi = 0;
uint8_t _valLo = 0;
size_t _frameCycles = (size_t)(bIsPAL ? CYCLES_TOTAL_PAL : CYCLES_TOTAL_NTSC);
v_events.push_back(SDHREvent(false, false, false, false, 0xC005, 0)); // RAMWRTON
uint32_t _dataPtr = 0;
while (_dataPtr < dbaLength)
{
if (_currFramePtr >= _currFrameSize) {
file.read(reinterpret_cast<char*>(&_currFrameSize), 4);
_dataPtr += 4;
if (_currFrameSize == 4) {
// Some apps don't use the frame size and set it to 00000004
// In that case, make the whole thing a single frame
_currFrameSize = dbaLength;
}
_currFramePtr = 4; // move to right after the 4 bytes of size
}
file.read(reinterpret_cast<char*>(&_off), 2);
file.read(reinterpret_cast<char*>(&_valHi), 1);
file.read(reinterpret_cast<char*>(&_valLo), 1);
_dataPtr += 4;
if (_off == 0) // delay
{
// add the delay between the frames using a dummy read event
for (size_t _d = 0; _d < ((size_t)frameDelay * _frameCycles); ++_d) {
v_events.push_back(SDHREvent(false, false, false, true, 0, 0));
}
} else { // proper offset, will roll over if offset > 0xE0000
v_events.push_back(SDHREvent(false, false, false, false, _off + 0x2000, _valHi));
v_events.push_back(SDHREvent(false, false, false, false, _off + 0x2001, _valLo));
}
}
v_events.push_back(SDHREvent(false, false, false, false, 0xC004, 0)); // RAMWRTOFF
}
bHasRecording = true;
}
void EventRecorder::WriteEvent(const SDHREvent& event, std::ofstream& file) {
// Serialize and write each member of SDHREvent to the file
file.write(reinterpret_cast<const char*>(&event.is_iigs), sizeof(event.is_iigs));
file.write(reinterpret_cast<const char*>(&event.m2b0), sizeof(event.m2b0));
file.write(reinterpret_cast<const char*>(&event.rw), sizeof(event.rw));
file.write(reinterpret_cast<const char*>(&event.addr), sizeof(event.addr));
file.write(reinterpret_cast<const char*>(&event.data), sizeof(event.data));
}
void EventRecorder::ReadEvent(std::ifstream& file) {
auto event = SDHREvent(false, false, false, 0, 0, 0);
file.read(reinterpret_cast<char*>(&event.is_iigs), sizeof(event.is_iigs));
file.read(reinterpret_cast<char*>(&event.m2b0), sizeof(event.m2b0));
file.read(reinterpret_cast<char*>(&event.rw), sizeof(event.rw));
file.read(reinterpret_cast<char*>(&event.addr), sizeof(event.addr));
file.read(reinterpret_cast<char*>(&event.data), sizeof(event.data));
v_events.push_back(std::move(event));
}
//////////////////////////////////////////////////////////////////////////
// Replay methods
//////////////////////////////////////////////////////////////////////////
void EventRecorder::StopReplay()
{
if (m_state == EventRecorderStates_e::PLAYING)
{
bShouldStopReplay = true;
if (thread_replay.joinable())
thread_replay.join();
}
}
void EventRecorder::StartReplay()
{
if (!bHasRecording)
return;
RewindReplay();
bShouldPauseReplay = false;
bShouldStopReplay = false;
SetState(EventRecorderStates_e::PLAYING);
ApplyRAMSnapshot(0);
thread_replay = std::thread(&EventRecorder::replay_events_thread, this,
&bShouldPauseReplay, &bShouldStopReplay);
}
void EventRecorder::PauseReplay(bool pause)
{
bShouldPauseReplay = pause;
}
void EventRecorder::RewindReplay()
{
StopReplay();
currentReplayEvent = 0;
bUserMovedEventSlider = true;
}
int EventRecorder::replay_events_thread(bool* shouldPauseReplay, bool* shouldStopReplay)
{
using namespace std::chrono;
// Duration of an Apple 2 clock cycle (not stretched)
auto duration_ns = 1'000'000'000 / (bIsPAL ? _A2_CPU_FREQUENCY_PAL : _A2_CPU_FREQUENCY_NTSC);
auto targetDuration = duration_cast<high_resolution_clock::duration>(duration<double, std::nano>(slowdownMultiplier * duration_ns));
auto startTime = high_resolution_clock::now();
auto nextTime = startTime;
while (!*shouldStopReplay)
{
if (*shouldPauseReplay)
{
SetState(EventRecorderStates_e::PAUSED);
std::this_thread::sleep_for(seconds(1));
continue;
}
if (GetState() != EventRecorderStates_e::PLAYING)
{
SetState(EventRecorderStates_e::PLAYING);
startTime = high_resolution_clock::now();
nextTime = startTime;
}
// Check if the user requested to move to a different area in the recording
if (bUserMovedEventSlider)
{
bUserMovedEventSlider = false;
// Move to the requested event. In order to do this cleanly, we need:
// 1. to find the closest previous memory snapshot
// 2. run all events between the mem snapshot and the requested event
// These events can be run at max speed
auto snapshot_index = currentReplayEvent / m_current_snapshot_cycles;
ApplyRAMSnapshot(snapshot_index);
auto first_event_index = snapshot_index * m_current_snapshot_cycles;
for (auto i = first_event_index; i < currentReplayEvent; i++)
{
auto e = v_events.at(i);
process_single_event(e);
}
}
if ((v_events.size() > 0) && (currentReplayEvent < v_events.size()))
{
if (*shouldStopReplay) // In case a stop was sent while sleeping
break;
auto e = v_events.at(currentReplayEvent);
process_single_event(e);
currentReplayEvent += 1;
// wait 1 clock cycle before adding the next event, compensating for drift
nextTime += targetDuration;
while (true)
{
if (high_resolution_clock::now() >= nextTime)
break;
}
startTime = nextTime;
}
else {
currentReplayEvent = 0;
ApplyRAMSnapshot(0);
}
}
SetState(EventRecorderStates_e::STOPPED);
return 0;
}
//////////////////////////////////////////////////////////////////////////
// Recording methods
//////////////////////////////////////////////////////////////////////////
void EventRecorder::StartRecording()
{
ClearRecording();
v_events.reserve(1000000 * MAXRECORDING_SECONDS);
SetState(EventRecorderStates_e::RECORDING);
}
void EventRecorder::StopRecording()
{
bHasRecording = true;
SaveRecording();
SetState(EventRecorderStates_e::STOPPED);
}
void EventRecorder::ClearRecording()
{
v_memSnapshots.clear();
v_events.clear();
v_events.shrink_to_fit();
bHasRecording = false;
currentReplayEvent = 0;
m_current_snapshot_cycles = RECORDER_MEM_SNAPSHOT_CYCLES;
}
void EventRecorder::SaveRecording()
{
IGFD::FileDialogConfig config;
config.path = "./recordings/";
ImGui::SetNextWindowSize(ImVec2(800, 400));
ImGuiFileDialog::Instance()->OpenDialog("ChooseRecordingSave", "Save to File", ".vcr,", config);
}
void EventRecorder::LoadRecording()
{
SetState(EventRecorderStates_e::STOPPED);
IGFD::FileDialogConfig config;
config.path = "./recordings/";
ImGui::SetNextWindowSize(ImVec2(800, 400));
ImGuiFileDialog::Instance()->OpenDialog("ChooseRecordingLoad", "Load Recording File", ".vcr,.shra,#C20000, #C20002", config);
}
void EventRecorder::LoadTextEventsFromFile()
{
SetState(EventRecorderStates_e::STOPPED);
IGFD::FileDialogConfig config;
config.path = "./recordings/";
ImGui::SetNextWindowSize(ImVec2(800, 400));
ImGuiFileDialog::Instance()->OpenDialog("ChooseTextEventsFileLoad", "Load Text Events File", ".csv,", config);
}
//////////////////////////////////////////////////////////////////////////
// Public methods
//////////////////////////////////////////////////////////////////////////
void EventRecorder::SetPAL(bool isPal) {
// Don't stop playing here because this may be called from the playing thread
bIsPAL = isPal;
}
void EventRecorder::RecordEvent(SDHREvent* sdhr_event)
{
if (m_state != EventRecorderStates_e::RECORDING)
return;
if ((currentReplayEvent % RECORDER_MEM_SNAPSHOT_CYCLES) == 0)
MakeRAMSnapshot(currentReplayEvent);
v_events.push_back(*sdhr_event);
++currentReplayEvent;
if (v_events.size() == (size_t)1'000'000 * MAXRECORDING_SECONDS)
StopRecording();
}
void EventRecorder::DisplayImGuiWindow(bool* p_open)
{
if (p_open)
{
ImGui::Begin("Event Recorder", p_open);
ImGui::PushItemWidth(200);
if (m_state == EventRecorderStates_e::RECORDING)
ImGui::Text("RECORDING IN PROGRESS...");
else {
if (bHasRecording)
ImGui::Text("Recording available");
else
ImGui::Text("No recording loaded");
}
if (m_state == EventRecorderStates_e::RECORDING)
{
if (ImGui::Button("Stop Recording"))
this->StopRecording();
}
else {
if (ImGui::Button("Start Recording"))
this->StartRecording();
}
static bool bIsInReplayMode = (this->IsInReplayMode());
if (ImGui::Checkbox("Replay Mode", &bIsInReplayMode))
{
if (bIsInReplayMode)
SetState(EventRecorderStates_e::STOPPED);
else
SetState(EventRecorderStates_e::DISABLED);
}
if (bHasRecording == false)
{
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f); // Reduce button opacity
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); // Disable button (and make it unclickable)
}
bUserMovedEventSlider = ImGui::SliderInt("Event Timeline", reinterpret_cast<int*>(¤tReplayEvent), 0, (int)v_events.size());
if (bIsInReplayMode)
{
if (ImGui::InputInt("X Slowdown", &slowdownMultiplier))
{
if (slowdownMultiplier < 0)
slowdownMultiplier = 0;
// make sure targetDuration is updated
this->PauseReplay(true);
this->PauseReplay(false);
}
}
if (thread_replay.joinable())
{
if (ImGui::Button("Stop##Replay"))
this->StopReplay();
}
else {
if (ImGui::Button("Play##Replay"))
this->StartReplay();
}
ImGui::SameLine();
if (m_state == EventRecorderStates_e::PAUSED)
{
if (ImGui::Button("Unpause##Recording"))
this->PauseReplay(false);
}
else {
if (ImGui::Button("Pause##Recording"))
this->PauseReplay(true);
}
ImGui::SameLine();
if (ImGui::Button("Rewind##Recording"))
this->RewindReplay();
if (bHasRecording == false)
{
ImGui::PopItemFlag();
ImGui::PopStyleVar();
}
ImGui::Separator();
if (ImGui::Button("Load##Recording"))
{
if (ImGuiFileDialog::Instance()->IsOpened())
ImGuiFileDialog::Instance()->Close();
this->LoadRecording();
}
ImGui::SameLine();
ImGui::Dummy(ImVec2(50.0f, 0.0f));
ImGui::SameLine();
if (ImGui::Button("Load CSV##Recording"))
{
if (ImGuiFileDialog::Instance()->IsOpened())
ImGuiFileDialog::Instance()->Close();
this->LoadTextEventsFromFile();
}
ImGui::SameLine();
ImGui::Dummy(ImVec2(50.0f, 0.0f));
ImGui::SameLine();
if (bHasRecording == false)
{
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f); // Reduce button opacity
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true); // Disable button (and make it unclickable)
}
if (ImGui::Button("Save##Recording"))
{
if (ImGuiFileDialog::Instance()->IsOpened())
ImGuiFileDialog::Instance()->Close();
this->SaveRecording();
}
if (bHasRecording == false)
{
ImGui::PopItemFlag();
ImGui::PopStyleVar();
}
ImGui::PopItemWidth();
// Display the load file dialog
if (ImGuiFileDialog::Instance()->Display("ChooseRecordingLoad")) {
// Check if a file was selected
if (ImGuiFileDialog::Instance()->IsOk()) {
auto _fileExtension = ImGuiFileDialog::Instance()->GetCurrentFilter();
std::ifstream file(ImGuiFileDialog::Instance()->GetFilePathName().c_str(), std::ios::binary);
if (file.is_open()) {
try
{
if (_fileExtension == ".vcr")
ReadRecordingFile(file);
else if (_fileExtension == ".shra")
ReadPaintWorksAnimationsFile(file);
else if (_fileExtension == "#C20000")
ReadPaintWorksAnimationsFile(file);
else if (_fileExtension == "#C20002")
ReadPaintWorksAnimationsFile(file);
}
catch (std::ifstream::failure& e)
{
m_lastErrorString = e.what();
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
file.close();
}
else {
m_lastErrorString = "Error opening file";
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
}
ImGuiFileDialog::Instance()->Close();
}
// Display the load csv file dialog
if (ImGuiFileDialog::Instance()->Display("ChooseTextEventsFileLoad")) {
// Check if a file was selected
if (ImGuiFileDialog::Instance()->IsOk()) {
std::ifstream file(ImGuiFileDialog::Instance()->GetFilePathName().c_str());
if (file.is_open()) {
try
{
ReadTextEventsFromFile(file);
}
catch (std::ifstream::failure& e)
{
m_lastErrorString = e.what();
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
file.close();
}
else {
m_lastErrorString = "Error opening file";
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
}
ImGuiFileDialog::Instance()->Close();
}
// Display the save file dialog
if (ImGuiFileDialog::Instance()->Display("ChooseRecordingSave")) {
// Check if a file was selected
if (ImGuiFileDialog::Instance()->IsOk()) {
std::ofstream file(ImGuiFileDialog::Instance()->GetFilePathName().c_str(), std::ios::binary);
if (file.is_open()) {
try
{
WriteRecordingFile(file);
}
catch (std::ofstream::failure& e)
{
m_lastErrorString = e.what();
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
file.close();
}
else {
m_lastErrorString = "Error opening file";
bImGuiOpenModal = true;
ImGui::OpenPopup("Recorder Error Modal");
}
}
ImGuiFileDialog::Instance()->Close();
}
if (bImGuiOpenModal) {
if (ImGui::BeginPopupModal("Recorder Error Modal", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("%s", m_lastErrorString.c_str());
// Buttons to close the modal
if (ImGui::Button("OK", ImVec2(120, 0))) {
// Handle OK (e.g., process data, close modal)
ImGui::CloseCurrentPopup();
bImGuiOpenModal = false;
}
ImGui::EndPopup();
}
}
ImGui::End();
}
}
void EventRecorder::SetState(EventRecorderStates_e _state)
{
if (_state == m_state)
return;
m_state = _state;
/*
// do things based on state
switch (m_state) {
default:
break;
}
*/
}