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

[API] Include ImPlot #56

Merged
merged 49 commits into from
Jun 21, 2021
Merged

[API] Include ImPlot #56

merged 49 commits into from
Jun 21, 2021

Conversation

calvertdw
Copy link
Contributor

For #27

I might need some help. When I run ./gradlew :imgui-binding:generateLibs -Denvs=linux64 -Dlocal I don't see any generated Java. Am I missing something? I tried to copy from what @AAstroPhysiCS was doing with Imguizmo.

Do I have to write the Java classes by hand? 😅
No problem if so.

@calvertdw
Copy link
Contributor Author

Hm. I tried to add only the method that shows the demo window, unfortunately it gives an error when I enable the checkbox.

Exception in thread "main" java.lang.UnsatisfiedLinkError: imgui.extension.implot.ImPlot.nShowDemoWindow([Z)V
        at imgui.extension.implot.ImPlot.nShowDemoWindow(Native Method)
        at imgui.extension.implot.ImPlot.showDemoWindow(ImPlot.java:16)
        at ExampleImPlot.show(ExampleImPlot.java:29)
        at Extra.show(Extra.java:39)
        at Main.process(Main.java:85)
        at imgui.app.Window.run(Window.java:146)
        at imgui.app.Application.launch(Application.java:83)
        at Main.main(Main.java:97)

Any ideas? Maybe I have to implement the whole thing to get one method to work?

@SpaiR
Copy link
Owner

SpaiR commented May 14, 2021

I don't see any generated Java.

Generator creates JNI layer, so it could be defined much easier. But everything else should be created manually. See PR with ImGuizmo, classes has a code where they are calling native API.

@calvertdw
Copy link
Contributor Author

😭 I already referenced ImGuizmo. As far as I can tell, I'm doing it as closely the same as possible, however ImNodes and ImGuizmo have some differences to ImPlot. Not sure what to try.

@calvertdw
Copy link
Contributor Author

calvertdw commented May 14, 2021

The ImNodes editor works but I get the UnsatisfiedLinkException when I try to show the ImPlot demo.
image

@calvertdw
Copy link
Contributor Author

calvertdw commented May 14, 2021

It is generating the JNI files:
image

package imgui.extension.implot;

import imgui.type.ImBoolean;

public final class ImPlot {
    private ImPlot() {

    }

    /*JNI
        #include "_common.h"
        #include "implot.h"

     */

    public static void showDemoWindow(ImBoolean pOpen) {
        nShowDemoWindow(pOpen.getData());
    }

    private static native void nShowDemoWindow(boolean[] pOpen); /*
        ImPlot::ShowDemoWindow(&pOpen[0]);
    */
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class imgui_extension_implot_ImPlot */

#ifndef _Included_imgui_extension_implot_ImPlot
#define _Included_imgui_extension_implot_ImPlot
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     imgui_extension_implot_ImPlot
 * Method:    nShowDemoWindow
 * Signature: ([Z)V
 */
JNIEXPORT void JNICALL Java_imgui_extension_implot_ImPlot_nShowDemoWindow
  (JNIEnv *, jclass, jbooleanArray);

#ifdef __cplusplus
}
#endif
#endif
#include <imgui_extension_implot_ImPlot.h>

//@line:10

        #include "_common.h"
        #include "implot.h"

     JNIEXPORT void JNICALL Java_imgui_extension_implot_ImPlot_nShowDemoWindow(JNIEnv* env, jclass clazz, jbooleanArray obj_pOpen) {
	bool* pOpen = (bool*)env->GetPrimitiveArrayCritical(obj_pOpen, 0);


//@line:20

        ImPlot::ShowDemoWindow(&pOpen[0]);
    
	env->ReleasePrimitiveArrayCritical(obj_pOpen, pOpen, 0);

}

@AAstroPhysiCS
Copy link
Contributor

AAstroPhysiCS commented May 14, 2021

Either you are not correctly building the dll files or you are not using the newest dll file.
After you've built the native libraries, you should get a new dll file and you should be using that one.

The UnsatisfiedLinkError is thrown when an application attempts to load a native library like .so in Linux, .dll on Windows or .dylib in Mac and that library does not exist.

@calvertdw
Copy link
Contributor Author

Ohhh! It works now. I didn't perform the step of copying from imgui-binding/build/libsNative/linux64/libimgui-java64.so -> bin/libimgui-java64.so.

2021-05-14.11-23-54.mp4

@calvertdw calvertdw changed the title Include Implot [API] Include ImPlot May 15, 2021
@SpaiR SpaiR added 3rd party Everything out of the Dear ImGui scope enhancement New feature or request labels May 15, 2021
@SpaiR
Copy link
Owner

SpaiR commented May 15, 2021

@calvertdw Can you please mark your PR as a WIP in the title? So I will understand when it will be good to review.

@calvertdw calvertdw changed the title [API] Include ImPlot [API] [WIP] Include ImPlot May 15, 2021
@SpaiR SpaiR force-pushed the master branch 7 times, most recently from de78a7c to cf09a20 Compare May 30, 2021 19:12
@calvertdw calvertdw changed the title [API] [WIP] Include ImPlot [API] Include ImPlot Jun 18, 2021
@calvertdw
Copy link
Contributor Author

@SpaiR @perrymacmurray has helped me put together the rest of this PR. It is ready for your review. Thanks!

Copy link
Owner

@SpaiR SpaiR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please remove bin/libimgui-java64.so file from the list of modified stuff.

With minor adjustments changes look very decent and clean. Great job!

import java.net.URI;

public class ExampleImPlot {
private static final String URL = "https://github.com/epezent/implot";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the url to point to the concrete commit, instead of repo in general. As for other examples.

ImGui.checkbox("Show ImPlot Built-In Demo", showDemo);

final Integer[] xs = {0, 1, 2, 3, 4, 5};
final Integer[] ys = {0, 1, 2, 3, 4, 5};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to move those axes to the class fields.

ImGui.setNextWindowSize(500, 400, ImGuiCond.Once);
ImGui.setNextWindowPos(ImGui.getMainViewport().getPosX() + 100, ImGui.getMainViewport().getPosY() + 100, ImGuiCond.Once);
if (ImGui.begin("ImPlot Demo", showImPlotWindow))
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use java style braces.


if (ImPlot.beginPlot("Example Plot")) {

ImPlot.plotLine("Line", xs, ys);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken indentation.

@@ -32,6 +36,11 @@ protected void initImGui(final Configuration config) {
io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable); // Enable Multi-Viewport / Platform Windows
io.setConfigViewportsNoTaskBarIcon(true);

ImNodes.createContext();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, you've fixed merge conflicts imporperly. See how this method looks in master.

IMPLOT_POINT = new ImPlotPoint(0);
IMPLOT_LIMITS = new ImPlotLimits(0);
IMPLOT_STYLE = new ImPlotStyle(0);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create objects directly, without creating separate static block.

}

private static native long nMin(long ptr); /*
ImPlotPoint* p = new ImPlotPoint(((ImPlotLimits*)ptr)->Min());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like in all places with calling new you'll create a memory leak, since in C++ we need to deallocate the heap memory.

}

private static native boolean nContains(long ptr, double x, double y); /*
return ((ImPlotLimits*)ptr)->Contains(x, y);
Copy link
Owner

@SpaiR SpaiR Jun 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

((ImPlotLimits*)ptr)

Instead of this construction you can use define like here: https://github.com/SpaiR/imgui-java/blob/master/imgui-binding/src/main/java/imgui/ImFont.java#L23 It's a common way I use everywhere.

I won't go through all places, but it's a common problem of the PR.

Changed ImPlotLimits, ImPlotRange, and ImPlotPoint to extend ImGuiStructDestroyable instead of ImGuiStruct so user-created instances can be destroyed. Methods that create instances that must be destroyed manually specify this in the docs.
@perrymacmurray
Copy link
Contributor

@SpaiR I went ahead and fixed the problems you pointed out. I addressed the memory leaks by making the returned instances of the affected classes (ImPlotLimits, ImPlotRange, and ImPlotPoint) extend ImGuiStructDestroyable - this preserves allowing modifying passed data for ImPlot functions that return a Limit or Point pointer (such as for modifying a Range inside a Limit). I also added ImVec2 versions of some getters for when a user might want to simply get data and not worry about manual deallocation.

If there's another way of handling memory that you would prefer so users don't have to worry about calling destroy(), I can change how I implemented the classes, but this method was the best way I could think of.

Copy link
Owner

@SpaiR SpaiR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@SpaiR SpaiR merged commit 92fa416 into SpaiR:master Jun 21, 2021
@SpaiR SpaiR mentioned this pull request Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Everything out of the Dear ImGui scope enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants