From 5d8e45469dc3db29248e18b79a2341d8562fddb8 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 22 Jun 2025 23:28:43 +0500 Subject: [PATCH] FloatMods --- addons/floatmodifiers/FlatModsInspector.cs | 23 +++++ .../floatmodifiers/FlatModsInspector.cs.uid | 1 + addons/floatmodifiers/FloatModifiers.cs | 59 +++++++++++++ addons/floatmodifiers/FloatModifiers.cs.uid | 1 + addons/floatmodifiers/FloatModsProperty.cs | 85 +++++++++++++++++++ .../floatmodifiers/FloatModsProperty.cs.uid | 1 + addons/floatmodifiers/Plugin.cs | 20 +++++ addons/floatmodifiers/Plugin.cs.uid | 1 + .../floatmodifiers/float_mods_property.tscn | 57 +++++++++++++ addons/floatmodifiers/plugin.cfg | 7 ++ 10 files changed, 255 insertions(+) create mode 100644 addons/floatmodifiers/FlatModsInspector.cs create mode 100644 addons/floatmodifiers/FlatModsInspector.cs.uid create mode 100644 addons/floatmodifiers/FloatModifiers.cs create mode 100644 addons/floatmodifiers/FloatModifiers.cs.uid create mode 100644 addons/floatmodifiers/FloatModsProperty.cs create mode 100644 addons/floatmodifiers/FloatModsProperty.cs.uid create mode 100644 addons/floatmodifiers/Plugin.cs create mode 100644 addons/floatmodifiers/Plugin.cs.uid create mode 100644 addons/floatmodifiers/float_mods_property.tscn create mode 100644 addons/floatmodifiers/plugin.cfg diff --git a/addons/floatmodifiers/FlatModsInspector.cs b/addons/floatmodifiers/FlatModsInspector.cs new file mode 100644 index 0000000..561a01b --- /dev/null +++ b/addons/floatmodifiers/FlatModsInspector.cs @@ -0,0 +1,23 @@ +#if TOOLS +using Godot; + +public partial class FlatModsInspector : EditorInspectorPlugin +{ + public override bool _CanHandle(GodotObject @object) + { + return true; + } + public override bool _ParseProperty(GodotObject @object, Variant.Type type, string name, PropertyHint hintType, string hintString, PropertyUsageFlags usageFlags, bool wide) + { + if (hintString == "FloatModifiers") + { + AddPropertyEditor(name, new FloatModsProperty()); + return true; + } + return false; + } + +} + + +#endif \ No newline at end of file diff --git a/addons/floatmodifiers/FlatModsInspector.cs.uid b/addons/floatmodifiers/FlatModsInspector.cs.uid new file mode 100644 index 0000000..f325609 --- /dev/null +++ b/addons/floatmodifiers/FlatModsInspector.cs.uid @@ -0,0 +1 @@ +uid://uc8igx80sr6u diff --git a/addons/floatmodifiers/FloatModifiers.cs b/addons/floatmodifiers/FloatModifiers.cs new file mode 100644 index 0000000..e540413 --- /dev/null +++ b/addons/floatmodifiers/FloatModifiers.cs @@ -0,0 +1,59 @@ + +using Godot; + +[GlobalClass] [Tool] +public partial class FloatModifiers : Resource +{ + [Export] private float flat_value; + [Export] private float percentage_value; + [Export] private float mult_value; + + public static FloatModifiers Instantiate(float flat = 0.0f, float per = 0.0f, float mult = 1.0f) + { + FloatModifiers mod = new() + { + flat_value = flat, + percentage_value = per, + mult_value = mult + }; + return mod; + } + + public float GetValue() => flat_value * mult_value * (1.0f + percentage_value); + + public float GetFlat() => flat_value; + + public float GetPercentage() => percentage_value; + + public float GetMult() => mult_value; + + public void SetFlat(float value) + { + flat_value = value; + } + + public void SetPercentage(float value) + { + percentage_value = value; + } + + public void SetMult(float value) + { + mult_value = value; + } + + public void ChangeFlat(float amount) + { + flat_value += amount; + } + + public void ChangePercentage(float amount) + { + percentage_value += amount; + } + + public void ChangeMult(float amount) + { + mult_value *= amount; + } +} diff --git a/addons/floatmodifiers/FloatModifiers.cs.uid b/addons/floatmodifiers/FloatModifiers.cs.uid new file mode 100644 index 0000000..e874efc --- /dev/null +++ b/addons/floatmodifiers/FloatModifiers.cs.uid @@ -0,0 +1 @@ +uid://c3cfnrmnnuqms diff --git a/addons/floatmodifiers/FloatModsProperty.cs b/addons/floatmodifiers/FloatModsProperty.cs new file mode 100644 index 0000000..b2d5b14 --- /dev/null +++ b/addons/floatmodifiers/FloatModsProperty.cs @@ -0,0 +1,85 @@ +#if TOOLS +using Godot; + +public partial class FloatModsProperty : EditorProperty +{ + private VBoxContainer _modsControl; + private FloatModifiers _currentValue; + private bool _updating; + + public FloatModsProperty() + { + _modsControl = GD.Load("res://addons/floatmodifiers/float_mods_property.tscn").Instantiate(); + AddChild(_modsControl); + AddFocusable(_modsControl); + + RefreshControl(); + + _modsControl.GetNode("%Flat").ValueChanged += UpdateFlat; + _modsControl.GetNode("%Percentage").ValueChanged += UpdatePercentage; + _modsControl.GetNode("%Mult").ValueChanged += UpdateMult; + } + + private void UpdateFlat(double value) + { + if (_updating) return; + + _currentValue.SetFlat((float)value); + + RefreshControl(); + EmitChanged(GetEditedProperty(), _currentValue); + } + private void UpdatePercentage(double value) + { + if (_updating) return; + + _currentValue.SetPercentage((float)value); + + RefreshControl(); + EmitChanged(GetEditedProperty(), _currentValue); + } + private void UpdateMult(double value) + { + if (_updating) return; + + _currentValue.SetMult((float)value); + + RefreshControl(); + EmitChanged(GetEditedProperty(), _currentValue); + } + + private void RefreshControl() + { + if (_currentValue == null) + { + return; + } + + _modsControl.GetNode("%Flat").SetValueNoSignal(_currentValue.GetFlat()); + _modsControl.GetNode("%Percentage").SetValueNoSignal(_currentValue.GetPercentage()); + _modsControl.GetNode("%Mult").SetValueNoSignal(_currentValue.GetMult()); + } + + + public override void _UpdateProperty() + { + // Read the current value from the property. + var newValue = GetEditedObject().Get(GetEditedProperty()).As(); + if (newValue == null) + { + newValue = new(); + EmitChanged(GetEditedProperty(), newValue); + } + if (newValue == _currentValue) + { + return; + } + + // Update the control with the new value. + _updating = true; + _currentValue = newValue; + RefreshControl(); + _updating = false; + } +} +#endif \ No newline at end of file diff --git a/addons/floatmodifiers/FloatModsProperty.cs.uid b/addons/floatmodifiers/FloatModsProperty.cs.uid new file mode 100644 index 0000000..3e20dd2 --- /dev/null +++ b/addons/floatmodifiers/FloatModsProperty.cs.uid @@ -0,0 +1 @@ +uid://dv5koj37coavh diff --git a/addons/floatmodifiers/Plugin.cs b/addons/floatmodifiers/Plugin.cs new file mode 100644 index 0000000..9a24081 --- /dev/null +++ b/addons/floatmodifiers/Plugin.cs @@ -0,0 +1,20 @@ +#if TOOLS +using Godot; + +[Tool] +public partial class Plugin : EditorPlugin +{ + private FlatModsInspector _plugin; + + public override void _EnterTree() + { + _plugin = new FlatModsInspector(); + AddInspectorPlugin(_plugin); + } + + public override void _ExitTree() + { + RemoveInspectorPlugin(_plugin); + } +} +#endif diff --git a/addons/floatmodifiers/Plugin.cs.uid b/addons/floatmodifiers/Plugin.cs.uid new file mode 100644 index 0000000..cd73c61 --- /dev/null +++ b/addons/floatmodifiers/Plugin.cs.uid @@ -0,0 +1 @@ +uid://fjnw4mev4eiq diff --git a/addons/floatmodifiers/float_mods_property.tscn b/addons/floatmodifiers/float_mods_property.tscn new file mode 100644 index 0000000..c842ea7 --- /dev/null +++ b/addons/floatmodifiers/float_mods_property.tscn @@ -0,0 +1,57 @@ +[gd_scene format=3 uid="uid://d2ne6ml10xgcl"] + +[node name="FloatModsProperty" type="VBoxContainer"] +offset_right = 163.0 +offset_bottom = 104.0 + +[node name="FlatContainer" type="HBoxContainer" parent="."] +layout_mode = 2 + +[node name="Label" type="Label" parent="FlatContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "Flat" + +[node name="Flat" type="SpinBox" parent="FlatContainer"] +unique_name_in_owner = true +custom_minimum_size = Vector2(128, 0) +layout_mode = 2 +step = 0.001 +allow_greater = true +allow_lesser = true + +[node name="PercentageContainer" type="HBoxContainer" parent="."] +layout_mode = 2 + +[node name="Label" type="Label" parent="PercentageContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "%" + +[node name="Percentage" type="SpinBox" parent="PercentageContainer"] +unique_name_in_owner = true +custom_minimum_size = Vector2(128, 0) +layout_mode = 2 +max_value = 10.0 +step = 0.001 +allow_greater = true +allow_lesser = true +prefix = "+" + +[node name="MultContainer" type="HBoxContainer" parent="."] +layout_mode = 2 + +[node name="Label" type="Label" parent="MultContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "Mult" + +[node name="Mult" type="SpinBox" parent="MultContainer"] +unique_name_in_owner = true +custom_minimum_size = Vector2(128, 0) +layout_mode = 2 +step = 0.001 +value = 1.0 +allow_greater = true +allow_lesser = true +prefix = "*" diff --git a/addons/floatmodifiers/plugin.cfg b/addons/floatmodifiers/plugin.cfg new file mode 100644 index 0000000..e434f40 --- /dev/null +++ b/addons/floatmodifiers/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="FloatModifiers" +description="" +author="R34nd0" +version="1.0" +script="Plugin.cs"