Skip to content

Commit

Permalink
clearer naming
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Dec 9, 2023
1 parent 4d100bb commit 8dd147d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private Matrix4d getEndEffectorTargetPose() {
}

public Entity getChildTarget() {
return getEntity().findChildNamed(TARGET_NAME);
return getEntity().findChild(TARGET_NAME);
}

/**
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/com/marginallyclever/robotoverlord/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String getUniqueChildName(Entity e) {
}

@Deprecated
public void addEntity(int index, Entity e) {
public void addChild(int index, Entity e) {
// check if any child has a matching name
e.setName(getUniqueChildName(e));
children.add(index,e);
Expand All @@ -95,7 +95,7 @@ public void addEntity(int index, Entity e) {
* @param child the child to add
*/
@Deprecated
public void addEntity(Entity child) {
public void addChild(Entity child) {
//System.out.println("add "+child.getFullPath()+" to "+this.getFullPath());
children.add(child);
child.setParent(this);
Expand All @@ -106,7 +106,7 @@ public void addEntity(Entity child) {
* @param child the child to remove
*/
@Deprecated
public void removeEntity(Entity child) {
public void removeChild(Entity child) {
if (children.contains(child)) {
children.remove(child);
if(child.getParent()==this) // is this always true? then why test it?
Expand All @@ -127,7 +127,7 @@ public Entity getParent() {
}

public void setParent(Entity e) {
if(parent != null) parent.removeEntity(this);
if(parent != null) parent.removeChild(this);
parent = e;
}

Expand Down Expand Up @@ -245,6 +245,21 @@ public void addComponent(Component c) {
c.onAttach();
}

private void addComponentDependencies(Class<?> myClass) {
while(myClass!=null) {
ComponentDependency[] annotations = myClass.getAnnotationsByType(ComponentDependency.class);
for (ComponentDependency a : annotations) {
Class<? extends Component> [] components = a.components();
for(Class<? extends Component> c : components) {
if(null==getComponent(c)) {
addComponent(ComponentFactory.createInstance(c));
}
}
}
myClass = myClass.getSuperclass();
}
}

public boolean containsAnInstanceOfTheSameClass(Component c0) {
Class<?> clazz = c0.getClass();
for(Component c : components) {
Expand Down Expand Up @@ -335,20 +350,20 @@ private JSONArray getComponentsAsJSON(SerializationContext context) {
public void parseJSON(JSONObject jo,SerializationContext context) throws JSONException {
this.name = jo.getString("name");
if(jo.has("uniqueID")) this.uniqueID = jo.getString("uniqueID");
if(jo.has("entities")) readEntities(jo.getJSONArray("entities"),context);
if(jo.has("components")) readComponents(jo.getJSONArray("components"),context);
if(jo.has("entities")) parseEntities(jo.getJSONArray("entities"),context);
if(jo.has("components")) parseComponents(jo.getJSONArray("components"),context);
}

private void readEntities(JSONArray jo,SerializationContext context) throws JSONException {
private void parseEntities(JSONArray jo, SerializationContext context) throws JSONException {
for (Object o : jo) {
JSONObject jo2 = (JSONObject) o;
Entity entity = new Entity();
this.addEntity(entity);
this.addChild(entity);
entity.parseJSON(jo2,context);
}
}

private void readComponents(JSONArray jo,SerializationContext context) throws JSONException {
private void parseComponents(JSONArray jo, SerializationContext context) throws JSONException {
for (Object o : jo) {
JSONObject jo2 = (JSONObject) o;
Component component = ComponentFactory.load(jo2.getString("type"));
Expand Down Expand Up @@ -416,27 +431,12 @@ public List<Entity> getEntireTree() {
return list;
}

private void addComponentDependencies(Class<?> myClass) {
while(myClass!=null) {
ComponentDependency[] annotations = myClass.getAnnotationsByType(ComponentDependency.class);
for (ComponentDependency a : annotations) {
Class<? extends Component> [] components = a.components();
for(Class<? extends Component> c : components) {
if(null==getComponent(c)) {
addComponent(ComponentFactory.createInstance(c));
}
}
}
myClass = myClass.getSuperclass();
}
}

/**
* Search the children of this entity for a child with the given name.
* @param name the name of the child to find
* @return the child or null if not found
*/
public Entity findChildNamed(String name) {
public Entity findChild(String name) {
for( Entity child : children) {
if(child.getName().equals(name)) {
return child;
Expand All @@ -454,7 +454,7 @@ public List<Component> getComponents() {
* @param subject the entity to test
* @return depth of this entity in the tree, or -1 if not found.
*/
public int isChildOf(Entity subject) {
public int findParent(Entity subject) {
int result=1;
Entity parent = this.parent;
while(parent!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public void addEntityToParent(Entity child,Entity parent) {
if(child.getParent()!=null) {
removeEntityFromParent(child,child.getParent());
}
parent.addEntity(child);
parent.addChild(child);
fireEntityManagerEvent(new EntityManagerEvent(EntityManagerEvent.ENTITY_ADDED, child, parent));
}

public void removeEntityFromParent(Entity child,Entity parent) {
if(!parent.children.contains(child)) return;

parent.removeEntity(child);
parent.removeChild(child);
fireEntityManagerEvent(new EntityManagerEvent(EntityManagerEvent.ENTITY_REMOVED, child, parent));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.marginallyclever.robotoverlord.entity.Entity;
import com.marginallyclever.robotoverlord.entity.EntityManager;
import com.marginallyclever.robotoverlord.parameters.swing.ViewElementButton;
import com.marginallyclever.robotoverlord.parameters.swing.ViewElementComboBox;
import com.marginallyclever.robotoverlord.parameters.swing.ComponentSwingViewFactory;
import com.marginallyclever.robotoverlord.swing.translator.Translator;
import com.marginallyclever.robotoverlord.systems.EntitySystem;
Expand Down Expand Up @@ -292,7 +291,7 @@ private void removeGripperAndJawsFromHits(GripperComponentAbstract gripper, List
for(RayHit hit : hits) {
Entity e = hit.target.getEntity();
// do not consider the gripper itself, the jaws, or the contact points.
if(e.isChildOf(gripperEntity)==-1 && e != gripperEntity) {
if(e.findParent(gripperEntity)==-1 && e != gripperEntity) {
keep.add(hit);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Entity findEntityUnderCursor() {
}

private void createPickPoint() {
Entity pickPoint = entityManager.getRoot().findChildNamed(PICK_POINT_NAME);
Entity pickPoint = entityManager.getRoot().findChild(PICK_POINT_NAME);
if(pickPoint == null) {
pickPoint = new Entity(PICK_POINT_NAME);
entityManager.addEntityToParent(pickPoint,entityManager.getRoot());
Expand All @@ -204,7 +204,7 @@ private void createPickPoint() {

private void setPickPoint(Ray ray, RayHit rayHit) {
createPickPoint();
Entity pickPoint = entityManager.getRoot().findChildNamed(PICK_POINT_NAME);
Entity pickPoint = entityManager.getRoot().findChild(PICK_POINT_NAME);

Vector3d from = ray.getPoint(rayHit.distance);
Vector3d to = new Vector3d(from);
Expand Down

0 comments on commit 8dd147d

Please sign in to comment.