Guest User

Untitled

a guest
Mar 21st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.87 KB | None | 0 0
  1. public class Control : Transformable, Drawable {
  2.       #region Defaults:...
  3.       protected static Color defaultClearColor = Color.Black;
  4.       #endregion
  5.  
  6.       //Handler
  7.       public event EventHandler MouseEnter, MouseMove, MouseDown, MouseUp, MouseClick, MouseLeave;
  8.      
  9.       //Fields
  10.       protected bool enabled;
  11.       protected bool hovered;
  12.       protected bool pressed;
  13.       protected Vector2i prevMouse;
  14.       protected RenderTexture surface;
  15.       protected List<Drawable> drawables;
  16.       protected Sprite sprite;
  17.  
  18.       //Properties
  19.       public bool Enabled {
  20.          get { return enabled; }
  21.       }
  22.       public Vector2u Size {
  23.          get { return surface.Size; }
  24.       }
  25.      
  26.       //Creators
  27.       public Control(uint _width, uint _height) {
  28.          enabled = true;
  29.          hovered = false;
  30.          pressed = false;
  31.          surface = new RenderTexture(_width, _height);
  32.          drawables = new List<Drawable>();
  33.          sprite = new Sprite(surface.Texture);
  34.          sprite.Position = Position;
  35.          sprite.Origin = Origin;
  36.       }
  37.  
  38.  
  39.       //Internals
  40.       protected void RaiseMouseEnter() {
  41.          EventHandler handler = MouseEnter;
  42.          if (handler != null) handler(this, EventArgs.Empty);
  43.       }
  44.       protected void RaiseMouseMove() {
  45.          EventHandler handler = MouseMove;
  46.          if (handler != null) handler(this, EventArgs.Empty);
  47.       }
  48.       protected void RaiseMouseDown() {
  49.          EventHandler handler = MouseDown;
  50.          if (handler != null) handler(this, EventArgs.Empty);
  51.       }
  52.       protected void RaiseMouseUp() {
  53.          EventHandler handler = MouseUp;
  54.          if (handler != null) handler(this, EventArgs.Empty);
  55.       }
  56.       protected void RaiseMouseClick() {
  57.          EventHandler handler = MouseClick;
  58.          if (handler != null) handler(this, EventArgs.Empty);
  59.       }
  60.       protected void RaiseMouseLeave() {
  61.          EventHandler handler = MouseLeave;
  62.          if (handler != null) handler(this, EventArgs.Empty);
  63.       }
  64.  
  65.       //Externals
  66.       public void SetPosition(Vector2f _position) {
  67.          Position = _position;
  68.          sprite.Position = _position;
  69.       }
  70.       public void SetOrigin(Vector2f _origin) {
  71.          Origin = _origin;
  72.          sprite.Origin = _origin;
  73.       }
  74.       public virtual void SetEnabled(bool _enabled = true) {
  75.          enabled = _enabled;
  76.       }
  77.       public virtual void Update(RenderTarget _target) {
  78.          if (enabled) {
  79.             if (hovered) {
  80.                if (Mouse.IsButtonPressed(Mouse.Button.Left)) {
  81.                   if (!pressed) {
  82.                      RaiseMouseDown();
  83.                      pressed = true;
  84.                   }
  85.                }
  86.                else {
  87.                   if (pressed) {
  88.                      RaiseMouseUp();
  89.                      RaiseMouseClick();
  90.                      pressed = false;
  91.                   }
  92.                }
  93.             }
  94.             Vector2i mouse = Mouse.GetPosition();
  95.             if (mouse != prevMouse) {
  96.                prevMouse = mouse;
  97.                FloatRect body = new FloatRect(Position + Origin, new Vector2f(Size.X, Size.Y));
  98.                if (body.Contains(mouse.X, mouse.Y)) {
  99.                   if (!hovered) {
  100.                      RaiseMouseEnter();
  101.                      hovered = true;
  102.                   }
  103.                   RaiseMouseMove();
  104.                }
  105.                else if (hovered) {
  106.                   RaiseMouseLeave();
  107.                   hovered = false;
  108.                }
  109.             }
  110.          }
  111.       }
  112.       public void Draw(RenderTarget target, RenderStates states) {
  113.          surface.Clear(defaultClearColor);
  114.          foreach (Drawable d in drawables) d.Draw(surface, states);
  115.          surface.Display();
  116.          target.Draw(sprite, states);
  117.       }
  118.    }
  119.  
  120.    public struct ButtonStyle {
  121.       public Color BackColor;
  122.       public Color BorderColor;
  123.       public Color TextColor;
  124.       public Text.Styles TextStyle;
  125.  
  126.       public ButtonStyle(Color _backColor, Color _borderColor, Color _textColor, Text.Styles _textStyle) {
  127.          BackColor = _backColor;
  128.          BorderColor = _borderColor;
  129.          TextColor = _textColor;
  130.          TextStyle = _textStyle;
  131.       }
  132.    }
  133.    public struct ButtonStyles {
  134.       public ButtonStyle Enabled, Hovered, Clicked, Disabled;
  135.  
  136.       public ButtonStyles(ButtonStyle _enabled, ButtonStyle _hovered, ButtonStyle _clicked, ButtonStyle _disabled) {
  137.          Enabled = _enabled;
  138.          Hovered = _hovered;
  139.          Clicked = _clicked;
  140.          Disabled = _disabled;
  141.       }
  142.    }
  143.  
  144.    public class Button : Control {
  145.       #region Defaults:...
  146.       private static Color dEBC = new Color(225, 225, 225);
  147.       private static Color dEOC = Color.Black;
  148.       private static Color dETC = Color.Black;
  149.       private static Text.Styles dETS = SFML.Graphics.Text.Styles.Regular;
  150.       private static ButtonStyle defEnabledStyle = new ButtonStyle(dEBC, dEOC, dETC, dETS);
  151.  
  152.       private static Color dCBC = Color.White;
  153.       private static Color dCOC = Color.Black;
  154.       private static Color dCTC = Color.Black;
  155.       private static Text.Styles dCTS = SFML.Graphics.Text.Styles.Underlined;
  156.       private static ButtonStyle defClickedStyle = new ButtonStyle(dCBC, dCOC, dCTC, dCTS);
  157.  
  158.       private static Color dHBC = Color.White;
  159.       private static Color dHOC = Color.Black;
  160.       private static Color dHTC = Color.Black;
  161.       private static Text.Styles dHTS = SFML.Graphics.Text.Styles.Underlined;
  162.       private static ButtonStyle defHoveredStyle = new ButtonStyle(dHBC, dHOC, dHTC, dHTS);
  163.  
  164.       private static Color dDBC = new Color(225, 225, 225);
  165.       private static Color dDOC = new Color(40, 40, 40);
  166.       private static Color dDTC = new Color(80, 80, 80);
  167.       private static Text.Styles dDTS = SFML.Graphics.Text.Styles.Regular;
  168.       private static ButtonStyle defDisabledStyle = new ButtonStyle(dDBC, dDOC, dDTC, dDTS);
  169.  
  170.       private static float defBorder = 1f;
  171.       private static string defaultText = "New Button";
  172.       private static Font defaultFont = new Font("resources/tahoma.ttf");
  173.       private static uint defaultCharSize = 12;
  174.       private static Color defaultTextColor = Color.Black;
  175.       #endregion
  176.  
  177.       //Fields
  178.       protected bool animatedStyle;
  179.       protected ButtonStyles styles;
  180.       protected Shape back;
  181.       protected Text text;
  182.  
  183.       //Properties
  184.       public bool AnimatedStyle {
  185.          get { return animatedStyle; }
  186.          set { animatedStyle = value; }
  187.       }
  188.       public Color BackColor {
  189.          get { return back.FillColor; }
  190.          set {
  191.             back.FillColor = value;
  192.          }
  193.       }
  194.       public float Border {
  195.          get { return back.OutlineThickness; }
  196.          set {
  197.             back.OutlineThickness = value;
  198.             UpdateBack();
  199.          }
  200.       }
  201.       public Color BorderColor {
  202.          get { return back.OutlineColor; }
  203.          set { back.OutlineColor = value; }
  204.       }
  205.       public ButtonStyles Styles {
  206.          get { return styles; }
  207.          set { styles = value; }
  208.       }
  209.       public string Text {
  210.          get { return text.DisplayedString; }
  211.          set {
  212.             text.DisplayedString = value;
  213.             UpdateText();
  214.          }
  215.       }
  216.       public Font Font {
  217.          get { return text.Font; }
  218.          set {
  219.             text.Font = value;
  220.             UpdateText();
  221.          }
  222.       }
  223.       public uint CharSize {
  224.          get { return text.CharacterSize; }
  225.          set {
  226.             text.CharacterSize = value;
  227.             UpdateText();
  228.          }
  229.       }
  230.       public Color TextColor {
  231.          get { return text.Color; }
  232.          set {
  233.             text.Color = value;
  234.          }
  235.       }
  236.       public Text.Styles TextStyle {
  237.          get { return text.Style; }
  238.          set {
  239.             text.Style = value;
  240.             if (value != SFML.Graphics.Text.Styles.Underlined) UpdateText();
  241.          }
  242.       }
  243.  
  244.       //Creators
  245.       public Button(uint _width, uint _height) : base( _width, _height) {
  246.          back = new RectangleShape(new Vector2f(_width - defBorder, _height - defBorder));
  247.          back.Position = new Vector2f(defBorder, defBorder);
  248.          back.OutlineThickness = defBorder;
  249.          text = new Text(defaultText, defaultFont, defaultCharSize);
  250.          text.Color = defaultTextColor;
  251.          styles = new ButtonStyles(defEnabledStyle, defHoveredStyle, defClickedStyle, defDisabledStyle);
  252.          SetButtonStyle(styles.Enabled);
  253.          animatedStyle = true;
  254.          drawables.Add(back);
  255.          drawables.Add(text);
  256.  
  257.          MouseEnter += InternalOnMouseEnter;
  258.          MouseDown += InternalOnMouseDown;
  259.          MouseUp += InternalOnMouseUp;
  260.          MouseLeave += InternalOnMouseLeave;
  261.       }
  262.  
  263.       //Internals
  264.       protected void UpdateBack() {
  265.          Color fc = back.FillColor;
  266.          Color oc = back.OutlineColor;
  267.          float ot = back.OutlineThickness;
  268.          Vector2u s = Size;
  269.          Vector2f size = new Vector2f(s.X - ot, s.Y - ot);
  270.          back = new RectangleShape(size);
  271.          back.Position = new Vector2f(ot, ot);
  272.          back.OutlineThickness = ot;
  273.          back.OutlineColor = oc;
  274.          back.FillColor = fc;
  275.       }
  276.       protected void UpdateText() {
  277.          FloatRect tr = text.GetGlobalBounds();
  278.          text.Origin = new Vector2f(tr.Width / 2, tr.Height / 2);
  279.          Vector2u us = Size;
  280.          Vector2f fs = new Vector2f(us.X, us.Y);
  281.          text.Position = new Vector2f(fs.X / 2, fs.Y / 2);
  282.       }
  283.       protected void InternalOnMouseEnter(object sender, EventArgs e) {
  284.          if (animatedStyle) SetButtonStyle(styles.Hovered);
  285.       }
  286.       protected void InternalOnMouseDown(object sender, EventArgs e) {
  287.          if (animatedStyle) SetButtonStyle(styles.Clicked);
  288.       }
  289.       protected void InternalOnMouseUp(object sender, EventArgs e) {
  290.          if (animatedStyle) SetButtonStyle(styles.Hovered);
  291.       }
  292.       protected void InternalOnMouseLeave(object sender, EventArgs e) {
  293.          if (animatedStyle) SetButtonStyle(styles.Enabled);
  294.       }
  295.  
  296.  
  297.       //Externals
  298.       public override void SetEnabled(bool _enabled = true) {
  299.          enabled = _enabled;
  300.          if (enabled) SetButtonStyle(styles.Enabled);
  301.          else SetButtonStyle(styles.Disabled);
  302.       }
  303.       public void SetSize(Vector2u _size) {
  304.          surface = new RenderTexture(_size.X, _size.Y);
  305.          Vector2u us = Size;
  306.          Vector2f fs = new Vector2f(us.X, us.Y);
  307.          UpdateBack();
  308.          UpdateText();
  309.       }
  310.  
  311.       public ButtonStyle GetButtonStyle () {
  312.          return new ButtonStyle(BackColor, BorderColor, TextColor, TextStyle);
  313.       }
  314.       public void SetButtonStyle(ButtonStyle _style) {
  315.          BackColor = _style.BackColor;
  316.          BorderColor = _style.BorderColor;
  317.          TextColor = _style.TextColor;
  318.          TextStyle = _style.TextStyle;
  319.       }
  320.    }
Add Comment
Please, Sign In to add comment