Skip to content

Commit

Permalink
Location name placing in map view improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruediger.lunde@gmail.com committed Aug 9, 2016
1 parent 1cde3be commit 6e01276
Showing 1 changed file with 37 additions and 35 deletions.
Expand Up @@ -27,7 +27,7 @@
public class MapEnvironmentViewCtrl extends SimpleEnvironmentViewCtrl { public class MapEnvironmentViewCtrl extends SimpleEnvironmentViewCtrl {


protected Pane envStateView; protected Pane envStateView;
protected Map map; protected MapEnvironment env;
protected String goal; protected String goal;
protected List<String> track; protected List<String> track;
protected double scale = 1; protected double scale = 1;
Expand All @@ -39,30 +39,34 @@ public MapEnvironmentViewCtrl(StackPane parent) {
splitPane.setDividerPosition(0, 0.7); splitPane.setDividerPosition(0, 0.7);
splitPane.setStyle("-fx-background-color: white"); splitPane.setStyle("-fx-background-color: white");
envStateView.setMinWidth(0.0); envStateView.setMinWidth(0.0);
envStateView.widthProperty().addListener((obs, o, n) -> adjustTransform()); envStateView.widthProperty().addListener((obs, o, n) -> update());
envStateView.heightProperty().addListener((obs, o, n) -> adjustTransform()); envStateView.heightProperty().addListener((obs, o, n) -> update());
track = new ArrayList<String>(); track = new ArrayList<String>();
} }

public void setGoal(String goal) { public void setGoal(String goal) {
this.goal = goal; this.goal = goal;
} }


@Override @Override
public void initialize(Environment env) { public void initialize(Environment env) {
if (env instanceof MapEnvironment) { if (env instanceof MapEnvironment) {
map = ((MapEnvironment) env).getMap(); this.env = (MapEnvironment) env;
track.clear(); track.clear();
} }
super.initialize(env); super.initialize(env);
} }


@Override @Override
protected void updateEnvStateView(Environment env) { protected void updateEnvStateView(Environment env) {
envStateView.getChildren().clear(); update();
if (env instanceof MapEnvironment) { }
MapEnvironment mEnv = (MapEnvironment) env;
protected void update() {
if (env != null) {
adjustTransform(); adjustTransform();
envStateView.getChildren().clear();
Map map = env.getMap();
// print connections // print connections
for (String loc1 : map.getLocations()) { for (String loc1 : map.getLocations()) {
Point2D pt1 = map.getPosition(loc1); Point2D pt1 = map.getPosition(loc1);
Expand All @@ -74,12 +78,12 @@ protected void updateEnvStateView(Environment env) {
} }
} }
// print track of first agent // print track of first agent
if (!mEnv.getAgents().isEmpty()) { if (!env.getAgents().isEmpty()) {
String aLoc = mEnv.getAgentLocation(mEnv.getAgents().get(0)); String aLoc = env.getAgentLocation(env.getAgents().get(0));
if (track.isEmpty() || track.get(track.size()-1) != aLoc) if (track.isEmpty() || track.get(track.size() - 1) != aLoc)
track.add(aLoc); track.add(aLoc);
for (int i = 1; i < track.size(); i++) { for (int i = 1; i < track.size(); i++) {
Point2D pt1 = map.getPosition(track.get(i-1)); Point2D pt1 = map.getPosition(track.get(i - 1));
Point2D pt2 = map.getPosition(track.get(i)); Point2D pt2 = map.getPosition(track.get(i));
Shape line = new Line(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY()); Shape line = new Line(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
line.setStroke(Color.RED); line.setStroke(Color.RED);
Expand All @@ -90,14 +94,14 @@ protected void updateEnvStateView(Environment env) {
// print locations // print locations
for (String loc : map.getLocations()) { for (String loc : map.getLocations()) {
Point2D point = map.getPosition(loc); Point2D point = map.getPosition(loc);
Text text = new Text(point.getX() + 5, point.getY() + 10, loc); Text text = new Text(point.getX() + 10 / scale, point.getY(), loc);
text.setFont(new Font(12.0 / scale)); text.setFont(new Font(12.0 / scale));
envStateView.getChildren().add(text); envStateView.getChildren().add(text);
envStateView.getChildren().add(new Circle(point.getX(), point.getY(), 2 / scale)); envStateView.getChildren().add(new Circle(point.getX(), point.getY(), 2 / scale));
} }
// print agent locations // print agent locations
for (Agent agent : mEnv.getAgents()) { for (Agent agent : env.getAgents()) {
String loc = mEnv.getAgentLocation(agent); String loc = env.getAgentLocation(agent);
if (loc != null) { if (loc != null) {
Point2D pt = map.getPosition(loc); Point2D pt = map.getPosition(loc);
envStateView.getChildren().add(new Circle(pt.getX(), pt.getY(), 8 / scale, Color.RED)); envStateView.getChildren().add(new Circle(pt.getX(), pt.getY(), 8 / scale, Color.RED));
Expand All @@ -112,26 +116,24 @@ protected void updateEnvStateView(Environment env) {
} }


private void adjustTransform() { private void adjustTransform() {
if (map != null) { double xMin = Double.POSITIVE_INFINITY;
double xMin = Double.POSITIVE_INFINITY; double xMax = Double.NEGATIVE_INFINITY;
double xMax = Double.NEGATIVE_INFINITY; double yMin = Double.POSITIVE_INFINITY;
double yMin = Double.POSITIVE_INFINITY; double yMax = Double.NEGATIVE_INFINITY;
double yMax = Double.NEGATIVE_INFINITY; for (String loc : env.getMap().getLocations()) {
for (String loc : map.getLocations()) { Point2D point = env.getMap().getPosition(loc);
Point2D point = map.getPosition(loc); xMin = Math.min(xMin, point.getX());
xMin = Math.min(xMin, point.getX()); xMax = Math.max(xMax, point.getX());
xMax = Math.max(xMax, point.getX()); yMin = Math.min(yMin, point.getY());
yMin = Math.min(yMin, point.getY()); yMax = Math.max(yMax, point.getY());
yMax = Math.max(yMax, point.getY());
}
scale = Math.min((envStateView.getWidth() - 150) / (xMax - xMin),
(envStateView.getHeight() - 60) / (yMax - yMin));

envStateView.setScaleY(scale);
envStateView.setTranslateX((scale * (envStateView.getWidth() - xMin - xMax) / 2.0 - 30));
envStateView.setTranslateY((scale * (envStateView.getHeight() - yMin - yMax) / 2.0 - 10));
envStateView.setScaleX(scale);
envStateView.setScaleY(scale);
} }
scale = Math.min((envStateView.getWidth() - 150) / (xMax - xMin),
(envStateView.getHeight() - 60) / (yMax - yMin));

envStateView.setScaleY(scale);
envStateView.setTranslateX((scale * (envStateView.getWidth() - xMin - xMax) / 2.0 - 30));
envStateView.setTranslateY((scale * (envStateView.getHeight() - yMin - yMax) / 2.0 - 10));
envStateView.setScaleX(scale);
envStateView.setScaleY(scale);
} }
} }

0 comments on commit 6e01276

Please sign in to comment.