Examples - Add Grid Size Up/Down Buttons

This commit is contained in:
Marty Oehme 2018-07-20 19:51:13 +02:00
parent d11a47abc5
commit 76b0e68cf1
2 changed files with 42 additions and 10 deletions

View file

@ -11,11 +11,11 @@ class Game {
double _oscill = 0.1;
bool _oscillDir = true; // oscillate upwards (true) or downwards (false)
double _OSCILLSPEED = 0.1;
double _OSCILLSPEED = 1.0;
bool _fwd = true;
Game(CanvasElement this.canvas) {
grid = _buildGrid(5, 5, new Color(255, 0, 255));
grid = _buildGrid(5, new Color(255, 255, 255));
}
// In-World Logic Updates
@ -33,20 +33,27 @@ class Game {
ctx.clearRect(0, 0, canvas.width, canvas.height);
_grid_foreach((x, y) {
Color col = _oscillate(grid[y][x]);
Color col = _getOscillatedCol(grid[y][x]);
ctx.setFillColorRgb(col.r, col.g, col.b);
ctx.fillRect(x*brickW, y*brickH, brickW, brickH);
});
// This should usually be place in update()
// Placed here to highlight render speed changes for some examples
// See dirty flag Loop for details
_oscillate();
}
// Slightly oscillating colors to highlight rendering updates
Color _oscillate(Color col) {
void _oscillate() {
if(_oscill >= 50.0 || _oscill <= -50.0) {
_oscillDir = !_oscillDir;
_oscill = max( min(_oscill, 49.0), -49.0);
}
else _oscillDir == true ? _oscill += _OSCILLSPEED : _oscill -= _OSCILLSPEED;
}
Color _getOscillatedCol(Color col) {
int o = _oscill.toInt();
return new Color(col.r + o, col.g + o, col.b + o);
}
@ -59,16 +66,29 @@ class Game {
}
}
List<List<Color>> _buildGrid(int w, int h, Color col) {
List<List<Color>> grid = new List(h);
for (int y = 0; y < h; y++) {
grid[y] = new List(w);
for (int x = 0; x < w; x++) {
List<List<Color>> _buildGrid(int size, Color col) {
List<List<Color>> grid = new List(size);
for (int y = 0; y < size; y++) {
grid[y] = new List(size);
for (int x = 0; x < size; x++) {
grid[y][x] = col;
}
}
return grid;
}
// Grid Size Button Implementation
// true makes squares larger
// false makes squares smaller
void changeGridSize(bool larger) {
if(larger) {
if(grid.length <= 5) return;
this.grid = _buildGrid(grid.length - 5, Color(255, 255, 255));
} else {
if(grid.length >= 60) return;
this.grid = _buildGrid(grid.length + 5, Color(255, 255, 255));
}
}
}
class Color {

View file

@ -112,7 +112,19 @@ void addControls(Example ex) {
..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);
}))
);
// Don't add controls which don't work anyways (for simple examples)
if(examples.indexOf(ex) <= 1) return;