Remove ConfigurationService

Replaced with direct access to both EngineService and SimulationService.
This commit is contained in:
Unknown 2018-10-18 12:51:35 +02:00
parent 99ead8691b
commit 58971016da
7 changed files with 41 additions and 108 deletions

View file

@ -4,7 +4,6 @@ import 'package:rules_of_living/components/configuration_component.dart';
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/engine_service.dart';
import 'package:rules_of_living/service/simulation_service.dart';
@ -24,7 +23,6 @@ import 'package:rules_of_living/service/simulation_service.dart';
providers: [
materialProviders,
ClassProvider(EngineService),
ClassProvider(ConfigurationService),
ClassProvider(SimulationService)
],
styleUrls: const [

View file

@ -1,3 +1,5 @@
import 'dart:math';
import 'package:angular/angular.dart';
import 'package:angular_components/material_button/material_button.dart';
import 'package:angular_components/material_icon/material_icon.dart';
@ -5,7 +7,8 @@ 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';
import 'package:rules_of_living/service/engine_service.dart';
import 'package:rules_of_living/service/simulation_service.dart';
@Component(
selector: "configuration",
@ -23,28 +26,29 @@ import 'package:rules_of_living/service/configuration_service.dart';
NgModel
])
class ConfigurationComponent {
final ConfigurationService config;
final EngineService engine;
final SimulationService sim;
int get width => config.gridSize.x;
int get width => sim.gridSize.x;
void set width(num value) {
if (value == null || value <= 0) return;
config.setGridSize(x: value.toInt());
sim.gridSize = Point(value, sim.gridSize.y);
}
int get height => config.gridSize.y;
int get height => sim.gridSize.y;
void set height(num value) {
if (value == null || value <= 0) return;
config.setGridSize(y: value.toInt());
sim.gridSize = Point(sim.gridSize.x, value);
}
int get simSpeed => config.simSpeed;
void set simSpeed(int value) => config.simSpeed = value;
int get simSpeed => engine.simSpeed;
void set simSpeed(int value) => engine.simSpeed = value;
String get speedSliderTooltip => "Simulation Speed: $simSpeed";
ConfigurationComponent(this.config);
ConfigurationComponent(this.engine, this.sim);
void onEdgesClicked() {
config.toggleGrid();
engine.toggleGrid();
}
}

View file

@ -35,6 +35,7 @@ class ControlsComponent {
void onRandomClicked() {
sim.addRandomPattern();
engine.stop();
}
void onClearClicked() {

View file

@ -1,7 +1,7 @@
import 'dart:html' as html;
import 'package:angular/angular.dart';
import 'package:rules_of_living/service/configuration_service.dart';
import 'package:rules_of_living/service/engine_service.dart';
@Component(
selector: 'gol-simulation',
@ -10,9 +10,9 @@ import 'package:rules_of_living/service/configuration_service.dart';
providers: [],
)
class SimulationComponent implements OnInit {
final ConfigurationService config;
final EngineService engine;
SimulationComponent(this.config);
SimulationComponent(this.engine);
@override
void ngOnInit() {
@ -29,6 +29,6 @@ class SimulationComponent implements OnInit {
the canvas did not load correctly :(
''', canvas.width / 2 - 50, canvas.height / 2);
config.canvas = canvas;
engine.canvas = canvas;
}
}

View file

@ -1,44 +0,0 @@
import 'dart:html' as html;
import 'dart:math';
import 'package:rules_of_living/service/engine_service.dart';
import 'package:rules_of_living/service/simulation_service.dart';
class ConfigurationService {
final EngineService _engine;
final SimulationService _sim;
bool showGrid;
int _simSpeed;
ConfigurationService(this._engine, this._sim) {
showGrid = false;
simSpeed = 5;
}
/// Simulation Speed
///
/// Sets the number of updates the simulation takes per second. Can range from
/// 1 to arbitrarily high numbers (though setting it too high can potentially
/// make the app brittle).
int get simSpeed => _simSpeed;
void set simSpeed(int val) {
_simSpeed = val;
//TODO make method in EngineService to respect Demeter
_engine.engine.stepsPerSecond = simSpeed;
}
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
html.CanvasElement get canvas => _engine.engine.canvas;
void toggleGrid() {
showGrid = !showGrid;
}
void setGridSize({int x, int y}) {
_sim.gridSize = Point<int>(x ?? gridSize.x, y ?? gridSize.y);
}
Point<int> get gridSize => _sim.gridSize;
}

View file

@ -1,8 +1,14 @@
import 'dart:html' as html;
import 'package:rules_of_living/src/Engine.dart';
class EngineService {
Engine _uncachedEngineAccess;
EngineService() {
simSpeed = 5;
}
Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
void set engine(Engine newEngine) {
_uncachedEngineAccess = newEngine;
@ -29,6 +35,22 @@ class EngineService {
engine.step();
}
/// Simulation Speed
///
/// Sets the number of updates the simulation takes per second. Can range from
/// 1 to arbitrarily high numbers (though setting it too high can potentially
/// make the app brittle).
int get simSpeed => engine.stepsPerSecond;
void set simSpeed(int val) => engine.stepsPerSecond = val;
//TODO split into RenderService when rendering is decoupled from engine.
html.CanvasElement get canvas => engine.canvas;
void set canvas(html.CanvasElement canvas) => engine.canvas = canvas;
void toggleGrid() {
engine.toggleEdgeRendering();
}
void reset() {
engine.reset();
}

View file

@ -1,48 +0,0 @@
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/service/simulation_service.dart';
import 'package:rules_of_living/src/Engine.dart';
@TestOn('browser')
import 'package:test/test.dart';
class MockEngine extends Mock implements Engine {}
void main() {
ConfigurationService sut;
EngineService engineService;
SimulationService simService;
MockEngine me;
setUp(() {
me = MockEngine();
engineService = EngineService();
engineService.engine = me;
sut = ConfigurationService(engineService, simService);
});
group("simulation speed", () {
test("speed changes propagate to engine", () {
sut.simSpeed = 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));
});
});
}