forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNUIFileDialog.cpp
More file actions
142 lines (116 loc) · 2.96 KB
/
NUIFileDialog.cpp
File metadata and controls
142 lines (116 loc) · 2.96 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
#include <StdInc.h>
#if __has_include(<ConsoleHost.h>) && __has_include(<imgui.h>) && __has_include(<grcTexture.h>)
#include <ConsoleHost.h>
#include <CefOverlay.h>
#include <grcTexture.h>
#include <DrawCommands.h>
#include <nutsnbolts.h>
#include <json.hpp>
using json = nlohmann::json;
#define STB_IMAGE_IMPLEMENTATION
#include "imfd/ImFileDialog.h"
static std::mutex g_pfdMutex;
static std::map<std::string, std::shared_ptr<ifd::FileDialog>> g_pendingFileDialogs;
static std::set<void*> g_deleteTexQueue;
static InitFunction initFunction([]()
{
nui::OnInvokeNative.Connect([](const wchar_t* type, const wchar_t* arg)
{
if (wcscmp(type, L"openFileDialog") == 0)
{
if (!nui::HasMainUI())
{
return;
}
std::unique_lock _(g_pfdMutex);
if (g_pendingFileDialogs.empty())
{
ConHost::SetCursorMode(true);
}
auto id = std::make_shared<ifd::FileDialog>();
id->CreateTexture = [](uint8_t* data, int width, int height, char fmt) -> void*
{
if (width > 2048 || height > 2048)
{
return nullptr;
}
rage::grcTextureReference reference;
memset(&reference, 0, sizeof(reference));
reference.width = width;
reference.height = height;
reference.depth = 1;
reference.stride = width * 4;
reference.format = (fmt) ? 12 : 11;
reference.pixelData = (uint8_t*)data;
auto iconPtr = new void*[2];
iconPtr[0] = rage::grcTextureFactory::getInstance()->createImage(&reference, nullptr);
iconPtr[1] = nullptr;
return iconPtr;
};
id->DeleteTexture = [](void* tex)
{
g_deleteTexQueue.insert(tex);
};
id->Open(ToNarrow(arg), "Select a file", "Image files (*.png;*.jpg;*.jpeg;*.bmp){.png,.jpg,.jpeg,.bmp}");
g_pendingFileDialogs.insert({ ToNarrow(arg), id });
}
});
OnPostFrontendRender.Connect([]()
{
auto deleteThat = [](uintptr_t, uintptr_t)
{
std::unique_lock _(g_pfdMutex);
for (auto& entry : g_deleteTexQueue)
{
if (entry)
{
void** td = (void**)entry;
delete (rage::grcTexture*)td[0];
delete[] td;
}
}
g_deleteTexQueue.clear();
};
if (IsOnRenderThread())
{
deleteThat(0, 0);
}
});
ConHost::OnShouldDrawGui.Connect([](bool* should)
{
*should = *should || !g_pendingFileDialogs.empty();
});
ConHost::OnDrawGui.Connect([]()
{
std::unique_lock _(g_pfdMutex);
if (!g_pendingFileDialogs.empty())
{
auto nowPending = g_pendingFileDialogs;
for (auto& [ entryName, id ] : nowPending)
{
if (id->IsDone(entryName))
{
// has result?
json result = nullptr;
if (id->HasResult())
{
result = id->GetResult().u8string();
}
id->Close();
nui::PostFrameMessage("mpMenu", json::object({
{ "type", "fileDialogResult" },
{ "dialogKey", entryName },
{ "result", result }
}).dump());
// remove dialog
g_pendingFileDialogs.erase(entryName);
if (g_pendingFileDialogs.empty())
{
ConHost::SetCursorMode(false);
}
}
}
}
});
});
#endif