Editor/ChitChat/ControlWidgets/SpeakActionControlWidget.cs
using Sandbox;
using Editor;

namespace ChitChat.Editor;

[CustomEditor(typeof(SpeakAction))]
public class SpeakActionControlWidget : ControlWidget
{
	public override bool SupportsMultiEdit => false;

	public SpeakActionControlWidget(SerializedProperty property) : base(property)
	{
		Layout = Layout.Row();
		Layout.Spacing = 8;
		Layout.Margin = 5;

		MinimumSize = new Vector2(600, 100);

		SetStyles("background-color: #424242; border-radius: 5px;");

		Rebuild();
	}

	public void Rebuild()
	{
		SpeakAction speak = SerializedProperty.GetValue<DialogueActionBase>() as SpeakAction;
		SerializedObject serializedObject = speak.GetSerialized();

		if (serializedObject == null)
		{
			Log.Error("Couldn't get " + nameof(SpeakAction) + " as serializedObject!");
			return;
		}

		Widget characterTexts = new Widget(this);
		characterTexts.Layout = Layout.Column();
		characterTexts.Layout.Alignment = TextFlag.LeftTop;


		if (serializedObject.TryGetProperty(nameof(SpeakAction.Character), out SerializedProperty characterProp))
		{
			DialogueCharacter character = characterProp.GetValue<DialogueCharacter>();
			int index = SerializedProperty.GetValue<SpeakAction>().CharacterIndex;

			TextureWidget characterPicturePreview = new TextureWidget()
			{
				Parent = this,
				FixedSize = new Vector2(100, 100),
				RetainAspectRatio = true
			};

			if (character.IsValid() && character.CharacterPictures?.Count > 0 && index < character.CharacterPictures.Count && index > -1)
			{
				characterPicturePreview.Texture = character.CharacterPictures[index].Picture;
			}
			else
				characterPicturePreview.Texture = null;

			Layout.Add(characterPicturePreview);

			//Name
			Label nameLabel = new Label(character.IsValid() ? character.Name : "No character selected", characterTexts);
			nameLabel.SetStyles("font-size: 15px; font-weight: bold;");
			nameLabel.MaximumHeight = 18;

			characterTexts.Layout.Add(nameLabel);
			characterTexts.Layout.AddSpacingCell(3);
			characterTexts.Layout.AddSpacingCell(3);
		}
		else
			Log.Error(nameof(SpeakAction) + " could not get character data.");

		//Draw Text
		if (serializedObject.TryGetProperty(nameof(SpeakAction.Text), out SerializedProperty textProp))
		{
			string text = textProp.GetValue<string>();
			if (string.IsNullOrEmpty(text))
				text = "Empty...";

			Widget textBackground = new Widget(characterTexts);
			textBackground.Name = "Text Background";
			textBackground.HorizontalSizeMode = SizeMode.Flexible;
			textBackground.FixedHeight = 70;
			textBackground.SetStyles("background-color: #303030; border-radius: 5px;");

			Label textLabel = new Label(text, characterTexts) { ContentMargins = new Sandbox.UI.Margin(0, 0, 5, 0) };
			textLabel.Alignment = TextFlag.LeftTop;
			textLabel.ContentMargins = 4;
			textLabel.WordWrap = true;
			textBackground.Layout = Layout.Column();
			textBackground.Layout.Add(textLabel);

			characterTexts.Layout.Add(textBackground);
		}
		else
			Log.Error(nameof(SpeakAction) + " could not get Text data.");

		Layout.Add(characterTexts);
	}

	protected override void OnPaint() { }
}