Здесь будет код из проекта и идеи алгоритма по шагам. Вот какой будет результат в конце.
Оттолкнувшись от него, можно будет сделать в своем проекте аналогичное выкладывание плиток
ШАГ 0 – рисуем на бумаге
ШАГ 1 – Сколько картинок влезет в горизонталь?
1 |
maxN_X := Math.Floor(canvasWidth / (elementWidth + leftDist + rightDist)); |
leftDist и rightDist это отступы слева и справа
Шаг 2 – раскладываем плитки
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 |
procedure TCatalog.PositionElements; var x: Integer; y: Integer; maxN_X: Integer; leftDist: Integer; rightDist: Integer; canvasWidth: Integer; elementWidth: Integer; k: Integer; i: Integer; canvasHeight: integer; elementHeight: integer; topDist: integer; bottomDist: integer; maxN_Y: Integer; j: Integer; nRows: Integer; indexFirstElLastRow: Int64; countElLastRow: Integer; canvasMiddle: Int64; lastRowElementsMiddle: Int64; shiftRightDist: Int64; begin if FOL.Count = 0 then exit; // Self.Height := 10; pGrid.Height := 10; // pGrid.Left := trunc(Self.Width / 2 - pGrid.Width / 2); pGrid.Top := 10; //trunc(Self.Height / 2 - pGrid.Height / 2); leftDist := 10; rightDist := 10; topDist := 10; bottomDist := 10; // canvasWidth := pGrid.Width; canvasHeight := pGrid.Height; elementWidth := FOL[0].Width; // elementHeight := FOL[0].Height; // How much elements will be on Canvas Width maxN_X := Math.Floor(canvasWidth / (elementWidth + leftDist + rightDist)); maxN_Y := Math.Floor(canvasHeight / (elementHeight + topDist + bottomDist)); // i := 0; j := 0; // k := 0; y := topDist; nRows := 0; repeat if i = maxN_X - 1 + 1 then // nextRow begin inc(nRows); i := 0; y := y + elementHeight + topDist; end; x := trunc(((canvasWidth / (maxN_X * 2) + (i / maxN_X) * canvasWidth) - (elementWidth / 2))); FOL[k].Left := x; FOL[k].Top := y; if (y + elementHeight + topDist > canvasHeight) then begin canvasHeight := canvasHeight + elementHeight + topDist; Self.Height := Self.Height + elementHeight + topDist; pGrid.Height := pGrid.Height + elementHeight + topDist; end; inc(k); inc(i); until k = FOL.Count; //---------LAST ROW ELEMENTS - PUT IN THE MIDDLE // last Row Check and center - in last Row mayBe not maxN_X elements, so we shoud check them // firstElement in last Row indexFirstElLastRow := trunc(FOL.Count / (maxN_X)) * maxN_X; if indexFirstElLastRow < 0 then indexFirstElLastRow := 0; countElLastRow := FOL.Count - indexFirstElLastRow; if (countElLastRow > 0) and (countElLastRow < maxN_X) then // shift last row elements to right begin // -- count shift to right canvasMiddle := trunc(canvasWidth / 2); lastRowElementsMiddle := trunc(((FOL[FOL.Count - 1].Left + FOL[FOL.Count - 1].Width) - (FOL[0].Left)) / 2); shiftRightDist := canvasMiddle - lastRowElementsMiddle; // k := indexFirstElLastRow; repeat FOL[k].Left := FOL[k].Left + shiftRightDist - leftDist - rightDist; inc(k) until k >= FOL.Count; end; //---------------------------- // Self.Height := Self.Height + 5 * topDist; pGrid.Height := pGrid.Height + 5 * topDist; // resize external container if Assigned(FOnResizeExternalContainer) then // send event to external container FOnResizeExternalContainer(Self.Height); end; |
Пояснения
Плитки находятся в контейнере FOL:TObjectList<>
Поверхность, на которую они выкладываются это pGrid, который находится на TUniFrame
Полный код 2-х юнитов, чтобы более детально разобраться и понять
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 |
unit uCatalog; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uCommon, uCatalogElement, System.Generics.Collections, uniGUIBaseClasses, uniPanel; type TCatalog = class(TUniFrame) pGrid: TUniPanel; pCatalog: TUniPanel; procedure UniFrameCreate(Sender: TObject); procedure UniFrameDestroy(Sender: TObject); private FBrowserWidth: Integer; FBrowserHeight: Integer; FOL: TObjectList<TCatalogElement>; FNameCount: integer; FSelfTop: integer; FOnResizeExternalContainer: TOnResizeExternalContainer; procedure resizeView(aBrowserWidth: integer); procedure setView(aView: TView); procedure SetOL(const Value: TObjectList<TCatalogElement>); procedure SetSelfTop(const Value: integer); procedure SetOnResizeExternalContainer(const Value: TOnResizeExternalContainer); { Private declarations } public { Public declarations } procedure OnBrowserResize(aWidth, aHeight, aOldWidth, aOldHeight: integer); procedure OnAfterRender(aWidth, aHeight: integer); procedure PositionElements; property OL: TObjectList<TCatalogElement> read FOL write SetOL; property SelfTop: integer read FSelfTop write SetSelfTop; property OnResizeExternalContainer: TOnResizeExternalContainer read FOnResizeExternalContainer write SetOnResizeExternalContainer; end; implementation {$R *.dfm} uses Math; procedure TCatalog.OnAfterRender(aWidth, aHeight: integer); begin FBrowserWidth := aWidth; FBrowserHeight := aHeight; // resizeView(FBrowserWidth); end; procedure TCatalog.OnBrowserResize(aWidth, aHeight, aOldWidth, aOldHeight: integer); begin FBrowserWidth := aWidth; FBrowserHeight := aHeight; resizeView(FBrowserWidth); end; procedure TCatalog.PositionElements; var x: Integer; y: Integer; maxN_X: Integer; leftDist: Integer; rightDist: Integer; canvasWidth: Integer; elementWidth: Integer; k: Integer; i: Integer; canvasHeight: integer; elementHeight: integer; topDist: integer; bottomDist: integer; maxN_Y: Integer; j: Integer; nRows: Integer; indexFirstElLastRow: Int64; countElLastRow: Integer; canvasMiddle: Int64; lastRowElementsMiddle: Int64; shiftRightDist: Int64; begin if FOL.Count = 0 then exit; // Self.Height := 10; pGrid.Height := 10; // pGrid.Left := trunc(Self.Width / 2 - pGrid.Width / 2); pGrid.Top := 10; //trunc(Self.Height / 2 - pGrid.Height / 2); leftDist := 10; rightDist := 10; topDist := 10; bottomDist := 10; // canvasWidth := pGrid.Width; canvasHeight := pGrid.Height; elementWidth := FOL[0].Width; // elementHeight := FOL[0].Height; // How much elements will be on Canvas Width maxN_X := Math.Floor(canvasWidth / (elementWidth + leftDist + rightDist)); maxN_Y := Math.Floor(canvasHeight / (elementHeight + topDist + bottomDist)); // i := 0; j := 0; // k := 0; y := topDist; nRows := 0; repeat if i = maxN_X - 1 + 1 then // nextRow begin inc(nRows); i := 0; y := y + elementHeight + topDist; end; x := trunc(((canvasWidth / (maxN_X * 2) + (i / maxN_X) * canvasWidth) - (elementWidth / 2))); FOL[k].Left := x; FOL[k].Top := y; if (y + elementHeight + topDist > canvasHeight) then begin canvasHeight := canvasHeight + elementHeight + topDist; Self.Height := Self.Height + elementHeight + topDist; pGrid.Height := pGrid.Height + elementHeight + topDist; end; inc(k); inc(i); until k = FOL.Count; //---------LAST ROW ELEMENTS - PUT IN THE MIDDLE // last Row Check and center - in last Row mayBe not maxN_X elements, so we shoud check them // firstElement in last Row indexFirstElLastRow := trunc(FOL.Count / (maxN_X)) * maxN_X; if indexFirstElLastRow < 0 then indexFirstElLastRow := 0; countElLastRow := FOL.Count - indexFirstElLastRow; if (countElLastRow > 0) and (countElLastRow < maxN_X) then // shift last row elements to right begin // -- count shift to right canvasMiddle := trunc(canvasWidth / 2); lastRowElementsMiddle := trunc(((FOL[FOL.Count - 1].Left + FOL[FOL.Count - 1].Width) - (FOL[0].Left)) / 2); shiftRightDist := canvasMiddle - lastRowElementsMiddle; // k := indexFirstElLastRow; repeat FOL[k].Left := FOL[k].Left + shiftRightDist - leftDist - rightDist; inc(k) until k >= FOL.Count; end; //---------------------------- // Self.Height := Self.Height + 5 * topDist; pGrid.Height := pGrid.Height + 5 * topDist; // resize external container if Assigned(FOnResizeExternalContainer) then // send event to external container FOnResizeExternalContainer(Self.Height); end; procedure TCatalog.resizeView(aBrowserWidth: integer); begin // Self.Top := 400; Self.Width := aBrowserWidth - 20; pGrid.Width := aBrowserWidth - 30; // // pGrid.Left := 10; // pGrid.Top := 10; if aBrowserWidth > rightWidthBorder then begin setView(bigView); end else if (aBrowserWidth > leftWidthBorder) and (aBrowserWidth <= rightWidthBorder) then begin setView(middleView); end else if aBrowserWidth < leftWidthBorder then begin setView(smallView); end; end; procedure TCatalog.SetOL(const Value: TObjectList<TCatalogElement>); begin FOL := Value; end; procedure TCatalog.SetOnResizeExternalContainer(const Value: TOnResizeExternalContainer); begin FOnResizeExternalContainer := Value; end; procedure TCatalog.SetSelfTop(const Value: integer); begin FSelfTop := Value; end; procedure TCatalog.setView(aView: TView); var i: Integer; begin if aView = smallView then begin for i := 0 to FOl.Count - 1 do begin FOL[i].Width := 300; FOL[i].Top := 100; FOL[i].PositionElements; end; PositionElements; end; if aView = middleView then begin for i := 0 to FOl.Count - 1 do begin FOL[i].Width := 200; FOL[i].Top := 50; FOL[i].PositionElements; end; PositionElements; end; if aView = bigView then begin for i := 0 to FOl.Count - 1 do begin FOL[i].Width := 200; FOL[i].Top := 50; FOL[i].PositionElements; end; PositionElements; end; end; procedure TCatalog.UniFrameCreate(Sender: TObject); procedure addCatalogElement(aName, aDescription: string); var i: integer; begin i := FOL.Add(TCatalogElement.Create(Self)); FOL[i].Parent := pGrid; FOL[i].Name := FOL[i].Name + FNameCount.ToString; inc(FNameCount); FOL[i].lName.Caption := aName; FOL[i].lDescription.Caption := aDescription; end; var i: Integer; k: integer; begin // FNameCount := 0; FOL := TObjectList<TCatalogElement>.Create(true); for i := 0 to 9 do addCatalogElement('Швейные машины', 'Описание'); end; procedure TCatalog.UniFrameDestroy(Sender: TObject); begin FOL.Free; end; end. |
2
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 |
unit uCatalogElement; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uniLabel, uniGUIBaseClasses, uniImage, uniPanel; type TCatalogElement = class(TUniFrame) pCenter: TUniPanel; pBottom: TUniPanel; lName: TUniLabel; lDescription: TUniLabel; iPicture: TUniImage; procedure UniFrameCreate(Sender: TObject); private { Private declarations } public { Public declarations } procedure PositionElements; end; implementation {$R *.dfm} procedure TCatalogElement.PositionElements; begin pCenter.Left := trunc(Self.Width / 2 - pCenter.Width / 2); pCenter.Top := trunc(Self.Height / 2 - pCenter.Height / 2); // iPicture.Align := alClient; // lName.Left := trunc(pBottom.Width / 2 - lName.Width / 2); lDescription.Left := trunc(pBottom.Width / 2 - lDescription.Width / 2); end; procedure TCatalogElement.UniFrameCreate(Sender: TObject); begin PositionElements(); end; end. |