-
Notifications
You must be signed in to change notification settings - Fork 36
Using libGDX
First of all, we need to use the LWJGL3 backend in our project to be able to use imgui. To do this, I recommend using gdx-setup by czyzby which allows you to easily add LWJGL3 to your new project.
Once the project is imported in your favorite IDE, go to the build.gradle file located in the root of your project. Then add the following repositories in the subprojects block:
subprojects {
...
repositories {
...
maven { url 'https://jitpack.io' }
maven { url "https://dl.bintray.com/kotlin/kotlin-dev" } // Required since imgui uses version 1.2 of Kotlin
}
}
Next, go to the build.gradle
file located in the core module and add the following dependencies:
dependencies {
...
compile "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
compile "com.github.kotlin-graphics:imgui:-SNAPSHOT"
}
Now, check if all the gradle dependencies have been recovered (Idea -> View -> Window Tool -> Gradle -> Force refresh all linked Gradle projects)
All that remains is to initialize imgui. To do this, go to your game class and add at the end of the create
method:
LwjglGL3.init(GlfwWindow((Gdx.graphics as Lwjgl3Graphics).window.windowHandle), false)
In the render
method:
super.render()
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT or GL20.GL_DEPTH_BUFFER_BIT)
LwjglGL3.newFrame()
ImGui.text("Hi")
ImGui.render()
Finally, do not forget to dispose imgui in the dispose
method:
LwjglGL3.shutdown()
That's all!
In order to use image and imageButton from imgui, we need to recover the ID handle from our texture in order to be able to pass it to imgui.
To retrieve this handle, just call the getTextureObjectHandle
method of our texture and pass the result to imgui for the userTextureId
as for example:
ImGui.image(texture.textureObjectHandle, Vec2(imageWidth, imageHeight))
To be able to use imgui inputs with libGDX, you must first create an InputProcessor or a InputAdapter in order to send the various callback to imgui.
Here is an example of the implementation :
class ImGuiInputProcessor : InputAdapter() {
override fun keyTyped(character: Char): Boolean {
LwjglGL3.charCallback(character.toInt())
return false
}
override fun scrolled(amount: Int): Boolean {
LwjglGL3.scrollCallback(Vec2d(0, -amount))
return false
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
LwjglGL3.mouseButtonCallback(button, GLFW.GLFW_PRESS, 0)
return false
}
override fun keyUp(keycode: Int): Boolean {
fun sendKeyRelease(key: Int) {
LwjglGL3.keyCallback(key, 0, GLFW.GLFW_RELEASE, 0)
}
when(keycode) {
Input.Keys.BACKSPACE -> sendKeyRelease(GLFW.GLFW_KEY_BACKSPACE)
Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT -> sendKeyRelease(GLFW.GLFW_KEY_LEFT_CONTROL)
Input.Keys.ALT_LEFT, Input.Keys.ALT_RIGHT -> sendKeyRelease(GLFW.GLFW_KEY_LEFT_ALT)
Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT -> sendKeyRelease(GLFW.GLFW_KEY_LEFT_SHIFT)
Input.Keys.BACKSLASH -> sendKeyRelease(GLFW.GLFW_KEY_BACKSLASH)
}
return false
}
override fun keyDown(keycode: Int): Boolean {
fun sendKeyPressed(key: Int) {
LwjglGL3.keyCallback(key, 0, GLFW.GLFW_PRESS, 0)
}
when(keycode) {
Input.Keys.BACKSPACE -> sendKeyPressed(GLFW.GLFW_KEY_BACKSPACE)
Input.Keys.BACKSLASH -> sendKeyPressed(GLFW.GLFW_KEY_BACKSLASH)
Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT -> sendKeyPressed(GLFW.GLFW_KEY_LEFT_CONTROL)
Input.Keys.ALT_LEFT, Input.Keys.ALT_RIGHT -> sendKeyPressed(GLFW.GLFW_KEY_LEFT_ALT)
Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT -> sendKeyPressed(GLFW.GLFW_KEY_LEFT_SHIFT)
}
return false
}
}
Then, add this input processor in libGDX :
Gdx.input.inputProcessor = ImGuiInputProcessor()
Or with a multiplexer to be able to use it with a stage :
Gdx.input.inputProcessor = InputMultiplexer(stage, ImGuiInputProcessor())