我在XNA中使用大量颜色。所有这些颜色都来自Theme类,其中包含当前主题的可能颜色。
这些颜色在SpriteBatch.Draw()中用作叠加层,使用白色纹理(1,1 px)。
用户可以选择在运行时更改此主题,然后将Theme类的颜色重新分配给。
但是,由于Color是一个结构体,我为其指定的所有内容都会获得此对象的新副本。
问题是如果我更改原始颜色,则不会反映更改。 有什么方法可以通过参考传递颜色吗?还是另一种解决方案?
更多信息:
我的主题课:
/// <summary>
/// Theme enumerator
/// </summary>
public enum Theme
{
Standard = 1, Blue, Dark
}
public class ThemeColor
{
//TODO: Refactor this out
private readonly SmartHome _gameRef;
public Color FBlue;
public Color FDarkGrey;
public Color FGreen;
public Color FLightBlue;
public Color FLightGrey;
public Color FOrange;
public Color FWhite;
public ThemeColor(SmartHome gameref)
{
Dal.Instance.Setting.OnChangedSetting += ChangedColor;
FBlue = new Color(0, 114, 186);
FDarkGrey = new Color(107, 108, 110);
FGreen = new Color(140, 189, 70);
FLightBlue = new Color(0, 179, 231);
FLightGrey = new Color(233, 233, 234);
FOrange = new Color(241, 142, 0);
FWhite = Color.White;
}
/// <summary>
/// Called if a setting is changed, checks if this setting is a theme setting,
/// if so, change the colors
/// </summary>
/// <param name="setting">Changed setting</param>
public void ChangedColor(Setting setting)
{
if (setting.Key == "Theme")
{
switch ((Theme)Int32.Parse(setting.Value))
{
case Theme.Standard:
FBlue = new Color(0, 114, 186);
FDarkGrey = new Color(107, 108, 110);
FGreen = new Color(140, 189, 70);
FLightBlue = new Color(0, 179, 231);
FLightGrey = new Color(233, 233, 234);
FOrange = new Color(241, 142, 0);
FWhite = Color.White;
break;
case Theme.Blue:
FBlue = new Color(0, 114, 186);
FDarkGrey = new Color(107, 108, 110);
FGreen = new Color(0, 179, 231);
FLightBlue = new Color(241, 142, 0);
FLightGrey = new Color(233, 233, 234);
FOrange = new Color(140, 189, 70);
FWhite = Color.White;
break;
case Theme.Dark:
FBlue = new Color(233, 233, 234);
FDarkGrey = new Color(0, 179, 231);
FGreen = new Color(107, 108, 110);
FLightBlue = new Color(0, 114, 186);
FLightGrey = new Color(140, 189, 70);
FOrange = new Color(241, 142, 0);
FWhite = Color.White;
break;
}
}
}
}
控件具有属性Color,并设置如下:
control ctrl.Color = ThemeColor.FGreen
然后在控件类中:
SpriteBatch.Draw(x,x,Color);
答案 0 :(得分:1)
您可以在类中包含应用程序主题颜色,并在每次将其传递到某个位置时访问该结构的类副本。当然,结构和颜色本身将保持一个值类型,但重要的是访问相同的颜色副本,据我所知。
public static class ColourContainer
{
public static Color ThemeColour { get; }
public static void UpdateTheme(Color newColour)
{
// Update, etc.
}
}
如何将这些传递给方法可能无法改变(除非代码是你的),但颜色的来源是重要的。只要你每次从容器中获取它并在主题更改时更新容器,变化就会在整个系统中波动。
答案 1 :(得分:0)
在你的控件中以这种方式改变你的Color属性......
class Control
{
public int UseThemeColor = -1;
public Color Color {
get { return (UseThemeColor==-1 ? _color : ThemeColor.GetByIndex(UseThemColor); }
set { _color = value; UseThemColor = -1; }
}
}