<Serializable()> _
Public MustInherit Class Item
Private _Color As Color
Private _Width As Single
Public Property Color() As Color
Get
Return _Color
End Get
Set(ByVal value As Color)
_Color = value
End Set
End Property
Public Property Width() As Single
Get
Return _Width
End Get
Set(ByVal value As Single)
_Width = value
End Set
End Property
Public MustOverride Sub Draw(ByVal G As Graphics)
End Class
<Serializable()> _
Public MustInherit Class FillableItem
Inherits Item
Private _Fill As Boolean = False
Private _DrawOutline As Boolean = True
Private _Blend As Boolean = False
Private _FillColor As Color
Private _BlendColor As Color
Private _BlendAngle As Single
<NonSerialized()> _
Protected _Region As Region
Public Property FillColor() As Color
Get
Return _FillColor
End Get
Set(ByVal value As Color)
_FillColor = value
End Set
End Property
Public Property BlendColor() As Color
Get
Return _BlendColor
End Get
Set(ByVal value As Color)
_BlendColor = value
End Set
End Property
Public Property Fill() As Boolean
Get
Return _Fill
End Get
Set(ByVal value As Boolean)
_Fill = value
End Set
End Property
Public Property Blend() As Boolean
Get
Return _Blend
End Get
Set(ByVal value As Boolean)
_Blend = value
End Set
End Property
Public Property DrawOutline() As Boolean
Get
Return _DrawOutline
End Get
Set(ByVal value As Boolean)
_DrawOutline = value
End Set
End Property
Public Property BlendAngle() As Single
Get
Return _BlendAngle
End Get
Set(ByVal value As Single)
_BlendAngle = value
End Set
End Property
Public ReadOnly Property CoveredRegion() As Region
Get
Return _Region
End Get
End Property
Protected Sub TranslateAndScalePath(ByRef Path As Drawing2D.GraphicsPath, ByVal Area As Rectangle, Optional ByVal ZoomIfSmaller As Boolean = False)
Dim StartArea As RectangleF = Path.GetBounds
Dim Matrix As New Drawing2D.Matrix
Dim Vector As New Point
Dim Wider As Boolean
Dim Zoom As Single
If (StartArea.Width <= Area.Width) And (StartArea.Height <= Area.Height) And (Not ZoomIfSmaller) Then
Zoom = 1
Else
If Area.Width / StartArea.Width < Area.Height / StartArea.Height Then
Zoom = Area.Width / StartArea.Width
Wider = True
Else
Zoom = Area.Height / StartArea.Height
Wider = False
End If
End If
Matrix.Scale(Zoom, Zoom)
Path.Transform(Matrix)
Matrix = New Drawing2D.Matrix
StartArea = Path.GetBounds
Vector.X = -StartArea.X
Vector.Y = -StartArea.Y
Matrix.Translate(Vector.X, Vector.Y)
Path.Transform(Matrix)
If Zoom = 1 Then
Vector.X = Area.Width / 2 - StartArea.Width / 2
Vector.Y = Area.Height / 2 - StartArea.Height / 2
Matrix = New Drawing2D.Matrix
Matrix.Translate(Vector.X, Vector.Y)
Path.Transform(Matrix)
Else
If Wider Then
Vector.X = 0
Vector.Y = Area.Height / 2 - StartArea.Height / 2
Else
Vector.Y = 0
Vector.X = Area.Width / 2 - StartArea.Width / 2
End If
Matrix = New Drawing2D.Matrix
Matrix.Translate(Vector.X, Vector.Y)
Path.Transform(Matrix)
End If
End Sub
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
End Sub
Public MustOverride Sub DrawPreview(ByVal G As Graphics, ByVal Area As Rectangle)
End Class
Public Interface IUndoable
Sub Undo()
End Interface
Public Interface ICloseable
Property IsClosed() As Boolean
End Interface
<Serializable()> _
Public MustInherit Class PointConnectionItem
Inherits FillableItem
Implements IUndoable, ICloseable
Private _Points As List(Of Point)
Private _IsClosed As Boolean = False
Public Property IsClosed() As Boolean Implements ICloseable.IsClosed
Get
Return _IsClosed
End Get
Set(ByVal value As Boolean)
_IsClosed = value
End Set
End Property
Public ReadOnly Property Points() As List(Of Point)
Get
Return _Points
End Get
End Property
Public Overridable Sub Undo() Implements IUndoable.Undo
If Me.Points.Count >= 1 Then
Me.Points.RemoveAt(Me.Points.Count - 1)
End If
End Sub
Sub New()
_Points = New List(Of Point)
Me.Width = 1.7
Me.Color = Drawing.Color.Black
End Sub
Protected Sub DrawFirstPoint(ByVal G As Graphics)
Dim P As Point = Me.Points(0)
G.DrawLine(Pens.Red, P.X - 1, P.Y, P.X + 1, P.Y)
G.DrawLine(Pens.Red, P.X, P.Y - 1, P.X, P.Y + 1)
End Sub
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
End Sub
Public Overrides Sub DrawPreview(ByVal G As Graphics, ByVal Area As Rectangle)
End Sub
End Class
<Serializable()> _
Public Class ClosedCurve
Inherits PointConnectionItem
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
Dim Path As New Drawing2D.GraphicsPath
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Me.DrawFirstPoint(G)
Exit Sub
End If
Path.AddCurve(Me.Points.ToArray)
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
Public Overrides Sub DrawPreview(ByVal G As Graphics, ByVal Area As Rectangle)
Dim Path As New Drawing2D.GraphicsPath
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Me.DrawFirstPoint(G)
Exit Sub
End If
Path.AddCurve(Me.Points.ToArray)
Me.TranslateAndScalePath(Path, Area)
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
End Class
<Serializable()> _
Public Class ClosedLine
Inherits PointConnectionItem
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
Dim Path As New Drawing2D.GraphicsPath
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Me.DrawFirstPoint(G)
Exit Sub
End If
Path.AddLines(Me.Points.ToArray)
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
Public Overrides Sub DrawPreview(ByVal G As Graphics, ByVal Area As Rectangle)
Dim Path As New Drawing2D.GraphicsPath
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Me.DrawFirstPoint(G)
Exit Sub
End If
Path.AddLines(Me.Points.ToArray)
Me.TranslateAndScalePath(Path, Area)
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
End Class
<Serializable()> _
Public Class Mix
Inherits FillableItem
Implements IUndoable, ICloseable
<Serializable()> _
Public Structure MixPoint
Private _Base As Point
Private _BelongsToCurve As Boolean
Public Property Base() As Point
Get
Return _Base
End Get
Set(ByVal value As Point)
_Base = value
End Set
End Property
Public Property BelongsToCurve() As Boolean
Get
Return _BelongsToCurve
End Get
Set(ByVal value As Boolean)
_BelongsToCurve = value
End Set
End Property
Sub New(ByVal Pt As Point, ByVal IsInCurve As Boolean)
Me.Base = Pt
Me.BelongsToCurve = IsInCurve
End Sub
End Structure
Private _Points As List(Of MixPoint)
Private _IsClosed As Boolean = False
Public Property IsClosed() As Boolean Implements ICloseable.IsClosed
Get
Return _IsClosed
End Get
Set(ByVal value As Boolean)
_IsClosed = value
End Set
End Property
Public ReadOnly Property Points() As List(Of MixPoint)
Get
Return _Points
End Get
End Property
Sub New()
_Points = New List(Of MixPoint)
Me.Width = 1.7
Me.Color = Drawing.Color.Black
End Sub
Public Overridable Sub Undo() Implements IUndoable.Undo
If Me.Points.Count >= 1 Then
Me.Points.RemoveAt(Me.Points.Count - 1)
End If
End Sub
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
Dim Path As New Drawing2D.GraphicsPath
Dim Temp As New List(Of Point)
Dim Type As Boolean
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Dim P As Point = Me.Points(0).Base
G.DrawLine(Pens.Red, P.X - 1, P.Y, P.X + 1, P.Y)
G.DrawLine(Pens.Red, P.X, P.Y - 1, P.X, P.Y + 1)
Exit Sub
End If
Type = Me.Points(0).BelongsToCurve
For Each M As MixPoint In Me.Points
'Raggruppa i punti dello stesso tipo in una lista
If M.BelongsToCurve = Type Then
Temp.Add(M.Base)
Else
'Quando c'è un punto di tipo diverso, termina la linea o
'la curva e inizia un altro tipo
If Type = True Then
Path.AddCurve(Temp.ToArray)
Else
Path.AddLines(Temp.ToArray)
End If
Dim Last As Point = Temp(Temp.Count - 1)
Type = M.BelongsToCurve
Temp.Clear()
If Type = True Then
'Le curve necessitano di iniziare dallo stesso punto finale
'delle linee
Temp.Add(Last)
End If
Temp.Add(M.Base)
End If
Next
If Type = True Then
Path.AddCurve(Temp.ToArray)
Else
Path.AddLines(Temp.ToArray)
End If
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
Public Overrides Sub DrawPreview(ByVal G As Graphics, ByVal Area As Rectangle)
Dim Path As New Drawing2D.GraphicsPath
Dim Temp As New List(Of Point)
Dim Type As Boolean
If Me.Points.Count = 0 Then
Exit Sub
End If
If Me.Points.Count = 1 Then
Dim P As Point = Me.Points(0).Base
G.DrawLine(Pens.Red, P.X - 1, P.Y, P.X + 1, P.Y)
G.DrawLine(Pens.Red, P.X, P.Y - 1, P.X, P.Y + 1)
Exit Sub
End If
Type = Me.Points(0).BelongsToCurve
For Each M As MixPoint In Me.Points
'Raggruppa i punti dello stesso tipo in una lista
If M.BelongsToCurve = Type Then
Temp.Add(M.Base)
Else
'Quando c'è un punto di tipo diverso, termina la linea o
'la curva e inizia un altro tipo
If Type = True Then
Path.AddCurve(Temp.ToArray)
Else
Path.AddLines(Temp.ToArray)
End If
Dim Last As Point = Temp(Temp.Count - 1)
Type = M.BelongsToCurve
Temp.Clear()
If Type = True Then
'Le curve necessitano di iniziare dallo stesso punto finale
'delle linee
Temp.Add(Last)
End If
Temp.Add(M.Base)
End If
Next
If Type = True Then
Path.AddCurve(Temp.ToArray)
Else
Path.AddLines(Temp.ToArray)
End If
Me.TranslateAndScalePath(Path, Area)
If Me.IsClosed Then
Path.CloseFigure()
End If
If Me.Fill Then
If Me.Blend Then
Dim BlendBrush As New Drawing2D.LinearGradientBrush(Path.GetBounds, Me.FillColor, Me.BlendColor, -Me.BlendAngle)
G.FillPath(BlendBrush, Path)
Else
G.FillPath(New SolidBrush(Me.FillColor), Path)
End If
End If
If Me.DrawOutline Then
G.DrawPath(New Pen(Me.Color, Me.Width), Path)
End If
_Region = New Region(Path.GetBounds)
End Sub
End Class
<Serializable()> _
Public Class BackgroundImage
Private _Alpha As Byte
Private _Image As Image
Private _Center As Boolean
Private _Zoom As Single
Private _BufferImage As Image
Public Property Alpha() As Byte
Get
Return _Alpha
End Get
Set(ByVal value As Byte)
_Alpha = value
End Set
End Property
Public Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
End Set
End Property
Public Property Center() As Boolean
Get
Return _Center
End Get
Set(ByVal value As Boolean)
_Center = value
End Set
End Property
Public Property Zoom() As Single
Get
Return _Zoom
End Get
Set(ByVal value As Single)
_Zoom = value
End Set
End Property
Public ReadOnly Property BufferImage() As Image
Get
Return _BufferImage
End Get
End Property
Public Sub CreateBuffer()
Dim Buffer As New Bitmap(CInt(Image.Width * Zoom), CInt(Image.Height * Zoom))
Dim G As Graphics = Graphics.FromImage(Buffer)
Dim M As New Imaging.ColorMatrix
Dim A As New Imaging.ImageAttributes
Dim R As New Rectangle(New Point(0, 0), New Size(Image.Width * Zoom, Image.Height * Zoom))
M.Matrix00 = 1
M.Matrix11 = 1
M.Matrix22 = 1
M.Matrix33 = Me.Alpha / 255
M.Matrix44 = 1
A.SetColorMatrix(M)
G.DrawImage(Me.Image, R, 0, 0, Me.Image.Width, Me.Image.Height, GraphicsUnit.Pixel, A)
Dim Memory As New IO.MemoryStream()
If Me.Alpha >= 240 Then
Buffer.Save(Memory, Drawing.Imaging.ImageFormat.Jpeg)
Else
Buffer.Save(Memory, Drawing.Imaging.ImageFormat.Png)
End If
_BufferImage = Drawing.Image.FromStream(Memory)
End Sub
Public Sub Draw(ByVal G As System.Drawing.Graphics)
'G.DrawImage(BufferImage, Me.Location)
End Sub
End Class
Public Class ItemByIndexComparer
Implements IComparer(Of ListViewItem)
Public Function Compare(ByVal x As ListViewItem, ByVal y As ListViewItem) As Integer Implements System.Collections.Generic.IComparer(Of ListViewItem).Compare
Return DirectCast(x.Tag, Layer).Index.CompareTo(DirectCast(y.Tag, Layer).Index)
End Function
End Class
Public Class LayerByIndexComparer
Implements IComparer(Of Layer)
Public Function Compare(ByVal x As Layer, ByVal y As Layer) As Integer Implements System.Collections.Generic.IComparer(Of Layer).Compare
Return x.Index.CompareTo(y.Index)
End Function
End Class
<Serializable()> _
Public Class Layer
Inherits Item
Private _Items As List(Of FillableItem)
Private _Name As String
Private _BgImage As BackgroundImage
Private _Index As Int32
Public ReadOnly Property Items() As List(Of FillableItem)
Get
Return _Items
End Get
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property BgImage() As BackgroundImage
Get
Return _BgImage
End Get
Set(ByVal value As BackgroundImage)
_BgImage = value
End Set
End Property
Public Property Index() As Int32
Get
Return _Index
End Get
Set(ByVal value As Int32)
_Index = value
End Set
End Property
Sub New()
_Items = New List(Of FillableItem)
End Sub
Public Overrides Sub Draw(ByVal G As System.Drawing.Graphics)
For Each I As Item In Me.Items
I.Draw(G)
Next
End Sub
End Class
<Serializable()> _
Public Class Project
Private _CanavasWidth As Int32
Private _PickedColor As Color
Private _Layers As List(Of Layer)
Private _Name As String
Private _WindowState As FormWindowState
Private _WindowSize As Size
Public Property CanavasWidth() As Int32
Get
Return _CanavasWidth
End Get
Set(ByVal value As Int32)
_CanavasWidth = value
End Set
End Property
Public Property PickedColor() As Color
Get
Return _PickedColor
End Get
Set(ByVal value As Color)
_PickedColor = value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property WindowState() As FormWindowState
Get
Return _WindowState
End Get
Set(ByVal value As FormWindowState)
_WindowState = value
End Set
End Property
Public Property WindowSize() As Size
Get
Return _WindowSize
End Get
Set(ByVal value As Size)
_WindowSize = value
End Set
End Property
Public ReadOnly Property Layers() As List(Of Layer)
Get
Return _Layers
End Get
End Property
Sub New()
_Layers = New List(Of Layer)
End Sub
End Class