Giter Site home page Giter Site logo

kotlin-imgui's Introduction

GitHub license Build Status

Kotlin/Multiplatform bindings and wrapper for Dear ImGui

This is meant to be a Kotlin-friendly wrapper (and binding) for Dear ImGui. It is experimental, as I am iterating on the API design and considering optimisations.

The current implementations are based on kgl for OpenGL and GLFW.

To build, you just need to clone and run ./gradlew build.

Usage

repositories {
    maven("https://maven.pkg.github.com/Dominaezzz/kotlin-imgui") {
        credentials {
            username = System.getenv("GITHUB_USER") // Your GitHub username.
            password = System.getenv("GITHUB_TOKEN") // A GitHub token with `read:packages`.
        }
    }
}

dependencies {
    implementation("com.kotlin-imgui:imgui:$imguiVersion")
    implementation("com.kotlin-imgui:imgui-glfw:$imguiVersion")
    implementation("com.kotlin-imgui:imgui-opengl:$imguiVersion")

    // For jvm binaries
    implementation("com.kotlin-imgui:cimgui-jvmlinuxx64:$imguiVersion")
    implementation("com.kotlin-imgui:cimgui-jvmmacosx64:$imguiVersion")
    implementation("com.kotlin-imgui:cimgui-jvmmingwx64:$imguiVersion")

    // Optional
    implementation("com.kgl:kgl-glfw:$kglVersion")
    implementation("com.kgl:kgl-glfw-static:$kglVersion")
    implementation("com.kgl:kgl-opengl:$kglVersion")
}

After kotlin-imgui is set up in your application, you can use it from anywhere in your program loop:

with(ImGui) {
    text("Hello, world ${123}")
    if (button("Save")) {
        mySaveFunction()
    }
    inputText("string", buf)
    sliderFloat("float", ::f, 0.0f, 1.0f)
}

Result:

screenshot of sample code alongside its output with ImGui

(settings: Dark style (left), Light style (right) / Font: Roboto-Medium, 16px / Rounding: 5)

Code:

// Create a window called "My First Tool", with a menu bar.
begin("My First Tool", ::myToolActive, ImGuiWindowFlags.MenuBar)
menuBar {
    menu("File") {
        if (menuItem("Open..", "Ctrl+O")) { /* Do stuff */ }
        if (menuItem("Save", "Ctrl+S"))   { /* Do stuff */ }
        if (menuItem("Close", "Ctrl+W"))  { myToolActive = false }
    }
}

// Edit a color (stored as FloatArray[4])
colorEdit4("Color", myColor)

// Plot some values
val myValues = floatArrayOf(0.2f, 0.1f, 1f, 0.5f, 0.9f, 2.2f)
plotLines("Frame Times", myValues)
 
// Display contents in a scrolling region
textColored(Vec4(1.0f , 1.0f, 0.0f, 1.0f), "Important Stuff")
beginChild("Scrolling")
for (n in 0 until 50) {
    text("${n.toString().padStart(4, '0')}: Some text")
}
endChild()
end()

Result:

screenshot of sample code alongside its output with ImGui

More information about how the api work is here.

Notes

  • Functions with variadic/va_list arguments have been skipped See #1. So formatting will have to be done in Koltin.
  • If ImGui.setNextWindowSizeConstraints is called with a callback, it must be followed by a call to ImGui.begin to release the callback.
  • Supports LinuxX64, MingwX64 and MacosX64 via JVM or Native. I will add more targets on demand.
  • The safe api (wrapper) supports almost all of the original Dear ImGui api but if you need something it does not provide you can depend on cimgui and use it directly. Remember to play nice with the wrapper when doing so.

kotlin-imgui's People

Contributors

dominaezzz avatar nlbuescher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-imgui's Issues

Separate publication for `docking` branch?

imgui, and by extension cimgui, support viewports and window docking in a separate branch. Would it be possible to publish a separate imgui-docking artifact based on that branch?

Generate DSL

Convenience DSLs like these should be generated.

inline fun ImGui.menuBar(block: () -> Unit) {
    if (beginMenuBar()) {
        try {
            block()
        } finally {
            endMenuBar()
        }
    }
}
inline fun ImGui.withId(id: Int, block: () -> Unit) {
    pushId(id)
    try {
        block()
    } finally {
        popId()
    }
}

Support variadic functions

At the moment Kotlin/Native doesn't support forwarding args to variadic C functions.

fun ImGui.text(fmt: String, varags args: Any?) {
    igText(fmt, *args)
}

result in

e:(...path...): When calling variadic C functions spread operator is supported only for *arrayOf(...)

Some problems compiling for `linuxArm32Hfp`

Ok, I've started using the sample from Dominaezzz's kotlin-imgui library for the K/N, but I get errors thrown when trying to compile it for linuxArmHfp.
error, my build.gradle.kts and my folder layout
2020-01-31_12-37|277x198
It compiles and runs fine on the host, but it doesn't get past the errors when compiling for the ARM32HFP.

I can also test new builds on this architecture. I have a Raspberry Pi 2b+ and an IMX6 to test it on.

Make sure all Flag parameters are nullable

Ran into this problem just now: function like ImGui.treeNodeEx which optionally take a Flag<ImGuiTreeNodeFlags> don't currently support sending nullable flags:

val flags = (if (selected == current) ImGuiTreeNodeFlags.Selected else null) or ImGuiTreeNodeFlags.OpenOnArrow
ImGui.treeNodeEx(current.uuid(), flags, "Some node") // compile error: required Flag<ImGuiTreeNodeFlags>, found Flag<ImGuiTreeNodeFlags>?

there are a few workarounds, but since 0 is a valid Flag value, null should be accepted anywhere

Compute size of text in advance.

I could not find a way to compute the size of a text in advance, without rendering it on screen.

As far as I know, ImGui has a CalcTextSize function that facilitates this functionality.

Visible GLFW window.

Running the example from samples shows both the imgui and glfw window. Similar to:

image

However, I believe that only the imgui Hello, World window should be visible by default.
Alternatively, the ImGui window should be removed and its content should fill GLFW window (not sure if possible).

How to arrange items ?

The example are arranging the views vertically only

How to arrange them as different layout ?

For example this image :
image

Finished with nonzero exit code.

I have created a simple Hello-World app:

import com.imgui.*

fun main() {
    with(ImGui) {
        text("Hello, world!")
   }
}

Yet, it doesn't show anything on screen and exits with the following message:

Execution failed for task ':runDebugExecutableNative'.
> Process 'command 'D:\hello-imgui\build\bin\native\debugExecutable\hello-imgui.exe'' finished with non-zero exit value -1073741819

Mutable properties for classes in Common

Currently a system exists in codegen to generate setters for a selected whitelist of properties in ImGui classes. One notable property missing is ImGuiIO.configFlags, and modification of ImGuiStyle's properties is probably another good use case, but the question comes to mind: At what point might it be better to tweak the generation to make properties mutable based on whether they're const rather than explicitly following a whitelist?

Unable to build hello-world project.

I have created a new native project with intelli and added imgui dependencies:

// build.gradle.kts

plugins {
    kotlin("multiplatform") version "1.5.0"
}

group = "me.me"
version = "1.0-SNAPSHOT"
val imguiVersion = "0.1.9"
val kglVersion = "0.3-RC1"

repositories {
    mavenCentral()
    maven("https://maven.pkg.github.com/Dominaezzz/kotlin-imgui") {
        credentials {
            username = System.getenv("GITHUB_USER") // Your GitHub username.
            password = System.getenv("GITHUB_TOKEN") // A GitHub token with `read:packages`.
        }
    }
}


kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTarget.apply {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }
    sourceSets {
        val nativeMain by getting {
            dependencies {
                implementation("com.kotlin-imgui:imgui:$imguiVersion")
                implementation("com.kotlin-imgui:imgui-glfw:$imguiVersion")
                implementation("com.kotlin-imgui:imgui-opengl:$imguiVersion")

                // For jvm binaries
                implementation("com.kotlin-imgui:cimgui-jvmlinuxx64:$imguiVersion")
                implementation("com.kotlin-imgui:cimgui-jvmmacosx64:$imguiVersion")
                implementation("com.kotlin-imgui:cimgui-jvmmingwx64:$imguiVersion")

                // Optional
                implementation("com.kgl:kgl-glfw:$kglVersion")
                implementation("com.kgl:kgl-glfw-static:$kglVersion")
                implementation("com.kgl:kgl-opengl:$kglVersion")
            }
        }
        val nativeTest by getting
    }
}

But building the project on windows ends up with the following error message:

