From 50ae79a81f24e7b4cbb10899b3683efa558f34ce Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 20 Apr 2022 20:36:43 +0300 Subject: [PATCH 1/8] Combo: init --- Widgets.go | 34 ++++++++++++++++++++++++++++++++++ wrapper/Widgets.cpp | 5 +++++ wrapper/Widgets.h | 1 + 3 files changed, 40 insertions(+) diff --git a/Widgets.go b/Widgets.go index b6e07bef..62de5ae3 100644 --- a/Widgets.go +++ b/Widgets.go @@ -6,8 +6,28 @@ import "C" import ( "fmt" "math" + "strings" + "sync" ) +var ( + syncInts []pntCgoInt + syncMutex sync.Mutex +) + +// pntCgoInt struct `int` for sync +type pntCgoInt struct { + pntC *C.int + pntGo *int +} + +// addSyncInt add pointer of `int` for sync +func addSyncInt(f *C.int, t *int) { + m.Lock() + syncInts = append(syncInts, pntCgoInt{pntC: f, pntGo: t}) + m.Unlock() +} + // Text adds formatted text. See PushTextWrapPosV() or PushStyleColorV() for modifying the output. // Without any modified style stack, the text is unformatted. func Text(text string) { @@ -210,6 +230,20 @@ func EndCombo() { C.iggEndCombo() } +// Combo is implementation of ImGui::Combo. +// id is UI identification +// value is position in list strings +func Combo(id string, value *int, list []string, heightInItems int) bool { + var position C.int = (C.int) * value + addSyncInt(&position, value) // add pointers C and Go for sync + return C.iggCombo( + C.CString(id), + position, + C.CString(strings.Join(list, string(`\x000`))), + (C.int)(heightInItems), + ) +} + // SliderFlags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. // We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. type SliderFlags int diff --git a/wrapper/Widgets.cpp b/wrapper/Widgets.cpp index 6494a18d..d1da542e 100644 --- a/wrapper/Widgets.cpp +++ b/wrapper/Widgets.cpp @@ -82,6 +82,11 @@ void iggEndCombo(void) ImGui::EndCombo(); } +IggBool iggCombo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) +{ + ImGui::Combo(label, current_item, items_separated_by_zeros, height_in_items); +} + IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, int flags) { return ImGui::DragFloat(label, value, speed, min, max, format, flags) ? 1 : 0; diff --git a/wrapper/Widgets.h b/wrapper/Widgets.h index 22954ac7..9c0e9640 100644 --- a/wrapper/Widgets.h +++ b/wrapper/Widgets.h @@ -25,6 +25,7 @@ extern void iggProgressBar(float fraction, IggVec2 const *size, char const *over extern IggBool iggBeginCombo(char const *label, char const *previewValue, int flags); extern void iggEndCombo(void); +extern IggBool iggCombo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items); extern IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, int flags); extern IggBool iggDragFloatN(char const *label, float *value, int n, float speed, float min, float max, char const *format, int flags); From d9862bd11cacd65b0417c1e8e9d1c2ed16e4ca05 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 20 Apr 2022 20:42:43 +0300 Subject: [PATCH 2/8] Combo: add update --- Widgets.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Widgets.go b/Widgets.go index 62de5ae3..3e99c3b2 100644 --- a/Widgets.go +++ b/Widgets.go @@ -28,6 +28,13 @@ func addSyncInt(f *C.int, t *int) { m.Unlock() } +// UpdatePointers update values in pointers +func UpdatePointers() { + for i := range syncInts { + *syncInts.pntGo = (int)(*syncInts.pntC) + } +} + // Text adds formatted text. See PushTextWrapPosV() or PushStyleColorV() for modifying the output. // Without any modified style stack, the text is unformatted. func Text(text string) { From 4a0ab7ab04eb7bf7352be0e937d1d4926cf79e2b Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 20 Apr 2022 21:39:05 +0300 Subject: [PATCH 3/8] Combo:example is ok --- Widgets.go | 18 +++++++++--------- wrapper/Widgets.cpp | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Widgets.go b/Widgets.go index 3e99c3b2..d512b8ad 100644 --- a/Widgets.go +++ b/Widgets.go @@ -23,15 +23,15 @@ type pntCgoInt struct { // addSyncInt add pointer of `int` for sync func addSyncInt(f *C.int, t *int) { - m.Lock() + syncMutex.Lock() syncInts = append(syncInts, pntCgoInt{pntC: f, pntGo: t}) - m.Unlock() + syncMutex.Unlock() } // UpdatePointers update values in pointers func UpdatePointers() { for i := range syncInts { - *syncInts.pntGo = (int)(*syncInts.pntC) + *syncInts[i].pntGo = (int)(*syncInts[i].pntC) } } @@ -240,15 +240,15 @@ func EndCombo() { // Combo is implementation of ImGui::Combo. // id is UI identification // value is position in list strings -func Combo(id string, value *int, list []string, heightInItems int) bool { - var position C.int = (C.int) * value +func Combo(id string, value *int, list []string) bool { + var position C.int = (C.int)(*value) addSyncInt(&position, value) // add pointers C and Go for sync return C.iggCombo( C.CString(id), - position, - C.CString(strings.Join(list, string(`\x000`))), - (C.int)(heightInItems), - ) + &position, + C.CString(strings.Join(list, string(byte(0)))+string(byte(0))), + (C.int)(len(list)), + ) != 0 } // SliderFlags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. diff --git a/wrapper/Widgets.cpp b/wrapper/Widgets.cpp index d1da542e..26f84420 100644 --- a/wrapper/Widgets.cpp +++ b/wrapper/Widgets.cpp @@ -84,7 +84,7 @@ void iggEndCombo(void) IggBool iggCombo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) { - ImGui::Combo(label, current_item, items_separated_by_zeros, height_in_items); + return ImGui::Combo(label, current_item, items_separated_by_zeros, height_in_items); } IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, int flags) From cb6a6d1edab7eb2dab25a310af6bef5a09967719 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 20 Apr 2022 22:03:49 +0300 Subject: [PATCH 4/8] minimaze --- Widgets.go | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/Widgets.go b/Widgets.go index d512b8ad..feef5a1c 100644 --- a/Widgets.go +++ b/Widgets.go @@ -7,34 +7,8 @@ import ( "fmt" "math" "strings" - "sync" ) -var ( - syncInts []pntCgoInt - syncMutex sync.Mutex -) - -// pntCgoInt struct `int` for sync -type pntCgoInt struct { - pntC *C.int - pntGo *int -} - -// addSyncInt add pointer of `int` for sync -func addSyncInt(f *C.int, t *int) { - syncMutex.Lock() - syncInts = append(syncInts, pntCgoInt{pntC: f, pntGo: t}) - syncMutex.Unlock() -} - -// UpdatePointers update values in pointers -func UpdatePointers() { - for i := range syncInts { - *syncInts[i].pntGo = (int)(*syncInts[i].pntC) - } -} - // Text adds formatted text. See PushTextWrapPosV() or PushStyleColorV() for modifying the output. // Without any modified style stack, the text is unformatted. func Text(text string) { @@ -240,12 +214,12 @@ func EndCombo() { // Combo is implementation of ImGui::Combo. // id is UI identification // value is position in list strings -func Combo(id string, value *int, list []string) bool { - var position C.int = (C.int)(*value) - addSyncInt(&position, value) // add pointers C and Go for sync +func Combo(id string, value *int32, list []string) bool { + valueArg, valueFin := wrapInt32(value) + defer valueFin() return C.iggCombo( C.CString(id), - &position, + valueArg, C.CString(strings.Join(list, string(byte(0)))+string(byte(0))), (C.int)(len(list)), ) != 0 From 7ba8a1ced362971642c2c8b0513c42fdf0bbc3de Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Fri, 22 Apr 2022 20:40:41 +0300 Subject: [PATCH 5/8] step --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b2dedbbf..86031b93 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/inkyblackness/imgui-go/v4 +module github.com/Konstantin8105/imgui-go/v4 require github.com/stretchr/testify v1.3.0 From f6e2dd707032014b7e4ee0dfdd7c37d963287279 Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Sat, 4 Jun 2022 15:05:58 +0200 Subject: [PATCH 6/8] revert module name change --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 86031b93..b2dedbbf 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Konstantin8105/imgui-go/v4 +module github.com/inkyblackness/imgui-go/v4 require github.com/stretchr/testify v1.3.0 From e50f3eeafe6e67bc04c441a2aa0c16a032d05034 Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Sat, 4 Jun 2022 15:35:18 +0200 Subject: [PATCH 7/8] Cleanup code style --- wrapper/Widgets.cpp | 4 ++-- wrapper/Widgets.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wrapper/Widgets.cpp b/wrapper/Widgets.cpp index 26f84420..f89b80e5 100644 --- a/wrapper/Widgets.cpp +++ b/wrapper/Widgets.cpp @@ -82,9 +82,9 @@ void iggEndCombo(void) ImGui::EndCombo(); } -IggBool iggCombo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) +IggBool iggCombo(char const *label, int *currentItem, char const *itemsSeparatedByZeros, int heightInItems) { - return ImGui::Combo(label, current_item, items_separated_by_zeros, height_in_items); + return ImGui::Combo(label, currentItem, itemsSeparatedByZeros, heightInItems); } IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, int flags) diff --git a/wrapper/Widgets.h b/wrapper/Widgets.h index 9c0e9640..22d7ebfe 100644 --- a/wrapper/Widgets.h +++ b/wrapper/Widgets.h @@ -25,7 +25,7 @@ extern void iggProgressBar(float fraction, IggVec2 const *size, char const *over extern IggBool iggBeginCombo(char const *label, char const *previewValue, int flags); extern void iggEndCombo(void); -extern IggBool iggCombo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items); +extern IggBool iggCombo(char const *label, int *currentItem, char const *itemsSeparatedByZeros, int heightInItems); extern IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, int flags); extern IggBool iggDragFloatN(char const *label, float *value, int n, float speed, float min, float max, char const *format, int flags); From 68adfb843c51ff9cf2815c531b33bcf76f31f5c8 Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Sat, 4 Jun 2022 15:36:17 +0200 Subject: [PATCH 8/8] Separate Combo() from ComboV(); use proper default for Combo() --- Widgets.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Widgets.go b/Widgets.go index feef5a1c..37ec1439 100644 --- a/Widgets.go +++ b/Widgets.go @@ -211,17 +211,21 @@ func EndCombo() { C.iggEndCombo() } -// Combo is implementation of ImGui::Combo. -// id is UI identification -// value is position in list strings +// Combo calls ComboV(id, value, list, -1). func Combo(id string, value *int32, list []string) bool { + return ComboV(id, value, list, -1) +} + +// ComboV is a helper over BeginCombo()/EndCombo() which are kept available for convenience purpose. +// This is analogous to how ListBox are created. +func ComboV(id string, value *int32, list []string, heightInItems int) bool { valueArg, valueFin := wrapInt32(value) defer valueFin() return C.iggCombo( C.CString(id), valueArg, C.CString(strings.Join(list, string(byte(0)))+string(byte(0))), - (C.int)(len(list)), + (C.int)(heightInItems), ) != 0 }