如何在动态更新的TornadoFX视图中处理“脏孩子”?

时间:2019-06-04 00:37:12

标签: javafx kotlin tornadofx

我编写了一个MVP模拟,它会在后台初始化模拟“世界”,然后将其绘制到UI。这是主要视图:

class SimView : View() {
val presenter: SimPresenter = find(SimPresenter::class)
val canvas: Pane
val registeredVehiclesRender: MutableList<Vehicle.VehicleRender> = mutableListOf()


init {
    val (worldWidth, worldHeight, startingVehicles) = arrayOf(400.0, 400.0, 10.0)
    canvas = pane {
    }
    canvas.prefHeight = worldHeight
    canvas.prefWidth = worldWidth
    runAsync {
        presenter.startSimulation(worldWidth, worldHeight, startingVehicles, find(SimView::class))
    } ui {
        renderWorld(it)
    }
}


override val root = vbox {
    anchorpane {
        canvas
        anchorpaneConstraints {
            topAnchor = 0.0
            bottomAnchor = 0.0
            rightAnchor = 0.0
            leftAnchor = 0.0
        }
        vboxConstraints {
            vgrow = Priority.ALWAYS
        }
    }
}

fun renderWorld(model: SimModel) {
    renderVehicles(model.vehicles)
    renderWorldObjects(model.objects)
}

fun renderVehicles(
    vehicles: Set<Vehicle>
) { //TODO
    vehicles.forEach {
        if (!registeredVehiclesRender.contains(it.render)) { //add new
            with(canvas) {
                this += it.render.element //JavaFX Shape (Group of Rectangles)
            }
            registeredVehiclesRender.add(it.render)
        } else {
            it.render.update(it.speed)
        }
    }
}

fun renderWorldObjects(wobjs: MutableCollection<WorldObject>) = wobjs.forEach {
    //TODO
    with(canvas) {
        this += it.shape //JavaFX Shape (circle)
    }
}
}

我没有插入整个初始化代码。然后将作为子级的“ javafx.Shape”添加到“ canvas”中。 “画布”窗格首先添加所有这些形状,并声明它们为“脏”,然后,在任务的UI部分运行之后,完全刷新“画布”的子项(0个子项和0个脏项)。 我该如何使它们“黏附”不消失?

0 个答案:

没有答案