FloatMods

This commit is contained in:
Rendo 2025-06-22 23:28:43 +05:00
commit 5d8e45469d
10 changed files with 255 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1 @@
uid://uc8igx80sr6u

View file

@ -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;
}
}

View file

@ -0,0 +1 @@
uid://c3cfnrmnnuqms

View file

@ -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<PackedScene>("res://addons/floatmodifiers/float_mods_property.tscn").Instantiate<VBoxContainer>();
AddChild(_modsControl);
AddFocusable(_modsControl);
RefreshControl();
_modsControl.GetNode<SpinBox>("%Flat").ValueChanged += UpdateFlat;
_modsControl.GetNode<SpinBox>("%Percentage").ValueChanged += UpdatePercentage;
_modsControl.GetNode<SpinBox>("%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<SpinBox>("%Flat").SetValueNoSignal(_currentValue.GetFlat());
_modsControl.GetNode<SpinBox>("%Percentage").SetValueNoSignal(_currentValue.GetPercentage());
_modsControl.GetNode<SpinBox>("%Mult").SetValueNoSignal(_currentValue.GetMult());
}
public override void _UpdateProperty()
{
// Read the current value from the property.
var newValue = GetEditedObject().Get(GetEditedProperty()).As<FloatModifiers>();
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

View file

@ -0,0 +1 @@
uid://dv5koj37coavh

View file

@ -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

View file

@ -0,0 +1 @@
uid://fjnw4mev4eiq

View file

@ -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 = "*"

View file

@ -0,0 +1,7 @@
[plugin]
name="FloatModifiers"
description=""
author="R34nd0"
version="1.0"
script="Plugin.cs"