forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog.cpp
More file actions
192 lines (161 loc) · 4.36 KB
/
Copy pathdialog.cpp
File metadata and controls
192 lines (161 loc) · 4.36 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
#include "dialog.hpp"
extern "C"
{
#include "style.h"
}
#include "pkgi.hpp"
#include <imgui.h>
#include <imgui_internal.h>
namespace
{
typedef enum
{
DialogNone,
DialogMessage,
DialogError,
DialogQuestion,
} DialogType;
DialogType dialog_type;
std::string dialog_title;
std::string dialog_text;
std::vector<Response> dialog_responses;
int dialog_allow_close;
int dialog_cancelled;
int dialog_name = 0;
}
void pkgi_dialog_init()
{
dialog_type = DialogNone;
dialog_allow_close = 1;
}
int pkgi_dialog_is_open()
{
return dialog_type != DialogNone;
}
int pkgi_dialog_is_cancelled()
{
return dialog_cancelled;
}
void pkgi_dialog_allow_close(int allow)
{
pkgi_dialog_lock();
dialog_allow_close = allow;
pkgi_dialog_unlock();
}
void pkgi_dialog_message(const char* text, int allow_close)
{
pkgi_dialog_lock();
++dialog_name;
dialog_text = text;
dialog_title = "";
dialog_allow_close = allow_close;
dialog_cancelled = 0;
dialog_type = DialogMessage;
pkgi_dialog_unlock();
}
void pkgi_dialog_error(const char* text)
{
LOGF("Showing error dialog: {}", text);
pkgi_dialog_lock();
++dialog_name;
dialog_title = "ERROR";
dialog_text = text;
dialog_allow_close = 1;
dialog_cancelled = 0;
dialog_type = DialogError;
pkgi_dialog_unlock();
}
void pkgi_dialog_question(
const std::string& text, const std::vector<Response>& responses)
{
pkgi_dialog_lock();
++dialog_name;
dialog_text = text;
dialog_title = "";
dialog_cancelled = 0;
dialog_type = DialogQuestion;
dialog_responses = responses;
pkgi_dialog_unlock();
}
void pkgi_dialog_close()
{
pkgi_dialog_lock();
dialog_type = DialogNone;
dialog_responses = {};
pkgi_dialog_unlock();
}
void pkgi_do_dialog()
{
pkgi_dialog_lock();
DialogType local_type = dialog_type;
std::string local_title = dialog_title;
std::string local_text = dialog_text;
const auto local_name = dialog_name;
int local_allow_close = dialog_allow_close;
const auto responses = dialog_responses;
std::function<void()> callback;
pkgi_dialog_unlock();
ImGui::SetNextWindowPos(
ImVec2{VITA_WIDTH / 2, VITA_HEIGHT / 2}, 0, ImVec2{.5f, .5f});
ImGui::SetNextWindowSize(ImVec2{PKGI_DIALOG_WIDTH, -1});
ImGui::Begin(
std::to_string(local_name).c_str(),
nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoSavedSettings);
ImGui::PushTextWrapPos(0.f);
if (local_type == DialogError)
ImGui::TextColored(
ImVec4{1.f, .2f, .2f, 1.f}, "%s", local_text.c_str());
else
ImGui::TextUnformatted(local_text.c_str());
ImGui::PopTextWrapPos();
if (local_type == DialogQuestion)
{
ImGui::Separator();
int i = 0;
for (auto const& response : responses)
{
if (ImGui::Button(
response.text.c_str(),
ImVec2{ImGui::GetWindowContentRegionWidth(),
ImGui::GetTextLineHeightWithSpacing()}))
{
pkgi_dialog_lock();
dialog_type = DialogNone;
dialog_responses = {};
pkgi_dialog_unlock();
callback = response.callback;
}
if (++i == 1)
{
// ImGui::SetItemDefaultFocus doesn't seem to work here...
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
if (window->Appearing)
ImGui::SetKeyboardFocusHere(-1);
ImGui::SetItemDefaultFocus();
}
}
}
else if (local_allow_close)
{
ImGui::Separator();
if (ImGui::Button(
"OK",
ImVec2{ImGui::GetWindowContentRegionWidth(),
ImGui::GetTextLineHeightWithSpacing()}))
{
pkgi_dialog_lock();
dialog_type = DialogNone;
pkgi_dialog_unlock();
}
ImGui::SetItemDefaultFocus();
}
ImGui::End();
if (callback)
callback();
}