GroupNode.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package holeg.model;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.Set;
  6. import java.util.logging.Logger;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.Stream;
  9. import holeg.preferences.ImagePreference;
  10. import holeg.serialize.PostDeserialize;
  11. import holeg.ui.model.GuiSettings;
  12. import holeg.utility.math.vector.Vec2i;
  13. public class GroupNode extends AbstractCanvasObject implements PostDeserialize {
  14. private static final Logger log = Logger.getLogger(AbstractCanvasObject.class.getName());
  15. private final List<HolonObject> objectList = new ArrayList<>();
  16. private final List<HolonSwitch> switchList = new ArrayList<>();
  17. private final List<Node> nodeList = new ArrayList<>();
  18. private final List<GroupNode> groupNodeList = new ArrayList<>();
  19. public GroupNode(String nodeName) {
  20. super(nodeName);
  21. }
  22. public GroupNode(GroupNode other){
  23. super(other.name);
  24. for (HolonObject obj : other.objectList) {
  25. this.add(new HolonObject(obj));
  26. }
  27. for (HolonSwitch sw : other.switchList) {
  28. this.add(new HolonSwitch(sw));
  29. }
  30. for (Node node : other.nodeList) {
  31. this.add(new Node(node));
  32. }
  33. for (GroupNode groupNode : other.groupNodeList) {
  34. this.add(new GroupNode(groupNode));
  35. }
  36. }
  37. @Override
  38. public AbstractCanvasObject copy() {
  39. return new GroupNode(this);
  40. }
  41. @Override
  42. public String getImagePath() {
  43. return ImagePreference.Canvas.GroupNode;
  44. }
  45. public void addAll(Stream<AbstractCanvasObject> stream) {
  46. stream.forEach(this::add);
  47. }
  48. public void addAll(Collection<AbstractCanvasObject> collection) {
  49. collection.forEach(this::add);
  50. }
  51. public void add(AbstractCanvasObject object) {
  52. if (object instanceof HolonObject hObject) {
  53. objectList.add(hObject);
  54. }else if(object instanceof HolonSwitch hSwitch) {
  55. switchList.add(hSwitch);
  56. }else if(object instanceof Node node) {
  57. nodeList.add(node);
  58. }else if(object instanceof GroupNode groupNode) {
  59. groupNodeList.add(groupNode);
  60. }
  61. object.setGroupNode(this);
  62. }
  63. public void remove(AbstractCanvasObject object) {
  64. if (object instanceof HolonObject hObject) {
  65. objectList.remove(hObject);
  66. }else if(object instanceof HolonSwitch hSwitch) {
  67. switchList.remove(hSwitch);
  68. }else if(object instanceof Node node) {
  69. nodeList.remove(node);
  70. }else if(object instanceof GroupNode groupNode) {
  71. groupNodeList.remove(groupNode);
  72. }
  73. object.setGroupNode(null);
  74. }
  75. public void removeAll(Stream<AbstractCanvasObject> stream){
  76. stream.forEach(this::remove);
  77. }
  78. public void removeAll(Collection<AbstractCanvasObject> collection){
  79. collection.forEach(this::remove);
  80. }
  81. public Stream<AbstractCanvasObject> getObjectsInThisLayer() {
  82. return Stream.of(objectList.stream(), switchList.stream(), nodeList.stream(), groupNodeList.stream())
  83. .flatMap(s -> s);
  84. }
  85. public Stream<AbstractCanvasObject> getAllObjectsRecursive() {
  86. Stream<AbstractCanvasObject> objects = Stream.of(objectList.stream(), switchList.stream(), nodeList.stream())
  87. .flatMap(s -> s);
  88. return Stream.concat(objects,
  89. groupNodeList.stream().flatMap(GroupNode::getObjectsInThisLayer));
  90. }
  91. public Stream<HolonObject> getAllHolonObjectsRecursive() {
  92. return Stream.concat(objectList.stream(),
  93. groupNodeList.stream().flatMap(GroupNode::getAllHolonObjectsRecursive));
  94. }
  95. public Stream<HolonSwitch> getAllSwitchObjectsRecursive() {
  96. return Stream.concat(switchList.stream(),
  97. groupNodeList.stream().flatMap(GroupNode::getAllSwitchObjectsRecursive));
  98. }
  99. public Stream<Node> getAllNodeObjectsRecursive() {
  100. return Stream.concat(nodeList.stream(),
  101. groupNodeList.stream().flatMap(GroupNode::getAllNodeObjectsRecursive));
  102. }
  103. public Stream<GroupNode> getAllGroupNodeObjectsRecursive() {
  104. return Stream.concat(groupNodeList.stream(),
  105. groupNodeList.stream().flatMap(GroupNode::getAllGroupNodeObjectsRecursive));
  106. }
  107. public Stream<HolonObject> getHolonObjects() {
  108. return objectList.stream();
  109. }
  110. public Stream<HolonSwitch> getSwitches() {
  111. return switchList.stream();
  112. }
  113. public Stream<Node> getNodes() {
  114. return nodeList.stream();
  115. }
  116. public Stream<GroupNode> getGroupNodes() {
  117. return groupNodeList.stream();
  118. }
  119. public Stream<HolonElement> getHolonElements(){
  120. return objectList.stream().flatMap(HolonObject::elementsStream);
  121. }
  122. public void ungroup(){
  123. getObjectsInThisLayer().forEach(obj -> {
  124. obj.setGroupNode(null);
  125. });
  126. getGroupNode().ifPresent(parent -> {
  127. parent.addAll(getObjectsInThisLayer());
  128. parent.remove(this);
  129. });
  130. clear();
  131. }
  132. public static Vec2i calculateMiddlePosition(Collection<AbstractCanvasObject> objects){
  133. Vec2i middle = new Vec2i();
  134. if(!objects.isEmpty()){
  135. objects.forEach(obj -> middle.addAssign(obj.getPosition()));
  136. middle.divideAssign(objects.size());
  137. }
  138. return middle;
  139. }
  140. public void clear() {
  141. objectList.clear();
  142. switchList.clear();
  143. nodeList.clear();
  144. groupNodeList.clear();
  145. }
  146. @Override
  147. public void postDeserialize() {
  148. getObjectsInThisLayer().forEach(obj -> obj.setGroupNode(this));
  149. }
  150. @Override
  151. public String toString(){
  152. return "" + objectList + switchList + nodeList +groupNodeList ;
  153. }
  154. }