Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c# example for ImGuiTextFilter #107

Closed
zaafar opened this issue Jan 29, 2019 · 9 comments
Closed

c# example for ImGuiTextFilter #107

zaafar opened this issue Jan 29, 2019 · 9 comments

Comments

@zaafar
Copy link
Collaborator

zaafar commented Jan 29, 2019

I was trying to use ImGuiTextFilter but couldn't find a c# example of it. I looked at the ImGui demo code example and tried to replicate that on ImGui.NET but failed. Here's the code I was using, let me know how to use this feature.

ImGuiTextFilter testing = new ImGuiTextFilter();
testing.draw()

I did find the ImGuiTextFilterPtr struct/class but not sure how to use it.

reference c++ eample

static ImGuiTextFilter filter;
filter.draw();
        const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
        for (int i = 0; i < IM_ARRAYSIZE(lines); i++)
            if (filter.PassFilter(lines[i]))
                ImGui::BulletText("%s", lines[i]);
@mellinoe
Copy link
Collaborator

The C# code generator needs to be augmented to allow constructors for native objects to be called. Without that, there's no current way to create an ImGuiTextFilter. It's not a lot of work to add this functionality to the code generator; the information is already there waiting to be used.

@sa-exe
Copy link

sa-exe commented Feb 21, 2019

I'm not sure what the generated constructor for ImGuiTextFilter would look like. Clearly a constructor that simply accepts an ImVector and an int would be useless (the struct is mutable anyway, and it's not like there's currently a way to create an ImVector).

@zaafar
Copy link
Collaborator Author

zaafar commented Feb 23, 2019

Currently, I am not an expert in code generator technique you folks are using :( so I can not modify/update that code but I ported the "AutoComplete" InputBox code from this and this thread.

@prime31
Copy link
Contributor

prime31 commented Feb 23, 2019

It turns out this not only affects text filters but also ImFontConfig which needs to be created on the native side. @mellinoe if you can point me in the right direction (brief overview of what needs to be done) I can try to carve out some time to get this going.

@mellinoe
Copy link
Collaborator

I pushed two changes to master that fix the raw PInvokes for constructor and destructor functions:

8e3caa1
4588142

Ideally, there would be static functions that invoke the native constructor, with default parameters being generated as with other overloads. Right now, you can invoke the functions added in the first commit and get back a raw pointer, which needs to be deleted later with a matching thing_destroy call.

Another note: you could already create these structures on the C# side and pass a pointer to them to the native side. Where it gets tricky is:

  • ImVector -- The native side will likely try to reallocate the buffer at some point, and things will probably not work. If you preallocate a large enough vector, it might work. This matters for ImGuiTextFilter because it contains a vector.
  • Default values -- Allocating the struct in C# will zero-init everything, which means it won't have the default values it would have in C++: for example.
  • Lifetime -- if you're passing a pointer to the C# object, you need to make sure it remains valid (e.g. not on the stack if it's long-lived).

@prime31
Copy link
Contributor

prime31 commented Feb 25, 2019

Sorry about double post. Used the wrong GitHub account.

Thanks @mellinoe! Works like a champ. For those who find this later here is an ImFontConfig example which is very similar to 'ImGuiTextFilter' and should let you know how it all works:

// create the object on the native side
var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();

// fill with data
(*nativeConfig).OversampleH = 3;
(*nativeConfig).OversampleV = 1;
(*nativeConfig).RasterizerMultiply = 1f;
(*nativeConfig).GlyphExtraSpacing = new Num.Vector2(10, 0);

// use the object
ImGui.GetIO().Fonts.AddFontFromFileTTF("/Library/Fonts/Comic Sans MS.ttf", 16, nativeConfig);

// delete the reference. ImGui copies it
ImGuiNative.ImFontConfig_destroy(nativeConfig);

Or, using the Ptr wrapper class:

// create the object on the native side and wrap it in a C# class
var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();
var config = new ImFontConfigPtr(nativeConfig);

// use the object
config.OversampleH = 2;
config.OversampleV = 1;
config.RasterizerMultiply = 1f;

ImGui.GetIO().Fonts.AddFontFromFileTTF("/Library/Fonts/Comic Sans MS.ttf", 16, nativeConfig);

// delete the reference. ImGui copies it
config.Destroy();

@prime31
Copy link
Contributor

prime31 commented Feb 25, 2019

For the sake of completeness and future visitors, here is an ImGuiTextFilter example as well:

unsafe public class CleanTest
{
    string[] _lines = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" };
    ImGuiTextFilterPtr _filter;

    public CleanTest()
    {
        var filterPtr = ImGuiNative.ImGuiTextFilter_ImGuiTextFilter(null);
        _filter = new ImGuiTextFilterPtr(filterPtr);
    }

    public void Draw()
    {
        ImGui.Begin("Here We Go");

        _filter.Draw("This is a text filter");

        foreach(var line in _lines)
        {
            if (_filter.PassFilter(line))
                ImGui.BulletText(line);
        }

        ImGui.End();
    }

    public void Destroy()
    {
        ImGuiNative.ImGuiTextFilter_destroy(_filter.NativePtr);
    }
}

@prime31
Copy link
Contributor

prime31 commented Feb 25, 2019

One last thing for @mellinoe: any chance of a NuGet bump?

@lokinmodar
Copy link

Sorry about double post. Used the wrong GitHub account.

Thanks @mellinoe! Works like a champ. For those who find this later here is an ImFontConfig example which is very similar to 'ImGuiTextFilter' and should let you know how it all works:

// create the object on the native side
var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();

// fill with data
(*nativeConfig).OversampleH = 3;
(*nativeConfig).OversampleV = 1;
(*nativeConfig).RasterizerMultiply = 1f;
(*nativeConfig).GlyphExtraSpacing = new Num.Vector2(10, 0);

// use the object
ImGui.GetIO().Fonts.AddFontFromFileTTF("/Library/Fonts/Comic Sans MS.ttf", 16, nativeConfig);

// delete the reference. ImGui copies it
ImGuiNative.ImFontConfig_destroy(nativeConfig);

Or, using the Ptr wrapper class:

// create the object on the native side and wrap it in a C# class
var nativeConfig = ImGuiNative.ImFontConfig_ImFontConfig();
var config = new ImFontConfigPtr(nativeConfig);

// use the object
config.OversampleH = 2;
config.OversampleV = 1;
config.RasterizerMultiply = 1f;

ImGui.GetIO().Fonts.AddFontFromFileTTF("/Library/Fonts/Comic Sans MS.ttf", 16, nativeConfig);

// delete the reference. ImGui copies it
config.Destroy();

This helped me with other issue i was having creating ImFontConfig. Thanks!

@zaafar zaafar closed this as completed Nov 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants