Package com.j3d.gen.settings
Class Setting<T>
java.lang.Object
com.j3d.gen.settings.Setting<T>
- Type Parameters:
T- The type of the setting.
- All Implemented Interfaces:
EventEmitterInterface,SettingsChild
- Direct Known Subclasses:
BooleanSetting,ComplexSetting,DoubleSetting,EnumSetting,IntSetting,StringSetting
A generic class representing a single setting.
This class is used to store a single setting, along with its description. It can be used for any type of setting,
by specifying the type parameter <T>.
A setting also serializes into a key value pair. (although this is usually with dot syntax)
So the setting CameraProperties.movementSpeed serializes into:
settings.cameraProperties.movementSpeed = 5.0
and alot more logic that's readable in serialize() deserialize(ArrayList)
provided by SettingsChild and also found in SettingsParent
- Author:
- Lehlogonolo Poole
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidattachListener(EventListener event) Attaches anEventListenerto this setting to receive updates.<K> voidbroadcast(EventType eventType, EventPayload<K> properties) Broadcasts an event of a specific type with associated properties to all registered listeners.voiddeserialize(ArrayList<String> leftover) Deserializes the setting's value from a list of strings.voidDetaches all registeredEventListeners from this setting.voiddetachListener(EventListener event) Detaches anEventListenerfrom this setting.fromString(String str) Converts a string representation back to the setting's type.Returns the default value of this setting.Returns the description of this setting.getName()Returns the name of this setting.getValue()Returns the current value of this setting.booleanChecks if a specificEventListeneris currently attached to this setting.onSetValue(Function<T, Void> callback) Registers a callback function to be executed whenever the setting's value is changed.<R extends Component>
Rpanel()Returns the panel associated with this setting for display and interaction.Serializes the current setting into a list of strings.voidSets the value of this setting and broadcasts an update event.setValueNoBroadcast(T value) Sets the value of this setting without broadcasting a specificSETTING_CODE_UPDATEDevent.valueToString(T value) Converts the setting's value to its string representation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.j3d.gen.settings.SettingsChild
serializedName
-
Field Details
-
value
-
description
-
defaultValue
-
name
-
registered
-
callback
-
-
Constructor Details
-
Setting
Constructs a new Setting with the given name, initial value, and description. The initial value also serves as the default value.- Parameters:
name- The name of the setting.value- The initial value of the setting.description- A brief description of what the setting controls.
-
-
Method Details
-
valueToString
Converts the setting's value to its string representation.- Parameters:
value- The value to convert to a string.- Returns:
- The string representation of the value.
-
fromString
Converts a string representation back to the setting's type.- Parameters:
str- The string to convert.- Returns:
- The value of type T parsed from the string.
- Throws:
ClassCastException- if the string cannot be cast to type T (for the default implementation).
-
serialize
Serializes the current setting into a list of strings. For a single setting, this typically returns a list containing one string in the format "settingName = settingValue".- Specified by:
serializein interfaceSettingsChild- Returns:
- An
ArrayListof strings representing the serialized setting.
-
deserialize
Deserializes the setting's value from a list of strings. It searches for a string containing the serialized name of this setting, parses its value, and updates the setting. If no match is found or parsing fails, the setting is reset to its default value.- Specified by:
deserializein interfaceSettingsChild- Parameters:
leftover- AnArrayListof strings from which to deserialize the setting.
-
getName
Returns the name of this setting.- Specified by:
getNamein interfaceSettingsChild- Returns:
- The name of the setting.
-
getValue
Returns the current value of this setting. If the current value is null, the default value is returned.- Returns:
- The current value of the setting.
-
setValue
Sets the value of this setting and broadcasts an update event.- Parameters:
value- The new value to set.
-
setValueNoBroadcast
Sets the value of this setting without broadcasting a specificSETTING_CODE_UPDATEDevent.- Parameters:
value- The new value to set.- Returns:
- The old value of the setting before the update.
-
getDefaultValue
Returns the default value of this setting.- Returns:
- The default value.
-
getDescription
Returns the description of this setting.- Specified by:
getDescriptionin interfaceSettingsChild- Returns:
- The description string.
-
panel
Returns the panel associated with this setting for display and interaction.- Specified by:
panelin interfaceSettingsChild- Type Parameters:
R- The type of the UI component, which must extendComponent.- Returns:
- A UI component representing this setting, or
nullif not implemented by a subclass.
-
attachListener
Attaches anEventListenerto this setting to receive updates.- Specified by:
attachListenerin interfaceEventEmitterInterface- Parameters:
event- The listener to attach.
-
detachListener
Detaches anEventListenerfrom this setting.- Specified by:
detachListenerin interfaceEventEmitterInterface- Parameters:
event- The listener to detach.
-
detachAll
public void detachAll()Detaches all registeredEventListeners from this setting.- Specified by:
detachAllin interfaceEventEmitterInterface
-
broadcast
Broadcasts an event of a specific type with associated properties to all registered listeners.- Specified by:
broadcastin interfaceEventEmitterInterface- Type Parameters:
K- The emitter of the payload.- Parameters:
eventType- The type of the event to broadcast.properties- The payload containing event-specific data.
-
isAttached
Checks if a specificEventListeneris currently attached to this setting.- Specified by:
isAttachedin interfaceEventEmitterInterface- Parameters:
e- The listener to check.- Returns:
trueif the listener is attached,falseotherwise.
-
onSetValue
Registers a callback function to be executed whenever the setting's value is changed. The callback receives the new value as an argument.
-