From 804833866c0233a52f06d28159eb9b3853464fdb Mon Sep 17 00:00:00 2001 From: hperigo Date: Sun, 7 Oct 2018 14:47:33 -0300 Subject: [PATCH 1/6] added slider float --- WrapperConverter.go | 13 +++++++++++++ imgui.go | 16 ++++++++++++++++ imguiWrapper.cpp | 5 +++++ imguiWrapper.h | 1 + 4 files changed, 35 insertions(+) diff --git a/WrapperConverter.go b/WrapperConverter.go index d4180319..ff14d249 100644 --- a/WrapperConverter.go +++ b/WrapperConverter.go @@ -41,6 +41,19 @@ func wrapInt32(goValue *int32) (wrapped *C.int, finisher func()) { return } +func wrapFloat(goValue *float32) (wrapped *C.float, finisher func()) { + if goValue != nil { + cValue := C.float(*goValue) + wrapped = &cValue + finisher = func() { + *goValue = float32(cValue) + } + } else { + finisher = func() {} + } + return +} + func wrapString(value string) (wrapped *C.char, finisher func()) { wrapped = C.CString(value) finisher = func() { C.free(unsafe.Pointer(wrapped)) } // nolint: gas diff --git a/imgui.go b/imgui.go index 431a80c4..db966596 100644 --- a/imgui.go +++ b/imgui.go @@ -360,6 +360,22 @@ func SliderInt(label string, value *int32, min, max int32) bool { return SliderIntV(label, value, min, max, "%d") } +// SliderIntV creates a slider for integers. +func SliderFloatV(label string, value *float32, min, max float32, format string) bool { + labelArg, labelFin := wrapString(label) + defer labelFin() + valueArg, valueFin := wrapFloat(value) + defer valueFin() + formatArg, formatFin := wrapString(format) + defer formatFin() + return C.iggSliderFloat(labelArg, valueArg, C.float(min), C.float(max), formatArg) != 0 +} + +// SliderInt calls SliderIntV(label, value, min, max, "%d"). +func SliderFloat(label string, value *float32, min, max float32) bool { + return SliderFloatV(label, value, min, max, "%f") +} + // Separator is generally horizontal. Inside a menu bar or in horizontal layout mode, this becomes a vertical separator. func Separator() { C.iggSeparator() diff --git a/imguiWrapper.cpp b/imguiWrapper.cpp index b0566046..648b96ed 100644 --- a/imguiWrapper.cpp +++ b/imguiWrapper.cpp @@ -244,6 +244,11 @@ IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0; } +IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format) +{ + return ImGui::SliderFloat(label, value, minValue, maxValue, format) ? 1 : 0; +} + void iggSeparator(void) { ImGui::Separator(); diff --git a/imguiWrapper.h b/imguiWrapper.h index 976ccad8..a925af5e 100644 --- a/imguiWrapper.h +++ b/imguiWrapper.h @@ -66,6 +66,7 @@ extern IggBool iggBeginCombo(char const *label, char const *previewValue, int fl extern void iggEndCombo(void); extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format); +extern IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format); extern void iggSeparator(void); extern void iggSameLine(float posX, float spacingW); From cd74cd3e7b1d3ce2d695a5d3aa9ecba5dc7c7d59 Mon Sep 17 00:00:00 2001 From: hperigo Date: Sun, 7 Oct 2018 15:35:44 -0300 Subject: [PATCH 2/6] added Drag float and drag int --- imgui.go | 38 ++++++++++++++++++++++++++++++++++++++ imguiWrapper.cpp | 10 ++++++++++ imguiWrapper.h | 3 +++ 3 files changed, 51 insertions(+) diff --git a/imgui.go b/imgui.go index db966596..6ad85161 100644 --- a/imgui.go +++ b/imgui.go @@ -360,6 +360,25 @@ func SliderInt(label string, value *int32, min, max int32) bool { return SliderIntV(label, value, min, max, "%d") } +// DragInt calls DragIntV(label, value, min, max, "%%d"). +func DragInt(label string, value *int32, min, max int32) bool { + return DragIntV(label, value, 1, min, max, "%d") +} + +// SliderFloat calls SliderIntV(label, value, speed = 1, min, max, "%0.3f"). +func DragIntV(label string, value *int32, speed, min, max int32, format string) bool { + labelArg, labelFin := wrapString(label) + defer labelFin() + + valueArg, valueFin := wrapInt32(value) + defer valueFin() + + formatArg, formatFin := wrapString(format) + defer formatFin() + + return C.iggDragInt(labelArg, valueArg, C.int(speed), C.int(min), C.int(max), formatArg) != 0 +} + // SliderIntV creates a slider for integers. func SliderFloatV(label string, value *float32, min, max float32, format string) bool { labelArg, labelFin := wrapString(label) @@ -376,6 +395,25 @@ func SliderFloat(label string, value *float32, min, max float32) bool { return SliderFloatV(label, value, min, max, "%f") } +// SliderFloat calls SliderFloatV(label, value, min, max, "%0.3f"). +func DragFloat(label string, value *float32, speed, min, max float32) bool { + return DragFloatV(label, value, speed, min, max, "%0.3f") +} + +// SliderFloat calls SliderFloatV(label, value, min, max, "%0.3f"). +func DragFloatV(label string, value *float32, speed, min, max float32, format string) bool { + labelArg, labelFin := wrapString(label) + defer labelFin() + + valueArg, valueFin := wrapFloat(value) + defer valueFin() + + formatArg, formatFin := wrapString(format) + defer formatFin() + + return C.iggDragFloat(labelArg, valueArg, C.float(speed), C.float(min), C.float(max), formatArg) != 0 +} + // Separator is generally horizontal. Inside a menu bar or in horizontal layout mode, this becomes a vertical separator. func Separator() { C.iggSeparator() diff --git a/imguiWrapper.cpp b/imguiWrapper.cpp index 648b96ed..2889c53a 100644 --- a/imguiWrapper.cpp +++ b/imguiWrapper.cpp @@ -244,11 +244,21 @@ IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0; } +IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, const char* format) +{ + return ImGui::DragInt( label, value, speed, min, max, format ) ? 1 : 0; +} + IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format) { return ImGui::SliderFloat(label, value, minValue, maxValue, format) ? 1 : 0; } +IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, const char* format) +{ + return ImGui::DragFloat( label, value, speed, min, max, format ) ? 1 : 0; +} + void iggSeparator(void) { ImGui::Separator(); diff --git a/imguiWrapper.h b/imguiWrapper.h index a925af5e..1bc68de7 100644 --- a/imguiWrapper.h +++ b/imguiWrapper.h @@ -66,7 +66,10 @@ extern IggBool iggBeginCombo(char const *label, char const *previewValue, int fl extern void iggEndCombo(void); extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format); +extern IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, const char* format); + extern IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format); +extern IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, const char* format); extern void iggSeparator(void); extern void iggSameLine(float posX, float spacingW); From ec8d76417079e82968201bfd3e35c0ed618107ba Mon Sep 17 00:00:00 2001 From: hperigo Date: Wed, 10 Oct 2018 20:52:40 -0300 Subject: [PATCH 3/6] fixed comments and char const *type order" --- imgui.go | 10 +++++----- imguiWrapper.cpp | 4 ++-- imguiWrapper.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/imgui.go b/imgui.go index 6ad85161..2f65e1e7 100644 --- a/imgui.go +++ b/imgui.go @@ -365,7 +365,7 @@ func DragInt(label string, value *int32, min, max int32) bool { return DragIntV(label, value, 1, min, max, "%d") } -// SliderFloat calls SliderIntV(label, value, speed = 1, min, max, "%0.3f"). +// DragIntV creates a dragable slider for integers, func DragIntV(label string, value *int32, speed, min, max int32, format string) bool { labelArg, labelFin := wrapString(label) defer labelFin() @@ -379,7 +379,7 @@ func DragIntV(label string, value *int32, speed, min, max int32, format string) return C.iggDragInt(labelArg, valueArg, C.int(speed), C.int(min), C.int(max), formatArg) != 0 } -// SliderIntV creates a slider for integers. +//SliderFloatV creates a slider for floats. func SliderFloatV(label string, value *float32, min, max float32, format string) bool { labelArg, labelFin := wrapString(label) defer labelFin() @@ -390,17 +390,17 @@ func SliderFloatV(label string, value *float32, min, max float32, format string) return C.iggSliderFloat(labelArg, valueArg, C.float(min), C.float(max), formatArg) != 0 } -// SliderInt calls SliderIntV(label, value, min, max, "%d"). +// SliderFloat calls SliderIntV(label, value, min, max, "%d"). func SliderFloat(label string, value *float32, min, max float32) bool { return SliderFloatV(label, value, min, max, "%f") } -// SliderFloat calls SliderFloatV(label, value, min, max, "%0.3f"). +// DragFloat calls DragFloatV(label, value, min, max, "%0.3f"). func DragFloat(label string, value *float32, speed, min, max float32) bool { return DragFloatV(label, value, speed, min, max, "%0.3f") } -// SliderFloat calls SliderFloatV(label, value, min, max, "%0.3f"). +// DragFloatV creates a dragglable slider for floats func DragFloatV(label string, value *float32, speed, min, max float32, format string) bool { labelArg, labelFin := wrapString(label) defer labelFin() diff --git a/imguiWrapper.cpp b/imguiWrapper.cpp index 2889c53a..bce8679d 100644 --- a/imguiWrapper.cpp +++ b/imguiWrapper.cpp @@ -244,7 +244,7 @@ IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0; } -IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, const char* format) +IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, char const *format) { return ImGui::DragInt( label, value, speed, min, max, format ) ? 1 : 0; } @@ -254,7 +254,7 @@ IggBool iggSliderFloat(char const *label, float *value, float minValue, float ma return ImGui::SliderFloat(label, value, minValue, maxValue, format) ? 1 : 0; } -IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, const char* format) +IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, char const *format) { return ImGui::DragFloat( label, value, speed, min, max, format ) ? 1 : 0; } diff --git a/imguiWrapper.h b/imguiWrapper.h index 1bc68de7..fc6b6650 100644 --- a/imguiWrapper.h +++ b/imguiWrapper.h @@ -66,10 +66,10 @@ extern IggBool iggBeginCombo(char const *label, char const *previewValue, int fl extern void iggEndCombo(void); extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format); -extern IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, const char* format); +extern IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, char const *format); extern IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format); -extern IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, const char* format); +extern IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, char const *format); extern void iggSeparator(void); extern void iggSameLine(float posX, float spacingW); From 7d3526740d4c599053d8e168a8841fd9299dc1a4 Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Thu, 1 Nov 2018 10:55:43 +0100 Subject: [PATCH 4/6] clarified API naming and signatures --- CONTRIBUTING.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06ea1bd4..327db001 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -20,3 +20,28 @@ At the moment, this library is primarily used by **InkyBlackness**. If you can a * Code is properly formatted & linted (use [golangci-lint](https://github.com/golangci/golangci-lint) for a full check) * Public Go API is documented (copied documentation from **Dear ImGui** is acceptable and recommended, assuming it is adapted regarding type names) * API and version philosophies are respected (see README.md) + +#### Clarification on API naming and signatures + +If an **Dear ImGui** function has the signature of + +``` +SomeControl(const char *label, int value, int optArg1 = 0, const char *optArg2 = "stuff"); +``` + +then the wrapper functions should be + +``` +// SomeControl calls SomeControlV(label, value, 0, "stuff"). +SomeControl(label string, value int32) { + SomeControlV(label, value, 0, "stuff") +} + +// SomeControlV does things (text possibly copied from imgui.h). +SomeControlV(label string, value int32, optArg1 int32, optArg2 string) { + // ... +} +``` + +The "idiomatic" function should have only the required parameters of the underlying function, and its comment specifies all the defaults, matching that of `imgui.h`. +The "verbose" variant should require all the parameters of the underlying function. From 59c7649255d1b36fb7f6e588b8891c5a29cf2b0f Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Thu, 1 Nov 2018 10:57:21 +0100 Subject: [PATCH 5/6] restructured drag/slider extensions according to guidelines - parameter lists - order of functions (mirroring sequence in imgui.h) - comments --- imgui.go | 60 ++++++++++++++++++++++-------------------------- imguiWrapper.cpp | 16 ++++++------- imguiWrapper.h | 8 +++---- 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/imgui.go b/imgui.go index 6e09a082..7414ce1b 100644 --- a/imgui.go +++ b/imgui.go @@ -345,74 +345,68 @@ func EndCombo() { C.iggEndCombo() } -// SliderIntV creates a slider for integers. -func SliderIntV(label string, value *int32, min, max int32, format string) bool { +// DragFloatV creates a draggable slider for floats. +func DragFloatV(label string, value *float32, speed, min, max float32, format string, power float32) bool { labelArg, labelFin := wrapString(label) defer labelFin() - valueArg, valueFin := wrapInt32(value) + valueArg, valueFin := wrapFloat(value) defer valueFin() formatArg, formatFin := wrapString(format) defer formatFin() - return C.iggSliderInt(labelArg, valueArg, C.int(min), C.int(max), formatArg) != 0 + return C.iggDragFloat(labelArg, valueArg, C.float(speed), C.float(min), C.float(max), formatArg, C.float(power)) != 0 } -// SliderInt calls SliderIntV(label, value, min, max, "%d"). -func SliderInt(label string, value *int32, min, max int32) bool { - return SliderIntV(label, value, min, max, "%d") -} - -// DragInt calls DragIntV(label, value, min, max, "%%d"). -func DragInt(label string, value *int32, min, max int32) bool { - return DragIntV(label, value, 1, min, max, "%d") +// DragFloat calls DragFloatV(label, value, 1.0, 0.0, 0.0, "%.3f", 1.0). +func DragFloat(label string, value *float32) bool { + return DragFloatV(label, value, 1.0, 0.0, 0.0, "%.3f", 1.0) } -// DragIntV creates a dragable slider for integers, -func DragIntV(label string, value *int32, speed, min, max int32, format string) bool { +// DragIntV creates a draggable slider for integers. +func DragIntV(label string, value *int32, speed float32, min, max int32, format string) bool { labelArg, labelFin := wrapString(label) defer labelFin() - valueArg, valueFin := wrapInt32(value) defer valueFin() - formatArg, formatFin := wrapString(format) defer formatFin() + return C.iggDragInt(labelArg, valueArg, C.float(speed), C.int(min), C.int(max), formatArg) != 0 +} - return C.iggDragInt(labelArg, valueArg, C.int(speed), C.int(min), C.int(max), formatArg) != 0 +// DragInt calls DragIntV(label, value, 1.0, 0, 0, "%d"). +func DragInt(label string, value *int32) bool { + return DragIntV(label, value, 1.0, 0, 0, "%d") } -//SliderFloatV creates a slider for floats. -func SliderFloatV(label string, value *float32, min, max float32, format string) bool { +// SliderFloatV creates a slider for floats. +func SliderFloatV(label string, value *float32, min, max float32, format string, power float32) bool { labelArg, labelFin := wrapString(label) defer labelFin() valueArg, valueFin := wrapFloat(value) defer valueFin() formatArg, formatFin := wrapString(format) defer formatFin() - return C.iggSliderFloat(labelArg, valueArg, C.float(min), C.float(max), formatArg) != 0 + return C.iggSliderFloat(labelArg, valueArg, C.float(min), C.float(max), formatArg, C.float(power)) != 0 } -// SliderFloat calls SliderIntV(label, value, min, max, "%d"). +// SliderFloat calls SliderIntV(label, value, min, max, "%.3f", 1.0). func SliderFloat(label string, value *float32, min, max float32) bool { - return SliderFloatV(label, value, min, max, "%f") + return SliderFloatV(label, value, min, max, "%.3f", 1.0) } -// DragFloat calls DragFloatV(label, value, min, max, "%0.3f"). -func DragFloat(label string, value *float32, speed, min, max float32) bool { - return DragFloatV(label, value, speed, min, max, "%0.3f") -} - -// DragFloatV creates a dragglable slider for floats -func DragFloatV(label string, value *float32, speed, min, max float32, format string) bool { +// SliderIntV creates a slider for integers. +func SliderIntV(label string, value *int32, min, max int32, format string) bool { labelArg, labelFin := wrapString(label) defer labelFin() - - valueArg, valueFin := wrapFloat(value) + valueArg, valueFin := wrapInt32(value) defer valueFin() - formatArg, formatFin := wrapString(format) defer formatFin() + return C.iggSliderInt(labelArg, valueArg, C.int(min), C.int(max), formatArg) != 0 +} - return C.iggDragFloat(labelArg, valueArg, C.float(speed), C.float(min), C.float(max), formatArg) != 0 +// SliderInt calls SliderIntV(label, value, min, max, "%d"). +func SliderInt(label string, value *int32, min, max int32) bool { + return SliderIntV(label, value, min, max, "%d") } // Separator is generally horizontal. Inside a menu bar or in horizontal layout mode, this becomes a vertical separator. diff --git a/imguiWrapper.cpp b/imguiWrapper.cpp index bce8679d..09de0e75 100644 --- a/imguiWrapper.cpp +++ b/imguiWrapper.cpp @@ -239,24 +239,24 @@ void iggEndCombo(void) ImGui::EndCombo(); } -IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format) +IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, float power) { - return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0; + return ImGui::DragFloat(label, value, speed, min, max, format, power) ? 1 : 0; } -IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, char const *format) +IggBool iggDragInt(char const *label, int *value, float speed, int min, int max, char const *format) { - return ImGui::DragInt( label, value, speed, min, max, format ) ? 1 : 0; + return ImGui::DragInt(label, value, speed, min, max, format) ? 1 : 0; } -IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format) +IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format, float power) { - return ImGui::SliderFloat(label, value, minValue, maxValue, format) ? 1 : 0; + return ImGui::SliderFloat(label, value, minValue, maxValue, format, power) ? 1 : 0; } -IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, char const *format) +IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format) { - return ImGui::DragFloat( label, value, speed, min, max, format ) ? 1 : 0; + return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0; } void iggSeparator(void) diff --git a/imguiWrapper.h b/imguiWrapper.h index fc6b6650..fa36b0e0 100644 --- a/imguiWrapper.h +++ b/imguiWrapper.h @@ -65,11 +65,11 @@ extern IggBool iggCheckbox(char const *label, IggBool *selected); extern IggBool iggBeginCombo(char const *label, char const *previewValue, int flags); extern void iggEndCombo(void); -extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format); -extern IggBool iggDragInt( char const *label, int *value, int speed, int min, int max, char const *format); +extern IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, float power); +extern IggBool iggDragInt(char const *label, int *value, float speed, int min, int max, char const *format); -extern IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format); -extern IggBool iggDragFloat( char const *label, float *value, float speed, float min, float max, char const *format); +extern IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format, float power); +extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format); extern void iggSeparator(void); extern void iggSameLine(float posX, float spacingW); From 0918010f1e64c11c06b8e2c9f14a4e3d654022b5 Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Thu, 1 Nov 2018 10:59:49 +0100 Subject: [PATCH 6/6] fixed import order --- _examples/opengl3_example/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_examples/opengl3_example/main.go b/_examples/opengl3_example/main.go index c97f4ca6..9e1378fa 100644 --- a/_examples/opengl3_example/main.go +++ b/_examples/opengl3_example/main.go @@ -1,10 +1,10 @@ package main import ( + "fmt" "runtime" "time" - "fmt" "github.com/go-gl/gl/v3.2-core/gl" "github.com/go-gl/glfw/v3.2/glfw" "github.com/inkyblackness/imgui-go"