diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java b/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java index cfc4fdd270537ebde4fe40b6e6a3500f813ff231..8125b05854eaf41a28e2fbe7cb1760730bbafa64 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java @@ -94,10 +94,10 @@ public class Pad implements IPad { padListener = new PadUpdateListener(this); } - public Pad(Project project, int index, Page page) { + public Pad(Project project, int position, Page page) { this(project); - setPosition(index); + setPosition(position); setPage(page); setStatus(PadStatus.EMPTY); @@ -187,6 +187,7 @@ public class Pad implements IPad { * * @return uuid */ + @Override public UUID getUuid() { return uuid; } @@ -223,6 +224,7 @@ public class Pad implements IPad { * * @return position */ + @Override public int getPositionReadable() { return positionProperty.get() + 1; } @@ -232,6 +234,7 @@ public class Pad implements IPad { * * @return position */ + @Override public int getPosition() { return positionProperty.get(); } @@ -276,6 +279,7 @@ public class Pad implements IPad { * * @return name */ + @Override public String getName() { return nameProperty.get(); } @@ -398,6 +402,7 @@ public class Pad implements IPad { * * @return status */ + @Override public PadStatus getStatus() { return statusProperty.get(); } diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/Project.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/Project.java index 9d9d3962f8b991c7c6ba09830843c75883f873be..d68220796b6cbed568d9512ce40ae57f2aabe12e 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/project/Project.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/Project.java @@ -74,6 +74,7 @@ public class Project implements IProject { .forEach(Pad::stop); } + @Override public ProjectSettings getSettings() { return settings; } @@ -82,15 +83,18 @@ public class Project implements IProject { return projectReference; } + @Override public Pad getPad(int x, int y, int page) { return getPage(page).getPad(x, y); } + @Override public Pad getPad(PadIndex index) { Page page = pages.get(index.getPagePosition()); return page.getPad(index.getId()); } + @Override public Pad getPad(UUID uuid) { for (Page page : pages) { for (Pad pad : page.getPads()) { @@ -118,6 +122,7 @@ public class Project implements IProject { page.setPad(index.getId(), pad); } + @Override public Collection<Pad> getPads() { return getPads(p -> true); } @@ -136,6 +141,7 @@ public class Project implements IProject { } // Pages + @Override public Page getPage(int position) { if (position >= ProjectSettings.MAX_PAGES) { return null; @@ -147,7 +153,7 @@ public class Project implements IProject { return pages.get(position); } - + @Override public Page getPage(UUID uuid) { for (Page page : pages) { if (page.getId().equals(uuid)) { @@ -157,6 +163,7 @@ public class Project implements IProject { return null; } + @Override public ObservableList<Page> getPages() { // Create new page if all is empty (automatic) if (pages.isEmpty()) { diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPad.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPad.java index cca8d7203b80679feca92d1df8421924091a86d9..4aae5c73b676722ee7e530f1cca089e37edb7bf4 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPad.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPad.java @@ -11,5 +11,7 @@ public interface IPad { PadStatus getStatus(); + int getPosition(); + int getPositionReadable(); } diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPage.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPage.java index 23ba1b4a8c7eff9b1f3935c281869786a5e5e5ef..d780c8457fa745c72d267e65693a49018d9135b8 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPage.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IPage.java @@ -1,5 +1,22 @@ package de.tobias.playpad.project.api; +import java.util.Collection; +import java.util.UUID; + public interface IPage { + UUID getId(); + + int getPosition(); + + String getName(); + + IPad getPad(int position); + IPad getPad(int x, int y); + + Collection<? extends IPad> getPads(); + + default int getPadPosition(int x, int y, IProjectSettings settings) { + return y * settings.getColumns() + x; + } } diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IProject.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IProject.java index 1b19a1baabfe815cad8cb5eb7525d3ba2249981b..8e8bd5de57a916ead9d16d28fbc210f2db700915 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IProject.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/api/IProject.java @@ -1,11 +1,26 @@ package de.tobias.playpad.project.api; +import de.tobias.playpad.pad.Pad; +import de.tobias.playpad.project.page.PadIndex; + +import java.util.Collection; import java.util.List; +import java.util.UUID; public interface IProject { - List<? extends IPage> getPages(); + IProjectSettings getSettings(); + + Pad getPad(int x, int y, int page); + + Pad getPad(PadIndex index); + + Pad getPad(UUID uuid); + + Collection<? extends IPad> getPads(); IPage getPage(int index); + + IPage getPage(UUID uuid); - IProjectSettings getSettings(); + List<? extends IPage> getPages(); } diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/page/Page.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/page/Page.java index 1419a7e88f1d68a1f018cf2a1dd7408d7ea1a637..b791b8b094a824f034995a90b5df2d1505676bf5 100644 --- a/PlayWallCore/src/main/java/de/tobias/playpad/project/page/Page.java +++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/page/Page.java @@ -85,6 +85,7 @@ public class Page implements IPage { * * @return id */ + @Override public UUID getId() { return id; } @@ -94,6 +95,7 @@ public class Page implements IPage { * * @return page position. */ + @Override public int getPosition() { return positionProperty.get(); } @@ -124,6 +126,7 @@ public class Page implements IPage { * * @return name */ + @Override public String getName() { return nameProperty.get(); } @@ -158,28 +161,28 @@ public class Page implements IPage { /** * Get the pad at this index. It will create a new pad at this index if this index is empty. In case of a wrong index the method will throw an exception. * - * @param id index + * @param position index * @return pad * @throws IllegalArgumentException bad index */ - public Pad getPad(int id) throws IllegalArgumentException { + public Pad getPad(int position) throws IllegalArgumentException { ProjectSettings settings = projectReference.getSettings(); int maxId = settings.getRows() * settings.getColumns(); - if (id < 0 || id > maxId) { - throw new IllegalArgumentException("Illegal index: index is " + id + " but it must in a range of 0 to " + maxId); + if (position < 0 || position > maxId) { + throw new IllegalArgumentException("Illegal index: index is " + position + " but it must in a range of 0 to " + maxId); } - if (pads.stream().noneMatch(p -> p.getPosition() == id)) { + if (pads.stream().noneMatch(p -> p.getPosition() == position)) { // Create new pad for positionProperty - Pad pad = new Pad(projectReference, id, this); - setPad(id, pad); + Pad pad = new Pad(projectReference, position, this); + setPad(position, pad); if (projectReference.getProjectReference().isSync()) { CommandManager.execute(Commands.PAD_ADD, projectReference.getProjectReference(), pad); CommandManager.execute(Commands.PAD_SETTINGS_ADD, projectReference.getProjectReference(), pad.getPadSettings()); } } - Optional<Pad> padOptional = pads.stream().filter(p -> p.getPosition() == id).findFirst(); + Optional<Pad> padOptional = pads.stream().filter(p -> p.getPosition() == position).findFirst(); return padOptional.orElse(null); } @@ -190,20 +193,16 @@ public class Page implements IPage { * @param y y position * @return pad */ + @Override public Pad getPad(int x, int y) { ProjectSettings settings = projectReference.getSettings(); if (x < settings.getColumns() && y < settings.getRows()) { - int position = getPadPosition(x, y); + int position = getPadPosition(x, y, settings); return getPad(position); } return null; } - private int getPadPosition(int x, int y) { - ProjectSettings settings = projectReference.getSettings(); - return y * settings.getColumns() + x; - } - /** * Set a pad to a new id. It overwrites the old pad. If the pad argument is null, it only removes the old pad. * @@ -228,6 +227,7 @@ public class Page implements IPage { * * @return pads */ + @Override public Collection<Pad> getPads() { return Collections.unmodifiableCollection(pads); } @@ -276,7 +276,7 @@ public class Page implements IPage { * @param lastPadIndex index of the last pad on the page */ private void insertPadAndShiftSuccessor(int x, int y, int lastPadIndex) { - int position = getPadPosition(x, y); + int position = getPadPosition(x, y, getProject().getSettings()); // Going backwards to avoid overwriting the next pad when updating the position of the current one. for (int i = lastPadIndex - 1; i >= position; i--) { getPad(i).setPosition(i + 1); diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/serialize/ProjectSerializer.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/serialize/ProjectSerializer.scala index 3c1e157137bf1ed7d405a784bdf87510581443a8..8b34e20e5eea7b581f9645fed98be1fa38b9f064 100644 --- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/serialize/ProjectSerializer.scala +++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/serialize/ProjectSerializer.scala @@ -12,8 +12,6 @@ object ProjectSerializer { result.addProperty("id", project.getProjectReference.getUuid.toString) result.addProperty("name", project.getProjectReference.getName) - result.addProperty("columns", project.getSettings.getColumns) - result.addProperty("rows", project.getSettings.getRows) val pageArray = new JsonArray() project.getPages.forEach(page => { @@ -51,6 +49,11 @@ object ProjectSerializer { globalDesign.add("play", serializeDesign(profile.getProfileSettings.getDesign.getPlayColor)) result.add("design", globalDesign) + val settings = new JsonObject + settings.addProperty("columns", project.getSettings.getColumns) + settings.addProperty("rows", project.getSettings.getRows) + result.add("settings", settings) + result }