/
ComponentAlignments.kt
88 lines (80 loc) · 2.93 KB
/
ComponentAlignments.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.hexworks.zircon.api
import org.hexworks.zircon.api.behavior.Boundable
import org.hexworks.zircon.api.component.AlignmentStrategy
import org.hexworks.zircon.api.component.Component
import org.hexworks.zircon.api.component.ComponentAlignment
import org.hexworks.zircon.api.component.Container
import org.hexworks.zircon.api.data.Position
import org.hexworks.zircon.api.grid.TileGrid
import org.hexworks.zircon.internal.component.alignment.AroundAlignmentStrategy
import org.hexworks.zircon.internal.component.alignment.PositionalAlignmentStrategy
import org.hexworks.zircon.internal.component.alignment.WithinAlignmentStrategy
/**
* This object contains factory functions for creating [AlignmentStrategy] objects.
*/
object ComponentAlignments {
/**
* Calculates the [Position] of the resulting [Component] within
* the given [tileGrid] using the given [ComponentAlignment].
*
* E.g. `TOP_LEFT` will align this [Component] to the top left
* corner of the [tileGrid].
*/
fun alignmentWithin(
tileGrid: TileGrid,
alignmentType: ComponentAlignment
): AlignmentStrategy {
return WithinAlignmentStrategy(
other = Boundable.create(size = tileGrid.size),
alignmentType = alignmentType
)
}
/**
* Calculates the [Position] of the resulting [Component] within
* the given [container] using the given [ComponentAlignment].
*
* E.g. `TOP_LEFT` will align this [Component] to the top left
* corner of the [container].
*/
fun alignmentWithin(
container: Container,
alignmentType: ComponentAlignment
): AlignmentStrategy {
return WithinAlignmentStrategy(
other = Boundable.create(size = container.contentSize),
alignmentType = alignmentType
)
}
/**
* Calculates the [Position] of the resulting [Component] relative to
* the given [component] using the given [ComponentAlignment].
*
* E.g. `TOP_LEFT` will align this [Component] to the top left
* corner of [component].
*/
fun alignmentAround(
component: Component,
alignmentType: ComponentAlignment
): AlignmentStrategy {
return AroundAlignmentStrategy(
other = component.relativeBounds,
alignmentType = alignmentType
)
}
/**
* Creates a [AlignmentStrategy] which will align a [Component]
* within another one relative to its top left corner
* using the given [x],[y] coordinates.
*/
fun positionalAlignment(x: Int, y: Int): AlignmentStrategy {
return positionalAlignment(Position.create(x, y))
}
/**
* Creates a [AlignmentStrategy] which will align a [Component]
* within another one relative to its top left corner
* using the given [Position].
*/
fun positionalAlignment(position: Position): AlignmentStrategy {
return PositionalAlignmentStrategy(position)
}
}