Renamed project folders from Subsurface to Barotrauma
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class UISprite
|
||||
{
|
||||
public Sprite Sprite
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public bool Tile
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public bool Slice
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Rectangle[] Slices
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public bool MaintainAspectRatio
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public UISprite(Sprite sprite, bool tile, bool maintainAspectRatio)
|
||||
{
|
||||
Sprite = sprite;
|
||||
Tile = tile;
|
||||
MaintainAspectRatio = maintainAspectRatio;
|
||||
}
|
||||
}
|
||||
|
||||
public 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 Dictionary<GUIComponent.ComponentState, List<UISprite>> Sprites;
|
||||
|
||||
public Dictionary<string, GUIComponentStyle> ChildStyles;
|
||||
|
||||
public GUIComponentStyle(XElement element)
|
||||
{
|
||||
Sprites = new Dictionary<GUIComponent.ComponentState, List<UISprite>>();
|
||||
foreach (GUIComponent.ComponentState state in Enum.GetValues(typeof(GUIComponent.ComponentState)))
|
||||
{
|
||||
Sprites[state] = new List<UISprite>();
|
||||
}
|
||||
|
||||
ChildStyles = new Dictionary<string, GUIComponentStyle>();
|
||||
|
||||
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().ToLowerInvariant())
|
||||
{
|
||||
case "sprite":
|
||||
Sprite sprite = new Sprite(subElement);
|
||||
bool maintainAspect = ToolBox.GetAttributeBool(subElement, "maintainaspectratio",false);
|
||||
bool tile = ToolBox.GetAttributeBool(subElement, "tile", true);
|
||||
|
||||
string stateStr = ToolBox.GetAttributeString(subElement, "state", "None");
|
||||
GUIComponent.ComponentState spriteState = GUIComponent.ComponentState.None;
|
||||
Enum.TryParse(stateStr, out spriteState);
|
||||
|
||||
UISprite newSprite = new UISprite(sprite, tile, maintainAspect);
|
||||
|
||||
Vector4 sliceVec = ToolBox.GetAttributeVector4(subElement, "slice", Vector4.Zero);
|
||||
if (sliceVec != Vector4.Zero)
|
||||
{
|
||||
Rectangle slice = new Rectangle((int)sliceVec.X, (int)sliceVec.Y, (int)(sliceVec.Z - sliceVec.X), (int)(sliceVec.W - sliceVec.Y));
|
||||
|
||||
newSprite.Slice = true;
|
||||
|
||||
newSprite.Slices = new Rectangle[9];
|
||||
|
||||
//top-left
|
||||
newSprite.Slices[0] = new Rectangle(newSprite.Sprite.SourceRect.Location, slice.Location - newSprite.Sprite.SourceRect.Location);
|
||||
//top-mid
|
||||
newSprite.Slices[1] = new Rectangle(slice.Location.X, newSprite.Slices[0].Y, slice.Width, newSprite.Slices[0].Height);
|
||||
//top-right
|
||||
newSprite.Slices[2] = new Rectangle(slice.Right, newSprite.Slices[0].Y, newSprite.Sprite.SourceRect.Right - slice.Right, newSprite.Slices[0].Height);
|
||||
|
||||
//mid-left
|
||||
newSprite.Slices[3] = new Rectangle(newSprite.Slices[0].X, slice.Y, newSprite.Slices[0].Width, slice.Height);
|
||||
//center
|
||||
newSprite.Slices[4] = slice;
|
||||
//mid-right
|
||||
newSprite.Slices[5] = new Rectangle(newSprite.Slices[2].X, slice.Y, newSprite.Slices[2].Width, slice.Height);
|
||||
|
||||
//bottom-left
|
||||
newSprite.Slices[6] = new Rectangle(newSprite.Slices[0].X, slice.Bottom, newSprite.Slices[0].Width, newSprite.Sprite.SourceRect.Bottom - slice.Bottom);
|
||||
//bottom-mid
|
||||
newSprite.Slices[7] = new Rectangle(newSprite.Slices[1].X, slice.Bottom, newSprite.Slices[1].Width, newSprite.Sprite.SourceRect.Bottom - slice.Bottom);
|
||||
//bottom-right
|
||||
newSprite.Slices[8] = new Rectangle(newSprite.Slices[2].X, slice.Bottom, newSprite.Slices[2].Width, newSprite.Sprite.SourceRect.Bottom - slice.Bottom);
|
||||
}
|
||||
|
||||
Sprites[spriteState].Add(newSprite);
|
||||
break;
|
||||
default:
|
||||
ChildStyles.Add(subElement.Name.ToString().ToLowerInvariant(), new GUIComponentStyle(subElement));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,615 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
[Flags]
|
||||
public enum Alignment
|
||||
{
|
||||
CenterX = 1, Left = 2, Right = 4, CenterY = 8, Top = 16, Bottom = 32,
|
||||
TopLeft = (Top | Left), TopCenter = (CenterX | Top), TopRight = (Top | Right),
|
||||
CenterLeft = (Left | CenterY), Center = (CenterX | CenterY), CenterRight = (Right | CenterY),
|
||||
BottomLeft = (Bottom | Left), BottomCenter = (CenterX | Bottom), BottomRight = (Bottom | Right),
|
||||
}
|
||||
|
||||
public enum GUISoundType
|
||||
{
|
||||
Message,
|
||||
RadioMessage,
|
||||
DeadMessage,
|
||||
Click,
|
||||
Inventory,
|
||||
}
|
||||
|
||||
public class GUI
|
||||
{
|
||||
|
||||
public static GUIStyle Style;
|
||||
|
||||
private static Texture2D t;
|
||||
public static ScalableFont Font, SmallFont, LargeFont;
|
||||
|
||||
private static Sprite cursor;
|
||||
|
||||
private static GraphicsDevice graphicsDevice;
|
||||
public static GraphicsDevice GraphicsDevice
|
||||
{
|
||||
get
|
||||
{
|
||||
return graphicsDevice;
|
||||
}
|
||||
set
|
||||
{
|
||||
graphicsDevice = value;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<GUIMessage> messages = new List<GUIMessage>();
|
||||
|
||||
private static Sound[] sounds;
|
||||
|
||||
private static bool pauseMenuOpen, settingsMenuOpen;
|
||||
private static GUIFrame pauseMenu;
|
||||
|
||||
public static Color ScreenOverlayColor;
|
||||
|
||||
private static Sprite submarineIcon, arrow;
|
||||
|
||||
public static Sprite SubmarineIcon
|
||||
{
|
||||
get { return submarineIcon; }
|
||||
}
|
||||
|
||||
public static Sprite SpeechBubbleIcon
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public static Sprite Arrow
|
||||
{
|
||||
get { return arrow; }
|
||||
}
|
||||
|
||||
public static bool DisableHUD;
|
||||
|
||||
public static void Init(ContentManager content)
|
||||
{
|
||||
Font = new ScalableFont("Content/Exo2-Medium.otf", 14, graphicsDevice);
|
||||
SmallFont = new ScalableFont("Content/Exo2-Light.otf", 12, graphicsDevice);
|
||||
LargeFont = new ScalableFont("Content/Code Pro Bold.otf", 22, graphicsDevice);
|
||||
|
||||
cursor = new Sprite("Content/UI/cursor.png", Vector2.Zero);
|
||||
|
||||
Style = new GUIStyle("Content/UI/style.xml");
|
||||
}
|
||||
|
||||
public static bool PauseMenuOpen
|
||||
{
|
||||
get { return pauseMenuOpen; }
|
||||
}
|
||||
|
||||
public static bool SettingsMenuOpen
|
||||
{
|
||||
get { return settingsMenuOpen; }
|
||||
}
|
||||
|
||||
public static void LoadContent(bool loadSounds = true)
|
||||
{
|
||||
if (loadSounds)
|
||||
{
|
||||
sounds = new Sound[Enum.GetValues(typeof(GUISoundType)).Length];
|
||||
sounds[(int)GUISoundType.Message] = Sound.Load("Content/Sounds/UI/UImsg.ogg", false);
|
||||
sounds[(int)GUISoundType.RadioMessage] = Sound.Load("Content/Sounds/UI/radiomsg.ogg", false);
|
||||
sounds[(int)GUISoundType.DeadMessage] = Sound.Load("Content/Sounds/UI/deadmsg.ogg", false);
|
||||
sounds[(int)GUISoundType.Click] = Sound.Load("Content/Sounds/UI/beep-shinymetal.ogg", false);
|
||||
|
||||
sounds[(int)GUISoundType.Inventory] = Sound.Load("Content/Sounds/pickItem.ogg", false);
|
||||
|
||||
}
|
||||
|
||||
// create 1x1 texture for line drawing
|
||||
t = new Texture2D(graphicsDevice, 1, 1);
|
||||
t.SetData(new Color[] { Color.White });// fill the texture with white
|
||||
|
||||
submarineIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 192, 64, 64), null);
|
||||
submarineIcon.Origin = submarineIcon.size / 2;
|
||||
|
||||
arrow = new Sprite("Content/UI/uiIcons.png", new Rectangle(80, 240, 16, 16), null);
|
||||
arrow.Origin = arrow.size / 2;
|
||||
|
||||
SpeechBubbleIcon = new Sprite("Content/UI/uiIcons.png", new Rectangle(0, 129, 65, 61), null);
|
||||
SpeechBubbleIcon.Origin = SpeechBubbleIcon.size / 2;
|
||||
}
|
||||
|
||||
public static void TogglePauseMenu()
|
||||
{
|
||||
if (Screen.Selected == GameMain.MainMenuScreen) return;
|
||||
|
||||
settingsMenuOpen = false;
|
||||
|
||||
TogglePauseMenu(null, null);
|
||||
|
||||
if (pauseMenuOpen)
|
||||
{
|
||||
pauseMenu = new GUIFrame(new Rectangle(0, 0, 200, 300), null, Alignment.Center, "");
|
||||
|
||||
int y = 0;
|
||||
var button = new GUIButton(new Rectangle(0, y, 0, 30), "Resume", Alignment.CenterX, "", pauseMenu);
|
||||
button.OnClicked = TogglePauseMenu;
|
||||
|
||||
y += 60;
|
||||
|
||||
button = new GUIButton(new Rectangle(0, y, 0, 30), "Settings", Alignment.CenterX, "", pauseMenu);
|
||||
button.OnClicked = (btn, userData) =>
|
||||
{
|
||||
TogglePauseMenu();
|
||||
settingsMenuOpen = !settingsMenuOpen;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
y += 60;
|
||||
|
||||
if (Screen.Selected == GameMain.GameScreen && GameMain.GameSession != null)
|
||||
{
|
||||
SinglePlayerMode spMode = GameMain.GameSession.gameMode as SinglePlayerMode;
|
||||
if (spMode != null)
|
||||
{
|
||||
button = new GUIButton(new Rectangle(0, y, 0, 30), "Load previous", Alignment.CenterX, "", pauseMenu);
|
||||
button.OnClicked += TogglePauseMenu;
|
||||
button.OnClicked += GameMain.GameSession.LoadPrevious;
|
||||
|
||||
y += 60;
|
||||
}
|
||||
}
|
||||
|
||||
if (Screen.Selected == GameMain.LobbyScreen)
|
||||
{
|
||||
SinglePlayerMode spMode = GameMain.GameSession.gameMode as SinglePlayerMode;
|
||||
if (spMode != null)
|
||||
{
|
||||
button = new GUIButton(new Rectangle(0, y, 0, 30), "Save & quit", Alignment.CenterX, "", pauseMenu);
|
||||
button.OnClicked += QuitClicked;
|
||||
button.OnClicked += TogglePauseMenu;
|
||||
button.UserData = "save";
|
||||
|
||||
y += 60;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
button = new GUIButton(new Rectangle(0, y, 0, 30), "Quit", Alignment.CenterX, "", pauseMenu);
|
||||
button.OnClicked += QuitClicked;
|
||||
button.OnClicked += TogglePauseMenu;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static bool TogglePauseMenu(GUIButton button, object obj)
|
||||
{
|
||||
pauseMenuOpen = !pauseMenuOpen;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool QuitClicked(GUIButton button, object obj)
|
||||
{
|
||||
if (button.UserData as string == "save")
|
||||
{
|
||||
SaveUtil.SaveGame(GameMain.GameSession.SaveFile);
|
||||
}
|
||||
|
||||
if (GameMain.NetworkMember != null)
|
||||
{
|
||||
GameMain.NetworkMember.Disconnect();
|
||||
GameMain.NetworkMember = null;
|
||||
}
|
||||
|
||||
CoroutineManager.StopCoroutines("EndCinematic");
|
||||
|
||||
GameMain.GameSession = null;
|
||||
|
||||
GameMain.MainMenuScreen.Select();
|
||||
//Game1.MainMenuScreen.SelectTab(null, (int)MainMenuScreen.Tabs.Main);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void DrawLine(SpriteBatch sb, Vector2 start, Vector2 end, Color clr, float depth = 0.0f, int width = 1)
|
||||
{
|
||||
Vector2 edge = end - start;
|
||||
// calculate angle to rotate line
|
||||
float angle = (float)Math.Atan2(edge.Y, edge.X);
|
||||
|
||||
sb.Draw(t,
|
||||
new Rectangle(// rectangle defines shape of line and position of start of line
|
||||
(int)start.X,
|
||||
(int)start.Y,
|
||||
(int)edge.Length(), //sb will strech the texture to fill this rectangle
|
||||
width), //width of line, change this to make thicker line
|
||||
null,
|
||||
clr, //colour of line
|
||||
angle, //angle of line (calulated above)
|
||||
new Vector2(0, 0), // point in line about which to rotate
|
||||
SpriteEffects.None,
|
||||
depth);
|
||||
}
|
||||
|
||||
public static void DrawString(SpriteBatch sb, Vector2 pos, string text, Color color, Color? backgroundColor=null, int backgroundPadding=0, ScalableFont font = null)
|
||||
{
|
||||
|
||||
if (font == null) font = Font;
|
||||
if (backgroundColor != null)
|
||||
{
|
||||
Vector2 textSize = font.MeasureString(text);
|
||||
DrawRectangle(sb, pos - Vector2.One * backgroundPadding, textSize + Vector2.One * 2.0f * backgroundPadding, (Color)backgroundColor, true);
|
||||
}
|
||||
|
||||
font.DrawString(sb, text, pos, color);
|
||||
}
|
||||
|
||||
public static void DrawRectangle(SpriteBatch sb, Vector2 start, Vector2 size, Color clr, bool isFilled = false, float depth = 0.0f, int thickness = 1)
|
||||
{
|
||||
if (size.X < 0)
|
||||
{
|
||||
start.X += size.X;
|
||||
size.X = -size.X;
|
||||
}
|
||||
if (size.Y < 0)
|
||||
{
|
||||
start.Y += size.Y;
|
||||
size.Y = -size.Y;
|
||||
}
|
||||
DrawRectangle(sb, new Rectangle((int)start.X, (int)start.Y, (int)size.X, (int)size.Y), clr, isFilled, depth, thickness);
|
||||
}
|
||||
|
||||
public static void DrawRectangle(SpriteBatch sb, Rectangle rect, Color clr, bool isFilled = false, float depth = 0.0f, int thickness = 1)
|
||||
{
|
||||
if (isFilled)
|
||||
{
|
||||
sb.Draw(t, rect, null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Draw(t, new Rectangle(rect.X + thickness, rect.Y, rect.Width - thickness * 2, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
|
||||
sb.Draw(t, new Rectangle(rect.X + thickness, rect.Y + rect.Height - thickness, rect.Width - thickness * 2, thickness), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
|
||||
|
||||
sb.Draw(t, new Rectangle(rect.X, rect.Y, thickness, rect.Height), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
|
||||
sb.Draw(t, new Rectangle(rect.X + rect.Width - thickness, rect.Y, thickness, rect.Height), null, clr, 0.0f, Vector2.Zero, SpriteEffects.None, depth);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawProgressBar(SpriteBatch sb, Vector2 start, Vector2 size, float progress, Color clr, float depth = 0.0f)
|
||||
{
|
||||
DrawProgressBar(sb, start, size, progress, clr, new Color(0.5f, 0.57f, 0.6f, 1.0f), depth);
|
||||
}
|
||||
|
||||
public static void DrawProgressBar(SpriteBatch sb, Vector2 start, Vector2 size, float progress, Color clr, Color outlineColor, float depth = 0.0f)
|
||||
{
|
||||
DrawRectangle(sb, new Vector2(start.X, -start.Y), size, outlineColor, false, depth);
|
||||
|
||||
int padding = 2;
|
||||
DrawRectangle(sb, new Rectangle((int)start.X + padding, -(int)(start.Y - padding), (int)((size.X - padding * 2) * progress), (int)size.Y - padding * 2),
|
||||
clr, true, depth);
|
||||
}
|
||||
|
||||
|
||||
public static Texture2D CreateCircle(int radius)
|
||||
{
|
||||
int outerRadius = radius * 2 + 2; // So circle doesn't go out of bounds
|
||||
Texture2D texture = new Texture2D(graphicsDevice, outerRadius, outerRadius);
|
||||
|
||||
Color[] data = new Color[outerRadius * outerRadius];
|
||||
|
||||
// Colour the entire texture transparent first.
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
data[i] = Color.Transparent;
|
||||
|
||||
// Work out the minimum step necessary using trigonometry + sine approximation.
|
||||
double angleStep = 1f / radius;
|
||||
|
||||
for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
|
||||
{
|
||||
// Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
|
||||
int x = (int)Math.Round(radius + radius * Math.Cos(angle));
|
||||
int y = (int)Math.Round(radius + radius * Math.Sin(angle));
|
||||
|
||||
data[y * outerRadius + x + 1] = Color.White;
|
||||
}
|
||||
|
||||
texture.SetData(data);
|
||||
return texture;
|
||||
}
|
||||
|
||||
public static Texture2D CreateCapsule(int radius, int height)
|
||||
{
|
||||
int textureWidth = radius * 2, textureHeight = height + radius * 2;
|
||||
|
||||
Texture2D texture = new Texture2D(graphicsDevice, textureWidth, textureHeight);
|
||||
Color[] data = new Color[textureWidth * textureHeight];
|
||||
|
||||
// Colour the entire texture transparent first.
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
data[i] = Color.Transparent;
|
||||
|
||||
// Work out the minimum step necessary using trigonometry + sine approximation.
|
||||
double angleStep = 1f / radius;
|
||||
|
||||
for (int i = 0; i < 2; i++ )
|
||||
{
|
||||
for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
|
||||
{
|
||||
// Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
|
||||
int x = (int)Math.Round(radius + radius * Math.Cos(angle));
|
||||
int y = (height-1)*i + (int)Math.Round(radius + radius * Math.Sin(angle));
|
||||
|
||||
data[y * textureWidth + x] = Color.White;
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = radius; y<textureHeight-radius; y++)
|
||||
{
|
||||
data[y * textureWidth] = Color.White;
|
||||
data[y * textureWidth + (textureWidth-1)] = Color.White;
|
||||
}
|
||||
|
||||
texture.SetData(data);
|
||||
return texture;
|
||||
}
|
||||
|
||||
public static Texture2D CreateRectangle(int width, int height)
|
||||
{
|
||||
Texture2D texture = new Texture2D(graphicsDevice, width, height);
|
||||
Color[] data = new Color[width * height];
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
data[i] = Color.Transparent;
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
data[y * width] = Color.White;
|
||||
data[y * width + (width-1)] = Color.White;
|
||||
}
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
data[x] = Color.White;
|
||||
data[(height - 1) * width + x] = Color.White;
|
||||
}
|
||||
|
||||
texture.SetData(data);
|
||||
return texture;
|
||||
}
|
||||
|
||||
public static bool DrawButton(SpriteBatch sb, Rectangle rect, string text, Color color, bool isHoldable = false)
|
||||
{
|
||||
bool clicked = false;
|
||||
|
||||
if (rect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
clicked = PlayerInput.LeftButtonHeld();
|
||||
|
||||
color = clicked ?
|
||||
new Color((int)(color.R * 0.8f), (int)(color.G * 0.8f), (int)(color.B * 0.8f), color.A) :
|
||||
new Color((int)(color.R * 1.2f), (int)(color.G * 1.2f), (int)(color.B * 1.2f), color.A);
|
||||
|
||||
if (!isHoldable) clicked = PlayerInput.LeftButtonClicked();
|
||||
}
|
||||
|
||||
DrawRectangle(sb, rect, color, true);
|
||||
|
||||
Vector2 origin;
|
||||
try
|
||||
{
|
||||
origin = Font.MeasureString(text)/2;
|
||||
}
|
||||
catch
|
||||
{
|
||||
origin = Vector2.Zero;
|
||||
}
|
||||
|
||||
Font.DrawString(sb, text, new Vector2(rect.Center.X, rect.Center.Y) , Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
|
||||
|
||||
return clicked;
|
||||
}
|
||||
|
||||
public static void Draw(float deltaTime, SpriteBatch spriteBatch, Camera cam)
|
||||
{
|
||||
if (ScreenOverlayColor.A>0.0f)
|
||||
{
|
||||
DrawRectangle(
|
||||
spriteBatch,
|
||||
new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight),
|
||||
ScreenOverlayColor, true);
|
||||
}
|
||||
|
||||
if (GameMain.DebugDraw)
|
||||
{
|
||||
DrawString(spriteBatch, new Vector2(10, 10),
|
||||
"FPS: " + (int)GameMain.FrameCounter.AverageFramesPerSecond,
|
||||
Color.White, Color.Black * 0.5f, 0, SmallFont);
|
||||
|
||||
DrawString(spriteBatch, new Vector2(10, 25),
|
||||
"Physics: " + GameMain.World.UpdateTime,
|
||||
Color.White, Color.Black * 0.5f, 0, SmallFont);
|
||||
|
||||
DrawString(spriteBatch, new Vector2(10, 40),
|
||||
"Bodies: " + GameMain.World.BodyList.Count + " (" + GameMain.World.BodyList.FindAll(b => b.Awake && b.Enabled).Count + " awake)",
|
||||
Color.White, Color.Black * 0.5f, 0, SmallFont);
|
||||
|
||||
if (Screen.Selected.Cam != null)
|
||||
{
|
||||
DrawString(spriteBatch, new Vector2(10, 55),
|
||||
"Camera pos: " + Screen.Selected.Cam.Position.ToPoint(),
|
||||
Color.White, Color.Black * 0.5f, 0, SmallFont);
|
||||
}
|
||||
|
||||
if (Submarine.MainSub != null)
|
||||
{
|
||||
DrawString(spriteBatch, new Vector2(10, 70),
|
||||
"Sub pos: " + Submarine.MainSub.Position.ToPoint(),
|
||||
Color.White, Color.Black * 0.5f, 0, SmallFont);
|
||||
}
|
||||
|
||||
for (int i = 1; i < Sounds.SoundManager.DefaultSourceCount; i++)
|
||||
{
|
||||
Color clr = Color.White;
|
||||
|
||||
string soundStr = i+": ";
|
||||
|
||||
var playingSound = Sounds.SoundManager.GetPlayingSound(i);
|
||||
|
||||
if (playingSound == null)
|
||||
{
|
||||
soundStr+= "none";
|
||||
clr *= 0.5f;
|
||||
}
|
||||
else
|
||||
{
|
||||
soundStr += System.IO.Path.GetFileNameWithoutExtension(playingSound.FilePath);
|
||||
|
||||
if (Sounds.SoundManager.IsLooping(i))
|
||||
{
|
||||
soundStr += " (looping)";
|
||||
clr = Color.Yellow;
|
||||
}
|
||||
}
|
||||
|
||||
GUI.DrawString(spriteBatch, new Vector2(200, i * 15), soundStr, clr, Color.Black * 0.5f, 0, GUI.SmallFont);
|
||||
}
|
||||
}
|
||||
|
||||
if (GameMain.NetworkMember != null) GameMain.NetworkMember.Draw(spriteBatch);
|
||||
|
||||
DrawMessages(spriteBatch, (float)deltaTime);
|
||||
|
||||
if (GUIMessageBox.VisibleBox != null)
|
||||
{
|
||||
GUIMessageBox.VisibleBox.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
if (pauseMenuOpen)
|
||||
{
|
||||
pauseMenu.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
if (settingsMenuOpen)
|
||||
{
|
||||
GameMain.Config.SettingsFrame.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
DebugConsole.Draw(spriteBatch);
|
||||
|
||||
if (GUIComponent.MouseOn != null && !string.IsNullOrWhiteSpace(GUIComponent.MouseOn.ToolTip)) GUIComponent.MouseOn.DrawToolTip(spriteBatch);
|
||||
|
||||
if (!GUI.DisableHUD)
|
||||
cursor.Draw(spriteBatch, PlayerInput.LatestMousePosition);
|
||||
}
|
||||
|
||||
public static void AddToGUIUpdateList()
|
||||
{
|
||||
if (pauseMenuOpen)
|
||||
{
|
||||
pauseMenu.AddToGUIUpdateList();
|
||||
}
|
||||
|
||||
if (settingsMenuOpen)
|
||||
{
|
||||
GameMain.Config.SettingsFrame.AddToGUIUpdateList();
|
||||
}
|
||||
|
||||
if (GUIMessageBox.VisibleBox != null)
|
||||
{
|
||||
GUIMessageBox.VisibleBox.AddToGUIUpdateList();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Update(float deltaTime)
|
||||
{
|
||||
if (pauseMenuOpen)
|
||||
{
|
||||
pauseMenu.Update(deltaTime);
|
||||
}
|
||||
|
||||
if (settingsMenuOpen)
|
||||
{
|
||||
GameMain.Config.SettingsFrame.Update(deltaTime);
|
||||
}
|
||||
|
||||
if (GUIMessageBox.VisibleBox != null)
|
||||
{
|
||||
GUIMessageBox.VisibleBox.Update(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddMessage(string message, Color color, float lifeTime = 3.0f, bool playSound = true)
|
||||
{
|
||||
if (messages.Count>0 && messages[messages.Count-1].Text == message)
|
||||
{
|
||||
messages[messages.Count - 1].LifeTime = lifeTime;
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 currPos = new Vector2(GameMain.GraphicsWidth / 2.0f, GameMain.GraphicsHeight * 0.7f);
|
||||
currPos.Y += messages.Count * 30;
|
||||
|
||||
messages.Add(new GUIMessage(message, color, currPos, lifeTime));
|
||||
if (playSound) PlayUISound(GUISoundType.Message);
|
||||
}
|
||||
|
||||
public static void PlayUISound(GUISoundType soundType)
|
||||
{
|
||||
if (sounds == null) return;
|
||||
|
||||
int soundIndex = (int)soundType;
|
||||
if (soundIndex < 0 || soundIndex >= sounds.Length) return;
|
||||
|
||||
sounds[soundIndex].Play();
|
||||
}
|
||||
|
||||
private static void DrawMessages(SpriteBatch spriteBatch, float deltaTime)
|
||||
{
|
||||
if (messages.Count == 0) return;
|
||||
|
||||
Vector2 currPos = new Vector2(GameMain.GraphicsWidth / 2.0f, GameMain.GraphicsHeight * 0.7f);
|
||||
|
||||
int i = 1;
|
||||
foreach (GUIMessage msg in messages)
|
||||
{
|
||||
float alpha = 1.0f;
|
||||
|
||||
if (msg.LifeTime < 1.0f)
|
||||
{
|
||||
alpha -= 1.0f - msg.LifeTime;
|
||||
}
|
||||
|
||||
msg.Pos = MathUtils.SmoothStep(msg.Pos, currPos, deltaTime*20.0f);
|
||||
|
||||
Font.DrawString(spriteBatch, msg.Text,
|
||||
new Vector2((int)msg.Pos.X - 1, (int)msg.Pos.Y - 1),
|
||||
Color.Black * alpha*0.5f, 0.0f,
|
||||
new Vector2((int)(0.5f * msg.Size.X), (int)(0.5f * msg.Size.Y)), 1.0f, SpriteEffects.None, 0.0f);
|
||||
|
||||
Font.DrawString(spriteBatch, msg.Text,
|
||||
new Vector2((int)msg.Pos.X, (int)msg.Pos.Y),
|
||||
msg.Color * alpha, 0.0f,
|
||||
new Vector2((int)(0.5f * msg.Size.X), (int)(0.5f * msg.Size.Y)), 1.0f, SpriteEffects.None, 0.0f);
|
||||
|
||||
|
||||
|
||||
currPos.Y += 30.0f;
|
||||
|
||||
messages[0].LifeTime -= deltaTime/i;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (messages[0].LifeTime <= 0.0f) messages.Remove(messages[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIButton : GUIComponent
|
||||
{
|
||||
protected GUITextBlock textBlock;
|
||||
protected GUIFrame frame;
|
||||
|
||||
public delegate bool OnClickedHandler(GUIButton button, object obj);
|
||||
public OnClickedHandler OnClicked;
|
||||
|
||||
public delegate bool OnPressedHandler();
|
||||
public OnPressedHandler OnPressed;
|
||||
|
||||
public bool CanBeSelected = true;
|
||||
|
||||
private bool enabled;
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (value == enabled) return;
|
||||
|
||||
enabled = value;
|
||||
frame.Color = enabled ? color : Color.Gray * 0.7f;
|
||||
}
|
||||
}
|
||||
|
||||
public override Color Color
|
||||
{
|
||||
get { return base.Color; }
|
||||
set
|
||||
{
|
||||
base.Color = value;
|
||||
frame.Color = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Color HoverColor
|
||||
{
|
||||
get { return base.HoverColor; }
|
||||
set
|
||||
{
|
||||
base.HoverColor = value;
|
||||
frame.HoverColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Color SelectedColor
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.SelectedColor;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.SelectedColor = value;
|
||||
frame.SelectedColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Color OutlineColor
|
||||
{
|
||||
get { return base.OutlineColor; }
|
||||
set
|
||||
{
|
||||
base.OutlineColor = value;
|
||||
if (frame != null) frame.OutlineColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Color TextColor
|
||||
{
|
||||
get { return textBlock.TextColor; }
|
||||
set { textBlock.TextColor = value; }
|
||||
}
|
||||
|
||||
public override ScalableFont Font
|
||||
{
|
||||
get
|
||||
{
|
||||
return (textBlock==null) ? GUI.Font : textBlock.Font;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Font = value;
|
||||
if (textBlock != null) textBlock.Font = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get { return textBlock.Text; }
|
||||
set { textBlock.Text = value; }
|
||||
}
|
||||
|
||||
public override string ToolTip
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.ToolTip;
|
||||
}
|
||||
set
|
||||
{
|
||||
textBlock.ToolTip = value;
|
||||
base.ToolTip = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Rectangle Rect
|
||||
{
|
||||
get
|
||||
{
|
||||
return rect;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Rect = value;
|
||||
|
||||
frame.Rect = new Rectangle(value.X, value.Y, frame.Rect.Width, frame.Rect.Height);
|
||||
textBlock.Rect = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Selected { get; set; }
|
||||
|
||||
public GUIButton(Rectangle rect, string text, string style, GUIComponent parent = null)
|
||||
: this(rect, text, null, Alignment.Left, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Alignment alignment, string style, GUIComponent parent = null)
|
||||
: this(rect, text, null, alignment, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color? color, string style, GUIComponent parent = null)
|
||||
: this(rect, text, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color? color, Alignment alignment, string style = "", GUIComponent parent = null)
|
||||
: this(rect, text, color, alignment, Alignment.Center, style, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public GUIButton(Rectangle rect, string text, Color? color, Alignment alignment, Alignment textAlignment, string style = "", GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
if (color != null) this.color = (Color)color;
|
||||
this.alignment = alignment;
|
||||
|
||||
if (parent != null) parent.AddChild(this);
|
||||
|
||||
frame = new GUIFrame(Rectangle.Empty, style, this);
|
||||
GUI.Style.Apply(frame, style == "" ? "GUIButton" : style);
|
||||
|
||||
textBlock = new GUITextBlock(Rectangle.Empty, text,
|
||||
Color.Transparent, (this.style == null) ? Color.Black : this.style.textColor,
|
||||
textAlignment, null, this);
|
||||
GUI.Style.Apply(textBlock, style, this);
|
||||
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
public override void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
base.ApplyStyle(style);
|
||||
|
||||
if (frame != null) frame.ApplyStyle(style);
|
||||
if (textBlock != null) textBlock.ApplyStyle(style);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
base.Update(deltaTime);
|
||||
if (rect.Contains(PlayerInput.MousePosition) && CanBeSelected && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn)))
|
||||
{
|
||||
state = ComponentState.Hover;
|
||||
if (PlayerInput.LeftButtonHeld())
|
||||
{
|
||||
if (OnPressed != null)
|
||||
{
|
||||
if (OnPressed()) state = ComponentState.Pressed;
|
||||
}
|
||||
}
|
||||
else if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
GUI.PlayUISound(GUISoundType.Click);
|
||||
if (OnClicked != null)
|
||||
{
|
||||
if (OnClicked(this, UserData) && CanBeSelected) state = ComponentState.Selected;
|
||||
}
|
||||
else
|
||||
{
|
||||
Selected = !Selected;
|
||||
// state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
state = Selected ? ComponentState.Selected : ComponentState.None;
|
||||
}
|
||||
frame.State = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using EventInput;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public abstract class GUIComponent
|
||||
{
|
||||
const float FlashDuration = 1.5f;
|
||||
|
||||
public static GUIComponent MouseOn
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
public static void ForceMouseOn(GUIComponent c)
|
||||
{
|
||||
MouseOn = c;
|
||||
}
|
||||
|
||||
protected static List<GUIComponent> ComponentsToUpdate = new List<GUIComponent>();
|
||||
|
||||
public virtual void AddToGUIUpdateList()
|
||||
{
|
||||
if (!Visible) return;
|
||||
if (ComponentsToUpdate.Contains(this)) return;
|
||||
ComponentsToUpdate.Add(this);
|
||||
|
||||
try
|
||||
{
|
||||
List<GUIComponent> fixedChildren = new List<GUIComponent>(children);
|
||||
foreach (GUIComponent c in fixedChildren)
|
||||
{
|
||||
c.AddToGUIUpdateList();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.NewMessage("Error in AddToGUIUPdateList! GUIComponent runtime type: "+this.GetType().ToString()+"; children count: "+children.Count.ToString(),Color.Red);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearUpdateList()
|
||||
{
|
||||
if (keyboardDispatcher != null &&
|
||||
KeyboardDispatcher.Subscriber is GUIComponent &&
|
||||
!ComponentsToUpdate.Contains((GUIComponent)KeyboardDispatcher.Subscriber))
|
||||
{
|
||||
KeyboardDispatcher.Subscriber = null;
|
||||
}
|
||||
|
||||
ComponentsToUpdate.Clear();
|
||||
}
|
||||
|
||||
public static GUIComponent UpdateMouseOn()
|
||||
{
|
||||
MouseOn = null;
|
||||
for (int i=ComponentsToUpdate.Count-1;i>=0;i--)
|
||||
{
|
||||
GUIComponent c = ComponentsToUpdate[i];
|
||||
if (c.MouseRect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
MouseOn = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return MouseOn;
|
||||
}
|
||||
|
||||
protected static KeyboardDispatcher keyboardDispatcher;
|
||||
|
||||
public enum ComponentState { None, Hover, Pressed, Selected };
|
||||
|
||||
protected Alignment alignment;
|
||||
|
||||
protected GUIComponentStyle style;
|
||||
|
||||
protected object userData;
|
||||
|
||||
protected Rectangle rect;
|
||||
|
||||
public bool CanBeFocused;
|
||||
|
||||
protected Vector4 padding;
|
||||
|
||||
protected Color color;
|
||||
protected Color hoverColor;
|
||||
protected Color selectedColor;
|
||||
|
||||
protected GUIComponent parent;
|
||||
public List<GUIComponent> children;
|
||||
|
||||
protected ComponentState state;
|
||||
|
||||
protected Color flashColor;
|
||||
protected float flashTimer;
|
||||
|
||||
public virtual ScalableFont Font
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public virtual string ToolTip
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public GUIComponentStyle Style
|
||||
{
|
||||
get { return style; }
|
||||
}
|
||||
|
||||
public bool Visible
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public bool TileSprites;
|
||||
|
||||
private static GUITextBlock toolTipBlock;
|
||||
|
||||
//protected float alpha;
|
||||
|
||||
public GUIComponent Parent
|
||||
{
|
||||
get { return parent; }
|
||||
}
|
||||
|
||||
public Vector2 Center
|
||||
{
|
||||
get { return new Vector2(rect.Center.X, rect.Center.Y); }
|
||||
}
|
||||
|
||||
public virtual Rectangle Rect
|
||||
{
|
||||
get { return rect; }
|
||||
set
|
||||
{
|
||||
int prevX = rect.X, prevY = rect.Y;
|
||||
int prevWidth = rect.Width, prevHeight = rect.Height;
|
||||
|
||||
rect = value;
|
||||
|
||||
if (prevX == rect.X && prevY == rect.Y && rect.Width == prevWidth && rect.Height == prevHeight) return;
|
||||
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
child.Rect = new Rectangle(
|
||||
child.rect.X + (rect.X - prevX),
|
||||
child.rect.Y + (rect.Y - prevY),
|
||||
Math.Max(child.rect.Width + (rect.Width - prevWidth),0),
|
||||
Math.Max(child.rect.Height + (rect.Height - prevHeight),0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Rectangle MouseRect
|
||||
{
|
||||
get { return CanBeFocused ? rect : Rectangle.Empty; }
|
||||
}
|
||||
|
||||
public Dictionary<GUIComponent.ComponentState, List<UISprite>> sprites;
|
||||
//public Alignment SpriteAlignment { get; set; }
|
||||
//public bool RepeatSpriteX, RepeatSpriteY;
|
||||
|
||||
public virtual Color OutlineColor { get; set; }
|
||||
|
||||
public ComponentState State
|
||||
{
|
||||
get { return state; }
|
||||
set { state = value; }
|
||||
}
|
||||
|
||||
public object UserData
|
||||
{
|
||||
get { return userData; }
|
||||
set { userData = value; }
|
||||
}
|
||||
|
||||
public virtual Vector4 Padding
|
||||
{
|
||||
get { return padding; }
|
||||
set { padding = value; }
|
||||
}
|
||||
|
||||
public int CountChildren
|
||||
{
|
||||
get { return children.Count; }
|
||||
}
|
||||
|
||||
public virtual Color Color
|
||||
{
|
||||
get { return color; }
|
||||
set { color = value; }
|
||||
}
|
||||
|
||||
public virtual Color HoverColor
|
||||
{
|
||||
get { return hoverColor; }
|
||||
set { hoverColor = value; }
|
||||
}
|
||||
|
||||
public virtual Color SelectedColor
|
||||
{
|
||||
get { return selectedColor; }
|
||||
set { selectedColor = value; }
|
||||
}
|
||||
|
||||
public static KeyboardDispatcher KeyboardDispatcher
|
||||
{
|
||||
get { return keyboardDispatcher; }
|
||||
}
|
||||
|
||||
protected GUIComponent(string style)
|
||||
{
|
||||
Visible = true;
|
||||
|
||||
TileSprites = true;
|
||||
|
||||
OutlineColor = Color.Transparent;
|
||||
|
||||
Font = GUI.Font;
|
||||
|
||||
children = new List<GUIComponent>();
|
||||
|
||||
CanBeFocused = true;
|
||||
|
||||
if (style != null)
|
||||
GUI.Style.Apply(this, style);
|
||||
}
|
||||
|
||||
public static void Init(GameWindow window)
|
||||
{
|
||||
keyboardDispatcher = new KeyboardDispatcher(window);
|
||||
}
|
||||
|
||||
public T GetChild<T>() where T : GUIComponent
|
||||
{
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
if (child is T) return (T)(object)child;
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public GUIComponent GetChild(object obj)
|
||||
{
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
if (child.UserData == obj) return child;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsParentOf(GUIComponent component)
|
||||
{
|
||||
for(int i = children.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (children[i] == component) return true;
|
||||
if (children[i].IsParentOf(component)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual void Flash(Color? color = null)
|
||||
{
|
||||
flashTimer = FlashDuration;
|
||||
flashColor = (color == null) ? Color.Red * 0.8f : (Color)color;
|
||||
|
||||
//foreach (GUIComponent child in children)
|
||||
//{
|
||||
// child.Flash();
|
||||
//}
|
||||
}
|
||||
|
||||
public virtual void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
|
||||
if (flashTimer > 0.0f)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch,
|
||||
new Rectangle(rect.X - 5, rect.Y - 5, rect.Width + 10, rect.Height + 10),
|
||||
flashColor * (flashTimer / FlashDuration), true);
|
||||
}
|
||||
|
||||
if (currColor.A > 0.0f && (sprites == null || !sprites.Any())) GUI.DrawRectangle(spriteBatch, rect, currColor * (currColor.A / 255.0f), true);
|
||||
|
||||
if (sprites != null && sprites[state] != null && currColor.A > 0.0f)
|
||||
{
|
||||
foreach (UISprite uiSprite in sprites[state])
|
||||
{
|
||||
if (uiSprite.Slice)
|
||||
{
|
||||
Vector2 pos = new Vector2(rect.X, rect.Y);
|
||||
|
||||
int centerWidth = Math.Max(rect.Width - uiSprite.Slices[0].Width - uiSprite.Slices[2].Width, 0);
|
||||
int centerHeight = Math.Max(rect.Height - uiSprite.Slices[0].Height - uiSprite.Slices[8].Height, 0);
|
||||
|
||||
Vector2 scale = new Vector2(
|
||||
MathHelper.Clamp((float)rect.Width / (uiSprite.Slices[0].Width + uiSprite.Slices[2].Width),0, 1),
|
||||
MathHelper.Clamp((float)rect.Height / (uiSprite.Slices[0].Height + uiSprite.Slices[6].Height), 0, 1));
|
||||
|
||||
for (int x = 0; x < 3; x++)
|
||||
{
|
||||
float width = (x == 1 ? centerWidth : uiSprite.Slices[x].Width) * scale.X;
|
||||
for (int y = 0; y < 3; y++)
|
||||
{
|
||||
float height = (y == 1 ? centerHeight : uiSprite.Slices[x + y * 3].Height) * scale.Y;
|
||||
|
||||
spriteBatch.Draw(uiSprite.Sprite.Texture,
|
||||
new Rectangle((int)pos.X, (int)pos.Y, (int)width, (int)height),
|
||||
uiSprite.Slices[x + y * 3],
|
||||
currColor * (currColor.A / 255.0f));
|
||||
|
||||
pos.Y += height;
|
||||
}
|
||||
pos.X += width;
|
||||
pos.Y = rect.Y;
|
||||
}
|
||||
}
|
||||
else if (uiSprite.Tile)
|
||||
{
|
||||
Vector2 startPos = new Vector2(rect.X, rect.Y);
|
||||
Vector2 size = new Vector2(Math.Min(uiSprite.Sprite.SourceRect.Width, rect.Width), Math.Min(uiSprite.Sprite.SourceRect.Height, rect.Height));
|
||||
|
||||
if (uiSprite.Sprite.size.X == 0.0f) size.X = rect.Width;
|
||||
if (uiSprite.Sprite.size.Y == 0.0f) size.Y = rect.Height;
|
||||
|
||||
uiSprite.Sprite.DrawTiled(spriteBatch, startPos, size, currColor * (currColor.A / 255.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (uiSprite.MaintainAspectRatio)
|
||||
{
|
||||
float scale = (float)(rect.Width) / uiSprite.Sprite.SourceRect.Width;
|
||||
|
||||
spriteBatch.Draw(uiSprite.Sprite.Texture, rect,
|
||||
new Rectangle(uiSprite.Sprite.SourceRect.X, uiSprite.Sprite.SourceRect.Y, (int)(uiSprite.Sprite.SourceRect.Width), (int)(rect.Height / scale)),
|
||||
currColor * (currColor.A / 255.0f), 0.0f, Vector2.Zero, SpriteEffects.None, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
spriteBatch.Draw(uiSprite.Sprite.Texture, rect, uiSprite.Sprite.SourceRect, currColor * (currColor.A / 255.0f));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Color newColor = color;
|
||||
//if (state == ComponentState.Selected) newColor = selectedColor;
|
||||
//if (state == ComponentState.Hover) newColor = hoverColor;
|
||||
|
||||
//GUI.DrawRectangle(spriteBatch, rect, newColor*alpha, true);
|
||||
//DrawChildren(spriteBatch);
|
||||
}
|
||||
|
||||
public void DrawToolTip(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
int width = 400;
|
||||
if (toolTipBlock == null || (string)toolTipBlock.userData != ToolTip)
|
||||
{
|
||||
toolTipBlock = new GUITextBlock(new Rectangle(0, 0, width, 18), ToolTip, "GUIToolTip", Alignment.TopLeft, Alignment.TopLeft, null, true, GUI.SmallFont);
|
||||
toolTipBlock.padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
|
||||
toolTipBlock.rect.Width = (int)(GUI.SmallFont.MeasureString(toolTipBlock.WrappedText).X + 20);
|
||||
toolTipBlock.rect.Height = toolTipBlock.WrappedText.Split('\n').Length * 18 + 7;
|
||||
toolTipBlock.userData = ToolTip;
|
||||
|
||||
}
|
||||
|
||||
toolTipBlock.rect = new Rectangle(MouseOn.Rect.Center.X, MouseOn.rect.Bottom, toolTipBlock.rect.Width, toolTipBlock.rect.Height);
|
||||
if (toolTipBlock.rect.Right > GameMain.GraphicsWidth - 10)
|
||||
{
|
||||
toolTipBlock.rect.Location -= new Point(toolTipBlock.rect.Right - (GameMain.GraphicsWidth - 10), 0);
|
||||
}
|
||||
|
||||
toolTipBlock.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
public virtual void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
if (flashTimer>0.0f) flashTimer -= deltaTime;
|
||||
|
||||
/*if (CanBeFocused)
|
||||
{
|
||||
if (rect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
MouseOn = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (MouseOn == this) MouseOn = null;
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
try
|
||||
{
|
||||
//use a fixed list since children can change their order in the main children list
|
||||
//TODO: maybe find a more efficient way of handling changes in list order
|
||||
List<GUIComponent> fixedChildren = new List<GUIComponent>(children);
|
||||
foreach (GUIComponent c in fixedChildren)
|
||||
{
|
||||
if (!c.Visible) continue;
|
||||
c.Update(deltaTime);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.NewMessage("Error in Update! GUIComponent runtime type: " + this.GetType().ToString() + "; children count: " + children.Count.ToString(), Color.Red);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void UpdateDimensions(GUIComponent parent = null)
|
||||
{
|
||||
Rectangle parentRect = (parent==null) ? new Rectangle(0,0,GameMain.GraphicsWidth, GameMain.GraphicsHeight) : parent.rect;
|
||||
|
||||
Vector4 padding = (parent == null) ? Vector4.Zero : parent.padding;
|
||||
|
||||
if (rect.Width == 0) rect.Width = parentRect.Width - rect.X
|
||||
- (int)padding.X - (int)padding.Z;
|
||||
|
||||
if (rect.Height == 0) rect.Height = parentRect.Height - rect.Y
|
||||
- (int)padding.Y - (int)padding.W;
|
||||
|
||||
if (alignment.HasFlag(Alignment.CenterX))
|
||||
{
|
||||
rect.X += parentRect.X + (int)parentRect.Width/2 - (int)rect.Width/2;
|
||||
}
|
||||
else if (alignment.HasFlag(Alignment.Right))
|
||||
{
|
||||
rect.X += parentRect.X + (int)parentRect.Width - (int)padding.Z - (int)rect.Width;
|
||||
}
|
||||
else
|
||||
{
|
||||
rect.X += parentRect.X + (int)padding.X;
|
||||
}
|
||||
|
||||
if (alignment.HasFlag(Alignment.CenterY))
|
||||
{
|
||||
rect.Y += parentRect.Y + (int)parentRect.Height / 2 - (int)rect.Height / 2;
|
||||
}
|
||||
else if (alignment.HasFlag(Alignment.Bottom))
|
||||
{
|
||||
rect.Y += parentRect.Y + (int)parentRect.Height - (int)padding.W - (int)rect.Height;
|
||||
}
|
||||
else
|
||||
{
|
||||
rect.Y += parentRect.Y + (int)padding.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
if (style == null) return;
|
||||
|
||||
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)
|
||||
{
|
||||
for (int i = 0; i < children.Count; i++ )
|
||||
{
|
||||
children[i].Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void AddChild(GUIComponent child)
|
||||
{
|
||||
if (child == null) return;
|
||||
if (child.IsParentOf(this))
|
||||
{
|
||||
DebugConsole.ThrowError("Tried to add the parent of a GUIComponent as a child.\n" + Environment.StackTrace);
|
||||
return;
|
||||
}
|
||||
if (child == this)
|
||||
{
|
||||
DebugConsole.ThrowError("Tried to add a GUIComponent as its own child\n" + Environment.StackTrace);
|
||||
return;
|
||||
}
|
||||
if (children.Contains(child))
|
||||
{
|
||||
DebugConsole.ThrowError("Tried to add a the same child twice to a GUIComponent" + Environment.StackTrace);
|
||||
return;
|
||||
}
|
||||
|
||||
child.parent = this;
|
||||
child.UpdateDimensions(this);
|
||||
|
||||
children.Add(child);
|
||||
}
|
||||
|
||||
public virtual void RemoveChild(GUIComponent child)
|
||||
{
|
||||
if (child == null) return;
|
||||
if (children.Contains(child)) children.Remove(child);
|
||||
}
|
||||
|
||||
public GUIComponent FindChild(object userData)
|
||||
{
|
||||
return children.FirstOrDefault(c => c.userData == userData);
|
||||
}
|
||||
|
||||
public List<GUIComponent> FindChildren(object userData)
|
||||
{
|
||||
return children.FindAll(c => c.userData == userData);
|
||||
}
|
||||
|
||||
public virtual void ClearChildren()
|
||||
{
|
||||
children.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIDropDown : GUIComponent
|
||||
{
|
||||
|
||||
public delegate bool OnSelectedHandler(GUIComponent selected, object obj = null);
|
||||
public OnSelectedHandler OnSelected;
|
||||
|
||||
private GUIButton button;
|
||||
private GUIListBox listBox;
|
||||
|
||||
public bool Dropped { get; set; }
|
||||
|
||||
public object SelectedItemData
|
||||
{
|
||||
get
|
||||
{
|
||||
if (listBox.Selected == null) return null;
|
||||
return listBox.Selected.UserData;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get { return listBox.Enabled; }
|
||||
set { listBox.Enabled = value; }
|
||||
}
|
||||
|
||||
public GUIComponent Selected
|
||||
{
|
||||
get { return listBox.Selected; }
|
||||
}
|
||||
|
||||
public GUIListBox ListBox
|
||||
{
|
||||
get { return listBox; }
|
||||
}
|
||||
|
||||
public object SelectedData
|
||||
{
|
||||
get
|
||||
{
|
||||
return (listBox.Selected == null) ? null : listBox.Selected.UserData;
|
||||
}
|
||||
}
|
||||
|
||||
public int SelectedIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
if (listBox.Selected == null) return -1;
|
||||
return listBox.children.FindIndex(x => x == listBox.Selected);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToolTip
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.ToolTip;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.ToolTip = value;
|
||||
button.ToolTip = value;
|
||||
listBox.ToolTip = value;
|
||||
}
|
||||
}
|
||||
|
||||
public GUIDropDown(Rectangle rect, string text, string style, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
if (parent != null) parent.AddChild(this);
|
||||
|
||||
button = new GUIButton(this.rect, text, Color.White, Alignment.TopLeft, Alignment.CenterLeft, "GUIDropDown", null);
|
||||
GUI.Style.Apply(button, style, this);
|
||||
|
||||
button.OnClicked = OnClicked;
|
||||
|
||||
listBox = new GUIListBox(new Rectangle(this.rect.X, this.rect.Bottom, this.rect.Width, 200), style, null);
|
||||
listBox.OnSelected = SelectItem;
|
||||
}
|
||||
|
||||
public override void AddChild(GUIComponent child)
|
||||
{
|
||||
listBox.AddChild(child);
|
||||
}
|
||||
|
||||
public void AddItem(string text, object userData = null)
|
||||
{
|
||||
GUITextBlock textBlock = new GUITextBlock(new Rectangle(0,0,0,20), text, "ListBoxElement", Alignment.TopLeft, Alignment.CenterLeft, listBox);
|
||||
textBlock.UserData = userData;
|
||||
}
|
||||
|
||||
public override void ClearChildren()
|
||||
{
|
||||
listBox.ClearChildren();
|
||||
}
|
||||
|
||||
public List<GUIComponent> GetChildren()
|
||||
{
|
||||
return listBox.children;
|
||||
}
|
||||
|
||||
private bool SelectItem(GUIComponent component, object obj)
|
||||
{
|
||||
GUITextBlock textBlock = component as GUITextBlock;
|
||||
if (textBlock==null) return false;
|
||||
button.Text = textBlock.Text;
|
||||
|
||||
Dropped = false;
|
||||
|
||||
if (OnSelected != null) OnSelected(component, component.UserData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SelectItem(object userData)
|
||||
{
|
||||
//GUIComponent child = listBox.children.FirstOrDefault(c => c.UserData == userData);
|
||||
|
||||
//if (child == null) return;
|
||||
|
||||
listBox.Select(userData);
|
||||
|
||||
//SelectItem(child, userData);
|
||||
}
|
||||
|
||||
public void Select(int index)
|
||||
{
|
||||
listBox.Select(index);
|
||||
}
|
||||
|
||||
|
||||
private bool wasOpened;
|
||||
|
||||
private bool OnClicked(GUIComponent component, object obj)
|
||||
{
|
||||
if (wasOpened) return false;
|
||||
|
||||
wasOpened = true;
|
||||
Dropped = !Dropped;
|
||||
|
||||
if (Dropped && parent.children[parent.children.Count-1]!=this)
|
||||
{
|
||||
parent.children.Remove(this);
|
||||
parent.children.Add(this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void AddToGUIUpdateList()
|
||||
{
|
||||
base.AddToGUIUpdateList();
|
||||
button.AddToGUIUpdateList();
|
||||
if (Dropped) listBox.AddToGUIUpdateList();
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
wasOpened = false;
|
||||
|
||||
base.Update(deltaTime);
|
||||
|
||||
if (Dropped && PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Rectangle listBoxRect = listBox.Rect;
|
||||
listBoxRect.Width += 20;
|
||||
if (!listBoxRect.Contains(PlayerInput.MousePosition) && !button.Rect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
Dropped = false;
|
||||
}
|
||||
}
|
||||
|
||||
button.Update(deltaTime);
|
||||
|
||||
if (Dropped) listBox.Update(deltaTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
base.Draw(spriteBatch);
|
||||
|
||||
button.Draw(spriteBatch);
|
||||
|
||||
if (!Dropped) return;
|
||||
|
||||
listBox.Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIFrame : GUIComponent
|
||||
{
|
||||
public GUIFrame(Rectangle rect, string style = "", GUIComponent parent = null)
|
||||
: this(rect, null, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public GUIFrame(Rectangle rect, Color color, string style = "", GUIComponent parent = null)
|
||||
: this(rect, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIFrame(Rectangle rect, Color? color, Alignment alignment, string style = "", GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
if (color != null) this.color = (Color)color;
|
||||
|
||||
if (parent != null)
|
||||
{
|
||||
parent.AddChild(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateDimensions();
|
||||
}
|
||||
|
||||
//if (style != null) ApplyStyle(style);
|
||||
}
|
||||
|
||||
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
|
||||
if (sprites == null || !sprites.Any()) GUI.DrawRectangle(spriteBatch, rect, currColor * (currColor.A/255.0f), true);
|
||||
base.Draw(spriteBatch);
|
||||
|
||||
if (OutlineColor != Color.Transparent)
|
||||
{
|
||||
GUI.DrawRectangle(spriteBatch, rect, OutlineColor * (OutlineColor.A/255.0f), false);
|
||||
}
|
||||
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIImage : GUIComponent
|
||||
{
|
||||
public float Rotation;
|
||||
|
||||
private Sprite sprite;
|
||||
|
||||
private Rectangle sourceRect;
|
||||
|
||||
bool crop;
|
||||
|
||||
public bool Crop
|
||||
{
|
||||
get
|
||||
{
|
||||
return crop;
|
||||
}
|
||||
set
|
||||
{
|
||||
crop = value;
|
||||
if (crop)
|
||||
{
|
||||
sourceRect.Width = Math.Min(sprite.SourceRect.Width, Rect.Width);
|
||||
sourceRect.Height = Math.Min(sprite.SourceRect.Height, Rect.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float Scale
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Rectangle SourceRect
|
||||
{
|
||||
get { return sourceRect; }
|
||||
set { sourceRect = value; }
|
||||
}
|
||||
|
||||
public GUIImage(Rectangle rect, string spritePath, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, new Sprite(spritePath, Vector2.Zero), alignment, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIImage(Rectangle rect, Sprite sprite, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, sprite==null ? Rectangle.Empty : sprite.SourceRect, sprite, alignment, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIImage(Rectangle rect, Rectangle sourceRect, Sprite sprite, Alignment alignment, GUIComponent parent = null)
|
||||
: base(null)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
color = Color.White;
|
||||
|
||||
//alpha = 1.0f;
|
||||
|
||||
Scale = 1.0f;
|
||||
|
||||
this.sprite = sprite;
|
||||
|
||||
if (rect.Width == 0) this.rect.Width = (int)sprite.size.X;
|
||||
if (rect.Height == 0) this.rect.Height = (int)Math.Min(sprite.size.Y, sprite.size.Y * (this.rect.Width / sprite.size.X));
|
||||
|
||||
this.sourceRect = sourceRect;
|
||||
|
||||
if (parent != null) parent.AddChild(this);
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
|
||||
if (sprite != null && sprite.Texture != null)
|
||||
{
|
||||
spriteBatch.Draw(sprite.Texture, new Vector2(rect.X, rect.Y), sourceRect, currColor * (currColor.A / 255.0f), Rotation, Vector2.Zero,
|
||||
Scale, SpriteEffects.None, 0.0f);
|
||||
}
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIListBox : GUIComponent
|
||||
{
|
||||
protected List<GUIComponent> selected;
|
||||
|
||||
public delegate bool OnSelectedHandler(GUIComponent component, object obj);
|
||||
public OnSelectedHandler OnSelected;
|
||||
|
||||
public delegate object CheckSelectedHandler();
|
||||
public CheckSelectedHandler CheckSelected;
|
||||
|
||||
private GUIScrollBar scrollBar;
|
||||
private GUIFrame frame;
|
||||
|
||||
private int totalSize;
|
||||
|
||||
private int spacing;
|
||||
|
||||
private bool scrollBarEnabled;
|
||||
private bool scrollBarHidden;
|
||||
|
||||
private bool enabled;
|
||||
|
||||
public bool SelectMultiple;
|
||||
|
||||
public GUIComponent Selected
|
||||
{
|
||||
get
|
||||
{
|
||||
return selected.Any() ? selected[0] : null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<GUIComponent> AllSelected
|
||||
{
|
||||
get { return selected; }
|
||||
}
|
||||
|
||||
public object SelectedData
|
||||
{
|
||||
get
|
||||
{
|
||||
return (Selected == null) ? null : Selected.UserData;
|
||||
}
|
||||
}
|
||||
|
||||
public int SelectedIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Selected == null) return -1;
|
||||
return children.FindIndex(x => x == Selected);
|
||||
}
|
||||
}
|
||||
|
||||
public float BarScroll
|
||||
{
|
||||
get { return scrollBar.BarScroll; }
|
||||
set { scrollBar.BarScroll = value; }
|
||||
}
|
||||
|
||||
public float BarSize
|
||||
{
|
||||
get { return scrollBar.BarSize; }
|
||||
}
|
||||
|
||||
public int Spacing
|
||||
{
|
||||
get { return spacing; }
|
||||
set { spacing = value; }
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get { return enabled; }
|
||||
set { enabled = value; }
|
||||
}
|
||||
|
||||
public override Color Color
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Color;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Color = value;
|
||||
|
||||
frame.Color = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ScrollBarEnabled
|
||||
{
|
||||
get { return scrollBarEnabled; }
|
||||
set
|
||||
{
|
||||
scrollBarEnabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, string style, GUIComponent parent = null)
|
||||
: this(rect, style, Alignment.TopLeft, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, string style, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, null, alignment, style, parent, false)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, Color? color, string style = null, GUIComponent parent = null)
|
||||
: this(rect, color, (Alignment.Left | Alignment.Top), style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIListBox(Rectangle rect, Color? color, Alignment alignment, string style = null, GUIComponent parent = null, bool isHorizontal = false)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
this.alignment = alignment;
|
||||
|
||||
selected = new List<GUIComponent>();
|
||||
|
||||
if (color != null) this.color = (Color)color;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
scrollBarHidden = true;
|
||||
|
||||
if (isHorizontal)
|
||||
{
|
||||
scrollBar = new GUIScrollBar(
|
||||
new Rectangle(this.rect.X, this.rect.Bottom - 20, this.rect.Width, 20), null, 1.0f, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
scrollBar = new GUIScrollBar(
|
||||
new Rectangle(this.rect.Right - 20, this.rect.Y, 20, this.rect.Height), null, 1.0f, "");
|
||||
}
|
||||
|
||||
scrollBar.IsHorizontal = isHorizontal;
|
||||
|
||||
frame = new GUIFrame(new Rectangle(0, 0, this.rect.Width, this.rect.Height), style, this);
|
||||
if (style != null) GUI.Style.Apply(frame, style, this);
|
||||
|
||||
UpdateScrollBarSize();
|
||||
|
||||
children.Clear();
|
||||
|
||||
enabled = true;
|
||||
|
||||
scrollBarEnabled = true;
|
||||
|
||||
scrollBar.BarScroll = 0.0f;
|
||||
}
|
||||
|
||||
public void Select(object userData, bool force = false)
|
||||
{
|
||||
for (int i = 0; i < children.Count; i++)
|
||||
{
|
||||
if (!children[i].UserData.Equals(userData)) continue;
|
||||
|
||||
Select(i, force);
|
||||
|
||||
//if (OnSelected != null) OnSelected(Selected, Selected.UserData);
|
||||
if (!SelectMultiple) return;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateChildrenRect(float deltaTime)
|
||||
{
|
||||
int x = rect.X, y = rect.Y;
|
||||
|
||||
if (!scrollBarHidden)
|
||||
{
|
||||
if (scrollBar.IsHorizontal)
|
||||
{
|
||||
x -= (int)((totalSize - rect.Width) * scrollBar.BarScroll);
|
||||
}
|
||||
else
|
||||
{
|
||||
y -= (int)((totalSize - rect.Height) * scrollBar.BarScroll);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < children.Count; i++)
|
||||
{
|
||||
GUIComponent child = children[i];
|
||||
if (child == frame || !child.Visible) continue;
|
||||
|
||||
child.Rect = new Rectangle(x, y, child.Rect.Width, child.Rect.Height);
|
||||
if (scrollBar.IsHorizontal)
|
||||
{
|
||||
x += child.Rect.Width + spacing;
|
||||
}
|
||||
else
|
||||
{
|
||||
y += child.Rect.Height + spacing;
|
||||
}
|
||||
|
||||
if (deltaTime>0.0f) child.Update(deltaTime);
|
||||
if (enabled && child.CanBeFocused &&
|
||||
(MouseOn == this || (MouseOn != null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
child.State = ComponentState.Hover;
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Select(i);
|
||||
}
|
||||
}
|
||||
else if (selected.Contains(child))
|
||||
{
|
||||
child.State = ComponentState.Selected;
|
||||
|
||||
if (CheckSelected != null)
|
||||
{
|
||||
if (CheckSelected() != child.UserData) selected.Remove(child);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
child.State = ComponentState.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void AddToGUIUpdateList()
|
||||
{
|
||||
if (!Visible) return;
|
||||
if (ComponentsToUpdate.Contains(this)) return;
|
||||
ComponentsToUpdate.Add(this);
|
||||
|
||||
try
|
||||
{
|
||||
List<GUIComponent> fixedChildren = new List<GUIComponent>(children);
|
||||
int lastVisible = 0;
|
||||
for (int i = 0; i < fixedChildren.Count; i++)
|
||||
{
|
||||
if (fixedChildren[i] == frame) continue;
|
||||
|
||||
if (!IsChildVisible(fixedChildren[i]))
|
||||
{
|
||||
if (lastVisible > 0) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
lastVisible = i;
|
||||
fixedChildren[i].AddToGUIUpdateList();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.NewMessage("Error in AddToGUIUpdateList! GUIComponent runtime type: " + this.GetType().ToString() + "; children count: " + children.Count.ToString(), Color.Red);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (scrollBarEnabled && !scrollBarHidden) scrollBar.AddToGUIUpdateList();
|
||||
}
|
||||
|
||||
public override Rectangle MouseRect
|
||||
{
|
||||
get
|
||||
{
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
UpdateChildrenRect(deltaTime);
|
||||
|
||||
//base.Update(deltaTime);
|
||||
|
||||
if (scrollBarEnabled && !scrollBarHidden) scrollBar.Update(deltaTime);
|
||||
|
||||
if ((MouseOn == this || MouseOn == scrollBar || IsParentOf(MouseOn)) && PlayerInput.ScrollWheelSpeed != 0)
|
||||
{
|
||||
scrollBar.BarScroll -= (PlayerInput.ScrollWheelSpeed / 500.0f) * BarSize;
|
||||
}
|
||||
}
|
||||
|
||||
public void Select(int childIndex, bool force = false)
|
||||
{
|
||||
if (childIndex >= children.Count || childIndex < 0) return;
|
||||
|
||||
bool wasSelected = true;
|
||||
if (OnSelected != null) wasSelected = OnSelected(children[childIndex], children[childIndex].UserData) || force;
|
||||
|
||||
if (!wasSelected) return;
|
||||
|
||||
if (SelectMultiple)
|
||||
{
|
||||
if (selected.Contains(children[childIndex]))
|
||||
{
|
||||
selected.Remove(children[childIndex]);
|
||||
}
|
||||
else
|
||||
{
|
||||
selected.Add(children[childIndex]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
selected.Clear();
|
||||
selected.Add(children[childIndex]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
selected.Clear();
|
||||
}
|
||||
|
||||
public void UpdateScrollBarSize()
|
||||
{
|
||||
totalSize = 0;
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
if (child == frame) continue;
|
||||
totalSize += (scrollBar.IsHorizontal) ? child.Rect.Width : child.Rect.Height;
|
||||
totalSize += spacing;
|
||||
}
|
||||
|
||||
scrollBar.BarSize = scrollBar.IsHorizontal ?
|
||||
Math.Max(Math.Min((float)rect.Width / (float)totalSize, 1.0f), 5.0f / rect.Width) :
|
||||
Math.Max(Math.Min((float)rect.Height / (float)totalSize, 1.0f), 5.0f / rect.Height);
|
||||
|
||||
scrollBarHidden = scrollBar.BarSize >= 1.0f;
|
||||
}
|
||||
|
||||
public override void AddChild(GUIComponent child)
|
||||
{
|
||||
//temporarily reduce the size of the rect to prevent the child from expanding over the scrollbar
|
||||
if (scrollBar.IsHorizontal)
|
||||
rect.Height -= scrollBar.Rect.Height;
|
||||
else
|
||||
rect.Width -= scrollBar.Rect.Width;
|
||||
|
||||
base.AddChild(child);
|
||||
|
||||
if (scrollBar.IsHorizontal)
|
||||
rect.Height += scrollBar.Rect.Height;
|
||||
else
|
||||
rect.Width += scrollBar.Rect.Width;
|
||||
|
||||
UpdateScrollBarSize();
|
||||
UpdateChildrenRect(0.0f);
|
||||
}
|
||||
|
||||
public override void ClearChildren()
|
||||
{
|
||||
base.ClearChildren();
|
||||
selected.Clear();
|
||||
}
|
||||
|
||||
public override void RemoveChild(GUIComponent child)
|
||||
{
|
||||
base.RemoveChild(child);
|
||||
|
||||
if (selected.Contains(child)) selected.Remove(child);
|
||||
|
||||
UpdateScrollBarSize();
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
frame.Draw(spriteBatch);
|
||||
|
||||
if (!scrollBarHidden) scrollBar.Draw(spriteBatch);
|
||||
|
||||
Rectangle prevScissorRect = spriteBatch.GraphicsDevice.ScissorRectangle;
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = frame.Rect;
|
||||
|
||||
int lastVisible = 0;
|
||||
for (int i = 0; i < children.Count; i++)
|
||||
{
|
||||
GUIComponent child = children[i];
|
||||
if (child == frame || !child.Visible) continue;
|
||||
|
||||
if (!IsChildVisible(child))
|
||||
{
|
||||
if (lastVisible > 0) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
lastVisible = i;
|
||||
child.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = prevScissorRect;
|
||||
}
|
||||
|
||||
private bool IsChildVisible(GUIComponent child)
|
||||
{
|
||||
if (child == null) return false;
|
||||
|
||||
if (scrollBar.IsHorizontal)
|
||||
{
|
||||
if (child.Rect.Right < rect.X) return false;
|
||||
if (child.Rect.X > rect.Right) return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (child.Rect.Bottom < rect.Y) return false;
|
||||
if (child.Rect.Y > rect.Bottom) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class GUIMessage
|
||||
{
|
||||
private ColoredText coloredText;
|
||||
private Vector2 pos;
|
||||
|
||||
private float lifeTime;
|
||||
|
||||
private Vector2 size;
|
||||
|
||||
public string Text
|
||||
{
|
||||
get { return coloredText.Text; }
|
||||
}
|
||||
|
||||
public Color Color
|
||||
{
|
||||
get { return coloredText.Color; }
|
||||
}
|
||||
|
||||
public Vector2 Pos
|
||||
{
|
||||
get { return pos; }
|
||||
set { pos = value; }
|
||||
}
|
||||
|
||||
public Vector2 Size
|
||||
{
|
||||
get { return size; }
|
||||
}
|
||||
|
||||
|
||||
public float LifeTime
|
||||
{
|
||||
get { return lifeTime; }
|
||||
set { lifeTime = value; }
|
||||
}
|
||||
|
||||
public GUIMessage(string text, Color color, Vector2 position, float lifeTime)
|
||||
{
|
||||
coloredText = new ColoredText(text, color);
|
||||
pos = position;
|
||||
this.lifeTime = lifeTime;
|
||||
|
||||
size = GUI.Font.MeasureString(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIMessageBox : GUIFrame
|
||||
{
|
||||
public static List<GUIComponent> MessageBoxes = new List<GUIComponent>();
|
||||
|
||||
const int DefaultWidth=400, DefaultHeight=250;
|
||||
|
||||
//public delegate bool OnClickedHandler(GUIButton button, object obj);
|
||||
//public OnClickedHandler OnClicked;
|
||||
|
||||
//GUIFrame frame;
|
||||
public GUIButton[] Buttons;
|
||||
|
||||
public static GUIComponent VisibleBox
|
||||
{
|
||||
get { return MessageBoxes.Count == 0 ? null : MessageBoxes[0]; }
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get { return (children[0].children[1] as GUITextBlock).Text; }
|
||||
set { (children[0].children[1] as GUITextBlock).Text = value; }
|
||||
}
|
||||
|
||||
public GUIMessageBox(string headerText, string text)
|
||||
: this(headerText, text, new string[] {"OK"})
|
||||
{
|
||||
this.Buttons[0].OnClicked = Close;
|
||||
}
|
||||
|
||||
public GUIMessageBox(string headerText, string text, int width, int height)
|
||||
: this(headerText, text, new string[] { "OK" }, width, height)
|
||||
{
|
||||
this.Buttons[0].OnClicked = Close;
|
||||
}
|
||||
|
||||
public GUIMessageBox(string headerText, string text, string[] buttons, int width = DefaultWidth, int height = DefaultHeight, Alignment textAlignment = Alignment.TopLeft, GUIComponent parent = null)
|
||||
: base(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight),
|
||||
Color.Black * 0.5f, Alignment.TopLeft, null, parent)
|
||||
{
|
||||
if (height == 0)
|
||||
{
|
||||
string wrappedText = ToolBox.WrapText(text, width, GUI.Font);
|
||||
string[] lines = wrappedText.Split('\n');
|
||||
foreach (string line in lines)
|
||||
{
|
||||
height += (int)GUI.Font.MeasureString(line).Y;
|
||||
}
|
||||
height += 220;
|
||||
}
|
||||
|
||||
var frame = new GUIFrame(new Rectangle(0, 0, width, height), null, Alignment.Center, "", this);
|
||||
GUI.Style.Apply(frame, "", this);
|
||||
|
||||
var header = new GUITextBlock(new Rectangle(0, 0, 0, 30), headerText, null, null, textAlignment, "", frame, true);
|
||||
GUI.Style.Apply(header, "", this);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
var textBlock = new GUITextBlock(new Rectangle(0, 30, 0, height - 70), text,
|
||||
null, null, textAlignment, "", frame, true);
|
||||
GUI.Style.Apply(textBlock, "", this);
|
||||
}
|
||||
|
||||
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], Alignment.Left | Alignment.Bottom, "", frame);
|
||||
|
||||
x += this.Buttons[i].Rect.Width + 20;
|
||||
}
|
||||
|
||||
MessageBoxes.Add(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public bool Close(GUIButton button, object obj)
|
||||
{
|
||||
if (parent != null) parent.RemoveChild(this);
|
||||
if (MessageBoxes.Contains(this)) MessageBoxes.Remove(this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void CloseAll()
|
||||
{
|
||||
MessageBoxes.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIProgressBar : GUIComponent
|
||||
{
|
||||
private bool isHorizontal;
|
||||
|
||||
private GUIFrame frame, slider;
|
||||
private float barSize;
|
||||
|
||||
public delegate float ProgressGetterHandler();
|
||||
public ProgressGetterHandler ProgressGetter;
|
||||
|
||||
public bool IsHorizontal
|
||||
{
|
||||
get { return isHorizontal; }
|
||||
set { isHorizontal = value; }
|
||||
}
|
||||
|
||||
public float BarSize
|
||||
{
|
||||
get { return barSize; }
|
||||
set
|
||||
{
|
||||
float oldBarSize = barSize;
|
||||
barSize = MathHelper.Clamp(value, 0.0f, 1.0f);
|
||||
if (barSize != oldBarSize) UpdateRect();
|
||||
}
|
||||
}
|
||||
|
||||
public GUIProgressBar(Rectangle rect, Color color, float barSize, GUIComponent parent = null)
|
||||
: this(rect, color, barSize, (Alignment.Left | Alignment.Top), parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIProgressBar(Rectangle rect, Color color, float barSize, Alignment alignment, GUIComponent parent = null)
|
||||
: this(rect, color, null, barSize, alignment, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public GUIProgressBar(Rectangle rect, Color color, string style, float barSize, Alignment alignment, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
this.color = color;
|
||||
isHorizontal = (rect.Width > rect.Height);
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
frame = new GUIFrame(new Rectangle(0, 0, 0, 0), null, this);
|
||||
GUI.Style.Apply(frame, "", this);
|
||||
|
||||
slider = new GUIFrame(new Rectangle(0, 0, 0, 0), null);
|
||||
GUI.Style.Apply(slider, "Slider", this);
|
||||
|
||||
this.barSize = barSize;
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
/*public override void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
if (frame == null) return;
|
||||
|
||||
frame.Color = style.Color;
|
||||
frame.HoverColor = style.HoverColor;
|
||||
frame.SelectedColor = style.SelectedColor;
|
||||
|
||||
Padding = style.Padding;
|
||||
|
||||
frame.OutlineColor = style.OutlineColor;
|
||||
|
||||
this.style = style;
|
||||
}*/
|
||||
|
||||
private void UpdateRect()
|
||||
{
|
||||
slider.Rect = new Rectangle(
|
||||
(int)(frame.Rect.X + padding.X),
|
||||
(int)(frame.Rect.Y + padding.Y),
|
||||
isHorizontal ? (int)((frame.Rect.Width - padding.X - padding.Z) * barSize) : frame.Rect.Width,
|
||||
isHorizontal ? (int)(frame.Rect.Height - padding.Y - padding.W) : (int)(frame.Rect.Height * barSize));
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
if (ProgressGetter != null) BarSize = ProgressGetter();
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
|
||||
if (slider.sprites != null && slider.sprites[state].Count > 0)
|
||||
{
|
||||
foreach (UISprite uiSprite in slider.sprites[state])
|
||||
{
|
||||
if (uiSprite.Tile)
|
||||
{
|
||||
uiSprite.Sprite.DrawTiled(spriteBatch, slider.Rect.Location.ToVector2(), slider.Rect.Size.ToVector2(), currColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
spriteBatch.Draw(uiSprite.Sprite.Texture,
|
||||
slider.Rect, new Rectangle(
|
||||
uiSprite.Sprite.SourceRect.X,
|
||||
uiSprite.Sprite.SourceRect.Y,
|
||||
(int)(uiSprite.Sprite.SourceRect.Width * (isHorizontal ? barSize : 1.0f)),
|
||||
(int)(uiSprite.Sprite.SourceRect.Height * (isHorizontal ? 1.0f : barSize))),
|
||||
currColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIScrollBar : GUIComponent
|
||||
{
|
||||
public static GUIScrollBar draggingBar;
|
||||
|
||||
private bool isHorizontal;
|
||||
|
||||
private GUIFrame frame;
|
||||
private GUIButton bar;
|
||||
private float barSize;
|
||||
private float barScroll;
|
||||
|
||||
private float step;
|
||||
|
||||
private bool enabled;
|
||||
|
||||
public delegate bool OnMovedHandler(GUIScrollBar scrollBar, float barScroll);
|
||||
public OnMovedHandler OnMoved;
|
||||
|
||||
public bool IsHorizontal
|
||||
{
|
||||
get { return isHorizontal; }
|
||||
set
|
||||
{
|
||||
if (isHorizontal == value) return;
|
||||
isHorizontal = value;
|
||||
UpdateRect();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get { return enabled; }
|
||||
set { enabled = value; }
|
||||
}
|
||||
|
||||
public float BarScroll
|
||||
{
|
||||
get { return step == 0.0f ? barScroll : MathUtils.RoundTowardsClosest(barScroll, step); }
|
||||
set
|
||||
{
|
||||
barScroll = MathHelper.Clamp(value, 0.0f, 1.0f);
|
||||
int newX = bar.Rect.X - frame.Rect.X;
|
||||
int newY = bar.Rect.Y - frame.Rect.Y;
|
||||
|
||||
float newScroll = step == 0.0f ? barScroll : MathUtils.RoundTowardsClosest(barScroll, step);
|
||||
|
||||
if (isHorizontal)
|
||||
{
|
||||
newX = (int)(frame.Padding.X + newScroll * (frame.Rect.Width - bar.Rect.Width - frame.Padding.X - frame.Padding.Z));
|
||||
newX = MathHelper.Clamp(newX, (int)frame.Padding.X, frame.Rect.Width - bar.Rect.Width - (int)frame.Padding.Z);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
newY = (int)(frame.Padding.Y + newScroll * (frame.Rect.Height - bar.Rect.Height - frame.Padding.Y - frame.Padding.W));
|
||||
newY = MathHelper.Clamp(newY, (int)frame.Padding.Y, frame.Rect.Height - bar.Rect.Height - (int)frame.Padding.W);
|
||||
|
||||
}
|
||||
bar.Rect = new Rectangle(newX + frame.Rect.X, newY + frame.Rect.Y, bar.Rect.Width, bar.Rect.Height);
|
||||
}
|
||||
}
|
||||
|
||||
public float Step
|
||||
{
|
||||
get
|
||||
{
|
||||
return step;
|
||||
}
|
||||
set
|
||||
{
|
||||
step = MathHelper.Clamp(value, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
public float BarSize
|
||||
{
|
||||
get { return barSize; }
|
||||
set
|
||||
{
|
||||
float oldBarSize = barSize;
|
||||
barSize = Math.Min(Math.Max(value, 0.0f), 1.0f);
|
||||
if (barSize != oldBarSize) UpdateRect();
|
||||
}
|
||||
}
|
||||
|
||||
public GUIScrollBar(Rectangle rect, string style, float barSize, GUIComponent parent = null)
|
||||
: this(rect, null, barSize, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIScrollBar(Rectangle rect, Color? color, float barSize, string style = "", GUIComponent parent = null)
|
||||
: this(rect, color, barSize, Alignment.TopLeft, style, parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public GUIScrollBar(Rectangle rect, Color? color, float barSize, Alignment alignment, string style = "", GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
//GetDimensions(parent);
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
isHorizontal = (rect.Width > rect.Height);
|
||||
frame = new GUIFrame(new Rectangle(0,0,0,0), style, this);
|
||||
GUI.Style.Apply(frame, isHorizontal ? "GUIFrameHorizontal" : "GUIFrameVertical", this);
|
||||
|
||||
this.barSize = barSize;
|
||||
|
||||
bar = new GUIButton(new Rectangle(0, 0, 0, 0), "", color, "", this);
|
||||
GUI.Style.Apply(bar, isHorizontal ? "GUIButtonHorizontal" : "GUIButtoneVertical", this);
|
||||
|
||||
bar.OnPressed = SelectBar;
|
||||
|
||||
enabled = true;
|
||||
|
||||
UpdateRect();
|
||||
}
|
||||
|
||||
private void UpdateRect()
|
||||
{
|
||||
float width = frame.Rect.Width - frame.Padding.X - frame.Padding.Z;
|
||||
float height = frame.Rect.Height - frame.Padding.Y - frame.Padding.W;
|
||||
|
||||
bar.Rect = new Rectangle(
|
||||
bar.Rect.X,
|
||||
bar.Rect.Y,
|
||||
isHorizontal ? (int)(width * barSize) : (int)width,
|
||||
isHorizontal ? (int)height : (int)(height * barSize));
|
||||
|
||||
ClampRect();
|
||||
|
||||
foreach (GUIComponent child in bar.children)
|
||||
{
|
||||
child.Rect = bar.Rect;
|
||||
}
|
||||
}
|
||||
|
||||
private void ClampRect()
|
||||
{
|
||||
bar.Rect = new Rectangle(
|
||||
(int)MathHelper.Clamp(bar.Rect.X, frame.Rect.X + frame.Padding.X, frame.Rect.Right - bar.Rect.Width - frame.Padding.X - frame.Padding.Z),
|
||||
(int)MathHelper.Clamp(bar.Rect.Y, frame.Rect.Y + frame.Padding.Y, frame.Rect.Bottom - bar.Rect.Height - frame.Padding.Y - frame.Padding.W),
|
||||
bar.Rect.Width,
|
||||
bar.Rect.Height);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
base.Update(deltaTime);
|
||||
|
||||
if (MouseOn == frame)
|
||||
{
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
MoveButton(new Vector2(
|
||||
Math.Sign(PlayerInput.MousePosition.X - bar.Rect.Center.X) * bar.Rect.Width,
|
||||
Math.Sign(PlayerInput.MousePosition.Y - bar.Rect.Center.Y) * bar.Rect.Height));
|
||||
}
|
||||
}
|
||||
|
||||
if (draggingBar == this)
|
||||
{
|
||||
if (!PlayerInput.LeftButtonHeld()) draggingBar = null;
|
||||
MoveButton(PlayerInput.MouseSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
|
||||
private bool SelectBar()
|
||||
{
|
||||
if (!enabled) return false;
|
||||
if (barSize == 1.0f) return false;
|
||||
|
||||
draggingBar = this;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private void MoveButton(Vector2 moveAmount)
|
||||
{
|
||||
if (isHorizontal)
|
||||
{
|
||||
moveAmount.Y = 0.0f;
|
||||
barScroll += moveAmount.X / (frame.Rect.Width - bar.Rect.Width - frame.Padding.X - frame.Padding.Z);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveAmount.X = 0.0f;
|
||||
barScroll += moveAmount.Y / (frame.Rect.Height - bar.Rect.Height - frame.Padding.Y - frame.Padding.W);
|
||||
}
|
||||
|
||||
BarScroll = barScroll;
|
||||
|
||||
if (moveAmount != Vector2.Zero && OnMoved != null) OnMoved(this, BarScroll);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.Xml.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUIStyle
|
||||
{
|
||||
private Dictionary<string, GUIComponentStyle> componentStyles;
|
||||
|
||||
public GUIStyle(string file)
|
||||
{
|
||||
componentStyles = new Dictionary<string, GUIComponentStyle>();
|
||||
|
||||
XDocument doc;
|
||||
try
|
||||
{
|
||||
ToolBox.IsProperFilenameCase(file);
|
||||
doc = XDocument.Load(file);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (XElement subElement in doc.Root.Elements())
|
||||
{
|
||||
GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
|
||||
componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle);
|
||||
}
|
||||
}
|
||||
|
||||
public void Apply(GUIComponent targetComponent, string styleName = "", GUIComponent parent = null)
|
||||
{
|
||||
GUIComponentStyle componentStyle = null;
|
||||
if (parent != null)
|
||||
{
|
||||
|
||||
GUIComponentStyle parentStyle = parent.Style;
|
||||
|
||||
if (parent.Style == null)
|
||||
{
|
||||
string parentStyleName = parent.GetType().Name.ToLowerInvariant();
|
||||
|
||||
if (!componentStyles.TryGetValue(parentStyleName, out parentStyle))
|
||||
{
|
||||
DebugConsole.ThrowError("Couldn't find a GUI style \""+ parentStyleName + "\"");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
string childStyleName = string.IsNullOrEmpty(styleName) ? targetComponent.GetType().Name : styleName;
|
||||
parentStyle.ChildStyles.TryGetValue(childStyleName.ToLowerInvariant(), out componentStyle);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(styleName))
|
||||
{
|
||||
styleName = targetComponent.GetType().Name;
|
||||
}
|
||||
if (!componentStyles.TryGetValue(styleName.ToLowerInvariant(), out componentStyle))
|
||||
{
|
||||
DebugConsole.ThrowError("Couldn't find a GUI style \""+ styleName+"\"");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
targetComponent.ApplyStyle(componentStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUITextBlock : GUIComponent
|
||||
{
|
||||
protected string text;
|
||||
|
||||
protected Alignment textAlignment;
|
||||
|
||||
private float textScale;
|
||||
|
||||
protected Vector2 textPos;
|
||||
protected Vector2 origin;
|
||||
|
||||
protected Vector2 caretPos;
|
||||
|
||||
protected Color textColor;
|
||||
|
||||
private string wrappedText;
|
||||
|
||||
public delegate string TextGetterHandler();
|
||||
public TextGetterHandler TextGetter;
|
||||
|
||||
public bool Wrap;
|
||||
|
||||
private bool overflowClipActive;
|
||||
public bool OverflowClip;
|
||||
|
||||
private float textDepth;
|
||||
|
||||
public override Vector4 Padding
|
||||
{
|
||||
get { return padding; }
|
||||
set
|
||||
{
|
||||
padding = value;
|
||||
SetTextPos();
|
||||
}
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get { return text; }
|
||||
set
|
||||
{
|
||||
if (Text == value) return;
|
||||
|
||||
text = value;
|
||||
wrappedText = value;
|
||||
SetTextPos();
|
||||
}
|
||||
}
|
||||
|
||||
public string WrappedText
|
||||
{
|
||||
get { return wrappedText; }
|
||||
}
|
||||
|
||||
public override Rectangle Rect
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Rect;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (base.Rect == value) return;
|
||||
foreach (GUIComponent child in children)
|
||||
{
|
||||
child.Rect = new Rectangle(child.Rect.X + value.X - rect.X, child.Rect.Y + value.Y - rect.Y, child.Rect.Width, child.Rect.Height);
|
||||
}
|
||||
|
||||
if (value.Width != rect.Width || value.Height != rect.Height)
|
||||
{
|
||||
SetTextPos();
|
||||
}
|
||||
|
||||
rect = value;
|
||||
}
|
||||
}
|
||||
|
||||
public float TextDepth
|
||||
{
|
||||
get { return textDepth; }
|
||||
set { textDepth = MathHelper.Clamp(value, 0.0f, 1.0f); }
|
||||
}
|
||||
|
||||
public Vector2 TextPos
|
||||
{
|
||||
get { return textPos; }
|
||||
}
|
||||
|
||||
public float TextScale
|
||||
{
|
||||
get { return textScale; }
|
||||
set
|
||||
{
|
||||
if (value != textScale)
|
||||
{
|
||||
textScale = value;
|
||||
SetTextPos();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Origin
|
||||
{
|
||||
get { return origin; }
|
||||
}
|
||||
|
||||
public Color TextColor
|
||||
{
|
||||
get { return textColor; }
|
||||
set { textColor = value; }
|
||||
}
|
||||
|
||||
public Vector2 CaretPos
|
||||
{
|
||||
get { return caretPos; }
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, string style, GUIComponent parent, ScalableFont font)
|
||||
: this(rect, text, style, Alignment.TopLeft, Alignment.TopLeft, parent, false, font)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, string style, GUIComponent parent = null, bool wrap = false)
|
||||
: this(rect, text, style, Alignment.TopLeft, Alignment.TopLeft, parent, wrap)
|
||||
{
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color? color, Color? textColor, Alignment textAlignment = Alignment.Left, string style = null, GUIComponent parent = null, bool wrap = false)
|
||||
: this(rect, text,color, textColor, Alignment.TopLeft, textAlignment, style, parent, wrap)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void UpdateDimensions(GUIComponent parent = null)
|
||||
{
|
||||
base.UpdateDimensions(parent);
|
||||
|
||||
SetTextPos();
|
||||
}
|
||||
|
||||
public override void ApplyStyle(GUIComponentStyle style)
|
||||
{
|
||||
if (style == null) return;
|
||||
base.ApplyStyle(style);
|
||||
|
||||
textColor = style.textColor;
|
||||
}
|
||||
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, Color? color, Color? textColor, Alignment alignment, Alignment textAlignment = Alignment.Left, string style = null, GUIComponent parent = null, bool wrap = false, ScalableFont font = null)
|
||||
: this (rect, text, style, alignment, textAlignment, parent, wrap, font)
|
||||
{
|
||||
if (color != null) this.color = (Color)color;
|
||||
if (textColor != null) this.textColor = (Color)textColor;
|
||||
}
|
||||
|
||||
public GUITextBlock(Rectangle rect, string text, string style, Alignment alignment = Alignment.TopLeft, Alignment textAlignment = Alignment.TopLeft, GUIComponent parent = null, bool wrap = false, ScalableFont font = null)
|
||||
: base(style)
|
||||
{
|
||||
this.Font = font == null ? GUI.Font : font;
|
||||
|
||||
this.rect = rect;
|
||||
|
||||
this.text = text;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
this.padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
|
||||
|
||||
this.textAlignment = textAlignment;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
this.Wrap = wrap;
|
||||
|
||||
SetTextPos();
|
||||
|
||||
TextScale = 1.0f;
|
||||
|
||||
if (rect.Height == 0 && !string.IsNullOrEmpty(Text))
|
||||
{
|
||||
this.rect.Height = (int)Font.MeasureString(wrappedText).Y;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetTextPos()
|
||||
{
|
||||
if (text == null) return;
|
||||
|
||||
overflowClipActive = false;
|
||||
|
||||
wrappedText = text;
|
||||
|
||||
Vector2 size = MeasureText(text);
|
||||
|
||||
if (Wrap && rect.Width > 0)
|
||||
{
|
||||
wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font, textScale);
|
||||
size = MeasureText(wrappedText);
|
||||
}
|
||||
else if (OverflowClip)
|
||||
{
|
||||
overflowClipActive = size.X > rect.Width;
|
||||
}
|
||||
|
||||
textPos = new Vector2(rect.Width / 2.0f, rect.Height / 2.0f);
|
||||
origin = size * 0.5f;
|
||||
|
||||
if (textAlignment.HasFlag(Alignment.Left) && !overflowClipActive)
|
||||
origin.X += (rect.Width / 2.0f - padding.X) - size.X / 2;
|
||||
|
||||
if (textAlignment.HasFlag(Alignment.Right) || overflowClipActive)
|
||||
origin.X -= (rect.Width / 2.0f - padding.Z) - size.X / 2;
|
||||
|
||||
if (textAlignment.HasFlag(Alignment.Top))
|
||||
origin.Y += (rect.Height / 2.0f - padding.Y) - size.Y / 2;
|
||||
|
||||
if (textAlignment.HasFlag(Alignment.Bottom))
|
||||
origin.Y -= (rect.Height / 2.0f - padding.W) - size.Y / 2;
|
||||
|
||||
origin.X = (int)origin.X;
|
||||
origin.Y = (int)origin.Y;
|
||||
|
||||
textPos.X = (int)textPos.X;
|
||||
textPos.Y = (int)textPos.Y;
|
||||
|
||||
if (wrappedText.Contains("\n"))
|
||||
{
|
||||
string[] lines = wrappedText.Split('\n');
|
||||
Vector2 lastLineSize = MeasureText(lines[lines.Length-1]);
|
||||
caretPos = new Vector2(rect.X + lastLineSize.X, rect.Y + size.Y - lastLineSize.Y) + textPos - origin;
|
||||
}
|
||||
else
|
||||
{
|
||||
caretPos = new Vector2(rect.X + size.X, rect.Y) + textPos - origin;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Vector2 MeasureText(string text)
|
||||
{
|
||||
if (Font == null) return Vector2.Zero;
|
||||
|
||||
Vector2 size = Vector2.Zero;
|
||||
while (size == Vector2.Zero)
|
||||
{
|
||||
try { size = Font.MeasureString((text == "") ? " " : text); }
|
||||
catch { text = text.Substring(0, text.Length - 1); }
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
Draw(spriteBatch, Vector2.Zero);
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, Vector2 offset)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
Color currColor = color;
|
||||
if (state == ComponentState.Hover) currColor = hoverColor;
|
||||
if (state == ComponentState.Selected) currColor = selectedColor;
|
||||
|
||||
Rectangle drawRect = rect;
|
||||
if (offset != Vector2.Zero) drawRect.Location += offset.ToPoint();
|
||||
|
||||
base.Draw(spriteBatch);
|
||||
|
||||
if (TextGetter != null) Text = TextGetter();
|
||||
|
||||
Rectangle prevScissorRect = spriteBatch.GraphicsDevice.ScissorRectangle;
|
||||
if (overflowClipActive)
|
||||
{
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = rect;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
Font.DrawString(spriteBatch,
|
||||
Wrap ? wrappedText : text,
|
||||
new Vector2(rect.X, rect.Y) + textPos + offset,
|
||||
textColor * (textColor.A / 255.0f),
|
||||
0.0f, origin, TextScale,
|
||||
SpriteEffects.None, textDepth);
|
||||
}
|
||||
|
||||
if (overflowClipActive)
|
||||
{
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = prevScissorRect;
|
||||
}
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
if (OutlineColor.A * currColor.A > 0.0f) GUI.DrawRectangle(spriteBatch, rect, OutlineColor * (currColor.A / 255.0f), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
using System;
|
||||
using EventInput;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
|
||||
delegate void TextBoxEvent(GUITextBox sender, Keys key);
|
||||
|
||||
class GUITextBox : GUIComponent, IKeyboardSubscriber
|
||||
{
|
||||
public event TextBoxEvent OnSelected;
|
||||
|
||||
bool caretVisible;
|
||||
float caretTimer;
|
||||
|
||||
GUITextBlock textBlock;
|
||||
|
||||
public delegate bool OnEnterHandler(GUITextBox textBox, string text);
|
||||
public OnEnterHandler OnEnterPressed;
|
||||
|
||||
public event TextBoxEvent OnKeyHit;
|
||||
|
||||
public delegate bool OnTextChangedHandler(GUITextBox textBox, string text);
|
||||
public OnTextChangedHandler OnTextChanged;
|
||||
|
||||
public bool CaretEnabled;
|
||||
|
||||
private int? maxTextLength;
|
||||
|
||||
public GUITextBlock.TextGetterHandler TextGetter
|
||||
{
|
||||
get { return textBlock.TextGetter; }
|
||||
set { textBlock.TextGetter = value; }
|
||||
}
|
||||
|
||||
public bool Wrap
|
||||
{
|
||||
get { return textBlock.Wrap; }
|
||||
set { textBlock.Wrap = value; }
|
||||
}
|
||||
|
||||
public int? MaxTextLength
|
||||
{
|
||||
get { return maxTextLength; }
|
||||
set
|
||||
{
|
||||
textBlock.OverflowClip = true;
|
||||
maxTextLength = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public override string ToolTip
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.ToolTip;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.ToolTip = value;
|
||||
textBlock.ToolTip = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override ScalableFont Font
|
||||
{
|
||||
set
|
||||
{
|
||||
base.Font = value;
|
||||
if (textBlock == null) return;
|
||||
textBlock.Font = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Color Color
|
||||
{
|
||||
get { return color; }
|
||||
set
|
||||
{
|
||||
color = value;
|
||||
textBlock.Color = color;
|
||||
}
|
||||
}
|
||||
|
||||
public Color TextColor
|
||||
{
|
||||
get { return textBlock.TextColor; }
|
||||
set { textBlock.TextColor = value; }
|
||||
}
|
||||
|
||||
public override Color HoverColor
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.HoverColor;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.HoverColor = value;
|
||||
textBlock.HoverColor = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Rectangle Rect
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Rect;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Rect = value;
|
||||
|
||||
textBlock.Rect = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get
|
||||
{
|
||||
return textBlock.Text;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (textBlock.Text == value) return;
|
||||
|
||||
textBlock.Text = value;
|
||||
if (textBlock.Text == null) textBlock.Text = "";
|
||||
|
||||
if (textBlock.Text != "")
|
||||
{
|
||||
if (!Wrap)
|
||||
{
|
||||
if (maxTextLength != null)
|
||||
{
|
||||
if (Text.Length > maxTextLength)
|
||||
{
|
||||
Text = textBlock.Text.Substring(0, (int)maxTextLength);
|
||||
}
|
||||
}
|
||||
else if (Font.MeasureString(textBlock.Text).X > (int)(textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z))
|
||||
{
|
||||
Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GUITextBox(Rectangle rect, string style = null, GUIComponent parent = null)
|
||||
: this(rect, null, null, Alignment.Left, Alignment.Left, style, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public GUITextBox(Rectangle rect, Alignment alignment = Alignment.Left, string 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.CenterLeft, string style = null, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
Enabled = true;
|
||||
|
||||
this.rect = rect;
|
||||
|
||||
if (color != null) this.color = (Color)color;
|
||||
|
||||
this.alignment = alignment;
|
||||
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
|
||||
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), "", color, textColor, textAlignment, style, this);
|
||||
|
||||
Font = GUI.Font;
|
||||
|
||||
GUI.Style.Apply(textBlock, style == "" ? "GUITextBox" : style);
|
||||
textBlock.Padding = new Vector4(3.0f, 0.0f, 3.0f, 0.0f);
|
||||
|
||||
CaretEnabled = true;
|
||||
}
|
||||
|
||||
public void Select()
|
||||
{
|
||||
Selected = true;
|
||||
keyboardDispatcher.Subscriber = this;
|
||||
//if (Clicked != null) Clicked(this);
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
Selected = false;
|
||||
if (keyboardDispatcher.Subscriber == this) keyboardDispatcher.Subscriber = null;
|
||||
}
|
||||
|
||||
public override void Flash(Color? color = null)
|
||||
{
|
||||
textBlock.Flash(color);
|
||||
}
|
||||
|
||||
//MouseState previousMouse;
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
if (flashTimer > 0.0f) flashTimer -= deltaTime;
|
||||
if (!Enabled) return;
|
||||
|
||||
if (rect.Contains(PlayerInput.MousePosition) && Enabled &&
|
||||
(MouseOn == null || MouseOn == this || IsParentOf(MouseOn) || MouseOn.IsParentOf(this)))
|
||||
{
|
||||
|
||||
state = ComponentState.Hover;
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Select();
|
||||
if (OnSelected != null) OnSelected(this, Keys.None);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
state = ComponentState.None;
|
||||
}
|
||||
|
||||
|
||||
if (CaretEnabled)
|
||||
{
|
||||
caretTimer += deltaTime;
|
||||
caretVisible = ((caretTimer * 1000.0f) % 1000) < 500;
|
||||
}
|
||||
|
||||
if (keyboardDispatcher.Subscriber == this)
|
||||
{
|
||||
state = ComponentState.Selected;
|
||||
Character.DisableControls = true;
|
||||
if (OnEnterPressed != null && PlayerInput.KeyHit(Keys.Enter))
|
||||
{
|
||||
string input = Text;
|
||||
Text = "";
|
||||
OnEnterPressed(this, input);
|
||||
}
|
||||
#if LINUX
|
||||
else if (PlayerInput.KeyHit(Keys.Back) && Text.Length>0)
|
||||
{
|
||||
Text = Text.Substring(0, Text.Length-1);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
textBlock.State = state;
|
||||
textBlock.Update(deltaTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
|
||||
if (!CaretEnabled) return;
|
||||
|
||||
Vector2 caretPos = textBlock.CaretPos;
|
||||
|
||||
if (caretVisible && Selected)
|
||||
{
|
||||
GUI.DrawLine(spriteBatch,
|
||||
new Vector2((int)caretPos.X + 2, caretPos.Y + 3),
|
||||
new Vector2((int)caretPos.X + 2, caretPos.Y + Font.MeasureString("I").Y - 3),
|
||||
textBlock.TextColor * (textBlock.TextColor.A / 255.0f));
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceiveTextInput(char inputChar)
|
||||
{
|
||||
Text = Text + inputChar;
|
||||
|
||||
if (OnTextChanged!=null) OnTextChanged(this, Text);
|
||||
}
|
||||
public void ReceiveTextInput(string text)
|
||||
{
|
||||
Text = Text + text;
|
||||
|
||||
if (OnTextChanged != null) OnTextChanged(this, Text);
|
||||
}
|
||||
public void ReceiveCommandInput(char command)
|
||||
{
|
||||
if (Text == null) Text = "";
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case '\b': //backspace
|
||||
if (Text.Length > 0) Text = Text.Substring(0, Text.Length - 1);
|
||||
break;
|
||||
//case '\r': //return
|
||||
// if (OnEnterPressed != null)
|
||||
// OnEnterPressed(this);
|
||||
// break;
|
||||
//case '\t': //tab
|
||||
// if (OnTabPressed != null)
|
||||
// OnTabPressed(this);
|
||||
// break;
|
||||
}
|
||||
|
||||
if (OnTextChanged != null) OnTextChanged(this, Text);
|
||||
}
|
||||
|
||||
public void ReceiveSpecialInput(Keys key)
|
||||
{
|
||||
if (OnKeyHit != null) OnKeyHit(this, key);
|
||||
}
|
||||
|
||||
//public event TextBoxEvent OnTabPressed;
|
||||
|
||||
public bool Selected
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class GUITickBox : GUIComponent
|
||||
{
|
||||
GUIFrame box;
|
||||
GUITextBlock text;
|
||||
|
||||
public delegate bool OnSelectedHandler(GUITickBox obj);
|
||||
public OnSelectedHandler OnSelected;
|
||||
|
||||
private bool selected;
|
||||
|
||||
public bool Selected
|
||||
{
|
||||
get { return selected; }
|
||||
set
|
||||
{
|
||||
if (value == selected) return;
|
||||
selected = value;
|
||||
state = (selected) ? ComponentState.Selected : ComponentState.None;
|
||||
}
|
||||
}
|
||||
|
||||
private bool enabled;
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
set
|
||||
{
|
||||
enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override Rectangle Rect
|
||||
{
|
||||
get
|
||||
{
|
||||
return rect;
|
||||
}
|
||||
set
|
||||
{
|
||||
base.Rect = value;
|
||||
|
||||
box.Rect = new Rectangle(value.X,value.Y,box.Rect.Width,box.Rect.Height);
|
||||
text.Rect = new Rectangle(box.Rect.Right + 10, box.Rect.Y + 2, 20, box.Rect.Height);
|
||||
}
|
||||
}
|
||||
|
||||
public Color TextColor
|
||||
{
|
||||
get { return text.TextColor; }
|
||||
set { text.TextColor = value; }
|
||||
}
|
||||
|
||||
public override Rectangle MouseRect
|
||||
{
|
||||
get { return box.Rect; }
|
||||
}
|
||||
|
||||
public override ScalableFont Font
|
||||
{
|
||||
get
|
||||
{
|
||||
return base.Font;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
base.Font = value;
|
||||
if (text != null) text.Font = value;
|
||||
}
|
||||
}
|
||||
|
||||
public GUITickBox(Rectangle rect, string label, Alignment alignment, GUIComponent parent)
|
||||
: this(rect, label, alignment, GUI.Font, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUITickBox(Rectangle rect, string label, Alignment alignment, ScalableFont font, GUIComponent parent)
|
||||
: base(null)
|
||||
{
|
||||
if (parent != null)
|
||||
parent.AddChild(this);
|
||||
|
||||
box = new GUIFrame(rect, Color.DarkGray, "", this);
|
||||
box.HoverColor = Color.Gray;
|
||||
box.SelectedColor = Color.DarkGray;
|
||||
box.CanBeFocused = false;
|
||||
|
||||
GUI.Style.Apply(box, "GUITickBox");
|
||||
|
||||
text = new GUITextBlock(new Rectangle(rect.Right, rect.Y, 20, rect.Height), label, "", Alignment.TopLeft, Alignment.Left | Alignment.CenterY, this, false, font);
|
||||
GUI.Style.Apply(text, "GUIButtonHorizontal", this);
|
||||
|
||||
this.rect = new Rectangle(box.Rect.X, box.Rect.Y, 240, rect.Height);
|
||||
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible || !Enabled) return;
|
||||
|
||||
if (MouseOn == this)
|
||||
{
|
||||
box.State = ComponentState.Hover;
|
||||
|
||||
if (PlayerInput.LeftButtonHeld())
|
||||
{
|
||||
box.State = ComponentState.Selected;
|
||||
}
|
||||
|
||||
if (PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Selected = !Selected;
|
||||
if (OnSelected != null) OnSelected(this);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
box.State = ComponentState.None;
|
||||
}
|
||||
|
||||
if (selected)
|
||||
{
|
||||
box.State = ComponentState.Selected;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
DrawChildren(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Microsoft.Xna.Framework.Media;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class LoadingScreen
|
||||
{
|
||||
private Texture2D backgroundTexture,monsterTexture,titleTexture;
|
||||
|
||||
readonly RenderTarget2D renderTarget;
|
||||
|
||||
float state;
|
||||
|
||||
public Vector2 CenterPosition;
|
||||
|
||||
public Vector2 TitlePosition;
|
||||
|
||||
private float? loadState;
|
||||
#if !LINUX
|
||||
Video splashScreenVideo;
|
||||
VideoPlayer videoPlayer;
|
||||
#endif
|
||||
public Vector2 TitleSize
|
||||
{
|
||||
get { return new Vector2(titleTexture.Width, titleTexture.Height); }
|
||||
}
|
||||
|
||||
public float Scale
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public float? LoadState
|
||||
{
|
||||
get { return loadState; }
|
||||
set
|
||||
{
|
||||
loadState = value;
|
||||
DrawLoadingText = true;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DrawLoadingText
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public LoadingScreen(GraphicsDevice graphics)
|
||||
{
|
||||
#if !LINUX
|
||||
|
||||
if (GameMain.Config.EnableSplashScreen)
|
||||
{
|
||||
try
|
||||
{
|
||||
splashScreenVideo = GameMain.Instance.Content.Load<Video>("utg_4");
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.ThrowError("Failed to load splashscreen", e);
|
||||
GameMain.Config.EnableSplashScreen = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
backgroundTexture = TextureLoader.FromFile("Content/UI/titleBackground.png");
|
||||
monsterTexture = TextureLoader.FromFile("Content/UI/titleMonster.png");
|
||||
titleTexture = TextureLoader.FromFile("Content/UI/titleText.png");
|
||||
|
||||
renderTarget = new RenderTarget2D(graphics, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
|
||||
|
||||
DrawLoadingText = true;
|
||||
}
|
||||
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphics, float deltaTime)
|
||||
{
|
||||
#if !LINUX
|
||||
if (GameMain.Config.EnableSplashScreen && splashScreenVideo != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
DrawSplashScreen(spriteBatch);
|
||||
if (videoPlayer != null && videoPlayer.State == MediaState.Playing)
|
||||
return;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.ThrowError("Playing splash screen video failed", e);
|
||||
GameMain.Config.EnableSplashScreen = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
drawn = true;
|
||||
|
||||
graphics.SetRenderTarget(renderTarget);
|
||||
|
||||
Scale = GameMain.GraphicsHeight/1500.0f;
|
||||
|
||||
state += deltaTime;
|
||||
|
||||
if (DrawLoadingText)
|
||||
{
|
||||
CenterPosition = new Vector2(GameMain.GraphicsWidth*0.3f, GameMain.GraphicsHeight/2.0f);
|
||||
TitlePosition = CenterPosition + new Vector2(-0.0f + (float)Math.Sqrt(state) * 220.0f, 0.0f) * Scale;
|
||||
TitlePosition.X = Math.Min(TitlePosition.X, (float)GameMain.GraphicsWidth / 2.0f);
|
||||
}
|
||||
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
||||
graphics.Clear(Color.Black);
|
||||
|
||||
spriteBatch.Draw(backgroundTexture, CenterPosition, null, Color.White * Math.Min(state / 5.0f, 1.0f), 0.0f,
|
||||
new Vector2(backgroundTexture.Width / 2.0f, backgroundTexture.Height / 2.0f),
|
||||
Scale*1.5f, SpriteEffects.None, 0.2f);
|
||||
|
||||
spriteBatch.Draw(monsterTexture,
|
||||
CenterPosition + new Vector2((state % 40) * 100.0f - 1800.0f, (state % 40) * 30.0f - 200.0f) * Scale, null,
|
||||
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0.1f);
|
||||
|
||||
spriteBatch.Draw(titleTexture,
|
||||
TitlePosition, null,
|
||||
Color.White * Math.Min((state - 1.0f) / 5.0f, 1.0f), 0.0f, new Vector2(titleTexture.Width / 2.0f, titleTexture.Height / 2.0f), Scale, SpriteEffects.None, 0.0f);
|
||||
|
||||
spriteBatch.End();
|
||||
|
||||
graphics.SetRenderTarget(null);
|
||||
|
||||
if (Hull.renderer != null)
|
||||
{
|
||||
Hull.renderer.ScrollWater(deltaTime);
|
||||
Hull.renderer.RenderBack(spriteBatch, renderTarget, 0.0f);
|
||||
}
|
||||
|
||||
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
||||
|
||||
spriteBatch.Draw(titleTexture,
|
||||
TitlePosition, null,
|
||||
Color.White * Math.Min((state - 3.0f) / 5.0f, 1.0f), 0.0f, new Vector2(titleTexture.Width / 2.0f, titleTexture.Height / 2.0f), Scale, SpriteEffects.None, 0.0f);
|
||||
|
||||
if (DrawLoadingText)
|
||||
{
|
||||
string loadText = "";
|
||||
if (loadState == 100.0f)
|
||||
{
|
||||
loadText = "Press any key to continue";
|
||||
}
|
||||
else
|
||||
{
|
||||
loadText = "Loading... ";
|
||||
if (loadState!=null)
|
||||
{
|
||||
loadText += (int)loadState + " %";
|
||||
}
|
||||
}
|
||||
|
||||
if (GUI.LargeFont!=null)
|
||||
{
|
||||
GUI.LargeFont.DrawString(spriteBatch, loadText,
|
||||
new Vector2(GameMain.GraphicsWidth/2.0f - GUI.LargeFont.MeasureString(loadText).X/2.0f, GameMain.GraphicsHeight*0.8f),
|
||||
Color.White);
|
||||
}
|
||||
|
||||
}
|
||||
spriteBatch.End();
|
||||
|
||||
}
|
||||
|
||||
#if !LINUX
|
||||
private void DrawSplashScreen(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (videoPlayer == null)
|
||||
{
|
||||
videoPlayer = new VideoPlayer();
|
||||
videoPlayer.Play(splashScreenVideo);
|
||||
videoPlayer.Volume = GameMain.Config.SoundVolume;
|
||||
}
|
||||
else
|
||||
{
|
||||
Texture2D videoTexture = null;
|
||||
|
||||
if (videoPlayer.State == MediaState.Stopped)
|
||||
{
|
||||
videoPlayer.Dispose();
|
||||
videoPlayer = null;
|
||||
|
||||
splashScreenVideo.Dispose();
|
||||
splashScreenVideo = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
videoTexture = videoPlayer.GetTexture();
|
||||
|
||||
spriteBatch.Begin();
|
||||
spriteBatch.Draw(videoTexture, new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.White);
|
||||
spriteBatch.End();
|
||||
|
||||
if (PlayerInput.KeyHit(Keys.Space) || PlayerInput.KeyHit(Keys.Enter) || PlayerInput.LeftButtonDown())
|
||||
{
|
||||
videoPlayer.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool drawn;
|
||||
public IEnumerable<object> DoLoading(IEnumerable<object> loader)
|
||||
{
|
||||
drawn = false;
|
||||
LoadState = null;
|
||||
|
||||
while (!drawn)
|
||||
{
|
||||
yield return CoroutineStatus.Running;
|
||||
}
|
||||
|
||||
CoroutineManager.StartCoroutine(loader);
|
||||
|
||||
yield return CoroutineStatus.Running;
|
||||
|
||||
while (CoroutineManager.IsCoroutineRunning(loader.ToString()))
|
||||
{
|
||||
yield return CoroutineStatus.Running;
|
||||
}
|
||||
|
||||
loadState = 100.0f;
|
||||
|
||||
yield return CoroutineStatus.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user