Files
Vocaluxe/VocaluxeLib/Menu/CButton.cs
T

376 lines
14 KiB
C#

#region license
// /*
// This file is part of Vocaluxe.
//
// Vocaluxe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vocaluxe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
// */
#endregion
using System;
using System.Xml;
using VocaluxeLib.Draw;
namespace VocaluxeLib.Menu
{
struct SThemeButton
{
public string Name;
public string ColorName;
public string SelColorName;
public string TextureName;
public string SelTextureName;
}
public class CButton : IMenuElement
{
private SThemeButton _Theme;
private bool _ThemeLoaded;
private readonly int _PartyModeID;
public CTexture Texture;
public CTexture SelTexture;
public SRectF Rect;
public SColorF Color;
public SColorF SelColor;
private bool _IsSelText;
public readonly CText Text;
private readonly CText _SelText;
private bool _Reflection;
private float _ReflectionSpace;
private float _ReflectionHeight;
private bool _SelReflection;
private float _SelReflectionSpace;
private float _SelReflectionHeight;
public bool Pressed;
public bool EditMode
{
get { return Text.EditMode; }
set
{
Text.EditMode = value;
_SelText.EditMode = value;
}
}
private bool _Selected;
public bool Selected
{
get { return _Selected; }
set
{
_Selected = value;
Text.Selected = value;
}
}
public bool Visible = true;
public bool Enabled = true;
public string GetThemeName()
{
return _Theme.Name;
}
public CButton(int partyModeID)
{
_PartyModeID = partyModeID;
Text = new CText(_PartyModeID);
_SelText = new CText(_PartyModeID);
Selected = false;
EditMode = false;
}
public CButton(CButton button)
{
_PartyModeID = button._PartyModeID;
_Theme = new SThemeButton
{
ColorName = button._Theme.ColorName,
SelColorName = button._Theme.SelColorName,
TextureName = button._Theme.TextureName,
SelTextureName = button._Theme.SelTextureName
};
Rect = new SRectF(button.Rect);
Color = new SColorF(button.Color);
SelColor = new SColorF(button.Color);
Texture = button.Texture;
SelTexture = button.SelTexture;
Text = new CText(button.Text);
_SelText = new CText(button._SelText);
Selected = false;
EditMode = false;
Enabled = button.Enabled;
_Reflection = button._Reflection;
_ReflectionHeight = button._ReflectionHeight;
_ReflectionSpace = button._ReflectionSpace;
_SelReflection = button._SelReflection;
_SelReflectionHeight = button._SelReflectionHeight;
_SelReflectionSpace = button._SelReflectionSpace;
}
public bool LoadTheme(string xmlPath, string elementName, CXMLReader xmlReader, int skinIndex)
{
string item = xmlPath + "/" + elementName;
_ThemeLoaded = true;
_ThemeLoaded &= xmlReader.GetValue(item + "/Skin", out _Theme.TextureName, String.Empty);
_ThemeLoaded &= xmlReader.GetValue(item + "/SkinSelected", out _Theme.SelTextureName, String.Empty);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/X", ref Rect.X);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/Y", ref Rect.Y);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/Z", ref Rect.Z);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/W", ref Rect.W);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/H", ref Rect.H);
if (xmlReader.GetValue(item + "/Color", out _Theme.ColorName, String.Empty))
_ThemeLoaded &= CBase.Theme.GetColor(_Theme.ColorName, skinIndex, out Color);
else
{
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/R", ref Color.R);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/G", ref Color.G);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/B", ref Color.B);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/A", ref Color.A);
}
if (xmlReader.GetValue(item + "/SColor", out _Theme.SelColorName, String.Empty))
_ThemeLoaded &= CBase.Theme.GetColor(_Theme.SelColorName, skinIndex, out SelColor);
else
{
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SR", ref SelColor.R);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SG", ref SelColor.G);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SB", ref SelColor.B);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SA", ref SelColor.A);
}
_ThemeLoaded &= Text.LoadTheme(item, "Text", xmlReader, skinIndex, true);
Text.Z = Rect.Z;
if (xmlReader.ItemExists(item + "/SText"))
{
_IsSelText = true;
_ThemeLoaded &= _SelText.LoadTheme(item, "SText", xmlReader, skinIndex, true);
_SelText.Z = Rect.Z;
}
//Reflections
if (xmlReader.ItemExists(item + "/Reflection"))
{
_Reflection = true;
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/Reflection/Space", ref _ReflectionSpace);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/Reflection/Height", ref _ReflectionHeight);
}
else
_Reflection = false;
if (xmlReader.ItemExists(item + "/SReflection"))
{
_SelReflection = true;
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SReflection/Space", ref _SelReflectionSpace);
_ThemeLoaded &= xmlReader.TryGetFloatValue(item + "/SReflection/Height", ref _SelReflectionHeight);
}
else
_SelReflection = false;
if (_ThemeLoaded)
{
_Theme.Name = elementName;
LoadTextures();
}
return _ThemeLoaded;
}
public bool SaveTheme(XmlWriter writer)
{
if (_ThemeLoaded)
{
writer.WriteStartElement(_Theme.Name);
writer.WriteComment("<Skin>: Texture name");
writer.WriteElementString("Skin", _Theme.TextureName);
writer.WriteComment("<SkinSelected>: Texture name for selected button");
writer.WriteElementString("SkinSelected", _Theme.SelTextureName);
writer.WriteComment("<X>, <Y>, <Z>, <W>, <H>: Button position, width and height");
writer.WriteElementString("X", Rect.X.ToString("#0"));
writer.WriteElementString("Y", Rect.Y.ToString("#0"));
writer.WriteElementString("Z", Rect.Z.ToString("#0.00"));
writer.WriteElementString("W", Rect.W.ToString("#0"));
writer.WriteElementString("H", Rect.H.ToString("#0"));
writer.WriteComment("<Color>: Button color from ColorScheme (high priority)");
writer.WriteComment("or <R>, <G>, <B>, <A> (lower priority)");
if (_Theme.ColorName != "")
writer.WriteElementString("Color", _Theme.ColorName);
else
{
writer.WriteElementString("R", Color.R.ToString("#0.00"));
writer.WriteElementString("G", Color.G.ToString("#0.00"));
writer.WriteElementString("B", Color.B.ToString("#0.00"));
writer.WriteElementString("A", Color.A.ToString("#0.00"));
}
writer.WriteComment("<SColor>: Selected button color from ColorScheme (high priority)");
writer.WriteComment("or <SR>, <SG>, <SB>, <SA> (lower priority)");
if (_Theme.SelColorName != "")
writer.WriteElementString("SColor", _Theme.SelColorName);
else
{
writer.WriteElementString("SR", SelColor.R.ToString("#0.00"));
writer.WriteElementString("SG", SelColor.G.ToString("#0.00"));
writer.WriteElementString("SB", SelColor.B.ToString("#0.00"));
writer.WriteElementString("SA", SelColor.A.ToString("#0.00"));
}
Text.SaveTheme(writer);
if (_IsSelText)
_SelText.SaveTheme(writer);
writer.WriteComment("<Reflection> If exists:");
writer.WriteComment(" <Space>: Reflection Space");
writer.WriteComment(" <Height>: Reflection Height");
if (_Reflection)
{
writer.WriteStartElement("Reflection");
writer.WriteElementString("Space", _ReflectionSpace.ToString("#0"));
writer.WriteElementString("Height", _ReflectionHeight.ToString("#0"));
writer.WriteEndElement();
}
writer.WriteComment("<SReflection> If exists:");
writer.WriteComment(" <Space>: Reflection Space of selected button");
writer.WriteComment(" <Height>: Reflection Height of selected button");
if (_SelReflection)
{
writer.WriteStartElement("SReflection");
writer.WriteElementString("Space", _ReflectionSpace.ToString("#0"));
writer.WriteElementString("Height", _ReflectionHeight.ToString("#0"));
writer.WriteEndElement();
}
writer.WriteEndElement();
return true;
}
return false;
}
public void Draw(bool forceDraw = false)
{
if (!Visible && CBase.Settings.GetGameState() != EGameState.EditTheme && !forceDraw)
return;
CTexture texture;
if (!Selected && !Pressed || !Enabled)
{
texture = Texture ?? CBase.Theme.GetSkinTexture(_Theme.TextureName, _PartyModeID);
CBase.Drawing.DrawTexture(texture, Rect, Color);
if (_Reflection)
{
CBase.Drawing.DrawTextureReflection(texture, Rect, Color, Rect, _ReflectionSpace, _ReflectionHeight);
Text.DrawRelative(Rect.X, Rect.Y, _ReflectionHeight, _ReflectionSpace, Rect.H);
}
else
Text.DrawRelative(Rect.X, Rect.Y);
}
else if (!_IsSelText)
{
texture = Texture ?? CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID);
CBase.Drawing.DrawTexture(texture, Rect, SelColor);
if (_Reflection)
{
CBase.Drawing.DrawTextureReflection(texture, Rect, SelColor, Rect, _ReflectionSpace, _ReflectionHeight);
Text.DrawRelative(Rect.X, Rect.Y, _ReflectionHeight, _ReflectionSpace, Rect.H);
}
else
Text.DrawRelative(Rect.X, Rect.Y);
}
else if (_IsSelText)
{
texture = SelTexture ?? CBase.Theme.GetSkinTexture(_Theme.SelTextureName, _PartyModeID);
CBase.Drawing.DrawTexture(texture, Rect, SelColor);
if (_Reflection)
{
CBase.Drawing.DrawTextureReflection(texture, Rect, SelColor, Rect, _ReflectionSpace, _ReflectionHeight);
_SelText.DrawRelative(Rect.X, Rect.Y, _ReflectionHeight, _ReflectionSpace, Rect.H);
}
else
_SelText.DrawRelative(Rect.X, Rect.Y);
}
}
public void ProcessMouseMove(int x, int y)
{
Selected = CHelper.IsInBounds(Rect, x, y);
}
public void UnloadTextures()
{
Text.UnloadTextures();
}
public void LoadTextures()
{
Text.LoadTextures();
if (_Theme.ColorName != "")
Color = CBase.Theme.GetColor(_Theme.ColorName, _PartyModeID);
if (_Theme.SelColorName != "")
SelColor = CBase.Theme.GetColor(_Theme.SelColorName, _PartyModeID);
}
public void ReloadTextures()
{
UnloadTextures();
LoadTextures();
}
#region ThemeEdit
public void MoveElement(int stepX, int stepY)
{
Rect.X += stepX;
Rect.Y += stepY;
}
public void ResizeElement(int stepW, int stepH)
{
Rect.W += stepW;
if (Rect.W <= 0)
Rect.W = 1;
Rect.H += stepH;
if (Rect.H <= 0)
Rect.H = 1;
}
#endregion ThemeEdit
}
}