如何使用向量< myObject>使用Imgui :: ListBox?

发布于 2025-02-12 06:53:12 字数 683 浏览 1 评论 0原文

我正在尝试在列表框中显示对象的向量,该对象将在每个帧中都会动态渲染。

这是我的班级,我想在列表框中显示每个属性:

class Waypoint {
public:
    int x, y, z;
    char action;
};

我现在尝试的是我真的不知道的是:

Waypoint wp1;
wp1.action = 'R';
wp1.x = 100;
wp1.y = 100;
wp1.z = 7;
Waypoint wp2;
wp2.action = 'S';
wp2.x = 100;
wp2.y = 100;
wp2.z = 6;
std::vector<Waypoint> listbox_items { wp1, wp2 };
static int listbox_item_current = 1;
ImGui::ListBox("listbox::Cavebot", &listbox_item_current, listbox_items);

当然这是不起作用的,我遇到了这个错误:

E0304   no instance of overloaded function "ImGui::ListBox" matches the argument list

我该如何在列表框中动态显示我所有的对象属性?

I'm trying to display a vector of objects in a listbox that will be rendered dynamically in every frame.

This is my class and I want to display every attribute later in the listbox:

class Waypoint {
public:
    int x, y, z;
    char action;
};

What I'm trying now as I don't really know is this:

Waypoint wp1;
wp1.action = 'R';
wp1.x = 100;
wp1.y = 100;
wp1.z = 7;
Waypoint wp2;
wp2.action = 'S';
wp2.x = 100;
wp2.y = 100;
wp2.z = 6;
std::vector<Waypoint> listbox_items { wp1, wp2 };
static int listbox_item_current = 1;
ImGui::ListBox("listbox::Cavebot", &listbox_item_current, listbox_items);

Of course this is not working, and I'm getting this error:

E0304   no instance of overloaded function "ImGui::ListBox" matches the argument list

How can I display dynamically all my objects attributes in the listbox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-02-19 06:53:13

imgui :: ListBoxchar*作为显示的文本,因此您无法使用单个char> char。您应该像这样重新设计课程:

class Waypoint {
public:
    int x, y, z;
    std::string action;
};

然后使用此功能:

bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)

示例:

bool waypoint_getter(void* data, int index, const char** output)
{
    Waypoint* waypoints = (Waypoint*)data;
    Waypoint& current_waypoint = waypoints[index];

    *output = current_waypoint.action.c_str(); // not very safe

    return true;
}

ImGui::ListBox(
    "listbox::Cavebot", 
    &listbox_item_current, 
    waypoint_getter, 
    listbox_items.data(), 
    listbox_items.size()
);

ImGui::ListBox takes a char* as a displayed text, so you could not use a single char. You should re-design your class like this:

class Waypoint {
public:
    int x, y, z;
    std::string action;
};

Then use this function:

bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)

Example:

bool waypoint_getter(void* data, int index, const char** output)
{
    Waypoint* waypoints = (Waypoint*)data;
    Waypoint& current_waypoint = waypoints[index];

    *output = current_waypoint.action.c_str(); // not very safe

    return true;
}

ImGui::ListBox(
    "listbox::Cavebot", 
    &listbox_item_current, 
    waypoint_getter, 
    listbox_items.data(), 
    listbox_items.size()
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文