Skip to content

Commit

Permalink
Initial Work on scroll view component
Browse files Browse the repository at this point in the history
  • Loading branch information
dassjosh committed Apr 30, 2024
1 parent 1f171e6 commit 22afbce
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/Rust.UIFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Global
Release|Carbon = Release|Carbon
Debug|Carbon = Debug|Carbon
Debug|Oxide = Debug|Oxide
Benchmarks|Oxide = Benchmarks|Oxide
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Debug|Carbon.ActiveCfg = Debug|Carbon
Expand All @@ -19,12 +18,8 @@ Global
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Debug|Oxide.Build.0 = Debug|Oxide
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Release|Carbon.ActiveCfg = Release|Carbon
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Release|Carbon.Build.0 = Release|Carbon
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Benchmarks|Oxide.ActiveCfg = Benchmarks|Oxide
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Benchmarks|Oxide.Build.0 = Benchmarks|Oxide
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Release|Oxide.ActiveCfg = Release|Oxide
{2CEE08B1-A5BD-4F64-A1A9-5242440758D0}.Release|Oxide.Build.0 = Release|Oxide
{6C18B22C-7319-4C3F-B7F9-F9B87B6B244C}.Benchmarks|Oxide.ActiveCfg = Benchmarks|Oxide
{6C18B22C-7319-4C3F-B7F9-F9B87B6B244C}.Benchmarks|Oxide.Build.0 = Benchmarks|Oxide
{6C18B22C-7319-4C3F-B7F9-F9B87B6B244C}.Release|Oxide.ActiveCfg = Release|Oxide
{6C18B22C-7319-4C3F-B7F9-F9B87B6B244C}.Release|Oxide.Build.0 = Release|Oxide
EndGlobalSection
Expand Down
12 changes: 11 additions & 1 deletion src/Rust.UIFramework/Builder/BaseUiBuilder.Components.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Oxide.Ext.UiFramework.Colors;
using Oxide.Ext.UiFramework.Controls;
using Oxide.Ext.UiFramework.Enums;
using Oxide.Ext.UiFramework.Exceptions;
using Oxide.Ext.UiFramework.Offsets;
using Oxide.Ext.UiFramework.Positions;
using Oxide.Ext.UiFramework.UiElements;
Expand Down Expand Up @@ -199,5 +198,16 @@ public UiSection Anchor(in UiReference parent, in UiPosition pos, in UiOffset of
return section;
}
#endregion

#region ScrollView

public UiScrollView ScrollView(in UiReference parent, in UiPosition pos, in UiOffset offset, bool horizontal = false, bool vertical = false, ScrollRect.MovementType movementType = ScrollRect.MovementType.Clamped, float elasticity = 0.1f,
bool inertia = false, float decelerationRate = 0.135f, float scrollSensitivity = 1f)
{
UiScrollView scroll = UiScrollView.Create(pos, offset, horizontal, vertical, movementType, elasticity, inertia, decelerationRate, scrollSensitivity);
AddComponent(scroll, parent);
return scroll;
}
#endregion
}
}
4 changes: 2 additions & 2 deletions src/Rust.UIFramework/Builder/UI/UiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public CachedUiBuilder ToCachedBuilder()
public override void AddComponent(BaseUiComponent component, in UiReference parent)
{
UiReferenceException.ThrowIfInvalidParent(parent);
component.Reference = new UiReference(parent.Name, UiNameCache.GetComponentName(RootName, Components.Count));
component.Reference = parent.WithChild(UiNameCache.GetComponentName(RootName, Components.Count));
Components.Add(component);
}

protected override void AddAnchor(BaseUiComponent component, in UiReference parent)
{
UiReferenceException.ThrowIfInvalidParent(parent);
component.Reference = new UiReference(parent.Name, UiNameCache.GetAnchorName(RootName, Anchors.Count));
component.Reference = parent.WithChild(UiNameCache.GetAnchorName(RootName, Anchors.Count));
Anchors.Add(component);
}
#endregion
Expand Down
5 changes: 1 addition & 4 deletions src/Rust.UIFramework/Colors/UiColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public UiColor(float red, float green, float blue, float alpha = 1f) : this(Math

private static float ToFloat(byte value) => value / 255f;

public bool Equals(UiColor other)
{
return this == other;
}
public bool Equals(UiColor other) => this == other;

public override bool Equals(object obj)
{
Expand Down
25 changes: 25 additions & 0 deletions src/Rust.UIFramework/Components/BasePoolableComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Oxide.Ext.UiFramework.Json;
using Oxide.Ext.UiFramework.Pooling;

namespace Oxide.Ext.UiFramework.Components
{
public abstract class BasePoolableComponent : BasePoolable, IComponent
{
public bool Enabled = true;

public virtual void WriteComponent(JsonFrameworkWriter writer)
{
writer.AddField(JsonDefaults.Common.EnabledName, Enabled, true);
}

protected override void EnterPool()
{
Reset();
}

public virtual void Reset()
{
Enabled = true;
}
}
}
19 changes: 19 additions & 0 deletions src/Rust.UIFramework/Components/RectTransformComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Oxide.Ext.UiFramework.Json;
using Oxide.Ext.UiFramework.Offsets;
using Oxide.Ext.UiFramework.Positions;

namespace Oxide.Ext.UiFramework.Components;

public abstract class RectTransformComponent : IComponent
{
public UiPosition Position;
public UiOffset Offset;

public abstract void WriteComponent(JsonFrameworkWriter writer);

public virtual void Reset()
{
Position = default;
Offset = default;
}
}
64 changes: 64 additions & 0 deletions src/Rust.UIFramework/Components/ScrollViewComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Oxide.Ext.UiFramework.Json;
using UnityEngine.UI;

namespace Oxide.Ext.UiFramework.Components;

public class ScrollViewComponent : IComponent
{
private const string Type = "UnityEngine.UI.ScrollView";

public ScrollViewContentTransformComponent ContentTransform = new();
public bool Horizontal;
public bool Vertical;
public ScrollRect.MovementType MovementType = ScrollRect.MovementType.Clamped;
public float Elasticity = JsonDefaults.ScrollView.Elasticity;
public bool Inertia;
public float DecelerationRate = JsonDefaults.ScrollView.DecelerationRate;
public float ScrollSensitivity = JsonDefaults.ScrollView.ScrollSensitivity;
public ScrollbarComponent HorizontalScrollbar;
public ScrollbarComponent VerticalScrollbar;

public void WriteComponent(JsonFrameworkWriter writer)
{
writer.WriteStartObject();
writer.AddFieldRaw(JsonDefaults.Common.ComponentTypeName, Type);
writer.AddField(JsonDefaults.ScrollView.Horizontal, Horizontal, false);
writer.AddField(JsonDefaults.ScrollView.Vertical, Vertical, false);
writer.AddField(JsonDefaults.ScrollView.MovementType, MovementType);
writer.AddField(JsonDefaults.ScrollView.ElasticityName, Elasticity, JsonDefaults.ScrollView.Elasticity);
writer.AddField(JsonDefaults.ScrollView.Inertia, Inertia, false);
writer.AddField(JsonDefaults.ScrollView.DecelerationRateName, DecelerationRate, JsonDefaults.ScrollView.DecelerationRate);
writer.AddField(JsonDefaults.ScrollView.ScrollSensitivityName, ScrollSensitivity, JsonDefaults.ScrollView.ScrollSensitivity);

if (Horizontal)
{
writer.AddComponent(JsonDefaults.ScrollView.HorizontalScrollbar, HorizontalScrollbar);
}

if (Vertical)
{
writer.AddComponent(JsonDefaults.ScrollView.VerticalScrollbar, VerticalScrollbar);
}

if (ContentTransform != null)
{
writer.AddComponent(JsonDefaults.ScrollView.ContentTransform, ContentTransform);
}

writer.WriteEndObject();
}

public void Reset()
{
ContentTransform.Reset();
Horizontal = false;
Vertical = false;
MovementType = ScrollRect.MovementType.Clamped;
Elasticity = JsonDefaults.ScrollView.Elasticity;
Inertia = false;
DecelerationRate = JsonDefaults.ScrollView.DecelerationRate;
ScrollSensitivity = JsonDefaults.ScrollView.ScrollSensitivity;
HorizontalScrollbar = null;
VerticalScrollbar = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Oxide.Ext.UiFramework.Json;
using Oxide.Ext.UiFramework.Offsets;
using Oxide.Ext.UiFramework.Positions;

namespace Oxide.Ext.UiFramework.Components;

public class ScrollViewContentTransformComponent : RectTransformComponent
{
public ScrollViewContentTransformComponent()
{
Position = new UiPosition(0, 0, 1, 1);
Offset = new UiOffset(0, 0, 0, 0);
}

public override void WriteComponent(JsonFrameworkWriter writer)
{
writer.WriteStartObject();
writer.AddPosition(JsonDefaults.Position.AnchorMinName, Position.Min, JsonDefaults.ScrollView.Min);
writer.AddPosition(JsonDefaults.Position.AnchorMaxName, Position.Max, JsonDefaults.ScrollView.AnchorMax);
writer.AddOffset(JsonDefaults.Offset.OffsetMinName, Offset.Min, JsonDefaults.ScrollView.Min);
writer.AddOffset(JsonDefaults.Offset.OffsetMaxName, Offset.Max, JsonDefaults.ScrollView.OffsetMax);
writer.WriteEndObject();
}

public override void Reset()
{
Position = new UiPosition(0, 0, 1, 1);
Offset = new UiOffset(0, 0, 0, 0);
}
}
47 changes: 47 additions & 0 deletions src/Rust.UIFramework/Components/ScrollbarComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Oxide.Ext.UiFramework.Colors;
using Oxide.Ext.UiFramework.Json;

namespace Oxide.Ext.UiFramework.Components;

public class ScrollbarComponent : BasePoolableComponent
{
public bool Invert;
public bool AutoHide;
public string HandleSprite;
public string TrackSprite;
public float Size = JsonDefaults.ScrollBar.Size;
public UiColor HandleColor = JsonDefaults.ScrollBar.HandleColor;
public UiColor HighlightColor = JsonDefaults.ScrollBar.HighlightColor;
public UiColor PressedColor = JsonDefaults.ScrollBar.PressedColor;
public UiColor TrackColor = JsonDefaults.ScrollBar.TrackColor;

public override void WriteComponent(JsonFrameworkWriter writer)
{
writer.WriteStartObject();
base.WriteComponent(writer);
writer.AddField(JsonDefaults.ScrollBar.Invert, Invert, false);
writer.AddField(JsonDefaults.ScrollBar.AutoHide, AutoHide, false);
writer.AddField(JsonDefaults.ScrollBar.HandleSprite, HandleSprite, null);
writer.AddField(JsonDefaults.ScrollBar.TrackSprite, TrackSprite, null);
writer.AddField(JsonDefaults.ScrollBar.SizeName, Size, JsonDefaults.ScrollBar.Size);
writer.AddField(JsonDefaults.ScrollBar.HandleColorName, HandleColor, JsonDefaults.ScrollBar.HandleColor);
writer.AddField(JsonDefaults.ScrollBar.HighlightColorName, HighlightColor, JsonDefaults.ScrollBar.HighlightColor);
writer.AddField(JsonDefaults.ScrollBar.PressedColorName, PressedColor, JsonDefaults.ScrollBar.PressedColor);
writer.AddField(JsonDefaults.ScrollBar.TrackColorName, TrackColor, JsonDefaults.ScrollBar.TrackColor);
writer.WriteEndObject();
}

public override void Reset()
{
base.Reset();
Invert = false;
AutoHide = false;
HandleSprite = null;
TrackSprite = null;
Size = JsonDefaults.ScrollBar.Size;
HandleColor = JsonDefaults.ScrollBar.HandleColor;
HighlightColor = JsonDefaults.ScrollBar.HighlightColor;
PressedColor = JsonDefaults.ScrollBar.PressedColor;
TrackColor = JsonDefaults.ScrollBar.TrackColor;
}
}
40 changes: 40 additions & 0 deletions src/Rust.UIFramework/Json/JsonDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,45 @@ public static class Countdown
public const int StepValue = 1;
public const string CountdownCommandName = "command";
}

public static class ScrollView
{
public const string Horizontal = "horizontal";
public const string Vertical = "vertical";
public const string MovementType = "movementType";
public const string ElasticityName = "elasticity";
public const string Inertia = "inertia";
public const string DecelerationRateName = "decelerationRate";
public const string ScrollSensitivityName = "scrollSensitivity";
public const string HorizontalScrollbar = "horizontalScrollbar";
public const string VerticalScrollbar = "verticalScrollbar";
public const string ContentTransform = "contentTransform";

public static readonly float Elasticity = 0.1f;
public static readonly float DecelerationRate = 0.135f;
public static readonly float ScrollSensitivity = 1f;

public static readonly Vector2 Min = new(0, 0);
public static readonly Vector2 AnchorMax = new(1, 1);
public static readonly Vector2 OffsetMax = new(0, 0);
}

public static class ScrollBar
{
public const string Invert = "invert";
public const string AutoHide = "autoHide";
public const string HandleSprite = "handleSprite";
public const string TrackSprite = "trackSprite";
public const string SizeName = "size";
public const float Size = 20f;
public const string HandleColorName = "handleColor";
public const string HighlightColorName = "highlightColor";
public const string PressedColorName = "pressedColor";
public const string TrackColorName = "trackColor";
public static readonly UiColor HandleColor = UiColor.ParseHexColor("#262626");
public static readonly UiColor HighlightColor = UiColor.ParseHexColor("#2B2B2B");
public static readonly UiColor PressedColor = UiColor.ParseHexColor("#333333");
public static readonly UiColor TrackColor = UiColor.ParseHexColor("#171717");
}
}
}
39 changes: 39 additions & 0 deletions src/Rust.UIFramework/Json/JsonFrameworkWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Network;
using Oxide.Ext.UiFramework.Cache;
using Oxide.Ext.UiFramework.Colors;
using Oxide.Ext.UiFramework.Components;
using Oxide.Ext.UiFramework.Pooling;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -147,6 +148,15 @@ public void AddField(string name, VerticalWrapMode value)
WriteValue(EnumCache<VerticalWrapMode>.ToString(value));
}
}

public void AddField(string name, ScrollRect.MovementType value)
{
if (value != ScrollRect.MovementType.Clamped)
{
WritePropertyName(name);
WriteValue(EnumCache<ScrollRect.MovementType>.ToString(value));
}
}

public void AddField(string name, int value, int defaultValue)
{
Expand Down Expand Up @@ -193,6 +203,35 @@ public void AddField(string name, UiColor color)
}
}

public void AddField(string name, UiColor color, UiColor defaultColor)
{
if (color != defaultColor)
{
WritePropertyName(name);
WriteValue(color);
}
}

public void AddComponent(string name, IComponent component)
{
WritePropertyName(name);
bool objectComma = _objectComma;
bool propertyComma = _propertyComma;
_objectComma = false;
_propertyComma = false;
if (component != null)
{
component.WriteComponent(this);
}
else
{
WriteStartObject();
WriteEndObject();
}
_objectComma = objectComma;
_propertyComma = propertyComma;
}

public void AddKeyField(string name)
{
WritePropertyName(name);
Expand Down
2 changes: 2 additions & 0 deletions src/Rust.UIFramework/UiElements/UiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public UiReference(string parent, string name)
Name = name;
}

public UiReference WithChild(string name) => new(Name, name);

public bool IsValidParent() => !string.IsNullOrEmpty(Parent);
public bool IsValidReference() => IsValidParent() && !string.IsNullOrEmpty(Name);
}
Expand Down
Loading

0 comments on commit 22afbce

Please sign in to comment.