-
Notifications
You must be signed in to change notification settings - Fork 30
/
PlotComponent.kt
179 lines (150 loc) · 6.88 KB
/
PlotComponent.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
package gg.essential.elementa.components.plot
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIContainer
import gg.essential.elementa.components.UIText
import gg.essential.elementa.constraints.*
import gg.essential.elementa.dsl.*
import gg.essential.universal.UMatrixStack
class PlotComponent(
private val points: List<PlotPoint>,
private val xBounds: Bounds = Bounds.fromPoints(points.map { it.x }),
private val yBounds: Bounds = Bounds.fromPoints(points.map { it.y }),
private val style: PlotStyle = PlotStyle()
) : UIComponent() {
private val container = UIContainer().constrain {
x = style.padding.left.pixels()
y = style.padding.top.pixels()
width = RelativeConstraint() - (style.padding.left + style.padding.right).pixels()
height = RelativeConstraint() - (style.padding.top + style.padding.bottom).pixels()
} childOf this
private val xLabelContainer = UIContainer()
private val yLabelContainer = UIContainer()
private var drawWidth = -1f
private var drawHeight = -1f
private var drawLeft = -1f
private var drawTop = -1f
init {
if (points.groupBy { it.x }.any { it.value.size > 1 })
throw IllegalArgumentException("The point list contains two points that share the same x value")
if (xBounds.numberOfGridLines != 0 && xBounds.numberOfGridLines < 2)
throw IllegalArgumentException("xBounds.numberOfGridLines lines must be either 0 or >= 2")
if (yBounds.numberOfGridLines != 0 && yBounds.numberOfGridLines < 2)
throw IllegalArgumentException("yBounds.numberOfGridLines lines must be either 0 or >= 2")
constrain {
width = 100.percent()
height = 100.percent()
}
when {
xBounds.showLabels && !yBounds.showLabels -> {
xLabelContainer.constrain {
y = 0.pixels(alignOpposite = true)
width = RelativeConstraint()
height = ChildBasedMaxSizeConstraint()
} childOf container
}
!xBounds.showLabels && yBounds.showLabels -> {
yLabelContainer.constrain {
width = ChildBasedMaxSizeConstraint() + 5.pixels()
height = RelativeConstraint()
} childOf container
}
xBounds.showLabels && yBounds.showLabels -> {
yLabelContainer.constrain {
width = ChildBasedMaxSizeConstraint() + 5.pixels()
height = basicHeightConstraint { container.getHeight() - xLabelContainer.getHeight() }
} childOf container
xLabelContainer.constrain {
x = basicXConstraint { yLabelContainer.getRight() }
y = 0.pixels(alignOpposite = true)
width = basicWidthConstraint { container.getWidth() - yLabelContainer.getWidth() }
height = ChildBasedMaxSizeConstraint()
} childOf container
}
}
if (yBounds.showLabels) {
repeat(yBounds.numberOfGridLines) { index ->
val percentage = index.toFloat() / (yBounds.numberOfGridLines - 1)
val number = (1f - percentage) * yBounds.range + yBounds.min
UIText(yBounds.labelToString(number)).constrain {
x = CenterConstraint()
y = RelativeConstraint(percentage) - (percentage * 3f).pixels()
width = TextAspectConstraint()
height = 6.pixels()
color = yBounds.labelColor.toConstraint()
} childOf yLabelContainer
}
}
if (xBounds.showLabels) {
repeat(xBounds.numberOfGridLines) { index ->
val percentage = (1f - index.toFloat() / (xBounds.numberOfGridLines - 1))
val number = percentage * xBounds.range + xBounds.min
val text = xBounds.labelToString(number)
val textComponent = UIText(text)
textComponent.constrain {
x = basicXConstraint {
val width = container.getWidth() - yLabelContainer.getWidth()
width * percentage + yLabelContainer.getRight() - textComponent.getWidth() / 2f + 2f
}
width = TextAspectConstraint()
height = 6.pixels()
color = xBounds.labelColor.toConstraint()
} childOf xLabelContainer
}
}
}
private fun drawGrid(bounds: Bounds, isX: Boolean) {
if (bounds.numberOfGridLines == 0)
return
val values = (0 until bounds.numberOfGridLines).map {
bounds.min + bounds.range * (it.toFloat() / (bounds.numberOfGridLines - 1))
}
values.forEach {
val (start, end) = if (isX) {
PlotPoint(it, yBounds.min) to PlotPoint(it, yBounds.max)
} else {
PlotPoint(xBounds.min, it) to PlotPoint(xBounds.max, it)
}.let { (a, b) -> transformPoint(a) to transformPoint(b) }
style.gridStyle.type.draw(listOf(start, end), style.gridStyle.color, style.gridStyle.width)
}
}
private fun drawPoints() {
style.pointStyle.type.draw(points.map(::transformPoint), style)
}
private fun drawLines() {
style.lineStyle.type.draw(
points.map(::transformPoint),
style.lineStyle.color,
style.lineStyle.width
)
}
private fun transformPoint(point: PlotPoint): PlotPoint {
val xPercent = (point.x - xBounds.min) / xBounds.range
val yPercent = (point.y - yBounds.min) / yBounds.range
val newX = drawLeft + xPercent * drawWidth
val newY = drawTop + yPercent * drawHeight
return PlotPoint(newX, newY)
}
override fun draw(matrixStack: UMatrixStack) {
super.draw(matrixStack)
drawWidth = if (yBounds.showLabels) {
container.getWidth() - yLabelContainer.getWidth()
} else container.getWidth()
drawHeight = when {
xBounds.showLabels && yBounds.showLabels -> container.getHeight() - xLabelContainer.getHeight() - 6f
xBounds.showLabels -> container.getHeight() - xLabelContainer.getHeight() - 3f
yBounds.showLabels -> container.getHeight() - 3f
else -> container.getHeight()
}
drawLeft = if (yBounds.showLabels) {
yLabelContainer.getRight()
} else container.getLeft()
drawTop = container.getTop() + if (yBounds.showLabels) 3f else 0f
// TODO propagate matrix stack and switch to line shader for 1.17
matrixStack.runWithGlobalState {
drawGrid(xBounds, isX = true)
drawGrid(yBounds, isX = false)
drawPoints()
drawLines()
}
}
}