217 lines
6.7 KiB
C#
217 lines
6.7 KiB
C#
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Text;
|
|
using System.Windows.Forms;
|
|
using MaterialSkin.Animations;
|
|
using System;
|
|
|
|
namespace MaterialSkin.Controls
|
|
{
|
|
public class MaterialRaisedButton : Button, IMaterialControl
|
|
{
|
|
[Browsable(false)]
|
|
public int Depth { get; set; }
|
|
[Browsable(false)]
|
|
public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
|
|
[Browsable(false)]
|
|
public MouseState MouseState { get; set; }
|
|
public bool Primary { get; set; }
|
|
|
|
|
|
public bool DrawBorder
|
|
{
|
|
get { return _drawBorder; }
|
|
set
|
|
{
|
|
_drawBorder = value;
|
|
}
|
|
}
|
|
|
|
private readonly AnimationManager _animationManager;
|
|
|
|
private SizeF _textSize;
|
|
|
|
private Image _icon;
|
|
private bool _drawBorder;
|
|
private Point _lastLocation;
|
|
private bool _wasClick;
|
|
|
|
public Image Icon
|
|
{
|
|
get { return _icon; }
|
|
set
|
|
{
|
|
_icon = value;
|
|
if (AutoSize)
|
|
Size = GetPreferredSize();
|
|
Invalidate();
|
|
}
|
|
}
|
|
|
|
public MaterialRaisedButton()
|
|
{
|
|
Primary = true;
|
|
Cursor = Cursors.Hand;
|
|
_animationManager = new AnimationManager(false)
|
|
{
|
|
Increment = 0.02,
|
|
AnimationType = AnimationType.EaseOut
|
|
};
|
|
_animationManager.OnAnimationProgress += sender => Invalidate();
|
|
_animationManager.OnAnimationFinished +=AnimationManagerOnOnAnimationFinished;
|
|
AutoSizeMode = AutoSizeMode.GrowAndShrink;
|
|
|
|
}
|
|
|
|
private void AnimationManagerOnOnAnimationFinished(object o)
|
|
{
|
|
if (_wasClick)
|
|
{
|
|
_wasClick = false;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public override string Text
|
|
{
|
|
get { return base.Text; }
|
|
set
|
|
{
|
|
base.Text = value;
|
|
_textSize = CreateGraphics().MeasureString(value.ToUpper(), SkinManager.ROBOTO_MEDIUM_10);
|
|
if (AutoSize)
|
|
Size = GetPreferredSize();
|
|
Invalidate();
|
|
}
|
|
}
|
|
|
|
protected override void OnMouseUp(MouseEventArgs mevent)
|
|
{
|
|
base.OnMouseUp(mevent);
|
|
|
|
|
|
}
|
|
|
|
protected override void OnMouseDown(MouseEventArgs mevent)
|
|
{
|
|
base.OnMouseDown(mevent);
|
|
_lastLocation = mevent.Location;
|
|
|
|
}
|
|
|
|
protected override void OnClick(EventArgs e)
|
|
{
|
|
_animationManager.StartNewAnimation(AnimationDirection.In,_lastLocation);
|
|
base.OnClick(e);
|
|
}
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs pevent)
|
|
{
|
|
try
|
|
{
|
|
var g = pevent.Graphics;
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
//g.TextRenderingHint = TextRenderingHint.AntiAlias;
|
|
|
|
g.Clear(Parent.BackColor);
|
|
|
|
using (var backgroundPath = DrawHelper.CreateRoundRect(ClientRectangle.X,
|
|
ClientRectangle.Y,
|
|
ClientRectangle.Width - 1,
|
|
ClientRectangle.Height - 1,
|
|
1f))
|
|
{
|
|
|
|
g.FillPath(
|
|
Enabled
|
|
? (Primary
|
|
? SkinManager.ColorScheme.PrimaryBrush
|
|
: SkinManager.GetRaisedButtonBackgroundBrush())
|
|
: Brushes.DarkGray, backgroundPath);
|
|
if (!Primary && this.DrawBorder)
|
|
{
|
|
g.DrawPath(SkinManager.ColorScheme.PrimaryPen, backgroundPath);
|
|
}
|
|
}
|
|
|
|
if (_animationManager.IsAnimating())
|
|
{
|
|
for (int i = 0; i < _animationManager.GetAnimationCount(); i++)
|
|
{
|
|
var animationValue = _animationManager.GetProgress(i);
|
|
var animationSource = _animationManager.GetSource(i);
|
|
var rippleBrush = new SolidBrush(Color.FromArgb((int) (51 - (animationValue * 50)),
|
|
Primary ? Color.White : SkinManager.ColorScheme.PrimaryColor));
|
|
var rippleSize = (int) (animationValue * Width * 2);
|
|
g.FillEllipse(rippleBrush,
|
|
new Rectangle(animationSource.X - rippleSize / 2, animationSource.Y - rippleSize / 2,
|
|
rippleSize, rippleSize));
|
|
}
|
|
}
|
|
|
|
//Icon
|
|
var iconRect = new Rectangle(8, 6, 24, 24);
|
|
|
|
if (string.IsNullOrEmpty(Text))
|
|
// Center Icon
|
|
iconRect.X += 2;
|
|
|
|
if (Icon != null)
|
|
g.DrawImage(Icon, iconRect);
|
|
|
|
//Text
|
|
var textRect = ClientRectangle;
|
|
|
|
if (Icon != null)
|
|
{
|
|
//
|
|
// Resize and move Text container
|
|
//
|
|
|
|
// First 8: left padding
|
|
// 24: icon width
|
|
// Second 4: space between Icon and Text
|
|
// Third 8: right padding
|
|
textRect.Width -= 8 + 24 + 4 + 8;
|
|
|
|
// First 8: left padding
|
|
// 24: icon width
|
|
// Second 4: space between Icon and Text
|
|
textRect.X += 8 + 24 + 4;
|
|
}
|
|
|
|
g.DrawString(
|
|
Text.ToUpper(),
|
|
Font,
|
|
Primary ? SkinManager.GetRaisedButtonTextBrush(Primary) : SkinManager.ColorScheme.PrimaryBrush,
|
|
textRect,
|
|
new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center});
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
private Size GetPreferredSize()
|
|
{
|
|
return GetPreferredSize(new Size(0, 0));
|
|
}
|
|
|
|
public override Size GetPreferredSize(Size proposedSize)
|
|
{
|
|
// Provides extra space for proper padding for content
|
|
var extra = 16;
|
|
|
|
if (Icon != null)
|
|
// 24 is for icon size
|
|
// 4 is for the space between icon & text
|
|
extra += 24 + 4;
|
|
|
|
return new Size((int)Math.Ceiling(_textSize.Width) + extra, 36);
|
|
}
|
|
}
|
|
}
|