Skip to content

Commit

Permalink
Fixed #616: Support multiple newlines in Text
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 18, 2023
1 parent 28b62e0 commit 93accaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions indigo/indigo/src/main/scala/indigo/shared/BoundaryLocator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ final class BoundaryLocator(

def textLineBounds(lineText: String, fontInfo: FontInfo): Rectangle =
QuickCache(s"""textline-${fontInfo.fontKey}-$lineText""") {
lineText
.toCharArray()
.map(c => fontInfo.findByCharacter(c).bounds)
.foldLeft(Rectangle.zero) { (acc, curr) =>
Rectangle(0, 0, acc.width + curr.width, Math.max(acc.height, curr.height))
}
if lineText.isEmpty then {
val b = fontInfo.findByCharacter(' ').bounds
b.withSize(Size(0, b.height))
} else {
lineText
.toCharArray()
.map(c => fontInfo.findByCharacter(c).bounds)
.foldLeft(Rectangle.zero) { (acc, curr) =>
Rectangle(0, 0, acc.width + curr.width, Math.max(acc.height, curr.height))
}
}
}

def textAsLinesWithBounds(text: String, fontKey: FontKey): Batch[TextLine] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val viewportHeight: Int = gameHeight * magnificationLevel // 256

def initialScene(bootData: SandboxBootData): Option[SceneName] =
Some(LineReflectionScene.name)
Some(OriginalScene.name)

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
NonEmptyList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ object SandboxView:
Batch(
Text("AB!\n!C", 2, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignLeft,
Text("AB!\n!C", 100, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignCenter,
Text("AB!\n!C", 200, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignRight
Text("AB!\n\n!C", 200, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignRight
.withEventHandler {
case (txt, MouseEvent.Click(pt)) if bl.bounds(txt).contains(pt) =>
println("Clicked me!")
Expand Down

0 comments on commit 93accaa

Please sign in to comment.