Skip to content

Commit

Permalink
examples: add proof-of-concept record and replay implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Sep 3, 2023
1 parent 27d95c2 commit 673c02e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
80 changes: 80 additions & 0 deletions examples/record_and_replay/record_and_replay.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright(C) 2023 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main

import shy.lib as shy
import shy.embed
import time

fn main() {
mut app := &App{}
shy.run[App](mut app)!
}

[heap]
struct App {
embed.ExampleApp
mut:
fa_x &shy.FollowAnimator[f32] = shy.null
fa_y &shy.FollowAnimator[f32] = shy.null
timer time.StopWatch = time.new_stopwatch()
}

[markused]
pub fn (mut a App) init() ! {
a.ExampleApp.init()!

a.fa_x = a.shy.new_follow_animator[f32]()
a.fa_y = a.shy.new_follow_animator[f32]()
}

[markused]
pub fn (mut a App) frame(dt f64) {
mouse := a.mouse

a.fa_x.target = mouse.x
a.fa_y.target = mouse.y

a.quick.rect(
x: a.fa_x.value
y: a.fa_y.value
origin: .center
)

a.quick.text(
x: a.canvas().width * 0.01
y: a.canvas().height * 0.01
origin: .top_left
text: 'Press "R" key to start recording\nPress "P" to playback the recorded events'
)
}

[markused]
pub fn (mut a App) event(e shy.Event) {
a.ExampleApp.event(e)

match e {
shy.KeyEvent {
if e.state == .up {
match e.key_code {
.r {
mut events := a.shy.events()
events.record()
}
.p {
mut events := a.shy.events()
events.play_back()
}
else {}
}
}
}
shy.MouseButtonEvent {
if e.state == .down {
}
}
shy.ResetStateEvent {}
else {}
}
}
13 changes: 6 additions & 7 deletions lib/event.b.v
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ pub fn (mut e Events) poll() ?Event {
e.shy.log.ginfo('${@STRUCT}.${@FN}', 'nothing to play back, returning to normal')
e.state = .normal
} else {
e.shy.log.gwarn('${@STRUCT}.${@FN}', '[WIP] returning to normal')
e.state = .normal
/*
if e.shy.wm().active_window().state.frame == e.play_next.frame {
if e.shy.ticks() >= e.play_next.timestamp {
if e.shy.ticks() != e.play_next.timestamp {
e.shy.log.gwarn('${@STRUCT}.${@FN}', 'TODO replaying event at ${e.shy.ticks()} vs ${e.play_next.timestamp} was not precise')
}
if e.play_next is UnkownEvent {
e.play_next = e.play_queue.pop()
return none
Expand All @@ -81,7 +81,6 @@ pub fn (mut e Events) poll() ?Event {
e.play_next = e.play_queue.pop()
}
}
*/
}
} else if event := input.poll_event() {
e.send(event) or { panic(err) }
Expand All @@ -104,18 +103,18 @@ pub fn (mut e Events) poll() ?Event {

pub fn (mut e Events) record() {
e.shy.log.ginfo('${@STRUCT}.${@FN}', '')
e.shy.timer.restart()
// e.send_reset_state_event()
e.play_queue.clear()
e.recorded.clear()
e.shy.wm().active_window().state.frame = 0 // TODO make a complete state reset possible to minimize any errors
e.state = .record
}

pub fn (mut e Events) play_back() {
// e.send_reset_state_event()
e.play_queue = e.recorded.reverse()
e.shy.log.ginfo('${@STRUCT}.${@FN}', 'starting play back of ${e.play_queue.len} events')
e.shy.wm().active_window().state.frame = 0 // TODO make a complete state reset possible to minimize any errors
e.shy.timer.restart()
e.state = .play
e.play_next = e.play_queue.pop()
}
Expand Down

0 comments on commit 673c02e

Please sign in to comment.