Execution failed for task ':compileKotlinNative'.
> Could not resolve all files for configuration ':nativeCompileKlibraries'.
   > Could not resolve com.kotlin-imgui:cimgui-jvmlinuxx64:0.1.9.
     Required by:
         project :
      > No matching variant of com.kotlin-imgui:cimgui-jvmlinuxx64:0.1.9 was found. The consumer was configured to find a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64' but:
          - Variant 'jvmLinuxX64-api' capability com.kotlin-imgui:cimgui-jvmlinuxx64:0.1.9 declares an API of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'jvmLinuxX64-runtime' capability com.kotlin-imgui:cimgui-jvmlinuxx64:0.1.9 declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'metadata-api' capability com.kotlin-imgui:cimgui-jvmlinuxx64:0.1.9 declares a usage of 'kotlin-api' of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
   > Could not resolve com.kotlin-imgui:cimgui-jvmmacosx64:0.1.9.
     Required by:
         project :
      > No matching variant of com.kotlin-imgui:cimgui-jvmmacosx64:0.1.9 was found. The consumer was configured to find a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64' but:
          - Variant 'jvmMacosX64-api' capability com.kotlin-imgui:cimgui-jvmmacosx64:0.1.9 declares an API of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'jvmMacosX64-runtime' capability com.kotlin-imgui:cimgui-jvmmacosx64:0.1.9 declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'metadata-api' capability com.kotlin-imgui:cimgui-jvmmacosx64:0.1.9 declares a usage of 'kotlin-api' of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
   > Could not resolve com.kotlin-imgui:cimgui-jvmmingwx64:0.1.9.
     Required by:
         project :
      > No matching variant of com.kotlin-imgui:cimgui-jvmmingwx64:0.1.9 was found. The consumer was configured to find a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64' but:
          - Variant 'jvmMingwX64-api' capability com.kotlin-imgui:cimgui-jvmmingwx64:0.1.9 declares an API of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'jvmMingwX64-runtime' capability com.kotlin-imgui:cimgui-jvmmingwx64:0.1.9 declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'metadata-api' capability com.kotlin-imgui:cimgui-jvmmingwx64:0.1.9 declares a usage of 'kotlin-api' of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
   > Could not find com.kgl:kgl-glfw:0.3-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/com/kgl/kgl-glfw/0.3-RC1/kgl-glfw-0.3-RC1.pom
       - https://maven.pkg.github.com/Dominaezzz/kotlin-imgui/com/kgl/kgl-glfw/0.3-RC1/kgl-glfw-0.3-RC1.pom
     Required by:
         project :
   > Could not find com.kgl:kgl-glfw-static:0.3-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/com/kgl/kgl-glfw-static/0.3-RC1/kgl-glfw-static-0.3-RC1.pom
       - https://maven.pkg.github.com/Dominaezzz/kotlin-imgui/com/kgl/kgl-glfw-static/0.3-RC1/kgl-glfw-static-0.3-RC1.pom
     Required by:
         project :
   > Could not find com.kgl:kgl-opengl:0.3-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/com/kgl/kgl-opengl/0.3-RC1/kgl-opengl-0.3-RC1.pom
       - https://maven.pkg.github.com/Dominaezzz/kotlin-imgui/com/kgl/kgl-opengl/0.3-RC1/kgl-opengl-0.3-RC1.pom
     Required by:
         project :
   > Could not find com.kgl:kgl-glfw:0.3-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/com/kgl/kgl-glfw/0.3-RC1/kgl-glfw-0.3-RC1.pom
       - https://maven.pkg.github.com/Dominaezzz/kotlin-imgui/com/kgl/kgl-glfw/0.3-RC1/kgl-glfw-0.3-RC1.pom
     Required by:
         project : > com.kotlin-imgui:imgui-glfw:0.1.9 > com.kotlin-imgui:imgui-glfw-mingwx64:0.1.9
   > Could not find com.kgl:kgl-opengl:0.3-RC1.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/com/kgl/kgl-opengl/0.3-RC1/kgl-opengl-0.3-RC1.pom
       - https://maven.pkg.github.com/Dominaezzz/kotlin-imgui/com/kgl/kgl-opengl/0.3-RC1/kgl-opengl-0.3-RC1.pom
     Required by:
         project : > com.kotlin-imgui:imgui-opengl:0.1.9 > com.kotlin-imgui:imgui-opengl-mingwx64:0.1.9

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html


What am I doing wrong? Would you consider creating a self contained hello-world template in its own github repo? Thx!

Imgui version

I am habing trouble with the imgui version you set (1.77)

UnsatisfiedLinkError: GLIBC_2.29 not found

I encountered the following error when trying kotlin-imgui 0.1.7 on Ubuntu 18.04 with JDK 11.0.9.

Exception in thread "main" java.lang.UnsatisfiedLinkError: /tmp/libcimgui7590039100609315263.so: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by /tmp/libcimgui7590039100609315263.so)
        at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)
        at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2442)
        at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2498)
        at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2694)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2627)
        at java.base/java.lang.Runtime.load0(Runtime.java:768)
        at java.base/java.lang.System.load(System.java:1837)
        at com.imgui.UtilsKt.loadCImGuiNativeLibs(Utils.kt:58)
        at com.imgui.ImGui.<clinit>(ImGui.kt:308)
        at org.acejump.tracejump.HelloImGuiKt.main(HelloImGui.kt:15)
        at org.acejump.tracejump.HelloImGuiKt.main(HelloImGui.kt)

Here is my build.gradle.kts excerpt:

    val imguiVersion = "0.1.7"
    implementation("com.kotlin-imgui:imgui:$imguiVersion")
    implementation("com.kotlin-imgui:imgui-glfw:$imguiVersion")
    implementation("com.kotlin-imgui:imgui-opengl:$imguiVersion")

    // For jvm binaries
    implementation("com.kotlin-imgui:cimgui-jvmlinuxx64:$imguiVersion")
    implementation("com.kotlin-imgui:cimgui-jvmmacosx64:$imguiVersion")
    implementation("com.kotlin-imgui:cimgui-jvmmingwx64:$imguiVersion")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.