// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using System;
namespace PixelCrushers
{
///
/// This struct is passed to listeners of the MessageSystem when a message is sent.
///
[Serializable]
public struct MessageArgs
{
///
/// Reference to the message sender (e.g., GameObject or possibly custom-defined string ID).
///
public object sender;
///
/// Reference to the message target (e.g., GameObject or possibly custom-defined string ID).
/// Typically null or blank string is interpreted as broadcast to all targets.
///
public object target;
public string message;
public string parameter;
public object[] values;
///
/// If true, the message arguments specify a target.
///
public bool hasTarget {
get
{
return !(target == null || string.IsNullOrEmpty(targetString));
}
}
///
/// True if the target value is a string or StringField.
///
public bool isTargetString {
get
{
var type = (target != null) ? target.GetType() : null;
return target != null && (type == typeof(string) || type == typeof(StringField));
}
}
///
/// If the target is a string or StringField, its value.
///
public string targetString {
get
{
if (target == null) return string.Empty;
var type = target.GetType();
return (type == typeof(string)) ? (string)target :
((type == typeof(StringField)) ? StringField.GetStringValue((StringField)target) : string.Empty);
}
}
public MessageArgs(object sender, object target, string message, string parameter, object[] values = null)
{
this.sender = sender;
this.target = target;
this.message = message;
this.parameter = parameter;
this.values = values;
}
public MessageArgs(object sender, string message, string parameter, object[] values = null)
{
this.sender = sender;
this.target = null;
this.message = message;
this.parameter = parameter;
this.values = values;
}
public bool Matches(string message, string parameter)
{
return string.Equals(message, this.message) && (string.IsNullOrEmpty(parameter) || string.Equals(parameter, this.parameter));
}
public bool Matches(StringField message, StringField parameter)
{
return string.Equals(message.value, this.message) && (StringField.IsNullOrEmpty(parameter) || string.Equals(parameter.value, this.parameter));
}
public bool Matches(StringField message, string parameter)
{
return string.Equals(message.value, this.message) && (string.IsNullOrEmpty(parameter) || string.Equals(parameter, this.parameter));
}
public bool Matches(string message, StringField parameter)
{
return string.Equals(message, this.message) && (StringField.IsNullOrEmpty(parameter) || string.Equals(parameter.value, this.parameter));
}
///
/// Returns true if the args' sender matches a required sender.
///
public bool IsRequiredSender(string requiredSender)
{
return string.IsNullOrEmpty(requiredSender) || string.Equals(requiredSender, GetSenderString());
}
///
/// Returns true if the args' target matches a required target.
///
public bool IsRequiredTarget(string requiredTarget)
{
return string.IsNullOrEmpty(requiredTarget) || string.Equals(requiredTarget, GetTargetString());
}
///
/// Returns the string name of the sender.
///
public string GetSenderString()
{
return GetObjectString(sender);
}
///
/// Returns the string name of the target.
///
public string GetTargetString()
{
return GetObjectString(target);
}
private string GetObjectString(object obj)
{
if (obj == null) return string.Empty;
var type = obj.GetType();
if (type == typeof(string)) return (string)obj;
if (type == typeof(StringField)) return StringField.GetStringValue((StringField)obj);
if (type == typeof(GameObject)) return (obj as GameObject).name;
if (type == typeof(Component)) return (obj as Component).name;
return obj.ToString();
}
public object firstValue
{
get
{
return (values != null && values.Length > 0) ? values[0] : null;
}
}
public int intValue
{
get
{
try
{
return (int)firstValue;
}
catch (Exception)
{
return 0;
}
}
}
}
}