Skip to content

Commit 8f31c86

Browse files
author
Ben Jack
committed
bare-bones scene manager
1 parent f7dc67b commit 8f31c86

File tree

5 files changed

+160
-22
lines changed

5 files changed

+160
-22
lines changed

src/imageLoader.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default class PImageLoader{
22

3-
constructor(p5){
3+
constructor(){
44
this._total_images_to_load = 0;
55
this._loaded_images = 0;
66
this._images = {};
@@ -19,13 +19,12 @@ export default class PImageLoader{
1919
}
2020
}
2121

22-
draw_image(name, ctx, x, y){
23-
ctx.image(this._images[name], x, y);
22+
draw_image(name, x, y){
23+
image(this._images[name], x, y);
2424
}
2525

26-
draw_image_from_sequence(name, frame_lerp, ctx, x, y){
27-
let frame = math.floor(frame_lerp*this._image_sequences[name].length);
28-
ctx.image(this._image_sequences[name][frame], x, y);
26+
draw_image_from_sequence(name, frame, x, y){
27+
image(this._image_sequences[name][frame], x, y);
2928
}
3029

3130
all_images_loaded(){

src/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vignettes from './vignettes.js'
22
import {setup_window_variables} from "./windowVariables.js"
33

4-
let p = new p5();
4+
//let p = new p5();
55

66
window.setup = function() {
77
angleMode(DEGREES);
@@ -10,16 +10,16 @@ window.setup = function() {
1010
let vignettes = new Vignettes();
1111
setup_window_variables(vignettes);
1212
setup_vignettes(vignettes);
13-
setup_new_canvas(100, 100);
13+
setup_new_canvas(1280, 720);
1414
}
1515

1616
window.draw = function() {
1717
background(255);
18-
// vignettes.draw();
18+
vignettes.draw();
1919
}
2020

2121
window.setup_new_canvas = function(width, height){
22-
createCanvas(width, height);
22+
window.p5Canvas = createCanvas(width, height).canvas;
2323
setup_scenes(vignettes);
2424
}
2525

@@ -35,10 +35,20 @@ window.mouseMoved = function(){
3535
vignettes.mouse_moved();
3636
}
3737

38-
window.keyPressed = function(){
39-
console.log(keyCode);
38+
//p5js' default keyPressed function only seems to trigger once for certian keys in chrome.
39+
//I am overriding this event with one that seems more reliable.
40+
document.onkeydown = function(e) {
41+
if (!e.metaKey) {
42+
e.preventDefault();
43+
}
44+
45+
keyCode = e.keyCode;
46+
key = e.key;
47+
48+
vignettes.key_pressed();
49+
4050
}
4151

42-
window.keyReleased = function(){
52+
window.onkeyrelease = function(oPEvt){
4353
vignettes.key_released();
4454
}

src/scene.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export default class Scene{
2+
3+
constructor(draw_function){
4+
this._draw_function = draw_function;
5+
this._click_function = function(){};
6+
this._mouse_moved_function = function(){};
7+
this._mouse_dragged_function = function(){};
8+
this._key_pressed_function = function(){};
9+
this._key_released_function = function(){};
10+
vignettes.add_scene(this);
11+
}
12+
13+
draw(){
14+
this._draw_function();
15+
}
16+
17+
click(){
18+
this._click_function();
19+
}
20+
21+
mouse_moved(){
22+
this._mouse_moved_function();
23+
}
24+
25+
mouse_dragged(){
26+
this._mouse_dragged_function();
27+
}
28+
29+
key_pressed(){
30+
this._key_pressed_function();
31+
}
32+
33+
key_released(keyboard_event){
34+
this._key_released_function();
35+
}
36+
37+
get click(){
38+
return this._click_function;
39+
}
40+
41+
get mouse_moved(){
42+
return this._mouse_moved_function;
43+
}
44+
45+
get mouse_dragged(){
46+
return this._mouse_dragged_function;
47+
}
48+
49+
get key_pressed(){
50+
return this._key_pressed_function;
51+
}
52+
53+
get key_released(){
54+
return this._key_released_function;
55+
}
56+
57+
set click(func){
58+
this._click_function = func;
59+
}
60+
61+
set mouse_moved(func){
62+
this._mouse_moved_function = func;
63+
}
64+
65+
set mouse_dragged(func){
66+
this._mouse_dragged_function = func;
67+
}
68+
69+
set key_pressed(func){
70+
this._key_pressed_function = func;
71+
}
72+
73+
set key_released(func){
74+
this._key_released_function = func;
75+
}
76+
77+
}

src/vignettes.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import ImageLoader from "./imageLoader.js"
2+
13
export default class Vignettes{
24

35
constructor(){
46
this._scenes = [];
7+
this._image_loader = new ImageLoader();
58
this._current_scene = 0;
9+
this._recording = false;
610
}
711

812
add_scene(scene){
@@ -13,6 +17,10 @@ export default class Vignettes{
1317
if(this._scenes.length > 0){
1418
this._scenes[this._current_scene].draw();
1519
}
20+
21+
if(this._recording){
22+
this.capture_frame();
23+
}
1624
}
1725

1826
click(){
@@ -33,24 +41,68 @@ export default class Vignettes{
3341
}
3442
}
3543

36-
key_pressed(keyboard_event){
44+
key_pressed(){
3745

38-
console.log(keyCode);
39-
if(keyCode == 39){
40-
this._current_scene = this._current_scene - 1;
41-
this._current_scene = this._current_scene < 0 ? this._scenes.length - 1 : this._current_scene;
42-
}else if(keyCode == 37){
43-
this.current_scene = (this.current_scene + 1)%this._scenes.length;
46+
console.log(keyCode, key);
47+
48+
if(key == "ArrowRight"){// 'rightArrow'
49+
this.increment_current_scene();
50+
}else if(key == "ArrowLeft"){// 'leftArrow'
51+
this.decrement_current_scene();
52+
}else if(key == "r" && !this._recording){// 'r'
53+
this.begin_recording();
54+
}else if(key == "r" && this._recording){
55+
this.end_recording();
4456
}else if(this._scenes.length > 0){
4557
this._scenes[this._current_scene].key_pressed();
4658
}
4759

48-
console.log(this._current_scene);
60+
}
4961

62+
increment_current_scene(){
63+
this._current_scene = (this._current_scene + 1)%this._scenes.length;
64+
}
65+
66+
decrement_current_scene(){
67+
this._current_scene = this._current_scene - 1;
68+
this._current_scene = this._current_scene < 0 ? this._scenes.length - 1 : this._current_scene;
5069
}
5170

5271
key_released(keyboard_event){
5372
this._scenes[this._current_scene].key_released();
5473
}
5574

75+
begin_recording(){
76+
console.log("started recording");
77+
ccapturer.start();
78+
this.capture_frame();
79+
this._recording = true;
80+
}
81+
82+
capture_frame(){
83+
ccapturer.capture(p5Canvas);
84+
}
85+
86+
end_recording(){
87+
console.log("finished recording");
88+
ccapturer.stop();
89+
ccapturer.save();
90+
this._recording = false;
91+
}
92+
93+
//-------------image loading and displaying------------------
94+
load_image(name, file_type){
95+
this._image_loader.load_image(name, file_type);
96+
}
97+
load_image_sequence(name, file_type, sequence_length){
98+
this._image_loader.load_image_sequence(name, file_type, sequence_length);
99+
}
100+
draw_image(name, x, y){
101+
this._image_loader.draw_image(name, x, y);
102+
}
103+
draw_image_from_sequence(name, x, y, frame){
104+
this._image_loader.draw_image_from_sequence(name, frame, x, y);
105+
}
106+
//-----------------------------------------------------------
107+
56108
}

src/windowVariables.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import Scene from "./scene.js"
33
export const setup_window_variables = function(vignettes){
44
window.vignettes = vignettes;
55
window.Scene = Scene;
6-
window.ccapturer = new CCapture( { format: 'webm' } );
6+
window.ccapturer = new CCapture( { format: 'webm', framerate: 24 } );
77
}

0 commit comments

Comments
 (0)