Skip to content

Commit

Permalink
inventory ok, click on screen ok, game grid ok, game inventory ok
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloppeurPascal committed Jan 8, 2023
1 parent 5d006b5 commit c77078f
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 55 deletions.
7 changes: 3 additions & 4 deletions src/cInventoryItem.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ object cadInventoryItem: TcadInventoryItem
Margins.Left = 5.000000000000000000
Margins.Top = 5.000000000000000000
Margins.Bottom = 5.000000000000000000
Position.X = 5.000000000000000000
Position.Y = 5.000000000000000000
Size.Width = 320.000000000000000000
Size.Height = 79.000000000000000000
Size.Height = 0.000000000000000000
Size.PlatformDefault = False
OnClick = FrameClick
OnResize = FrameResize
object Background: TRectangle
Align = Contents
Locked = True
HitTest = False
Size.Width = 320.000000000000000000
Size.Height = 79.000000000000000000
Size.Height = 0.000000000000000000
Size.PlatformDefault = False
end
object ItemCountBackground: TEllipse
Expand Down
137 changes: 121 additions & 16 deletions src/cInventoryItem.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,35 @@ TcadInventoryItem = class(TFrame)
ItemCount: TText;
ItemCountBackground: TEllipse;
procedure FrameResize(Sender: TObject);
procedure FrameClick(Sender: TObject);
private
FInventoryItem: TInventoryItem;
FActive: boolean;
FOnSelectInventoryItem: tnotifyevent;
FOnUnSelectInventoryItem: tnotifyevent;
procedure SetInventoryItem(const Value: TInventoryItem);
function GetCountLabel: string;
procedure SetCountLabel(const Value: string);
procedure SetActive(const Value: boolean);
procedure SetCount(const Value: integer);
function GetCount: integer;
{ Déclarations privées }
procedure KillMySelf;
function GetColor: TGameItemColor;
procedure SetOnSelectInventoryItem(const Value: tnotifyevent);
procedure SetOnUnSelectInventoryItem(const Value: tnotifyevent);
public
{ Déclarations publiques }
property InventoryItem: TInventoryItem read FInventoryItem
write SetInventoryItem;
property CountLabel: string read GetCountLabel write SetCountLabel;
property Count: integer read GetCount write SetCount;
property Color: TGameItemColor read GetColor;
property Active: boolean read FActive write SetActive;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property OnSelectInventoryItem: tnotifyevent read FOnSelectInventoryItem
write SetOnSelectInventoryItem;
property OnUnSelectInventoryItem: tnotifyevent read FOnUnSelectInventoryItem
write SetOnUnSelectInventoryItem;
function GetGameItem: tgameitem;
end;

implementation
Expand All @@ -37,39 +54,127 @@ constructor TcadInventoryItem.Create(AOwner: TComponent);
begin
inherited;
name := ''; // avoid duplicate component name
FInventoryItem := nil;
Active := false;
end;

destructor TcadInventoryItem.Destroy;
begin
FInventoryItem.Free;
inherited;
end;

procedure TcadInventoryItem.FrameClick(Sender: TObject);
begin
Active := not Active;

if Active then
for var i := 0 to parent.ChildrenCount - 1 do
if (parent.Children[i] is TcadInventoryItem) and
(parent.Children[i] <> self) then
(parent.Children[i] as TcadInventoryItem).Active := false;
end;

procedure TcadInventoryItem.FrameResize(Sender: TObject);
begin
width := 2 * height;
end;

function TcadInventoryItem.GetCountLabel: string;
function TcadInventoryItem.GetColor: TGameItemColor;
begin
if assigned(FInventoryItem) then
result := FInventoryItem.Color
else
result := 0; // transparent color
end;

function TcadInventoryItem.GetCount: integer;
begin
if assigned(FInventoryItem) then
result := FInventoryItem.Count
else
result := 0;
end;

function TcadInventoryItem.GetGameItem: tgameitem;
begin
result := tgameitem.Create;
result.State := TGameItemState.first; // TODO : à compléter
if assigned(FInventoryItem) then
result.Color := FInventoryItem.Color
else
result.Color := 0; // transparent color
result.Duration := 0;
end;

procedure TcadInventoryItem.KillMySelf;
begin
result := ItemCount.Text;
Active := false;
tthread.forcequeue(nil,
procedure
begin
self.Free;
// If no inventory item attached, then kill Self next program loop
end);
end;

procedure TcadInventoryItem.SetCountLabel(const Value: string);
procedure TcadInventoryItem.SetActive(const Value: boolean);
begin
ItemCount.Text := Value;
if Value then
begin
Background.Stroke.Color := talphacolors.green;
Background.Stroke.Thickness := 3;
end
else
begin
Background.Stroke.Color := talphacolors.black;
Background.Stroke.Thickness := 1;
end;

if (FActive <> Value) then
begin
FActive := Value;
if FActive then
begin
if assigned(OnSelectInventoryItem) then
OnSelectInventoryItem(self);
end
else if assigned(OnUnSelectInventoryItem) then
OnUnSelectInventoryItem(self);
end;
end;

procedure TcadInventoryItem.SetCount(const Value: integer);
begin
ItemCount.Text := Value.ToString;
if (Value < 1) then
KillMySelf
else if assigned(FInventoryItem) then
FInventoryItem.Count := Value;
end;

procedure TcadInventoryItem.SetInventoryItem(const Value: TInventoryItem);
begin
if assigned(Value) or (FInventoryItem.Count < 1) then
if assigned(Value) and (Value.Count > 0) then
begin
FInventoryItem := Value;
Background.Fill.Color := FInventoryItem.Color;
CountLabel := FInventoryItem.Count.ToString;
ItemCount.Text := FInventoryItem.Count.ToString;
// TODO : add event to link inventory item and its display
end
else
tthread.forcequeue(nil,
procedure
begin
Self.Free;
// If no inventory item attached, then kill Self next program loop
end);
else // TODO: potential memory lost if value assigned with count=0
KillMySelf;
end;

procedure TcadInventoryItem.SetOnSelectInventoryItem(const Value: tnotifyevent);
begin
FOnSelectInventoryItem := Value;
end;

procedure TcadInventoryItem.SetOnUnSelectInventoryItem
(const Value: tnotifyevent);
begin
FOnUnSelectInventoryItem := Value;
end;

end.
2 changes: 2 additions & 0 deletions src/fMain.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ object frmMain: TfrmMain
Size.PlatformDefault = False
TabOrder = 2
object GameGrid: TImage
OnTap = GameGridTap
MultiResBitmap = <
item
end>
Expand All @@ -169,6 +170,7 @@ object frmMain: TfrmMain
Size.Height = 440.000000000000000000
Size.PlatformDefault = False
WrapMode = Original
OnMouseDown = GameGridMouseDown
OnResize = GameGridResize
end
object PlayerInventory: TRectangle
Expand Down
Loading

0 comments on commit c77078f

Please sign in to comment.