diff --git a/src/intents/sui/SuiWindowIntent.java b/src/intents/sui/SuiWindowIntent.java index 53504a282..357d64587 100644 --- a/src/intents/sui/SuiWindowIntent.java +++ b/src/intents/sui/SuiWindowIntent.java @@ -29,23 +29,23 @@ package intents.sui; import resources.control.Intent; import resources.player.Player; -import resources.sui.SuiWindow; +import resources.sui.SuiBaseWindow; public class SuiWindowIntent extends Intent { public static final String TYPE = "SuiWindowIntent"; - private SuiWindow window; + private SuiBaseWindow window; private Player player; private SuiWindowEvent event; - public SuiWindowIntent(Player player, SuiWindow window, SuiWindowEvent event) { + public SuiWindowIntent(Player player, SuiBaseWindow window, SuiWindowEvent event) { super(TYPE); this.player = player; this.window = window; this.event = event; } - public SuiWindow getWindow() { return this.window; } + public SuiBaseWindow getWindow() { return this.window; } public Player getPlayer() { return this.player; } public SuiWindowEvent getEvent() { return event; } diff --git a/src/network/packets/swg/zone/server_ui/SuiCreatePageMessage.java b/src/network/packets/swg/zone/server_ui/SuiCreatePageMessage.java index dd348bbc1..cfb9c0615 100644 --- a/src/network/packets/swg/zone/server_ui/SuiCreatePageMessage.java +++ b/src/network/packets/swg/zone/server_ui/SuiCreatePageMessage.java @@ -28,20 +28,18 @@ package network.packets.swg.zone.server_ui; import network.packets.swg.SWGPacket; -import resources.sui.SuiWindow; +import resources.sui.SuiBaseWindow; import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; public class SuiCreatePageMessage extends SWGPacket { public static final int CRC = getCrc("SuiCreatePageMessage"); - private SuiWindow window; + private SuiBaseWindow window; public SuiCreatePageMessage() {} - public SuiCreatePageMessage(SuiWindow window) { + public SuiCreatePageMessage(SuiBaseWindow window) { this.window = window; } @@ -52,7 +50,7 @@ public class SuiCreatePageMessage extends SWGPacket { public void decode(ByteBuffer data) { if (!super.decode(data, CRC)) return; - window = getEncodable(data, SuiWindow.class); + window = getEncodable(data, SuiBaseWindow.class); } public ByteBuffer encode() { @@ -64,7 +62,7 @@ public class SuiCreatePageMessage extends SWGPacket { return data; } - public SuiWindow getWindow() { + public SuiBaseWindow getWindow() { return window; } } diff --git a/src/resources/commands/callbacks/ServerCmdCallback.java b/src/resources/commands/callbacks/ServerCmdCallback.java index ca2743602..176fa55e1 100644 --- a/src/resources/commands/callbacks/ServerCmdCallback.java +++ b/src/resources/commands/callbacks/ServerCmdCallback.java @@ -32,6 +32,7 @@ import intents.server.ServerManagementIntent.ServerManagementEvent; import resources.commands.ICmdCallback; import resources.objects.SWGObject; import resources.player.Player; +import resources.server_info.Log; import resources.sui.ISuiCallback; import resources.sui.SuiButtons; import resources.sui.SuiEvent; @@ -80,7 +81,7 @@ public class ServerCmdCallback implements ICmdCallback { case 2: handleUnbanPlayer(player); break; case 3: handleShutdownServer(player); break; case 4: handleCustomShutdownServer(player); break; - default: System.out.println("Nothing for selection " + selection); break; + default: Log.i("ServerCmdCallback", "There is no handle function for selected list item %d", selection); break; } } @@ -137,7 +138,7 @@ public class ServerCmdCallback implements ICmdCallback { if (index < 0 || index >= unitValues.length) return; timeUnitReference.set(unitValues[index]); - timeWindow.display(); // Ziggy: Display the next window + timeWindow.display(); // Ziggy: Display the next window }); timeWindow.addOkButtonCallback("handleCustomShutdownCountdown", (player, actor1, event, parameters) -> { diff --git a/src/resources/sui/SuiBaseWindow.java b/src/resources/sui/SuiBaseWindow.java index 440896c8b..04e83a2b2 100644 --- a/src/resources/sui/SuiBaseWindow.java +++ b/src/resources/sui/SuiBaseWindow.java @@ -27,121 +27,332 @@ ***********************************************************************************/ package resources.sui; +import intents.sui.SuiWindowIntent; +import intents.sui.SuiWindowIntent.SuiWindowEvent; +import network.packets.Packet; +import resources.encodables.Encodable; import resources.player.Player; +import resources.server_info.Log; -public class SuiBaseWindow extends SuiWindow { +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; - public SuiBaseWindow(String script, Player owner, SuiButtons buttons, String title, String prompt) { - super(script, owner); - setTitle(title); - setPrompt(prompt); - setButtons(buttons); +public class SuiBaseWindow implements Encodable { + + private int id; + private String suiScript; + private Player owner; + private long rangeObjId; + private float maxDistance = 0; + private List components = new ArrayList<>(); + private Map callbacks; + private Map scriptCallbacks; + private boolean hasSubscriptionComponent = false; + + public SuiBaseWindow() { } - public SuiBaseWindow(String script, Player owner, SuiButtons buttons, String title, String prompt, String callbackScript, String function) { - this(script, owner, buttons, title, prompt); - - addOkButtonCallback(callbackScript, function); - addCancelButtonCallback(callbackScript, function); + public SuiBaseWindow(String suiScript, Player owner) { + this.suiScript = suiScript; + this.owner = owner; } - public SuiBaseWindow(String script, Player owner, SuiButtons buttons, String title, String prompt, String callback, ISuiCallback suiCallback) { - this(script, owner, buttons, title, prompt); - - addOkButtonCallback(callback, suiCallback); - addCancelButtonCallback(callback, suiCallback); + public final void clearDataSource(String dataSource) { + SuiComponent component = new SuiComponent(SuiComponent.Type.CLEAR_DATA_SOURCE, dataSource); + components.add(component); } - public SuiBaseWindow(String script, Player owner, String title, String prompt) { - this(script, owner, SuiButtons.DEFAULT, title, prompt); + public final void addChildWidget(String type, String childWidget, String parentWidget) { + SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_CHILD_WIDGET, parentWidget); + + component.addNarrowParam(type); + component.addNarrowParam(childWidget); + + components.add(component); } - public void setTitle(String title) { - if (title != null) - setPropertyText("bg.caption.lblTitle", title); - } - - public void setPrompt(String prompt) { - if (prompt != null) - setPropertyText("Prompt.lblPrompt", prompt); + public final void setProperty(String widget, String property, String value) { + SuiComponent component = new SuiComponent(SuiComponent.Type.SET_PROPERTY, widget); + + component.addNarrowParam(property); + component.addWideParam(value); + + components.add(component); } - public void setPropertyText(String widget, String value) { - setProperty(widget, "Text", value); + public final void addDataItem(String dataSource, String name, String value) { + SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_ITEM, dataSource); + + component.addNarrowParam(name); + component.addWideParam(value); + + components.add(component); } - public void setAutosave(boolean autosave) { - setProperty("this", "autosave", String.valueOf(autosave)); - } + private void subscribeToEvent(int event, String widgetSource, String callback) { + SuiComponent component = getSubscriptionForEvent(event, widgetSource); + if (component != null) { + Log.i("SuiWindow", "Added event callback %d to %s when the event is already subscribed to, replacing callback to %s", event, widgetSource, callback); + component.getNarrowParams().set(2, callback); + } else { + component = new SuiComponent(SuiComponent.Type.SUBSCRIBE_TO_EVENT, widgetSource); + component.addNarrowParam(getWrappedEventString(event)); + component.addNarrowParam(callback); - public void setLocation(int x, int y) { - setProperty("this", "Location", String.valueOf(x) + "," + String.valueOf(y)); - } - - public void setSize(int width, int height) { - setProperty("this", "Size", String.valueOf(width) + "," + String.valueOf(height)); - } - - protected void setOkButtonText() { - setOkButtonText("@ok"); - } - - protected void setOkButtonText(String text) { - setProperty("btnOk", "Text", text); - } - - protected void setCancelButtonText() { - setCancelButtonText("@cancel"); - } - - protected void setCancelButtonText(String text) { - setProperty("btnCancel", "Text", text); - } - - protected void setShowOtherButton(boolean display, String text) { - setProperty("btnOther", "Visible", String.valueOf(display)); - if (display) { - setProperty("btnOther", "Text", text); - addOtherButtonReturnable(); + components.add(component); } + if (!hasSubscriptionComponent()) + hasSubscriptionComponent = true; } - protected void setShowCancelButton(boolean display) { - String value = String.valueOf(display); - setProperty("btnCancel", "Enabled", value); - setProperty("btnCancel", "Visible", value); - } - - public void addOkButtonCallback(String callback, ISuiCallback suiCallback) { - addCallback(SuiEvent.OK_PRESSED, callback, suiCallback); - } - - public void addOkButtonCallback(String script, String function) { - addCallback(SuiEvent.OK_PRESSED, script, function); - } - - public void addCancelButtonCallback(String callback, ISuiCallback suiCallback) { - addCallback(SuiEvent.CANCEL_PRESSED, callback, suiCallback); - } - - public void addCancelButtonCallback(String script, String function) { - addCallback(SuiEvent.CANCEL_PRESSED, script, function); - } - - public void addOtherButtonReturnable() { - addReturnableProperty(SuiEvent.OK_PRESSED, "this", "otherPressed"); - } - - protected void setButtons(SuiButtons buttons) { - switch(buttons) { - case OK_CANCEL: - setOkButtonText(); - setCancelButtonText(); - break; - case OK: - default: - setOkButtonText(); - break; + private void subscribeToPropertyEvent(int event, String widgetSource, String propertyWidget, String propertyName) { + SuiComponent component = getSubscriptionForEvent(event, widgetSource); + if (component != null) { + // This component already has the trigger and source param, just need to add the widget and property + // for client to return the value to the server + component.addNarrowParam(propertyWidget); + component.addNarrowParam(propertyName); + } else { + component = new SuiComponent(SuiComponent.Type.SUBSCRIBE_TO_EVENT, widgetSource); + component.addNarrowParam(getWrappedEventString(event)); + component.addNarrowParam(""); + component.addNarrowParam(propertyWidget); + component.addNarrowParam(propertyName); + components.add(component); } + if (!hasSubscriptionComponent()) + hasSubscriptionComponent = true; + } + + private void addDataSourceContainer(String dataSourceContainer, String name, String value) { + SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_SOURCE_CONTAINER, dataSourceContainer); + + component.addNarrowParam(name); + component.addWideParam(value); + + components.add(component); + } + + public final void clearDataSourceContainer(String dataSourceContainer) { + SuiComponent component = new SuiComponent(SuiComponent.Type.CLEAR_DATA_SOURCE_CONTAINER, dataSourceContainer); + components.add(component); + } + + public final void addDataSource(String dataSource, String name, String value) { + SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_SOURCE, dataSource); + + component.addNarrowParam(name); + component.addWideParam(value); + + components.add(component); + } + + public final void addReturnableProperty(SuiEvent event, String source, String widget, String property) { + subscribeToPropertyEvent(event.getValue(), source, widget, property); + } + + public final void addReturnableProperty(SuiEvent event, String widget, String property) { + addReturnableProperty(event, "", widget, property); + } + + public final void addReturnableProperty(String widget, String property) { + subscribeToPropertyEvent(SuiEvent.OK_PRESSED.getValue(), "", widget, property); + subscribeToPropertyEvent(SuiEvent.CANCEL_PRESSED.getValue(), "", widget, property); + } + + public final void addCallback(SuiEvent event, String source, String name, ISuiCallback callback) { + subscribeToEvent(event.getValue(), source, name); + addJavaCallback(name, callback); + } + + public final void addCallback(SuiEvent event, String name, ISuiCallback callback) { + addCallback(event, "", name, callback); + } + + public final void addCallback(SuiEvent event, String source, String script, String function) { + subscribeToEvent(event.getValue(), source, function); + addScriptCallback(function, script); + } + + public final void addCallback(SuiEvent event, String script, String function) { + addCallback(event, "", script, function); + } + + public final void addCallback(String source, String script, String function) { + subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), source, function); + subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), source, function); + addScriptCallback(function, script); + } + + public final void addCallback(String script, String function) { + addCallback("", script, function); + } + + public final void addCallback(String source, String name, ISuiCallback callback) { + subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), source, name); + subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), source, name); + addJavaCallback(name, callback); + } + + public final void addCallback(String name, ISuiCallback callback) { + addCallback("", name, callback); + } + + public final SuiComponent getSubscriptionForEvent(int event, String widget) { + for (SuiComponent component : components) { + if (component.getType() != SuiComponent.Type.SUBSCRIBE_TO_EVENT) + continue; + + List narrowParams = component.getNarrowParams(); + int eventType = component.getSubscribedToEventType(); + + if (eventType == event && component.getTarget().equals(widget)) + return component; + } + return null; + } + + public final SuiComponent getSubscriptionByIndex(int index) { + int count = 0; + for (SuiComponent component : components) { + if (component.getType() == SuiComponent.Type.SUBSCRIBE_TO_EVENT) { + if (index == count) return component; + else count++; + } + } + return null; + } + + public final void display() { + onDisplayRequest(); + new SuiWindowIntent(owner, this, SuiWindowEvent.NEW).broadcast(); + } + + public final void display(Player player) { + onDisplayRequest(); + new SuiWindowIntent(player, this, SuiWindowEvent.NEW).broadcast(); + } + + public final long getRangeObjId() { + return rangeObjId; + } + + public final void setRangeObjId(long rangeObjId) { + this.rangeObjId = rangeObjId; + } + + public final int getId() { + return id; + } + + public final void setId(int id) { + this.id = id; + } + + public final String getSuiScript() { + return suiScript; + } + + public final Player getOwner() { + return owner; + } + + public final float getMaxDistance() { + return maxDistance; + } + + public final void setMaxDistance(float maxDistance) { + this.maxDistance = maxDistance; + } + + public final List getComponents() { + return components; + } + + public final ISuiCallback getJavaCallback(String name) { + return callbacks != null ? callbacks.get(name) : null; + } + + public final String getCallbackScript(String function) { + return scriptCallbacks != null ? scriptCallbacks.get(function) : null; + } + + public final boolean hasCallbackFunction(String function) { + return scriptCallbacks != null && scriptCallbacks.containsKey(function); + } + + public final boolean hasJavaCallback(String name) { + return callbacks != null && callbacks.containsKey(name); + } + + public final boolean hasSubscriptionComponent() { + return hasSubscriptionComponent; + } + + // Add a simple ok/cancel button subscriptions if no callbacks is assigned so the server is sent a SuiEventNotification when client destroys the page. + // Doing it this way will ensures the server removes the stored SuiWindow from memory. + private void prepare() { + if (hasSubscriptionComponent()) + return; + + subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), "", "handleSUI"); + subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), "", "handleSUI"); + } + + protected void onDisplayRequest() { + prepare(); + } + + private void addJavaCallback(String name, ISuiCallback callback) { + if (callbacks == null) callbacks = new HashMap<>(); + + callbacks.put(name, callback); + } + + private void addScriptCallback(String function, String script) { + if (scriptCallbacks == null) scriptCallbacks = new HashMap<>(); + + scriptCallbacks.put(function, script); + } + + private String getWrappedEventString(int event) { + return new String(new byte[]{(byte) event}); + } + + @Override + public byte[] encode() { + + int listSize = 0; + List componentData = new ArrayList<>(); + for (SuiComponent component : components) { + byte[] data = component.encode(); + componentData.add(data); + listSize += data.length; + } + + ByteBuffer data = ByteBuffer.allocate(34 + suiScript.length() + listSize); + Packet.addInt(data, id); + Packet.addAscii(data, suiScript); + Packet.addList(data, componentData); + Packet.addLong(data, rangeObjId); + Packet.addFloat(data, maxDistance); + Packet.addLong(data, 0); // Window Location? + Packet.addInt(data, 0); + + return data.array(); + } + + @Override + public void decode(ByteBuffer data) { + id = Packet.getInt(data); + suiScript = Packet.getAscii(data); + components = Packet.getList(data, SuiComponent.class); + rangeObjId = Packet.getLong(data); + maxDistance = Packet.getFloat(data); + // unk long + // unk int } } diff --git a/src/resources/sui/SuiComponent.java b/src/resources/sui/SuiComponent.java index fbc592bb3..55d8427e3 100644 --- a/src/resources/sui/SuiComponent.java +++ b/src/resources/sui/SuiComponent.java @@ -91,7 +91,7 @@ public class SuiComponent implements Encodable { int size = narrowParams.size(); if (size < 3) { - Log.i("SuiComponent", "Tried to get subscribed properties when there are none for target %s", getTarget()); + Log.w("SuiComponent", "Tried to get subscribed properties when there are none for target %s", getTarget()); } else { List subscribedProperties = new ArrayList<>(); @@ -111,7 +111,7 @@ public class SuiComponent implements Encodable { int size = narrowParams.size(); if (size < 3) { - Log.i("SuiComponent", "Tried to get subscribed callback when there is none for target %s", getTarget()); + Log.w("SuiComponent", "Tried to get subscribed callback when there is none for target %s", getTarget()); } else { return narrowParams.get(2); @@ -125,11 +125,11 @@ public class SuiComponent implements Encodable { int size = narrowParams.size(); if (size < 3) { - Log.i("SuiComponent", "Tried to get subscribed event type when there is none for target %s", getTarget()); + Log.w("SuiComponent", "Tried to get subscribed event type when there is none for target %s", getTarget()); } else { byte[] bytes = narrowParams.get(1).getBytes(); if (bytes.length > 1) { - Log.i("SuiComponent", "Tried to get event tyep but narrowparams string byte array length is more than 1"); + Log.w("SuiComponent", "Tried to get eventType but narrowparams string byte array length is more than 1"); return -1; } diff --git a/src/resources/sui/SuiInputBox.java b/src/resources/sui/SuiInputBox.java index fb2d6e608..e4717ce96 100644 --- a/src/resources/sui/SuiInputBox.java +++ b/src/resources/sui/SuiInputBox.java @@ -31,7 +31,7 @@ import resources.player.Player; import java.util.Map; -public class SuiInputBox extends SuiBaseWindow { +public class SuiInputBox extends SuiWindow { public SuiInputBox(Player owner, SuiButtons buttons, String title, String prompt) { super("Script.inputBox", owner, buttons, title, prompt); diff --git a/src/resources/sui/SuiListBox.java b/src/resources/sui/SuiListBox.java index 58ab2ef44..7b2825efc 100644 --- a/src/resources/sui/SuiListBox.java +++ b/src/resources/sui/SuiListBox.java @@ -33,7 +33,7 @@ import java.util.Map; import resources.player.Player; -public class SuiListBox extends SuiBaseWindow { +public class SuiListBox extends SuiWindow { private List list; public SuiListBox(Player owner, SuiButtons buttons, String title, String prompt) { diff --git a/src/resources/sui/SuiMessageBox.java b/src/resources/sui/SuiMessageBox.java index 62b1f2c5d..3083d44c5 100644 --- a/src/resources/sui/SuiMessageBox.java +++ b/src/resources/sui/SuiMessageBox.java @@ -29,7 +29,7 @@ package resources.sui; import resources.player.Player; -public class SuiMessageBox extends SuiBaseWindow { +public class SuiMessageBox extends SuiWindow { public SuiMessageBox(Player owner, SuiButtons buttons, String title, String prompt) { super("Script.messageBox", owner, buttons, title, prompt); diff --git a/src/resources/sui/SuiTableWindow.java b/src/resources/sui/SuiTableWindow.java index 5b7fa2480..70aafd66d 100644 --- a/src/resources/sui/SuiTableWindow.java +++ b/src/resources/sui/SuiTableWindow.java @@ -32,7 +32,7 @@ import java.util.List; import resources.player.Player; -public class SuiTableWindow extends SuiBaseWindow { +public class SuiTableWindow extends SuiWindow { private List table; diff --git a/src/resources/sui/SuiWindow.java b/src/resources/sui/SuiWindow.java index 3b30f8837..909869e4d 100644 --- a/src/resources/sui/SuiWindow.java +++ b/src/resources/sui/SuiWindow.java @@ -27,332 +27,121 @@ ***********************************************************************************/ package resources.sui; -import intents.sui.SuiWindowIntent; -import intents.sui.SuiWindowIntent.SuiWindowEvent; -import network.packets.Packet; -import resources.encodables.Encodable; import resources.player.Player; -import resources.server_info.Log; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +public class SuiWindow extends SuiBaseWindow { -public class SuiWindow implements Encodable { - - private int id; - private String suiScript; - private Player owner; - private long rangeObjId; - private float maxDistance = 0; - private List components = new ArrayList<>(); - private Map callbacks; - private Map scriptCallbacks; - private boolean hasSubscriptionComponent = false; - - public SuiWindow() { + public SuiWindow(String script, Player owner, SuiButtons buttons, String title, String prompt) { + super(script, owner); + setTitle(title); + setPrompt(prompt); + setButtons(buttons); } - public SuiWindow(String suiScript, Player owner) { - this.suiScript = suiScript; - this.owner = owner; + public SuiWindow(String script, Player owner, SuiButtons buttons, String title, String prompt, String callbackScript, String function) { + this(script, owner, buttons, title, prompt); + + addOkButtonCallback(callbackScript, function); + addCancelButtonCallback(callbackScript, function); } - public final void clearDataSource(String dataSource) { - SuiComponent component = new SuiComponent(SuiComponent.Type.CLEAR_DATA_SOURCE, dataSource); - components.add(component); + public SuiWindow(String script, Player owner, SuiButtons buttons, String title, String prompt, String callback, ISuiCallback suiCallback) { + this(script, owner, buttons, title, prompt); + + addOkButtonCallback(callback, suiCallback); + addCancelButtonCallback(callback, suiCallback); } - public final void addChildWidget(String type, String childWidget, String parentWidget) { - SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_CHILD_WIDGET, parentWidget); - - component.addNarrowParam(type); - component.addNarrowParam(childWidget); - - components.add(component); + public SuiWindow(String script, Player owner, String title, String prompt) { + this(script, owner, SuiButtons.DEFAULT, title, prompt); } - public final void setProperty(String widget, String property, String value) { - SuiComponent component = new SuiComponent(SuiComponent.Type.SET_PROPERTY, widget); - - component.addNarrowParam(property); - component.addWideParam(value); - - components.add(component); + public void setTitle(String title) { + if (title != null) + setPropertyText("bg.caption.lblTitle", title); + } + + public void setPrompt(String prompt) { + if (prompt != null) + setPropertyText("Prompt.lblPrompt", prompt); } - public final void addDataItem(String dataSource, String name, String value) { - SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_ITEM, dataSource); - - component.addNarrowParam(name); - component.addWideParam(value); - - components.add(component); + public void setPropertyText(String widget, String value) { + setProperty(widget, "Text", value); } - private void subscribeToEvent(int event, String widgetSource, String callback) { - SuiComponent component = getSubscriptionForEvent(event, widgetSource); - if (component != null) { - Log.i("SuiWindow", "Added event callback %d to %s when the event is already subscribed to, replacing callback to %s", event, widgetSource, callback); - component.getNarrowParams().set(2, callback); - } else { - component = new SuiComponent(SuiComponent.Type.SUBSCRIBE_TO_EVENT, widgetSource); - component.addNarrowParam(getWrappedEventString(event)); - component.addNarrowParam(callback); + public void setAutosave(boolean autosave) { + setProperty("this", "autosave", String.valueOf(autosave)); + } - components.add(component); + public void setLocation(int x, int y) { + setProperty("this", "Location", String.valueOf(x) + "," + String.valueOf(y)); + } + + public void setSize(int width, int height) { + setProperty("this", "Size", String.valueOf(width) + "," + String.valueOf(height)); + } + + protected void setOkButtonText() { + setOkButtonText("@ok"); + } + + protected void setOkButtonText(String text) { + setProperty("btnOk", "Text", text); + } + + protected void setCancelButtonText() { + setCancelButtonText("@cancel"); + } + + protected void setCancelButtonText(String text) { + setProperty("btnCancel", "Text", text); + } + + protected void setShowOtherButton(boolean display, String text) { + setProperty("btnOther", "Visible", String.valueOf(display)); + if (display) { + setProperty("btnOther", "Text", text); + addOtherButtonReturnable(); } - if (!hasSubscriptionComponent()) - hasSubscriptionComponent = true; } - private void subscribeToPropertyEvent(int event, String widgetSource, String propertyWidget, String propertyName) { - SuiComponent component = getSubscriptionForEvent(event, widgetSource); - if (component != null) { - // This component already has the trigger and source param, just need to add the widget and property - // for client to return the value to the server - component.addNarrowParam(propertyWidget); - component.addNarrowParam(propertyName); - } else { - component = new SuiComponent(SuiComponent.Type.SUBSCRIBE_TO_EVENT, widgetSource); - component.addNarrowParam(getWrappedEventString(event)); - component.addNarrowParam(""); - component.addNarrowParam(propertyWidget); - component.addNarrowParam(propertyName); - components.add(component); + protected void setShowCancelButton(boolean display) { + String value = String.valueOf(display); + setProperty("btnCancel", "Enabled", value); + setProperty("btnCancel", "Visible", value); + } + + public void addOkButtonCallback(String callback, ISuiCallback suiCallback) { + addCallback(SuiEvent.OK_PRESSED, callback, suiCallback); + } + + public void addOkButtonCallback(String script, String function) { + addCallback(SuiEvent.OK_PRESSED, script, function); + } + + public void addCancelButtonCallback(String callback, ISuiCallback suiCallback) { + addCallback(SuiEvent.CANCEL_PRESSED, callback, suiCallback); + } + + public void addCancelButtonCallback(String script, String function) { + addCallback(SuiEvent.CANCEL_PRESSED, script, function); + } + + public void addOtherButtonReturnable() { + addReturnableProperty(SuiEvent.OK_PRESSED, "this", "otherPressed"); + } + + protected void setButtons(SuiButtons buttons) { + switch(buttons) { + case OK_CANCEL: + setOkButtonText(); + setCancelButtonText(); + break; + case OK: + default: + setOkButtonText(); + break; } - if (!hasSubscriptionComponent()) - hasSubscriptionComponent = true; - } - - private void addDataSourceContainer(String dataSourceContainer, String name, String value) { - SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_SOURCE_CONTAINER, dataSourceContainer); - - component.addNarrowParam(name); - component.addWideParam(value); - - components.add(component); - } - - public final void clearDataSourceContainer(String dataSourceContainer) { - SuiComponent component = new SuiComponent(SuiComponent.Type.CLEAR_DATA_SOURCE_CONTAINER, dataSourceContainer); - components.add(component); - } - - public final void addDataSource(String dataSource, String name, String value) { - SuiComponent component = new SuiComponent(SuiComponent.Type.ADD_DATA_SOURCE, dataSource); - - component.addNarrowParam(name); - component.addWideParam(value); - - components.add(component); - } - - public final void addReturnableProperty(SuiEvent event, String source, String widget, String property) { - subscribeToPropertyEvent(event.getValue(), source, widget, property); - } - - public final void addReturnableProperty(SuiEvent event, String widget, String property) { - addReturnableProperty(event, "", widget, property); - } - - public final void addReturnableProperty(String widget, String property) { - subscribeToPropertyEvent(SuiEvent.OK_PRESSED.getValue(), "", widget, property); - subscribeToPropertyEvent(SuiEvent.CANCEL_PRESSED.getValue(), "", widget, property); - } - - public final void addCallback(SuiEvent event, String source, String name, ISuiCallback callback) { - subscribeToEvent(event.getValue(), source, name); - addJavaCallback(name, callback); - } - - public final void addCallback(SuiEvent event, String name, ISuiCallback callback) { - addCallback(event, "", name, callback); - } - - public final void addCallback(SuiEvent event, String source, String script, String function) { - subscribeToEvent(event.getValue(), source, function); - addScriptCallback(function, script); - } - - public final void addCallback(SuiEvent event, String script, String function) { - addCallback(event, "", script, function); - } - - public final void addCallback(String source, String script, String function) { - subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), source, function); - subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), source, function); - addScriptCallback(function, script); - } - - public final void addCallback(String script, String function) { - addCallback("", script, function); - } - - public final void addCallback(String source, String name, ISuiCallback callback) { - subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), source, name); - subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), source, name); - addJavaCallback(name, callback); - } - - public final void addCallback(String name, ISuiCallback callback) { - addCallback("", name, callback); - } - - public final SuiComponent getSubscriptionForEvent(int event, String widget) { - for (SuiComponent component : components) { - if (component.getType() != SuiComponent.Type.SUBSCRIBE_TO_EVENT) - continue; - - List narrowParams = component.getNarrowParams(); - int eventType = component.getSubscribedToEventType(); - - if (eventType == event && component.getTarget().equals(widget)) - return component; - } - return null; - } - - public final SuiComponent getSubscriptionByIndex(int index) { - int count = 0; - for (SuiComponent component : components) { - if (component.getType() == SuiComponent.Type.SUBSCRIBE_TO_EVENT) { - if (index == count) return component; - else count++; - } - } - return null; - } - - public final void display() { - onDisplayRequest(); - new SuiWindowIntent(owner, this, SuiWindowEvent.NEW).broadcast(); - } - - public final void display(Player player) { - onDisplayRequest(); - new SuiWindowIntent(player, this, SuiWindowEvent.NEW).broadcast(); - } - - public final long getRangeObjId() { - return rangeObjId; - } - - public final void setRangeObjId(long rangeObjId) { - this.rangeObjId = rangeObjId; - } - - public final int getId() { - return id; - } - - public final void setId(int id) { - this.id = id; - } - - public final String getSuiScript() { - return suiScript; - } - - public final Player getOwner() { - return owner; - } - - public final float getMaxDistance() { - return maxDistance; - } - - public final void setMaxDistance(float maxDistance) { - this.maxDistance = maxDistance; - } - - public final List getComponents() { - return components; - } - - public final ISuiCallback getJavaCallback(String name) { - return callbacks != null ? callbacks.get(name) : null; - } - - public final String getCallbackScript(String function) { - return scriptCallbacks != null ? scriptCallbacks.get(function) : null; - } - - public final boolean hasCallbackFunction(String function) { - return scriptCallbacks != null && scriptCallbacks.containsKey(function); - } - - public final boolean hasJavaCallback(String name) { - return callbacks != null && callbacks.containsKey(name); - } - - public final boolean hasSubscriptionComponent() { - return hasSubscriptionComponent; - } - - // Add a simple ok/cancel button subscriptions if no callbacks is assigned so the server is sent a SuiEventNotification when client destroys the page. - // Doing it this way will ensures the server removes the stored SuiWindow from memory. - private void prepare() { - if (hasSubscriptionComponent()) - return; - - subscribeToEvent(SuiEvent.OK_PRESSED.getValue(), "", "handleSUI"); - subscribeToEvent(SuiEvent.CANCEL_PRESSED.getValue(), "", "handleSUI"); - } - - protected void onDisplayRequest() { - prepare(); - } - - private void addJavaCallback(String name, ISuiCallback callback) { - if (callbacks == null) callbacks = new HashMap<>(); - - callbacks.put(name, callback); - } - - private void addScriptCallback(String function, String script) { - if (scriptCallbacks == null) scriptCallbacks = new HashMap<>(); - - scriptCallbacks.put(function, script); - } - - private String getWrappedEventString(int event) { - return new String(new byte[]{(byte) event}); - } - - @Override - public byte[] encode() { - - int listSize = 0; - List componentData = new ArrayList<>(); - for (SuiComponent component : components) { - byte[] data = component.encode(); - componentData.add(data); - listSize += data.length; - } - - ByteBuffer data = ByteBuffer.allocate(34 + suiScript.length() + listSize); - Packet.addInt(data, id); - Packet.addAscii(data, suiScript); - Packet.addList(data, componentData); - Packet.addLong(data, rangeObjId); - Packet.addFloat(data, maxDistance); - Packet.addLong(data, 0); // Window Location? - Packet.addInt(data, 0); - - return data.array(); - } - - @Override - public void decode(ByteBuffer data) { - id = Packet.getInt(data); - suiScript = Packet.getAscii(data); - components = Packet.getList(data, SuiComponent.class); - rangeObjId = Packet.getLong(data); - maxDistance = Packet.getFloat(data); - // unk long - // unk int } } diff --git a/src/services/sui/SuiService.java b/src/services/sui/SuiService.java index f71e08ae9..5b923986e 100644 --- a/src/services/sui/SuiService.java +++ b/src/services/sui/SuiService.java @@ -41,7 +41,7 @@ import resources.server_info.Log; import resources.sui.ISuiCallback; import resources.sui.SuiComponent; import resources.sui.SuiEvent; -import resources.sui.SuiWindow; +import resources.sui.SuiBaseWindow; import utilities.Scripts; import java.util.ArrayList; @@ -52,7 +52,7 @@ import java.util.concurrent.ConcurrentHashMap; public class SuiService extends Service { - private final Map> windows; + private final Map> windows; public SuiService() { windows = new ConcurrentHashMap<>(); @@ -105,23 +105,23 @@ public class SuiService extends Service { } private void handleSuiEventNotification(Player player, SuiEventNotification p) { - List activeWindows = windows.get(player.getNetworkId()); + List activeWindows = windows.get(player.getNetworkId()); if (activeWindows == null || activeWindows.size() <= 0) { - Log.i("SuiService:SuiEventNotification", "There are no active windows for %s!", player); + Log.w("SuiService:SuiEventNotification", "There are no active windows for %s!", player); return; } - SuiWindow window = getWindowById(activeWindows, p.getWindowId()); + SuiBaseWindow window = getWindowById(activeWindows, p.getWindowId()); if (window == null) { - Log.i("SuiService:SuiEventNotification", "Received window ID %i that is not assigned to the player %s", p.getWindowId(), player); + Log.w("SuiService:SuiEventNotification", "Received window ID %i that is not assigned to the player %s", p.getWindowId(), player); return; } SuiComponent component = window.getSubscriptionByIndex(p.getEventIndex()); if (component == null) { - Log.i("SuiService:SuiEventNotification", "SuiWindow %s retrieved null subscription from supplied event index %i", window, p.getEventIndex()); + Log.w("SuiService:SuiEventNotification", "SuiWindow %s retrieved null subscription from supplied event index %i", window, p.getEventIndex()); return; } @@ -144,7 +144,6 @@ public class SuiService extends Service { parameters.put(suiSubscribedProperties.get(i), eventNotificationProperties.get(i)); } - parameters.forEach((k, v) -> System.out.println("Key: " + k + " | Value: " + v)); if (window.hasCallbackFunction(callback)) { String script = window.getCallbackScript(callback); Scripts.invoke(script, callback, player, player.getCreatureObject(), event, parameters); @@ -158,7 +157,7 @@ public class SuiService extends Service { activeWindows.remove(window); } - private void displayWindow(Player player, SuiWindow window) { + private void displayWindow(Player player, SuiBaseWindow window) { int id = createWindowId(); window.setId(id); @@ -166,7 +165,7 @@ public class SuiService extends Service { player.sendPacket(packet); long networkId = player.getNetworkId(); - List activeWindows = windows.get(networkId); + List activeWindows = windows.get(networkId); if (activeWindows == null) { activeWindows = new ArrayList<>(); windows.put(networkId, activeWindows); @@ -174,8 +173,8 @@ public class SuiService extends Service { activeWindows.add(window); } - private SuiWindow getWindowById(List windows, int id) { - for (SuiWindow window : windows) { + private SuiBaseWindow getWindowById(List windows, int id) { + for (SuiBaseWindow window : windows) { if (window.getId() == id) return window; } diff --git a/src/utilities/IntentFactory.java b/src/utilities/IntentFactory.java index 4a809a9e6..c98a705ee 100644 --- a/src/utilities/IntentFactory.java +++ b/src/utilities/IntentFactory.java @@ -13,7 +13,7 @@ import resources.player.Player; import intents.chat.ChatBroadcastIntent; import intents.chat.ChatBroadcastIntent.BroadcastType; import intents.chat.PersistentMessageIntent; -import resources.sui.SuiWindow; +import resources.sui.SuiBaseWindow; /** * @author Mads @@ -122,11 +122,11 @@ public final class IntentFactory { } /** - * Displays the provided {@link SuiWindow} to the player. - * @param player Player to be shown the {@link SuiWindow}. - * @param suiWindow {@link SuiWindow} to be shown + * Displays the provided {@link SuiBaseWindow} to the player. + * @param player Player to be shown the {@link SuiBaseWindow}. + * @param suiWindow {@link SuiBaseWindow} to be shown */ - public void showSuiWindow(Player player, SuiWindow suiWindow) { + public void showSuiWindow(Player player, SuiBaseWindow suiWindow) { new SuiWindowIntent(player, suiWindow, SuiWindowIntent.SuiWindowEvent.NEW).broadcast(); } } diff --git a/src/utilities/Scripts.java b/src/utilities/Scripts.java index 76c9cd7a3..b783cc768 100644 --- a/src/utilities/Scripts.java +++ b/src/utilities/Scripts.java @@ -48,7 +48,7 @@ public class Scripts { ENGINE.eval("var RadialOption = Java.type('resources.radial.RadialOption')"); ENGINE.eval("var RadialItem = Java.type('resources.radial.RadialItem')"); ENGINE.eval("var Log = Java.type('resources.server_info.Log')"); - ENGINE.eval("var SuiWindow = Java.type('resources.sui.SuiBaseWindow')"); + ENGINE.eval("var SuiWindow = Java.type('resources.sui.SuiWindow')"); ENGINE.eval("var SuiButtons = Java.type('resources.sui.SuiButtons')"); ENGINE.eval("var SuiEvent = Java.type('resources.sui.SuiEvent')"); } catch (ScriptException e) {