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

Replace FrameContext with Context #784

Merged
merged 13 commits into from
Nov 12, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indigo.benchmarks

import indigo.*

import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package indigo.benchmarks

import indigo.*

import indigo.platform.assets.DynamicText
import indigo.shared.AnimationsRegister
import indigo.shared.FontRegister
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._
import indigo.shared.FontRegister
import indigo.shared.AnimationsRegister
import indigo.platform.assets.DynamicText

object BoundaryLocatorBenchmarks:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package indigo.benchmarks

import indigo.*

import indigo.facades.WeakMap
import indigo.shared.QuickCache
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._
import indigo.shared.QuickCache
import indigo.facades.WeakMap

import scala.scalajs.js

object Caching:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indigo.benchmarks

import indigo.*

import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indigo.benchmarks

import indigo.*

import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package indigo.benchmarks

import org.scalajs.dom.document
import japgolly.scalajs.benchmark.gui.BenchmarkGUI
import org.scalajs.dom.document

import scala.scalajs.js.annotation.JSExportTopLevel

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package indigo.benchmarks

import indigo.*
import indigo.syntax.*
import indigo.physics.*

import indigo.syntax.*
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down Expand Up @@ -51,7 +50,7 @@ object PhysicsWorldBenchmarks:

object TestWorlds:

val dice: Dice = Dice.fromSeed(0)
val dice: Dice = Dice.default

val basicWorld: World[MyTag] =
val circles =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indigo.benchmarks

import indigo.*

import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package indigo.benchmarks

import indigo.*
import indigo.syntax.*

import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package indigo.benchmarks

import indigo.*

import indigo.platform.assets.DynamicText
import indigo.shared.AnimationsRegister
import indigo.shared.FontRegister
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._
import indigo.shared.FontRegister
import indigo.shared.AnimationsRegister
import indigo.platform.assets.DynamicText

object SimpleComparisons:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import indigo.shared.datatypes.BindingKey
import indigo.shared.events.GlobalEvent
import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId

