Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from inkyblackness/float-sliders
Browse files Browse the repository at this point in the history
Float sliders thanks to @Hperigo
  • Loading branch information
dertseha authored Nov 1, 2018
2 parents 52d503b + 0918010 commit 10c6d3a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions WrapperConverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _examples/opengl3_example/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
48 changes: 48 additions & 0 deletions imgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,54 @@ func EndCombo() {
C.iggEndCombo()
}

// 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 := 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, C.float(power)) != 0
}

// 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 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
}

// 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, 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, C.float(power)) != 0
}

// 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, "%.3f", 1.0)
}

// SliderIntV creates a slider for integers.
func SliderIntV(label string, value *int32, min, max int32, format string) bool {
labelArg, labelFin := wrapString(label)
Expand Down
15 changes: 15 additions & 0 deletions imguiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ void iggEndCombo(void)
ImGui::EndCombo();
}

IggBool iggDragFloat(char const *label, float *value, float speed, float min, float max, char const *format, float power)
{
return ImGui::DragFloat(label, value, speed, min, max, format, power) ? 1 : 0;
}

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;
}

IggBool iggSliderFloat(char const *label, float *value, float minValue, float maxValue, char const *format, float power)
{
return ImGui::SliderFloat(label, value, minValue, maxValue, format, power) ? 1 : 0;
}

IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format)
{
return ImGui::SliderInt(label, value, minValue, maxValue, format) ? 1 : 0;
Expand Down
4 changes: 4 additions & 0 deletions imguiWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ 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 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, float power);
extern IggBool iggSliderInt(char const *label, int *value, int minValue, int maxValue, char const *format);

extern void iggSeparator(void);
Expand Down

0 comments on commit 10c6d3a

Please sign in to comment.