WorldWindDrawEngine

class WorldWindDrawEngine(scope: CoroutineScope, overlay: OverlayEngine? = null, historyCapacity: Int = 100, handleStyle: HandleStyle = HandleStyle(), highlightStyle: HighlightStyle = HighlightStyle(), buildOnFirstFrame: Boolean = false)

The whole draw engine in one object: the shapes, the undo history, the drawing state, the layer that puts them on the map and the input that lets a person draw and edit on it. It builds and connects those, so a host does not have to.

// In a Compose screen (what the demo does; a plain Android or iOS host does the same three things):
val engine = remember { WorldWindDrawEngine(scope, overlay = JtsOverlayEngine()) } // 1. build it

LaunchedEffect(engine) { // 2. give it the clock
var last = withFrameNanos { it }
while (true) {
val now = withFrameNanos { it }
engine.tick((now - last) / 1_000_000)
last = now
}
}

WorldWindow(
state = rememberWorldWindowState { ... },
onCreated = { window -> engine.attachTo(window) }, // 3. put it on the map
)

// The host's own buttons:
val state by engine.state.collectAsState()
when (val mode = state.mode) {
is DrawMode.Draw -> { Button("Finish") { engine.finishDrawing() }; Button("Cancel") { engine.cancel() } }
is DrawMode.Edit -> Button("Done") { engine.stopEditing() }
is DrawMode.Select -> { Button("Edit") { engine.edit(mode.entityId) }; Button("Delete") { engine.deleteSelected() } }
else -> Button("Draw") { engine.startDrawing(DrawableEntityType.Polygon) }
}

What the host does: attachTo its map window (there is one for each platform, because WorldWind's WorldWindow is not one common type), and call tick once per frame so that animations (the selected shape's pulse, an effect that is playing) advance: the engine has no clock of its own. Everything else is the engine's.

What a person does reaches it through input (taps and drags on the map), and the host's own buttons call the functions here (startDrawing, finishDrawing, select, edit, undo, ...). What the engine shows is in state, entities, canUndo and canRedo, all of them flows to collect.

The parts it is made of are public for a host that needs more than this: repository (to put shapes in without a history step, or to play an effect on), history, layer and send (any DrawEvent).

Parameters

scope

the scope the layer builds the shapes in (the host's own, so that it ends when the host's does).

overlay

what merges and cuts polygons (mergeWith, cutBy); null if the host does not need them, then they do nothing.

historyCapacity

how many steps can be undone.

handleStyle

how the handles of a shape being edited look; can be changed later through layer.

highlightStyle

how a selected shape is marked; can be changed later through layer.

buildOnFirstFrame

whether the layer builds the shapes in the first frame instead of in the background (see DrawLayer).

Constructors

Link copied to clipboard
constructor(scope: CoroutineScope, overlay: OverlayEngine? = null, historyCapacity: Int = 100, handleStyle: HandleStyle = HandleStyle(), highlightStyle: HighlightStyle = HighlightStyle(), buildOnFirstFrame: Boolean = false)

Properties

Link copied to clipboard
val canRedo: StateFlow<Boolean>

Whether there is a step to redo.

Link copied to clipboard
val canUndo: StateFlow<Boolean>

Whether there is a step to undo.

Link copied to clipboard
val entities: StateFlow<List<DrawableEntity>>

The shapes that exist, as a flow.

Link copied to clipboard

The undo history; undo and redo are what a host needs of it.

Link copied to clipboard
val input: SelectDragCallback

Where the touches on the map go: taps and drags to select, draw and edit. attachTo gives it to the window.

Link copied to clipboard

The layer that shows the shapes on the map; attachTo adds it to a window.

Link copied to clipboard

The shapes that exist. Putting one in here directly is not a step of the history (it cannot be undone); see DrawEvent for what is.

Link copied to clipboard
val state: StateFlow<DrawEngineState>

What the engine is doing: idle, drawing, selecting, editing...

Functions

