browserloops-examples/web/main.dart

154 lines
4.4 KiB
Dart
Raw Normal View History

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';
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
CanvasElement baseCanvas = new CanvasElement(width: 480, height: 480);
2018-07-20 09:58:11 +00:00
List<Example> examples = [
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;
Example(this.name, this.query, this.loop);
CanvasElement get canvas {
if(loop != null) return loop.game.canvas;
return new CanvasElement(width: 480, height: 480);
}
set canvas(CanvasElement canvas) {
if(loop != null) loop.game.canvas = canvas;
}
2018-07-20 09:58:11 +00:00
}
void main() {
examples.forEach((ex) {
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);
print("${example.query} appended to DOM");
2018-07-20 09:58:11 +00:00
}
void removeFromDOM(Example example) {
2018-07-20 09:58:11 +00:00
querySelector(example.query).children.clear();
print("${example.query} removed from DOM");
2018-07-20 09:58:11 +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
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);
ctx.setFillColorRgb(183, 20, 39);
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) {
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) {
resetExample(ex);
addControls(ex);
} else {
resetExample(ex);
2018-07-20 09:58:11 +00:00
}
});
}
void addControls(Example ex) {
DivElement group = new DivElement();
2018-07-24 11:34:29 +00:00
ButtonElement randomButton = new ButtonElement()..text = "Random/Ordered";
querySelector(ex.query).append(group
..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();
}))
..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();
})));
// 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:19:24 +00:00
// Update Speed Slider
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"
..value = "3"
..step = "1"
..onInput.listen((Event e) {
2018-07-20 17:19:24 +00:00
loop.MS_PER_UPDATE = (1000 / int.parse((e.target as InputElement).value));
}));
// querySelector('#reset').onClick.listen((e) => ex.loop.game.reset());
// querySelector('#plus').onClick.listen((e) => _changeGrid(-5));
// querySelector('#minus').onClick.listen((e) => _changeGrid(5));
}