... how to get a repeated Array of that and have mouse clickable events to change the colour of a square on the initial grid.
You can do it using easy way or hard way. The easy one is to use an array of TShape. But if performance is important, you should go with the lightweight solution, which is the hard way.
Because the easy way is easy, all the explanation below if for the hard way solution.
Basically you need 2 things:
1. Store the information of all the clickable regions
2. Check if the mouse click any of the regions
The information you need to store is the X1, Y1, X2, Y2 of the clickable position. Alternatively you can store X, Y, W, H. You may want to consider to use
TRect:
https://lazarus-ccr.sourceforge.io/docs/rtl/types/trect.htmlFor checking if mouse click on any of the region, you can use
OnClick or
OnMouseDown events. To read the mouse position you use
CursorPos. But then you need to do the conversion make it relative to the client coordinates.
Read more:
https://lazarus-ccr.sourceforge.io/docs/lcl/controls/tmouse.cursorpos.htmlhttps://lazarus-ccr.sourceforge.io/docs/lcl/lclintf/screentoclient.htmlYou can learn from these examples:
https://forum.lazarus.freepascal.org/index.php/topic,57003.msg423728.html#msg423728https://forum.lazarus.freepascal.org/index.php/topic,48291.msg348084.html#msg348084https://forum.lazarus.freepascal.org/index.php/topic,41036.msg284131.html#msg284131There is a thread on this forumn that talks about using Arrays OR a TList to do the initial array. What would be the advantages of using a TList?
For beginners, just pick the one most convenient to you.
But if you interested, here is how I choose between array and TList.
For something well-arranged like grids, I prefer array or dynamic array. Especially if you can calculate which cell is clicked because the grid has fix column widths and row heights.
TList is good for items that frequently need to be inserted and removed, because TList has the methods for doing insertion, deletion and sorting. But TList is slower than array as it is not lightweight, it perform a lot things behind the scene.
You can still use array to store items that frequently need to be inserted and removed. But you have to write the code for performing the insertion and deletion, that is relatively not easy. For this case, you should simply use TList. Because TList is properly written and tightly optimized, it should perform faster than the code written from non-professionals.
I done a test for my game engine, using TList vs array to store the game items than spawn on the screen runtime. When using array, I properly optimized the code. The result is, there were cases TList run faster than dynamic array. But on some cases dynamic array run faster. The performance difference, if I remember correctly is less than 1%.
Nowadays computer run very fast. You may not notice performance different using TList or array, unless you're doing some gigantic scientific calculations.