From dd18fc3bc7ded19b134e26867988bdb48455d2bb Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 25 Aug 2018 17:23:06 +0200 Subject: [PATCH 01/10] Make Engine Gridsize configurable --- lib/src/Engine.dart | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index be3f31f..b05f79c 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -1,4 +1,5 @@ import 'dart:html' as html; +import 'dart:math'; import 'package:rules_of_living/src/Grid.dart'; @@ -28,9 +29,10 @@ class Engine { // ms stuck in updateloop after which game will declare itself unresponsive final int SAFETY_TIMEOUT = 2000; - // Grid Size TODO add as configurable option - static final GRID_X = 100; - static final GRID_Y = 100; + /// Grid Size + /// + /// Number of cells on x coordinate and y coordinate. Can be set individually. + Point gridSize = Point(100, 100); num _updateLag = 0.0; num _drawLag = 0.0; @@ -41,10 +43,12 @@ class Engine { /// be used if no canvas was defined at engine creation and it should be /// rendered later. html.CanvasElement canvas; - Grid _grid = new Grid(GRID_X, GRID_Y); + Grid _grid; bool running = false; Engine([this.canvas]) { + _grid = Grid(gridSize.x, gridSize.y); + _elapsed.start(); _grid.addPattern(amount: 15, dispersal: 5); html.window.animationFrame.then(animFrame); @@ -61,7 +65,7 @@ class Engine { } void clear() { - _grid = new Grid(GRID_X, GRID_Y); + _grid = new Grid(gridSize.x, gridSize.y); running = false; } From 5e8f83cf8a6cb186dc4d77bec58aa3e57b86ad53 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 19:33:17 +0200 Subject: [PATCH 02/10] Refactor EngineService to be able to inject custom Engine fixes #38 --- lib/service/engine_service.dart | 7 +++--- test/service/engine_service_test.dart | 32 ++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index d7b0bed..f43cd55 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -5,10 +5,10 @@ import 'package:rules_of_living/src/Engine.dart'; class EngineService { Engine _engine; - Engine get engine => _engine ?? createEngine(null); + Engine get engine => _engine ?? getEngine(Engine()); - Engine createEngine(html.CanvasElement canvas) { - _engine = Engine(canvas); + Engine getEngine(Engine engine) { + _engine = engine; return _engine; } @@ -45,5 +45,4 @@ class EngineService { } bool get isRunning => engine.running; - } diff --git a/test/service/engine_service_test.dart b/test/service/engine_service_test.dart index 9d4a226..209fd47 100644 --- a/test/service/engine_service_test.dart +++ b/test/service/engine_service_test.dart @@ -1,19 +1,41 @@ @TestOn('browser') - import 'package:test/test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/src/Engine.dart'; import 'package:rules_of_living/service/engine_service.dart'; +class MockEngine extends Mock implements Engine {} + void main() { EngineService sut; + MockEngine me; setUp(() { + me = MockEngine(); sut = EngineService(); }); + group("Dependency Injection", () { + test("EngineService accesses the Engine defined in getEngine", () { + sut.getEngine(me); - test("EngineService creates an engine on demand", () { - expect(sut.engine, isNotNull); + Engine result = sut.engine; + expect(result, equals(me)); + }); }); + group("caching", () { + test("EngineService creates an engine on demand", () { + Engine result = sut.engine; + expect(result, TypeMatcher()); + }); - test("EngineService returns the cached engine on subsequent requests", () { - expect(sut.engine, allOf(isNotNull, equals(sut.engine))); + test("EngineService returns the cached engine on subsequent requests", () { + Engine result = sut.engine; + expect(sut.engine, equals(result)); + }); + test("caching can be overriden by providing a custom engine", () { + Engine first = sut.engine; + sut.getEngine(me); + Engine second = sut.engine; + expect(second, isNot(equals(first))); + }); }); } From 9886f13d69bf07eb6e46e4414c633bda91cfc0ee Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 19:55:30 +0200 Subject: [PATCH 03/10] Add grid changing function --- lib/service/configuration_service.dart | 10 ++++- test/service/configuration_service_test.dart | 44 +++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index 93b89d7..8ca89ec 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:rules_of_living/service/engine_service.dart'; class ConfigurationService { @@ -26,4 +28,10 @@ class ConfigurationService { void toggleGrid() { showGrid = !showGrid; } -} \ No newline at end of file + + void setGridSize({int x, int y}) { + x = x ?? engineService.engine.gridSize.x; + y = y ?? engineService.engine.gridSize.y; + engineService.engine.gridSize = Point(x, y); + } +} diff --git a/test/service/configuration_service_test.dart b/test/service/configuration_service_test.dart index 1afa50a..f28f6e8 100644 --- a/test/service/configuration_service_test.dart +++ b/test/service/configuration_service_test.dart @@ -1,30 +1,46 @@ +import 'dart:math'; + import 'package:rules_of_living/src/Engine.dart'; @TestOn('browser') - import 'package:test/test.dart'; import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; import 'package:mockito/mockito.dart'; class MockEngine extends Mock implements Engine {} -class MockEngineService extends Mock implements EngineService { - MockEngine _engine = MockEngine(); - @override - Engine get engine => _engine; -} void main() { - group("simulation speed", () { - ConfigurationService sut; - MockEngineService mes; - setUp(() { - mes = MockEngineService(); - sut = ConfigurationService(mes); - }); + ConfigurationService sut; + EngineService engineService; + MockEngine me; + setUp(() { + me = MockEngine(); + engineService = EngineService(); + engineService.getEngine(me); + sut = ConfigurationService(engineService); + }); + group("simulation speed", () { test("speed changes propagate to engine", () { sut.simSpeed = 312; - verify(mes.engine.stepsPerSecond=312); + verify(me.stepsPerSecond = 312); + }); + }); + + group("grid size", () { + test("grid changes are sent to engine", () { + sut.setGridSize(x: 512, y: 388); + verify(me.gridSize = Point(512, 388)); + }); + test("grid can be changed solely on x axis", () { + when(me.gridSize).thenReturn(Point(100, 100)); + sut.setGridSize(x: 555); + verify(me.gridSize = Point(555, 100)); + }); + test("grid can be changed solely on y axis", () { + when(me.gridSize).thenReturn(Point(100, 100)); + sut.setGridSize(y: 556); + verify(me.gridSize = Point(100, 556)); }); }); } From 873bd9c8814b8105b50611d00bc389d09e65b924 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 20:13:19 +0200 Subject: [PATCH 04/10] Allow Optional Injection of grid size into engine on creation --- lib/src/Engine.dart | 6 ++--- test/service/engine_service_test.dart | 6 ++--- test/src/engine_test.dart | 33 +++++++++++++++------------ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index b05f79c..aa2a36c 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -46,8 +46,8 @@ class Engine { Grid _grid; bool running = false; - Engine([this.canvas]) { - _grid = Grid(gridSize.x, gridSize.y); + Engine([x = 100, y = 100, this.canvas]) { + _grid = Grid(x, y); _elapsed.start(); _grid.addPattern(amount: 15, dispersal: 5); @@ -115,7 +115,7 @@ class Engine { /// the internal engine processing. Does not do anything if no canvas is /// defined. void render([num interp]) { - if(canvas != null) _grid.render(canvas, interp); + if (canvas != null) _grid.render(canvas, interp); } void addPattern( diff --git a/test/service/engine_service_test.dart b/test/service/engine_service_test.dart index 209fd47..cbd9655 100644 --- a/test/service/engine_service_test.dart +++ b/test/service/engine_service_test.dart @@ -1,8 +1,8 @@ +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/src/Engine.dart'; @TestOn('browser') import 'package:test/test.dart'; -import 'package:mockito/mockito.dart'; -import 'package:rules_of_living/src/Engine.dart'; -import 'package:rules_of_living/service/engine_service.dart'; class MockEngine extends Mock implements Engine {} diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart index 43ea13e..7707418 100644 --- a/test/src/engine_test.dart +++ b/test/src/engine_test.dart @@ -10,23 +10,26 @@ void main() { setUp(() { sut = Engine(); }); + group("canvas", () { + test("Engine can be instantiated without canvas", () { + expect(sut, isNot(throwsNoSuchMethodError)); + }); - test("Engine can be instantiated without canvas", () { - expect(sut, isNot(throwsNoSuchMethodError)); - }); + test("Engine does not throw errors when calling render directly", () { + // anonymous function necessary since throws can not use functions with args + expect(() => sut.render, isNot(throwsNoSuchMethodError)); + }); - test("Engine does not throw errors when calling render directly", () { - // anonymous function necessary since throws can not use functions with args - expect(() => sut.render, isNot(throwsNoSuchMethodError)); - }); + test("Engine does not throw errors when processing without attached canvas", + () { + // anonymous function necessary since throws can not use functions with args + expect(() => sut.process, isNot(throwsNoSuchMethodError)); + }); - test("Engine does not throw errors when processing without attached canvas", () { - // anonymous function necessary since throws can not use functions with args - expect(() => sut.process, isNot(throwsNoSuchMethodError)); + test("setCanvas allows setting a canvas for an engine at any point", () { + sut.canvas = new html.CanvasElement(); + expect(sut.canvas, isNotNull); + }); }); - - test("setCanvas allows setting a canvas for an engine at any point", () { - sut.canvas = new html.CanvasElement(); - expect(sut.canvas, isNotNull); }); -} \ No newline at end of file +} From 04d61bfa029128fd705ad879f5cb0dbb5286f66c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 20:18:19 +0200 Subject: [PATCH 05/10] Error out on bad gridsize input --- lib/src/Engine.dart | 7 ++++++- test/src/engine_test.dart | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index aa2a36c..b06e53a 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -32,7 +32,12 @@ class Engine { /// Grid Size /// /// Number of cells on x coordinate and y coordinate. Can be set individually. - Point gridSize = Point(100, 100); + Point get gridSize => Point(_grid.w, _grid.h); + void set gridSize(Point value) { + if (value.x <= 0 || value.y <= 0) + throw ArgumentError("grid size must not be smaller than 1"); + _grid = Grid(value.x, value.y); + } num _updateLag = 0.0; num _drawLag = 0.0; diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart index 7707418..fcfefc2 100644 --- a/test/src/engine_test.dart +++ b/test/src/engine_test.dart @@ -1,7 +1,7 @@ import 'dart:html' as html; +import 'dart:math'; @TestOn('browser') - import 'package:rules_of_living/src/Engine.dart'; import 'package:test/test.dart'; @@ -31,5 +31,12 @@ void main() { expect(sut.canvas, isNotNull); }); }); + group("gridSize", () { + test("zero gridSizes throw ArgumentErrors", () { + expect(() => sut.gridSize = Point(0, 5), throwsArgumentError); + }); + test("negative gridSizes throw ArgumentErrors", () { + expect(() => sut.gridSize = Point(1, -5), throwsArgumentError); + }); }); } From a92b864dfa9b5cb2f9bb4aa5aab0199863dd47a6 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 20:50:12 +0200 Subject: [PATCH 06/10] Extract EngineService into separate service --- lib/app_component.dart | 13 +++++- lib/components/controls_component.dart | 16 +++---- lib/components/simulation_component.dart | 8 ++-- lib/service/configuration_service.dart | 12 ++--- lib/service/control_service.dart | 43 ++++++++++++++++++ lib/service/engine_service.dart | 47 +++----------------- test/service/configuration_service_test.dart | 8 ++-- test/service/engine_service_test.dart | 6 +-- 8 files changed, 86 insertions(+), 67 deletions(-) create mode 100644 lib/service/control_service.dart diff --git a/lib/app_component.dart b/lib/app_component.dart index e9b44cc..c9f05e1 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -5,6 +5,7 @@ import 'package:rules_of_living/components/controls_component.dart'; import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/simulation_component.dart'; import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; @Component( @@ -20,8 +21,16 @@ import 'package:rules_of_living/service/engine_service.dart'; ControlsComponent, ConfigurationComponent ], - providers: [materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService)], - styleUrls: const ['package:angular_components/app_layout/layout.scss.css', 'app_component.css'], + providers: [ + materialProviders, + ClassProvider(EngineService), + ClassProvider(ConfigurationService), + ClassProvider(EngineService) + ], + styleUrls: const [ + 'package:angular_components/app_layout/layout.scss.css', + 'app_component.css' + ], ) class AppComponent { var name = "World"; diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 511a7d4..954d94b 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -1,6 +1,6 @@ import 'package:angular/angular.dart'; import 'package:angular_components/angular_components.dart'; -import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; @Component( selector: 'sim-controls', @@ -15,27 +15,27 @@ import 'package:rules_of_living/service/engine_service.dart'; styleUrls: const ["controls_component.css"], ) class ControlsComponent { - final EngineService engine; + final ControlService ctrl; - ControlsComponent(this.engine); + ControlsComponent(this.ctrl); void onStartClicked() { - engine.toggleRunning(); + ctrl.toggleRunning(); } void onStepClicked() { - engine.step(); + ctrl.step(); } void onResetClicked() { - engine.reset(); + ctrl.reset(); } void onRandomClicked() { - engine.addRandomPattern(); + ctrl.addRandomPattern(); } void onClearClicked() { - engine.clear(); + ctrl.clear(); } } diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 12b7844..99b3f02 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -1,7 +1,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; -import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; @Component( selector: 'gol-simulation', @@ -10,9 +10,9 @@ import 'package:rules_of_living/service/engine_service.dart'; providers: [], ) class SimulationComponent implements OnInit { - final EngineService engineService; + final ControlService ctrl; - SimulationComponent(this.engineService); + SimulationComponent(this.ctrl); @override void ngOnInit() { @@ -29,6 +29,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - engineService.canvas = canvas; + ctrl.canvas = canvas; } } diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index 8ca89ec..edee10c 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -3,7 +3,7 @@ import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; class ConfigurationService { - final EngineService engineService; + final EngineService _es; bool showGrid; @@ -17,10 +17,10 @@ class ConfigurationService { int get simSpeed => _simSpeed; void set simSpeed(int val) { _simSpeed = val; - engineService.engine.stepsPerSecond = simSpeed; + _es.engine.stepsPerSecond = simSpeed; } - ConfigurationService(this.engineService) { + ConfigurationService(this._es) { showGrid = false; simSpeed = 5; } @@ -30,8 +30,8 @@ class ConfigurationService { } void setGridSize({int x, int y}) { - x = x ?? engineService.engine.gridSize.x; - y = y ?? engineService.engine.gridSize.y; - engineService.engine.gridSize = Point(x, y); + x = x ?? _es.engine.gridSize.x; + y = y ?? _es.engine.gridSize.y; + _es.engine.gridSize = Point(x, y); } } diff --git a/lib/service/control_service.dart b/lib/service/control_service.dart new file mode 100644 index 0000000..abc1f57 --- /dev/null +++ b/lib/service/control_service.dart @@ -0,0 +1,43 @@ +import 'dart:html' as html; + +import 'package:rules_of_living/service/engine_service.dart'; + +class ControlService { + EngineService _es; + + ControlService(this._es); + + void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas; + html.CanvasElement get canvas => _es.engine.canvas; + + void run() { + _es.engine.running = true; + } + + void stop() { + _es.engine.running = false; + } + + void toggleRunning() { + _es.engine.running = !_es.engine.running; + } + + void step() { + _es.engine.step(); + } + + void reset() { + _es.engine.reset(); + } + + void addRandomPattern() { + _es.engine.running = false; + _es.engine.addPattern(); + } + + void clear() { + _es.engine.clear(); + } + + bool get isRunning => _es.engine.running; +} diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index f43cd55..8168735 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -1,48 +1,15 @@ -import 'dart:html' as html; - import 'package:rules_of_living/src/Engine.dart'; class EngineService { - Engine _engine; + Engine _uncachedEngineAccess; - Engine get engine => _engine ?? getEngine(Engine()); - - Engine getEngine(Engine engine) { - _engine = engine; - return _engine; + Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine()); + void set engine(Engine newEngine) { + _uncachedEngineAccess = newEngine; } - void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; - html.CanvasElement get canvas => engine.canvas; - - void run() { - engine.running = true; + Engine _setCachedAndReturn(Engine newEngine) { + engine = newEngine; + return newEngine; } - - void stop() { - engine.running = false; - } - - void toggleRunning() { - engine.running = !engine.running; - } - - void step() { - engine.step(); - } - - void reset() { - engine.reset(); - } - - void addRandomPattern() { - engine.running = false; - engine.addPattern(); - } - - void clear() { - engine.clear(); - } - - bool get isRunning => engine.running; } diff --git a/test/service/configuration_service_test.dart b/test/service/configuration_service_test.dart index f28f6e8..44e23b2 100644 --- a/test/service/configuration_service_test.dart +++ b/test/service/configuration_service_test.dart @@ -1,11 +1,11 @@ import 'dart:math'; +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/src/Engine.dart'; @TestOn('browser') import 'package:test/test.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; -import 'package:rules_of_living/service/engine_service.dart'; -import 'package:mockito/mockito.dart'; class MockEngine extends Mock implements Engine {} @@ -16,7 +16,7 @@ void main() { setUp(() { me = MockEngine(); engineService = EngineService(); - engineService.getEngine(me); + engineService.engine = me; sut = ConfigurationService(engineService); }); diff --git a/test/service/engine_service_test.dart b/test/service/engine_service_test.dart index cbd9655..a792b93 100644 --- a/test/service/engine_service_test.dart +++ b/test/service/engine_service_test.dart @@ -14,8 +14,8 @@ void main() { sut = EngineService(); }); group("Dependency Injection", () { - test("EngineService accesses the Engine defined in getEngine", () { - sut.getEngine(me); + test("EngineService can be passed a custom Engine", () { + sut.engine = me; Engine result = sut.engine; expect(result, equals(me)); @@ -33,7 +33,7 @@ void main() { }); test("caching can be overriden by providing a custom engine", () { Engine first = sut.engine; - sut.getEngine(me); + sut.engine = me; Engine second = sut.engine; expect(second, isNot(equals(first))); }); From b6919cff6bae126a05206086d57be3b6f56dce81 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 20:55:55 +0200 Subject: [PATCH 07/10] Move Canvas setting to configuration service Fix #39 --- lib/components/simulation_component.dart | 8 ++++---- lib/service/configuration_service.dart | 12 ++++++++---- lib/service/control_service.dart | 5 ----- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 99b3f02..312eef4 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -1,7 +1,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; -import 'package:rules_of_living/service/control_service.dart'; +import 'package:rules_of_living/service/configuration_service.dart'; @Component( selector: 'gol-simulation', @@ -10,9 +10,9 @@ import 'package:rules_of_living/service/control_service.dart'; providers: [], ) class SimulationComponent implements OnInit { - final ControlService ctrl; + final ConfigurationService config; - SimulationComponent(this.ctrl); + SimulationComponent(this.config); @override void ngOnInit() { @@ -29,6 +29,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - ctrl.canvas = canvas; + config.canvas = canvas; } } diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index edee10c..880cf45 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -1,3 +1,4 @@ +import 'dart:html' as html; import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; @@ -9,6 +10,11 @@ class ConfigurationService { int _simSpeed; + ConfigurationService(this._es) { + showGrid = false; + simSpeed = 5; + } + /// Simulation Speed /// /// Sets the number of updates the simulation takes per second. Can range from @@ -20,10 +26,8 @@ class ConfigurationService { _es.engine.stepsPerSecond = simSpeed; } - ConfigurationService(this._es) { - showGrid = false; - simSpeed = 5; - } + void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas; + html.CanvasElement get canvas => _es.engine.canvas; void toggleGrid() { showGrid = !showGrid; diff --git a/lib/service/control_service.dart b/lib/service/control_service.dart index abc1f57..0066106 100644 --- a/lib/service/control_service.dart +++ b/lib/service/control_service.dart @@ -1,5 +1,3 @@ -import 'dart:html' as html; - import 'package:rules_of_living/service/engine_service.dart'; class ControlService { @@ -7,9 +5,6 @@ class ControlService { ControlService(this._es); - void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas; - html.CanvasElement get canvas => _es.engine.canvas; - void run() { _es.engine.running = true; } From 3a1ba1c1e95c9a173fe70a34c47c7c19aa30cf18 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 23:02:48 +0200 Subject: [PATCH 08/10] Fix Wrong ControlService being provided to Angular --- lib/app_component.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/app_component.dart b/lib/app_component.dart index c9f05e1..ae8a931 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -25,7 +25,7 @@ import 'package:rules_of_living/service/engine_service.dart'; materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService), - ClassProvider(EngineService) + ClassProvider(ControlService) ], styleUrls: const [ 'package:angular_components/app_layout/layout.scss.css', From 800c85d14fb5c192cb5170af6f74d933819a7d2b Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 23:03:13 +0200 Subject: [PATCH 09/10] Fix wrong variable being accessed by controls_component --- lib/components/controls_component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/components/controls_component.html b/lib/components/controls_component.html index 42e79f7..b4c43f3 100644 --- a/lib/components/controls_component.html +++ b/lib/components/controls_component.html @@ -1,7 +1,7 @@
- + From b1221c7c84f8aa5df4fc7a620f0fcb7215575969 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 27 Aug 2018 23:03:35 +0200 Subject: [PATCH 10/10] Add Grid Size Input to Configuration Bar Fixes #37 --- lib/components/configuration_component.dart | 25 ++++++++++++++++++--- lib/components/configuration_component.html | 20 +++++++++++++++-- lib/service/configuration_service.dart | 2 ++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/components/configuration_component.dart b/lib/components/configuration_component.dart index bd3ae0c..cc1ecd1 100644 --- a/lib/components/configuration_component.dart +++ b/lib/components/configuration_component.dart @@ -1,6 +1,8 @@ import 'package:angular/angular.dart'; import 'package:angular_components/material_button/material_button.dart'; import 'package:angular_components/material_icon/material_icon.dart'; +import 'package:angular_components/material_input/material_input.dart'; +import 'package:angular_components/material_input/material_number_accessor.dart'; import 'package:angular_components/material_slider/material_slider.dart'; import 'package:angular_components/material_tooltip/material_tooltip.dart'; import 'package:rules_of_living/service/configuration_service.dart'; @@ -8,18 +10,35 @@ import 'package:rules_of_living/service/configuration_service.dart'; @Component( selector: "configuration", templateUrl: "configuration_component.html", - styleUrls: ["configuration_component.css"], + styleUrls: [ + "configuration_component.css" + ], directives: [ MaterialButtonComponent, MaterialIconComponent, MaterialSliderComponent, - MaterialTooltipDirective + MaterialTooltipDirective, + materialInputDirectives, + materialNumberInputDirectives, + NgModel ]) class ConfigurationComponent { final ConfigurationService config; + int get width => config.gridSize.x; + void set width(num value) { + if (value == null || value <= 0) return; + config.setGridSize(x: value.toInt()); + } + + int get height => config.gridSize.y; + void set height(num value) { + if (value == null || value <= 0) return; + config.setGridSize(y: value.toInt()); + } + int get simSpeed => config.simSpeed; - int set simSpeed(int value) => config.simSpeed = value; + void set simSpeed(int value) => config.simSpeed = value; String get speedSliderTooltip => "Simulation Speed: $simSpeed"; diff --git a/lib/components/configuration_component.html b/lib/components/configuration_component.html index a17261d..d8a3e8b 100644 --- a/lib/components/configuration_component.html +++ b/lib/components/configuration_component.html @@ -1,5 +1,21 @@
- - + + + + Ruleset: + + +
\ No newline at end of file diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index 880cf45..e11a5e8 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -38,4 +38,6 @@ class ConfigurationService { y = y ?? _es.engine.gridSize.y; _es.engine.gridSize = Point(x, y); } + + Point get gridSize => _es.engine.gridSize; }