Skip to content

Commit

Permalink
remove primitive array + index overloads, user can call `mutablePrope…
Browse files Browse the repository at this point in the history
…rtyAt`
  • Loading branch information
elect86 committed May 18, 2023
1 parent 7faf32c commit 2404dff
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 133 deletions.
4 changes: 0 additions & 4 deletions core/src/main/kotlin/imgui/api/tabBarsTabs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ interface tabBarsTabs {
g.currentTabBar = g.currentTabBarStack.lastOrNull()?.tabBar
}

/** create a Tab. Returns true if the Tab is selected. */
fun beginTabItem(label: String, pOpen: BooleanArray, index: Int, flags: TabItemOnlyFlags = emptyFlags): Boolean =
beginTabItem(label, pOpen mutablePropertyAt index, flags)

/** create a Tab. Returns true if the Tab is selected. */
fun beginTabItem(label: String, pOpen: KMutableProperty0<Boolean>? = null, flags: TabItemOnlyFlags = emptyFlags): Boolean {

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/imgui/api/widgetsColorEditorPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ inline fun colorEdit4(label: String, x: Float, y: Float, z: Float, w: Float, fla
if (flags has Cef.Float) {
valueChanged /= drag(ids[n], f mutablePropertyAt n, 1f / 255f, 0f, if (hdr) 0f else 1f, fmtTableFloat[fmtIdx][n])
valueChangedAsFloat /= valueChanged
} else valueChanged /= drag(ids[n], i, n, 1f, 0, if (hdr) 0 else 255, fmtTableInt[fmtIdx][n])
} else valueChanged /= drag(ids[n], i mutablePropertyAt n, 1f, 0, if (hdr) 0 else 255, fmtTableInt[fmtIdx][n])
if (flags hasnt Cef.NoOptions) openPopupOnItemClick("context", PopupFlag.MouseButtonRight)
}

Expand Down
84 changes: 38 additions & 46 deletions core/src/main/kotlin/imgui/api/widgetsDataPlotting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,45 @@ import imgui.internal.sections.PlotType
// Widgets: Data Plotting
// - Consider using ImPlot (https://github.com/epezent/implot) which is much better!
interface widgetsDataPlotting {
fun plotLines(
label: String,
values: FloatArray,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
stride: Int = 1
) = plotEx(PlotType.Lines, label, values.size, valuesOffset, overlayText, scaleMin, scaleMax, graphSize) {
values[it * stride]
}
fun plotLines(label: String,
values: FloatArray,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
stride: Int = 1): Int =
plotEx(PlotType.Lines, label, values.size, valuesOffset, overlayText, scaleMin, scaleMax, graphSize) {
values[it * stride]
}

fun plotHistogram(
label: String,
values: FloatArray,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
stride: Int = 1
) = plotEx(PlotType.Histogram, label, values.size, valuesOffset, overlayText, scaleMin, scaleMax, graphSize) {
values[it * stride]
}
fun plotHistogram(label: String,
values: FloatArray,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
stride: Int = 1): Int =
plotEx(PlotType.Histogram, label, values.size, valuesOffset, overlayText, scaleMin, scaleMax, graphSize) {
values[it * stride]
}
}

inline fun plotLines(
label: String,
valuesCount: Int,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
valuesGetter: (idx: Int) -> Float
) = plotEx(PlotType.Lines, label, valuesCount, valuesOffset, overlayText, scaleMin, scaleMax, graphSize, valuesGetter)
inline fun plotLines(label: String,
valuesCount: Int,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
valuesGetter: (idx: Int) -> Float) = plotEx(PlotType.Lines, label, valuesCount, valuesOffset, overlayText, scaleMin, scaleMax, graphSize, valuesGetter)

