Skip to content

Commit

Permalink
Added world position to the entity info widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanVandenBosch committed Jul 14, 2024
1 parent 62a49b0 commit 22d0177
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ class TranslateEntityCommand(
private val oldSection: Int?,
private val newPosition: Vector3,
private val oldPosition: Vector3,
private val world: Boolean,
) : Command {
override val description: String = "Move ${entity.type.simpleName}"

override fun execute() {
questEditorStore.setEntityPosition(entity, newSection, newPosition)
if (world) {
questEditorStore.setEntityWorldPosition(entity, newSection, newPosition)
} else {
questEditorStore.setEntityPosition(entity, newSection, newPosition)
}
}

override fun undo() {
questEditorStore.setEntityPosition(entity, oldSection, oldPosition)
if (world) {
questEditorStore.setEntityWorldPosition(entity, oldSection, oldPosition)
} else {
questEditorStore.setEntityPosition(entity, oldSection, oldPosition)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ class EntityInfoController(
val posY: Cell<Double> = pos.map { it.y }
val posZ: Cell<Double> = pos.map { it.z }

private val worldPos: Cell<Vector3> =
questEditorStore.selectedEntity.flatMap { it?.worldPosition ?: DEFAULT_POSITION }
val worldPosX: Cell<Double> = worldPos.map { it.x }
val worldPosY: Cell<Double> = worldPos.map { it.y }
val worldPosZ: Cell<Double> = worldPos.map { it.z }

private val rot: Cell<Euler> =
questEditorStore.selectedEntity.flatMap { it?.rotation ?: DEFAULT_ROTATION }
val rotX: Cell<Double> = rot.map { radToDeg(it.x) }
Expand Down Expand Up @@ -207,6 +213,44 @@ class EntityInfoController(
oldSection = null,
newPosition = Vector3(x, y, z),
oldPosition = entity.position.value,
world = false,
)
)
}

fun setWorldPosX(x: Double) {
questEditorStore.selectedEntity.value?.let { entity ->
val pos = entity.worldPosition.value
setWorldPos(entity, x, pos.y, pos.z)
}
}

fun setWorldPosY(y: Double) {
questEditorStore.selectedEntity.value?.let { entity ->
val pos = entity.worldPosition.value
setWorldPos(entity, pos.x, y, pos.z)
}
}

fun setWorldPosZ(z: Double) {
questEditorStore.selectedEntity.value?.let { entity ->
val pos = entity.worldPosition.value
setWorldPos(entity, pos.x, pos.y, z)
}
}

private fun setWorldPos(entity: QuestEntityModel<*, *>, x: Double, y: Double, z: Double) {
if (!enabled.value) return

questEditorStore.executeAction(
TranslateEntityCommand(
questEditorStore,
entity,
newSection = null,
oldSection = null,
newPosition = Vector3(x, y, z),
oldPosition = entity.worldPosition.value,
world = true,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class StateContext(
oldSection?.id,
newPosition,
oldPosition,
world = false,
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ class QuestEditorStore(
}
}

fun setEntityWorldPosition(entity: QuestEntityModel<*, *>, sectionId: Int?, position: Vector3) {
mutate {
setSelectedEntity(entity)
sectionId?.let { setEntitySection(entity, it) }
entity.setWorldPosition(position)
}
}

fun setEntityRotation(entity: QuestEntityModel<*, *>, rotation: Euler) {
mutate {
setSelectedEntity(entity)
Expand Down Expand Up @@ -359,7 +367,7 @@ class QuestEditorStore(
* Sets [QuestEntityModel.sectionId] and [QuestEntityModel.section] if there's a section with
* [sectionId] as ID.
*/
fun setEntitySection(entity: QuestEntityModel<*, *>, sectionId: Int) {
private fun setEntitySection(entity: QuestEntityModel<*, *>, sectionId: Int) {
currentQuest.value?.let { quest ->
val variant = quest.areaVariants.value.find { it.area.id == entity.areaId }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class EntityInfoWidget(private val ctrl: EntityInfoController) : Widget(enabled
createCoordRow("X:", ctrl.posX, ctrl::setPosX)
createCoordRow("Y:", ctrl.posY, ctrl::setPosY)
createCoordRow("Z:", ctrl.posZ, ctrl::setPosZ)
tr {
th { colSpan = 2; textContent = "World Position:" }
}
createCoordRow("X:", ctrl.worldPosX, ctrl::setWorldPosX)
createCoordRow("Y:", ctrl.worldPosY, ctrl::setWorldPosY)
createCoordRow("Z:", ctrl.worldPosZ, ctrl::setWorldPosZ)
tr {
th { colSpan = 2; textContent = "Rotation:" }
}
Expand Down

0 comments on commit 22d0177

Please sign in to comment.