giu icon indicating copy to clipboard operation
giu copied to clipboard

example of (*InputTextWidget).Callback usage

Open gucio321 opened this issue 3 years ago • 8 comments

I can't see any example of using imgui.InputTextCallback. is it possible to add one to examples/?

gucio321 avatar Feb 16 '22 08:02 gucio321

Sure, I'll add one later today.

AllenDang avatar Feb 17 '22 02:02 AllenDang

Oh, I cannot add it into this repo cause using imgui.InputTextCallback will add reference of imgui into the go.mod. The usage should be very straight forward, define a callback function and pass it to InputText(..).Callback() and check the document of imgui.

AllenDang avatar Feb 18 '22 03:02 AllenDang

Oh, I cannot add it into this repo cause using imgui.InputTextCallback will add reference of imgui into the go.mod

Hmm but the referencje to your imguigo already is present in gomod

The usage should be very straight forward, define a callback function and pass it to InputText(..).Callback() and check the document of imgui..

That's the problem. I cannot figurę out how to use this callback xd

gucio321 avatar Feb 18 '22 06:02 gucio321

@gucio321 Can you explain more detail about the use case you are trying to archive?

AllenDang avatar Feb 22 '22 02:02 AllenDang

@allendang generally, I was trying to solve #460 and wondering, why the following code doesn't work as I expect

package main

import (
	"fmt"

	"github.com/AllenDang/giu"
	"github.com/AllenDang/imgui-go"
)

var container string

func loop() {
	giu.SingleWindow().Layout(
		giu.InputText(&container).Callback(func(i imgui.InputTextCallbackData) int32 {
			fmt.Println("input text callback for ", i)
			return 1
		}),
	)
}

func main() {
	wnd := giu.NewMasterWindow("issue 461 [Demo]", 640, 480, 0)
	wnd.Run(loop)
}

what imgui.InputTextCallback is supposed to do exactly?

gucio321 avatar Feb 28 '22 10:02 gucio321

@gucio321 Generally speaking, InputTextCallback is used to filter the input string. Here is the reference from C++.

                // Return 0 (pass) if the character is 'i' or 'm' or 'g' or 'u' or 'i'
                static int FilterImGuiLetters(ImGuiInputTextCallbackData* data)
                {
                    if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar))
                        return 0;
                    return 1;
                }

AllenDang avatar Feb 28 '22 13:02 AllenDang

yah @AllenDang but my code from previous post isn't caled at any time (I dont recieve log at all)

gucio321 avatar Feb 28 '22 13:02 gucio321

hmm @AllenDang I've implemented your code and it doesn't work

code
package main

import (
	"strings"

	"github.com/AllenDang/giu"
	"github.com/AllenDang/imgui-go"
)

var container string

func loop() {
	giu.SingleWindow().Layout(
		giu.InputText(&container).Callback(func(i imgui.InputTextCallbackData) int32 {
			if strings.Contains("imgui", string(i.EventChar())) {
				return 0
			}
			return 1
		}),
	)
}

func main() {
	wnd := giu.NewMasterWindow("issue 461 [Demo]", 640, 480, 0)
	wnd.Run(loop)
}

what am I doing wrong?

gucio321 avatar Mar 06 '22 20:03 gucio321

Need to specify (one or more of) InputTextFlagsCallback* flags (see #660 #460 #434)

gucio321 avatar May 08 '23 13:05 gucio321