-
Notifications
You must be signed in to change notification settings - Fork 3
/
Drawable.kt
384 lines (346 loc) · 12.5 KB
/
Drawable.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* This file is part of PolyUI
* PolyUI - Fast and lightweight UI framework
* Copyright (C) 2023-2024 Polyfrost and its contributors.
* <https://polyfrost.org> <https://github.com/Polyfrost/polui-jvm>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* PolyUI is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation,
* AND the simple request that you adequately accredit us if you use PolyUI.
* See details here <https://github.com/Polyfrost/polyui-jvm/ACCREDITATION.md>.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>.
*/
package org.polyfrost.polyui.component
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.MustBeInvokedByOverriders
import org.polyfrost.polyui.PolyUI
import org.polyfrost.polyui.PolyUI.Companion.INPUT_NONE
import org.polyfrost.polyui.color.Colors
import org.polyfrost.polyui.color.PolyColor
import org.polyfrost.polyui.component.extensions.countChildren
import org.polyfrost.polyui.data.Framebuffer
import org.polyfrost.polyui.operations.Recolor
import org.polyfrost.polyui.renderer.FramebufferController
import org.polyfrost.polyui.unit.Align
import org.polyfrost.polyui.unit.AlignDefault
import org.polyfrost.polyui.unit.Vec2
import org.polyfrost.polyui.utils.annotations.Locking
import org.polyfrost.polyui.utils.annotations.SideEffects
import org.polyfrost.polyui.utils.fastEach
import org.polyfrost.polyui.utils.fastRemoveIfReversed
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin
/**
* # Drawable
* The most basic thing in the PolyUI rendering system.
*
* Supports recoloring, animations, and the entire event system.
*/
abstract class Drawable(
vararg children: Component? = arrayOf(),
at: Vec2 = Vec2.ZERO,
alignment: Align = AlignDefault,
size: Vec2 = Vec2.ZERO,
visibleSize: Vec2 = Vec2.ZERO,
palette: Colors.Palette? = null,
focusable: Boolean = false,
) : Cloneable, Scrollable(at, size, visibleSize, alignment, focusable) {
init {
if (children.isNotEmpty()) {
this.children = children.filterNotNullTo(ArrayList<Component>(children.size)).also { list ->
list.fastEach {
it.parent = this
}
}
} else {
this.children = null
}
}
override var visibleSize: Vec2
get() = super.visibleSize * Vec2(scaleX, scaleY)
set(value) {
super.visibleSize = value
}
inline val renderer get() = polyUI.renderer
@Locking
@set:Synchronized
var framebuffer: Framebuffer? = null
private set
/**
* internal counter for framebuffer render count
*/
private var fbc: Short = 0
/**
* internal storage for the color.
*/
@ApiStatus.Internal
@get:JvmName("getColorOrNull")
@set:JvmName("setColorInternal")
var _color: PolyColor? = null
/**
* The color of this drawable. can be any of the subtypes of this class depending on the current situation.
*
* The only safe methods here generally are getting the current color. casting it is tricky as PolyUI supports many different 'types' of color,
* from gradients to chroma. For this reason, if you want to set it, use the [Recolor] operation on this drawable as it handles the conversions for you.
*
* **setting this value is marked as experimental as it is not generally advised**.
*/
@set:ApiStatus.Internal
var color: PolyColor
get() = _color ?: throw UninitializedPropertyAccessException("Color is not initialized")
set(value) {
_color = value
}
private var _palette: Colors.Palette? = palette
@SideEffects("color", "palette")
var palette: Colors.Palette
get() = _palette ?: throw UninitializedPropertyAccessException("Palette is not initialized")
set(value) {
_palette = value
(_color as? PolyColor.Mut)?.recolor(value.get(inputState)) ?: run { _color = value.get(inputState) }
}
@SideEffects("_parent.needsRedraw", `when` = "field != value")
@get:JvmName("needsRedraw")
var needsRedraw = true
set(value) {
if (value && !field) {
(_parent as? Drawable)?.needsRedraw = true
}
field = value
}
/**
* current rotation of this drawable (radians).
*
* note: this method locks due to the fact the object needs to be translated to the center, rotated, and then translated back.
* It only locks if the value is `0.0`.
*/
@Locking(`when` = "value == 0.0")
var rotation: Double = 0.0
set(value) {
if (field == value) return
if (value == 0.0 || value == 2 * PI) {
synchronized(this) {
// lock required!
field = 0.0
}
} else {
field = (value % (2 * PI)).let { if (it < 0.0) it + (2 * PI) else it }
}
needsRedraw = true
}
/** current skew in x dimension of this drawable (radians).
*
* locking if set to `0.0`. See [rotation].
*/
@Locking(`when` = "value == 0.0")
var skewX: Double = 0.0
set(value) {
if (field == value) return
if (value == 0.0) {
synchronized(this) {
field = value
}
} else field = value
needsRedraw = true
}
/**
* current skew in y dimension of this drawable (radians).
*
* Locking if set to `0.0`. See [rotation].
*/
@Locking(`when` = "value == 0.0")
var skewY: Double = 0.0
set(value) {
if (field == value) return
if (value == 0.0) {
synchronized(this) {
field = value
}
} else field = value
needsRedraw = true
}
/** current scale in x dimension of this drawable. */
var scaleX: Float = 1f
set(value) {
if (field == value) return
field = value
needsRedraw = true
}
/** current scale in y dimension of this drawable. */
var scaleY: Float = 1f
set(value) {
if (field == value) return
field = value
needsRedraw = true
}
/**
* The alpha value of this drawable.
*
* **Note:** This value is clamped to the parent's alpha value.
* @since 0.20.0
*/
var alpha = 1f
get() = field.coerceIn(0f, (_parent as? Drawable)?.alpha ?: 1f)
/** **a**t **c**ache **x** for transformations. */
private var acx = 0f
/** **a**t **c**ache **y** for transformations. */
private var acy = 0f
@Locking
@Synchronized
override fun draw() {
if (!renders) return
require(initialized) { "Drawable $name is not initialized!" }
val renderer = renderer
val framebuffer = framebuffer
val binds = framebuffer != null && fbc < 3
if (binds) {
renderer as FramebufferController
if (!needsRedraw) {
renderer.drawFramebuffer(framebuffer!!, x, y)
return
}
renderer.bindFramebuffer(framebuffer!!)
fbc++
}
needsRedraw = false
preRender(polyUI.delta)
render()
children?.fastEach(Component::draw)
postRender()
if (fbc > 0) fbc--
if (binds) {
renderer as FramebufferController
renderer.unbindFramebuffer()
renderer.drawFramebuffer(framebuffer!!, x, y)
}
}
/**
* pre-render functions, such as applying transforms.
* In this method, you should set needsRedraw to true if you have something to redraw for the **next frame**.
*
* **make sure to call super [Drawable.preRender]!**
*/
@MustBeInvokedByOverriders
protected open fun preRender(delta: Long) {
val renderer = renderer
renderer.push()
operations?.fastEach { it.apply() }
if (pushScroll(delta, renderer)) needsRedraw = true
val r = rotation != 0.0
val skx = skewX != 0.0
val sky = skewY != 0.0
val s = scaleX != 1f || scaleY != 1f
if (r || skx || sky || s) {
val mx = x + width / 2f
val my = y + height / 2f
if (renderer.transformsWithPoint()) {
if (r) renderer.rotate(rotation, mx, my)
if (skx) renderer.skewX(skewX, mx, my)
if (sky) renderer.skewY(skewY, mx, my)
if (s) renderer.scale(scaleX, scaleY, x, y)
} else {
renderer.translate(mx, my)
if (r) renderer.rotate(rotation, 0f, 0f)
if (skx) renderer.skewX(skewX, 0f, 0f)
if (sky) renderer.skewY(skewY, 0f, 0f)
renderer.translate(-(width / 2f), -(height / 2f))
if (s) renderer.scale(scaleX, scaleY, 0f, 0f)
acx = x
acy = y
x = 0f
y = 0f
}
}
if (alpha != 1f) renderer.globalAlpha(alpha)
}
/** draw script for this drawable. */
protected abstract fun render()
/**
* Called after rendering, for functions such as removing transformations.
*
* **make sure to call super [Drawable.postRender]!**
*/
@MustBeInvokedByOverriders
protected open fun postRender() {
operations?.fastRemoveIfReversed {
it.unapply()
}
popScroll(renderer)
renderer.pop()
if (acx != 0f) {
x = acx
y = acy
acx = 0f
}
}
override fun isInside(x: Float, y: Float): Boolean {
if (rotation == 0.0) return super.isInside(x, y)
val ta = screenAt
val tx = ta.x
val ty = ta.y
val vs = this.visibleSize
val tw = vs.x
val th = vs.y
val cx = tx + tw / 2f
val cy = ty + th / 2f
val dx = x - cx
val dy = y - cy
val cosine = cos(-rotation)
val sine = sin(-rotation)
val rx = dx * cosine - dy * sine + cx
val ry = dx * sine + dy * cosine + cy
return rx in tx..(tx + tw) && ry in ty..(ty + th)
}
override fun rescale0(scaleX: Float, scaleY: Float, withChildren: Boolean) {
super.rescale0(scaleX, scaleY, withChildren)
framebuffer?.let {
val renderer = renderer as FramebufferController
renderer.delete(it)
framebuffer = renderer.createFramebuffer(width, height)
}
}
fun debugDraw() {
if (!renders) return
debugRender()
children?.fastEach {
if (!it.renders) return@fastEach
if (it is Drawable) it.debugDraw()
}
}
/** add a debug render overlay for this drawable. This is always rendered regardless of the layout re-rendering if debug mode is on. */
protected open fun debugRender() {
val color = if (inputState > INPUT_NONE) polyUI.colors.page.border10 else polyUI.colors.page.border5
val staticAt = this.screenAt
val visibleSize = this.visibleSize
renderer.hollowRect(staticAt.x, staticAt.y, visibleSize.x, visibleSize.y, color, 1f)
}
override fun setup(polyUI: PolyUI): Boolean {
if (_color == null) {
if (_palette == null) palette = polyUI.colors.component.bg
}
if (!super.setup(polyUI)) return false
if (polyUI.canUseFramebuffers) {
if (countChildren() > polyUI.settings.minDrawablesForFramebuffer || (this === polyUI.master && polyUI.settings.isMasterFrameBuffer)) {
val renderer = renderer as FramebufferController
framebuffer = renderer.createFramebuffer(width, height)
if (polyUI.settings.debug) PolyUI.LOGGER.info("Drawable ${this.name} created with $framebuffer")
}
}
return true
}
/**
* Implement this function to enable cloning of your Drawable.
* @since 0.19.0
*/
public override fun clone() = (super.clone() as Drawable)
override operator fun get(index: Int) = super.get(index) as? Drawable ?: throw IllegalArgumentException("Object at $index on $this is not a Drawable")
}