Skip to content

Commit

Permalink
- layout
Browse files Browse the repository at this point in the history
- moved `Text Filter()` after `"Disable block"`
- switched `packedChars` to Array<>
- fixed bug in `buildWithStbTrueType`, forgot to slice properly (didn't drop the tail)
  • Loading branch information
elect86 committed Aug 18, 2023
1 parent 6e3f1d7 commit cd4f936
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 54 deletions.
50 changes: 23 additions & 27 deletions core/src/main/kotlin/imgui/demo/ShowDemoWindowLayout.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@file:OptIn(ExperimentalStdlibApi::class)

package imgui.demo

import glm_.has
import glm_.L
import glm_.f
import glm_.has
import glm_.i
import glm_.vec2.Vec2
import glm_.vec4.Vec4
Expand Down Expand Up @@ -44,7 +46,6 @@ import imgui.ImGui.invisibleButton
import imgui.ImGui.io
import imgui.ImGui.isDragging
import imgui.ImGui.isItemActive
import imgui.ImGui.isItemHovered
import imgui.ImGui.itemRectMax
import imgui.ImGui.itemRectSize
import imgui.ImGui.listBox
Expand Down Expand Up @@ -73,7 +74,6 @@ import imgui.ImGui.setScrollFromPosX
import imgui.ImGui.setScrollFromPosY
import imgui.ImGui.setScrollHereX
import imgui.ImGui.setScrollHereY
import imgui.ImGui.setTooltip
import imgui.ImGui.smallButton
import imgui.ImGui.spacing
import imgui.ImGui.style
Expand Down Expand Up @@ -122,8 +122,8 @@ object ShowDemoWindowLayout {
treeNode("Groups") {

helpMarker("BeginGroup() basically locks the horizontal position for new line. " +
"EndGroup() bundles the whole group so that you can use \"item\" functions such as " +
"IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group.")
"EndGroup() bundles the whole group so that you can use \"item\" functions such as " +
"IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group.")
beginGroup()
group {
button("AAA")
Expand Down Expand Up @@ -164,9 +164,8 @@ object ShowDemoWindowLayout {

run {
bulletText("Text baseline:")
sameLine(); helpMarker(
"This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. " +
"Lines only composed of text or \"small\" widgets use less vertical space than lines with framed widgets.")
sameLine(); helpMarker("This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. " +
"Lines only composed of text or \"small\" widgets use less vertical space than lines with framed widgets.")
indent {

text("KO Blahblah"); sameLine()
Expand Down Expand Up @@ -607,11 +606,10 @@ object ShowDemoWindowLayout {

// Horizontal scroll functions
spacing()
helpMarker(
"Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n" +
"Because the clipping rectangle of most window hides half worth of WindowPadding on the " +
"left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the " +
"equivalent SetScrollFromPosY(+1) wouldn't.")
helpMarker("Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n" +
"Because the clipping rectangle of most window hides half worth of WindowPadding on the " +
"left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the " +
"equivalent SetScrollFromPosY(+1) wouldn't.")
pushID("##HorizontalScrolling")
for (i in 0..4) {
val childHeight = textLineHeight + style.scrollbarSize + style.windowPadding.y * 2f
Expand Down Expand Up @@ -641,9 +639,8 @@ object ShowDemoWindowLayout {

// Miscellaneous Horizontal Scrolling Demo

helpMarker(
"Horizontal scrolling for a window is enabled via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\n" +
"You may want to also explicitly specify content width by using SetNextWindowContentWidth() before Begin().")
helpMarker("Horizontal scrolling for a window is enabled via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\n" +
"You may want to also explicitly specify content width by using SetNextWindowContentWidth() before Begin().")
slider("Lines", ::lines, 1, 15)
pushStyleVar(StyleVar.FrameRounding, 3f)
pushStyleVar(StyleVar.FramePadding, Vec2(2f, 1f))
Expand All @@ -655,7 +652,7 @@ object ShowDemoWindowLayout {
// the cursor position yourself, aka using SetCursorPos/SetCursorScreenPos to position the widgets
// yourself. You may also want to use the lower-level ImDrawList API.
val numButtons = 10 + (line * if (line has 1) 9 else 3)
for (n in 0 until numButtons) {
for (n in 0..<numButtons) {
if (n > 0) sameLine()
pushID(n + line * 1000)
val label = if (n % 15 == 0) "FizzBuzz" else if (n % 3 == 0) "Fizz" else if (n % 5 == 0) "Buzz" else "$n"
Expand Down Expand Up @@ -780,16 +777,15 @@ object ShowDemoWindowLayout {
drag2("size", size, 0.5f, 1f, 200f, "%.0f")
textWrapped("(Click and drag to scroll)")

helpMarker(
"(Left) Using ImGui::PushClipRect():\n" +
"Will alter ImGui hit-testing logic + ImDrawList rendering.\n" +
"(use this if you want your clipping rectangle to affect interactions)\n\n" +
"(Center) Using ImDrawList::PushClipRect():\n" +
"Will alter ImDrawList rendering only.\n" +
"(use this as a shortcut if you are only using ImDrawList calls)\n\n" +
"(Right) Using ImDrawList::AddText() with a fine ClipRect:\n" +
"Will alter only this specific ImDrawList::AddText() rendering.\n" +
"This is often used internally to avoid altering the clipping rectangle and minimize draw calls.")
helpMarker("(Left) Using ImGui::PushClipRect():\n" +
"Will alter ImGui hit-testing logic + ImDrawList rendering.\n" +
"(use this if you want your clipping rectangle to affect interactions)\n\n" +
"(Center) Using ImDrawList::PushClipRect():\n" +
"Will alter ImDrawList rendering only.\n" +
"(use this as a shortcut if you are only using ImDrawList calls)\n\n" +
"(Right) Using ImDrawList::AddText() with a fine ClipRect:\n" +
"Will alter only this specific ImDrawList::AddText() rendering.\n" +
"This is often used internally to avoid altering the clipping rectangle and minimize draw calls.")

for (n in 0..2) {
if (n > 0)
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/kotlin/imgui/demo/ShowDemoWindowWidgets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ object ShowDemoWindowWidgets {
`Drag and Drop`()
`Querying Item Status (Edited,Active,Hovered etc)`()
`Querying Window Status (Focused-Hovered etc,)`()
`Text Filter`()

// Demonstrate BeginDisabled/EndDisabled using a checkbox located at the bottom of the section (which is a bit odd:
// logically we'd have this checkbox at the top of the section, but we don't want this feature to steal that space)
Expand All @@ -250,6 +249,8 @@ object ShowDemoWindowWidgets {
checkbox("Disable entire section above", ::disableAll)
sameLine(); helpMarker("Demonstrate using BeginDisabled()/EndDisabled() across this section.")
}

`Text Filter`()
}

object Basic {
Expand Down Expand Up @@ -700,6 +701,7 @@ object ShowDemoWindowWidgets {
"Below we are displaying the font texture (which is the only texture we have access to in this demo). " +
"Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. " +
"Hover the texture for a zoomed view!")

// Below we are displaying the font texture because it is the only texture we have access to inside the demo!
// Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that
// will be passed to the rendering backend via the ImDrawCmd structure.
Expand Down
44 changes: 22 additions & 22 deletions core/src/main/kotlin/imgui/font/FontAtlas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class FontAtlas {
}

fun addFontFromMemoryTTF(fontData: CharArray, sizePixels: Float, fontCfg: FontConfig = FontConfig(), glyphRanges: Array<IntRange> = emptyArray()): Font =
addFontFromMemoryTTF(ByteArray(fontData.size) { fontData[it].b }, sizePixels, fontCfg, glyphRanges)
addFontFromMemoryTTF(ByteArray(fontData.size) { fontData[it].b }, sizePixels, fontCfg, glyphRanges)

/** @param compressedFontData still owned by caller. Compress with binary_to_compressed_c.cpp. */
fun addFontFromMemoryCompressedTTF(compressedFontData: CharArray, sizePixels: Float, fontCfg: FontConfig = FontConfig(), glyphRanges: Array<IntRange> = emptyArray()): Font {
Expand Down Expand Up @@ -205,10 +205,10 @@ class FontAtlas {
// using a hot-reloading scheme that messes up static data, store your own instance of ImFontBuilderIO somewhere
// and point to it instead of pointing directly to return value of the GetBuilderXXX functions.
val builderIo = fontBuilderIO ?:
// #ifdef IMGUI_ENABLE_FREETYPE
// TODO builderIo = ImGuiFreeType::GetBuilderForFreeType()
// #elif defined(IMGUI_ENABLE_STB_TRUETYPE)
getBuilderForStbTruetype()
// #ifdef IMGUI_ENABLE_FREETYPE
// TODO builderIo = ImGuiFreeType::GetBuilderForFreeType()
// #elif defined(IMGUI_ENABLE_STB_TRUETYPE)
getBuilderForStbTruetype()
// #else
// IM_ASSERT(0) // Invalid Build function

Expand Down Expand Up @@ -538,7 +538,7 @@ class FontAtlas {
var rects: Array<rectpack.Rect> = emptyArray()

/** Output glyphs */
var packedChars: List<PackedChar> = emptyList()
var packedChars: Array<PackedChar> = emptyArray()

/** Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF) */
lateinit var srcRanges: Array<IntRange>
Expand Down Expand Up @@ -694,8 +694,8 @@ class FontAtlas {
if (srcTmp.glyphsCount == 0)
continue

srcTmp.rects = bufRects.drop(bufRectsOutN).toTypedArray()
srcTmp.packedChars = bufPackedchars.drop(bufPackedcharsOutN)
srcTmp.rects = bufRects.copyOfRange(bufRectsOutN, bufRectsOutN + srcTmp.glyphsCount)
srcTmp.packedChars = bufPackedchars.copyOfRange(bufPackedcharsOutN, bufPackedcharsOutN + srcTmp.glyphsCount)
bufRectsOutN += srcTmp.glyphsCount
bufPackedcharsOutN += srcTmp.glyphsCount

Expand All @@ -706,7 +706,7 @@ class FontAtlas {
firstUnicodeCodepointInRange = 0
arrayOfUnicodeCodepoints = srcTmp.glyphsList.toIntArray()
numChars = srcTmp.glyphsList.size
chardataForRange = ArrayList(srcTmp.packedChars)
chardataForRange = srcTmp.packedChars
hOversample = cfg.oversample.x.ui
vOversample = cfg.oversample.y.ui
}
Expand Down Expand Up @@ -763,7 +763,7 @@ class FontAtlas {

// Extend texture height and mark missing glyphs as non-packed so we won't render them.
// FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?)
for (glyphIdx in 0 until srcTmp.glyphsCount)
for (glyphIdx in 0..<srcTmp.glyphsCount)
if (srcTmp.rects[glyphIdx].wasPacked != 0)
texSize.y = texSize.y max (srcTmp.rects[glyphIdx].y + srcTmp.rects[glyphIdx].h)
}
Expand Down Expand Up @@ -832,7 +832,7 @@ class FontAtlas {
val q = AlignedQuad()
getPackedQuad(srcTmp.packedChars, texSize.x, texSize.y, glyphIdx, q = q)
dstFont.addGlyph(cfg, codepoint, q.x0 + fontOff.x, q.y0 + fontOff.y,
q.x1 + fontOff.x, q.y1 + fontOff.y, q.s0, q.t0, q.s1, q.t1, pc.xAdvance)
q.x1 + fontOff.x, q.y1 + fontOff.y, q.s0, q.t0, q.s1, q.t1, pc.xAdvance)
}
}
// bufPackedchars.free()
Expand Down Expand Up @@ -928,7 +928,7 @@ class FontAtlas {
val uv1 = Vec2()
calcCustomRectUV(r, uv0, uv1)
font.addGlyph(null, r.glyphID, r.glyphOffset.x, r.glyphOffset.y, r.glyphOffset.x + r.width, r.glyphOffset.y + r.height,
uv0.x, uv0.y, uv1.x, uv1.y, r.glyphAdvanceX)
uv0.x, uv0.y, uv1.x, uv1.y, r.glyphAdvanceX)
}
// Build all fonts lookup tables
fonts.filter { it.dirtyLookupTables }.forEach { it.buildLookupTable() }
Expand Down Expand Up @@ -1113,15 +1113,15 @@ class FontAtlas {
}

val cursorDatas = arrayOf(
// Pos ........ Size ......... Offset ......
arrayOf(Vec2(0, 3), Vec2(12, 19), Vec2(0)), // MouseCursor.Arrow
arrayOf(Vec2(13, 0), Vec2(7, 16), Vec2(1, 8)), // MouseCursor.TextInput
arrayOf(Vec2(31, 0), Vec2(23), Vec2(11)), // MouseCursor.Move
arrayOf(Vec2(21, 0), Vec2(9, 23), Vec2(4, 11)), // MouseCursor.ResizeNS
arrayOf(Vec2(55, 18), Vec2(23, 9), Vec2(11, 4)), // MouseCursor.ResizeEW
arrayOf(Vec2(73, 0), Vec2(17), Vec2(8)), // MouseCursor.ResizeNESW
arrayOf(Vec2(55, 0), Vec2(17), Vec2(8)), // MouseCursor.ResizeNWSE
arrayOf(Vec2(91, 0), Vec2(17, 22), Vec2(5, 0)), // ImGuiMouseCursor_Hand
arrayOf(Vec2(109, 0), Vec2(13, 15), Vec2(6, 7))) // ImGuiMouseCursor_NotAllowed
// Pos ........ Size ......... Offset ......
arrayOf(Vec2(0, 3), Vec2(12, 19), Vec2(0)), // MouseCursor.Arrow
arrayOf(Vec2(13, 0), Vec2(7, 16), Vec2(1, 8)), // MouseCursor.TextInput
arrayOf(Vec2(31, 0), Vec2(23), Vec2(11)), // MouseCursor.Move
arrayOf(Vec2(21, 0), Vec2(9, 23), Vec2(4, 11)), // MouseCursor.ResizeNS
arrayOf(Vec2(55, 18), Vec2(23, 9), Vec2(11, 4)), // MouseCursor.ResizeEW
arrayOf(Vec2(73, 0), Vec2(17), Vec2(8)), // MouseCursor.ResizeNESW
arrayOf(Vec2(55, 0), Vec2(17), Vec2(8)), // MouseCursor.ResizeNWSE
arrayOf(Vec2(91, 0), Vec2(17, 22), Vec2(5, 0)), // ImGuiMouseCursor_Hand
arrayOf(Vec2(109, 0), Vec2(13, 15), Vec2(6, 7))) // ImGuiMouseCursor_NotAllowed
}
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/imgui/stb_/new texture baking api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PackRange {
var firstUnicodeCodepointInRange = 0 // if non-zero, then the chars are continuous, and this is the first codepoint
var arrayOfUnicodeCodepoints: IntArray? = null // if non-zero, then this is an array of unicode codepoints
var numChars = 0
var chardataForRange: ArrayList<PackedChar> = ArrayList() // output
var chardataForRange: Array<PackedChar> = emptyArray() // output

// don't set these, they're used internally
var hOversample = 0u
Expand Down Expand Up @@ -146,7 +146,7 @@ fun PackContext.packSetOversampling(hOversample: UInt, vOversample: UInt) {
// spc->skip_missing = skip;
//}

fun getPackedQuad(charData: List<PackedChar>, pw: Int, ph: Int, // same data as above
fun getPackedQuad(charData: Array<PackedChar>, pw: Int, ph: Int, // same data as above
charIndex: Int, // character to display
pos: Vec2 = Vec2(), // pointers to current position in screen pixel space
q: AlignedQuad, // output: quad to draw
Expand Down
3 changes: 1 addition & 2 deletions core/src/test/kotlin/fontAtlasTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import com.aayushatharva.brotli4j.Brotli4jLoader
import com.aayushatharva.brotli4j.decoder.Decoder
import com.aayushatharva.brotli4j.decoder.DecoderJNI
Expand Down Expand Up @@ -33,7 +32,7 @@ class fontAtlasTest {
val texPixelsAlpha8 = Decoder.decompress(value.toByteArray()).run {
check(resultStatus == DecoderJNI.Status.DONE)
decompressedData
}
}

for (j in atlas.texPixelsAlpha8!!.indices) {
if (texPixelsAlpha8[j] != atlas.texPixelsAlpha8!![j])
Expand Down

0 comments on commit cd4f936

Please sign in to comment.