1
0
Fork 0

Ajout d'elements graphiques importants

This commit is contained in:
sheychen 2016-10-12 14:37:17 +02:00
parent ba9d086ea8
commit 2599564de5
16 changed files with 617 additions and 233 deletions

View File

@ -11,7 +11,7 @@ with.
<!--
Modify this string to change the font that will be imported.
-->
<FontName>editundo</FontName>
<FontName>Trebuchet MS</FontName>
<!--
Size is a float value, measured in points. Modify this value to change

View File

@ -0,0 +1,43 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Galactic_Colors_Control_GUI.GUI
{
class Box : Element
{
protected boxSprites _backSprites;
protected Colors _colors;
public Box() { }
public Box(Rectangle pos, boxSprites backSprites, Colors colors)
{
_pos = pos;
_backSprites = backSprites;
_colors = colors;
}
public override void Draw(SpriteBatch spriteBatch)
{
Color backColor = _isFocus ? _colors._focus : (_isHover ? _colors._hover : _colors._normal);
int leftWidth = _backSprites.topLeft.Width;
int rightWidth = _backSprites.topRight.Width;
int centerWidth = _pos.Width - leftWidth - rightWidth;
int topHeight = _backSprites.topLeft.Height;
int bottomHeight = _backSprites.bottomLeft.Height;
int centerHeight = _pos.Height - topHeight - bottomHeight;
spriteBatch.Draw(_backSprites.topLeft, new Rectangle(_pos.X, _pos.Y, leftWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.topCenter, new Rectangle(_pos.X + leftWidth, _pos.Y, centerWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.topRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y, rightWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.centerLeft, new Rectangle(_pos.X, _pos.Y + topHeight, leftWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.centerCenter, new Rectangle(_pos.X + leftWidth, _pos.Y + topHeight, centerWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.centerRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y + topHeight, rightWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.bottomLeft, new Rectangle(_pos.X, _pos.Y + _pos.Height - bottomHeight, leftWidth, bottomHeight), backColor);
spriteBatch.Draw(_backSprites.bottomCenter, new Rectangle(_pos.X + leftWidth, _pos.Y + _pos.Height - bottomHeight, centerWidth, bottomHeight), backColor);
spriteBatch.Draw(_backSprites.bottomRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y + _pos.Height - bottomHeight, rightWidth, bottomHeight), backColor);
}
}
}

View File

@ -0,0 +1,52 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Input;
namespace Galactic_Colors_Control_GUI.GUI
{
class BoxButton : Box
{
protected Button _button;
private int _unFocusTime;
public BoxButton() { }
public BoxButton(Rectangle pos, boxSprites backSprites, Colors colors, EventHandler click = null)
{
_pos = pos;
_backSprites = backSprites;
_colors = colors;
_button = new Button(pos, click);
}
public override void Update(int x, int y, Mouse mouse, Keys key, bool isMaj, EventArgs e)
{
base.Update(x, y, mouse, key, isMaj, e);
_button.Update(x, y, mouse, key, isMaj, e);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
if (_isFocus)
{
if (_unFocusTime < 10)
{
_unFocusTime++;
}
else {
_isFocus = false;
_unFocusTime = 0;
}
}
_button.Draw(spriteBatch);
}
}
}

View File

@ -0,0 +1,38 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galactic_Colors_Control_GUI.GUI
{
class BoxLabel : Box
{
protected Label _label;
public BoxLabel() { }
public BoxLabel(Rectangle pos, boxSprites backSprites, Colors colors, string text, SpriteFont font, Colors textColors, Label.textAlign align = Label.textAlign.centerCenter)
{
_pos = pos;
_backSprites = backSprites;
_colors = colors;
_label = new Label(pos, text, font, textColors, align);
}
public override void Update(int x, int y, Mouse mouse, Keys key, bool isMaj, EventArgs e)
{
base.Update(x, y, mouse, key, isMaj, e);
_label.Update(x, y, mouse, key, isMaj, e);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
_label.Draw(spriteBatch);
}
}
}

View File

@ -0,0 +1,52 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galactic_Colors_Control_GUI.GUI
{
class BoxLabelButton : BoxLabel
{
protected Button _button;
private int _unFocusTime;
public BoxLabelButton(Rectangle pos, boxSprites backSprites, Colors colors, string text, SpriteFont font, Colors textColors, Label.textAlign align = Label.textAlign.centerCenter, EventHandler click = null)
{
_pos = pos;
_backSprites = backSprites;
_colors = colors;
_button = new Button(pos, click);
_label = new Label(pos, text, font , textColors, align);
}
public override void Update(int x, int y, Mouse mouse, Keys key, bool isMaj, EventArgs e)
{
base.Update(x, y, mouse, key, isMaj, e);
_button.Update(x, y, mouse, key, isMaj, e);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
if (_isFocus)
{
if (_unFocusTime < 10)
{
_unFocusTime++;
}
else {
_isFocus = false;
_label._isFocus = false;
_unFocusTime = 0;
}
}
_button.Draw(spriteBatch);
}
}
}

View File

@ -6,86 +6,14 @@ namespace Galactic_Colors_Control_GUI.GUI
{
class Button : Element
{
protected string _text;
protected SpriteFont _font;
protected Color _backColor, _backColorHover, _backColorFocus, _textColor, _textColorHover, _textColorFocus;
protected event EventHandler _click;
Texture2D _backSprite;
protected int _unFocusTime = 0;
public Button() { }
public Button(Rectangle pos, Color backColor, EventHandler click = null)
public Button(Rectangle pos, EventHandler click = null)
{
_pos = pos;
_backColor = backColor;
_backColorHover = backColor;
_backColorFocus = backColor;
_backSprite = Game1.nullSprite;
_click = click;
}
public Button(Rectangle pos, Color backColor, Color backColorHover, EventHandler click = null)
{
_pos = pos;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorHover;
_backSprite = Game1.nullSprite;
_click = click;
}
public Button(Rectangle pos, Color backColor, Color backColorHover, Color backColorFocus, EventHandler click = null)
{
_pos = pos;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorFocus;
_backSprite = Game1.nullSprite;
_click = click;
}
public Button(Rectangle pos, Color backColor, string text, SpriteFont font, Color textColor, EventHandler click = null)
{
_pos = pos;
_font = font;
_backColor = backColor;
_backColorHover = backColor;
_backColorFocus = backColor;
_textColor = textColor;
_textColorHover = textColor;
_textColorFocus = textColor;
_text = text;
_backSprite = Game1.nullSprite;
_click = click;
}
public Button(Rectangle pos, Color backColor, Color backColorHover, string text, SpriteFont font, Color textColor, Color textColorHover, EventHandler click = null)
{
_pos = pos;
_font = font;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorHover;
_textColor = textColor;
_textColorHover = textColorHover;
_textColorFocus = textColorHover;
_text = text;
_backSprite = Game1.nullSprite;
_click = click;
}
public Button(Rectangle pos, Color backColor, Color backColorHover, Color backColorFocus, string text, SpriteFont font, Color textColor, Color textColorHover, Color textColorFocus, EventHandler click = null)
{
_pos = pos;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorFocus;
_text = text;
_font = font;
_textColor = textColor;
_textColorHover = textColorHover;
_textColorFocus = textColorFocus;
_backSprite = Game1.nullSprite;
_click = click;
}
@ -99,14 +27,16 @@ namespace Galactic_Colors_Control_GUI.GUI
public override void Draw(SpriteBatch spriteBatch)
{
Color backColor = _isFocus ? _backColorFocus : (_isHover ? _backColorHover : _backColor);
spriteBatch.Draw(_backSprite, _pos, backColor);
if (_text != null)
{
Color textColor = _isFocus ? _textColorFocus : (_isHover ? _textColorHover : _textColor);
spriteBatch.DrawString(_font, _text, new Vector2(_pos.X + (_pos.Width - _font.MeasureString(_text).X) / 2, _pos.Y + (_pos.Height - _font.MeasureString(_text).Y) / 2), textColor);
if (_isFocus) {
if (_unFocusTime < 10)
{
_unFocusTime++;
}
else {
_isFocus = false;
_unFocusTime = 0;
}
}
if (_isFocus) { _isFocus = false; }
}
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galactic_Colors_Control_GUI.GUI
{
public class Colors
{
public Color _normal;
public Color _hover;
public Color _focus;
public Colors(Color color) {
_normal = color;
_hover = color;
_focus = color;
}
public Colors(Color normal, Color hover)
{
_normal = normal;
_hover = hover;
_focus = hover;
}
public Colors(Color normal, Color hover, Color focus)
{
_normal = normal;
_hover = hover;
_focus = focus;
}
}
}

View File

@ -2,17 +2,16 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Galactic_Colors_Control_GUI.GUI
{
class Element
{
protected Rectangle _pos;
protected bool _isHover;
protected bool _isFocus;
public bool _isHover;
public bool _isFocus;
public bool Contain(int x, int y)
public virtual bool Contain(int x, int y)
{
return _pos.Contains(x, y);
}
@ -22,7 +21,7 @@ namespace Galactic_Colors_Control_GUI.GUI
}
public void Update(int x, int y, Mouse mouse, EventArgs e)
public virtual void Update(int x, int y, Mouse mouse, Keys key, bool isMaj, EventArgs e)
{
if (mouse.leftPress)
{

View File

@ -0,0 +1,113 @@
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galactic_Colors_Control_GUI.GUI
{
public static class KeyString
{
private static Dictionary<Keys, CharPair> Data = new Dictionary<Keys, CharPair>();
class CharPair
{
public CharPair(char normalChar, Nullable<char> shiftChar)
{
this.NormalChar = normalChar;
this.ShiftChar = shiftChar;
}
public char NormalChar;
public Nullable<char> ShiftChar;
}
public static bool KeyToString(Keys key, bool shitKeyPressed, out char character)
{
bool result = false;
character = ' ';
CharPair charPair;
if ((Keys.A <= key && key <= Keys.Z) || key == Keys.Space)
{
// Use as is if it is AZ, or Space key.
character = (shitKeyPressed) ? (char)key : Char.ToLower((char)key);
result = true;
}
else if (Data.TryGetValue(key, out charPair))
{
// Otherwise, convert by key map.
if (!shitKeyPressed)
{
character = charPair.NormalChar;
result = true;
}
else if (charPair.ShiftChar.HasValue)
{
character = charPair.ShiftChar.Value;
result = true;
}
}
return result;
}
static void AddKeyMap(Keys key, string charPair)
{
char char1 = charPair[0];
Nullable<char> char2 = null;
if (charPair.Length > 1)
char2 = charPair[1];
Data.Add(key, new CharPair(char1, char2));
}
public static void InitializeKeyString()
{
// First row of US keyboard.
AddKeyMap(Keys.OemTilde, "`~");
AddKeyMap(Keys.D1, "1!");
AddKeyMap(Keys.D2, "2@");
AddKeyMap(Keys.D3, "3#");
AddKeyMap(Keys.D4, "4$");
AddKeyMap(Keys.D5, "5%");
AddKeyMap(Keys.D6, "6^");
AddKeyMap(Keys.D7, "7&");
AddKeyMap(Keys.D8, "8*");
AddKeyMap(Keys.D9, "9(");
AddKeyMap(Keys.D0, "0)");
AddKeyMap(Keys.OemMinus, "-_");
AddKeyMap(Keys.OemPlus, "=+");
// Second row of US keyboard.
AddKeyMap(Keys.OemOpenBrackets, "[{");
AddKeyMap(Keys.OemCloseBrackets, "]}");
AddKeyMap(Keys.OemPipe, "\\|");
// Third row of US keyboard.
AddKeyMap(Keys.OemSemicolon, ";:");
AddKeyMap(Keys.OemQuotes, "'\"");
AddKeyMap(Keys.OemComma, ",<");
AddKeyMap(Keys.OemPeriod, ".>");
AddKeyMap(Keys.OemQuestion, "/?");
// Keypad keys of US keyboard.
AddKeyMap(Keys.NumPad1, "1");
AddKeyMap(Keys.NumPad2, "2");
AddKeyMap(Keys.NumPad3, "3");
AddKeyMap(Keys.NumPad4, "4");
AddKeyMap(Keys.NumPad5, "5");
AddKeyMap(Keys.NumPad6, "6");
AddKeyMap(Keys.NumPad7, "7");
AddKeyMap(Keys.NumPad8, "8");
AddKeyMap(Keys.NumPad9, "9");
AddKeyMap(Keys.NumPad0, "0");
AddKeyMap(Keys.Add, "+");
AddKeyMap(Keys.Divide, "/");
AddKeyMap(Keys.Multiply, "*");
AddKeyMap(Keys.Subtract, "-");
AddKeyMap(Keys.Decimal, ".");
}
}
}

View File

@ -10,66 +10,149 @@ namespace Galactic_Colors_Control_GUI.GUI
{
class Label : Element
{
public enum textAlign { topLeft, topCenter, topRight, centerLeft, centerCenter, centerRight, bottomLeft, bottomCenter, bottomRight };
protected textAlign _align;
protected string _text;
protected SpriteFont _font;
protected Color _color, _colorHover, _colorFocus;
protected Colors _colors;
protected Vector _vector;
protected bool _center;
public Label() { }
public Label(Vector vector, string text, SpriteFont font, Color color, bool center = false)
public Label(Rectangle pos, string text, SpriteFont font, Colors colors, textAlign align = textAlign.centerCenter)
{
_vector = vector;
_pos = pos;
_text = text;
_font = font;
_color = color;
_colorHover = color;
_colorFocus = color;
_center = center;
OnTextChange(text);
_colors = colors;
_align = align;
OnTextChange();
}
public Label(Vector vector, string text, SpriteFont font, Color color, Color colorHover, bool center = false)
public Label(Vector vector, string text, SpriteFont font, Colors colors, textAlign align = textAlign.bottomRight)
{
_vector = vector;
_pos = new Rectangle(vector.X,vector.Y,0,0);
_text = text;
_font = font;
_color = color;
_colorHover = colorHover;
_colorFocus = colorHover;
_center = center;
OnTextChange(text);
_colors = colors;
_align = align;
OnTextChange();
}
public Label(Vector vector, string text, SpriteFont font, Color color, Color colorHover, Color colorFocus, bool center = false)
public override bool Contain(int x, int y)
{
_vector = vector;
_text = text;
_font = font;
_color = color;
_colorHover = colorHover;
_colorFocus = colorFocus;
_center = center;
OnTextChange(text);
}
protected void OnTextChange(string text)
{
if (_center)
bool isVector = (_pos.Height == 0 && _pos.Width == 0);
if (isVector)
{
_pos = new Rectangle(_vector.X - (int)_font.MeasureString(text).X / 2, _vector.Y - (int)_font.MeasureString(text).Y / 2, (int)_font.MeasureString(text).X, (int)_font.MeasureString(text).Y);
return new Rectangle(_vector.X, _vector.Y, (int)_font.MeasureString(_text).X, (int)_font.MeasureString(_text).Y).Contains(x, y);
}
else
{
_pos = new Rectangle(_vector.X, _vector.Y, (int)_font.MeasureString(text).X, (int)_font.MeasureString(text).Y);
return base.Contain(x, y);
}
}
protected void OnTextChange()
{
bool isVector = (_pos.Height == 0 && _pos.Width == 0);
switch (_align)
{
case textAlign.topLeft:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X, _pos.Y - (int)_font.MeasureString(_text).Y);
}
else{
_vector = new Vector(_pos.X, _pos.Y);
}
break;
case textAlign.topCenter:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X / 2, _pos.Y - (int)_font.MeasureString(_text).Y);
}
else {
_vector = new Vector(_pos.X + _pos.Width / 2 - (int)_font.MeasureString(_text).X / 2, _pos.Y);
}
break;
case textAlign.topRight:
if (isVector)
{
_vector = new Vector(_pos.X, _pos.Y - (int)_font.MeasureString(_text).Y);
}
else {
_vector = new Vector(_pos.X + _pos.Width - (int)_font.MeasureString(_text).X, _pos.Y);
}
break;
case textAlign.centerLeft:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X, _pos.Y - (int)_font.MeasureString(_text).Y / 2 );
}
else {
_vector = new Vector(_pos.X, _pos.Y + _pos.Height / 2 -(int)_font.MeasureString(_text).Y / 2);
}
break;
case textAlign.centerCenter:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X / 2, _pos.Y - (int)_font.MeasureString(_text).Y / 2);
}
else {
_vector = new Vector(_pos.X + _pos.Width / 2 - (int)_font.MeasureString(_text).X / 2, _pos.Y + _pos.Height / 2 -(int)_font.MeasureString(_text).Y / 2);
}
break;
case textAlign.centerRight:
if (isVector)
{
_vector = new Vector(_pos.X, _pos.Y - (int)_font.MeasureString(_text).Y / 2);
}
else {
_vector = new Vector(_pos.X + _pos.Width - (int)_font.MeasureString(_text).X, _pos.Y + _pos.Height / 2 -(int)_font.MeasureString(_text).Y / 2);
}
break;
case textAlign.bottomLeft:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X, _pos.Y);
}
else {
_vector = new Vector(_pos.X, _pos.Y + _pos.Height - (int)_font.MeasureString(_text).Y);
}
break;
case textAlign.bottomCenter:
if (isVector)
{
_vector = new Vector(_pos.X - (int)_font.MeasureString(_text).X / 2, _pos.Y);
}
else {
_vector = new Vector(_pos.X + _pos.Width / 2 - (int)_font.MeasureString(_text).X / 2, _pos.Y + _pos.Height - (int)_font.MeasureString(_text).Y);
}
break;
case textAlign.bottomRight:
if (isVector)
{
_vector = new Vector(_pos.X, _pos.Y);
}
else {
_vector = new Vector(_pos.X + _pos.Width - (int)_font.MeasureString(_text).X, _pos.Y + _pos.Height - (int)_font.MeasureString(_text).Y);
}
break;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
Color color = _isFocus ? _colorFocus : (_isHover ? _colorHover : _color);
spriteBatch.DrawString(_font, _text, new Vector2(_pos.X, _pos.Y), color);
Color color = _isFocus ? _colors._focus : (_isHover ? _colors._hover : _colors._normal);
spriteBatch.DrawString(_font, _text, new Vector2(_vector.X, _vector.Y), color);
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Galactic_Colors_Control_GUI.GUI
{
class TextField : Label
{
protected string _placeHolder;
protected string _value;
public string output { get { return _value; } set { _value = value; _text = (_placeHolder != null && _value == null) ? _placeHolder : _value; OnTextChange(); } }
protected event EventHandler _validate;
public TextField(Rectangle pos, string value, SpriteFont font, Colors colors, textAlign align = textAlign.centerCenter, string placeHolder = null, EventHandler validate = null)
{
_value = value;
_font = font;
_colors = colors;
_align = align;
_placeHolder = placeHolder;
_validate = validate;
_text = (placeHolder != null && value == null) ? placeHolder : value;
OnTextChange();
}
public TextField(Vector vector, string value, SpriteFont font, Colors colors, textAlign align = textAlign.bottomRight, string placeHolder = null, EventHandler validate = null)
{
_pos = new Rectangle(vector.X, vector.Y, 0, 0);
_value = value;
_font = font;
_colors = colors;
_align = align;
_placeHolder = placeHolder;
_validate = validate;
_text = (placeHolder != null && value == null) ? placeHolder : value;
OnTextChange();
}
public override void Update(int x, int y, Mouse mouse, Keys key, bool isMaj,EventArgs e)
{
base.Update(x, y, mouse, key, isMaj, e);
if (_isFocus)
{
//Only QWERTY support wait monogame 4.6 (https://github.com/MonoGame/MonoGame/issues/3836)
switch (key)
{
case Keys.Back:
if (_value.Length > 0) { _value = _value.Remove(_value.Length - 1); _text = (_placeHolder != null && _value == null) ? _placeHolder : _value; OnTextChange(); }
break;
case Keys.Enter:
Validate(this, e);
break;
default:
char ch;
if (KeyString.KeyToString(key, isMaj, out ch)) { _value += ch; _text = (_placeHolder != null && _value == null) ? _placeHolder : _value; OnTextChange(); }
break;
}
}
}
public void Validate(object sender, EventArgs e)
{
if (_validate != null)
{
_validate.Invoke(sender, e);
}
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Galactic_Colors_Control_GUI.GUI
{
class Texture : Element
{
protected Colors _colors;
protected Texture2D _sprite;
public Texture() { }
public Texture(Rectangle pos ,Texture2D sprite, Colors colors)
{
_pos = pos;
_sprite = sprite;
_colors = colors;
}
public override void Draw(SpriteBatch spriteBatch)
{
Color backColor = _isFocus ? _colors._focus : (_isHover ? _colors._hover : _colors._normal);
spriteBatch.Draw(_sprite, _pos, backColor);
}
}
}

View File

@ -1,113 +1,46 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace Galactic_Colors_Control_GUI.GUI
{
class TexturedButton : Button
class TexturedButton : Texture
{
buttonSprites _backSprites;
Button button;
private int _unFocusTime;
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, EventHandler click = null)
public TexturedButton(Rectangle pos, Texture2D sprite, Colors colors, EventHandler click = null)
{
_pos = pos;
_backSprites = backSprites;
_backColor = backColor;
_backColorHover = backColor;
_backColorFocus = backColor;
_click += click;
_sprite = sprite;
_colors = colors;
button = new Button(pos, click);
}
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, Color backColorHover, EventHandler click = null)
public override void Update(int x, int y, Mouse mouse, Keys key, bool isMaj, EventArgs e)
{
_pos = pos;
_backSprites = backSprites;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorHover;
_click += click;
}
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, Color backColorHover, Color backColorFocus, EventHandler click = null)
{
_pos = pos;
_backSprites = backSprites;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorFocus;
_click += click;
}
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, string text, SpriteFont font, Color textColor, EventHandler click = null)
{
_pos = pos;
_font = font;
_backSprites = backSprites;
_backColor = backColor;
_textColor = textColor;
_textColorHover = textColor;
_textColorFocus = textColor;
_text = text;
_click += click;
}
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, Color backColorHover, string text, SpriteFont font, Color textColor, Color textColorHover, EventHandler click = null)
{
_pos = pos;
_font = font;
_backSprites = backSprites;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorHover;
_textColor = textColor;
_textColorHover = textColorHover;
_textColorFocus = textColorHover;
_text = text;
_click += click;
}
public TexturedButton(Rectangle pos, buttonSprites backSprites, Color backColor, Color backColorHover, Color backColorFocus, string text, SpriteFont font, Color textColor, Color textColorHover, Color textColorFocus, EventHandler click = null)
{
_pos = pos;
_backSprites = backSprites;
_backColor = backColor;
_backColorHover = backColorHover;
_backColorFocus = backColorFocus;
_text = text;
_font = font;
_textColor = textColor;
_textColorHover = textColorHover;
_textColorFocus = textColorFocus;
_click += click;
base.Update(x, y, mouse, key, isMaj, e);
button.Update(x, y, mouse, key, isMaj, e);
}
public override void Draw(SpriteBatch spriteBatch)
{
Color backColor = _isFocus ? _backColorFocus : (_isHover ? _backColorHover : _backColor);
base.Draw(spriteBatch);
int leftWidth = _backSprites.topLeft.Width;
int rightWidth = _backSprites.topRight.Width;
int centerWidth = _pos.Width - leftWidth - rightWidth;
int topHeight = _backSprites.topLeft.Height;
int bottomHeight = _backSprites.bottomLeft.Height;
int centerHeight = _pos.Height - topHeight - bottomHeight;
spriteBatch.Draw(_backSprites.topLeft, new Rectangle(_pos.X, _pos.Y, leftWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.topCenter, new Rectangle(_pos.X + leftWidth, _pos.Y, centerWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.topRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y, rightWidth, topHeight), backColor);
spriteBatch.Draw(_backSprites.centerLeft, new Rectangle(_pos.X, _pos.Y + topHeight, leftWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.centerCenter, new Rectangle(_pos.X + leftWidth, _pos.Y + topHeight, centerWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.centerRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y + topHeight, rightWidth, centerHeight), backColor);
spriteBatch.Draw(_backSprites.bottomLeft, new Rectangle(_pos.X, _pos.Y + _pos.Height - bottomHeight, leftWidth, bottomHeight), backColor);
spriteBatch.Draw(_backSprites.bottomCenter, new Rectangle(_pos.X + leftWidth, _pos.Y + _pos.Height - bottomHeight, centerWidth, bottomHeight), backColor);
spriteBatch.Draw(_backSprites.bottomRight, new Rectangle(_pos.X + _pos.Width - rightWidth, _pos.Y + _pos.Height - bottomHeight, rightWidth, bottomHeight), backColor);
if (_text != null)
if (_isFocus)
{
Color textColor = _isFocus ? _textColorFocus : (_isHover ? _textColorHover : _textColor);
spriteBatch.DrawString(_font, _text, new Vector2(_pos.X + (_pos.Width - _font.MeasureString(_text).X) / 2, _pos.Y + (_pos.Height - _font.MeasureString(_text).Y) / 2), textColor);
if (_unFocusTime < 10)
{
_unFocusTime++;
}
else {
_isFocus = false;
_unFocusTime = 0;
}
}
if (_isFocus) { _isFocus = false; }
button.Draw(spriteBatch);
}
}
}

View File

@ -2,7 +2,7 @@
namespace Galactic_Colors_Control_GUI.GUI
{
public struct buttonSprites
public struct boxSprites
{
public Texture2D topLeft;
public Texture2D topCenter;

View File

@ -39,10 +39,18 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Game1.cs" />
<Compile Include="GUI\Box.cs" />
<Compile Include="GUI\BoxButton.cs" />
<Compile Include="GUI\BoxLabel.cs" />
<Compile Include="GUI\BoxLabelButton.cs" />
<Compile Include="GUI\Button.cs" />
<Compile Include="GUI\buttonSprites.cs" />
<Compile Include="GUI\boxSprites.cs" />
<Compile Include="GUI\Colors.cs" />
<Compile Include="GUI\KeyString.cs" />
<Compile Include="GUI\Label.cs" />
<Compile Include="GUI\Mouse.cs" />
<Compile Include="GUI\TextField.cs" />
<Compile Include="GUI\Texture.cs" />
<Compile Include="GUI\TexturedButton.cs" />
<Compile Include="GUI\Element.cs" />
<Compile Include="GUI\Vector.cs" />

View File

@ -28,7 +28,7 @@ namespace Galactic_Colors_Control_GUI
internal static Texture2D nullSprite;
private Texture2D[] pointerSprites = new Texture2D[1];
private GUI.buttonSprites[] buttonsSprites = new GUI.buttonSprites[1];
private GUI.boxSprites[] buttonsSprites = new GUI.boxSprites[1];
private List<GUI.Element> elements = new List<GUI.Element>();
@ -71,22 +71,11 @@ namespace Galactic_Colors_Control_GUI
version = Assembly.GetEntryAssembly().GetName().Version;
nullSprite = new Texture2D(GraphicsDevice, 1, 1);
nullSprite.SetData(new Color[1 * 1] { Color.White });
GUI.KeyString.InitializeKeyString();
base.Initialize();
}
// TODO : remove this
private void AddRandom(object sender, EventArgs e)
{
Random rand = new Random();
elements.Add(new GUI.Button(new Rectangle(rand.Next(0, ScreenHeight - 20), rand.Next(0, ScreenWidth - 20), 20, 20), Color.Black, Color.DarkSlateGray, Color.SlateGray, RemoveThis));
}
// TODO : remove this
private void RemoveThis(object sender, EventArgs e)
{
elements.Remove(sender as GUI.Element);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
@ -197,12 +186,19 @@ namespace Galactic_Colors_Control_GUI
oldKeys = newKeys;
newKeys = Keyboard.GetState().GetPressedKeys();
Keys key = Keys.None;
foreach (Keys newKey in newKeys)
{
if (!oldKeys.Contains(newKey)) { key = newKey; }
}
if (IsActive)
{
EventArgs e = new EventArgs();
foreach(GUI.Element element in elements.ToArray())
{
element.Update(mouseX, mouseY, nowState, e);
element.Update(mouseX, mouseY, nowState, key, Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift),e);
}
}
@ -242,7 +238,7 @@ namespace Galactic_Colors_Control_GUI
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.Clear(Color.DarkGray);
spriteBatch.Begin();
@ -323,9 +319,10 @@ namespace Galactic_Colors_Control_GUI
switch (newGameStatus)
{
case GameStatus.Home:
elements.Add(new GUI.Label(new GUI.Vector(ScreenWidth / 2, ScreenHeight / 4), "Galactic Colors Control", titleFont, Color.DarkRed, true));
//elements.Add(new GUI.TextField(new GUI.Vector(ScreenWidth / 2, ScreenHeight / 2), null, basicFont, Color.Black, Color.Black, Color.DarkSlateGray, true, "Server address"));
elements.Add(new GUI.TexturedButton(new Rectangle(ScreenWidth / 2 - 100, ScreenHeight * 3 / 4,200,40),buttonsSprites[0], Color.White, Color.LightGray, Color.DarkGray, "Connect", basicFont, Color.Black, Color.Black, Color.White, ConnectClick));
elements.Add(new GUI.Label(new GUI.Vector(ScreenWidth / 2, ScreenHeight / 4), "Galactic Colors Control", titleFont, new GUI.Colors(Color.DarkRed, Color.Green), GUI.Label.textAlign.centerCenter));
elements.Add(new GUI.TextField(new GUI.Vector(ScreenWidth / 2, ScreenHeight / 2), null, basicFont, new GUI.Colors(Color.White, Color.WhiteSmoke, Color.LightGray), GUI.Label.textAlign.centerCenter, "Server address", ConnectClick));
//elements.Add(new GUI.BoxButton(new Rectangle(ScreenWidth / 2 - 100, ScreenHeight * 3 / 4, 200, 40), buttonsSprites[0], new GUI.Colors(Color.White, Color.LightGray, Color.DarkGray), ConnectClick));
elements.Add(new GUI.BoxLabelButton(new Rectangle(ScreenWidth / 2 - 100, ScreenHeight * 3 / 4,200,40),buttonsSprites[0], new GUI.Colors(Color.White, Color.LightGray, Color.DarkGray), "Connect", basicFont, new GUI.Colors(Color.Black, Color.Black, Color.White), GUI.Label.textAlign.centerCenter, ConnectClick));
break;
case GameStatus.Options:
@ -353,7 +350,7 @@ namespace Galactic_Colors_Control_GUI
private void ConnectClick(object sender, EventArgs e)
{
Console.WriteLine("plop");
}
}
}