Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arsdever committed Sep 6, 2020
0 parents commit 0b5f656
Show file tree
Hide file tree
Showing 29 changed files with 2,515 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs
bin
obj
53 changes: 53 additions & 0 deletions AnimationUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace application
{
public static class AnimationUtil
{
public static void AnimateValueLinear(Point begin, Point end, int durationMS, Action<Point> onValueChange, Action onFinish)
{
new Task(() => {
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

while (stopWatch.ElapsedMilliseconds < durationMS)
{
double timepoint = (double)stopWatch.ElapsedMilliseconds / (double)durationMS;
int currentPointX = (int)((end.X - begin.X) * timepoint + begin.X);
int currentPointY = (int)((end.Y - begin.Y) * timepoint + begin.Y);
Point currentPoint = new Point(currentPointX, currentPointY);
onValueChange(currentPoint);
}

onFinish?.Invoke();
}).Start();
}
public static void AnimateValueLowPass(Point begin, Point end, int slowness, Action<Point> onValueChange, Action onFinish)
{
new Task(() => {
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

double currentPointX = begin.X;
double currentPointY = begin.Y;

while (stopWatch.ElapsedMilliseconds < 500)
{
currentPointX += ((double)end.X - (double)currentPointX) / (double)slowness;
currentPointY += ((double)end.Y - (double)currentPointY) / (double)slowness;
Point currentPoint = new Point((int)currentPointX, (int)currentPointY);
onValueChange(currentPoint);
}

onFinish?.Invoke();
}).Start();
}
}
}
16 changes: 16 additions & 0 deletions ClickEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Windows;

namespace application
{
public class ClickEventArgs
{
public Point Location;
public Duration ClickDuration;

public ClickEventArgs(Point location, Duration clickDuration)
{
Location = location;
ClickDuration = clickDuration;
}
}
}
158 changes: 158 additions & 0 deletions CustomButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace application
{
class CustomButton : UserControl
{
public CustomButton()
{
BackColor = Color.Transparent;
InitializeComponents();
}

protected override void OnMouseEnter(EventArgs e)
{
isHover = true;
Refresh();
}

protected override void OnMouseLeave(EventArgs e)
{
isHover = false;
Refresh();
}

protected override void OnMouseMove(MouseEventArgs e)
{
bool newHoverState = !(Math.Min(e.Location.X, e.Location.Y) < 0);
if (isHover != newHoverState)
{
isHover = newHoverState;
//HoverChanged?.Invoke(this, isHover);
Refresh();
}
}



protected override void OnMouseDown(MouseEventArgs e)
{
IsDown = true;
clickStarted = DateTime.Now;
Refresh();
}

protected override void OnMouseUp(MouseEventArgs e)
{
clickFinished = DateTime.Now;
IsDown = false;

ClickEventArgs args = new ClickEventArgs(
new System.Windows.Point(e.Location.X, e.Location.Y),
new System.Windows.Duration(clickFinished - clickStarted)
);

if (isHover)
LeftClick?.Invoke(this, args);

Refresh();
}

private void InitializeComponents()
{
ImageChanged += OnImageChanged;
IconSizeChanged += OnIconSizeChanged;
}

protected override void OnPaint(PaintEventArgs e)
{
if (Image == null)
return;

float canvasMin = IconSize;// * Image.VerticalResolution / e.Graphics.DpiX;
float imageMin = (float)Image.Height;
Bitmap newImg = ImageHelper.ResizeImage(Image, (float)imageMin / (float)canvasMin);

e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
PointF offset = new PointF((e.ClipRectangle.Width - newImg.Width) / 2.0f, (e.ClipRectangle.Height - newImg.Height) / 2.0f);

e.Graphics.DrawImage(newImg, offset);
}

protected override void OnPaintBackground(PaintEventArgs e)
{
Color clr;
if (IsDown && isHover)
{
clr = MouseDownBackColor;
}
else if (IsDown || isHover)
{
clr = MouseOverBackColor;
}
else
{
clr = BackColor;
}

e.Graphics.Clear(clr);
}

private void OnImageChanged(object sender, Image img)
{
Refresh();
}
private void OnIconSizeChanged(object sender, int size)
{
Refresh();
}

public event EventHandler<ClickEventArgs> LeftClick;
public event EventHandler<Image> ImageChanged;
public event EventHandler<int> IconSizeChanged;
//public event EventHandler<bool> HoverChanged;

public Image Image
{
get { return __image; }
set
{
if (__image != value)
{
__image = value;
ImageChanged?.Invoke(this, __image);
}
}
}
public int IconSize
{
get { return __iconSize; }
set
{
if (__iconSize != value)
{
__iconSize = value;
IconSizeChanged?.Invoke(this, __iconSize);
}
}
}

private Image __image;
private int __iconSize = 16;

public Color MouseDownBackColor = Color.FromArgb(32, 255, 255, 255);
public Color MouseOverBackColor = Color.FromArgb(50, 255, 255, 255);

private bool isHover = false;
private bool IsDown = false;
private DateTime clickStarted;
private DateTime clickFinished;
}
}
120 changes: 120 additions & 0 deletions CustomButton.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
42 changes: 42 additions & 0 deletions ImageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;

namespace application
{
static class ImageHelper
{
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);

destImage.SetResolution(96, 96);

using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}

return destImage;
}

public static Bitmap ResizeImage(Image image, float relation)
{
return ResizeImage(image, (int)Math.Round(image.Width / relation), (int)Math.Round(image.Height / relation));
}
}
}
Loading

0 comments on commit 0b5f656

Please sign in to comment.