unit untListItem;

{$mode Delphi}{$H+}

interface

uses
  Classes, SysUtils, orca_scene2d, ISCThread, untConstant, untCard;

type

  { TCardListItem }

  TCardListItem = class(TD2ListBoxItem)
  private
    FCardName: string;
    FId: Int64;
    FImg: TD2Image;
    FLbl: TD2Label;
    procedure SetCardName(AValue: string);
    procedure SetId(AValue: Int64);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure termImpl(ATID: TThreadID; AData: Int64);
  public
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
  published
    property Id: Int64 read FId write SetId;
    property CardName: string read FCardName write SetCardName;
  end;

  { TOnCardListItemOpDeleted }

  TOnCardListItemOpDeleted = procedure (ACardId: Int64; AIndex: Integer) of object;

  { TCardListItemOp }

  TCardListItemOp = class(TD2ListBoxItem)
  private
    FCard: TCard;
    FCardName: string;
    FFace: Integer;
    FShowFace: Boolean;
    FId: Int64;
    FIndex: Integer;
    FOnDelete: TOnCardListItemOpDeleted;
    FImg: TD2Image;
    FLbl: TD2Label;
    FbtnFace: TD2CornerButton;
    FBtnDel: TD2CornerButton;
    procedure innerBtnDelClick(Sender: TObject);
    procedure innerBtnFaceClick(Sender: TObject);
    procedure SetCardName(AValue: string);
    procedure SetFace(AValue: Integer);
    procedure SetId(AValue: Int64);
    procedure SetShowFace(AValue: Boolean);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure termImpl(ATID: TThreadID; AData: Int64);
  public
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
  published
    property Id: Int64 read FId write SetId;
    property CardName: string read FCardName write SetCardName;
    property Card: TCard read FCard write FCard;
    property OnDelete: TOnCardListItemOpDeleted read FOnDelete write FOnDelete;
    property Index: Integer read FIndex write FIndex;
    property Face: Integer read FFace write SetFace;
    property ShowFace: Boolean read FShowFace write SetShowFace;
  end;

  { TTextItem }

  TTextItem = class(TD2ListBoxItem)
  private
    FLbl: TD2Text;
    FOptionText: string;
    procedure SetOptionText(AValue: string);
  public
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
  published
    property OptionText: string read FOptionText write SetOptionText;
  end;

  { TTextItemEx }

  TTextItemEx = class(TTextItem)
  public
    constructor Create(AOwner: TComponent; AOptionText: string); reintroduce;
  end;

  { TCardOwnerPlayerItem }

  TCardOwnerPlayerItem = class(TTextItem)
  public
    constructor Create(AOwner: TComponent; APlayer: Integer); reintroduce;
  end;

  { TCardPositionItem }

  TCardPositionItem = class(TTextItem)
  public
    constructor Create(AOwner: TComponent; APosition: Integer); reintroduce;
  end;

  { TCardEquipToItem }

  TCardEquipToItem = class(TD2ListBoxItem)
  private
    FCard: TCard;
    FCardId: Int64;
    FCardName: string;
    FImg: TD2Image;
    FLbl: TD2Text;
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure SetCard(AValue: TCard);
    procedure termImpl(ATID: TThreadID; AData: Int64);
    procedure SetCardId(AValue: Int64);
    procedure SetCardName(AValue: string);
  public
    constructor Create(AOwner: TComponent; AC: TCard); reintroduce;
  published
    property Card: TCard read FCard write SetCard;
    property CardId: Int64 read FCardId write SetCardId;
    property CardName: string read FCardName write SetCardName;
  end;

  { TCardListItemCheck }

  TCardListItemCheck = class(TD2ListBoxItem)
  private
    FCard: TCard;
    FCardName: string;
    FId: Int64;
    FOnDelete: TOnCardListItemOpDeleted;
    FImg: TD2Image;
    FLbl: TD2Text;
    FChk: TD2CheckBox;
    FPlayer: Integer;
    function GetChecked: Boolean;
    procedure SetCard(AValue: TCard);
    procedure SetCardName(AValue: string);
    procedure SetChecked(AValue: Boolean);
    procedure SetId(AValue: Int64);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure SetPlayer(AValue: Integer);
    procedure termImpl(ATID: TThreadID; AData: Int64);
  public
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
  published
    property Card: TCard read FCard write SetCard;
    property Id: Int64 read FId write SetId;
    property CardName: string read FCardName write SetCardName;
    property Player: Integer read FPlayer write SetPlayer;
    property Checked: Boolean read GetChecked write SetChecked;
  end;


implementation

uses untImageDownloader;

{ TCardListItem }

procedure TCardListItem.SetId(AValue: Int64);
var
  AImgPath: string;
