Вот что у нас получится. Левая и правая панельки нужны для листания вправо и влево. Листание происходит за счет смещения на 1 плитку в ту или иную сторону.
Стартовое размещение плиток при загрузке.
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 |
procedure TGoodsOfMonth.PositionElements; var k: integer; canvasWidth: integer; elementWidth: Integer; maxN_X: integer; leftDist: integer; rightDist: integer; x: integer; canvasMiddle: Int64; y: Integer; j: integer; elementsCenter: Integer; shiftDist: Integer; begin if FOL.Count = 0 then exit; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; //pContent.Width; elementWidth := FOL[0].Width; leftDist := 10; rightDist := 10; k := 0; // put everything on pContent x := -FOL[0].Width + pLeft.Width; y := 10; FLeftIndex := 0; // make all invisible for j := k to FOL.Count - 1 do FOL[j].Visible := false; // repeat x := x + FOL[k].Width + leftDist; if (x + elementWidth) > CanvasWidth then begin FRightIndex := k - 1; if FRightIndex < 0 then FRightIndex := 0; // for j := k to FOL.Count - 1 do // FOL[j].Visible := false; break; end; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; inc(k); until k = FOL.Count; //shift to center canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; // How much elements will be on visible part of Canvas // maxN_X := Math.Floor(canvasWidth / (elementWidth + leftDist + rightDist)); end; |
Смещение на 1 плитку вправо
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 |
procedure TGoodsOfMonth.iRightClick(Sender: TObject); var i, k: Integer; count: integer; leftIndex, rightIndex: integer; x: integer; leftDist: Integer; y: integer; elementWidth: integer; canvasWidth: integer; j: integer; canvasMiddle: integer; elementsCenter: integer; shiftDist: integer; newLeftIndex: integer; newRightIndex: Integer; begin // move right to 1 panel if FRightIndex < FOL.Count - 1 then begin // new index borders newLeftIndex := FLeftIndex + 1; newRightIndex := FRightIndex + 1; if newLeftIndex > FOL.Count - 1 then newLeftIndex := FOL.Count - 1; if newRightIndex > FOL.Count - 1 then newRightIndex := FOL.Count - 1; // for i := FLeftIndex to FRightIndex do begin FOL[i].Visible := false; end; // k := newLeftIndex - 1; x := -FOL[k].Width + pLeft.Width; y := 10; elementWidth := FOL[k].Width; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; leftDist := 10; repeat inc(k); x := x + FOL[k].Width + leftDist; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; until k >= newRightIndex; FLeftIndex := newLeftIndex; FRightIndex := newRightIndex; // // centering canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; end; end; |
Смещение на 1 плитку влево
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 |
procedure TGoodsOfMonth.iLeftClick(Sender: TObject); var i, k: Integer; count: integer; leftIndex, rightIndex: integer; x: integer; leftDist: Integer; y: integer; elementWidth: integer; canvasWidth: integer; j: integer; canvasMiddle: integer; elementsCenter: integer; shiftDist: integer; newLeftIndex: integer; newRightIndex: Integer; begin // move right to 1 panel if FLeftIndex > 0 then begin // new index borders newLeftIndex := FLeftIndex - 1; newRightIndex := FRightIndex - 1; if newLeftIndex < 0 then newLeftIndex := 0; if newRightIndex < 0 then newRightIndex := 0; // for i := FLeftIndex to FRightIndex do begin FOL[i].Visible := false; end; // k := newLeftIndex - 1; x := -FOL[0].Width + pLeft.Width; y := 10; elementWidth := FOL[0].Width; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; leftDist := 10; repeat inc(k); x := x + FOL[k].Width + leftDist; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; until k >= newRightIndex; FLeftIndex := newLeftIndex; FRightIndex := newRightIndex; // centering canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; end; end; |
Полный код модуля
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 |
unit uGoodsOfMonth; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uCommon, uniGUIBaseClasses, uniPanel, uGoodsOfMonthElement, System.Generics.Collections, uniImage; type TGoodsOfMonth = class(TUniFrame) pTop: TUniPanel; pContent: TUniPanel; pLeft: TUniPanel; pRight: TUniPanel; iRight: TUniImage; iLeft: TUniImage; procedure UniFrameCreate(Sender: TObject); procedure UniFrameDestroy(Sender: TObject); procedure iRightClick(Sender: TObject); procedure iLeftClick(Sender: TObject); private FBrowserWidth: Integer; FBrowserHeight: Integer; FOL: TObjectList<TGoodsOfMonthElement>; FNameCount: integer; FLeftIndex: integer; FRightIndex: integer; procedure PositionElements; procedure resizeView(aBrowserWidth: integer); procedure setView(aView: TView); procedure SetOL(const Value: TObjectList<TGoodsOfMonthElement>); { Private declarations } public { Public declarations } procedure OnBrowserResize(aWidth, aHeight, aOldWidth, aOldHeight: integer); procedure OnAfterRender(aWidth, aHeight: integer); property OL: TObjectList<TGoodsOfMonthElement> read FOL write SetOL; end; implementation {$R *.dfm} uses Math; procedure TGoodsOfMonth.iLeftClick(Sender: TObject); var i, k: Integer; count: integer; leftIndex, rightIndex: integer; x: integer; leftDist: Integer; y: integer; elementWidth: integer; canvasWidth: integer; j: integer; canvasMiddle: integer; elementsCenter: integer; shiftDist: integer; newLeftIndex: integer; newRightIndex: Integer; begin // move right to 1 panel if FLeftIndex > 0 then begin // new index borders newLeftIndex := FLeftIndex - 1; newRightIndex := FRightIndex - 1; if newLeftIndex < 0 then newLeftIndex := 0; if newRightIndex < 0 then newRightIndex := 0; // for i := FLeftIndex to FRightIndex do begin FOL[i].Visible := false; end; // k := newLeftIndex - 1; x := -FOL[0].Width + pLeft.Width; y := 10; elementWidth := FOL[0].Width; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; leftDist := 10; repeat inc(k); x := x + FOL[k].Width + leftDist; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; until k >= newRightIndex; FLeftIndex := newLeftIndex; FRightIndex := newRightIndex; // centering canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; end; end; procedure TGoodsOfMonth.iRightClick(Sender: TObject); var i, k: Integer; count: integer; leftIndex, rightIndex: integer; x: integer; leftDist: Integer; y: integer; elementWidth: integer; canvasWidth: integer; j: integer; canvasMiddle: integer; elementsCenter: integer; shiftDist: integer; newLeftIndex: integer; newRightIndex: Integer; begin // move right to 1 panel if FRightIndex < FOL.Count - 1 then begin // new index borders newLeftIndex := FLeftIndex + 1; newRightIndex := FRightIndex + 1; if newLeftIndex > FOL.Count - 1 then newLeftIndex := FOL.Count - 1; if newRightIndex > FOL.Count - 1 then newRightIndex := FOL.Count - 1; // for i := FLeftIndex to FRightIndex do begin FOL[i].Visible := false; end; // k := newLeftIndex - 1; x := -FOL[k].Width + pLeft.Width; y := 10; elementWidth := FOL[k].Width; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; leftDist := 10; repeat inc(k); x := x + FOL[k].Width + leftDist; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; until k >= newRightIndex; FLeftIndex := newLeftIndex; FRightIndex := newRightIndex; // // centering canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; end; end; procedure TGoodsOfMonth.OnAfterRender(aWidth, aHeight: integer); begin FBrowserWidth := aWidth; FBrowserHeight := aHeight; // resizeView(FBrowserWidth); end; procedure TGoodsOfMonth.OnBrowserResize(aWidth, aHeight, aOldWidth, aOldHeight: integer); begin FBrowserWidth := aWidth; FBrowserHeight := aHeight; resizeView(FBrowserWidth); end; procedure TGoodsOfMonth.PositionElements; var k: integer; canvasWidth: integer; elementWidth: Integer; maxN_X: integer; leftDist: integer; rightDist: integer; x: integer; canvasMiddle: Int64; y: Integer; j: integer; elementsCenter: Integer; shiftDist: Integer; begin if FOL.Count = 0 then exit; canvasWidth := FBrowserWidth - pLeft.Width - pRight.Width; //pContent.Width; elementWidth := FOL[0].Width; leftDist := 10; rightDist := 10; k := 0; // put everything on pContent x := -FOL[0].Width + pLeft.Width; y := 10; FLeftIndex := 0; // make all invisible for j := k to FOL.Count - 1 do FOL[j].Visible := false; // repeat x := x + FOL[k].Width + leftDist; if (x + elementWidth) > CanvasWidth then begin FRightIndex := k - 1; if FRightIndex < 0 then FRightIndex := 0; // for j := k to FOL.Count - 1 do // FOL[j].Visible := false; break; end; FOL[k].Left := x; FOL[k].Top := y; FOL[k].Visible := true; inc(k); until k = FOL.Count; //shift to center canvasMiddle := trunc((canvasWidth - 40) / 2); elementsCenter := trunc(((FOL[FRightIndex].Left + FOL[FRightIndex].Width) - (FOL[FLeftIndex].Left)) / 2); shiftDist := canvasMiddle - elementsCenter; for j := FLeftIndex to FRightIndex do FOL[j].Left := FOL[j].Left + shiftDist; // How much elements will be on visible part of Canvas // maxN_X := Math.Floor(canvasWidth / (elementWidth + leftDist + rightDist)); end; procedure TGoodsOfMonth.resizeView(aBrowserWidth: integer); begin 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 TGoodsOfMonth.SetOL(const Value: TObjectList<TGoodsOfMonthElement>); begin FOL := Value; end; procedure TGoodsOfMonth.setView(aView: TView); begin if aView = smallView then begin PositionElements; end; if aView = middleView then begin PositionElements; end; if aView = bigView then begin PositionElements; end; end; procedure TGoodsOfMonth.UniFrameCreate(Sender: TObject); procedure addElement(); var i: integer; begin i := FOL.Add(TGoodsOfMonthElement.Create(Self)); FOL[i].Parent := pContent; FOL[i].Name := FOL[i].Name + FNameCount.ToString; FOL[i].pGoodOfMonth.Caption := FNameCount.ToString; inc(FNameCount); end; var i: Integer; begin FLeftIndex := 0; FOL := TObjectList<TGoodsOfMonthElement>.create(true); for i := 0 to 10 do addElement(); end; procedure TGoodsOfMonth.UniFrameDestroy(Sender: TObject); begin FOL.Free; end; end. |
Код модуля плитки
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 |
unit uGoodsOfMonthElement; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIFrame, uniGUIBaseClasses, uniPanel; type TGoodsOfMonthElement = class(TUniFrame) pGoodOfMonth: TUniPanel; private { Private declarations } public { Public declarations } end; implementation {$R *.dfm} end. |