-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComposer.cls
179 lines (153 loc) · 5.6 KB
/
Composer.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Composer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
'===============================================================================
' Ìîäóëü : Composer
' Âåðñèÿ : 2024.02.13
' Àâòîð : elvin-nsk ([email protected])
' Ñàéò : https://github.com/elvin-nsk/LowCoupledFromCore
' Íàçíà÷åíèå : Óêëàä÷èê øåéïîâ/ðåíäæåé ïî ñòðîêàì
' Çàâèñèìîñòè : Point
'===============================================================================
'@PredeclaredId
Option Explicit
'===============================================================================
Private Type This
Elements As Collection
ComposedElements As Collection
RemainingElements As Collection
StartingPoint As Point
MaxPlacesInWidth As Long
MaxPlacesInHeight As Long
MaxWidth As Double
MaxHeight As Double
HorizontalSpace As Double
VerticalSpace As Double
Cursor As Point
CurrentRowItemsInWidth As Long
CurrentRowWidth As Long
CurrentRowHeight As Double
CurrentRowNumber As Long
End Type
Private This As This
'===============================================================================
Public Function NewAndCompose( _
ByVal Elements As Collection, _
Optional ByVal StartingPoint As Point, _
Optional ByVal MaxPlacesInWidth As Long, _
Optional ByVal MaxPlacesInHeight As Long, _
Optional ByVal MaxWidth As Double, _
Optional ByVal MaxHeight As Double, _
Optional ByVal HorizontalSpace As Double, _
Optional ByVal VerticalSpace As Double _
) As Composer
Set NewAndCompose = New Composer
NewAndCompose.Inject _
Elements, StartingPoint, _
MaxPlacesInWidth, MaxPlacesInHeight, _
MaxWidth, MaxHeight, _
HorizontalSpace, VerticalSpace
End Function
Friend Sub Inject( _
ByVal Elements As Collection, _
ByVal StartingPoint As Point, _
ByVal MaxPlacesInWidth As Long, _
ByVal MaxPlacesInHeight As Long, _
ByVal MaxWidth As Double, _
ByVal MaxHeight As Double, _
ByVal HorizontalSpace As Double, _
ByVal VerticalSpace As Double _
)
With This
Set .Elements = Elements
Set .ComposedElements = New Collection
Set .RemainingElements = New Collection
.MaxPlacesInWidth = NumberOrMaxLong(MaxPlacesInWidth)
.MaxPlacesInHeight = NumberOrMaxLong(MaxPlacesInHeight)
.MaxWidth = NumberOrMaxDouble(MaxWidth)
.MaxHeight = NumberOrMaxDouble(MaxHeight)
.VerticalSpace = VerticalSpace
.HorizontalSpace = HorizontalSpace
If StartingPoint Is Nothing Then
Set .StartingPoint = Point.New_(0, 0)
Else
Set .StartingPoint = StartingPoint
End If
Set .Cursor = .StartingPoint.GetCopy
End With
Compose
End Sub
'===============================================================================
Public Property Get ComposedElements() As Collection
Set ComposedElements = This.ComposedElements
End Property
Public Property Get RemainingElements() As Collection
Set RemainingElements = This.RemainingElements
End Property
'===============================================================================
Private Sub Compose()
With This
Dim Item As ComposerElement
.CurrentRowNumber = 1
For Each Item In .Elements
BeginNextRowIfNeeded Item
If IsHeightExceeded(Item) Then
.RemainingElements.Add Item
Else
PlaceElement Item
.ComposedElements.Add Item
End If
Next Item
End With
End Sub
Private Sub PlaceElement(ByVal Item As ComposerElement)
With This
Item.PivotX = .Cursor.x
Item.PivotY = .Cursor.y
.CurrentRowWidth = .CurrentRowWidth + .HorizontalSpace + Item.Width
If .CurrentRowHeight < Item.Height Then _
.CurrentRowHeight = Item.Height
.CurrentRowItemsInWidth = .CurrentRowItemsInWidth + 1
.Cursor.x = .Cursor.x + .HorizontalSpace + Item.Width
End With
End Sub
Private Function IsHeightExceeded(ByVal Item As ComposerElement) As Boolean
With This
If (VBA.Abs(.StartingPoint.y - .Cursor.y) + Item.Height >= .MaxHeight) _
Or (.CurrentRowNumber > .MaxPlacesInHeight) Then _
IsHeightExceeded = True
End With
End Function
Private Sub BeginNextRowIfNeeded(ByVal Item As ComposerElement)
With This
If (.CurrentRowWidth + Item.Width > .MaxWidth) _
Or (.CurrentRowItemsInWidth = .MaxPlacesInWidth) Then
.Cursor.x = .StartingPoint.x
.Cursor.y = .Cursor.y - .CurrentRowHeight - .VerticalSpace
.CurrentRowWidth = 0
.CurrentRowHeight = 0
.CurrentRowItemsInWidth = 0
.CurrentRowNumber = .CurrentRowNumber + 1
End If
End With
End Sub
Private Function NumberOrMaxLong(ByVal Number As Long) As Long
If Number <= 0 Then
NumberOrMaxLong = 2147483647
Else
NumberOrMaxLong = Number
End If
End Function
Private Function NumberOrMaxDouble(ByVal Number As Double) As Double
If Number <= 0 Then
NumberOrMaxDouble = 1.79769313486231E+308
Else
NumberOrMaxDouble = Number
End If
End Function