diff --git a/.github/images/anchor.png b/.github/images/anchor.png new file mode 100644 index 0000000..f0e0822 Binary files /dev/null and b/.github/images/anchor.png differ diff --git a/.github/images/example.png b/.github/images/example.png new file mode 100644 index 0000000..8057a21 Binary files /dev/null and b/.github/images/example.png differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..475b96d --- /dev/null +++ b/.gitignore @@ -0,0 +1,131 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/**/misc.xml +.idea/**/discord.xml +.idea/**/encodings.xml +.idea/codeStyles/ +.idea/sonarlint/ + +# Generated files +.idea/**/contentModel.xml +.idea/**/jarRepositories.xml +.idea/**/uiDesigner.xml +.idea/**/inspectionProfiles +.idea/**/.name +.idea/**/vcs.xml +.idea/**/compiler.xml +run/** + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/dbnavigator.xml +.idea/kotlinScripting.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +.idea/modules.xml +.idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Gradle template +.gradle +/**/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties + +.idea/modules/ +src/test/ +**/src/main/generated/* + +.idea/intellij-javadocs-4.0.1.xml +/.idea/runConfigurations/runClient.xml diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cdfc3d --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +## Widget model to create custom User Interfaces via plugin messages + +Click [here](https://docs.labymod.net/pages/server/minecraft/screen/) for more detailed information. + +### Available widgets: +- Button +- Text Field +- Label +- Color Picker +- Image + +### Example +![example](.github/images/example.png) + +### Serialize widgets +```java +// Create new screen +JsonObject screen = new JsonObject(); +screen.addProperty("id", 42); // Screen id. The client will send this id back on an interaction +screen.addProperty("action", EnumScreenAction.OPEN.ordinal()); // Open the GUI + +// Centered anchor +Anchor anchor = new Anchor(50, 50); + +// Create widget list +List widgets = new ArrayList<>(); + +// Add button to widget list +widgets.add(new ButtonWidget(0, anchor, -50, 20, "Apply", 100, 20)); + +// Serialize widgets +screen.add("widgets", WidgetSerialization.toJsonArray(widgets)); +``` + +### Anchor explanation image +![example](.github/images/anchor.png) \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..7bf0d84 --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +plugins { + id 'java' +} + +group 'net.labymod.serverapi.common' +version '1.0.0' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'com.google.code.gson:gson:2.8.6' +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..be52383 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..4f906e0 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..6b584e7 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'widgets' + diff --git a/src/main/java/net/labymod/serverapi/common/widgets/EnumWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/EnumWidget.java new file mode 100644 index 0000000..8eff4b9 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/EnumWidget.java @@ -0,0 +1,59 @@ +package net.labymod.serverapi.common.widgets; + +import net.labymod.serverapi.common.widgets.components.Widget; +import net.labymod.serverapi.common.widgets.components.widgets.ButtonWidget; +import net.labymod.serverapi.common.widgets.components.widgets.ColorPickerWidget; +import net.labymod.serverapi.common.widgets.components.widgets.ImageWidget; +import net.labymod.serverapi.common.widgets.components.widgets.LabelWidget; +import net.labymod.serverapi.common.widgets.components.widgets.TextFieldWidget; + +/** + * Enum with all widget components + * + * @author LabyStudio + */ +public enum EnumWidget { + BUTTON(ButtonWidget.class), + TEXT_FIELD(TextFieldWidget.class), + LABEL(LabelWidget.class), + COLOR_PICKER(ColorPickerWidget.class), + IMAGE(ImageWidget.class); + + /** + * The class of the widget + */ + private final Class clazz; + + /** + * Create a widget type + * + * @param clazz The class of the widget + */ + EnumWidget(Class clazz) { + this.clazz = clazz; + } + + /** + * Get widget class + * + * @return Widget class + */ + public Class getClazz() { + return this.clazz; + } + + /** + * Get widget type by widget class + * + * @param clazz The class of the widget + * @return The type of the given class. (Can be null) + */ + public static EnumWidget getTypeOf(Class clazz) { + for (EnumWidget type : values()) { + if (type.clazz == clazz) { + return type; + } + } + return null; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/WidgetSerialization.java b/src/main/java/net/labymod/serverapi/common/widgets/WidgetSerialization.java new file mode 100644 index 0000000..ff5067a --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/WidgetSerialization.java @@ -0,0 +1,67 @@ +package net.labymod.serverapi.common.widgets; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.labymod.serverapi.common.widgets.components.Widget; + +import java.util.List; + +/** + * Widget serialization utils. Used to convert the widget model to json and back. + * + * @author LabyStudio + */ +public class WidgetSerialization { + + private static final Gson GSON = new Gson(); + + /** + * Create a widget from a json object + * + * @param object The json serialized widget + * @return Widget instance + */ + public static Widget createWidget(JsonObject object) { + int id = object.get("id").getAsInt(); + JsonObject attributes = object.get("attributes").getAsJsonObject(); + return GSON.fromJson(attributes, EnumWidget.values()[id].getClazz()); + } + + /** + * Serialize a widget model list to a json array + * + * @param widgets Widget model list + * @return Serialized widget model list as json array + */ + public static JsonArray toJsonArray(List widgets) { + JsonArray widgetArray = new JsonArray(); + for (Widget widget : widgets) { + widgetArray.add(toJsonObject(widget)); + } + return widgetArray; + } + + /** + * @param widget + * @return + */ + public static JsonObject toJsonObject(Widget widget) { + EnumWidget type = EnumWidget.getTypeOf(widget.getClass()); + if (type == null) { + return null; + } + + // Get id + int id = type.ordinal(); + + // Convert to json object + JsonObject attributes = GSON.toJsonTree(widget).getAsJsonObject(); + + // Create object + JsonObject object = new JsonObject(); + object.addProperty("id", id); + object.add("attributes", attributes); + return object; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/ContainerWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/ContainerWidget.java new file mode 100644 index 0000000..3d24b4a --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/ContainerWidget.java @@ -0,0 +1,51 @@ +package net.labymod.serverapi.common.widgets.components; + +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Abstract container widget. Used to handle with and height values of a widget. + * + * @author LabyStudio + */ +public abstract class ContainerWidget extends Widget { + + /** + * Container width in pixels + */ + protected int width; + + /** + * Container height in pixels + */ + protected int height; + + /** + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param width The width of the container in pixels + * @param height The height of the container in pixels + */ + public ContainerWidget(int id, Anchor anchor, double offsetX, double offsetY, int width, int height) { + super(id, anchor, offsetX, offsetY); + this.width = width; + this.height = height; + } + + public void setWidth(int width) { + this.width = width; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/ValueContainerWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/ValueContainerWidget.java new file mode 100644 index 0000000..95a9b29 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/ValueContainerWidget.java @@ -0,0 +1,41 @@ +package net.labymod.serverapi.common.widgets.components; + +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Abstract container with a string value to display + * + * @author LabyStudio + */ +public abstract class ValueContainerWidget extends ContainerWidget { + + /** + * The display value of the container + */ + protected String value; + + /** + * Abstract container with a string value + * + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param value The display value of the container + * @param width The width of the container in pixels + * @param height The height of the container in pixels + */ + public ValueContainerWidget(int id, Anchor anchor, double offsetX, double offsetY, String value, int width, int height) { + super(id, anchor, offsetX, offsetY, width, height); + + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/Widget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/Widget.java new file mode 100644 index 0000000..8cb4f08 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/Widget.java @@ -0,0 +1,78 @@ +package net.labymod.serverapi.common.widgets.components; + +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Abstract widget component + * + * @author LabyStudio + */ +public abstract class Widget { + + /** + * The unique widget id to track interactions + */ + protected int id; + + /** + * Anchor orientation position + */ + private Anchor anchor; + + /** + * Anchor x offset position in pixel + */ + private double offsetX; + + /** + * Anchor y offset position in pixel + */ + private double offsetY; + + /** + * Abstract widget + * + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + */ + public Widget(int id, Anchor anchor, double offsetX, double offsetY) { + this.id = id; + this.anchor = anchor; + this.offsetX = offsetX; + this.offsetY = offsetY; + } + + public void setId(int id) { + this.id = id; + } + + public void setAnchor(Anchor anchor) { + this.anchor = anchor; + } + + public void setOffsetX(double offsetX) { + this.offsetX = offsetX; + } + + public void setOffsetY(double offsetY) { + this.offsetY = offsetY; + } + + public int getId() { + return id; + } + + public Anchor getAnchor() { + return anchor; + } + + public double getOffsetX() { + return offsetX; + } + + public double getOffsetY() { + return offsetY; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ButtonWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ButtonWidget.java new file mode 100644 index 0000000..2266bc3 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ButtonWidget.java @@ -0,0 +1,42 @@ +package net.labymod.serverapi.common.widgets.components.widgets; + +import com.google.gson.annotations.SerializedName; +import net.labymod.serverapi.common.widgets.components.ValueContainerWidget; +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Widget to add a button to the screen + * + * @author LabyStudio + */ +public class ButtonWidget extends ValueContainerWidget { + + /** + * Close the screen on an interaction with the button + */ + @SerializedName("close_screen_on_click") + private boolean closeScreenOnClick; + + /** + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param value The display value of the container + * @param width The width of the container in pixels + * @param height The height of the container in pixels + */ + public ButtonWidget(int id, Anchor anchor, double offsetX, double offsetY, String value, int width, int height) { + super(id, anchor, offsetX, offsetY, value, width, height); + + this.value = value; + } + + public void setCloseScreenOnClick(boolean closeScreenOnClick) { + this.closeScreenOnClick = closeScreenOnClick; + } + + public boolean isCloseScreenOnClick() { + return closeScreenOnClick; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ColorPickerWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ColorPickerWidget.java new file mode 100644 index 0000000..edfab17 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ColorPickerWidget.java @@ -0,0 +1,72 @@ +package net.labymod.serverapi.common.widgets.components.widgets; + +import com.google.gson.annotations.SerializedName; +import net.labymod.serverapi.common.widgets.components.ContainerWidget; +import net.labymod.serverapi.common.widgets.util.Anchor; + +import java.awt.Color; + +/** + * Color picker widget to choose a rgb color + * + * @author LabyStudio + */ +public class ColorPickerWidget extends ContainerWidget { + + /** + * The title of the color picker + */ + private String title; + + /** + * The selected color of the color picker + */ + @SerializedName("selected_color") + private Color selectedColor; + + /** + * Allow more than the default 15 minecraft colors + */ + private boolean rgb; + + /** + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param width The width of the container in pixels + * @param height The height of the container in pixels + * @param title The title of the color picker + * @param selectedColor The selected color of the color picker + */ + public ColorPickerWidget(int id, Anchor anchor, double offsetX, double offsetY, int width, int height, String title, Color selectedColor) { + super(id, anchor, offsetX, offsetY, width, height); + + this.title = title; + this.selectedColor = selectedColor; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Color getSelectedColor() { + return selectedColor; + } + + public void setSelectedColor(Color selectedColor) { + this.selectedColor = selectedColor; + } + + public boolean isRgb() { + return rgb; + } + + public void setRgb(boolean rgb) { + this.rgb = rgb; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ImageWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ImageWidget.java new file mode 100644 index 0000000..681abc4 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/ImageWidget.java @@ -0,0 +1,106 @@ +package net.labymod.serverapi.common.widgets.components.widgets; + + +import com.google.gson.annotations.SerializedName; +import net.labymod.serverapi.common.widgets.components.ContainerWidget; +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Widget to render a custom image + * + * @author LabyStudio + */ +public class ImageWidget extends ContainerWidget { + + /** + * Remote url of the image to render + */ + private String url; + + /** + * The left cut value of the image texture (Default 0) + */ + @SerializedName("cut_x") + private int cutX; + + /** + * The top cut value of the image texture (Default 0) + */ + @SerializedName("cut_y") + private int cutY; + + /** + * The right cut value of the image texture (Default 256) + */ + @SerializedName("cut_width") + private int cutWidth = 256; + + /** + * The bottom cut value of the image texture (Default 256) + */ + @SerializedName("cut_height") + private int cutHeight = 256; + + /** + * Create a custom texture to render + * + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param width The width of the container in pixels + * @param height The height of the container in pixels + * @param url Remote url of the image to render + */ + public ImageWidget(int id, Anchor anchor, double offsetX, double offsetY, int width, int height, String url) { + super(id, anchor, offsetX, offsetY, width, height); + this.url = url; + } + + /** + * Cut the image by the given amount on the left and top side + * + * @param x Amount of pixels to cut off on the left side + * @param y Amount of pixels to cut off on the top side + */ + public void setCutXY(int x, int y) { + this.cutX = x; + this.cutY = y; + } + + /** + * Cut the image by the given amount on the right and bottom side. 256 is the entire image. 256 / 2 would cut off the half of the image on the right + * and bottom side. + * + * @param width 0 - 256 image cut scale on the right side + * @param height 0 - 256 image cut scale on the bottom side + */ + public void setCutWithHeight(int width, int height) { + this.cutWidth = width; + this.cutHeight = height; + } + + public int getCutX() { + return cutX; + } + + public int getCutY() { + return cutY; + } + + public int getCutWidth() { + return cutWidth; + } + + public int getCutHeight() { + return cutHeight; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/LabelWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/LabelWidget.java new file mode 100644 index 0000000..fe8d0ea --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/LabelWidget.java @@ -0,0 +1,109 @@ +package net.labymod.serverapi.common.widgets.components.widgets; + + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import net.labymod.serverapi.common.widgets.components.Widget; +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Widget to display a string + * + * @author LabyStudio + */ +public class LabelWidget extends Widget { + + /** + * The string to display + */ + private JsonElement value; + + /** + * String alignment.
- 0: Left alignment
- 1: Center alignment
- 2: Right alignment + */ + private int alignment; + + /** + * The scale of the string. The default scale is 1.0. To make it smaller by half, 0.5 must be specified. + */ + private double scale; + + /** + * Create a label widget to display a string. + * + * @param sourceWidget Widget as anchor and offset source + * @param value The value of the label as string + * @param alignment String alignment (0:LEFT, 1:CENTER, 2:RIGHT) + * @param scale String scale (Default 1.0) + */ + public LabelWidget(Widget sourceWidget, String value, int alignment, double scale) { + this(sourceWidget.getId(), sourceWidget.getAnchor(), sourceWidget.getOffsetX(), sourceWidget.getOffsetY(), value, alignment, scale); + } + + /** + * Create a label widget to display a string. + * + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param value The value of the label (It is possible to change the color of the string by using the ยง character.) + * @param alignment String alignment (0:LEFT, 1:CENTER, 2:RIGHT) + * @param scale String scale (Default 1.0) + */ + public LabelWidget(int id, Anchor anchor, double offsetX, double offsetY, String value, int alignment, double scale) { + this(id, anchor, offsetX, offsetY, new JsonPrimitive(value), alignment, scale); + } + + /** + * Create a label widget to display a string. + * + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param value The value of the label as raw text + * @param alignment String alignment (0:LEFT, 1:CENTER, 2:RIGHT) + * @param scale String scale (Default 1.0) + */ + public LabelWidget(int id, Anchor anchor, double offsetX, double offsetY, JsonElement value, int alignment, double scale) { + super(id, anchor, offsetX, offsetY); + + this.value = value; + this.alignment = alignment; + this.scale = scale; + } + + /** + * Get label value as raw text + * + * @return Serialized raw text label + */ + public JsonElement getValue() { + return value; + } + + public void setValue(JsonElement value) { + this.value = value; + } + + public void setValue(String value) { + this.value = new JsonPrimitive(value); + } + + public void setAlignment(int alignment) { + this.alignment = alignment; + } + + public void setScale(double scale) { + this.scale = scale; + } + + public int getAlignment() { + return alignment; + } + + public double getScale() { + return scale; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/TextFieldWidget.java b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/TextFieldWidget.java new file mode 100644 index 0000000..59177f3 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/components/widgets/TextFieldWidget.java @@ -0,0 +1,74 @@ +package net.labymod.serverapi.common.widgets.components.widgets; + +import com.google.gson.annotations.SerializedName; +import net.labymod.serverapi.common.widgets.components.ValueContainerWidget; +import net.labymod.serverapi.common.widgets.util.Anchor; + +/** + * Text field widget for user text input + * + * @author LabyStudio + */ +public class TextFieldWidget extends ValueContainerWidget { + + /** + * The placeholder that will be displayed when the field is not in focus and the value of the field is empty. + */ + protected String placeholder; + + /** + * The max length of the text field value + */ + @SerializedName("max_length") + protected int maxLength; + + /** + * Focus the text field on load + */ + protected boolean focused; + + /** + * @param id Unique id of the widget + * @param anchor Anchor point for orientation + * @param offsetX X anchor offset in pixel + * @param offsetY Y anchor offset in pixel + * @param value The display value of the container + * @param width The width of the container in pixels + * @param height The height of the container in pixels + * @param placeholder The placeholder that will be displayed when the field is not in focus and the value of the field is empty. + * @param maxLength The max length of the text field value + * @param focused Focus the text field on load + */ + public TextFieldWidget(int id, Anchor anchor, double offsetX, double offsetY, String value, int width, int height, String placeholder, + int maxLength, boolean focused) { + super(id, anchor, offsetX, offsetY, value, width, height); + + this.placeholder = placeholder; + this.maxLength = maxLength; + this.focused = focused; + } + + public void setPlaceholder(String placeholder) { + this.placeholder = placeholder; + } + + public void setMaxLength(int maxLength) { + this.maxLength = maxLength; + } + + public void setFocused(boolean focused) { + this.focused = focused; + } + + public String getPlaceholder() { + return placeholder; + } + + public int getMaxLength() { + return maxLength; + } + + public boolean isFocused() { + return focused; + } +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/util/Anchor.java b/src/main/java/net/labymod/serverapi/common/widgets/util/Anchor.java new file mode 100644 index 0000000..53b7127 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/util/Anchor.java @@ -0,0 +1,49 @@ +package net.labymod.serverapi.common.widgets.util; + +/** + * Anchor position on the screen for component orientation + * + * @author LabyStudio + */ +public class Anchor { + + /** + * Anchor x position in percent.

Examples:
0% would be on the left side of the screen.
50% is in the middle of the screen. + */ + private final double x; + + /** + * Anchor y position in percent

Examples:
0% would be on the top side of the screen.
50% is in the middle of the screen. + */ + private final double y; + + /** + * Create anchor for orientation + * + * @param x Anchor x percentage (0 - 100) + * @param y Anchor y percentage (0 - 100) + */ + public Anchor(double x, double y) { + this.x = x; + this.y = y; + } + + /** + * Get the anchor x percentage + * + * @return Anchor x percentage + */ + public double getX() { + return x; + } + + /** + * Get the anchor y percentage + * + * @return Anchor y percentage + */ + public double getY() { + return y; + } + +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/util/EnumResponse.java b/src/main/java/net/labymod/serverapi/common/widgets/util/EnumResponse.java new file mode 100644 index 0000000..65f3487 --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/util/EnumResponse.java @@ -0,0 +1,11 @@ +package net.labymod.serverapi.common.widgets.util; + +/** + * Response sent by the client when closing or interacting with the screen. + * + * @author LabyStudio + */ +public enum EnumResponse { + CLOSE, // Type id 0 - Sent when the player closes the GUI + INTERACT // Type id 1 - Sent when the players interact with a widget +} diff --git a/src/main/java/net/labymod/serverapi/common/widgets/util/EnumScreenAction.java b/src/main/java/net/labymod/serverapi/common/widgets/util/EnumScreenAction.java new file mode 100644 index 0000000..1a7d1de --- /dev/null +++ b/src/main/java/net/labymod/serverapi/common/widgets/util/EnumScreenAction.java @@ -0,0 +1,12 @@ +package net.labymod.serverapi.common.widgets.util; + +/** + * Screen action sent by the server to tell the client what to do with the provided widgets. + * + * @author LabyStudio + */ +public enum EnumScreenAction { + OPEN, // Action id 0 - Open a new screen + UPDATE, // Action id 1 - Update the current screen + CLOSE // Action id 2 - Force close the current screen and display the previous screen of the user +}