begin
  if FId=AValue then Exit;
  FId:=AValue;
  AImgPath:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath += Format('%d.jpg', [FId]);
  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
  end else begin
    ISCThreadExecuteGUI<Int64, Int64>(FId, execImpl, termImpl);
  end;
end;

function TCardListItem.execImpl(ATID: TThreadID; AData: Int64): Int64;
var
  ASucc: Boolean;
begin
  ASucc := DownloadCardImage(AData);
  Exit(IfThen<Int64>(ASucc, AData, 0));
end;

procedure TCardListItem.termImpl(ATID: TThreadID; AData: Int64);
begin
  LoadCardImage(AData, FImg);
end;

procedure TCardListItem.SetCardName(AValue: string);
begin
  if FCardName=AValue then Exit;
  FCardName:=AValue;
  FLbl.Text:= FCardName;
end;

constructor TCardListItem.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TD2Object(AOwner);

  Width:= 190;
  Height:= 190;

  FImg := TD2Image.Create(Self);
  FImg.Parent := Self;
  FImg.Align:= vaClient;
  FImg.Width:= 100;
  FImg.Height:= 145;
  FImg.Padding.Left:= 4;
  FImg.Padding.Right:= 4;
  FImg.Padding.Top:= 4;
  FImg.Padding.Bottom:= 4;
  FImg.WrapMode:= d2ImageFit;
  FImg.HitTest:= False;

  FLbl := TD2Label.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= vaBottom;
  FLbl.Height:= 32;
  FLbl.Font.Family:= 'Microsoft Yahei';
  FLbl.Font.Size:= 14;
  FLbl.Font.Style:= d2FontRegular;
  FLbl.WordWrap:= False;
  FLbl.HitTest:= False;
end;

destructor TCardListItem.Destroy;
begin
  FImg.Free;
  FLbl.Free;
  inherited Destroy;
end;

{ TCardListItemOp }

procedure TCardListItemOp.SetCardName(AValue: string);
begin
  if FCardName=AValue then Exit;
  FCardName:=AValue;
  FLbl.Text:= FCardName;
end;

procedure TCardListItemOp.SetFace(AValue: Integer);
begin
  FFace:=AValue;
  FbtnFace.Text:= IfThen<String>(FFace = 0, '正面', '反面');
  FCard.Position:= IfThen<TCardPosition>(FFace = 0, cpUp, cpDown);
end;

procedure TCardListItemOp.innerBtnDelClick(Sender: TObject);
begin
  if (Assigned(FOnDelete)) then begin
    FOnDelete(FId, FIndex);
  end;
end;

procedure TCardListItemOp.innerBtnFaceClick(Sender: TObject);
begin
  if (FFace = 0) then begin
    Face:= 1;
  end else begin
    Face:= 0;
  end;
end;

procedure TCardListItemOp.SetId(AValue: Int64);
var
  AImgPath: string;
begin
  if FId=AValue then Exit;
  FId:=AValue;
  AImgPath:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath += Format('%d.jpg', [FId]);
  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
  end else begin
    ISCThreadExecuteGUI<Int64, Int64>(FId, execImpl, termImpl);
  end;
end;

procedure TCardListItemOp.SetShowFace(AValue: Boolean);
begin
  FShowFace:= AValue;
  FbtnFace.Visible:= FShowFace;
end;

function TCardListItemOp.execImpl(ATID: TThreadID; AData: Int64): Int64;
var
  ASucc: Boolean;
begin
  ASucc := DownloadCardImage(AData);
  Exit(IfThen<Int64>(ASucc, AData, 0));
end;

procedure TCardListItemOp.termImpl(ATID: TThreadID; AData: Int64);
begin
  LoadCardImage(AData, FImg);
end;

