Skip to content

Commit

Permalink
Merge pull request #1843 from nicolasnoble/lua-imgui-colorpicker
Browse files Browse the repository at this point in the history
Adding color pickers / editors to imgui bindings.
  • Loading branch information
nicolasnoble authored Jan 15, 2025
2 parents 285eec3 + f1b1458 commit 119a057
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/core/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#pragma once

#include <stdarg.h>
#include <utility>

#include "core/system.h"
#include "fmt/printf.h"
Expand Down Expand Up @@ -54,9 +54,9 @@ enum class LogClass : unsigned {
template <LogClass logClass, bool enabled>
struct Logger {
template <typename... Args>
static void Log(const char *format, const Args &...args) {
static void Log(const char *format, Args &&...args) {
if (!enabled) return;
std::string s = fmt::sprintf(format, args...);
std::string s = fmt::sprintf(format, std::forward<Args>(args)...);
g_system->log(logClass, std::move(s));
}
static void Log(std::string &&s) {
Expand Down
87 changes: 87 additions & 0 deletions src/gui/luaimguiextra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include <string_view>

#include "gui/luaimguiextra.h"

#include "gui/gui.h"
Expand Down Expand Up @@ -70,6 +72,35 @@ void registerAllSymbols(PCSX::Lua L) {
L.pop();
}

bool validateColorArgs(PCSX::Lua L, std::string_view funcName) {
int n = L.gettop();
if (n < 2) return L.error("%s: not enough arguments", funcName);
if (n > 4) return L.error("%s: too many arguments", funcName);
if (!L.isstring(1)) return L.error("%s: argument 1 must be a string", funcName);
if (!L.istable(2)) return L.error("%s: argument 2 must be a table", funcName);
if (n == 3 && !L.isnumber(3)) return L.error("%s: argument 3 must be a number", funcName);
return true;
}

void extractColorComponents(PCSX::Lua L, float* col, int components) {
static const char* const fields[] = {"r", "g", "b", "a"};
for (int i = 0; i < components; i++) {
L.getfield(fields[i], 2);
col[i] = L.tonumber(-1);
L.pop();
}
}

void createColorTable(PCSX::Lua L, const float* col, int components) {
static const char* const fields[] = {"r", "g", "b", "a"};
L.newtable();
for (int i = 0; i < components; i++) {
L.push(fields[i]);
L.push(col[i]);
L.settable();
}
}

} // namespace

void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
Expand Down Expand Up @@ -155,6 +186,62 @@ void PCSX::LuaFFI::open_imguiextra(GUI* gui, Lua L) {
return 2;
},
-1);
L.declareFunc(
"ColorEdit3",
[](lua_State* L_) -> int {
Lua L(L_);
if (!validateColorArgs(L, "ColorEdit3")) return 0;
std::string label = L.tostring(1);
float col[3];
extractColorComponents(L, col, 3);
ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0;
L.push(ImGui::ColorEdit3(label.c_str(), col, flags));
createColorTable(L, col, 3);
return 2;
},
-1);
L.declareFunc(
"ColorEdit4",
[](lua_State* L_) -> int {
Lua L(L_);
if (!validateColorArgs(L, "ColorEdit4")) return 0;
std::string label = L.tostring(1);
float col[4];
extractColorComponents(L, col, 4);
ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0;
L.push(ImGui::ColorEdit4(label.c_str(), col, flags));
createColorTable(L, col, 4);
return 2;
},
-1);
L.declareFunc(
"ColorPicker3",
[](lua_State* L_) -> int {
Lua L(L_);
if (!validateColorArgs(L, "ColorPicker3")) return 0;
std::string label = L.tostring(1);
float col[3];
extractColorComponents(L, col, 3);
ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0;
L.push(ImGui::ColorPicker3(label.c_str(), col, flags));
createColorTable(L, col, 3);
return 2;
},
-1);
L.declareFunc(
"ColorPicker4",
[](lua_State* L_) -> int {
Lua L(L_);
if (!validateColorArgs(L, "ColorPicker4")) return 0;
std::string label = L.tostring(1);
float col[4];
extractColorComponents(L, col, 4);
ImGuiColorEditFlags flags = L.gettop() == 3 ? L.tonumber(3) : 0;
L.push(ImGui::ColorPicker4(label.c_str(), col, flags));
createColorTable(L, col, 4);
return 2;
},
-1);
L.pop(2);
assert(L.gettop() == 0);
}
2 changes: 1 addition & 1 deletion src/lua/luawrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static int callwrap(lua_State* raw, lua_CFunction func) {
try {
return func(raw);
} catch (std::exception& e) {
return L.error(std::string("LuaException: ") + e.what());
return L.error("LuaException: %s", e.what());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/lua/luawrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility>

#include "fmt/printf.h"
#include "json.hpp"
#include "lua.hpp"
#include "support/ssize_t.h"
Expand Down Expand Up @@ -177,6 +179,10 @@ class Lua {
int gettop() { return lua_gettop(L); }
void pushLuaContext(bool inTable = false);
int error(std::string_view msg);
template <typename... Args>
int error(const char* format, Args&&... args) {
return error(fmt::sprintf(format, std::forward<Args>(args)...));
}

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

0 comments on commit 119a057

Please sign in to comment.