Recent

Author Topic: Make a Dynamic Shape Array and make an Array of That  (Read 3913 times)

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #15 on: January 25, 2023, 07:10:07 am »
That's OK. Over here it is 4.56 pm afternoon, so over where you live must be different.

As ALWAYS, Thanks for helping out KodeZwerg.

I appreciate all your great help. Take care for now.

TRon

  • Hero Member
  • *****
  • Posts: 2512
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #16 on: January 25, 2023, 07:10:43 am »
@Boleeman:
If you have issues with loops and using wrong numbers but have no idea then you could use the debugger. That allows you to step through your code at runtime. The debugger allows you to see the contents of variables and you can even pace watches on variables.

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #17 on: January 25, 2023, 07:14:27 am »
Yes I will try that. I looked at KodeZwerg's code but could not find a fault.

The difference in squares occurs at the very start (row1), so it must be some sort of initialization qwerk.


Thanks to everyone for their assistance. Time to eat supper.

TRon, just turned out that the initial position needed to be shifted the other way!
« Last Edit: January 25, 2023, 08:32:41 am by Boleeman »

TRon

  • Hero Member
  • *****
  • Posts: 2512
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #18 on: January 25, 2023, 07:17:38 am »
The difference in squares occurs at the very start (row1), so it must be some sort of initialization qwerk.
Might be, might be not  :P

Instead of just telling you, try to toy with the debugger. You will be able to learn a lot from that. e.g. this is meant to inspire you to do an exercise (booh ! boring ! we hate learning !  ;) ) .

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #19 on: January 25, 2023, 07:18:27 am »
Replace my inner loop with this and you get whats in attachment.
Code: Pascal  [Select][+][-]
  1.                     Lleft                                           := Bcols * (5 + (FBlockCol * 40));
  2.                     Ltop                                            := Brows * (5 + (FBlockRow * 40));
  3.                     FBlocks[Brows, Bcols, Srows, Scols].Parent      := Self;
  4.                     FBlocks[Brows, Bcols, Srows, Scols].OnMouseUp   := @ShapeEvent;
  5.                     FBlocks[Brows, Bcols, Srows, Scols].Shape       := stRectangle;
  6.                     FBlocks[Brows, Bcols, Srows, Scols].Width       := 40;
  7.                     FBlocks[Brows, Bcols, Srows, Scols].Height      := 40;
  8.                     FBlocks[Brows, Bcols, Srows, Scols].Left        := Lleft + Pred(Scols * 40) + 5;
  9.                     FBlocks[Brows, Bcols, Srows, Scols].Top         := Ltop + Pred(Srows * 40) + 5;
  10.                     FBlocks[Brows, Bcols, Srows, Scols].Brush.Color := clRed;
  11.                     FBlocks[Brows, Bcols, Srows, Scols].Pen.Color   := clBlack;
This time really 3x3 with each 5x5
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #20 on: January 25, 2023, 07:39:40 am »
Yes I will try that. I looked at KodeZwerg's code but could not find a fault.
My error was, that I used your initial code which contained an error  >:D
As ALWAYS, Thanks for helping out KodeZwerg.
As always, you are welcome.  ;)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #21 on: January 25, 2023, 08:20:17 am »
Ah, in the initial code             FShapes[rows, cols].Top         := (rows - 1) * 40 - 1;
(Shape drawn in TitleBar!)

should be  FShapes[rows, cols].Top         := (rows) * 40 - 1;




Thanks KodeZwerg for that fix.

Works well now.




« Last Edit: January 25, 2023, 08:38:27 am by Boleeman »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #22 on: January 25, 2023, 08:39:24 am »
Code: Pascal  [Select][+][-]
  1.               shapeArray[(j-1)*numberRows+i].Top:=(j-1)*40-1;
That was the original bugged code by you, I just copy and paste without much thinking about %)

If needed, my current fully working example as project zipped in attachment, some minor additions made if you plan to put some more configuration in.  :)

