Skip to content

Commit

Permalink
GDScript: bind style colors
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jun 18, 2024
1 parent 0be2cc1 commit beafdcc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/examples/GdsDemo/demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func _ready():
var io := ImGui.GetIO()
io.ConfigFlags |= ImGui.ConfigFlags_ViewportsEnable

var style := ImGui.GetStyle()
style.Colors[ImGui.Col_PlotHistogram] = Color.REBECCA_PURPLE
style.Colors[ImGui.Col_PlotHistogramHovered] = Color.SLATE_BLUE

func _process(_delta: float) -> void:
ImGui.Begin("hello")
ImGui.Text("hello from GDScript")
Expand Down
41 changes: 39 additions & 2 deletions gdext/scripts/gds_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@
# "ImGuiWindowClass",
)

exclude_props = (
"MouseDown",
"KeysData",
"MouseClickedPos",
"MouseClickedTime",
"MouseClicked",
"MouseDoubleClicked",
"MouseClickedCount",
"MouseClickedLastCount",
"MouseReleased",
"MouseDownOwned",
"MouseDownOwnedUnlessPopupClose",
"MouseDownDuration",
"MouseDownDurationPrev",
"MouseDragMaxDistanceAbs",
"MouseDragMaxDistanceSqr",
"KeyMap",
"KeysDown",
"NavInputs",
)

array_types = {
"bool*": "bool",
"char*": "String",
Expand Down Expand Up @@ -338,6 +359,10 @@ class Property:
"bool": "BOOL",
}

array_types = {
"ImVec4": "PackedColorArray",
}

def __init__(self, j, name, struct_name):
self.struct_name = struct_name
self.name = name
Expand All @@ -347,9 +372,13 @@ def __init__(self, j, name, struct_name):
self.valid = False
if self.is_internal:
return
if self.name in exclude_props:
return
self.gdtype = type_map.get(self.orig_type, None)
if self.is_array:
self.gdtype = None
self.array_type, self.array_size = self.orig_type[:-1].split("[")
print(self.struct_name, self.name, self.is_array, self.orig_type)
self.gdtype = Property.array_types[self.array_type]
if self.gdtype == "String":
self.gdtype = None
self.valid = self.gdtype is not None
Expand All @@ -360,13 +389,16 @@ def gen_decl(self):
return rv

def gen_def(self):
# getter
rv = f"{self.gdtype} {self.struct_name}::_Get{self.name}() {{ \\\n"
fcall = f"ptr->{self.name}"
# TODO: refactor
if self.orig_type == "ImVec2":
fcall = f"ToVector2({fcall})"
elif self.orig_type in ["ImFont*"]:
fcall = f"(int64_t){fcall}"
elif self.gdtype == "PackedColorArray":
fcall = f"ToPackedColorArray({fcall}, {self.array_size})"

dv = "{}"
if self.gdtype.startswith("BitField"):
Expand All @@ -377,13 +409,18 @@ def gen_def(self):
rv += f"if (ptr) return {cast}{fcall}; else return {dv};\\\n"
rv += "} \\\n"

# setter
rv += f"void {self.struct_name}::_Set{self.name}({self.gdtype} x) {{ \\\n"
x = "x"
if self.orig_type == "ImVec2":
x = "{x.x, x.y}"
elif self.orig_type in ["ImFont*"]:
x = "(ImFont*)x"
rv += f"ptr->{self.name} = {x}; \\\n"

if self.gdtype == "PackedColorArray":
rv += f"FromPackedColorArray(x, ptr->{self.name}); \\\n"
else:
rv += f"ptr->{self.name} = {x}; \\\n"
rv += "} \\\n"
return rv

Expand Down
22 changes: 21 additions & 1 deletion gdext/src/ImGuiAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,31 @@ inline Vector2 ToVector2(ImVec2 v)
return Vector2(v.x, v.y);
}

inline Color ToColor(ImVec4 v)
inline Color ToColor(const ImVec4& v)
{
return Color(v.x, v.y, v.z, v.w);
}

inline ImVec4 FromColor(const Color& c)
{
return ImVec4(c.r, c.g, c.b, c.a);
}

inline PackedColorArray ToPackedColorArray(ImVec4* colors, int size)
{
PackedColorArray rv;
rv.resize(size);
for (int i = 0; i < size; ++i)
rv[i] = ToColor(colors[i]);
return rv;
}

inline void FromPackedColorArray(const PackedColorArray& in, ImVec4* out)
{
for (int i = 0; i < in.size(); ++i)
out[i] = FromColor(in[i]);
}

const char* sn_to_cstr(const StringName& sn)
{
static std::unordered_map<StringName, std::string> stringname_cache;
Expand Down

0 comments on commit beafdcc

Please sign in to comment.