Code Monkey home page Code Monkey logo

ebiten-imgui's Introduction

Dear ImGui for Ebitengine

A renderer of cimgui-go for Ebitengine!

preview

Usage:

package main

import (
	"fmt"
	"image/color"

	"github.com/gabstv/ebiten-imgui/v3/imcolor"
	ebimgui "github.com/gabstv/ebiten-imgui/v3"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	imgui "github.com/gabstv/cimgui-go"
)

func main() {
	gg := &G{
		name:       "Hello, Dear ImGui",
		clearColor: [3]float32{0,0,0},
	}

	ebiten.SetWindowSize(800, 600)
	ebiten.SetWindowTitle(gg.name)

	ebiten.RunGame(gg)
}

type G struct {
	clearColor [3]float32
	floatVal   float32
	counter    int
	name       string
}

func (g *G) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{uint8(g.clearColor[0] * 255), uint8(g.clearColor[1] * 255), uint8(g.clearColor[2] * 255), 255})
	ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %.2f", ebiten.CurrentTPS()))
	ebimgui.Draw(screen)
}

func InputText(label string, buf *string) bool {
	return imgui.InputTextWithHint(label, "", buf, 0, nil)
}

func (g *G) Update() error {
	ebimgui.Update(1.0 / 60.0)
	ebimgui.BeginFrame()
	defer ebimgui.EndFrame()

	imgui.Text("ภาษาไทย测试조선말")                        // To display these, you'll need to register a compatible font
	imgui.Text("Hello, world!")                       // Display some text
	imgui.SliderFloat("float", &g.floatVal, 0.0, 1.0) // Edit 1 float using a slider from 0.0f to 1.0f
	imgui.ColorEdit3("clear color", &g.clearColor)     // Edit 3 floats representing a color

	//imgui.Checkbox("Demo Window", &showDemoWindow) // Edit bools storing our window open/close state
	//imgui.Checkbox("Go Demo Window", &showGoDemoWindow)
	//imgui.Checkbox("Another Window", &showAnotherWindow)

	if imgui.Button("Button") { // Buttons return true when clicked (most widgets return true when edited/activated)
		g.counter++
	}
	imgui.SameLine()
	imgui.Text(fmt.Sprintf("counter = %d", g.counter))

	if InputText("Window title", &g.name) {
		ebiten.SetWindowTitle(g.name)
	}

	xcol := imcolor.ToVec4(color.RGBA{
		R: 0xFF,
		G: 0x00,
		B: 0xFF,
		A: 0x99,
	})

	imgui.PushStyleColor_Vec4(imgui.ImGuiCol_Text, xcol)
	imgui.Text(fmt.Sprintf("fps = %f", ebiten.CurrentFPS()))
	imgui.PopStyleColor()
	return nil
}

func (g *G) Layout(outsideWidth, outsideHeight int) (int, int) {
	ebimgui.SetDisplaySize(float32(800), float32(600))
	return 800, 600
}

ebiten-imgui's People

Contributors

csmith avatar divverent avatar eliasdaler avatar gabstv avatar magnuswahlstrand 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  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

ebiten-imgui's Issues

An example should show how to change the default title "Debug"

After crawling through all the examples and source code for this project and the imgui-go project I finally stumbled upon the secret sauce to get the window to have the text "Menu" instead of "Debug". Can you please consider adding this to one of your examples?

g.mgr.BeginFrame()
imgui.Begin("Menu") // changes menu title from default "Debug"
{
    imgui.Text("Hello, world!")
}
imgui.End()
g.mgr.EndFrame()

New version of cimgui is available

Hi there, I was curious if we could get this merged with the upstream repo? The following resolves an issue that is currently causing panic when building font ranges, an issue that I have also been dealing with.

AllenDang/cimgui-go#241

thanks for your consideration!

支持非英语

go调用 非英语字符串 的其它语言全部不能正常显示 这个应该怎么解决呢

Fatal panic in imgui code when SetDisplaySize not set

If you don't set display size properly, it throws a fatal panic in the imgui code that doesn't really explain much. There needs to be some check to make sure it's initialized correctly, and an error thrown if it's not.

ClipMask how to use?