Enjoy and stay healthy  O:-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #23 on: January 25, 2023, 02:05:38 pm »
For easier identification, if wanted at all - exemplary to do something specific inside the mouse event for a block instead for a single shape, i adjusted code again that it now looks like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.GenerateBlocks(const AOwner: TComponent; const AParent: TWinControl);
  2. var
  3.   Brows, Bcols, Srows, Scols: Integer;
  4.   Lleft, Ltop: Integer;
  5.   LTag: Integer;
  6. begin
  7.   LTag := 1;
  8.   SetLength(FBlocks, FBlocksRow, FBlocksCol, FBlockRow, FBlockCol);
  9.   for Brows := 0 to Pred(FBlocksRow) do
  10.     for Bcols := 0 to Pred(FBlocksCol) do
  11.       for Srows := 0 to Pred(FBlockRow) do
  12.         for Scols := 0 to Pred(FBlockCol) do
  13.           begin
  14.             FBlocks[Brows, Bcols, Srows, Scols] := TShape.Create(AOwner);
  15.             try
  16.               Lleft                                           := Bcols * (FSpacing + (FBlockCol * FShapeHeight));
  17.               Ltop                                            := Brows * (FSpacing + (FBlockRow * FShapeWidth));
  18.               FBlocks[Brows, Bcols, Srows, Scols].Parent      := AParent;
  19.               FBlocks[Brows, Bcols, Srows, Scols].OnMouseUp   := @ShapeEvent;
  20.               FBlocks[Brows, Bcols, Srows, Scols].Shape       := stRectangle;
  21.               FBlocks[Brows, Bcols, Srows, Scols].Width       := FShapeWidth;
  22.               FBlocks[Brows, Bcols, Srows, Scols].Height      := FShapeHeight;
  23.               FBlocks[Brows, Bcols, Srows, Scols].Left        := Lleft + Pred(Scols * FShapeWidth) + FSpacing;
  24.               FBlocks[Brows, Bcols, Srows, Scols].Top         := Ltop + Pred(Srows * FShapeHeight) + FSpacing;
  25.               FBlocks[Brows, Bcols, Srows, Scols].Brush.Color := clRed;
  26.               FBlocks[Brows, Bcols, Srows, Scols].Brush.Style := bsSolid;
  27.               FBlocks[Brows, Bcols, Srows, Scols].Pen.Color   := clBlack;
  28.               FBlocks[Brows, Bcols, Srows, Scols].Pen.Style   := psSolid;
  29.               FBlocks[Brows, Bcols, Srows, Scols].Tag         := LTag;
  30.               FBlocks[Brows, Bcols, Srows, Scols].Name        := 'DynShape' + LTag.ToString;
  31. //              FBlocks[Brows, Bcols, Srows, Scols].Hint        := 'DynShape' + LTag.ToString;
  32. //              FBlocks[Brows, Bcols, Srows, Scols].ShowHint    := True;
  33.               Inc(LTag);
  34.             finally
  35.               FBlocks[Brows, Bcols, Srows, Scols].Visible := True;
  36.             end;
  37.           end;
  38. end;
You can remove the "//" and hover with mouse over to see how it works with Tags, so in the mouse event you just would need to check in what block it is by checking in what range the tag property for current shape is or use the name property to quick find a wanted shape for actions... however you do - it should work in all ways
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #24 on: January 25, 2023, 10:53:59 pm »
Thanks KodeZwerg for the update and the new procedure with the additional 2 arguements (AOwner and AParent)

I added the modified GenerateBlocks procedure  to replace other one without the arguements   


  private
    //procedure GenerateBlocks;
    procedure GenerateBlocks(const AOwner: TComponent; const AParent: TWinControl);
    procedure ShapeEvent(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X , Y: Integer);
  public 


but in

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  FBlockRow := 4;
  FBlockCol := 4;
  FBlocksRow := 5;
  FBlocksCol := 5;
  FShapeWidth := 40;
  FShapeHeight := 40;
  FSpacing := 5;
  FBlocks := nil;
  Self.Width := (FBlockCol * FShapeWidth) * FBlocksCol + (FSpacing * 4);
  Self.Height := (FBlockRow * FShapeHeight) * FBlocksRow + (FSpacing * 4);
  GenerateBlocks(TForm1, TScrollBox1);
end;                                   

what do I put here:     GenerateBlocks(TForm1, which windowed control?);

I thought TWinControl was the scrollbox1 that I wanted the squares to be on, but comes up with error.

