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

KeyboardInput experiment #3

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion App.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let init = app => {
let window = App.createWindow(app, "Kirby Samurai");
let element =
<View style=appStyle>
<KirbySamurai />
<KirbySamurai />
</View>;

let _ = UI.start(window, element);
Expand Down
42 changes: 20 additions & 22 deletions KirbySamurai.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Revery;
open Revery_Core;
open Revery.UI;
open Revery_UI_Components;
open Revery.UI.Components;
open Styles;

type gameStateT =
Expand Down Expand Up @@ -52,21 +52,20 @@ let createElement = (~children as _, ()) =>
(hooks,
switch(state.gameState) {
| Stop => (
<View
ref={r => Focus.focus(r)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KeyboardInput at least lets us remove this ref/focus handling (handles it under the hood for us). Since the <KeyboardInput /> also grabs input if its lost - it seemed to work more reliably for me when clicking on the screen.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah!I like that <Keyboard /> handles ref/focus under the hood!

style=gameStyle
onKeyDown=((event: NodeEvents.keyEventParams) => {
//key event
let key = event.key |> Key.toString;
//press spacebar to start game
if(key == " ") {
dispatch(SetGameState(PreRunning))
}
})
>
<KeyboardInput
onKeyDown=((event: NodeEvents.keyEventParams) => {
//key event
let key = event.key |> Key.toString;
//press spacebar to start game
if(key == " ") {
dispatch(SetGameState(PreRunning))
}
})>
<game>
//<Image src="kirby1.png" width=50 height=50 />
<Image src="start.png" width=600 height=450 />
</View>
</game>
</KeyboardInput>
)
| PreRunning => {
let tickFn = t => dispatch(Count(t |> Time.toSeconds));
Expand All @@ -79,18 +78,16 @@ let createElement = (~children as _, ()) =>
//set game state Running
dispatch(SetGameState(Running));
};
<View style=gameStyle>
<game>
<Image
src="frame1.png"
width=600
height=450
/>
</View>
</game>
}
| Running => (
<View
style=gameStyle
ref={r => Focus.focus(r)}
<KeyboardInput
onKeyDown=((event: NodeEvents.keyEventParams) => {
//key event
let key = event.key |> Key.toString;
Expand All @@ -105,8 +102,8 @@ let createElement = (~children as _, ()) =>
if(key == " ") {
dispatch(ResetState(initialState));
}
})
>
})>
<game>
(
if(state.keyEvents |> List.length > 0) {
switch(List.nth(state.keyEvents, 0)) {
Expand All @@ -118,7 +115,8 @@ let createElement = (~children as _, ()) =>
<Image src="frame2.png" width=600 height=450 />
}
)
</View>
</game>
</KeyboardInput>
)
}
)
Expand Down
15 changes: 8 additions & 7 deletions Styles.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open Revery;
open Revery.UI;
open Revery.UI.Components;

let appStyle =
Style.[
Expand All @@ -13,13 +14,13 @@ let appStyle =
backgroundColor(Colors.black)
];

let gameStyle =
Style.[
justifyContent(`Center),
alignItems(`Center),
width(600),
height(450),
];
let game = (~children, ()) => {
<Center>
<Container width=600 height=450>
...children
</Container>
</Center>
};

let textStyle =
Style.[
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@opam/js_of_ocaml-compiler": "github:ocsigen/js_of_ocaml:js_of_ocaml-compiler.opam#db257ce",
"@brisk/brisk-reconciler": "github:briskml/brisk-reconciler#daa00be",
"rebez": "github:jchavarri/rebez#46cbc183",
"revery": "github:revery-ui/revery#827390f",
"@brisk/brisk-reconciler": "github:briskml/brisk-reconciler#7b48451"
},
"scripts": {
Expand Down