Skip to content

Commit

Permalink
Merged with loko's change
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyipeng committed Jan 31, 2018
1 parent 39e8102 commit 0fa85f2
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions ComponentsSource/FMX.ImageSlider.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
// https://github.com/zhaoyipeng/FMXComponents
//
// ***************************************************************************

// version history
// 2017-01-20, v0.1.0.0 : first release
// 2018-01-31, v0.2.0.0 : merged with loko's change

unit FMX.ImageSlider;

interface

uses
System.Classes,
System.Generics.Collections,
Expand All @@ -28,6 +31,7 @@ interface
FMX.ComponentsCommon;

type

[ComponentPlatformsAttribute(TFMXPlatforms)]
TFMXImageSlider = class(TLayout)
private
Expand All @@ -38,14 +42,15 @@ TFMXImageSlider = class(TLayout)
FDownPos: TPointF;
FDownIndex: Integer;
FAnimation: TFloatAnimation;
procedure SetActivePage(const Value: Integer);
procedure MoveToActivePage; { add }
procedure SetActivePage(const Value: Integer); { change }
procedure SetPageCount(const Value: Integer);
function GetPageCount: Integer;
protected
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseMove(Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override; { change }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Expand Down Expand Up @@ -91,8 +96,7 @@ function TFMXImageSlider.GetPageCount: Integer;
Result := FPages.Count;
end;

procedure TFMXImageSlider.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
procedure TFMXImageSlider.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
inherited;
if (PageCount > 0) and (Button = TMouseButton.mbLeft) then
Expand All @@ -116,8 +120,7 @@ procedure TFMXImageSlider.MouseMove(Shift: TShiftState; X, Y: Single);
end;
end;

procedure TFMXImageSlider.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
procedure TFMXImageSlider.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); { change }
var
DeltaX: Single;
begin
Expand All @@ -136,13 +139,20 @@ procedure TFMXImageSlider.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
if FActivePage < PageCount - 1 then
FActivePage := FActivePage + 1;
end;

FAnimation.StartValue := FContainer.Position.X;
FAnimation.StopValue := - FActivePage * Width;
FAnimation.Start;
{ Loko } MoveToActivePage; // move to page
end;
end;

procedure TFMXImageSlider.MoveToActivePage; { add }
begin
{ a methode to move to your active Page }
{ Loko } FAnimation.StartValue := FContainer.Position.X;
{ Loko } FAnimation.StopValue := -FActivePage * Width;
{ Loko } FAnimation.Interpolation := TInterpolationType.Quintic;
{ Loko } FAnimation.AnimationType := TAnimationType.Out;
{ Loko } FAnimation.Start;
end;

procedure TFMXImageSlider.Resize;
var
I: Integer;
Expand All @@ -152,23 +162,20 @@ procedure TFMXImageSlider.Resize;
FContainer.Width := Width
else
FContainer.Width := PageCount * Width;
for I := 0 to FPages.Count-1 do
for I := 0 to FPages.Count - 1 do
begin
FPages[I].Width := Width;
FPages[I].Height := Height;
FPages[I].Position.X := I * Width;
end;
end;

procedure TFMXImageSlider.SetActivePage(const Value: Integer);
procedure TFMXImageSlider.SetActivePage(const Value: Integer); { change }
begin
if FActivePage <> Value then
begin
if (FActivePage = -1) then
FContainer.Position.X := 0
else
FContainer.Position.X := -Width * FActivePage;
end;
if (Value < 0) or (Value > FPages.Count - 1) then // check if value valid
exit;
FActivePage := Value; // set FActivePage
MoveToActivePage; // move Page
end;

procedure TFMXImageSlider.SetPage(Index: Integer; AImage: TImage);
Expand All @@ -192,7 +199,7 @@ procedure TFMXImageSlider.SetPageCount(const Value: Integer);
OldCount := PageCount;
if OldCount < Value then
begin
for I := OldCount+1 to Value do
for I := OldCount + 1 to Value do
begin
L := TLayout.Create(Self);
L.Parent := FContainer;
Expand All @@ -205,11 +212,11 @@ procedure TFMXImageSlider.SetPageCount(const Value: Integer);
end
else if OldCount > Value then
begin
for I := OldCount-1 downto Value do
for I := OldCount - 1 downto Value do
begin
L := FPages[I];
// if L.ChildrenCount > 0 then
// L.Children[0].Parent := Self.Parent;
// if L.ChildrenCount > 0 then
// L.Children[0].Parent := Self.Parent;
L.DisposeOf;
end;
FPages.Count := Value;
Expand Down

0 comments on commit 0fa85f2

Please sign in to comment.