X2021/Wardrobe/GUI.vb

255 lines
8.9 KiB
VB.net

Imports XCCLibrary
Public Class GUI
Dim containerPanel As Panel
Dim containerX, containerY, containerW, containerH, containerMidX, containerMidY As Integer
Dim gratingMaxW, gratingMaxH As Decimal
Dim drawW, drawH As Integer
Dim drawAspect, gratingAspect As Decimal
Dim scaleDiff As Decimal
Public pCon1(3) As Decimal 'pCon(0) = pixel X, pCon(2) = SW X, etc.
Public pCon2(3) As Decimal
Public pCon3(3) As Decimal
Public pCon4(3) As Decimal
Dim points As New Dictionary(Of String, Decimal())
Dim pointsOrder As New List(Of String)
Dim PointButtons As New List(Of Button)
' --- Start method when GUI loads ---
Sub GUI_load() Handles MyBase.Load
containerPanel = DrawingPanel
AddHandler containerPanel.Paint, AddressOf DrawingPanel_Paint
Get_DrawboxParameters()
Set_ContainerPointsX()
Set_ContainerPointsY()
Create_StartPoints()
End Sub
' --- Retrive parameters for the drawing box ---
Private Sub Get_DrawboxParameters()
containerX = containerPanel.Location.X 'Behövs ej nu
containerY = containerPanel.Location.Y 'Behövs ej nu
containerW = containerPanel.Size.Width
containerH = containerPanel.Size.Height
containerMidX = containerW / 2
containerMidY = containerH / 2
drawW = containerW - 60
drawH = containerH - 60
drawAspect = drawW / drawH
End Sub
' --- Set containers points X-values ---
Private Sub Set_ContainerPointsX()
pCon1(0) = containerMidX - drawW / 2
pCon2(0) = containerMidX + drawW / 2
pCon3(0) = containerMidX + drawW / 2
pCon4(0) = containerMidX - drawW / 2
End Sub
' --- Set containers points Y-values ---
Private Sub Set_ContainerPointsY()
pCon1(1) = containerMidY - drawH / 2
pCon2(1) = containerMidY - drawH / 2
pCon3(1) = containerMidY + drawH / 2
pCon4(1) = containerMidY + drawH / 2
End Sub
' --- Sets the start points for the grating ---
Private Sub Create_StartPoints()
For i = 1 To 4
points.Add("p" & i, CallByName(Me, "pCon" & i, vbGet))
pointsOrder.Add("p" & i)
Next
End Sub
' --- Draw all the lines for the container and grating ---
Private Sub DrawingPanel_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs)
Dim penCon As Pen = New Pen(Color.DarkRed, 1)
penCon.DashPattern = {4, 8}
e.Graphics.DrawLine(penCon, CInt(pCon1(0)), CInt(pCon1(1)), CInt(pCon2(0)), CInt(pCon2(1)))
e.Graphics.DrawLine(penCon, CInt(pCon2(0)), CInt(pCon2(1)), CInt(pCon3(0)), CInt(pCon3(1)))
e.Graphics.DrawLine(penCon, CInt(pCon3(0)), CInt(pCon3(1)), CInt(pCon4(0)), CInt(pCon4(1)))
e.Graphics.DrawLine(penCon, CInt(pCon4(0)), CInt(pCon4(1)), CInt(pCon1(0)), CInt(pCon1(1)))
Dim pen As Pen = New Pen(Color.Black, 1)
For i = 0 To pointsOrder.Count - 1
Dim pTemp1() As Decimal
Dim pTemp2() As Decimal
pTemp1 = points(pointsOrder(i))
Try
pTemp2 = points(pointsOrder(i + 1))
Catch ex As Exception
pTemp2 = points(pointsOrder(0))
End Try
e.Graphics.DrawLine(pen, CInt(pTemp1(0)), CInt(pTemp1(1)), CInt(pTemp2(0)), CInt(pTemp2(1)))
Next
End Sub
' --- Generate a table containing all the points X- and Y-values ---
Private Function Create_PointTable()
Dim pointTable As New DataTable
pointTable.Columns.Add("X", GetType(Decimal))
pointTable.Columns.Add("Y", GetType(Decimal))
For i = 0 To pointsOrder.Count - 1
Dim pTemp() As Decimal
pTemp = points(pointsOrder(i))
pointTable.Rows.Add()
pointTable.Rows(i)("X") = pTemp(2)
pointTable.Rows(i)("Y") = pTemp(3)
Next
Return pointTable
End Function
' ---------------------------------- GUI interactions ----------------------------------
' --- When update button is pressed ---
Private Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
gratingMaxH = HeightBox.Text
gratingMaxW = WidthBox.Text
gratingAspect = gratingMaxW / gratingMaxH
If gratingAspect > drawAspect Then
'Change draw height
scaleDiff = gratingMaxW / drawW
pCon1(1) = containerMidY - gratingMaxH / (scaleDiff * 2)
pCon2(1) = containerMidY - gratingMaxH / (scaleDiff * 2)
pCon3(1) = containerMidY + gratingMaxH / (scaleDiff * 2)
pCon4(1) = containerMidY + gratingMaxH / (scaleDiff * 2)
Set_ContainerPointsX()
Else
'Change draw width
scaleDiff = gratingMaxH / drawH
pCon1(0) = containerMidX - gratingMaxW / (scaleDiff * 2)
pCon2(0) = containerMidX + gratingMaxW / (scaleDiff * 2)
pCon3(0) = containerMidX + gratingMaxW / (scaleDiff * 2)
pCon4(0) = containerMidX - gratingMaxW / (scaleDiff * 2)
Set_ContainerPointsY()
End If
'Redraw grating
Me.Refresh()
'SW X-values
pCon1(2) = (-gratingMaxW / 2) / 1000
pCon2(2) = (gratingMaxW / 2) / 1000
pCon3(2) = (gratingMaxW / 2) / 1000
pCon4(2) = (-gratingMaxW / 2) / 1000
'SW Y-values
pCon1(3) = (gratingMaxH / 2) / 1000
pCon2(3) = (gratingMaxH / 2) / 1000
pCon3(3) = (-gratingMaxH / 2) / 1000
pCon4(3) = (-gratingMaxH / 2) / 1000
End Sub
' --- When angle button is pressed ---
Private Sub AngleButton_Click(sender As Object, e As EventArgs) Handles AngleButton.Click
' Create Buttons for all clickable points
For i = 0 To pointsOrder.Count - 1
PointButtons.Add(New Button)
PointButtons(i).Width = 30
PointButtons(i).Height = 30
PointButtons(i).Left = points(pointsOrder(i))(0) - PointButtons(i).Width / 2
PointButtons(i).Top = points(pointsOrder(i))(1) - PointButtons(i).Height / 2
PointButtons(i).Name = pointsOrder(i) & "_Button"
PointButtons(i).Text = ""
PointButtons(i).BackColor = Color.FromArgb(50, Color.Red)
PointButtons(i).FlatStyle = FlatStyle.Flat
PointButtons(i).FlatAppearance.BorderSize = 0
PointButtons(i).FlatAppearance.MouseOverBackColor = Color.Red
PointButtons(i).FlatAppearance.MouseDownBackColor = Color.DarkRed
Dim gp As New Drawing.Drawing2D.GraphicsPath
gp.AddEllipse(New Rectangle(New Point(0, 0), New Size(30, 30)))
PointButtons(i).Region = New Region(gp)
containerPanel.Controls.Add(PointButtons(i))
AddHandler PointButtons(i).Click, AddressOf Me.PointButton_Click
Next
End Sub
Private Sub PointButton_Click(sender As Button, e As EventArgs)
'Determine which point is being pressed
Dim pointPressed As String = sender.Name.Split("_")(0)
Dim index As Integer = pointsOrder.IndexOf(pointPressed)
'Retrive that points coords
Dim tempX, tempY As Integer
tempX = points(pointPressed)(0)
tempY = points(pointPressed)(1)
Dim d1, d2 As Integer 'Kommer anges som double i SW coord men vi vill ha pixel coord här
d1 = 80
d2 = 50
If tempX < containerMidX Then
If tempY > containerMidY Then
'Kvadrant 4
points.Add("pA1", {tempX + d1, tempY, 0, 0})
points.Add("pA2", {tempX, tempY - d2, 0, 0})
Else
'Kvadrant 1
points.Add("pA1", {tempX, tempY + d2, 0, 0})
points.Add("pA2", {tempX + d1, tempY, 0, 0})
End If
Else
If tempY > containerMidY Then
'Kvadrant 3
points.Add("pA1", {tempX, tempY - d2, 0, 0})
points.Add("pA2", {tempX - d1, tempY, 0, 0})
Else
'Kvadrant 2
points.Add("pA1", {tempX - d1, tempY, 0, 0})
points.Add("pA2", {tempX, tempY + d2, 0, 0})
End If
End If
'Insert two new points
pointsOrder.Insert(index + 1, "pA1")
pointsOrder.Insert(index + 2, "pA2")
pointsOrder.RemoveAt(index)
'Remove points buttons
For i = 0 To PointButtons.Count - 1
RemoveHandler PointButtons(i).Click, AddressOf Me.PointButton_Click
containerPanel.Controls.RemoveByKey(PointButtons(i).Name)
Next
'Redraw grating
Me.Refresh()
End Sub
' --- When export to SW button is pressed ---
Private Sub ExportSWButton_Click(sender As Object, e As EventArgs) Handles ExportSWButton.Click
Dim pointTable As New DataTable
pointTable = Create_PointTable()
Program.Export_SW(pointTable)
Program.Create_Drawing()
End Sub
End Class