Link copied to clipboard
fun WorldWindDrawEngine.attachTo(window: WorldWindow)

Puts the engine on window: adds its layer, gives it the touches on the map and lets it ask the window to draw again. WorldWind's WorldWindow is a different class on each platform, so this is a function of each platform and the host's code on that platform calls it.

fun WorldWindDrawEngine.attachTo(window: WorldWindow)

Puts the engine on window: adds its layer, gives it the touches on the map and lets it ask the window to draw again. WorldWind's WorldWindow is a different class on each platform, so this is a function of each platform and the host's code on that platform calls it.

fun WorldWindDrawEngine.attachTo(window: WorldWindow)

Puts the engine on window: adds its layer, gives it the touches on the map and lets it ask the window to draw again. WorldWind's WorldWindow is a different class on each platform, so this is a function of each platform and the host's code on that platform calls it.

fun WorldWindDrawEngine.attachTo(window: WorldWindow)

Puts the engine on window: adds its layer, gives it the touches on the map and lets it ask the window to draw again. WorldWind's WorldWindow is a different class on each platform, so this is a function of each platform and the host's code on that platform calls it.

fun WorldWindDrawEngine.attachTo(window: WorldWindow)

Puts the engine on window: adds its layer, gives it the touches on the map and lets it ask the window to draw again. WorldWind's WorldWindow is a different class on each platform, so this is a function of each platform and the host's code on that platform calls it.

Link copied to clipboard
fun cancel()

Gives up what is being drawn, or what is being edited or selected: back to idle.

Link copied to clipboard
fun cutBy(cutterId: EntityId, idPrefix: String = Random.nextLong().toString())

Cuts the polygon cutterId out of the selected polygon; needs an overlay. New ids are made from idPrefix.

Link copied to clipboard

Deletes the selected shape (or the one being edited); it can be undone.

Link copied to clipboard
fun edit(id: EntityId)

Starts editing the shape id: its corners can be dragged and new ones added.

Link copied to clipboard
fun finishDrawing(id: EntityId = EntityId(Random.nextLong().toString())): EntityId?

Turns what is being drawn into a shape with the id id (a new one by default) and returns the id; null, and nothing happens, if nothing is being drawn. Whether the points make a shape (a polygon needs 3) is for the engine to say: if they do not, it stays as it was.

Link copied to clipboard
fun mergeWith(otherId: EntityId, idPrefix: String = Random.nextLong().toString())

Merges the selected polygon with the polygon otherId; needs an overlay. New ids are made from idPrefix.

Link copied to clipboard
fun onRedrawNeeded(request: () -> Unit)

Lets the engine ask the host's window to draw again when something changed (a shape, a selection, a step undone). attachTo does this; call it yourself to use a window attachTo does not know.

Link copied to clipboard
fun play(animatable: Animatable)

Moves animatable (an effect, see the morph module) along with every tick until stop is called for it.

Link copied to clipboard
fun redo()

Does again the step that was taken back (see HistoryManager.redo); canRedo says whether there is one.

Link copied to clipboard
fun select(id: EntityId)

Selects the shape id (it is marked; edit, deleteSelected, mergeWith and the like then apply to it).

Link copied to clipboard
fun send(event: DrawEvent)

Gives the engine any event (what the buttons below do, and more: see DrawEvent).

Link copied to clipboard
fun startDrawing(type: DrawableEntityType, style: ShapeStyle? = null)

Starts drawing a new shape of type; the person taps the map to place its points, then finishDrawing. style is how it looks while drawn.

Link copied to clipboard
fun stop(animatable: Animatable)

Stops moving animatable (an effect that has finished); the opposite of play.

Link copied to clipboard

Ends editing and leaves the shape selected.

Link copied to clipboard
fun tick(deltaMs: Long)

Advances what moves by deltaMs milliseconds: call it once per frame. The engine has no clock of its own.

Link copied to clipboard
fun undo()

Takes back the last step (see HistoryManager.undo); canUndo says whether there is one.