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

[Feature Request] Command-line demo #660

Closed
enty8080 opened this issue May 6, 2023 · 8 comments
Closed

[Feature Request] Command-line demo #660

enty8080 opened this issue May 6, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@enty8080
Copy link

enty8080 commented May 6, 2023

Related problem

No response

Your request

Hey.

That's me again. I am just wondering if it is possible to write a command line using giu. It should wait for user's input in loop and do something when user hits enter.

The main problem is that giu.InputText() appears with a border around the input, but I want a command-line to be a just a prompt, not an input box. Like in a regular terminal emulator or even cmd.exe.

Please, if you have any ideas on how to do this, suggest them.

Thanks in advance.

Alternative solution

No response

Additional context

No response

@enty8080 enty8080 added the enhancement New feature or request label May 6, 2023
@gucio321
Copy link
Collaborator

gucio321 commented May 7, 2023

Generally i can't understand what is the problem...
Is you want to modify border (size, rounding etc) take a look on style setter

@enty8080
Copy link
Author

enty8080 commented May 7, 2023

@gucio321 Thanks. I am not sure this will work, but I'll try. Moreover, I can't understand why this code from here won't run. - https://github.com/AllenDang/giu/blob/master/examples/issue-501/main.go

I received these errors:

# command-line-arguments
./main.go:27:64: undefined: giu.Action
./main.go:28:19: undefined: giu.Press
./main.go:35:6: wnd.SetAdditionalInputHandlerCallback undefined (type *giu.MasterWindow has no field or method SetAdditionalInputHandlerCallback)

First two giu.Action and giu.Press seems to be defined in Keycode.go in this repository.

Can you please suggest me any solution?

@gucio321
Copy link
Collaborator

gucio321 commented May 7, 2023

@enty8080 I can't reproduce your error 😢 On master (git rev-parse HEAD returns da051513855e9e2628cc62fbb19c7ac2b1eec2f5) it compiles successfully. Please make sure if your code is up-to-date.

In case of your issue:
If I understend correctly you'd want to achive something like this?

image

My code

package main

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

var c string

func loop() {
	imgui.PushStyleVarVec2(imgui.StyleVarWindowPadding, imgui.Vec2{0, 0})
	imgui.PushStyleVarFloat(imgui.StyleVarFrameRounding, 0)
	defer imgui.PopStyleVarV(2)
	giu.SingleWindow().Layout(
		giu.InputTextMultiline(&c).Size(-1, -1),
	)
}

func main() {
	wnd := giu.NewMasterWindow("Issue 660 [demo]", 640, 80, 0)
	wnd.Run(loop)
}

@enty8080
Copy link
Author

enty8080 commented May 7, 2023

@gucio321 Yeah, and I want to fmt.Println data entered by user after he hits enter button.

@gucio321
Copy link
Collaborator

gucio321 commented May 7, 2023

@enty8080 so e.g. like this

package main

import (
	"fmt"

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

var c string

func loop() {
	imgui.PushStyleVarVec2(imgui.StyleVarWindowPadding, imgui.Vec2{0, 0})
	imgui.PushStyleVarFloat(imgui.StyleVarFrameRounding, 0)
	defer imgui.PopStyleVarV(2)
	giu.SingleWindow().RegisterKeyboardShortcuts(giu.WindowShortcut{
		Key: giu.KeyEnter,
		Callback: func() {
			fmt.Println(c)
			c = ""
		},
	}).Layout(
		giu.InputTextMultiline(&c).Size(-1, -1),
	)
}

func main() {
	wnd := giu.NewMasterWindow("Issue 660 [demo]", 640, 80, 0)
	wnd.Run(loop)
}

@enty8080
Copy link
Author

enty8080 commented May 7, 2023

@gucio321 Thank you very much! May I ask, where did you master giu and imgui-go? I didn't find any documentation on these libraries.

P.S. I made a mistake, it should not print text, it should display it on the screen right below the input. Also, idk why, but c = "" does not clean the variable, it contains all previous commands.

@gucio321
Copy link
Collaborator

gucio321 commented May 7, 2023

@gucio321 Thank you very much! May I ask, where did you master giu and imgui-go? I didn't find any documentation on these libraries.

I just was using these libraries for a longer time (especially giu, because I'm not really familiar with imgui-go)
And the documentation (at least in giu) should be present in its source code (let me know if it doesn't) and on godoc https://pkg.go.dev/github.com/AllenDang/giu (but it shows its stage from latest release which was some time ago)

Also, idk why, but c = "" does not clean the variable, it contains all previous commands.

meh it is so tricky... I think you need to play with InputTextCallback.
I managed do this with the following code...

package main

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

var (
	c              string
	cmd            string
	isEnterPressed bool
)

func loop() {
	imgui.PushStyleVarVec2(imgui.StyleVarWindowPadding, imgui.Vec2{0, 0})
	imgui.PushStyleVarFloat(imgui.StyleVarFrameRounding, 0)
	defer imgui.PopStyleVarV(2)
	giu.SingleWindow().Layout(
		giu.Label(cmd),
		giu.InputTextMultiline(&c).Size(-1, -1).Callback(func(d imgui.InputTextCallbackData) int32 {
			if isEnterPressed {
				cmd += "\n" + c
				d.DeleteBytes(0, len(d.Buffer()))
				isEnterPressed = false
			}

			// in this particular case, we ar not able to edit buffer (WHY?)
			if d.EventFlag() == imgui.InputTextFlagsCallbackCharFilter {
				if rune(d.EventChar()) == '\n' {
					isEnterPressed = true
				} else {
					isEnterPressed = false
				}
			}

			return 0
		}).Flags(giu.InputTextFlagsCallbackAlways|giu.InputTextFlagsCallbackCharFilter),
	)
}

func main() {
	wnd := giu.NewMasterWindow("Issue 660 [demo]", 640, 80, 0)
	wnd.Run(loop)
}

also ref: #434 (thats not the same but it shows how to use input text callbacks)

@enty8080
Copy link
Author

enty8080 commented May 7, 2023

@gucio321 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants