forked from bkase/gameboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
index.html just holds the script tag in the body now. We do a nasty hack in Percy to workaround the lack of an `Rc<VirtualNode>` embedding. See chinedufn/percy#108 We suffer with an O(n^2) virtual node embedding for now. It seems like with the current WASM work, we only spend 0.85ms in our Rust code! We have plenty of time to spare for actual GameBoy logic. Included here is an unused (for now) memory table debug interface that will eventually be hooked into the data and GUI.
- Loading branch information
Showing
6 changed files
with
213 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use futures::{Future, FutureExt}; | ||
use futures_signals::map_ref; | ||
use futures_signals::signal::{Mutable, Signal, SignalExt}; | ||
use game; | ||
use hack_vdom::InjectNode; | ||
use virtual_dom_rs::prelude::*; | ||
use web_utils; | ||
|
||
// the idea is that "global" shared state would go here | ||
// Memory or Key-presses would go here | ||
pub struct Globals { | ||
// let's just make sure lifetime stuff is solid | ||
pub unit: Mutable<()>, | ||
// a simple counter per frame | ||
pub frames: Mutable<u32>, | ||
} | ||
|
||
/* | ||
* The plan: | ||
* | ||
* * AppState has all atomic pieces of mutable state in `Mutable`s | ||
* * Different components grab different mutables as needed | ||
* * Mutables that wrap mutable references are updated every frame | ||
* * They need cheap PartialEq instances so we can dedupe efficiently | ||
* * Each component (at the leaves!), combines the mutables together and | ||
* creates a single signal | ||
* * Leaf components return Signal<Rc<VirtualNode>> | ||
* * Non-leaves combine the children's Signal<Rc<VirtualNode>>s together | ||
* * At the end we have a single Signal<VirtualNode> that we patch into the DOM | ||
* * AppState owns all the Mutables, but all other components mixin the events they want by | ||
* reference. They mixin readonly signals by value (as these are cheap and are created from | ||
* mutables) | ||
*/ | ||
pub struct AppState { | ||
pub globals: Globals, | ||
// we could have one field per component that emits events | ||
} | ||
|
||
fn component(state: &AppState) -> impl Signal<Item = VirtualNode> { | ||
let unit = &(state.globals).unit; | ||
let game = game::component(game::State { | ||
unit: Box::new(unit.signal()), | ||
}); | ||
|
||
map_ref! { | ||
let _ = unit.signal(), | ||
let game_dom = game => { | ||
let inner_dom : InjectNode = InjectNode(game_dom.clone()); | ||
html! { | ||
<div class="flex"> | ||
<div class="mw7 ph4 mt2 w-60"> | ||
{ inner_dom } | ||
</div> | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn run(app_state: &AppState) -> impl Future<Output = ()> { | ||
let start_view = html! { <div /> }; | ||
|
||
let body = web_utils::document().body().unwrap(); | ||
|
||
let mut dom_updater = DomUpdater::new_append_to_mount(start_view, &body); | ||
|
||
component(app_state) | ||
.map(move |vdom| dom_updater.update(vdom)) | ||
.to_future() | ||
.map(|_| ()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use futures_signals::signal::{Signal, SignalExt}; | ||
use std::rc::Rc; | ||
use virtual_dom_rs::prelude::*; | ||
|
||
pub struct State<S> | ||
where | ||
S: Signal<Item = ()>, | ||
{ | ||
// let's just test this thing | ||
pub unit: S, | ||
} | ||
|
||
pub fn component<S: Signal<Item = ()>>(state: State<S>) -> impl Signal<Item = Rc<VirtualNode>> { | ||
state.unit.map(|_| { | ||
Rc::new(html! { | ||
<canvas width="160" height="144" id="canvas" style="width: 100%;image-rendering: pixelated;"></canvas> | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::convert::From; | ||
use std::rc::Rc; | ||
use virtual_dom_rs::prelude::*; | ||
use virtual_dom_rs::{Events, IterableNodes, VElement, VText}; | ||
|
||
pub struct InjectNode(pub Rc<VirtualNode>); | ||
|
||
impl From<InjectNode> for IterableNodes { | ||
fn from(h: InjectNode) -> IterableNodes { | ||
fn clone(n: &VirtualNode) -> VirtualNode { | ||
match n { | ||
VirtualNode::Element(e) => VirtualNode::Element(VElement { | ||
tag: e.tag.clone(), | ||
attrs: e.attrs.clone(), | ||
events: Events(e.events.0.clone()), | ||
children: e.children.iter().map(|v| clone(v)).collect(), | ||
}), | ||
VirtualNode::Text(txt) => VirtualNode::Text(VText { | ||
text: txt.to_string(), | ||
}), | ||
} | ||
} | ||
|
||
clone(&*h.0).into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters