Skip to content

Commit

Permalink
GDScript: fix const float arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jun 9, 2024
1 parent 3135bf7 commit 1a0d097
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
18 changes: 17 additions & 1 deletion doc/examples/GdsDemo/demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ extends Node

var myfloat := [0.0]
var mystr := [""]
var values := [2.0, 4.0, 0.0, 3.0, 1.0, 5.0]
var items := ["zero", "one", "two", "three"]
var current_item := [2]
var anim_counter := 0

func _ready():
var io := ImGui.GetIO()
io.ConfigFlags |= ImGui.ConfigFlags_ViewportsEnable

func _process(_delta):
func _process(_delta: float) -> void:
ImGui.Begin("hello")
ImGui.Text("hello from GDScript")
ImGui.DragFloat("myfloat", myfloat)
ImGui.Text(str(myfloat[0]))
ImGui.InputText("mystr", mystr, 32)
ImGui.Text(mystr[0])

ImGui.PlotHistogram("histogram", values, values.size())
ImGui.PlotLines("lines", values, values.size())
ImGui.ListBox("choices", current_item, items, items.size())
ImGui.Combo("combo", current_item, items)
ImGui.Text("choice = %s" % items[current_item[0]])
ImGui.End()

func _physics_process(_delta: float) -> void:
anim_counter += 1
if anim_counter >= 10:
anim_counter = 0
values.push_back(values.pop_front())
2 changes: 1 addition & 1 deletion gdext/scripts/gds_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __init__(self, j):
class Param:
def __init__(self, j):
self.name = j["name"]
self.is_array = j["is_array"]
self.is_array = j["is_array"] or self.name == "values" # HACK: because is_array not set
self.is_varargs = j["is_varargs"]
if not self.is_varargs:
self.orig_type = j["type"]["declaration"]
Expand Down
26 changes: 17 additions & 9 deletions gdext/src/ImGuiAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,35 @@ struct GdsArray
operator T*() { return buf.data(); }
};

// TODO: take PackedFloat32Array parameters instead
template <>
struct GdsArray<const char* const>
struct GdsArray<const float>
{
Array& arr;
std::vector<CharString> buf;
std::vector<const char*> ptrs;
std::vector<float> buf;

GdsArray(Array& a) : arr(a), buf(a.size()), ptrs(a.size())
GdsArray(Array& arr) : buf(arr.size())
{
for (int i = 0; i < arr.size(); ++i)
{
buf[i] = String(arr[i]).utf8();
ptrs[i] = buf[i].get_data();
buf[i] = arr[i];
}
}

~GdsArray()
operator const float*() { return buf.data(); }
};

template <>
struct GdsArray<const char* const>
{
std::vector<CharString> buf;
std::vector<const char*> ptrs;

GdsArray(Array& arr) : buf(arr.size()), ptrs(arr.size())
{
for (int i = 0; i < arr.size(); ++i)
{
arr[i] = buf[i].get_data();
buf[i] = String(arr[i]).utf8();
ptrs[i] = buf[i].get_data();
}
}

Expand Down

0 comments on commit 1a0d097

Please sign in to comment.