Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Control : Transformable, Drawable {
- #region Defaults:...
- protected static Color defaultClearColor = Color.Black;
- #endregion
- //Handler
- public event EventHandler MouseEnter, MouseMove, MouseDown, MouseUp, MouseClick, MouseLeave;
- //Fields
- protected bool enabled;
- protected bool hovered;
- protected bool pressed;
- protected Vector2i prevMouse;
- protected RenderTexture surface;
- protected List<Drawable> drawables;
- protected Sprite sprite;
- //Properties
- public bool Enabled {
- get { return enabled; }
- }
- public Vector2u Size {
- get { return surface.Size; }
- }
- //Creators
- public Control(uint _width, uint _height) {
- enabled = true;
- hovered = false;
- pressed = false;
- surface = new RenderTexture(_width, _height);
- drawables = new List<Drawable>();
- sprite = new Sprite(surface.Texture);
- sprite.Position = Position;
- sprite.Origin = Origin;
- }
- //Internals
- protected void RaiseMouseEnter() {
- EventHandler handler = MouseEnter;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- protected void RaiseMouseMove() {
- EventHandler handler = MouseMove;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- protected void RaiseMouseDown() {
- EventHandler handler = MouseDown;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- protected void RaiseMouseUp() {
- EventHandler handler = MouseUp;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- protected void RaiseMouseClick() {
- EventHandler handler = MouseClick;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- protected void RaiseMouseLeave() {
- EventHandler handler = MouseLeave;
- if (handler != null) handler(this, EventArgs.Empty);
- }
- //Externals
- public void SetPosition(Vector2f _position) {
- Position = _position;
- sprite.Position = _position;
- }
- public void SetOrigin(Vector2f _origin) {
- Origin = _origin;
- sprite.Origin = _origin;
- }
- public virtual void SetEnabled(bool _enabled = true) {
- enabled = _enabled;
- }
- public virtual void Update(RenderTarget _target) {
- if (enabled) {
- if (hovered) {
- if (Mouse.IsButtonPressed(Mouse.Button.Left)) {
- if (!pressed) {
- RaiseMouseDown();
- pressed = true;
- }
- }
- else {
- if (pressed) {
- RaiseMouseUp();
- RaiseMouseClick();
- pressed = false;
- }
- }
- }
- Vector2i mouse = Mouse.GetPosition();
- if (mouse != prevMouse) {
- prevMouse = mouse;
- FloatRect body = new FloatRect(Position + Origin, new Vector2f(Size.X, Size.Y));
- if (body.Contains(mouse.X, mouse.Y)) {
- if (!hovered) {
- RaiseMouseEnter();
- hovered = true;
- }
- RaiseMouseMove();
- }
- else if (hovered) {
- RaiseMouseLeave();
- hovered = false;
- }
- }
- }
- }
- public void Draw(RenderTarget target, RenderStates states) {
- surface.Clear(defaultClearColor);
- foreach (Drawable d in drawables) d.Draw(surface, states);
- surface.Display();
- target.Draw(sprite, states);
- }
- }
- public struct ButtonStyle {
- public Color BackColor;
- public Color BorderColor;
- public Color TextColor;
- public Text.Styles TextStyle;
- public ButtonStyle(Color _backColor, Color _borderColor, Color _textColor, Text.Styles _textStyle) {
- BackColor = _backColor;
- BorderColor = _borderColor;
- TextColor = _textColor;
- TextStyle = _textStyle;
- }
- }
- public struct ButtonStyles {
- public ButtonStyle Enabled, Hovered, Clicked, Disabled;
- public ButtonStyles(ButtonStyle _enabled, ButtonStyle _hovered, ButtonStyle _clicked, ButtonStyle _disabled) {
- Enabled = _enabled;
- Hovered = _hovered;
- Clicked = _clicked;
- Disabled = _disabled;
- }
- }
- public class Button : Control {
- #region Defaults:...
- private static Color dEBC = new Color(225, 225, 225);
- private static Color dEOC = Color.Black;
- private static Color dETC = Color.Black;
- private static Text.Styles dETS = SFML.Graphics.Text.Styles.Regular;
- private static ButtonStyle defEnabledStyle = new ButtonStyle(dEBC, dEOC, dETC, dETS);
- private static Color dCBC = Color.White;
- private static Color dCOC = Color.Black;
- private static Color dCTC = Color.Black;
- private static Text.Styles dCTS = SFML.Graphics.Text.Styles.Underlined;
- private static ButtonStyle defClickedStyle = new ButtonStyle(dCBC, dCOC, dCTC, dCTS);
- private static Color dHBC = Color.White;
- private static Color dHOC = Color.Black;
- private static Color dHTC = Color.Black;
- private static Text.Styles dHTS = SFML.Graphics.Text.Styles.Underlined;
- private static ButtonStyle defHoveredStyle = new ButtonStyle(dHBC, dHOC, dHTC, dHTS);
- private static Color dDBC = new Color(225, 225, 225);
- private static Color dDOC = new Color(40, 40, 40);
- private static Color dDTC = new Color(80, 80, 80);
- private static Text.Styles dDTS = SFML.Graphics.Text.Styles.Regular;
- private static ButtonStyle defDisabledStyle = new ButtonStyle(dDBC, dDOC, dDTC, dDTS);
- private static float defBorder = 1f;
- private static string defaultText = "New Button";
- private static Font defaultFont = new Font("resources/tahoma.ttf");
- private static uint defaultCharSize = 12;
- private static Color defaultTextColor = Color.Black;
- #endregion
- //Fields
- protected bool animatedStyle;
- protected ButtonStyles styles;
- protected Shape back;
- protected Text text;
- //Properties
- public bool AnimatedStyle {
- get { return animatedStyle; }
- set { animatedStyle = value; }
- }
- public Color BackColor {
- get { return back.FillColor; }
- set {
- back.FillColor = value;
- }
- }
- public float Border {
- get { return back.OutlineThickness; }
- set {
- back.OutlineThickness = value;
- UpdateBack();
- }
- }
- public Color BorderColor {
- get { return back.OutlineColor; }
- set { back.OutlineColor = value; }
- }
- public ButtonStyles Styles {
- get { return styles; }
- set { styles = value; }
- }
- public string Text {
- get { return text.DisplayedString; }
- set {
- text.DisplayedString = value;
- UpdateText();
- }
- }
- public Font Font {
- get { return text.Font; }
- set {
- text.Font = value;
- UpdateText();
- }
- }
- public uint CharSize {
- get { return text.CharacterSize; }
- set {
- text.CharacterSize = value;
- UpdateText();
- }
- }
- public Color TextColor {
- get { return text.Color; }
- set {
- text.Color = value;
- }
- }
- public Text.Styles TextStyle {
- get { return text.Style; }
- set {
- text.Style = value;
- if (value != SFML.Graphics.Text.Styles.Underlined) UpdateText();
- }
- }
- //Creators
- public Button(uint _width, uint _height) : base( _width, _height) {
- back = new RectangleShape(new Vector2f(_width - defBorder, _height - defBorder));
- back.Position = new Vector2f(defBorder, defBorder);
- back.OutlineThickness = defBorder;
- text = new Text(defaultText, defaultFont, defaultCharSize);
- text.Color = defaultTextColor;
- styles = new ButtonStyles(defEnabledStyle, defHoveredStyle, defClickedStyle, defDisabledStyle);
- SetButtonStyle(styles.Enabled);
- animatedStyle = true;
- drawables.Add(back);
- drawables.Add(text);
- MouseEnter += InternalOnMouseEnter;
- MouseDown += InternalOnMouseDown;
- MouseUp += InternalOnMouseUp;
- MouseLeave += InternalOnMouseLeave;
- }
- //Internals
- protected void UpdateBack() {
- Color fc = back.FillColor;
- Color oc = back.OutlineColor;
- float ot = back.OutlineThickness;
- Vector2u s = Size;
- Vector2f size = new Vector2f(s.X - ot, s.Y - ot);
- back = new RectangleShape(size);
- back.Position = new Vector2f(ot, ot);
- back.OutlineThickness = ot;
- back.OutlineColor = oc;
- back.FillColor = fc;
- }
- protected void UpdateText() {
- FloatRect tr = text.GetGlobalBounds();
- text.Origin = new Vector2f(tr.Width / 2, tr.Height / 2);
- Vector2u us = Size;
- Vector2f fs = new Vector2f(us.X, us.Y);
- text.Position = new Vector2f(fs.X / 2, fs.Y / 2);
- }
- protected void InternalOnMouseEnter(object sender, EventArgs e) {
- if (animatedStyle) SetButtonStyle(styles.Hovered);
- }
- protected void InternalOnMouseDown(object sender, EventArgs e) {
- if (animatedStyle) SetButtonStyle(styles.Clicked);
- }
- protected void InternalOnMouseUp(object sender, EventArgs e) {
- if (animatedStyle) SetButtonStyle(styles.Hovered);
- }
- protected void InternalOnMouseLeave(object sender, EventArgs e) {
- if (animatedStyle) SetButtonStyle(styles.Enabled);
- }
- //Externals
- public override void SetEnabled(bool _enabled = true) {
- enabled = _enabled;
- if (enabled) SetButtonStyle(styles.Enabled);
- else SetButtonStyle(styles.Disabled);
- }
- public void SetSize(Vector2u _size) {
- surface = new RenderTexture(_size.X, _size.Y);
- Vector2u us = Size;
- Vector2f fs = new Vector2f(us.X, us.Y);
- UpdateBack();
- UpdateText();
- }
- public ButtonStyle GetButtonStyle () {
- return new ButtonStyle(BackColor, BorderColor, TextColor, TextStyle);
- }
- public void SetButtonStyle(ButtonStyle _style) {
- BackColor = _style.BackColor;
- BorderColor = _style.BorderColor;
- TextColor = _style.TextColor;
- TextStyle = _style.TextStyle;
- }
- }
Add Comment
Please, Sign In to add comment