-
Notifications
You must be signed in to change notification settings - Fork 0
/
Switch.cs
157 lines (148 loc) · 8.88 KB
/
Switch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Button.cs
//
// Author: endofunk
//
// Copyright (c) 2019
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Endofunk.Monogame.UI {
public class Switch : IElement {
private readonly string TextOn;
private readonly string TextOff;
public readonly Canvas _Canvas;
public Vector2 Bounding => _Canvas.Bounding;
private bool IsMouseEnter;
private bool IsOn = true;
private bool IsOff => !IsOn;
private int ClickedUpdates;
private Rectangle Area = Rectangle.Empty;
public System.Drawing.Graphics Graphics => _Canvas.Graphics;
public Switch(Game game, Vector2 bounding) : this("On", "Off", game, bounding) { }
public Switch(string textOn, string textOff, Game game, Vector2 bounding) {
_Canvas = new Canvas(game, bounding.ToPoint());
TextOn = textOn;
TextOff = textOff;
DefaultLayout();
ClickedUpdates = 0;
}
public void Save() {
_Canvas.Save();
}
public void HoverLayout() {
var rounding = 6;
var buttonColor = System.Drawing.Color.Gray;
Graphics.Clear(System.Drawing.Color.Transparent);
Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 191, 191, 191), 4), new System.Drawing.Rectangle(2, 4, (int)Bounding.X - 4, (int)Bounding.Y - 5), rounding);
var linearGradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.PointF(0f, 4f), new System.Drawing.PointF(0f, Bounding.Y + 14), System.Drawing.Color.FromArgb(255, 255, 255, 255), buttonColor);
Graphics.FillRoundedRectangle(linearGradientBrush, new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 200, 200, 200), 2), new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
var strRect = new System.Drawing.Rectangle(new System.Drawing.Point(1, 3), new System.Drawing.Size((int)Bounding.X - 2, (int)Bounding.Y - 6));
var strFormat = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
strFormat.Alignment = System.Drawing.StringAlignment.Center;
strFormat.LineAlignment = System.Drawing.StringAlignment.Center;
Graphics.DrawString(TextOn, new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 16, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, strRect, strFormat);
Save();
}
private void DefaultLayout() {
var rounding = 6;
var buttonColor = System.Drawing.Color.Gray;
Graphics.Clear(System.Drawing.Color.Transparent);
Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 191, 191, 191), 4), new System.Drawing.Rectangle(2, 4, (int)Bounding.X - 4, (int)Bounding.Y - 5), rounding);
var linearGradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.PointF(0f, 4f), new System.Drawing.PointF(0f, Bounding.Y + 14), System.Drawing.Color.FromArgb(255, 255, 255, 255), buttonColor);
Graphics.FillRoundedRectangle(linearGradientBrush, new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 35, 35, 35), 2), new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
var strRect = new System.Drawing.Rectangle(new System.Drawing.Point(1, 3), new System.Drawing.Size((int)Bounding.X - 2, (int)Bounding.Y - 6));
var strFormat = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
strFormat.Alignment = System.Drawing.StringAlignment.Center;
strFormat.LineAlignment = System.Drawing.StringAlignment.Center;
Graphics.DrawString(TextOn, new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 16, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, strRect, strFormat);
Save();
}
public void ClickLayout() {
var rounding = 6;
var buttonColor = System.Drawing.Color.Gray;
Graphics.Clear(System.Drawing.Color.Transparent);
Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 0, 0, 0), 4), new System.Drawing.Rectangle(2, 2, (int)Bounding.X - 4, (int)Bounding.Y - 3), rounding);
var linearGradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.PointF(0f, 0f), new System.Drawing.PointF(0f, Bounding.Y - 6), buttonColor, System.Drawing.Color.FromArgb(255, 253, 253, 253));
Graphics.FillRoundedRectangle(linearGradientBrush, new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
Graphics.DrawRoundedRectangle(new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 255, 255, 255), 2), new System.Drawing.Rectangle(1, 3, (int)Bounding.X - 2, (int)Bounding.Y - 7), rounding);
var strRect = new System.Drawing.Rectangle(new System.Drawing.Point(1, 3), new System.Drawing.Size((int)Bounding.X - 2, (int)Bounding.Y - 6));
var strFormat = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
strFormat.Alignment = System.Drawing.StringAlignment.Center;
strFormat.LineAlignment = System.Drawing.StringAlignment.Center;
Graphics.DrawString(TextOn, new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, 16, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, strRect, strFormat);
Save();
}
public void Draw(SpriteBatch spriteBatch, Vector2 position) {
if (Area.IsEmpty) Area = new Rectangle(position.ToPoint(), Bounding.ToPoint());
_Canvas.Draw(spriteBatch, position);
}
public event EventHandler Clicked;
public event MouseEventHandler MouseEnter;
public event MouseEventHandler MouseLeave;
public event MouseEventHandler MouseMove;
public event UpdateHandler UpdateEvent;
protected virtual void OnClicked(EventArgs e) => Clicked?.Invoke(this, e);
protected virtual void OnMouseEnter(GameTime g, MouseState m) => MouseEnter?.Invoke(this, g, m);
protected virtual void OnMouseLeave(GameTime g, MouseState m) => MouseLeave?.Invoke(this, g, m);
protected virtual void OnMouseMove(GameTime g, MouseState m) => MouseMove?.Invoke(this, g, m);
protected virtual void OnUpdateEvent(GameTime g) => UpdateEvent?.Invoke(this, g);
public void Update(GameTime gameTime) {
ClickedUpdates = ClickedUpdates > 0 ? ClickedUpdates - 1 : ClickedUpdates;
_Canvas.Update(gameTime);
MouseState mouse = Mouse.GetState();
IsMouseEnter = mouse.HasMouseEntered(gameTime, IsMouseEnter, Area, OnMouseEnter, OnMouseLeave);
if (mouse.LeftButton == ButtonState.Pressed && IsMouseEnter && ClickedUpdates < 1) {
ClickedUpdates = 10;
//Current.Instance.Store.Fold(s => s.Sounds.Fold(a => a["Click1"].Play()));
OnClicked(EventArgs.Empty);
}
if (IsMouseEnter) {
if (ClickedUpdates > 0) {
ClickLayout();
} else {
HoverLayout();
}
}
if (!IsMouseEnter) {
if (ClickedUpdates > 0) {
ClickLayout();
} else {
DefaultLayout();
}
}
OnUpdateEvent(gameTime);
if (IsMouseEnter) OnMouseMove(gameTime, mouse);
}
}
public static partial class Prelude {
#region Syntactic Sugar
//public static Button2 Button2(Game game, Vector2 bounding) => new Button2(game, bounding);
//public static Button2 Button2(string text, Game game, Vector2 bounding) => new Button2(text, game, bounding);
#endregion
}
}