/** The JobMarket is a subsystem that manages a global pool of available jobs.
Expand Down Expand Up @@ -36,7 +36,7 @@ final case class JobMarket[Model](id: SubSystemId, availableJobs: List[Job]) ext
private given CanEqual[Option[Job], Option[Job]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
jobs: List[Job]
): JobMarketEvent => Outcome[List[Job]] = {
case JobMarketEvent.Post(job) =>
Expand All @@ -57,7 +57,7 @@ final case class JobMarket[Model](id: SubSystemId, availableJobs: List[Job]) ext
Outcome(jobs)
}

def present(frameContext: SubSystemFrameContext[ReferenceData], jobs: List[Job]): Outcome[SceneUpdateFragment] =
def present(context: SubSystemContext[ReferenceData], jobs: List[Job]): Outcome[SceneUpdateFragment] =
Outcome(SceneUpdateFragment.empty)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import indigo.shared.events.GlobalEvent
import indigo.shared.events.SubSystemEvent
import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId

// Provides "at least once" message delivery for updates on a bundle's loading status.
Expand All @@ -37,7 +37,7 @@ final class AssetBundleLoader[Model] extends SubSystem[Model]:
private given CanEqual[Option[Set[AssetType]], Option[Set[AssetType]]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
tracker: AssetBundleTracker
): GlobalEvent => Outcome[AssetBundleTracker] =
// Asset Bundle Loader Commands
Expand Down Expand Up @@ -76,7 +76,7 @@ final class AssetBundleLoader[Model] extends SubSystem[Model]:
Outcome(tracker)

def present(
frameContext: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
model: AssetBundleTracker
): Outcome[SceneUpdateFragment] =
Outcome(SceneUpdateFragment.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import indigo.shared.events.SubSystemEvent
import indigo.shared.scenegraph.SceneNode
import indigo.shared.scenegraph._
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.temporal.Signal
import indigo.shared.temporal.SignalReader
Expand Down Expand Up @@ -57,20 +57,20 @@ final case class Automata[Model](
private given CanEqual[Option[Int], Option[Int]] = CanEqual.derived

def update(
frameContext: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
state: AutomataState
): AutomataEvent => Outcome[AutomataState] =
case Spawn(key, position, lifeSpan, payload) if key == poolKey =>
val spawned =
SpawnedAutomaton(
automaton.node.giveNode(state.totalSpawned, frameContext.dice),
automaton.node.giveNode(state.totalSpawned, context.frame.dice),
automaton.modifier,
automaton.onCull,
new AutomatonSeedValues(
position,
frameContext.gameTime.running,
context.frame.time.running,
lifeSpan.getOrElse(automaton.lifespan),
frameContext.dice.roll,
context.frame.dice.roll,
payload
)
)
Expand Down Expand Up @@ -108,21 +108,21 @@ final case class Automata[Model](

case Update(key) if key == poolKey =>
val cullEvents = state.pool
.filterNot(_.isAlive(frameContext.gameTime.running))
.filterNot(_.isAlive(context.frame.time.running))
.flatMap(sa => sa.onCull(sa.seedValues))

Outcome(
state.copy(
pool = state.pool.filter(_.isAlive(frameContext.gameTime.running))
pool = state.pool.filter(_.isAlive(context.frame.time.running))
),
Batch(cullEvents)
)

case _ =>
Outcome(state)

def present(frameContext: SubSystemFrameContext[ReferenceData], state: AutomataState): Outcome[SceneUpdateFragment] =
val updated = Automata.renderNoLayer(state.pool, frameContext.gameTime)
def present(context: SubSystemContext[ReferenceData], state: AutomataState): Outcome[SceneUpdateFragment] =
val updated = Automata.renderNoLayer(state.pool, context.frame.time)

Outcome(
SceneUpdateFragment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.scenegraph.Shape
import indigo.shared.scenegraph.TextBox
import indigo.shared.subsystems.SubSystem
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.time.FPS
import indigo.shared.time.Seconds
Expand Down Expand Up @@ -56,15 +56,15 @@ final case class FPSCounter[Model](
Outcome(FPSCounterState.initial(startPosition))

def update(
context: SubSystemFrameContext[ReferenceData],
context: SubSystemContext[ReferenceData],
model: FPSCounterState
): GlobalEvent => Outcome[FPSCounterState] = {
case FrameTick =>
if (context.gameTime.running >= (model.lastInterval + Seconds(1)))
if (context.frame.time.running >= (model.lastInterval + Seconds(1)))
Outcome(
model.copy(
fps = decideNextFps(model.frameCountSinceInterval),
lastInterval = context.gameTime.running,
lastInterval = context.frame.time.running,
frameCountSinceInterval = 0
)
)
Expand All @@ -82,16 +82,15 @@ final case class FPSCounter[Model](
.withFontFamily(fontFamily)
.withFontSize(fontSize)

def present(context: SubSystemFrameContext[ReferenceData], model: FPSCounterState): Outcome[SceneUpdateFragment] =
def present(context: SubSystemContext[ReferenceData], model: FPSCounterState): Outcome[SceneUpdateFragment] =
val text: TextBox =
textBox
.withText(s"""FPS ${model.fps.toString}""")
.withColor(pickTint(idealFps, model.fps))
.moveTo(model.position + 2)

val size: Rectangle =
context.boundaryLocator
.measureText(text)
context.services.bounds.measureText(text)

val boxSize =
({ (s: Size) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package indigoextras.ui

import indigo.shared.BoundaryLocator
import indigo.shared.FrameContext
import indigo.shared.Context
import indigo.shared.Outcome
import indigo.shared.collections.Batch
import indigo.shared.constants.Key
Expand Down Expand Up @@ -35,8 +35,8 @@ final case class InputField(
onLoseFocus: () => Batch[GlobalEvent]
) derives CanEqual:

def bounds(boundaryLocator: BoundaryLocator): Option[Rectangle] =
boundaryLocator.findBounds(assets.text.withText(text).moveTo(position))
def bounds(_bounds: Context.Services.Bounds): Option[Rectangle] =
_bounds.find(assets.text.withText(text).moveTo(position))

def withText(newText: String): InputField =
this.copy(
Expand Down Expand Up @@ -166,7 +166,7 @@ final case class InputField(
def withLoseFocusActions(actions: => Batch[GlobalEvent]): InputField =
this.copy(onLoseFocus = () => actions)

def update(frameContext: FrameContext[?]): Outcome[InputField] = {
def update(context: Context[?]): Outcome[InputField] = {
@tailrec
def rec(
keysReleased: List[Key],
Expand All @@ -177,7 +177,7 @@ final case class InputField(
keysReleased match {
case Nil =>
if (touched)
Outcome(acc.copy(lastCursorMove = frameContext.gameTime.running), Batch.fromOption(changeEvent))
Outcome(acc.copy(lastCursorMove = context.frame.time.running), Batch.fromOption(changeEvent))
else
Outcome(acc, Batch.fromOption(changeEvent))

Expand Down Expand Up @@ -215,13 +215,13 @@ final case class InputField(

val updated: Outcome[InputField] =
if (hasFocus)
rec(frameContext.inputState.keyboard.keysReleased.toList, this, false, None)
rec(context.frame.input.keyboard.keysReleased.toList, this, false, None)
else Outcome(this)

if (frameContext.inputState.pointers.isReleased)
bounds(frameContext.boundaryLocator) match
if (context.frame.input.pointers.isReleased)
bounds(context.services.bounds) match
case Some(bounds) =>
if frameContext.inputState.pointers.wasUpWithin(bounds, MouseButton.LeftMouseButton) then
if context.frame.input.pointers.wasUpWithin(bounds, MouseButton.LeftMouseButton) then
updated.flatMap(_.giveFocus)
else updated.flatMap(_.loseFocus)
case _ =>
Expand All @@ -231,7 +231,7 @@ final case class InputField(

def draw(
gameTime: GameTime,
boundaryLocator: BoundaryLocator
boundaryLocator: Context.Services.Bounds
): Batch[SceneNode] = {
val field =
assets.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indigoextras.jobs
import indigo.platform.assets.DynamicText
import indigo.shared.AnimationsRegister
import indigo.shared.BoundaryLocator
import indigo.shared.Context
import indigo.shared.FontRegister
import indigo.shared.collections.Batch
import indigo.shared.datatypes.BindingKey
Expand All @@ -11,19 +12,18 @@ import indigo.shared.events.FrameTick
import indigo.shared.events.InputState
import indigo.shared.scenegraph.SceneAudio
import indigo.shared.subsystems
import indigo.shared.subsystems.SubSystemFrameContext
import indigo.shared.subsystems.SubSystemContext
import indigo.shared.subsystems.SubSystemId
import indigo.shared.time.GameTime

class JobMarketTests extends munit.FunSuite {

val context =
SubSystemFrameContext[Unit](
GameTime.zero,
Dice.loaded(6),
InputState.default,
new BoundaryLocator(new AnimationsRegister, new FontRegister, new DynamicText),
()
SubSystemContext.fromContext(
Context.initial
.modifyFrame(
_.withDice(Dice.loaded(6))
)
)

val workContext =
Expand Down
Loading
Loading