Skip to content

Commit

Permalink
Fix icons placement edge-changes
Browse files Browse the repository at this point in the history
When two or more pieces of furniture have the same position and are
associated with an HA entity, they overlap each other perfectly and so
the algorithm that tries to move them doesn't know where to. Added a
hack that in this case, they be moved by a pseudo-random direction.
  • Loading branch information
shmuelzon committed Jan 10, 2025
1 parent bee73f6 commit be322d3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/com/shmuelzon/HomeAssistantFloorPlan/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public enum EntityAction {MORE_INFO, NONE, TOGGLE}
private boolean useExistingRenders;

private class Entity {
public String id;
public String name;
public Point2d position;
public EntityDisplayType displayType;
Expand All @@ -101,7 +102,8 @@ private class Entity {
public boolean alwaysOn;
public boolean isRgb;

public Entity(String name, Point2d position, EntityDisplayType defaultDisplayType, EntityAction defaultTapAction, String title) {
public Entity(String id, String name, Point2d position, EntityDisplayType defaultDisplayType, EntityAction defaultTapAction, String title) {
this.id = id;
this.name = name;
this.position = position;
this.displayType = EntityDisplayType.valueOf(settings.get(name + "." + CONTROLLER_ENTITY_DISPLAY_TYPE, defaultDisplayType.name()));
Expand Down Expand Up @@ -885,8 +887,9 @@ private void generateLightEntities(Map<String, Entity> homeAssistantEntities) {
lightsCenter.add(getFurniture2dLocation(light));
lightsCenter.scale(1.0 / lightsList.size());

String name = lightsList.get(0).getName();
homeAssistantEntities.put(name, new Entity(name, lightsCenter, EntityDisplayType.ICON, EntityAction.TOGGLE,
HomeLight light = lightsList.get(0);
String name = light.getName();
homeAssistantEntities.put(name, new Entity(light.getId(), name, lightsCenter, EntityDisplayType.ICON, EntityAction.TOGGLE,
lightsList.get(0).getDescription()));
}
}
Expand All @@ -913,7 +916,7 @@ private void generateSensorEntities(Map<String, Entity> homeAssistantEntities) {
Point2d location = getFurniture2dLocation(piece);
String name = piece.getName();

homeAssistantEntities.put(name, new Entity(name, location,
homeAssistantEntities.put(name, new Entity(piece.getId(), name, location,
name.startsWith("sensor.") ? EntityDisplayType.LABEL : EntityDisplayType.ICON,
isHomeAssistantEntityActionable(piece.getName()) ? EntityAction.TOGGLE : EntityAction.MORE_INFO,
piece.getDescription()));
Expand Down Expand Up @@ -990,6 +993,12 @@ private void separateStateIcons(Set<Entity> entities) {

for (Entity entity : entities) {
Vector2d direction = new Vector2d(entity.position.x - centerPostition.x, entity.position.y - centerPostition.y);

if (direction.length() == 0) {
double[] randomRepeatableDirection = { entity.id.hashCode(), entity.name.hashCode() };
direction.set(randomRepeatableDirection);
}

direction.normalize();
direction.scale(STEP_SIZE);
entity.position.add(direction);
Expand Down

0 comments on commit be322d3

Please sign in to comment.