constructor TCardListItemOp.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TD2Object(AOwner);
  Align:= vaTop;

  Width:= 500;
  Height:= 75;

  FImg := TD2Image.Create(Self);
  FImg.Parent := Self;
  FImg.Align:= vaLeft;
  FImg.Width:= 45;
  FImg.Height:= 67;
  FImg.Padding.Left:= 8;
  FImg.Padding.Right:= 8;
  FImg.Padding.Top:= 4;
  FImg.Padding.Bottom:= 4;
  FImg.WrapMode:= d2ImageFit;
  FImg.HitTest:= False;

  FLbl := TD2Label.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= vaClient;
  FLbl.Height:= 75;
  FLbl.Font.Family:= 'Microsoft Yahei';
  FLbl.Font.Size:= 16;
  FLbl.Font.Style:= d2FontRegular;
  FLbl.WordWrap:= False;
  FLbl.HitTest:= False;
  FLbl.TextAlign:= d2TextAlignNear;
  FLbl.VertTextAlign:= d2TextAlignCenter;

  FbtnFace := TD2CornerButton.Create(Self);
  FbtnFace.Parent := Self;
  FbtnFace.Align:= vaRight;
  FbtnFace.Padding.Right:= 0;
  FbtnFace.Padding.Top:= 16;
  FbtnFace.Padding.Bottom:= 16;
  FbtnFace.Padding.Left:= 8;
  FbtnFace.Height:= 43;
  FbtnFace.Width:= 70;
  FbtnFace.Font.Family:= 'Microsoft Yahei';
  FbtnFace.Font.Size:= 14;
  FbtnFace.OnClick:= innerBtnFaceClick;

  FBtnDel := TD2CornerButton.Create(Self);
  FBtnDel.Parent := Self;
  FBtnDel.Align:= vaMostRight;
  FBtnDel.Padding.Right:= 8;
  FBtnDel.Padding.Top:= 16;
  FBtnDel.Padding.Bottom:= 16;
  FBtnDel.Padding.Left:= 8;
  FBtnDel.Text:= '删除';
  FBtnDel.Height:= 43;
  FBtnDel.Width:= 70;
  FBtnDel.Font.Family:= 'Microsoft Yahei';
  FBtnDel.Font.Size:= 14;
  FBtnDel.OnClick:= innerBtnDelClick;

end;

destructor TCardListItemOp.Destroy;
begin
  FImg.Free;
  FLbl.Free;
  FBtnDel.Free;
  inherited Destroy;
end;

{ TTextItem }

procedure TTextItem.SetOptionText(AValue: string);
begin
  FOptionText:=AValue;
  FLbl.Text:= FOptionText;
end;

constructor TTextItem.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Self.Parent := TD2Object(AOwner);
  Self.Height:= 26;
  self.Width:= 356;
  FLbl := TD2Text.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= vaClient;
  FLbl.HitTest:= False;
  FLbl.Font.Family:= 'Microsoft Yahei';
  FLbl.Font.Size:= 16;
  FLbl.Fill.Style:= d2BrushSolid;
  FLbl.Fill.Color:= vcWhite;
  FLbl.HorzTextAlign:= d2TextAlignCenter;
  FLbl.VertTextAlign:= d2TextAlignCenter;
end;

destructor TTextItem.Destroy;
begin
  FLbl.Free;
  inherited Destroy;
end;

{ TTextItemEx }

constructor TTextItemEx.Create(AOwner: TComponent; AOptionText: string);
begin
  inherited Create(AOwner);
  OptionText:= AOptionText;
end;

{ TCardOwnerPlayerItem }

constructor TCardOwnerPlayerItem.Create(AOwner: TComponent; APlayer: Integer);
begin
  inherited Create(AOwner);
  case APlayer of
  0:
    begin
      FLbl.Text:= '自己';
      FLbl.Fill.Color:= COLOR_SELF;
    end;
  1:
    begin
      FLbl.Text:= '对手';
      FLbl.Fill.Color:= COLOR_OPPONENT;
    end;
  end;
end;

{ TCardPositionItem }

constructor TCardPositionItem.Create(AOwner: TComponent; APosition: Integer);
begin
  inherited Create(AOwner);
  case APosition of
  0: FLbl.Text:= '正面攻击';
  // 1: FLbl.Text:= '反面攻击';
  1: FLbl.Text:= '正面防御';
  2: FLbl.Text:= '反面防御';
  3: FLbl.Text:= '正面';
  4: FLbl.Text:= '反面';
  end;
end;

{ TCardEquipToItem }

function TCardEquipToItem.execImpl(ATID: TThreadID; AData: Int64): Int64;
var
  ASucc: Boolean;
begin
  ASucc := DownloadCardImage(AData);
  Exit(IfThen<Int64>(ASucc, AData, 0));
end;

procedure TCardEquipToItem.SetCard(AValue: TCard);
begin
  FCard:=AValue;
  if (FCard <> nil) then begin
    CardName:= FCard.CardName;
    CardId:= FCard.CardId;
  end else begin
    CardName:= '无';
    CardId:= 0;
  end;

end;

procedure TCardEquipToItem.termImpl(ATID: TThreadID; AData: Int64);
begin
  LoadCardImage(AData, FImg);
end;

procedure TCardEquipToItem.SetCardId(AValue: Int64);
var
  AImgPath: string;
begin
  FCardId:=AValue;
  AImgPath:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath += Format('%d.jpg', [FCardId]);

  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
  end else begin
    ISCThreadExecuteGUI<Int64, Int64>(FCardId, execImpl, termImpl);
  end;
end;

procedure TCardEquipToItem.SetCardName(AValue: string);
begin
  FCardName:=AValue;
  FLbl.Text:= FCardName;
end;

