-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
128 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/shared/src/main/scala/eu/joaocosta/interim/api/LayoutAllocator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package eu.joaocosta.interim.api | ||
|
||
import eu.joaocosta.interim.{Font, Rect, TextLayout} | ||
|
||
/** A layout allocator is a side-effectful function that, given a size (width or height) tries to allocate | ||
* a new area. | ||
* | ||
* Note that calls to this function are side effectful, as each call reserves an area. | ||
*/ | ||
trait LayoutAllocator extends (Int => Rect): | ||
def allocate(size: Int): Rect | ||
def apply(size: Int): Rect = allocate(size) | ||
def allocate(text: String, font: Font): Rect | ||
|
||
object LayoutAllocator: | ||
final class RowAllocator(area: Rect, padding: Int) extends LayoutAllocator: | ||
private var currentY = area.y | ||
private var currentH = area.h | ||
|
||
def allocate(height: Int) = | ||
val absHeight = math.abs(height).toInt | ||
if (absHeight == 0 || currentH <= 0) // Empty | ||
area.copy(y = currentY, h = 0) | ||
else if (absHeight >= currentH) // Fill remaining area | ||
val areaY = currentY | ||
val areaH = currentH | ||
currentY = area.h | ||
currentH = 0 | ||
area.copy(y = areaY, h = areaH) | ||
else if (height >= 0) // Fill from the top | ||
val areaY = currentY | ||
currentY += absHeight + padding | ||
currentH -= absHeight + padding | ||
area.copy(y = areaY, h = absHeight) | ||
else // Fill from the bottom | ||
val areaY = currentY + currentH - absHeight | ||
currentH -= absHeight + padding | ||
area.copy(y = areaY, h = absHeight) | ||
|
||
def allocate(text: String, font: Font): Rect = | ||
val textArea = TextLayout.computeArea(area, text, font, (font.fontSize * 1.3).toInt) | ||
allocate(textArea.h) | ||
|
||
final class ColumnAllocator(area: Rect, padding: Int) extends LayoutAllocator: | ||
private var currentX = area.x | ||
private var currentW = area.w | ||
|
||
def allocate(width: Int): Rect = | ||
val absWidth = math.abs(width).toInt | ||
if (absWidth == 0 || currentW <= 0) // Empty | ||
area.copy(x = currentX, w = 0) | ||
else if (absWidth >= currentW) // Fill remaining area | ||
val areaX = currentX | ||
val areaW = currentW | ||
currentX = area.w | ||
currentW = 0 | ||
area.copy(x = areaX, w = areaW) | ||
else if (width >= 0) // Fill from the left | ||
val areaX = currentX | ||
currentX += absWidth + padding | ||
currentW -= absWidth + padding | ||
area.copy(x = areaX, w = absWidth) | ||
else // Fill from the right | ||
val areaX = currentX + currentW - absWidth | ||
currentW -= absWidth + padding | ||
area.copy(x = areaX, w = absWidth) | ||
|
||
def allocate(text: String, font: Font): Rect = | ||
val textArea = TextLayout.computeArea(area, text, font, (font.fontSize * 1.3).toInt) | ||
allocate(textArea.w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/shared/src/main/scala/eu/joaocosta/interim/api/Panels.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
core/shared/src/main/scala/eu/joaocosta/interim/skins/ButtonSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/shared/src/main/scala/eu/joaocosta/interim/skins/CheckboxSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/shared/src/main/scala/eu/joaocosta/interim/skins/HandleSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/shared/src/main/scala/eu/joaocosta/interim/skins/SliderSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/shared/src/main/scala/eu/joaocosta/interim/skins/TextInputSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
core/shared/src/main/scala/eu/joaocosta/interim/skins/WindowSkin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters