Skip to content

Instantly share code, notes, and snippets.

@cwensley
Created February 6, 2015 01:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwensley/95000998e37acd93e830 to your computer and use it in GitHub Desktop.
Save cwensley/95000998e37acd93e830 to your computer and use it in GitHub Desktop.
Custom image button based on an Eto.Forms.Drawable
using System;
using Eto.Forms;
using Eto.Drawing;
namespace Pablo.Controls
{
public class CustomButton : Drawable
{
bool pressed;
bool hover;
bool mouseDown;
public static Color DisabledColor = Color.FromGrayscale(0.4f, 0.3f);
public static Color EnabledColor = Colors.Black;
public override bool Enabled
{
get
{
return base.Enabled;
}
set
{
if (base.Enabled != value)
{
base.Enabled = value;
if (Loaded)
Invalidate();
}
}
}
public bool Pressed
{
get { return pressed; }
set
{
if (pressed != value)
{
pressed = value;
mouseDown = false;
if (Loaded)
Invalidate();
}
}
}
public Color DrawColor
{
get { return Enabled ? EnabledColor : DisabledColor; }
}
public bool Toggle { get; set; }
public bool Persistent { get; set; }
public event EventHandler<EventArgs> Click;
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
public CustomButton()
{
Enabled = true;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (Loaded)
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (Enabled)
{
mouseDown = true;
Invalidate();
}
}
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
hover = true;
Invalidate();
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
hover = false;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
var rect = new Rectangle(this.Size);
if (mouseDown && rect.Contains((Point)e.Location))
{
if (Toggle)
pressed = !pressed;
else if (Persistent)
pressed = true;
else
pressed = false;
mouseDown = false;
this.Invalidate();
if (Enabled)
OnClick(EventArgs.Empty);
}
else
{
mouseDown = false;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
var rect = new Rectangle(this.Size);
var col = Color.FromGrayscale(hover && Enabled ? 0.95f : 0.8f);
if (Enabled && (pressed || mouseDown))
{
pe.Graphics.FillRectangle(col, rect);
pe.Graphics.DrawInsetRectangle(Colors.Gray, Colors.White, rect);
}
else if (hover && Enabled)
{
pe.Graphics.FillRectangle(col, rect);
pe.Graphics.DrawInsetRectangle(Colors.White, Colors.Gray, rect);
}
base.OnPaint(pe);
}
}
}
using System;
using Eto.Drawing;
namespace Pablo.Controls
{
public class ImageButton : CustomButton
{
bool sizeSet;
public Image Image { get; set; }
public Image DisabledImage { get; set; }
public override Size Size {
get {
return base.Size;
}
set {
base.Size = value;
sizeSet = true;
}
}
public ImageButton ()
{
}
protected override void OnLoad (EventArgs e)
{
base.OnLoad (e);
if (Image != null) {
if (!sizeSet)
this.Size = Image.Size + 4;
}
}
protected override void OnLoadComplete (EventArgs e)
{
base.OnLoadComplete (e);
if (DisabledImage == null) {
var image = Image;
if (image != null) {
var disabledImage = new Bitmap (image.Size.Width, image.Size.Height, PixelFormat.Format32bppRgba);
using (var graphics = new Graphics(disabledImage)) {
graphics.DrawImage (image, 0, 0);
}
using (var bd = disabledImage.Lock())
{
unsafe
{
var data = (int*)bd.Data;
for (int i = 0; i < bd.ScanWidth * disabledImage.Size.Height; i++)
{
var col = Color.FromArgb(bd.TranslateDataToArgb(*data));
var gray = (col.R + col.G + col.B) / 3;
*data = bd.TranslateArgbToData(Color.FromGrayscale(gray, 0.8f).ToArgb());
}
}
}
}
}
}
protected override void OnPaint (Eto.Forms.PaintEventArgs pe)
{
base.OnPaint (pe);
var image = this.Enabled ? Image : DisabledImage;
var size = image.Size.FitTo (this.Size - 2);
var xoffset = (this.Size.Width - size.Width) / 2;
var yoffset = (this.Size.Height - size.Height) / 2;
pe.Graphics.DrawImage (image, xoffset, yoffset, size.Width, size.Height);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment