|
17 | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
|
18 | 18 | ***************************************************************************/
|
19 | 19 |
|
| 20 | +#include <string_view> |
| 21 | + |
20 | 22 | #include "gui/luaimguiextra.h"
|
21 | 23 |
|
22 | 24 | #include "gui/gui.h"
|
@@ -70,6 +72,35 @@ void registerAllSymbols(PCSX::Lua L) {
|
70 | 72 | L.pop();
|
71 | 73 | }
|
72 | 74 |
|
| 75 | +bool validateColorArgs(PCSX::Lua L, std::string_view funcName) { |
| 76 | + int n = L.gettop(); |
| 77 | + if (n < 2) return L.error("%s: not enough arguments", funcName); |
| 78 | + if (n > 4) return L.error("%s: too many arguments", funcName); |
| 79 | + if (!L.isstring(1)) return L.error("%s: argument 1 must be a string", funcName); |
| 80 | + if (!L.istable(2)) return L.error("%s: argument 2 must be a table", funcName); |
| 81 | + if (n == 3 && !L.isnumber(3)) return L.error("%s: argument 3 must be a number", funcName); |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +void extractColorComponents(PCSX::Lua L, float* col, int components) { |
| 86 | + static const char* const fields[] = {"r", "g", "b", "a"}; |
| 87 | + for (int i = 0; i < components; i++) { |
| 88 | + L.getfield(fields[i], 2); |
| 89 | + col[i] = L.tonumber(-1); |
| 90 | + L.pop(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void createColorTable(PCSX::Lua L, const float* col, int components) { |
| 95 | + static const char* const fields[] = {"r", "g", "b", "a"}; |
| 96 | + L.newtable(); |
| 97 | + for (int i = 0; i < components; i++) { |
| 98 | + L.push(fields[i]); |
| 99 | + L.push(col[i]); |
| 100 | + L.settable(); |
| 101 | + } |
| 102 | +} |
| 103 | + |
73 | 104 | } // namespace
|
74 | 105 |
|
75 | 106 | void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
|
@@ -155,6 +186,62 @@ void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
|
155 | 186 | return 2;
|
156 | 187 | },
|
157 | 188 | -1);
|
| 189 | + L.declareFunc( |
| 190 | + "ColorEdit3", |
| 191 | + [](lua_State* L_) -> int { |
| 192 | + Lua L(L_); |
| 193 | + if (!validateColorArgs(L, "ColorEdit3")) return 0; |
| 194 | + std::string label = L.tostring(1); |
| 195 | + float col[3]; |
| 196 | + extractColorComponents(L, col, 3); |
| 197 | + ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0; |
| 198 | + L.push(ImGui::ColorEdit3(label.c_str(), col, flags)); |
| 199 | + createColorTable(L, col, 3); |
| 200 | + return 2; |
| 201 | + }, |
| 202 | + -1); |
| 203 | + L.declareFunc( |
| 204 | + "ColorEdit4", |
| 205 | + [](lua_State* L_) -> int { |
| 206 | + Lua L(L_); |
| 207 | + if (!validateColorArgs(L, "ColorEdit4")) return 0; |
| 208 | + std::string label = L.tostring(1); |
| 209 | + float col[4]; |
| 210 | + extractColorComponents(L, col, 4); |
| 211 | + ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0; |
| 212 | + L.push(ImGui::ColorEdit4(label.c_str(), col, flags)); |
| 213 | + createColorTable(L, col, 4); |
| 214 | + return 2; |
| 215 | + }, |
| 216 | + -1); |
| 217 | + L.declareFunc( |
| 218 | + "ColorPicker3", |
| 219 | + [](lua_State* L_) -> int { |
| 220 | + Lua L(L_); |
| 221 | + if (!validateColorArgs(L, "ColorPicker3")) return 0; |
| 222 | + std::string label = L.tostring(1); |
| 223 | + float col[3]; |
| 224 | + extractColorComponents(L, col, 3); |
| 225 | + ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0; |
| 226 | + L.push(ImGui::ColorPicker3(label.c_str(), col, flags)); |
| 227 | + createColorTable(L, col, 3); |
| 228 | + return 2; |
| 229 | + }, |
| 230 | + -1); |
| 231 | + L.declareFunc( |
| 232 | + "ColorPicker4", |
| 233 | + [](lua_State* L_) -> int { |
| 234 | + Lua L(L_); |
| 235 | + if (!validateColorArgs(L, "ColorPicker4")) return 0; |
| 236 | + std::string label = L.tostring(1); |
| 237 | + float col[4]; |
| 238 | + extractColorComponents(L, col, 4); |
| 239 | + ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0; |
| 240 | + L.push(ImGui::ColorPicker4(label.c_str(), col, flags)); |
| 241 | + createColorTable(L, col, 4); |
| 242 | + return 2; |
| 243 | + }, |
| 244 | + -1); |
158 | 245 | L.pop(2);
|
159 | 246 | assert(L.gettop() == 0);
|
160 | 247 | }
|
0 commit comments