/
DefaultComponent.kt
197 lines (162 loc) · 7.63 KB
/
DefaultComponent.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package org.codetome.zircon.internal.component.impl
import org.codetome.zircon.api.Position
import org.codetome.zircon.api.Size
import org.codetome.zircon.api.behavior.Boundable
import org.codetome.zircon.api.behavior.DrawSurface
import org.codetome.zircon.api.behavior.Drawable
import org.codetome.zircon.api.behavior.FontOverride
import org.codetome.zircon.api.builder.LayerBuilder
import org.codetome.zircon.api.builder.TextCharacterBuilder
import org.codetome.zircon.api.builder.TextImageBuilder
import org.codetome.zircon.api.component.ComponentState
import org.codetome.zircon.api.component.ComponentStyles
import org.codetome.zircon.api.font.Font
import org.codetome.zircon.api.graphics.TextImage
import org.codetome.zircon.api.input.MouseAction
import org.codetome.zircon.internal.behavior.impl.DefaultBoundable
import org.codetome.zircon.internal.behavior.impl.DefaultFontOverride
import org.codetome.zircon.internal.component.InternalComponent
import org.codetome.zircon.internal.component.WrappingStrategy
import org.codetome.zircon.internal.event.EventBus
import org.codetome.zircon.internal.event.EventType
import java.util.*
import java.util.function.Consumer
abstract class DefaultComponent(initialSize: Size,
initialFont: Font,
position: Position,
private var componentStyles: ComponentStyles,
private val wrappers: Iterable<WrappingStrategy>,
private val fontOverride: FontOverride = DefaultFontOverride(
initialFont = initialFont),
private val drawSurface: TextImage = TextImageBuilder.newBuilder()
.filler(TextCharacterBuilder.EMPTY)
.size(initialSize)
.build(),
private val boundable: DefaultBoundable = DefaultBoundable(
size = initialSize,
position = position))
: InternalComponent, Drawable by drawSurface, FontOverride by fontOverride {
private val id: UUID = UUID.randomUUID()
private var currentOffset = Position.DEFAULT_POSITION
init {
drawSurface.setStyleFrom(componentStyles.getCurrentStyle())
applyWrappers()
EventBus.subscribe(EventType.MouseOver(id), {
if (componentStyles.getCurrentStyle() != componentStyles.getStyleFor(ComponentState.MOUSE_OVER)) {
drawSurface.applyColorsFromStyle(componentStyles.mouseOver())
EventBus.emit(EventType.ComponentChange)
}
})
EventBus.subscribe(EventType.MouseOut(id), {
if (componentStyles.getCurrentStyle() != componentStyles.getStyleFor(ComponentState.DEFAULT)) {
drawSurface.applyColorsFromStyle(componentStyles.reset())
EventBus.emit(EventType.ComponentChange)
}
})
}
override fun setPosition(position: Position) {
boundable.moveTo(position)
}
override fun containsBoundable(boundable: Boundable) = this.boundable.containsBoundable(boundable)
override fun containsPosition(position: Position) = boundable.containsPosition(position)
override fun intersects(boundable: Boundable) = this.boundable.intersects(boundable)
override fun getId() = id
override fun getPosition() = boundable.getPosition()
override fun drawOnto(surface: DrawSurface, offset: Position) {
surface.draw(drawSurface, boundable.getPosition())
}
override fun fetchComponentByPosition(position: Position) =
if (containsPosition(position)) {
Optional.of(this)
} else {
Optional.empty<InternalComponent>()
}
override fun onMousePressed(callback: Consumer<MouseAction>) {
EventBus.subscribe<MouseAction>(EventType.MousePressed(getId()), { (mouseAction) ->
callback.accept(mouseAction)
})
}
override fun onMouseReleased(callback: Consumer<MouseAction>) {
EventBus.subscribe<MouseAction>(EventType.MouseReleased(getId()), { (mouseAction) ->
callback.accept(mouseAction)
})
}
override fun onMouseMoved(callback: Consumer<MouseAction>) {
EventBus.subscribe<MouseAction>(EventType.MouseMoved(getId()), { (mouseAction) ->
callback.accept(mouseAction)
})
}
override fun getComponentStyles() = componentStyles
override fun setComponentStyles(componentStyles: ComponentStyles) {
this.componentStyles = componentStyles
drawSurface.applyColorsFromStyle(componentStyles.getCurrentStyle(), getNonThemeableOffset(), getEffectiveThemeableSize())
}
fun getBoundable() = boundable
fun getDrawSurface() = drawSurface
/**
* Returns the size which this component takes up without its wrappers.
*/
override fun getEffectiveSize() = getBoundableSize() - getWrappersSize()
/**
* Returns the position of this component offset by the wrappers it has.
*/
override fun getEffectivePosition() = getPosition() + getWrapperOffset()
/**
* Returns the position from which themes should be applied.
*/
fun getEffectiveThemeablePosition() = getPosition() + getNonThemeableOffset()
/**
* Returns the offset which is caused by the wrappers of this component.
* So basically this is the value of the component's position (`getPosition()`)
* plus the space which is taken up by the wrappers.
*/
fun getWrapperOffset() = wrappers.map { it.getOffset() }.fold(Position.TOP_LEFT_CORNER) { acc, position -> acc + position }
open fun transformToLayers() =
listOf(LayerBuilder.newBuilder()
.textImage(drawSurface)
.offset(getPosition())
.font(getCurrentFont())
.build())
override fun getBoundableSize() = boundable.getBoundableSize()
override fun toString(): String {
return "${javaClass.simpleName}(id=${id.toString().substring(0, 4)})"
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as DefaultComponent
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
private fun applyWrappers() {
var currSize = getEffectiveSize()
currentOffset = Position.DEFAULT_POSITION
wrappers.forEach {
currSize += it.getOccupiedSize()
it.apply(drawSurface, currSize, currentOffset, componentStyles.getCurrentStyle())
currentOffset += it.getOffset()
}
}
/**
* Returns the size which this component takes up with only its themeable wrappers.
*/
private fun getEffectiveThemeableSize() = getBoundableSize() - getNonThemedWrapperSize()
/**
* Calculate the size taken by all the wrappers.
*/
private fun getWrappersSize() = wrappers.map { it.getOccupiedSize() }.fold(Size.ZERO) { acc, size -> acc + size }
/**
* Returns the size of all wrappers which are not themeable.
*/
private fun getNonThemedWrapperSize() = wrappers
.filter { it.isThemeNeutral() }
.map { it.getOccupiedSize() }
.fold(Size.ZERO) { acc, size -> acc + size }
private fun getNonThemeableOffset() = wrappers
.filter { it.isThemeNeutral() }
.map { it.getOffset() }
.fold(Position.TOP_LEFT_CORNER) { acc, position -> acc + position }
}