-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLuaAnimTextPanel.kt
More file actions
122 lines (102 loc) · 4.07 KB
/
LuaAnimTextPanel.kt
File metadata and controls
122 lines (102 loc) · 4.07 KB
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
package me.anno.lua.ui
import me.anno.ecs.annotations.Docs
import me.anno.ecs.prefab.PrefabSaveable
import me.anno.io.base.BaseWriter
import me.anno.lua.ScriptComponent
import me.anno.lua.ScriptComponent.Companion.toLua
import me.anno.ui.anim.AnimTextPanel
import me.anno.ui.Style
import me.anno.utils.Color
import org.apache.logging.log4j.LogManager
import org.luaj.vm2.LuaString
import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.ThreeArgFunction
import org.luaj.vm2.lib.TwoArgFunction
@Docs("Char-wise animated text panel with Lua script for quick tests")
class LuaAnimTextPanel(text: String, var animation: String, style: Style) : AnimTextPanel(text, style) {
constructor(style: Style) : this("", "", style)
companion object {
@JvmStatic
private val LOGGER = LogManager.getLogger(LuaAnimTextPanel::class)
@JvmStatic
var cx = 0f
@JvmStatic
var cy = 0f
object Translate : TwoArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue): LuaValue {
translate(arg1.tofloat(), arg2.tofloat())
return NIL
}
}
object Scale : TwoArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue): LuaValue {
scale(arg1.tofloat(), arg2.tofloat())
return NIL
}
}
object Rotate : ThreeArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue, arg3: LuaValue): LuaValue {
if (arg2 == NIL) rotate(arg1.tofloat(), cx, cy)
else rotate(arg1.tofloat(), arg2.tofloat(), arg3.tofloat())
return NIL
}
}
object Hsluv : ThreeArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue, arg3: LuaValue): LuaValue {
return when {
arg2.isnil() -> hsluv(arg1.tofloat())
arg3.isnil() -> hsluv(arg1.tofloat(), arg2.tofloat())
else -> hsluv(arg1.tofloat(), arg2.tofloat(), arg3.tofloat())
}.toLua()
}
}
// cached, because LuaJ copies all string content every time otherwise
@JvmStatic
private val lTime = LuaString.valueOf("time")
@JvmStatic
private val lIndex = LuaString.valueOf("index")
@JvmStatic
private val lcx = LuaString.valueOf("cx")
@JvmStatic
private val lcy = LuaString.valueOf("cy")
}
override fun animate(time: Float, index: Int, cx: Float, cy: Float): Int {
val (globals, func) = ScriptComponent.getFunction(animation, LuaAnimTextPanel::class) { globals ->
// define all relevant properties; we could add a few helper functions and more properties later
// if you need any specific property in your project, consider writing it in Kotlin, or ask me
globals.set("translate", Translate)
globals.set("scale", Scale)
globals.set("rotate", Rotate)
globals.set("hsluv", Hsluv)
} ?: return textColor
if (!func.isfunction() && !func.isint()) LOGGER.warn("Function: $func")
if (func.isint()) return func.toint() or (textColor and Color.black)
if (func.isnil()) return textColor
globals.set(lTime, time.toLua())
globals.set(lIndex, index.toLua())
globals.set(lcx, cx.toLua())
globals.set(lcy, cy.toLua())
Companion.cx = cx
Companion.cy = cy
val ret = func.call()
if (!ret.isint()) LOGGER.warn("Return type: $ret")
return when {
ret.isint() -> ret.toint()
else -> textColor
}
}
override fun save(writer: BaseWriter) {
super.save(writer)
writer.writeString("animation", animation)
}
override fun clone(): LuaAnimTextPanel {
val clone = LuaAnimTextPanel(text, animation, style)
copyInto(clone)
return clone
}
override fun copyInto(dst: PrefabSaveable) {
super.copyInto(dst)
if (dst !is LuaAnimTextPanel) return
dst.animation = animation
}
}