I am a bit confused about what to put in GenerateBlocks(TForm1, "????????);

Help say:  TWinControl Implements a windowed control which can contain other child controls.

Thanks in Advance.
« Last Edit: January 25, 2023, 11:00:21 pm by Boleeman »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #25 on: January 25, 2023, 11:07:11 pm »
Thanks KodeZwerg for the update and the new procedure with the additional 2 arguements (AOwner and AParent)

I added the modified GenerateBlocks procedure  to replace other one without the arguements   


  private
    //procedure GenerateBlocks;
    procedure GenerateBlocks(const AOwner: TComponent; const AParent: TWinControl);
    procedure ShapeEvent(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X , Y: Integer);
  public 


but in

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  FBlockRow := 4;
  FBlockCol := 4;
  FBlocksRow := 5;
  FBlocksCol := 5;
  FShapeWidth := 40;
  FShapeHeight := 40;
  FSpacing := 5;
  FBlocks := nil;
  Self.Width := (FBlockCol * FShapeWidth) * FBlocksCol + (FSpacing * 4);
  Self.Height := (FBlockRow * FShapeHeight) * FBlocksRow + (FSpacing * 4);
  GenerateBlocks(TForm1, TScrollBox1);
end;                                   

what do I put here:     GenerateBlocks(TForm1, which windowed control?);

I thought TWinControl was the scrollbox1 that I wanted the squares to be on, but comes up with error.

I am a bit confused about what to put in GenerateBlocks(TForm1, "????????);

Help say:  TWinControl Implements a windowed control which can contain other child controls.

Thanks in Advance.
AOwner = Self // in that case, TForm1 will be automagical used and is responsable to free my stuff
AParent = Self // in that case, everything will be drawn/put/placed on TForm1 automagical. You can use Panel1 etc... (I assume you made a typo, not write TScrollBox1, write ScrollBox1)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #26 on: January 25, 2023, 11:11:15 pm »
I just checked, yes in my test I did used it like this ->
Code: Pascal  [Select][+][-]
  1.   GenerateBlocks(Self, Self);
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #27 on: January 25, 2023, 11:14:16 pm »
In attachment is a variant where all is put into a scrollbox. Enjoy.  :-*
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2064
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #28 on: January 25, 2023, 11:36:24 pm »
  GenerateBlocks(TForm1, TScrollBox1);
Both is wrong!
GenerateBlocks(Form1, ScrollBox1);
And instead of Form1, please adapt like I showed in source, use Self when you want that current form, whatever its name may be, will be used.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Boleeman

  • Sr. Member
  • ****
  • Posts: 433
Re: Make a Dynamic Shape Array and make an Array of That
« Reply #29 on: January 26, 2023, 01:17:38 am »
AH thanks KodeZwerg.


"Self"and not "Form1"   I tried a number of combinations but went round in circles.

 
Pascal way is a bit different VB6 so still getting used to it. I like the way you looped through to create the multiples of the original arrays. Looking at the code your naming of the variables is great (like FBlockRow and Srows). Still need to refine that technique.

Now for trying to make the same square of each repeated block the same colour after a mouse click (Mapping the mouse clicks to the original Srows x Scols array)?

I played around with Tag Hints that you suggested.

I also tried altering it a bit to show columns and rows but failed in that attempt (got not overloaded error message)              FBlocks[Brows, Bcols, Srows, Scols].Name        := 'DynShape' + LTag.ToString + InttoStr(Srows)+ ' , ' + InttoStr(Scols);

What I noticed was:
For each array group the 1st element is 1 more than a multiple of 25 = Srows * Scols

n * Srows * Scols + 1                          where n is an integer.

So this would be a similar pattern for others. 2nd element n * Srows * Scols + 2 ... and so on


So using the tag of the control is a smart way of keeping track of things.


Now how to make a synchronised mouse click event in  TForm1.ShapeEvent ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShapeEvent(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X , Y: Integer);
  2.                                                  
  3. var
  4.   Brows, Bcols, Srows, Scols, n: Integer;
  5.   LTag: Integer;  
  6.  
  7.  
  8.   for Brows := 0 to Pred(FBlocksRow) do
  9.     for Bcols := 0 to Pred(FBlocksCol) do
  10.       for Srows := 0 to Pred(FBlockRow) do
  11.         for Scols := 0 to Pred(FBlockCol) do
  12.           begin        
  13.  
  14.              If StrtoInt(LTag.ToString) mod Srows * Scols = //index of controlclicked[// then  
  15.  
  16.                 //  (Sender as TShape).Brush.Color := clBlue;   Make All the corresponding shapes Blue
  17.  
  18.           end;

Wow. This is getting confusing.
« Last Edit: January 26, 2023, 02:33:53 am by Boleeman »

 

TinyPortal © 2005-2018