Hello, I want to paste content in the input box. How should I use this ClipMask? Do you have any ideas?

imgui-go is getting discontinued/deprecated

Hello.
See inkyblackness/imgui-go#192

This is pretty important, because imgui-go will stop getting updates. I've already ported ebiten-imgui to cimgui-go, see this branch of my fork. Most of the work is done, however cimgui-go doesn't have some stuff exposed here and there - I'll work on that.

I can make a PR, but some discussion is needed. Obviously, this is a big breaking change, so ebiten-imgui probably will need to go to v2. If this is the case, maybe it might be the time to rethink some of API decisions if we're breaking it anyway.

Any thoughts on this?

P.S. I now see that readme still says that "API might change", so maybe there's no need for v2.

Actions on a window's top bar result in a crash [solved]

I ran into this issue but ended up solving it. Posting here anyway since I had it mostly written up, and it may help someone else who's new to imgui someday.


It seems that any window I create crashes if you either:

  • click on the caret on the left
  • double click anywhere in the top bar

It doesn't happen for the "Debug" menu that seems to be built in.

I've attached sample code in demo.go.txt (.txt because github doesn't allow .go attachments).

Example triggering the issue: Peek 2024-08-13 18-47

The error is:

File: /home/runner/work/cimgui-go/cimgui-go/cimgui/imgui/imgui.cpp, Line: 9932
exit status 1

That is: https://github.com/gabstv/cimgui-go/blob/main/cimgui/imgui/imgui.cpp#L9932 which states

IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?");

I debugged it a bit and figured out the issue. I was definitely holding it wrong, but it also feels like a footgun.

My code was:

if imgui.Begin("Sample window") {
	imgui.Text("foo")
	imgui.End()
}

The problem is that it was calling imgui.Begin() but not imgui.End(). In retrospect this is obvious, but I had for some reason assumed that the boolean returned by Begin indicated whether it began successfully, and if not then I shouldn't try anything else.

The following works, with indentation to make the UI layout more readable when things start getting nested.

{
	imgui.Begin("Sample window")
	imgui.Text("foo")
	imgui.End()
}

Drawing when TPS and FPS are not in sync / have different rates

Hello
I think that some restructuring of the code/API is needed to make ebiten-imgui support different TPS/FPS rates.

In usual Dear ImGui setup, you call Begin/EndFrame and widget creation functions in "Update", not "Draw". Actual drawing should be just just done in "Draw" function.

So, I think it's the usage should be like this:

func (g *Game) Update() {
    g.mgr.BeginFrame()
    // put stuff like imgui.Button etc. here
    g.mgr.EndFrame() 
}

func (g *Game) Draw(screen *ebiten.Image) {
    g.mgr.Draw(screen) // new function - doesn't call "EndFrame" for us
}

This is much better and allows to have 60 TPS (update calls per second) and 60+ FPS (Draw calls per second) - we just need to render the old frame if Draw has been called more than once for 1 update call. Note that we can call imgui.Render once - it doesn't break anything.

Thoughts? If you agree with this, I can make a PR which implements the changes, though it'll break the API.

Show original C error messages

For crashes, it would be great if it showed the original error message rather than the original line number. So instead of:

File: /home/runner/work/cimgui-go/cimgui-go/cimgui/imgui/imgui.cpp, Line: 9932
exit status 1

pointing to https://github.com/gabstv/cimgui-go/blob/main/cimgui/imgui/imgui.cpp#L9932

It would instead give the error: Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?
or even the full line: IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?");

I'm not familiar with cgo so I'm not sure how feasible it is, but it would definitely save a lot of time when debugging.

imgui.Image() example

Hey, thanks for making this library, it's been extremely useful for me! Just wondering if you had any examples for using imgui.Image with ebiten?

Need some example

imgui.Text("ภาษาไทย测试조선말") // To display these, you'll need to register a compatible font

Can you provide an example? thx

Docking support?

Hi there,

With the recent updates to the latest cimgui-go, I was attempting to try out the newer docking features. Unfortunately it seems that the current state of cimgui-go relies on having a handle to the main window in order to set docking flags? I'm not entirely sure. I came across this example which I was unable to get working under this repository. Any ideas on how to make it work? AllenDang/cimgui-go#101

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.