Skip to content

Commit 119a057

Browse files
authored
Merge pull request #1843 from nicolasnoble/lua-imgui-colorpicker
Adding color pickers / editors to imgui bindings.
2 parents 285eec3 + f1b1458 commit 119a057

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

src/core/logger.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#pragma once
2121

22-
#include <stdarg.h>
22+
#include <utility>
2323

2424
#include "core/system.h"
2525
#include "fmt/printf.h"
@@ -54,9 +54,9 @@ enum class LogClass : unsigned {
5454
template <LogClass logClass, bool enabled>
5555
struct Logger {
5656
template <typename... Args>
57-
static void Log(const char *format, const Args &...args) {
57+
static void Log(const char *format, Args &&...args) {
5858
if (!enabled) return;
59-
std::string s = fmt::sprintf(format, args...);
59+
std::string s = fmt::sprintf(format, std::forward<Args>(args)...);
6060
g_system->log(logClass, std::move(s));
6161
}
6262
static void Log(std::string &&s) {

src/gui/luaimguiextra.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
1818
***************************************************************************/
1919

20+
#include <string_view>
21+
2022
#include "gui/luaimguiextra.h"
2123

2224
#include "gui/gui.h"
@@ -70,6 +72,35 @@ void registerAllSymbols(PCSX::Lua L) {
7072
L.pop();
7173
}
7274

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+
73104
} // namespace
74105

75106
void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
@@ -155,6 +186,62 @@ void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
155186
return 2;
156187
},
157188
-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);
158245
L.pop(2);
159246
assert(L.gettop() == 0);
160247
}

src/lua/luawrapper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static int callwrap(lua_State* raw, lua_CFunction func) {
2727
try {
2828
return func(raw);
2929
} catch (std::exception& e) {
30-
return L.error(std::string("LuaException: ") + e.what());
30+
return L.error("LuaException: %s", e.what());
3131
}
3232
}
3333

src/lua/luawrapper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include <optional>
2525
#include <string>
2626
#include <string_view>
27+
#include <utility>
2728

29+
#include "fmt/printf.h"
2830
#include "json.hpp"
2931
#include "lua.hpp"
3032
#include "support/ssize_t.h"
@@ -177,6 +179,10 @@ class Lua {
177179
int gettop() { return lua_gettop(L); }
178180
void pushLuaContext(bool inTable = false);
179181
int error(std::string_view msg);
182+
template <typename... Args>
183+
int error(const char* format, Args&&... args) {
184+
return error(fmt::sprintf(format, std::forward<Args>(args)...));
185+
}
180186

181187
int type(int i = -1) { return lua_type(L, i); }
182188
const char* typestring(int i = -1) { return lua_typename(L, lua_type(L, i)); }

0 commit comments

Comments
 (0)