cimgui-go icon indicating copy to clipboard operation
cimgui-go copied to clipboard

Support *Scalar functions

Open ptxmac opened this issue 2 years ago • 11 comments

I was looking for a DragDouble widget, but imgui has been implementing new datatypes using the *Scalar widgets instead.

These work just like DragInt/Float/Etc but takes an enum declaring the type and a void pointer to the data.

For this to work from go we need a way to manually wrap the go pointer, i.e. make wrapNumberPtr public.

Or perhaps implement DragScalar with a switch that will type-cast based on the DataType.

something like:

func DragScalar(label string, data_type DataType, p_data unsafe.Pointer) bool {
	labelArg, labelFin := wrapString(label)
	defer labelFin()

        switch (data_type) {
        case DataType_Double:
             	vArg, vFin = wrapNumberPtr[C.double, float64](v)
        } 

      	defer vFin()

	return C.DragScalar(labelArg, C.ImGuiDataType(data_type), vArg) == C.bool(true)
}

ptxmac avatar Dec 31 '22 12:12 ptxmac

I'd say this is not necessary. We could easily create a one based on basic drag int or float.

AllenDang avatar Dec 31 '22 15:12 AllenDang

Mess with pointer data via CGO is not that pleasant after all.

AllenDang avatar Dec 31 '22 15:12 AllenDang

Mess with pointer data via CGO is not that pleasant after all.

agreed, but for certain applications the loss of precision when converting between float and double is unacceptable.

we could create DragDouble instead of switching in DragScalar, but then we should probably also create the rest of the supported datatypes: signed and unsigned 8/16/32/ 64 bit integers

ptxmac avatar Dec 31 '22 15:12 ptxmac

@AllenDang I think we can make our wrappers public to make *Scalar functions easier to use

gucio321 avatar Apr 17 '23 15:04 gucio321

@ptxmac from current code:

// InputScalarV parameter default value hint:
// p_step: NULL
// p_step_fast: NULL
// format: NULL
// flags: 0
func InputScalarV(label string, data_type DataType, p_data unsafe.Pointer, p_step unsafe.Pointer, p_step_fast unsafe.Pointer, format string, flags InputTextFl
        labelArg, labelFin := WrapString(label)
        formatArg, formatFin := WrapString(format)

        defer func() {
                labelFin()
                formatFin()
        }()
        return C.igInputScalar(labelArg, C.ImGuiDataType(data_type), (p_data), (p_step), (p_step_fast), formatArg, C.ImGuiInputTextFlags(flags)) == C.bool(tru
}

So I suppose its fixed

gucio321 avatar Sep 06 '23 19:09 gucio321

It looks like this has been broken again, now the code is uintptr instead of a real pointer

ptxmac avatar Oct 05 '24 13:10 ptxmac

@ptxmac what's wrong with it?

gucio321 avatar Oct 05 '24 14:10 gucio321

The code gen doesn't see the value as a pointer, so it's never copied.

Current code:

func InputScalar(label string, data_type DataType, p_data uintptr) bool {
	labelArg, labelFin := datautils.WrapString[C.char](label)

	defer func() {
		labelFin()
	}()
	return C.wrap_igInputScalar(labelArg, C.ImGuiDataType(data_type), C.uintptr_t(p_data)) == C.bool(true)
}

It should be similar to InputInt:

func InputInt(label string, v *int32) bool {
	labelArg, labelFin := datautils.WrapString[C.char](label)
	vArg, vFin := datautils.WrapNumberPtr[C.int, int32](v)

	defer func() {
		labelFin()
		vFin()
	}()
	return C.wrap_igInputInt(labelArg, vArg) == C.bool(true)
}

But where the type of v depends on the data_type argument.

ptxmac avatar Oct 05 '24 14:10 ptxmac

Basically the fix mentioned in https://github.com/AllenDang/cimgui-go/issues/74#issuecomment-1709000875 have regressed and we're back to the original problem

ptxmac avatar Oct 05 '24 15:10 ptxmac

yeah. I think there were a problem with -race maybe... let me reopen that and thats all I can do for now (i'm working on my other project atm)

gucio321 avatar Oct 05 '24 20:10 gucio321

I had another look, and it actually is possible to use using the current implementation. IT's just not very ergonomic 😄


func Scalar(v *int32) {
	vArg, vFin := datautils.WrapNumberPtr[C.int, int32](v)
	defer vFin()

	imgui.InputScalar("Scalar", imgui.DataTypeS32, uintptr(unsafe.Pointer(vArg)))
}

func ScalarN(v *[3]int32) {
	vArg := make([]C.int, len(v))
	for i, val := range v {
		vArg[i] = C.int(val)
	}

	defer func() {
		for i, val := range vArg {
			v[i] = int32(val)
		}
	}()

	imgui.InputScalarN("ScalarN", imgui.DataTypeS32, uintptr(unsafe.Pointer(&vArg[0])), 3)
}

ptxmac avatar Oct 12 '24 10:10 ptxmac