This commit is contained in:
Tungstend 2023-03-02 21:53:21 +08:00
parent 6078459bb2
commit 34be0f88cb
4 changed files with 108 additions and 129 deletions

View File

@ -284,7 +284,7 @@ public class BaseInfoData implements Cloneable, Observable {
}
@JsonAdapter(PercentageSize.Serializer.class)
public static class PercentageSize implements Cloneable {
public static class PercentageSize implements Cloneable, Observable {
public enum Reference {
SCREEN_WIDTH,
@ -329,7 +329,7 @@ public class BaseInfoData implements Cloneable, Observable {
}
public PercentageSize() {
addPropertyChangedListener(onInvalidating(this::invalidate));
}
public void addPropertyChangedListener(InvalidationListener listener) {
@ -337,6 +337,22 @@ public class BaseInfoData implements Cloneable, Observable {
sizeProperty.addListener(listener);
}
private ObservableHelper observableHelper = new ObservableHelper(this);
@Override
public void addListener(InvalidationListener listener) {
observableHelper.addListener(listener);
}
@Override
public void removeListener(InvalidationListener listener) {
observableHelper.removeListener(listener);
}
private void invalidate() {
observableHelper.invalidate();
}
@Override
public PercentageSize clone() {
PercentageSize size = new PercentageSize();

View File

@ -22,6 +22,8 @@ import com.tungsten.fclcore.fakefx.beans.property.SimpleBooleanProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty;
import com.tungsten.fclcore.fakefx.beans.property.StringProperty;
import com.tungsten.fclcore.fakefx.collections.FXCollections;
import com.tungsten.fclcore.fakefx.collections.ObservableList;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
import java.lang.reflect.Type;
@ -312,74 +314,33 @@ public class ButtonEventData implements Cloneable, Observable {
/**
* Output keycodes
*/
private final ObjectProperty<ArrayList<Integer>> outputKeycodesProperty = new SimpleObjectProperty<>(this, "outputKeycodes", new ArrayList<>());
private final ObservableList<Integer> outputKeycodesList = FXCollections.observableList(new ArrayList<>());
public ObjectProperty<ArrayList<Integer>> outputKeycodesProperty() {
return outputKeycodesProperty;
public ObservableList<Integer> outputKeycodesList() {
return outputKeycodesList;
}
public void setOutputKeycodes(ArrayList<Integer> outputKeycodes) {
outputKeycodesProperty.set(outputKeycodes);
}
public ArrayList<Integer> getOutputKeycodes() {
return outputKeycodesProperty.get();
public void setOutputKeycodes(ObservableList<Integer> outputKeycodes) {
outputKeycodesList.setAll(outputKeycodes);
}
/**
* Switch view group visibility
*/
private final ObjectProperty<ArrayList<String>> bindViewGroupProperty = new SimpleObjectProperty<>(this, "bindViewGroup", new ArrayList<>());
private final ObservableList<String> bindViewGroupList = FXCollections.observableList(new ArrayList<>());
public ObjectProperty<ArrayList<String>> bindViewGroupProperty() {
return bindViewGroupProperty;
public ObservableList<String> bindViewGroupList() {
return bindViewGroupList;
}
public void setBindViewGroup(ArrayList<String> bindViewGroup) {
bindViewGroupProperty.set(bindViewGroup);
}
public ArrayList<String> getBindViewGroup() {
return bindViewGroupProperty.get();
public void setBindViewGroup(ObservableList<String> bindViewGroup) {
bindViewGroupList.setAll(bindViewGroup);
}
public Event() {
addPropertyChangedListener(onInvalidating(this::invalidate));
}
public void addKeycode(int keycode) {
ArrayList<Integer> keycodes = getOutputKeycodes();
if (!keycodes.contains(keycode)) {
keycodes.add(keycode);
setOutputKeycodes(keycodes);
}
}
public void removeKeycode(int keycode) {
ArrayList<Integer> keycodes = getOutputKeycodes();
for (int i = 0; i < keycodes.size(); i++) {
if (keycodes.get(i) == keycode) {
keycodes.remove(i);
break;
}
}
setOutputKeycodes(keycodes);
}
public void bindViewGroup(String groupId) {
ArrayList<String> groups = getBindViewGroup();
if (!groups.contains(groupId)) {
groups.add(groupId);
setBindViewGroup(groups);
}
}
public void unbindViewGroup(String groupId) {
ArrayList<String> groups = getBindViewGroup();
groups.remove(groupId);
setBindViewGroup(groups);
}
public void addPropertyChangedListener(InvalidationListener listener) {
pointerFollowProperty.addListener(listener);
autoKeepProperty.addListener(listener);
@ -389,8 +350,8 @@ public class ButtonEventData implements Cloneable, Observable {
switchTouchModeProperty.addListener(listener);
inputProperty.addListener(listener);
outputTextProperty.addListener(listener);
outputKeycodesProperty.addListener(listener);
bindViewGroupProperty.addListener(listener);
outputKeycodesList.addListener(listener);
bindViewGroupList.addListener(listener);
}
private ObservableHelper observableHelper = new ObservableHelper(this);
@ -420,8 +381,8 @@ public class ButtonEventData implements Cloneable, Observable {
event.setSwitchTouchMode(isSwitchTouchMode());
event.setInput(isInput());
event.setOutputText(getOutputText());
event.setOutputKeycodes(getOutputKeycodes());
event.setBindViewGroup(getBindViewGroup());
event.setOutputKeycodes(outputKeycodesList());
event.setBindViewGroup(bindViewGroupList());
return event;
}
@ -441,8 +402,8 @@ public class ButtonEventData implements Cloneable, Observable {
obj.addProperty("switchTouchMode", src.isSwitchTouchMode());
obj.addProperty("input", src.isInput());
obj.addProperty("outputText", src.getOutputText());
obj.addProperty("outputKeycodes", gson.toJson(src.getOutputKeycodes()));
obj.addProperty("bindViewGroup", gson.toJson(src.getBindViewGroup()));
obj.addProperty("outputKeycodes", gson.toJson(src.outputKeycodesList()));
obj.addProperty("bindViewGroup", gson.toJson(src.bindViewGroupList()));
return obj;
}
@ -464,8 +425,8 @@ public class ButtonEventData implements Cloneable, Observable {
event.setSwitchTouchMode(Optional.ofNullable(obj.get("switchTouchMode")).map(JsonElement::getAsBoolean).orElse(false));
event.setInput(Optional.ofNullable(obj.get("input")).map(JsonElement::getAsBoolean).orElse(false));
event.setOutputText(Optional.ofNullable(obj.get("outputText")).map(JsonElement::getAsString).orElse(""));
event.setOutputKeycodes(gson.fromJson(Optional.ofNullable(obj.get("outputKeycodes")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken<Integer>(){}.getType()));
event.setBindViewGroup(gson.fromJson(Optional.ofNullable(obj.get("bindViewGroup")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken<String>(){}.getType()));
event.setOutputKeycodes(gson.fromJson(Optional.ofNullable(obj.get("outputKeycodes")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken<Integer>(){}.getType()));
event.setBindViewGroup(gson.fromJson(Optional.ofNullable(obj.get("bindViewGroup")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken<String>(){}.getType()));
return event;
}

View File

@ -20,6 +20,8 @@ import com.tungsten.fclcore.fakefx.beans.property.ObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty;
import com.tungsten.fclcore.fakefx.beans.property.StringProperty;
import com.tungsten.fclcore.fakefx.collections.FXCollections;
import com.tungsten.fclcore.fakefx.collections.ObservableList;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
import java.lang.reflect.Type;
@ -169,108 +171,108 @@ public class ControlViewGroup implements Cloneable, Observable {
}
@JsonAdapter(ViewData.Serializer.class)
public static class ViewData implements Cloneable {
public static class ViewData implements Cloneable, Observable {
/**
* Button data list
*/
private final ObjectProperty<ArrayList<ControlButtonData>> buttonListProperty = new SimpleObjectProperty<>(this, "buttonList", new ArrayList<>());
private final ObservableList<ControlButtonData> buttonList = FXCollections.observableArrayList(new ArrayList<>());
public ObjectProperty<ArrayList<ControlButtonData>> buttonListProperty() {
return buttonListProperty;
public ObservableList<ControlButtonData> buttonList() {
return buttonList;
}
public void setButtonList(ArrayList<ControlButtonData> buttonList) {
buttonListProperty.set(buttonList);
}
public ArrayList<ControlButtonData> getButtonList() {
return buttonListProperty.get();
public void setButtonList(ObservableList<ControlButtonData> list) {
buttonList.setAll(list);
}
/**
* Direction data list
*/
private final ObjectProperty<ArrayList<ControlDirectionData>> directionListProperty = new SimpleObjectProperty<>(this, "directionList", new ArrayList<>());
private final ObservableList<ControlDirectionData> directionList = FXCollections.observableArrayList(new ArrayList<>());
public ObjectProperty<ArrayList<ControlDirectionData>> directionListProperty() {
return directionListProperty;
public ObservableList<ControlDirectionData> directionList() {
return directionList;
}
public void setDirectionList(ArrayList<ControlDirectionData> directionList) {
directionListProperty.set(directionList);
}
public ArrayList<ControlDirectionData> getDirectionList() {
return directionListProperty.get();
public void setDirectionList(ObservableList<ControlDirectionData> list) {
directionList.setAll(list);
}
public void addButton(ControlButtonData data) {
ArrayList<ControlButtonData> list = getButtonList();
boolean exist = false;
for (ControlButtonData buttonData : list) {
for (ControlButtonData buttonData : buttonList()) {
if (buttonData.equals(data)) {
exist = true;
break;
}
}
if (!exist) {
list.add(data);
setButtonList(list);
buttonList.add(data);
}
}
public void removeButton(ControlButtonData data) {
ArrayList<ControlButtonData> list = getButtonList();
for (ControlButtonData buttonData : list) {
for (ControlButtonData buttonData : buttonList()) {
if (buttonData.equals(data)) {
list.remove(buttonData);
setButtonList(list);
buttonList.remove(buttonData);
break;
}
}
}
public void addDirection(ControlDirectionData data) {
ArrayList<ControlDirectionData> list = getDirectionList();
boolean exist = false;
for (ControlDirectionData directionData : list) {
for (ControlDirectionData directionData : directionList()) {
if (directionData.equals(data)) {
exist = true;
break;
}
}
if (!exist) {
list.add(data);
setDirectionList(list);
directionList.add(data);
}
}
public void removeDirection(ControlDirectionData data) {
ArrayList<ControlDirectionData> list = getDirectionList();
for (ControlDirectionData directionData : list) {
for (ControlDirectionData directionData : directionList()) {
if (directionData.equals(data)) {
list.remove(directionData);
setDirectionList(list);
directionList.remove(directionData);
break;
}
}
}
public ViewData() {
addPropertyChangedListener(onInvalidating(this::invalidate));
}
public void addPropertyChangedListener(InvalidationListener listener) {
buttonListProperty.addListener(listener);
directionListProperty.addListener(listener);
buttonList.addListener(listener);
directionList.addListener(listener);
}
private ObservableHelper observableHelper = new ObservableHelper(this);
@Override
public void addListener(InvalidationListener listener) {
observableHelper.addListener(listener);
}
@Override
public void removeListener(InvalidationListener listener) {
observableHelper.removeListener(listener);
}
private void invalidate() {
observableHelper.invalidate();
}
@Override
public ViewData clone() {
ViewData data = new ViewData();
data.setButtonList(getButtonList());
data.setDirectionList(getDirectionList());
data.setButtonList(buttonList());
data.setDirectionList(directionList());
return data;
}
@ -281,8 +283,8 @@ public class ControlViewGroup implements Cloneable, Observable {
JsonObject obj = new JsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
obj.addProperty("buttonList", gson.toJson(src.getButtonList()));
obj.addProperty("directionList", gson.toJson(src.getDirectionList()));
obj.addProperty("buttonList", gson.toJson(src.buttonList()));
obj.addProperty("directionList", gson.toJson(src.directionList()));
return obj;
}
@ -296,8 +298,8 @@ public class ControlViewGroup implements Cloneable, Observable {
ViewData data = new ViewData();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
data.setButtonList(gson.fromJson(Optional.ofNullable(obj.get("buttonList")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken<ControlButtonData>(){}.getType()));
data.setDirectionList(gson.fromJson(Optional.ofNullable(obj.get("directionList")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken<ControlDirectionData>(){}.getType()));
data.setButtonList(gson.fromJson(Optional.ofNullable(obj.get("buttonList")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken<ControlButtonData>(){}.getType()));
data.setDirectionList(gson.fromJson(Optional.ofNullable(obj.get("directionList")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken<ControlDirectionData>(){}.getType()));
return data;
}

View File

@ -22,12 +22,12 @@ import com.tungsten.fclauncher.FCLPath;
import com.tungsten.fclcore.fakefx.beans.InvalidationListener;
import com.tungsten.fclcore.fakefx.beans.Observable;
import com.tungsten.fclcore.fakefx.beans.property.IntegerProperty;
import com.tungsten.fclcore.fakefx.beans.property.ObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.ReadOnlyIntegerProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleIntegerProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty;
import com.tungsten.fclcore.fakefx.beans.property.StringProperty;
import com.tungsten.fclcore.fakefx.collections.FXCollections;
import com.tungsten.fclcore.fakefx.collections.ObservableList;
import com.tungsten.fclcore.util.ToStringBuilder;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
import com.tungsten.fclcore.util.gson.fakefx.factories.JavaFxPropertyTypeAdapterFactory;
@ -38,9 +38,10 @@ import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;
@JsonAdapter(Controller.Serializer.class)
public class Controller implements Observable {
public class Controller implements Cloneable, Observable {
private final SimpleStringProperty name;
@ -108,18 +109,14 @@ public class Controller implements Observable {
return controllerVersion.get();
}
private final SimpleObjectProperty<ArrayList<ControlViewGroup>> viewGroups;
private final ObservableList<ControlViewGroup> viewGroups;
public ObjectProperty<ArrayList<ControlViewGroup>> viewGroupsProperty() {
public ObservableList<ControlViewGroup> viewGroups() {
return viewGroups;
}
public ArrayList<ControlViewGroup> getViewGroups() {
return viewGroups.get();
}
public void setViewGroups(ArrayList<ControlViewGroup> viewGroups) {
this.viewGroups.set(viewGroups);
public void setViewGroups(ObservableList<ControlViewGroup> viewGroups) {
this.viewGroups.addAll(viewGroups);
}
public Controller(String name) {
@ -139,15 +136,15 @@ public class Controller implements Observable {
}
public Controller(String name, String version, String author, String description, int controllerVersion) {
this(name, version, author, description, controllerVersion, new ArrayList<>());
this(name, version, author, description, controllerVersion, FXCollections.observableArrayList(new ArrayList<>()));
}
public Controller(String name, String version, String author, String description, int controllerVersion, ArrayList<ControlViewGroup> viewGroups) {
public Controller(String name, String version, String author, String description, int controllerVersion, ObservableList<ControlViewGroup> viewGroups) {
this.name = new SimpleStringProperty(this, "name", name);
this.version = new SimpleStringProperty(this, "version", version);
this.author = new SimpleStringProperty(this, "author", author);
this.description = new SimpleStringProperty(this, "description", description);
this.viewGroups = new SimpleObjectProperty<>(this, "viewGroups", viewGroups);
this.viewGroups = viewGroups;
this.controllerVersion.set(controllerVersion);
@ -155,26 +152,22 @@ public class Controller implements Observable {
}
public void addViewGroup(ControlViewGroup viewGroup) {
ArrayList<ControlViewGroup> list = getViewGroups();
boolean exist = false;
for (ControlViewGroup group : list) {
for (ControlViewGroup group : viewGroups()) {
if (viewGroup.getId().equals(group.getId())) {
exist = true;
break;
}
}
if (!exist) {
list.add(viewGroup);
setViewGroups(list);
viewGroups.add(viewGroup);
}
}
public void removeViewGroup(ControlViewGroup viewGroup) {
ArrayList<ControlViewGroup> list = getViewGroups();
for (ControlViewGroup group : list) {
for (ControlViewGroup group : viewGroups()) {
if (viewGroup.getId().equals(group.getId())) {
list.remove(group);
setViewGroups(list);
viewGroups.remove(group);
break;
}
}
@ -217,6 +210,13 @@ public class Controller implements Observable {
observableHelper.invalidate();
}
@Override
public Controller clone() {
ObservableList<ControlViewGroup> viewGroups = FXCollections.observableArrayList(new ArrayList<>());
viewGroups.addAll(viewGroups().stream().map(ControlViewGroup::clone).collect(Collectors.toList()));
return new Controller(getName() + "_clone", getVersion(), getAuthor(), getDescription(), getControllerVersion(), viewGroups);
}
// function
public String getFileName() {
@ -249,7 +249,7 @@ public class Controller implements Observable {
jsonObject.addProperty("author", src.getAuthor());
jsonObject.addProperty("description", src.getDescription());
jsonObject.addProperty("controllerVersion", src.getControllerVersion());
jsonObject.addProperty("viewGroups", new GsonBuilder().setPrettyPrinting().create().toJson(src.getViewGroups()));
jsonObject.addProperty("viewGroups", new GsonBuilder().setPrettyPrinting().create().toJson(src.viewGroups()));
return jsonObject;
}
@ -265,7 +265,7 @@ public class Controller implements Observable {
String author = Optional.ofNullable(obj.get("author")).map(JsonElement::getAsString).orElse("");
String description = Optional.ofNullable(obj.get("description")).map(JsonElement::getAsString).orElse("");
int controllerVersion = Optional.ofNullable(obj.get("controllerVersion")).map(JsonElement::getAsInt).orElse(Constants.CONTROLLER_VERSION);
ArrayList<ControlViewGroup> viewGroups = gson.fromJson(Optional.ofNullable(obj.get("controllerVersion")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken<ControlViewGroup>(){}.getType());
ObservableList<ControlViewGroup> viewGroups = gson.fromJson(Optional.ofNullable(obj.get("viewGroups")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken<ControlViewGroup>(){}.getType());
return new Controller(name, version, author, description, controllerVersion, viewGroups);
}