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

Add string formatting overloads for Text, LabelText, TreeNode, SetTooltip #170

Merged
merged 3 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ Functions that don't have optional parameters don't come in a verbose variant.

The **Dear ImGui** functions `IO()` and `Style()` have been renamed to be `CurrentIO()` and `CurrentStyle()`.
This was done because their returned types have the same name, causing a name clash.
With the `Current` prefix, they also better describe what they return.
With the `Current` prefix, they also better describe what they return.

## API philosophy
This library does not intend to export all the functions of the wrapped **Dear ImGui**. The following filter applies as a rule of thumb:
* Functions marked as "obsolete" are not available. (The corresponding C code isn't even compiled - disabled by define)
* "Shortcut" Functions, which combine language features and/or other **Dear ImGui** functions, are not available. Prime example are the Text*() functions for instance: Text formatting should be done with fmt.Sprintf(), and style formatting with the corresponding Push/Pop functions.
* "Shortcut" Functions, which combine language features and/or other **Dear ImGui** functions, are not available. There may be exceptions for this if the shortcut exists in Dear ImGui code base (e.g. `ImGui::Text` which does printf style string formatting)
* Functions that are not needed by InkyBlackness are ignored. This doesn't mean that they can't be in the wrapper, they are simply not a priority. Feel free to propose an implementation or make a pull request, respecting the previous points :)

## Version philosophy
Expand Down
25 changes: 24 additions & 1 deletion Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package imgui
// #include "wrapper/Widgets.h"
import "C"

import "math"
import (
"fmt"
"math"
)

// Text adds formatted text. See PushTextWrapPosV() or PushStyleColorV() for modifying the output.
// Without any modified style stack, the text is unformatted.
Expand All @@ -14,6 +17,11 @@ func Text(text string) {
C.iggTextUnformatted(textArg)
}

// Textf calls Text(fmt.Sprintf(format, v...) .
func Textf(format string, v ...interface{}) {
Text(fmt.Sprintf(format, v...))
}

// LabelText adds text+label aligned the same way as value+label widgets.
func LabelText(label, text string) {
labelArg, labelFin := wrapString(label)
Expand All @@ -23,6 +31,11 @@ func LabelText(label, text string) {
C.iggLabelText(labelArg, textArg)
}

// LabelTextf calls LabelText(label, fmt.Sprintf(format, v...)) .
func LabelTextf(label, format string, v ...interface{}) {
LabelText(label, fmt.Sprintf(format, v...))
}

// ButtonV returns true if it is clicked.
func ButtonV(id string, size Vec2) bool {
idArg, idFin := wrapString(id)
Expand Down Expand Up @@ -757,6 +770,11 @@ func TreeNode(label string) bool {
return TreeNodeV(label, 0)
}

// TreeNodef calls TreeNode(fmt.Sprintf(format, v...)) .
func TreeNodef(format string, v ...interface{}) bool {
return TreeNode(fmt.Sprintf(format, v...))
}

// TreePop finishes a tree branch. This has to be called for a matching TreeNodeV call returning true.
func TreePop() {
C.iggTreePop()
Expand Down Expand Up @@ -938,6 +956,11 @@ func SetTooltip(text string) {
C.iggSetTooltip(textArg)
}

// SetTooltipf calls SetTooltip(fmt.Sprintf(format, v...)) .
func SetTooltipf(format string, v ...interface{}) {
SetTooltip(fmt.Sprintf(format, v...))
}

// BeginTooltip begins/appends to a tooltip window. Used to create full-featured tooltip (with any kind of contents).
// Requires a call to EndTooltip().
func BeginTooltip() {
Expand Down