2018-07-20 09:58:11 +00:00
|
|
|
import 'dart:html';
|
|
|
|
import 'package:browserloop/game/Game.dart';
|
|
|
|
import 'package:browserloop/game/LoopExample.dart';
|
2018-07-20 15:46:52 +00:00
|
|
|
import 'package:browserloop/src/03-VariableTimestep.dart';
|
2018-07-20 12:56:09 +00:00
|
|
|
import 'package:browserloop/src/04-FixedLoopVariableRender.dart';
|
2018-07-20 09:58:11 +00:00
|
|
|
import 'package:browserloop/src/02-AnimationFrameWhile.dart';
|
2018-07-20 16:01:49 +00:00
|
|
|
import 'package:browserloop/src/05_DirtyFlagRendering.dart';
|
2018-07-20 09:58:11 +00:00
|
|
|
|
2018-07-20 17:03:46 +00:00
|
|
|
CanvasElement baseCanvas = new CanvasElement(width: 480, height: 480);
|
|
|
|
|
2018-07-20 09:58:11 +00:00
|
|
|
List<Example> examples = [
|
2018-07-24 11:35:25 +00:00
|
|
|
Example(
|
|
|
|
"While Loop Example", "#while_loop", new WhileLoop(new Game(baseCanvas))),
|
|
|
|
Example("Variable Timestep", "#variable_timestep",
|
|
|
|
new VariableTimestep(new Game(baseCanvas))),
|
|
|
|
Example("Fixed Update, Variable Render", "#fixed_variable",
|
|
|
|
new FixedLoopVariableRender(new Game(baseCanvas))),
|
|
|
|
Example("Variable Render with Dirty Flag", "#dirty_flag",
|
|
|
|
new DirtyFlagRender(new Game(baseCanvas)))
|
2018-07-20 09:58:11 +00:00
|
|
|
];
|
|
|
|
LoopExample active;
|
|
|
|
|
|
|
|
class Example {
|
|
|
|
final String query;
|
|
|
|
final String name;
|
|
|
|
LoopExample loop;
|
|
|
|
|
2018-07-20 17:03:46 +00:00
|
|
|
Example(this.name, this.query, this.loop);
|
|
|
|
|
|
|
|
CanvasElement get canvas {
|
2018-07-24 11:35:25 +00:00
|
|
|
if (loop != null) return loop.game.canvas;
|
2018-07-20 17:03:46 +00:00
|
|
|
return new CanvasElement(width: 480, height: 480);
|
|
|
|
}
|
2018-07-24 11:35:25 +00:00
|
|
|
|
2018-07-20 17:03:46 +00:00
|
|
|
set canvas(CanvasElement canvas) {
|
2018-07-24 11:35:25 +00:00
|
|
|
if (loop != null) loop.game.canvas = canvas;
|
2018-07-20 17:03:46 +00:00
|
|
|
}
|
2018-07-20 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
examples.forEach((ex) {
|
2018-07-20 12:06:47 +00:00
|
|
|
resetExample(ex);
|
2018-07-20 09:58:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendToDOM(Example example) {
|
|
|
|
querySelector(example.query).append(example.canvas);
|
|
|
|
example.canvas.onClick.forEach(activate);
|
2018-07-20 12:06:47 +00:00
|
|
|
print("${example.query} appended to DOM");
|
2018-07-20 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 12:06:47 +00:00
|
|
|
void removeFromDOM(Example example) {
|
2018-07-20 09:58:11 +00:00
|
|
|
querySelector(example.query).children.clear();
|
2018-07-20 12:06:47 +00:00
|
|
|
print("${example.query} removed from DOM");
|
2018-07-20 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 12:06:47 +00:00
|
|
|
void resetExample(Example ex) {
|
|
|
|
removeFromDOM(ex);
|
|
|
|
if (ex.loop != null) ex.loop.stop();
|
|
|
|
ex.canvas =
|
|
|
|
new CanvasElement(width: ex.canvas.width, height: ex.canvas.height);
|
|
|
|
createPlaceholder(ex);
|
|
|
|
appendToDOM(ex);
|
|
|
|
}
|
2018-07-20 09:58:11 +00:00
|
|
|
|
2018-07-20 12:06:47 +00:00
|
|
|
void createPlaceholder(Example ex) {
|
|
|
|
if (ex.canvas == null) return;
|
|
|
|
|
|
|
|
// Clear Canvas
|
|
|
|
CanvasRenderingContext2D ctx = ex.canvas.context2D;
|
|
|
|
Point c = Point(ex.canvas.width / 2, ex.canvas.height / 2);
|
2018-07-20 18:17:19 +00:00
|
|
|
ctx.setFillColorRgb(183, 20, 39);
|
2018-07-20 12:06:47 +00:00
|
|
|
ctx.fillRect(0, 0, ex.canvas.width, ex.canvas.height);
|
|
|
|
|
|
|
|
// Draw Play Button
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(c.x - 25, c.y - 25);
|
|
|
|
ctx.lineTo(c.x + 25, c.y + 25);
|
|
|
|
ctx.lineTo(c.x - 25, c.y + 75);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.lineWidth = 10;
|
|
|
|
ctx.setStrokeColorRgb(155, 155, 155);
|
|
|
|
ctx.setFillColorRgb(255, 255, 255);
|
|
|
|
ctx.stroke();
|
|
|
|
ctx.fill();
|
|
|
|
|
|
|
|
// Draw Text
|
|
|
|
ctx.fillText(ex.name, c.x - ctx.measureText(ex.name).width / 2, c.y - 50);
|
|
|
|
|
|
|
|
print("${ex.query} placeholder created");
|
2018-07-20 09:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void activate(MouseEvent e) {
|
2018-07-20 12:06:47 +00:00
|
|
|
if (e.target is! CanvasElement) return;
|
2018-07-20 09:58:11 +00:00
|
|
|
CanvasElement c = (e.target as CanvasElement);
|
|
|
|
|
|
|
|
examples.forEach((Example ex) {
|
|
|
|
if (ex.canvas == c) {
|
2018-07-20 12:06:47 +00:00
|
|
|
resetExample(ex);
|
2018-07-20 16:19:57 +00:00
|
|
|
addControls(ex);
|
2018-07-20 12:06:47 +00:00
|
|
|
} else {
|
|
|
|
resetExample(ex);
|
2018-07-20 09:58:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-07-20 16:19:57 +00:00
|
|
|
|
|
|
|
void addControls(Example ex) {
|
2018-07-20 17:04:10 +00:00
|
|
|
DivElement group = new DivElement();
|
2018-07-24 11:34:29 +00:00
|
|
|
ButtonElement randomButton = new ButtonElement()..text = "Random/Ordered";
|
2018-07-20 17:04:10 +00:00
|
|
|
querySelector(ex.query).append(group
|
2018-07-20 16:19:57 +00:00
|
|
|
..append(new ButtonElement()
|
|
|
|
..text = "Start"
|
|
|
|
..onClick.listen((e) {
|
|
|
|
ex.loop.stop();
|
|
|
|
ex.loop.start();
|
|
|
|
}))
|
|
|
|
..append(new ButtonElement()
|
|
|
|
..text = "Stop"
|
|
|
|
..onClick.listen((e) {
|
|
|
|
ex.loop.stop();
|
2018-07-20 17:51:13 +00:00
|
|
|
}))
|
|
|
|
..append(new ButtonElement()
|
|
|
|
..text = "+"
|
|
|
|
..onClick.listen((e) {
|
|
|
|
ex.loop.game.changeGridSize(true);
|
|
|
|
}))
|
|
|
|
..append(new ButtonElement()
|
|
|
|
..text = "-"
|
|
|
|
..onClick.listen((e) {
|
|
|
|
ex.loop.game.changeGridSize(false);
|
|
|
|
}))
|
2018-07-24 11:34:29 +00:00
|
|
|
..append(randomButton
|
|
|
|
..onClick.listen((e) {
|
|
|
|
ex.loop.game.toggleRandomOrder();
|
|
|
|
})));
|
2018-07-20 17:04:10 +00:00
|
|
|
|
|
|
|
// Don't add controls which don't work anyways (for simple examples)
|
2018-07-24 11:34:29 +00:00
|
|
|
if (examples.indexOf(ex) <= 1) return;
|
2018-07-20 17:19:24 +00:00
|
|
|
VariableUpdates loop = (ex.loop as VariableUpdates);
|
2018-07-20 17:04:10 +00:00
|
|
|
|
2018-07-20 17:19:24 +00:00
|
|
|
// Update Speed Slider
|
2018-07-20 17:04:10 +00:00
|
|
|
group
|
|
|
|
..append(new LabelElement()
|
|
|
|
..htmlFor = "update_speed"
|
|
|
|
..innerHtml = "Updates per Second:")
|
|
|
|
..append(new InputElement(type: "range")
|
|
|
|
..id = "update_speed"
|
|
|
|
..min = "1"
|
2018-07-20 17:19:24 +00:00
|
|
|
..max = "50"
|
2018-07-20 17:04:10 +00:00
|
|
|
..value = "3"
|
|
|
|
..step = "1"
|
2018-07-24 11:35:25 +00:00
|
|
|
..onInput.listen((Event e) {
|
|
|
|
loop.MS_PER_UPDATE =
|
|
|
|
(1000 / int.parse((e.target as InputElement).value));
|
|
|
|
}));
|
2018-07-20 16:19:57 +00:00
|
|
|
// querySelector('#reset').onClick.listen((e) => ex.loop.game.reset());
|
|
|
|
// querySelector('#plus').onClick.listen((e) => _changeGrid(-5));
|
|
|
|
// querySelector('#minus').onClick.listen((e) => _changeGrid(5));
|
|
|
|
}
|