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

Explore ammonite integration/live-coding possibilities #45

Open
JD557 opened this issue Dec 30, 2020 · 1 comment
Open

Explore ammonite integration/live-coding possibilities #45

JD557 opened this issue Dec 30, 2020 · 1 comment

Comments

@JD557
Copy link
Owner

JD557 commented Dec 30, 2020

Minart can be called from ammonite, which can lead to some interesting live-coding usages.

I haven't thought about this in detail, but I'm thinking about adding a new minart-live package with some utilities such a special RenderLoop, like:

class LiveRenderLoop[S](innerRenderLoop: RenderLoop) {
  private[this] var renderFrame: (Canvas, S) => S = _._2

  def changeRenderFrame(newRenderFrame:  (Canvas, S) => S): Unit

  def init(canvasManager: CanvasManager, initialState: S, frameRate: FrameRate) {
    innerRenderLoop.infiniteRenderLoop(canvasManager, initialState, {(c, s) => renderFrame(c, s)}, frameRate)
  }
}

This would allow a render loop to keep the state and run in the background, while the rendering logic was updated in the REPL.

@JD557
Copy link
Owner Author

JD557 commented Jul 19, 2022

Another possibility here (which would just work on JS) would be to do something similar to Tyrian's HotReload

In this case, there would be something like:

class HotReloadRenderLoop[S](decodeState: String => Option[S], encodeState: S => String) {
  private def loadState(): Option[S] =
    Option(dom.window.localStorage.getItem(key)).flatMap(decodeState)
  private def saveState(state: S): String =
    dom.window.localStorage.setItem(key, encodeState(state))

  def run(runner: LoopRunner, canvasManager: CanvasManager, canvasSettings: Canvas.Settings, initialState: S) = {
    val canvas = canvasManager.init(canvasSettings)
    runner
      .finiteLoop(
        (state: S) => renderFrame(canvas, state).tap(saveState),
        (newState: S) => terminateWhen(newState) || !canvas.isCreated(),
        frameRate,
        () => if (canvas.isCreated()) canvas.close()
      )
      .run(loadState().getOrElse(initialState))
  }
}

I think that with some adjustments, it might also be able to plug this logic into the PureRenderLoop without a ton of work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant