Rainbow crayon + color-selectable crayons (#7786)

This commit is contained in:
Flipp Syder
2022-04-28 05:23:45 -07:00
committed by GitHub
parent 4c9e45a480
commit 2325a7df94
9 changed files with 105 additions and 3 deletions

View File

@@ -40,6 +40,11 @@ namespace Content.Client.Crayon.UI
SendMessage(new CrayonSelectMessage(state));
}
public void SelectColor(string color)
{
SendMessage(new CrayonColorMessage(color));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

View File

@@ -3,6 +3,7 @@
MinSize="250 300"
SetSize="250 300">
<BoxContainer Orientation="Vertical">
<ColorSelectorSliders Name="ColorSelector" Visible="False" />
<LineEdit Name="Search" />
<ScrollContainer VerticalExpand="True">
<GridContainer Name="Grid" Columns="6">

View File

@@ -30,6 +30,16 @@ namespace Content.Client.Crayon.UI
Owner = owner;
Search.OnTextChanged += _ => RefreshList();
ColorSelector.OnColorChanged += SelectColor;
}
private void SelectColor(Color color)
{
_color = color;
Owner.SelectColor(color.ToHex());
RefreshList();
}
private void RefreshList()
@@ -86,7 +96,14 @@ namespace Content.Client.Crayon.UI
public void UpdateState(CrayonBoundUserInterfaceState state)
{
_selected = state.Selected;
ColorSelector.Visible = state.SelectableColor;
_color = state.Color;
if (ColorSelector.Visible)
{
ColorSelector.Color = state.Color;
}
RefreshList();
}

View File

@@ -18,6 +18,10 @@ namespace Content.Server.Crayon
[ViewVariables]
public Color Color { get; private set; }
[ViewVariables(VVAccess.ReadWrite)]
[DataField("selectableColor")]
public bool SelectableColor { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public int Charges { get; set; }

View File

@@ -33,6 +33,7 @@ public sealed class CrayonSystem : EntitySystem
base.Initialize();
SubscribeLocalEvent<CrayonComponent, ComponentInit>(OnCrayonInit);
SubscribeLocalEvent<CrayonComponent, CrayonSelectMessage>(OnCrayonBoundUI);
SubscribeLocalEvent<CrayonComponent, CrayonColorMessage>(OnCrayonBoundUIColor);
SubscribeLocalEvent<CrayonComponent, UseInHandEvent>(OnCrayonUse);
SubscribeLocalEvent<CrayonComponent, AfterInteractEvent>(OnCrayonAfterInteract);
SubscribeLocalEvent<CrayonComponent, DroppedEvent>(OnCrayonDropped);
@@ -67,7 +68,21 @@ public sealed class CrayonSystem : EntitySystem
return;
}
if(!_decals.TryAddDecal(component.SelectedState, args.ClickLocation.Offset(new Vector2(-0.5f,-0.5f)), out _, Color.FromName(component._color), cleanable: true))
Color color = Color.White;
if (Color.TryFromName(component._color, out var namedColor))
{
color = namedColor;
}
else
{
var hexColor = Color.TryFromHex(component._color);
if (hexColor != null)
{
color = (Color) hexColor;
}
}
if(!_decals.TryAddDecal(component.SelectedState, args.ClickLocation.Offset(new Vector2(-0.5f,-0.5f)), out _, color, cleanable: true))
return;
if (component.UseSound != null)
@@ -96,7 +111,7 @@ public sealed class CrayonSystem : EntitySystem
if (component.UserInterface?.SessionHasOpen(actor.PlayerSession) == true)
{
// Tell the user interface the selected stuff
component.UserInterface.SetState(new CrayonBoundUserInterfaceState(component.SelectedState, component.Color));
component.UserInterface.SetState(new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
}
args.Handled = true;
@@ -108,9 +123,33 @@ public sealed class CrayonSystem : EntitySystem
if (!_prototypeManager.TryIndex<DecalPrototype>(args.State, out var prototype) || !prototype.Tags.Contains("crayon")) return;
component.SelectedState = args.State;
Dirty(component);
}
private void OnCrayonBoundUIColor(EntityUid uid, CrayonComponent component, CrayonColorMessage args)
{
// you still need to ensure that the given color is a valid color
if (component.SelectableColor && args.Color != component._color)
{
if (Color.TryFromName(component._color, out var namedColor))
{
component._color = args.Color;
}
else
{
var hexColor = Color.TryFromHex(component._color);
if (hexColor != null)
{
component._color = args.Color;
}
}
Dirty(component);
}
}
private void OnCrayonInit(EntityUid uid, CrayonComponent component, ComponentInit args)
{
component.Charges = component.Capacity;

View File

@@ -31,6 +31,16 @@ namespace Content.Shared.Crayon
}
}
[Serializable, NetSerializable]
public sealed class CrayonColorMessage : BoundUserInterfaceMessage
{
public readonly string Color;
public CrayonColorMessage(string color)
{
Color = color;
}
}
[Serializable, NetSerializable]
public enum CrayonVisuals
{
@@ -58,11 +68,13 @@ namespace Content.Shared.Crayon
public sealed class CrayonBoundUserInterfaceState : BoundUserInterfaceState
{
public string Selected;
public bool SelectableColor;
public Color Color;
public CrayonBoundUserInterfaceState(string selected, Color color)
public CrayonBoundUserInterfaceState(string selected, bool selectableColor, Color color)
{
Selected = selected;
SelectableColor = selectableColor;
Color = color;
}
}

View File

@@ -28,6 +28,7 @@
contents:
- id: BoxHug
- id: RubberStampClown
- id: CrayonRainbow
- type: entity
noSpawn: true

View File

@@ -65,6 +65,29 @@
- Recyclable
- Trash
- type: entity
parent: Crayon
id: CrayonRainbow
name: rainbow crayon
components:
- type: Sprite
sprite: Objects/Fun/crayons.rsi
state: rainbow
- type: Item
sprite: Objects/Fun/crayons.rsi
size: 1
HeldPrefix: rainbow
- type: Crayon
color: Red
selectableColor: true
capacity: 30
- type: Tag
tags:
- Write
- Crayon
- Recyclable
- Trash
- type: entity
parent: Crayon
id: CrayonBlack