inline fun plotHistogram(
label: String,
valuesCount: Int,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
valuesGetter: (idx: Int) -> Float
) = plotEx(
PlotType.Histogram, label, valuesCount, valuesOffset, overlayText, scaleMin, scaleMax, graphSize, valuesGetter
)
inline fun plotHistogram(label: String,
valuesCount: Int,
valuesOffset: Int = 0,
overlayText: String = "",
scaleMin: Float = Float.MAX_VALUE,
scaleMax: Float = Float.MAX_VALUE,
graphSize: Vec2 = Vec2(),
valuesGetter: (idx: Int) -> Float) = plotEx(PlotType.Histogram, label, valuesCount, valuesOffset, overlayText, scaleMin, scaleMax, graphSize, valuesGetter)
45 changes: 12 additions & 33 deletions core/src/main/kotlin/imgui/api/widgetsDrags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,8 @@ import kotlin.reflect.KMutableProperty0
// If you get a warning converting a float to ImGuiSliderFlags, read https://github.com/ocornut/imgui/issues/3361
interface widgetsDrags {

// [JVM] TODO inline? -> class

/** If v_min >= v_max we have no bound */
fun drag(label: String,
v: FloatArray,
ptr: Int,
vSpeed: Float = 1f,
vMin: Float = 0f,
vMax: Float = 0f,
format: String = "%.3f",
flags: SliderFlags = emptyFlags): Boolean = drag(label, v mutablePropertyAt ptr, vSpeed, vMin, vMax, format, flags)

fun drag2(label: String,
v: FloatArray,
vSpeed: Float = 1f,
Expand Down Expand Up @@ -159,15 +149,6 @@ interface widgetsDrags {
/** If v_min >= v_max we have no bound
*
* NB: vSpeed is float to allow adjusting the drag speed with more precision */
fun drag(label: String,
v: IntArray,
ptr: Int,
vSpeed: Float = 1f,
vMin: Int = 0,
vMax: Int = 0,
format: String? = "%d",
flags: SliderFlags = emptyFlags): Boolean = drag(label, v mutablePropertyAt ptr, vSpeed, vMin, vMax, format, flags)

fun drag2(label: String,
v: IntArray,
vSpeed: Float = 1f,
Expand Down Expand Up @@ -270,7 +251,7 @@ interface widgetsDrags {
min: Float = 0f,
max: Float = 0f,
format: String = "%.3f",
flags: SliderFlags = emptyFlags): Boolean = drag(label, pData, 0, vSpeed, min, max, format, flags)
flags: SliderFlags = emptyFlags): Boolean = drag(label, pData mutablePropertyAt 0, vSpeed, min, max, format, flags)

/** ote: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a Drag widget, p_min and p_max are optional.
* Read code of e.g. DragFloat(), DragInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. */
Expand Down Expand Up @@ -307,15 +288,14 @@ interface widgetsDrags {
if (!tempInputIsActive) {

// Tabbing or CTRL-clicking on Drag turns it into an InputText
val inputRequestedByTabbing =
tempInputAllowed && g.lastItemData.statusFlags has ItemStatusFlag.FocusedByTabbing
val inputRequestedByTabbing = tempInputAllowed && g.lastItemData.statusFlags has ItemStatusFlag.FocusedByTabbing
val clicked = hovered && MouseButton.Left.isClicked(id)
val doubleClicked = hovered && g.io.mouseClickedCount[0] == 2 && Key.MouseLeft testOwner id
val makeActive =
inputRequestedByTabbing || clicked || doubleClicked || g.navActivateId == id || g.navActivateInputId == id
val makeActive = inputRequestedByTabbing || clicked || doubleClicked || g.navActivateId == id || g.navActivateInputId == id
if (makeActive && (clicked || doubleClicked)) Key.MouseLeft.setOwner(id)
if (makeActive && tempInputAllowed) if (inputRequestedByTabbing || (clicked && ImGui.io.keyCtrl) || doubleClicked || g.navActivateInputId == id) tempInputIsActive =
true
if (makeActive && tempInputAllowed)
if (inputRequestedByTabbing || (clicked && ImGui.io.keyCtrl) || doubleClicked || g.navActivateInputId == id)
tempInputIsActive = true

// (Optional) simple click (without moving) turns Drag into an InputText
if (io.configDragClickToInputText && tempInputAllowed && !tempInputIsActive)
Expand Down Expand Up @@ -362,11 +342,8 @@ interface widgetsDrags {
if (g.logEnabled) logSetNextTextDecoration("{", "}")
ImGui.renderTextClipped(frameBb.min, frameBb.max, value, null, Vec2(0.5f))

if (labelSize.x > 0f) ImGui.renderText(
Vec2(
frameBb.max.x + ImGui.style.itemInnerSpacing.x, frameBb.min.y + ImGui.style.framePadding.y
), label
)
if (labelSize.x > 0f)
ImGui.renderText(Vec2(frameBb.max.x + ImGui.style.itemInnerSpacing.x, frameBb.min.y + ImGui.style.framePadding.y), label)

IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.lastItemData.statusFlags)
return valueChanged
Expand Down Expand Up @@ -394,7 +371,8 @@ inline fun <reified N> dragN(label: String,
format: String? = null,
flags: SliderFlags = emptyFlags,
properties: (Int) -> KMutableProperty0<N>)
: Boolean where N : Number, N : Comparable<N> = numberOps<N>().dragN(label, components, vSpeed, min, max, format, flags, properties)
: Boolean where N : Number, N : Comparable<N> =
numberOps<N>().dragN(label, components, vSpeed, min, max, format, flags, properties)

inline fun <N> NumberOps<N>.dragN(label: String,
components: Int,
Expand All @@ -404,4 +382,5 @@ inline fun <N> NumberOps<N>.dragN(label: String,
format: String? = null,
flags: SliderFlags = emptyFlags,
properties: (Int) -> KMutableProperty0<N>)
: Boolean where N : Number, N : Comparable<N> = widgetN(label, components) { i -> drag("", properties(i), vSpeed, min, max, format, flags) }
: Boolean where N : Number, N : Comparable<N> =
widgetN(label, components) { i -> drag("", properties(i), vSpeed, min, max, format, flags) }
5 changes: 2 additions & 3 deletions core/src/main/kotlin/imgui/api/widgetsInputWithKeyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ interface widgetsInputWithKeyboard {
return inputTextEx(label, hint, buf, Vec2(), flags, callback, userData)
}

fun input(label: String, v: FloatArray, step: Float = 0f, stepFast: Float = 0f, format: String = "%.3f", flags: InputTextSingleFlags = emptyFlags): Boolean = input(label, v, 0, step, stepFast, format, flags)

fun input(label: String, v: FloatArray, ptr: Int = 0, step: Float = 0f, stepFast: Float = 0f, format: String = "%.3f", flags: InputTextSingleFlags = emptyFlags): Boolean = input(label, v mutablePropertyAt ptr, step, stepFast, format, flags)
fun input(label: String, v: FloatArray, step: Float = 0f, stepFast: Float = 0f, format: String = "%.3f", flags: InputTextSingleFlags = emptyFlags): Boolean =
input(label, v mutablePropertyAt 0, step, stepFast, format, flags)

fun input(label: String, v: KMutableProperty0<Float>, step: Float = 0f, stepFast: Float = 0f, format: String = "%.3f", flags_: InputTextSingleFlags = emptyFlags): Boolean {
val flags = flags_ or Itf.CharsScientific
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/kotlin/imgui/api/widgetsSelectables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ interface widgetsSelectables {
return pressed
}

/** "bool* p_selected" point to the selection state (read-write), as a convenient helper. */
fun selectable(label: String, selected: BooleanArray, index: Int, flags: SelectableFlags = emptyFlags, size: Vec2 = Vec2()): Boolean =
selectable(label, selected mutablePropertyAt index, flags, size)

/** "bool* p_selected" point to the selection state (read-write), as a convenient helper. */
fun selectable(label: String, selectedPtr: KMutableProperty0<Boolean>, flags: SelectableFlags = emptyFlags, size: Vec2 = Vec2()): Boolean {
var selected by selectedPtr
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/kotlin/imgui/api/widgetsSliders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ interface widgetsSliders {
* "%5.2f secs" 01.23 secs
* "Gold: %.0f" Gold: 1
* adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. Use power!=1.0 for power curve sliders */
fun slider(label: String, v: FloatArray, ptr: Int, vMin: Float, vMax: Float, format: String = "%.3f", flags: SliderFlags = emptyFlags): Boolean = slider(label, v mutablePropertyAt ptr, vMin, vMax, format, flags)

fun slider2(label: String, v: FloatArray, vMin: Float, vMax: Float, format: String = "%.3f", flags: SliderFlags = emptyFlags): Boolean = sliderN(label, 2, vMin, vMax, format, flags, v::mutablePropertyAt)

Expand All @@ -56,8 +55,6 @@ interface widgetsSliders {
return slider(label, vRadPtr, vDegreesMin, vDegreesMax, format, flags).also { vRad = vRad.rad }
}

fun slider(label: String, v: IntArray, ptr: Int, vMin: Int, vMax: Int, format: String = "%d", flags: SliderFlags = emptyFlags): Boolean = slider(label, v mutablePropertyAt ptr, vMin, vMax, format, flags)

fun slider2(label: String, v: IntArray, vMin: Int, vMax: Int, format: String = "%d", flags: SliderFlags = emptyFlags): Boolean = sliderN(label, 2, vMin, vMax, format, flags, v::mutablePropertyAt)

fun slider2(label: String, v: Vec2i, vMin: Int, vMax: Int, format: String = "%d", flags: SliderFlags = emptyFlags): Boolean = sliderN(label, 2, vMin, vMax, format, flags, v::mutablePropertyAt)
Expand Down Expand Up @@ -217,13 +214,17 @@ interface widgetsSliders {

}

inline fun <reified N> slider(label: String, pData: KMutableProperty0<N>, min: N, max: N, format_: String? = null, flags: SliderFlags = emptyFlags): Boolean where N : Number, N : Comparable<N> = numberOps<N>().slider(label, pData, min, max, format_, flags)
inline fun <reified N> slider(label: String, pData: KMutableProperty0<N>, min: N, max: N, format_: String? = null, flags: SliderFlags = emptyFlags): Boolean where N : Number, N : Comparable<N> =
numberOps<N>().slider(label, pData, min, max, format_, flags)

/** Add multiple sliders on 1 line for compact edition of multiple components */
inline fun <reified N> sliderN(label: String, components: Int, min: N, max: N, format: String? = null, flags: SliderFlags = emptyFlags, properties: (Int) -> KMutableProperty0<N>): Boolean where N : Number, N : Comparable<N> = numberOps<N>().sliderN(label, components, min, max, format, flags, properties)
inline fun <reified N> sliderN(label: String, components: Int, min: N, max: N, format: String? = null, flags: SliderFlags = emptyFlags, properties: (Int) -> KMutableProperty0<N>): Boolean where N : Number, N : Comparable<N> =
numberOps<N>().sliderN(label, components, min, max, format, flags, properties)

inline fun <N> NumberOps<N>.sliderN(label: String, components: Int, min: N, max: N, format: String? = null, flags: SliderFlags = emptyFlags, properties: (Int) -> KMutableProperty0<N>): Boolean where N : Number, N : Comparable<N> = widgetN(label, components) { i ->
inline fun <N> NumberOps<N>.sliderN(label: String, components: Int, min: N, max: N, format: String? = null, flags: SliderFlags = emptyFlags, properties: (Int) -> KMutableProperty0<N>): Boolean where N : Number, N : Comparable<N> =
widgetN(label, components) { i ->
slider("", properties(i), min, max, format, flags)
}

inline fun <reified N> vSlider(label: String, size: Vec2, pData: KMutableProperty0<N>, min: N, max: N, format_: String? = null, flags: SliderFlags = emptyFlags): Boolean where N : Number, N : Comparable<N> = numberOps<N>().vSlider(label, size, pData, min, max, format_, flags)
inline fun <reified N> vSlider(label: String, size: Vec2, pData: KMutableProperty0<N>, min: N, max: N, format_: String? = null, flags: SliderFlags = emptyFlags): Boolean where N : Number, N : Comparable<N> =
numberOps<N>().vSlider(label, size, pData, min, max, format_, flags)
5 changes: 1 addition & 4 deletions core/src/main/kotlin/imgui/api/windows.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ import imgui.internal.sections.LayoutType as Lt
// - Note that the bottom of window stack always contains a window called "Debug".
interface windows {

fun begin(name: String, pOpen: BooleanArray, flags: WindowFlags): Boolean = begin(name, pOpen, 0, flags)

fun begin(name: String, pOpen: BooleanArray?, index: Int, flags: WindowFlags): Boolean =
begin(name, pOpen?.mutablePropertyAt(index), flags)
fun begin(name: String, pOpen: BooleanArray, flags: WindowFlags): Boolean = begin(name, pOpen mutablePropertyAt 0, flags)

/** Push a new Dear ImGui window to add widgets to:
- A default window called "Debug" is automatically stacked at the beginning of every frame so you can use
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/imgui/demo/ShowDemoWindowTables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ object ShowDemoWindowTables {
tableNextRow()
for (column in 0..2) {
tableSetColumnIndex(column)
selectable("Cell $column,$row", columnSelected, column)
selectable("Cell $column,$row", columnSelected mutablePropertyAt column)
}
}
}
Expand Down
Loading

0 comments on commit 2404dff

Please sign in to comment.