Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to center camera messages #775

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/main/kotlin/graphics/scenery/Camera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,60 @@
*
* It will be shown for [duration] milliseconds, with a default of 3000.
*/
@JvmOverloads fun showMessage(message: String, distance: Float = 0.75f, size: Float = 0.05f, messageColor: Vector4f = Vector4f(1.0f), backgroundColor: Vector4f = Vector4f(0.0f), duration: Int = 3000) {
@JvmOverloads
fun showMessage(

Check warning on line 340 in src/main/kotlin/graphics/scenery/Camera.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/kotlin/graphics/scenery/Camera.kt#L340

The function showMessage(message: String, distance: Float, size: Float, messageColor: Vector4f, backgroundColor: Vector4f, duration: Int, centered: Boolean) has too many parameters. The current threshold is set to 6.
message: String,
distance: Float = 0.75f,
size: Float = 0.05f,
messageColor: Vector4f = Vector4f(1.0f),
backgroundColor: Vector4f = Vector4f(0.0f),
duration: Int = 3000,
centered: Boolean = false
) {
val tb = TextBoard()
tb.fontColor = messageColor
tb.backgroundColor = backgroundColor
tb.text = message
var textGeom = tb.geometry().vertices

// get biggest X value, then move the text board along negative X to center it
fun centerMessage() {
if (textGeom != tb.geometry().vertices) {
val bv = tb.geometry().vertices.duplicate().clear()
var maxX = 0f
while (bv.hasRemaining()) {
maxX = java.lang.Float.max(bv.get(), maxX)
bv.get() // skip Y
bv.get() // skip Z
}
maxX *= tb.spatial().scale.x

tb.spatial {
position.x -= maxX / 2f
needsUpdate = true
}
textGeom = tb.geometry().vertices
}
}

tb.spatial {
scale = Vector3f(size, size, size)
position = Vector3f(0.0f, 0.0f, -1.0f * distance)
}
if (centered) {
centerMessage()
}

@Suppress("UNCHECKED_CAST")
val messages = metadata.getOrPut("messages", { mutableListOf<Node>() }) as? MutableList<Node>?
messages?.forEach { this.removeChild(it) }
messages?.clear()

messages?.add(tb)

this.addChild(tb)
if (centered) {
this.update += { centerMessage() }
}

thread {
Thread.sleep(duration.toLong())
Expand Down
Loading