constructor TCardEquipToItem.Create(AOwner: TComponent; AC: TCard);
begin
  inherited Create(AOwner);
  Parent := TD2Object(AOwner);
  Align:= vaTop;

  Width:= 356;
  Height:= 75;

  FImg := TD2Image.Create(Self);
  FImg.Parent := Self;
  FImg.Align:= vaLeft;
  FImg.Width:= 45;
  FImg.Height:= 67;
  FImg.Padding.Left:= 8;
  FImg.Padding.Right:= 8;
  FImg.Padding.Top:= 4;
  FImg.Padding.Bottom:= 4;
  FImg.WrapMode:= d2ImageFit;
  FImg.HitTest:= False;

  FLbl := TD2Text.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= vaClient;
  FLbl.Height:= 75;
  FLbl.Font.Family:= 'Microsoft Yahei';
  FLbl.Font.Size:= 16;
  FLbl.Font.Style:= d2FontRegular;
  FLbl.WordWrap:= False;
  FLbl.HitTest:= False;
  FLbl.HorzTextAlign:= d2TextAlignNear;
  FLbl.VertTextAlign:= d2TextAlignCenter;
  FLbl.Fill.Style:= d2BrushSolid;

  Card := AC;
  if (FCard = nil) then begin
    FLbl.Fill.Color:= COLOR_NONE;
    Exit;
  end;

  if FCard.Owner = coSelf then begin
    FLbl.Fill.Color:= COLOR_SELF;
  end else if FCard.Owner = coOpponent then begin
    FLbl.Fill.Color:= COLOR_OPPONENT;
  end else begin
    FLbl.Fill.Color:= COLOR_NONE;
  end;

end;

{ TCardListItemCheck }

procedure TCardListItemCheck.SetCardName(AValue: string);
begin
  FCardName:= AValue;
  FLbl.Text:= FCardName;
end;

function TCardListItemCheck.GetChecked: Boolean;
begin
  Exit(FChk.IsChecked);
end;

procedure TCardListItemCheck.SetCard(AValue: TCard);
begin
  FCard:=AValue;
end;

procedure TCardListItemCheck.SetChecked(AValue: Boolean);
begin
  FChk.IsChecked:= AValue;
end;

procedure TCardListItemCheck.SetId(AValue: Int64);
var
  AImgPath: string;
begin
  FId:= AValue;
  AImgPath:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath += Format('%d.jpg', [FId]);

  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
  end else begin
    ISCThreadExecuteGUI<Int64, Int64>(FId, execImpl, termImpl);
  end;
end;

function TCardListItemCheck.execImpl(ATID: TThreadID; AData: Int64): Int64;
var
  ASucc: Boolean;
begin
  ASucc := DownloadCardImage(AData);
  Exit(IfThen<Int64>(ASucc, AData, 0));
end;

procedure TCardListItemCheck.SetPlayer(AValue: Integer);
begin
  FPlayer:=AValue;
  case FPlayer of
  0:
    begin
      FLbl.Fill.Color:= COLOR_SELF;
    end;
  1:
    begin
      FLbl.Fill.Color:= COLOR_OPPONENT;
    end;
  end;
end;

procedure TCardListItemCheck.termImpl(ATID: TThreadID; AData: Int64);
begin
  LoadCardImage(AData, FImg);
end;

constructor TCardListItemCheck.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TD2Object(AOwner);
  Align:= vaTop;

  Width:= 584;
  Height:= 75;

  FImg := TD2Image.Create(Self);
  FImg.Parent := Self;
  FImg.Align:= vaLeft;
  FImg.Width:= 45;
  FImg.Height:= 67;
  FImg.Padding.Left:= 8;
  FImg.Padding.Right:= 8;
  FImg.Padding.Top:= 4;
  FImg.Padding.Bottom:= 4;
  FImg.WrapMode:= d2ImageFit;
  FImg.HitTest:= False;

  FLbl := TD2Text.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= vaClient;
  FLbl.Height:= 75;
  FLbl.Font.Family:= 'Microsoft Yahei';
  FLbl.Font.Size:= 16;
  FLbl.Font.Style:= d2FontRegular;
  FLbl.Fill.Style:= d2BrushSolid;
  FLbl.WordWrap:= False;
  FLbl.HitTest:= False;
  FLbl.HorzTextAlign:= d2TextAlignNear;
  FLbl.VertTextAlign:= d2TextAlignCenter;

  FChk := TD2CheckBox.Create(Self);
  FChk.Parent := Self;
  FChk.Position.X:= 536;
  FChk.Position.Y:= 17;
  FChk.Height:= 20;
  FChk.Width:= 20;
  FChk.Scale.X:= 2;
  FChk.Scale.Y:= 2;
end;

destructor TCardListItemCheck.Destroy;
begin
  FChk.Free;
  FLbl.Free;
  FImg.Free;
  inherited Destroy;
end;

end.

