/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using System.Collections.Generic;
using UnityEngine;
namespace Yarn.Unity
{
///
/// Represents a collection of marker names and colours.
///
///
/// This is intended to be used with the , and
/// also be a sample of using the markup system.
///
[CreateAssetMenu(fileName = "NewPalette", menuName = "Yarn Spinner/Markup Palette", order = 102)]
public sealed class MarkupPalette : ScriptableObject
{
///
/// Contains information describing the formatting style of text within
/// a named marker.
///
[System.Serializable]
public struct BasicMarker
{
///
/// The name of the marker which can be used in text to indicate
/// specific formatting.
///
public string Marker;
///
/// Indicates whethere or not the text associated with this marker should have a custom colour.
///
public bool CustomColor;
///
/// The color to use for text associated with this marker.
///
public Color Color;
///
/// Indicates whether the text associated with this marker should be
/// bolded.
///
public bool Boldened;
///
/// Indicates whether the text associated with this marker should be
/// italicized.
///
public bool Italicised;
///
/// Indicates whether the text associated with this marker should be
/// underlined.
///
public bool Underlined;
///
/// Indicates whether the text associated with this marker should
/// have a strikethrough effect.
///
public bool Strikedthrough;
}
[System.Serializable]
public struct CustomMarker
{
public string Marker;
public string Start;
public string End;
public int MarkerOffset;
public int TotalVisibleCharacterCount;
}
///
/// A list containing all the color markers defined in this palette.
///
[UnityEngine.Serialization.FormerlySerializedAs("ColourMarkers")]
public List BasicMarkers = new List();
public List CustomMarkers = new List();
///
/// Determines the colour for a particular marker inside this palette.
///
/// The marker you want to get a colour
/// for.
/// The colour of the marker, or if it doesn't exist in the .
/// if the marker exists within this
/// palette; otherwise.
public bool ColorForMarker(string Marker, out Color colour)
{
foreach (var item in BasicMarkers)
{
if (item.Marker == Marker)
{
colour = item.Color;
return true;
}
}
colour = Color.black;
return false;
}
public bool PaletteForMarker(string markerName, out CustomMarker palette)
{
// we first check if we have a marker of that name in the basic markers
foreach (var item in BasicMarkers)
{
if (item.Marker == markerName)
{
System.Text.StringBuilder front = new();
System.Text.StringBuilder back = new();
// do we have a custom colour set?
if (item.CustomColor)
{
front.AppendFormat("", ColorUtility.ToHtmlStringRGBA(item.Color));
back.Append("");
}
// do we need to bold it?
if (item.Boldened)
{
front.Append("");
back.Append("");
}
// do we need to italicise it?
if (item.Italicised)
{
front.Append("");
back.Append("");
}
// do we need to underline it?
if (item.Underlined)
{
front.Append("");
back.Append("");
}
// do we need to strikethrough it?
if (item.Strikedthrough)
{
front.Append("");
back.Append("");
}
palette = new CustomMarker()
{
Marker = item.Marker,
Start = front.ToString(),
End = back.ToString(),
MarkerOffset = 0,
TotalVisibleCharacterCount = 0,
};
return true;
}
}
// we now check if we have one in the format markers
foreach (var item in CustomMarkers)
{
if (item.Marker == markerName)
{
palette = item;
return true;
}
}
// we don't have anything for this marker
// so we return false and a default marker
palette = new();
return false;
}
}
}