File size: 25,421 Bytes
b1b3bae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Xml.Linq
Imports DWSIM.Interfaces
Imports Interfaces = DWSIM.Interfaces
Namespace GraphicObjects
Public MustInherit Class GraphicObject
Implements IGraphicObject, ICustomXMLSerialization, IDisposable
Private Shared _rtp, _btp, _itp, _bitp, _ttp As SKTypeface
Public Shared Sub SetGlobalTypeface(gtext As String)
_ttp = SKFontManager.Default.MatchCharacter(gtext(0))
End Sub
Public Property RegularTypeFace As SKTypeface
Get
If Not GlobalSettings.Settings.TranslatorActivated Then
Return _rtp
Else
Return _ttp
End If
End Get
Set(value As SKTypeface)
_rtp = value
End Set
End Property
Public Property BoldTypeFace As SKTypeface
Get
If Not GlobalSettings.Settings.TranslatorActivated Then
Return _btp
Else
Return _ttp
End If
End Get
Set(value As SKTypeface)
_btp = value
End Set
End Property
Public Property ItalicTypeFace As SKTypeface
Get
If Not GlobalSettings.Settings.TranslatorActivated Then
Return _itp
Else
Return _ttp
End If
End Get
Set(value As SKTypeface)
_itp = value
End Set
End Property
Public Property BoldItalicTypeFace As SKTypeface
Get
If Not GlobalSettings.Settings.TranslatorActivated Then
Return _bitp
Else
Return _ttp
End If
End Get
Set(value As SKTypeface)
_bitp = value
End Set
End Property
Public Property FontStyle As Enums.GraphicObjects.FontStyle = Enums.GraphicObjects.FontStyle.Regular Implements IGraphicObject.FontStyle
Friend EmbeddedResourceIconName As String = ""
Friend Image As SKImage
Private disposedValue As Boolean
Public Function GetFont() As SKTypeface
If Not GlobalSettings.Settings.TranslatorActivated Then
Select Case FontStyle
Case Enums.GraphicObjects.FontStyle.Regular
Return RegularTypeFace
Case Enums.GraphicObjects.FontStyle.Bold
Return BoldTypeFace
Case Enums.GraphicObjects.FontStyle.BoldItalic
Return BoldItalicTypeFace
Case Enums.GraphicObjects.FontStyle.Italic
Return ItalicTypeFace
Case Else
Return RegularTypeFace
End Select
Else
Return _ttp
End If
End Function
Public Overrides Function ToString() As String
If Tag <> "" Then
Return Tag
Else
Return MyBase.ToString()
End If
End Function
Public Function GetPaint(color As SKColor) As SKPaint
Dim p As New SKPaint
With p
.IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
.Color = color
.IsStroke = False
p.Typeface = RegularTypeFace
End With
Return p
End Function
Public Function GetStrokePaint(color As SKColor, StrokeWidth As Single) As SKPaint
Dim p As New SKPaint
With p
.IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
.Color = color
.IsStroke = True
.StrokeWidth = StrokeWidth
p.Typeface = RegularTypeFace
End With
Return p
End Function
Public Function GetRect(x As Single, y As Single, width As Single, height As Single)
Return New SKRect(x, y, x + width, y + height)
End Function
Public Function GetDrawingRect()
Return New SKRect(X, Y, X + Width, Y + Height)
End Function
Public Function MeasureString(text As String, paint As SKPaint) As SKSize
Dim trect As New SKRect(0, 0, 2, 2)
Dim aa = paint.IsAntialias
paint.IsAntialias = False
paint.GetTextPath(text, 0, 0).GetBounds(trect)
paint.IsAntialias = aa
Return New SKSize(trect.Width, trect.Height)
End Function
Public Sub StraightCanvas(canvas As SKCanvas)
If FlippedV And Not FlippedH Then
canvas.Scale(1, -1, (X + Width / 2), (Y + Height / 2))
ElseIf FlippedH And Not FlippedV Then
canvas.Scale(-1, 1, (X + Width / 2), (Y + Height / 2))
ElseIf FlippedH And FlippedV Then
canvas.Scale(-1, -1, (X + Width / 2), (Y + Height / 2))
End If
canvas.RotateDegrees(Rotation, X + Width / 2, Y + Height / 2)
End Sub
Public Overridable Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements ICustomXMLSerialization.LoadData
XMLSerializer.XMLSerializer.Deserialize(Me, data)
Return True
End Function
Public Overridable Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements ICustomXMLSerialization.SaveData
Dim elements As List(Of XElement) = XMLSerializer.XMLSerializer.Serialize(Me)
Dim ci As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
With elements
.Add(New XElement("InputConnectors"))
For Each cp As ConnectionPoint In InputConnectors
If cp.IsAttached And Not cp.AttachedConnector Is Nothing Then
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached),
New XAttribute("ConnType", cp.Type.ToString),
New XAttribute("AttachedFromObjID", cp.AttachedConnector.AttachedFrom.Name.ToString),
New XAttribute("AttachedFromConnIndex", cp.AttachedConnector.AttachedFromConnectorIndex),
New XAttribute("AttachedFromEnergyConn", cp.AttachedConnector.AttachedFromEnergy.ToString)))
Else
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached)))
End If
Next
.Add(New XElement("OutputConnectors"))
For Each cp As ConnectionPoint In OutputConnectors
If cp.IsAttached And Not cp.AttachedConnector Is Nothing Then
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached),
New XAttribute("ConnType", cp.Type.ToString),
New XAttribute("AttachedToObjID", cp.AttachedConnector.AttachedTo.Name.ToString),
New XAttribute("AttachedToConnIndex", cp.AttachedConnector.AttachedToConnectorIndex),
New XAttribute("AttachedToEnergyConn", cp.AttachedConnector.AttachedToEnergy.ToString)))
Else
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached)))
End If
Next
.Add(New XElement("EnergyConnector"))
If EnergyConnector.IsAttached Then
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", EnergyConnector.IsAttached),
New XAttribute("AttachedToObjID", EnergyConnector.AttachedConnector.AttachedTo.Name.ToString),
New XAttribute("AttachedToConnIndex", EnergyConnector.AttachedConnector.AttachedToConnectorIndex),
New XAttribute("AttachedToEnergyConn", EnergyConnector.AttachedConnector.AttachedToEnergy.ToString)),
New XAttribute("AttachedFromObjID", EnergyConnector.AttachedConnector.AttachedFrom.Name.ToString),
New XAttribute("AttachedFromConnIndex", EnergyConnector.AttachedConnector.AttachedFromConnectorIndex),
New XAttribute("AttachedFromEnergyConn", EnergyConnector.AttachedConnector.AttachedFromEnergy.ToString))
Else
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", EnergyConnector.IsAttached)))
End If
.Add(New XElement("SpecialConnectors"))
For Each cp As ConnectionPoint In SpecialConnectors
If cp.IsAttached Then
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached),
New XAttribute("AttachedToObjID", cp.AttachedConnector.AttachedTo.Name.ToString),
New XAttribute("AttachedToConnIndex", cp.AttachedConnector.AttachedToConnectorIndex),
New XAttribute("AttachedToEnergyConn", cp.AttachedConnector.AttachedToEnergy.ToString)),
New XAttribute("AttachedFromObjID", cp.AttachedConnector.AttachedFrom.Name.ToString),
New XAttribute("AttachedFromConnIndex", cp.AttachedConnector.AttachedFromConnectorIndex),
New XAttribute("AttachedFromEnergyConn", cp.AttachedConnector.AttachedFromEnergy.ToString))
Else
elements(elements.Count - 1).Add(New XElement("Connector", New XAttribute("IsAttached", cp.IsAttached)))
End If
Next
End With
Return elements
End Function
Public Shared Function ReturnInstance(typename As String) As IGraphicObject
Dim t As Type = Type.GetType(typename, False)
If Not t Is Nothing Then Return Activator.CreateInstance(t) Else Return Nothing
End Function
Public Overridable Property Calculated() As Boolean Implements IGraphicObject.Calculated
Get
Select Case Status
Case Enums.GraphicObjects.Status.Calculated
Return True
Case Enums.GraphicObjects.Status.Calculating
Return False
Case Enums.GraphicObjects.Status.ErrorCalculating
Return False
Case Enums.GraphicObjects.Status.Idle
Return True
Case Enums.GraphicObjects.Status.Inactive
Return False
Case Enums.GraphicObjects.Status.NotCalculated
Return False
Case Else
Return False
End Select
End Get
Set(ByVal Value As Boolean)
If Value = False Then
Status = Enums.GraphicObjects.Status.ErrorCalculating
Else
Status = Enums.GraphicObjects.Status.Calculated
End If
End Set
End Property
Public Overridable Overloads Function HitTest(ByVal pt As SKPoint) As Boolean
Return pt.X >= X And pt.Y >= Y And pt.X <= X + Width And pt.Y <= Y + Height
End Function
Public Overridable Function IsInRect(ByVal rect As SKRect) As Boolean
Return rect.Contains(New SKRect(X, Y, X + Width, Y + Height))
End Function
#Region "Constructors"
Public Sub New()
End Sub
Public Sub New(ByVal graphicPosition As SKPoint)
Me.New()
Me.SetPosition(graphicPosition)
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer)
Me.New(New SKPoint(posX, posY))
End Sub
Public Sub New(ByVal graphicPosition As SKPoint, ByVal graphicSize As SKSize)
Me.New(graphicPosition)
Me.SetSize(graphicSize)
Me.AutoSize = False
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal graphicSize As SKSize)
Me.New(New SKPoint(posX, posY), graphicSize)
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer,
ByVal width As Integer, ByVal height As Integer)
Me.New(New SKPoint(posX, posY), New SKSize(width, height))
End Sub
Public Sub New(ByVal graphicPosition As SKPoint, ByVal Rotation As Single)
Me.New()
Me.SetPosition(graphicPosition)
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal Rotation As Single)
Me.New(New SKPoint(posX, posY), Rotation)
End Sub
Public Sub New(ByVal graphicPosition As SKPoint, ByVal graphicSize As SKSize, ByVal Rotation As Single)
Me.New(graphicPosition, Rotation)
Me.SetSize(graphicSize)
Me.AutoSize = False
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer,
ByVal graphicSize As SKSize, ByVal Rotation As Single)
Me.New(New SKPoint(posX, posY), graphicSize, Rotation)
End Sub
Public Sub New(ByVal posX As Integer, ByVal posY As Integer, ByVal width As Integer,
ByVal height As Integer, ByVal Rotation As Single)
Me.New(New SKPoint(posX, posY), New SKSize(width, height), Rotation)
End Sub
#End Region
Public Overridable Function GetPosition() As SKPoint
Return New SKPoint(X, Y)
End Function
Public Overridable Function GetCenterPosition() As SKPoint
Return New SKPoint(X + Width / 2, Y + Height / 2)
End Function
Public Overridable Sub SetPosition(ByVal Value As SKPoint)
X = Value.X
Y = Value.Y
End Sub
Public Overridable Sub SetPosition(ByVal X0 As Integer, ByVal Y0 As Integer)
X = X0
Y = Y0
End Sub
Public Overridable Sub SetSize(ByVal Value As SKSize)
Width = Value.Width
Height = Value.Height
End Sub
Public Overridable Function GetSize() As SKSize
Dim mySize As New SKSize(Width, Height)
Return mySize
End Function
Public Overridable Sub Draw(ByVal g As Object)
PositionConnectors()
End Sub
Public Sub DrawRoundRect(ByVal g As Object, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal radius As Integer, ByVal myBrush As SKPaint)
If width / 2 < radius Then
radius = width / 2 - 2
ElseIf height / 2 < radius Then
radius = height / 2 - 2
End If
Dim gp As SKPath = New SKPath()
gp.AddRoundedRect(New SKRect(x, y, x + width, y + height), radius, radius, SKPathDirection.Clockwise)
gp.Close()
DirectCast(g, SKCanvas).DrawPath(gp, myBrush)
End Sub
Public Overridable Sub PositionConnectors() Implements IGraphicObject.PositionConnectors
'To be implemented in derived classes.
End Sub
Public Overridable Sub CreateConnectors(ByVal InCount As Integer, ByVal OutCount As Integer)
'Creates all the connection points.
For I As Integer = 1 To InCount
Dim Con As New ConnectionPoint
Con.Type = Enums.GraphicObjects.ConType.ConIn
InputConnectors.Add(Con)
Next
For I As Integer = 1 To OutCount
Dim Con As New ConnectionPoint
Con.Type = Enums.GraphicObjects.ConType.ConOut
OutputConnectors.Add(Con)
Next
End Sub
Public Property Active As Boolean = True Implements IGraphicObject.Active
Public Property AdditionalInfo As Object = Nothing Implements IGraphicObject.AdditionalInfo
Public Property Description As String = "" Implements IGraphicObject.Description
Public Property FlippedH As Boolean = False Implements IGraphicObject.FlippedH
Public Property FlippedV As Boolean = False Implements IGraphicObject.FlippedV
Public Property IsEnergyStream As Boolean = False Implements IGraphicObject.IsEnergyStream
Public Property ObjectType As Enums.GraphicObjects.ObjectType = Enums.GraphicObjects.ObjectType.Nenhum Implements IGraphicObject.ObjectType
Public Property Shape As Integer = 0 Implements IGraphicObject.Shape
Public Property ShapeOverride As Enums.GraphicObjects.ShapeIcon = Enums.GraphicObjects.ShapeIcon.DefaultShape Implements IGraphicObject.ShapeOverride
Public Property Status As Enums.GraphicObjects.Status = Enums.GraphicObjects.Status.NotCalculated Implements IGraphicObject.Status
Public Property AutoSize As Boolean = False Implements IGraphicObject.AutoSize
Public Property Height As Integer = 20 Implements IGraphicObject.Height
Public Property IsConnector As Boolean = False Implements IGraphicObject.IsConnector
Public Property Name As String = "" Implements IGraphicObject.Name
Public Property Tag As String = "" Implements IGraphicObject.Tag
Public Property Width As Integer = 20 Implements IGraphicObject.Width
Public Property X As Single = 0.0 Implements IGraphicObject.X
Public Property Y As Single = 0.0 Implements IGraphicObject.Y
Public Property EnergyConnector As IConnectionPoint = New ConnectionPoint() With {.IsEnergyConnector = True} Implements IGraphicObject.EnergyConnector
Public Property InputConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.InputConnectors
Public Property OutputConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.OutputConnectors
Public Property SpecialConnectors As New List(Of IConnectionPoint) Implements IGraphicObject.SpecialConnectors
Public Sub Draw1(surface As Object) Implements IGraphicObject.Draw
Draw(surface)
End Sub
Public Property Selected As Boolean = False Implements IGraphicObject.Selected
<Xml.Serialization.XmlIgnore> Property Owner As ISimulationObject Implements IGraphicObject.Owner
Public Property Position As IPoint Implements IGraphicObject.Position
Public Function HitTest1(zoomedSelection As Object) As Boolean Implements IGraphicObject.HitTest
Dim rect As SKRect = DirectCast(zoomedSelection, SKRect)
Return rect.Left <= X And rect.Top <= Y And rect.Right >= X + Width And rect.Bottom >= Y + Height
End Function
Public Function Clone() As IGraphicObject Implements IGraphicObject.Clone
Dim newobj = Activator.CreateInstance(Me.GetType)
DirectCast(newobj, ICustomXMLSerialization).LoadData(Me.SaveData)
Return newobj
End Function
Public Overridable Sub DisplayControlPanelModeEditor() Implements IGraphicObject.DisplayControlPanelModeEditor
ControlPanelModeEditorDisplayDelegate?.Invoke()
End Sub
Public Property Editor As Object Implements IGraphicObject.Editor
Public Property Rotation As Integer Implements IGraphicObject.Rotation
Public Property DrawOverride As Action(Of Object) Implements IGraphicObject.DrawOverride
Public Property Extensions As Dictionary(Of String, IGraphicObjectExtension) Implements IGraphicObject.Extensions
Public Property ControlPanelModeEditorDisplayDelegate As Action Implements IGraphicObject.ControlPanelModeEditorDisplayDelegate
Public Property DoubleClickAction As Action(Of Object) Implements IGraphicObject.DoubleClickAction
Public Property DrawMode As Integer = 0 Implements IGraphicObject.DrawMode
<Xml.Serialization.XmlIgnore>
Public Property Flowsheet As IFlowsheet Implements IGraphicObject.Flowsheet
Public Property DrawLabel As Boolean = True Implements IGraphicObject.DrawLabel
Public Function GetForeColor() As SKColor
Return Drawing.SkiaSharp.GraphicsSurface.ForegroundColor
End Function
Public Function GetBackColor() As SKColor
Return Drawing.SkiaSharp.GraphicsSurface.BackgroundColor
End Function
Public Overridable Function GetPointValue(type As Enums.GraphicObjects.PointValueType, X As Integer, Y As Integer, args As List(Of Object)) As Double Implements IGraphicObject.GetPointValue
Return Double.NaN
End Function
Public Overridable Function GetIconAsBitmap() As System.Drawing.Bitmap Implements IGraphicObject.GetIconAsBitmap
If EmbeddedResourceIconName <> "" Then
Dim bmp As New System.Drawing.Bitmap(Me.GetType().Assembly.GetManifestResourceStream(String.Format("DWSIM.Drawing.SkiaSharp.{0}", EmbeddedResourceIconName)))
Return bmp
Else
Return Nothing
End If
End Function
Public Overridable Function GetIconAsStream() As MemoryStream Implements IGraphicObject.GetIconAsStream
If EmbeddedResourceIconName <> "" Then
Using us = Me.GetType().Assembly.GetManifestResourceStream(String.Format("DWSIM.Drawing.SkiaSharp.{0}", EmbeddedResourceIconName))
Dim ms As New MemoryStream()
us.CopyTo(ms)
ms.Position = 0
Return ms
End Using
Else
Return Nothing
End If
End Function
Public Sub ReleaseReferences() Implements IGraphicObject.ReleaseReferences
Owner = Nothing
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects)
End If
' TODO: free unmanaged resources (unmanaged objects) and override finalizer
' TODO: set large fields to null
disposedValue = True
End If
End Sub
' ' TODO: override finalizer only if 'Dispose(disposing As Boolean)' has code to free unmanaged resources
' Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
' Dispose(disposing:=False)
' MyBase.Finalize()
' End Sub
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
End Class
End Namespace
|