-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplifying and commonising container code
- Loading branch information
1 parent
0ee9408
commit 1b46566
Showing
14 changed files
with
219 additions
and
288 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
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
2 changes: 1 addition & 1 deletion
2
...ui/components/group/ComponentLayout.scala → ...i/components/common/ComponentLayout.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
58 changes: 57 additions & 1 deletion
58
...rkit/src/main/scala/roguelikestarterkit/ui/components/common/ContainerLikeFunctions.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 |
---|---|---|
@@ -1,10 +1,66 @@ | ||
package roguelikestarterkit.ui.components.common | ||
|
||
import roguelikestarterkit.ui.components.group.Padding | ||
import indigo.* | ||
import roguelikestarterkit.ui.component.ComponentFragment | ||
import roguelikestarterkit.ui.datatypes.Bounds | ||
import roguelikestarterkit.ui.datatypes.Coords | ||
import roguelikestarterkit.ui.datatypes.UiContext | ||
|
||
object ContainerLikeFunctions: | ||
|
||
extension (b: Bounds) | ||
def withPadding(p: Padding): Bounds = | ||
b.moveBy(p.left, p.top).resize(b.width + p.right, b.height + p.bottom) | ||
|
||
def calculateNextOffset[ReferenceData](bounds: Bounds, layout: ComponentLayout)( | ||
reference: ReferenceData, | ||
components: Batch[ComponentEntry[?, ReferenceData]] | ||
): Coords = | ||
layout match | ||
case ComponentLayout.Horizontal(padding, Overflow.Hidden) => | ||
components | ||
.takeRight(1) | ||
.headOption | ||
.map(c => | ||
c.offset + Coords(c.component.bounds(reference, c.model).withPadding(padding).right, 0) | ||
) | ||
.getOrElse(Coords(padding.left, padding.top)) | ||
|
||
case ComponentLayout.Horizontal(padding, Overflow.Wrap) => | ||
val maxY = components | ||
.map(c => c.offset.y + c.component.bounds(reference, c.model).withPadding(padding).height) | ||
.sortWith(_ > _) | ||
.headOption | ||
.getOrElse(0) | ||
|
||
components | ||
.takeRight(1) | ||
.headOption | ||
.map { c => | ||
val padded = c.component.bounds(reference, c.model).withPadding(padding) | ||
val maybeOffset = c.offset + Coords(padded.right, 0) | ||
|
||
if padded.moveBy(maybeOffset).right < bounds.width then maybeOffset | ||
else Coords(padding.left, maxY) | ||
} | ||
.getOrElse(Coords(padding.left, padding.top)) | ||
|
||
case ComponentLayout.Vertical(padding) => | ||
components | ||
.takeRight(1) | ||
.headOption | ||
.map(c => | ||
c.offset + Coords(0, c.component.bounds(reference, c.model).withPadding(padding).bottom) | ||
) | ||
.getOrElse(Coords(padding.left, padding.top)) | ||
|
||
def present[ReferenceData]( | ||
context: UiContext[ReferenceData], | ||
components: Batch[ComponentEntry[?, ReferenceData]] | ||
): Outcome[ComponentFragment] = | ||
components | ||
.map { c => | ||
c.component.present(context.copy(bounds = context.bounds.moveBy(c.offset)), c.model) | ||
} | ||
.sequence | ||
.map(_.foldLeft(ComponentFragment.empty)(_ |+| _)) |
2 changes: 1 addition & 1 deletion
2
...terkit/ui/components/group/Overflow.scala → ...erkit/ui/components/common/Overflow.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
2 changes: 1 addition & 1 deletion
2
...rterkit/ui/components/group/Padding.scala → ...terkit/ui/components/common/Padding.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
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
Oops, something went wrong.