mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
feat: Refactor color handling in GUI commands and add ReadColorField method
This commit is contained in:
@@ -31,6 +31,7 @@ Use this script as a drop-in replacement for the original `dev_commands.py`.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
@@ -51,6 +52,7 @@ CMAKE_GENERATOR = {
|
||||
}
|
||||
|
||||
DEFAULT_BUILD_DIR = GENERATOR_DEFAULT_DIR[DEFAULT_GENERATOR]
|
||||
TRACE_ENV_VAR = "DEV_COMMANDS_TRACE"
|
||||
|
||||
DEFAULT_VCVARSALL = (
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional"
|
||||
@@ -84,6 +86,11 @@ def _print_cmd(argv: Sequence[str]) -> None:
|
||||
print("\n> " + rendered)
|
||||
|
||||
|
||||
def _trace(message: str) -> None:
|
||||
if os.environ.get(TRACE_ENV_VAR) == "1":
|
||||
print(f"[trace] {message}")
|
||||
|
||||
|
||||
def _strip_leading_double_dash(args: Sequence[str] | None) -> list[str]:
|
||||
"""Drop a leading `--` that argparse keeps with REMAINDER arguments."""
|
||||
if not args:
|
||||
@@ -94,7 +101,20 @@ def _strip_leading_double_dash(args: Sequence[str] | None) -> list[str]:
|
||||
return args_list
|
||||
|
||||
|
||||
def run_argvs(argvs: Iterable[Sequence[str]], dry_run: bool) -> None:
|
||||
def _has_runtime_config_arg(args: Sequence[str] | None) -> bool:
|
||||
if not args:
|
||||
return False
|
||||
for arg in args:
|
||||
if arg in {"-j", "--json-file-in"}:
|
||||
return True
|
||||
if arg.startswith("--json-file-in="):
|
||||
return True
|
||||
if arg.startswith("-j") and len(arg) > 2:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def run_argvs(argvs: Iterable[Sequence[str]], dry_run: bool, cwd: str | None = None) -> None:
|
||||
"""
|
||||
Run a sequence of commands represented as lists of arguments. Each command
|
||||
is printed before execution. If `dry_run` is True, commands are printed
|
||||
@@ -104,7 +124,7 @@ def run_argvs(argvs: Iterable[Sequence[str]], dry_run: bool) -> None:
|
||||
_print_cmd(argv)
|
||||
if dry_run:
|
||||
continue
|
||||
subprocess.run(list(argv), check=True)
|
||||
subprocess.run(list(argv), check=True, cwd=cwd)
|
||||
|
||||
|
||||
def _as_build_dir(path_str: str | None, fallback: str) -> str:
|
||||
@@ -343,7 +363,7 @@ def _sync_assets(build_dir: str, dry_run: bool) -> None:
|
||||
# Sync files matching patterns
|
||||
for pattern in patterns:
|
||||
for src_file in src_path.glob(pattern):
|
||||
if src_file.is_file():
|
||||
if src_file.is_file() and src_file.name != "dev_commands.py":
|
||||
dst_file = dst_path / src_file.name
|
||||
print(f" {src_file} -> {dst_file}")
|
||||
if not dry_run:
|
||||
@@ -369,12 +389,15 @@ def run_demo(args: argparse.Namespace) -> None:
|
||||
_sync_assets(build_dir, args.dry_run)
|
||||
|
||||
exe_name = args.target or ("sdl3_app.exe" if IS_WINDOWS else "sdl3_app")
|
||||
binary = str(Path(build_dir) / exe_name)
|
||||
cmd: list[str] = [binary, "-j", "config/seed_runtime.json"]
|
||||
binary = str(Path(build_dir).resolve() / exe_name)
|
||||
run_args = _strip_leading_double_dash(args.args)
|
||||
cmd: list[str] = [binary]
|
||||
if run_args:
|
||||
cmd.extend(run_args)
|
||||
run_argvs([cmd], args.dry_run)
|
||||
_print_cmd(cmd)
|
||||
import os
|
||||
os.chdir(build_dir)
|
||||
os.execv(binary, cmd)
|
||||
|
||||
|
||||
def gui(args: argparse.Namespace) -> None:
|
||||
|
||||
@@ -125,8 +125,9 @@ std::vector<GuiCommand> GuiScriptService::LoadGuiCommands() {
|
||||
if (std::strcmp(typeName, "rect") == 0) {
|
||||
command.type = GuiCommand::Type::Rect;
|
||||
command.rect = ReadRect(L, commandIndex);
|
||||
command.color = ReadColor(L, commandIndex, GuiColor{0.0f, 0.0f, 0.0f, 1.0f});
|
||||
command.borderColor = ReadColor(L, commandIndex, GuiColor{0.0f, 0.0f, 0.0f, 0.0f});
|
||||
command.color = ReadColorField(L, commandIndex, "color", GuiColor{0.0f, 0.0f, 0.0f, 1.0f});
|
||||
command.borderColor = ReadColorField(L, commandIndex, "borderColor",
|
||||
GuiColor{0.0f, 0.0f, 0.0f, 0.0f});
|
||||
lua_getfield(L, commandIndex, "borderWidth");
|
||||
if (lua_isnumber(L, -1)) {
|
||||
command.borderWidth = static_cast<float>(lua_tonumber(L, -1));
|
||||
@@ -159,7 +160,7 @@ std::vector<GuiCommand> GuiScriptService::LoadGuiCommands() {
|
||||
command.hasBounds = true;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
command.color = ReadColor(L, commandIndex, GuiColor{1.0f, 1.0f, 1.0f, 1.0f});
|
||||
command.color = ReadColorField(L, commandIndex, "color", GuiColor{1.0f, 1.0f, 1.0f, 1.0f});
|
||||
} else if (std::strcmp(typeName, "clip_push") == 0) {
|
||||
command.type = GuiCommand::Type::ClipPush;
|
||||
command.rect = ReadRect(L, commandIndex);
|
||||
@@ -169,12 +170,8 @@ std::vector<GuiCommand> GuiScriptService::LoadGuiCommands() {
|
||||
command.type = GuiCommand::Type::Svg;
|
||||
ReadStringField(L, commandIndex, "path", command.svgPath);
|
||||
command.rect = ReadRect(L, commandIndex);
|
||||
command.svgTint = ReadColor(L, commandIndex, GuiColor{1.0f, 1.0f, 1.0f, 0.0f});
|
||||
lua_getfield(L, commandIndex, "tint");
|
||||
if (lua_istable(L, -1)) {
|
||||
command.svgTint = ReadColor(L, -1, command.svgTint);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
command.svgTint = ReadColorField(L, commandIndex, "tint", GuiColor{1.0f, 1.0f, 1.0f, 0.0f});
|
||||
command.svgTint = ReadColorField(L, commandIndex, "color", command.svgTint);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
lua_pop(L, 1);
|
||||
@@ -314,6 +311,29 @@ GuiColor GuiScriptService::ReadColor(lua_State* L, int index, const GuiColor& de
|
||||
return color;
|
||||
}
|
||||
|
||||
GuiColor GuiScriptService::ReadColorField(lua_State* L, int index, const char* name,
|
||||
const GuiColor& defaultColor) const {
|
||||
if (logger_) {
|
||||
logger_->Trace("GuiScriptService", "ReadColorField",
|
||||
"index=" + std::to_string(index) +
|
||||
", name=" + std::string(name ? name : ""));
|
||||
}
|
||||
if (!lua_istable(L, index) || !name) {
|
||||
return defaultColor;
|
||||
}
|
||||
int absIndex = lua_absindex(L, index);
|
||||
lua_getfield(L, absIndex, name);
|
||||
GuiColor color = defaultColor;
|
||||
if (lua_istable(L, -1)) {
|
||||
color = ReadColor(L, -1, defaultColor);
|
||||
} else if (logger_) {
|
||||
logger_->Trace("GuiScriptService", "ReadColorField",
|
||||
"Field not found or not table: " + std::string(name));
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return color;
|
||||
}
|
||||
|
||||
bool GuiScriptService::ReadStringField(lua_State* L, int index, const char* name, std::string& outString) const {
|
||||
if (logger_) {
|
||||
logger_->Trace("GuiScriptService", "ReadStringField",
|
||||
|
||||
@@ -32,6 +32,7 @@ private:
|
||||
lua_State* GetLuaState() const;
|
||||
GuiCommand::RectData ReadRect(lua_State* L, int index) const;
|
||||
GuiColor ReadColor(lua_State* L, int index, const GuiColor& defaultColor) const;
|
||||
GuiColor ReadColorField(lua_State* L, int index, const char* name, const GuiColor& defaultColor) const;
|
||||
bool ReadStringField(lua_State* L, int index, const char* name, std::string& outString) const;
|
||||
|
||||
std::shared_ptr<IScriptEngineService> engineService_;
|
||||
|
||||
Reference in New Issue
Block a user