GUIStyle improvements & some simple UI graphics, networking bugfixes, separate Random class, some StyleCop cleanup
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class GUIComponentStyle
|
||||
{
|
||||
public readonly Vector4 Padding;
|
||||
|
||||
public readonly Color Color;
|
||||
|
||||
public readonly Color textColor;
|
||||
|
||||
public readonly Color HoverColor;
|
||||
public readonly Color SelectedColor;
|
||||
|
||||
public readonly Color OutlineColor;
|
||||
|
||||
public readonly List<Sprite> Sprites;
|
||||
|
||||
|
||||
public GUIComponentStyle(XElement element)
|
||||
{
|
||||
Sprites = new List<Sprite>();
|
||||
|
||||
Padding = ToolBox.GetAttributeVector4(element, "padding", Vector4.Zero);
|
||||
|
||||
Vector4 colorVector = ToolBox.GetAttributeVector4(element, "color", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
Color = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(element, "textcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
textColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(element, "hovercolor", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
HoverColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(element, "selectedcolor", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
SelectedColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(element, "outlinecolor", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
OutlineColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
switch (subElement.Name.ToString().ToLower())
|
||||
{
|
||||
case "sprite":
|
||||
Sprites.Add(new Sprite(subElement));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ namespace Subsurface
|
||||
t.SetData<Color>(
|
||||
new Color[] { Color.White });// fill the texture with white
|
||||
|
||||
style = new GUIStyle("Content/HUD/style.xml");
|
||||
style = new GUIStyle("Content/UI/style.xml");
|
||||
}
|
||||
|
||||
public static void DrawLine(SpriteBatch sb, Vector2 start, Vector2 end, Color clr, float depth = 0.0f)
|
||||
|
||||
+29
-18
@@ -7,6 +7,7 @@ namespace Subsurface
|
||||
class GUIButton : GUIComponent
|
||||
{
|
||||
protected GUITextBlock textBlock;
|
||||
protected GUIFrame frame;
|
||||
|
||||
public delegate bool OnClickedHandler(GUIButton button, object obj);
|
||||
public OnClickedHandler OnClicked;
|
||||
@@ -22,33 +23,41 @@ namespace Subsurface
|
||||
set { textBlock.Text = value; }
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, GUIStyle style, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, text, style.foreGroundColor, alignment, parent)
|
||||
public GUIButton(Rectangle rect, string text, GUIStyle style, GUIComponent parent = null)
|
||||
: this(rect, text, null, Alignment.Left, style, parent)
|
||||
{
|
||||
hoverColor = style.hoverColor;
|
||||
selectedColor = style.selectedColor;
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color color, GUIComponent parent = null)
|
||||
: this(rect, text, color, (Alignment.Left | Alignment.Top), parent)
|
||||
public GUIButton(Rectangle rect, string text, Alignment alignment, GUIStyle style, GUIComponent parent = null)
|
||||
: this(rect, text, null, alignment, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color color, Alignment alignment, GUIComponent parent = null)
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color? color, GUIStyle style, GUIComponent parent = null)
|
||||
: this(rect, text, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color? color, Alignment alignment, GUIStyle style, GUIComponent parent = null)
|
||||
:base (style)
|
||||
{
|
||||
this.rect = rect;
|
||||
this.color = color;
|
||||
if (color!=null) this.color = (Color)color;
|
||||
this.alignment = alignment;
|
||||
|
||||
Enabled = true;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), text, Color.Transparent, Color.Black, (Alignment.CenterX | Alignment.CenterY), this);
|
||||
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), style, this);
|
||||
if (style!=null) style.Apply(frame, this);
|
||||
|
||||
textBlock = new GUITextBlock(new Rectangle(0, 0, 0, 0), text,
|
||||
Color.Transparent, (this.style==null) ? Color.Black : this.style.textColor,
|
||||
Alignment.Center, style, this);
|
||||
}
|
||||
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (rect.Contains(PlayerInput.GetMouseState.Position) && Enabled && (MouseOn == this || IsParentOf(MouseOn)))
|
||||
@@ -74,15 +83,17 @@ namespace Subsurface
|
||||
state = ComponentState.None;
|
||||
}
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
frame.State = state;
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
|
||||
//Color currColor = color;
|
||||
//if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
//if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
|
||||
//spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
|
||||
//GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
|
||||
////spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
|
||||
|
||||
//GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
|
||||
@@ -9,15 +9,16 @@ namespace Subsurface
|
||||
{
|
||||
abstract class GUIComponent
|
||||
{
|
||||
|
||||
public static GUIComponent MouseOn;
|
||||
|
||||
protected static KeyboardDispatcher keyboardDispatcher;
|
||||
|
||||
public enum ComponentState { None, Hover, Selected};
|
||||
|
||||
protected Alignment alignment;
|
||||
|
||||
protected static KeyboardDispatcher keyboardDispatcher;
|
||||
|
||||
protected GUIComponentStyle style;
|
||||
|
||||
protected object userData;
|
||||
|
||||
protected Rectangle rect;
|
||||
@@ -33,7 +34,7 @@ namespace Subsurface
|
||||
|
||||
protected ComponentState state;
|
||||
|
||||
protected float alpha;
|
||||
//protected float alpha;
|
||||
|
||||
public GUIComponent Parent
|
||||
{
|
||||
@@ -68,6 +69,12 @@ namespace Subsurface
|
||||
}
|
||||
}
|
||||
|
||||
protected List<Sprite> sprites;
|
||||
//public Alignment SpriteAlignment { get; set; }
|
||||
//public bool RepeatSpriteX, RepeatSpriteY;
|
||||
|
||||
public Color OutlineColor { get; set; }
|
||||
|
||||
public ComponentState State
|
||||
{
|
||||
get { return state; }
|
||||
@@ -110,27 +117,32 @@ namespace Subsurface
|
||||
}
|
||||
|
||||
|
||||
public float Alpha
|
||||
{
|
||||
get
|
||||
{
|
||||
return alpha;
|
||||
}
|
||||
set
|
||||
{
|
||||
alpha = MathHelper.Clamp(value, 0.0f, 1.0f);
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
child.Alpha = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
//public float Alpha
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return alpha;
|
||||
// }
|
||||
// set
|
||||
// {
|
||||
// alpha = MathHelper.Clamp(value, 0.0f, 1.0f);
|
||||
// foreach (GUIComponent child in children)
|
||||
// {
|
||||
// child.Alpha = value;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
protected GUIComponent()
|
||||
protected GUIComponent(GUIStyle style)
|
||||
{
|
||||
alpha = 1.0f;
|
||||
//alpha = 1.0f;
|
||||
|
||||
OutlineColor = Color.Transparent;
|
||||
|
||||
sprites = new List<Sprite>();
|
||||
children = new List<GUIComponent>();
|
||||
|
||||
if (style!=null) style.Apply(this);
|
||||
}
|
||||
|
||||
public static void Init(GameWindow window)
|
||||
@@ -233,8 +245,21 @@ namespace Subsurface
|
||||
else
|
||||
{
|
||||
rect.Y += parentRect.Y + (int)padding.Y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
color = style.Color;
|
||||
hoverColor = style.HoverColor;
|
||||
selectedColor = style.SelectedColor;
|
||||
|
||||
padding = style.Padding;
|
||||
sprites = style.Sprites;
|
||||
|
||||
OutlineColor = style.OutlineColor;
|
||||
|
||||
this.style = style;
|
||||
}
|
||||
|
||||
public virtual void DrawChildren(SpriteBatch spriteBatch)
|
||||
|
||||
+41
-17
@@ -1,28 +1,29 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class GUIFrame : GUIComponent
|
||||
{
|
||||
public GUIFrame(Rectangle rect, Color color, Alignment alignment, GUIStyle style, GUIComponent parent = null)
|
||||
: this(rect, color, alignment, parent)
|
||||
{
|
||||
hoverColor = style.hoverColor;
|
||||
selectedColor = style.selectedColor;
|
||||
}
|
||||
|
||||
public GUIFrame(Rectangle rect, Color color, GUIComponent parent = null)
|
||||
: this(rect,color,(Alignment.Left | Alignment.Top), parent)
|
||||
{
|
||||
public GUIFrame(Rectangle rect, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, null, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIFrame(Rectangle rect, Color color, Alignment alignment, GUIComponent parent = null)
|
||||
|
||||
public GUIFrame(Rectangle rect, Color color, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIFrame(Rectangle rect, Color? color, Alignment alignment, GUIStyle style = null, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
this.color = color;
|
||||
if (color!=null) this.color = (Color)color;
|
||||
|
||||
if (parent != null)
|
||||
{
|
||||
@@ -32,17 +33,40 @@ namespace Subsurface
|
||||
{
|
||||
UpdateDimensions();
|
||||
}
|
||||
}
|
||||
|
||||
//if (style != null) ApplyStyle(style);
|
||||
}
|
||||
|
||||
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
|
||||
{
|
||||
base.Draw(spriteBatch);
|
||||
|
||||
Color newColor = color;
|
||||
if (state == ComponentState.Selected) newColor = selectedColor;
|
||||
if (state == ComponentState.Hover) newColor = hoverColor;
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, currColor * (currColor.A/255.0f), true);
|
||||
|
||||
if (sprites != null)
|
||||
{
|
||||
foreach (Sprite sprite in sprites)
|
||||
{
|
||||
Vector2 startPos = new Vector2(rect.X, rect.Y);
|
||||
Vector2 size = new Vector2(sprite.SourceRect.Width, sprite.SourceRect.Height);
|
||||
|
||||
if (sprite.size.X == 0.0f) size.X = rect.Width;
|
||||
if (sprite.size.Y == 0.0f) size.Y = rect.Height;
|
||||
|
||||
sprite.DrawTiled(spriteBatch, startPos, size, currColor * (currColor.A / 255.0f));
|
||||
}
|
||||
}
|
||||
|
||||
if (OutlineColor != Color.Transparent)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch, rect, OutlineColor * (OutlineColor.A/255.0f), false);
|
||||
}
|
||||
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, newColor * alpha, true);
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace Subsurface
|
||||
}
|
||||
|
||||
public GUIImage(Rectangle rect, Sprite sprite, Alignment alignment, GUIComponent parent = null)
|
||||
: base(null)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
@@ -54,7 +55,7 @@ namespace Subsurface
|
||||
|
||||
color = Color.White;
|
||||
|
||||
alpha = 1.0f;
|
||||
//alpha = 1.0f;
|
||||
|
||||
Scale = 1.0f;
|
||||
|
||||
@@ -75,7 +76,7 @@ namespace Subsurface
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
|
||||
spriteBatch.Draw(sprite.Texture, new Vector2(rect.X, rect.Y), sourceRect, currColor * alpha, 0.0f, Vector2.Zero,
|
||||
spriteBatch.Draw(sprite.Texture, new Vector2(rect.X, rect.Y), sourceRect, currColor * (currColor.A / 255.0f), 0.0f, Vector2.Zero,
|
||||
Scale, SpriteEffects.None, 0.0f);
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace Subsurface
|
||||
public CheckSelectedHandler CheckSelected;
|
||||
|
||||
private GUIScrollBar scrollBar;
|
||||
private GUIFrame frame;
|
||||
|
||||
private int totalSize;
|
||||
|
||||
@@ -73,36 +74,46 @@ namespace Subsurface
|
||||
}
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, GUIStyle style, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, style.foreGroundColor, alignment, parent)
|
||||
public GUIListBox(Rectangle rect, GUIStyle style, GUIComponent parent = null)
|
||||
: this(rect, style, Alignment.TopLeft, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, Color color, GUIComponent parent = null)
|
||||
: this(rect, color, (Alignment.Left | Alignment.Top), parent)
|
||||
public GUIListBox(Rectangle rect, GUIStyle style, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, null, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, Color? color, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, Color color, Alignment alignment, GUIComponent parent = null)
|
||||
public GUIListBox(Rectangle rect, Color? color, Alignment alignment, GUIStyle style = null, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
this.alignment = alignment;
|
||||
|
||||
this.color = color;
|
||||
if (color!=null) this.color = (Color)color;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
this.rect.Width -= 20;
|
||||
|
||||
scrollBar = new GUIScrollBar(
|
||||
new Rectangle(this.rect.X + this.rect.Width, this.rect.Y, 20, this.rect.Height), color, 1.0f);
|
||||
new Rectangle(this.rect.X + this.rect.Width, this.rect.Y, 20, this.rect.Height), color, 1.0f, style);
|
||||
|
||||
frame = new GUIFrame(Rectangle.Empty, style, this);
|
||||
if (style != null) style.Apply(frame, this);
|
||||
|
||||
UpdateScrollBarSize();
|
||||
|
||||
enabled = true;
|
||||
|
||||
scrollBarEnabled = true;
|
||||
|
||||
scrollBar.BarScroll = 0.0f;
|
||||
}
|
||||
|
||||
public void Select(object selection)
|
||||
@@ -126,6 +137,9 @@ namespace Subsurface
|
||||
|
||||
public void Select(int childIndex)
|
||||
{
|
||||
//children[0] is the GUIFrame, ignore it
|
||||
childIndex += 1;
|
||||
|
||||
if (childIndex >= children.Count || childIndex<0) return;
|
||||
|
||||
selected = children[childIndex];
|
||||
@@ -142,6 +156,7 @@ namespace Subsurface
|
||||
totalSize = 0;
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
if (child == frame) continue;
|
||||
totalSize += (scrollBar.IsHorizontal) ? child.Rect.Width : child.Rect.Height;
|
||||
totalSize += spacing;
|
||||
}
|
||||
@@ -161,8 +176,8 @@ namespace Subsurface
|
||||
float oldScroll = scrollBar.BarScroll;
|
||||
float oldSize = scrollBar.BarSize;
|
||||
UpdateScrollBarSize();
|
||||
|
||||
if (scrollBar.BarSize<1.0f && (oldSize>=1.0f || oldScroll==1.0f))
|
||||
|
||||
if (scrollBar.BarSize < 1.0f && oldScroll == 1.0f)
|
||||
{
|
||||
scrollBar.BarScroll = 1.0f;
|
||||
}
|
||||
@@ -199,7 +214,9 @@ namespace Subsurface
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
base.Draw(spriteBatch);
|
||||
GUI.DrawRectangle(spriteBatch, rect, color*alpha, true);
|
||||
|
||||
frame.Draw(spriteBatch);
|
||||
//GUI.DrawRectangle(spriteBatch, rect, color*alpha, true);
|
||||
|
||||
int x = rect.X, y = rect.Y;
|
||||
|
||||
@@ -219,6 +236,7 @@ namespace Subsurface
|
||||
for (int i = 0; i < children.Count; i++ )
|
||||
{
|
||||
GUIComponent child = children[i];
|
||||
if (child == frame) continue;
|
||||
|
||||
child.Rect = new Rectangle(child.Rect.X, y, child.Rect.Width, child.Rect.Height);
|
||||
y += child.Rect.Height + spacing;
|
||||
@@ -241,7 +259,7 @@ namespace Subsurface
|
||||
if (CheckSelected() != selected.UserData) selected = null;
|
||||
}
|
||||
}
|
||||
else if (enabled && (MouseOn==this || ( MouseOn!=null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.GetMouseState.Position))
|
||||
else if (enabled && (MouseOn == this || (MouseOn != null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.GetMouseState.Position))
|
||||
{
|
||||
child.State = ComponentState.Hover;
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
|
||||
@@ -23,9 +23,9 @@ namespace Subsurface
|
||||
|
||||
public GUIMessageBox(string header, string text, string[] buttons, Alignment textAlignment = (Alignment.Left | Alignment.Top))
|
||||
: base(new Rectangle(Game1.GraphicsWidth / 2 - DefaultWidth / 2, Game1.GraphicsHeight / 2 - DefaultHeight / 2, DefaultWidth, DefaultHeight),
|
||||
GUI.style.backGroundColor, Alignment.CenterX, GUI.style)
|
||||
null, Alignment.CenterX, GUI.style, null)
|
||||
{
|
||||
Padding = GUI.style.smallPadding;
|
||||
//Padding = GUI.style.smallPadding;
|
||||
|
||||
if (buttons == null || buttons.Length == 0)
|
||||
{
|
||||
@@ -33,14 +33,14 @@ namespace Subsurface
|
||||
return;
|
||||
}
|
||||
|
||||
new GUITextBlock(new Rectangle(0, 0, 0, 30), header, Color.Transparent, Color.White, textAlignment, this, true);
|
||||
new GUITextBlock(new Rectangle(0, 30, 0, DefaultHeight - 70), text, Color.Transparent, Color.White, textAlignment, this, true);
|
||||
new GUITextBlock(new Rectangle(0, 0, 0, 30), header, Color.Transparent, Color.White, textAlignment, GUI.style, this, true);
|
||||
new GUITextBlock(new Rectangle(0, 30, 0, DefaultHeight - 70), text, Color.Transparent, Color.White, textAlignment, GUI.style, this, true);
|
||||
|
||||
int x = 0;
|
||||
this.Buttons = new GUIButton[buttons.Length];
|
||||
for (int i = 0; i < buttons.Length; i++)
|
||||
{
|
||||
this.Buttons[i] = new GUIButton(new Rectangle(x, 0, 150, 30), buttons[i], GUI.style, Alignment.Left | Alignment.Bottom, this);
|
||||
this.Buttons[i] = new GUIButton(new Rectangle(x, 0, 150, 30), buttons[i], Alignment.Left | Alignment.Bottom, GUI.style, this);
|
||||
|
||||
x += this.Buttons[i].Rect.Width + 20;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace Subsurface
|
||||
}
|
||||
|
||||
public GUIProgressBar(Rectangle rect, Color color, float barSize, Alignment alignment, GUIComponent parent = null)
|
||||
: base(null)
|
||||
{
|
||||
this.rect = rect;
|
||||
this.color = color;
|
||||
@@ -49,7 +50,7 @@ namespace Subsurface
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), Color.White, this);
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), Color.White, null, this);
|
||||
|
||||
this.barSize = barSize;
|
||||
UpdateRect();
|
||||
@@ -70,7 +71,7 @@ namespace Subsurface
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, color*alpha, true);
|
||||
GUI.DrawRectangle(spriteBatch, rect, color * (color.A / 255.0f), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,22 +63,23 @@ namespace Subsurface
|
||||
{
|
||||
float oldBarSize = barSize;
|
||||
barSize = Math.Min(Math.Max(value, 0.0f), 1.0f);
|
||||
if (barSize!=oldBarSize) UpdateRect();
|
||||
if (barSize != oldBarSize) UpdateRect();
|
||||
}
|
||||
}
|
||||
|
||||
public GUIScrollBar(Rectangle rect, GUIStyle style, float barSize, GUIComponent parent = null)
|
||||
: this(rect, style.foreGroundColor, barSize, parent)
|
||||
: this(rect, null, barSize, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIScrollBar(Rectangle rect, Color color, float barSize, GUIComponent parent = null)
|
||||
: this(rect, color, barSize, (Alignment.Left | Alignment.Top), parent)
|
||||
public GUIScrollBar(Rectangle rect, Color? color, float barSize, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, color, barSize, Alignment.TopLeft, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public GUIScrollBar(Rectangle rect, Color color, float barSize, Alignment alignment, GUIComponent parent = null)
|
||||
public GUIScrollBar(Rectangle rect, Color? color, float barSize, Alignment alignment, GUIStyle style = null, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
//GetDimensions(parent);
|
||||
@@ -89,12 +90,13 @@ namespace Subsurface
|
||||
parent.AddChild(this);
|
||||
|
||||
isHorizontal = (rect.Width > rect.Height);
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), Color.White, this);
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), Color.White, style, this);
|
||||
//AddChild(frame);
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine(frame.rect);
|
||||
|
||||
bar = new GUIButton(new Rectangle(0, 0, 0, 0), "", color, this);
|
||||
bar = new GUIButton(new Rectangle(0, 0, 0, 0), "", color, style, this);
|
||||
|
||||
bar.OnPressed = SelectBar;
|
||||
//AddChild(bar);
|
||||
|
||||
|
||||
+39
-22
@@ -1,24 +1,19 @@
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class GUIStyle
|
||||
{
|
||||
public readonly Vector4 smallPadding;
|
||||
public readonly Vector4 largePadding;
|
||||
|
||||
public readonly Color backGroundColor;
|
||||
public readonly Color foreGroundColor;
|
||||
|
||||
public readonly Color textColor;
|
||||
|
||||
public readonly Color hoverColor;
|
||||
public readonly Color selectedColor;
|
||||
private Dictionary<string, GUIComponentStyle> componentStyles;
|
||||
|
||||
public GUIStyle(string file)
|
||||
{
|
||||
|
||||
componentStyles = new Dictionary<string, GUIComponentStyle>();
|
||||
|
||||
XDocument doc;
|
||||
try { doc = XDocument.Load(file); }
|
||||
catch (Exception e)
|
||||
@@ -27,24 +22,46 @@ namespace Subsurface
|
||||
return;
|
||||
}
|
||||
|
||||
smallPadding = ToolBox.GetAttributeVector4(doc.Root, "smallpadding", Vector4.Zero);
|
||||
largePadding = ToolBox.GetAttributeVector4(doc.Root, "largepadding", Vector4.Zero);
|
||||
//smallPadding = ToolBox.GetAttributeVector4(doc.Root, "smallpadding", Vector4.Zero);
|
||||
//largePadding = ToolBox.GetAttributeVector4(doc.Root, "largepadding", Vector4.Zero);
|
||||
|
||||
Vector4 colorVector = ToolBox.GetAttributeVector4(doc.Root, "backgroundcolor", new Vector4(0.0f,0.0f,0.0f,1.0f));
|
||||
backGroundColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
//Vector4 colorVector = ToolBox.GetAttributeVector4(doc.Root, "backgroundcolor", new Vector4(0.0f,0.0f,0.0f,1.0f));
|
||||
//backGroundColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(doc.Root, "foregroundcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
foreGroundColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
//colorVector = ToolBox.GetAttributeVector4(doc.Root, "foregroundcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
//foreGroundColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(doc.Root, "textcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
textColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
//colorVector = ToolBox.GetAttributeVector4(doc.Root, "textcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
//textColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(doc.Root, "hovercolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
hoverColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
//colorVector = ToolBox.GetAttributeVector4(doc.Root, "hovercolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
//hoverColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
colorVector = ToolBox.GetAttributeVector4(doc.Root, "selectedcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
selectedColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
//colorVector = ToolBox.GetAttributeVector4(doc.Root, "selectedcolor", new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
//selectedColor = new Color(colorVector.X, colorVector.Y, colorVector.Z, colorVector.W);
|
||||
|
||||
foreach (XElement subElement in doc.Root.Elements())
|
||||
{
|
||||
GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
|
||||
componentStyles.Add(subElement.Name.ToString().ToLower(), componentStyle);
|
||||
}
|
||||
}
|
||||
|
||||
public void Apply(GUIComponent targetComponent, GUIComponent parent = null)
|
||||
{
|
||||
GUIComponentStyle componentStyle = null;
|
||||
string name = (parent==null) ? targetComponent.GetType().Name.ToLower() : parent.GetType().Name.ToLower();
|
||||
componentStyles.TryGetValue(name, out componentStyle);
|
||||
|
||||
if (componentStyle==null)
|
||||
{
|
||||
DebugConsole.ThrowError("Couldn't find a GUI style for "+targetComponent.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
targetComponent.ApplyStyle(componentStyle);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,16 +60,24 @@ namespace Subsurface
|
||||
{
|
||||
get { return caretPos; }
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, GUIStyle style, Alignment alignment, Alignment textAlignment, GUIComponent parent = null, bool wrap = false)
|
||||
: this (rect, text, style.foreGroundColor, style.textColor, alignment, textAlignment, parent, wrap)
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, GUIStyle style, GUIComponent parent = null, bool wrap = false)
|
||||
: this(rect, text, style, Alignment.TopLeft, Alignment.TopLeft, parent, wrap)
|
||||
{
|
||||
hoverColor = style.hoverColor;
|
||||
selectedColor = style.selectedColor;
|
||||
//hoverColor = style.hoverColor;
|
||||
//selectedColor = style.selectedColor;
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color color, Color textColor, Alignment textAlignment = Alignment.Left, GUIComponent parent = null, bool wrap = false)
|
||||
: this(rect, text,color, textColor, (Alignment.Left | Alignment.Top), textAlignment, parent, wrap)
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, GUIStyle style, Alignment alignment = (Alignment.Left | Alignment.Top), Alignment textAlignment = (Alignment.Left | Alignment.Top), GUIComponent parent = null, bool wrap = false)
|
||||
: this (rect, text, null, null, alignment, textAlignment, style, parent, wrap)
|
||||
{
|
||||
//hoverColor = style.hoverColor;
|
||||
//selectedColor = style.selectedColor;
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color? color, Color? textColor, Alignment textAlignment = Alignment.Left, GUIStyle style = null, GUIComponent parent = null, bool wrap = false)
|
||||
: this(rect, text,color, textColor, (Alignment.Left | Alignment.Top), textAlignment, style, parent, wrap)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -80,13 +88,21 @@ namespace Subsurface
|
||||
SetTextPos();
|
||||
}
|
||||
|
||||
public override void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
base.ApplyStyle(style);
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color color, Color textColor, Alignment alignment, Alignment textAlignment = Alignment.Left, GUIComponent parent = null, bool wrap = false)
|
||||
textColor = style.textColor;
|
||||
}
|
||||
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color? color, Color? textColor, Alignment alignment, Alignment textAlignment = Alignment.Left, GUIStyle style = null, GUIComponent parent = null, bool wrap = false)
|
||||
:base (style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
this.color = color;
|
||||
this.textColor = textColor;
|
||||
if (color!=null) this.color = (Color)color;
|
||||
if (textColor!=null) this.textColor = (Color)textColor;
|
||||
this.text = text;
|
||||
|
||||
this.alignment = alignment;
|
||||
@@ -174,7 +190,7 @@ namespace Subsurface
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, rect, currColor*alpha, true);
|
||||
GUI.DrawRectangle(spriteBatch, rect, currColor*(currColor.A/255.0f), true);
|
||||
|
||||
if (TextGetter != null) text = TextGetter();
|
||||
|
||||
@@ -183,7 +199,7 @@ namespace Subsurface
|
||||
spriteBatch.DrawString(GUI.font,
|
||||
text,
|
||||
new Vector2(rect.X, rect.Y) + textPos,
|
||||
textColor * alpha,
|
||||
textColor * (textColor.A / 255.0f),
|
||||
0.0f, origin, 1.0f,
|
||||
SpriteEffects.None, 0.0f);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,12 @@ namespace Subsurface
|
||||
public delegate bool OnTextChangedHandler(GUITextBox textBox, string text);
|
||||
public OnTextChangedHandler OnTextChanged;
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public override Color Color
|
||||
{
|
||||
get { return color; }
|
||||
@@ -68,11 +74,24 @@ namespace Subsurface
|
||||
}
|
||||
}
|
||||
|
||||
public GUITextBox(Rectangle rect, Color color, Color textColor, Alignment alignment, Alignment textAlignment = Alignment.Left, GUIComponent parent = null)
|
||||
public GUITextBox(Rectangle rect, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, null, null, Alignment.Left, Alignment.Left, style, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public GUITextBox(Rectangle rect, Alignment alignment = Alignment.Left, GUIStyle style = null, GUIComponent parent = null)
|
||||
: this(rect, null, null, alignment, Alignment.Left, style, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public GUITextBox(Rectangle rect, Color? color, Color? textColor, Alignment alignment, Alignment textAlignment = Alignment.Left, GUIStyle style = null, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
this.color = color;
|
||||
if (color!=null) this.color = (Color)color;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
@@ -81,10 +100,11 @@ namespace Subsurface
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), "", color, textColor, textAlignment, this);
|
||||
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), "", color, textColor, textAlignment, null, this);
|
||||
textBlock.Padding = new Vector4(10.0f, 0.0f, 10.0f, 0.0f);
|
||||
if (style != null) style.Apply(textBlock, this);
|
||||
|
||||
_previousMouse = PlayerInput.GetMouseState;
|
||||
previousMouse = PlayerInput.GetMouseState;
|
||||
//SetTextPos();
|
||||
}
|
||||
|
||||
@@ -99,19 +119,18 @@ namespace Subsurface
|
||||
if (keyboardDispatcher.Subscriber == this) keyboardDispatcher.Subscriber = null;
|
||||
}
|
||||
|
||||
MouseState _previousMouse;
|
||||
MouseState previousMouse;
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
|
||||
caretTimer += deltaTime;
|
||||
caretVisible = ((caretTimer*1000.0f) % 1000) < 500;
|
||||
|
||||
if (rect.Contains(PlayerInput.GetMouseState.Position))
|
||||
{
|
||||
state = ComponentState.Hover;
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Select();
|
||||
}
|
||||
if (PlayerInput.LeftButtonClicked()) Select();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -120,7 +139,7 @@ namespace Subsurface
|
||||
|
||||
if (keyboardDispatcher.Subscriber == this)
|
||||
{
|
||||
Character.disableControls = true;
|
||||
Character.DisableControls = true;
|
||||
if (OnEnter != null && PlayerInput.KeyHit(Keys.Enter))
|
||||
{
|
||||
string input = Text;
|
||||
@@ -143,7 +162,7 @@ namespace Subsurface
|
||||
GUI.DrawLine(spriteBatch,
|
||||
new Vector2((int)caretPos.X + 2, caretPos.Y + 3),
|
||||
new Vector2((int)caretPos.X + 2, caretPos.Y + rect.Height - 3),
|
||||
textBlock.TextColor * alpha);
|
||||
textBlock.TextColor * (textBlock.TextColor.A / 255.0f));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,12 +15,13 @@ namespace Subsurface
|
||||
bool selected;
|
||||
|
||||
public GUITickBox(Rectangle rect, string label, Alignment alignment, GUIComponent parent)
|
||||
: base(null)
|
||||
{
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
box = new GUIFrame(new Rectangle(rect.X, rect.Y, 30, 30), Color.LightGray, this);
|
||||
text = new GUITextBlock(new Rectangle(rect.X + 40, rect.Y, 200, 30), label, Color.Transparent, Color.White, Alignment.Left | Alignment.CenterY, this);
|
||||
box = new GUIFrame(new Rectangle(rect.X, rect.Y, 30, 30), Color.LightGray, null, this);
|
||||
text = new GUITextBlock(new Rectangle(rect.X + 40, rect.Y, 200, 30), label, Color.Transparent, Color.White, Alignment.Left | Alignment.CenterY, null, this);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
@@ -53,7 +54,7 @@ namespace Subsurface
|
||||
|
||||
if (selected)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch, new Rectangle(box.Rect.X + 5, box.Rect.Y + 5, box.Rect.Width - 10, box.Rect.Height - 10), Color.Green * alpha, true);
|
||||
GUI.DrawRectangle(spriteBatch, new Rectangle(box.Rect.X + 5, box.Rect.Y + 5, box.Rect.Width - 10, box.Rect.Height - 10), Color.Green * (color.A / 255.0f), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user