How to save multidimensional position in to SetObjectData??

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

How to save multidimensional position in to SetObjectData??

Post by Bugala »

Im not sure this is even the best solution, but so far it seems like the best solution that comes to my mind.

Thing is, I will have info, that will be stored upon unknown dimensions.

Means that for example "info" can some times reside in List[1][2].info, while sometimes it might reside in List[1][2][3][4][5].info (or any other number of dimensions)

Since each of these datas info are contained in a layer (to be more precise, each data is a notebubble, which will hold text inside them), I have so far been thinking of using SetObjectData and GetObjectData to gain access to these info.

Idea would be, that if i have for example List[1][2][3].info, And user can click on that layer, I am thinking it would be simplest from programming point of view, that this List[1][2][3] layer, would have SetObjectData, which would be telling about its placing in that list. In this case "[1][2][3]". However, I havent quite figured out how to save that info.

Here is bit of example code to get the idea perhaps better:

Code: Select all

InsertItem(List[1][2][3], item   (Puts item to multidimensional place in list)
temp = InsertLayer(NIL, #BRUSH, 1, x, y)  (makes layer which is meant to present the list placing)
SetObjectData(#LAYER, temp, position, ?)  (Puts position inside that layers info to gain access at later point)
then later point user clicks on that layer and i can manipulate data inside it using:

Code: Select all

temp = thelayerthatuserclicked  (gets the id of the layer that user clicked)
position = GetObjectData(#LAYER, temp, position)   (uses that id to gain access to "position" info)
list[position].info = newinfo    (uses that position info to gain access to that place in that list



Any ideas how to do this? Or even completely different way?
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to save multidimensional position in to SetObjectDat

Post by Bugala »

This the solution I made, decided to post these two functions for the rest of you too, since they can be useful to others as well:

Code: Select all

Function p_SetCellsPosition(list, position)
cell = GetCell(list, position)
cell.position = position
EndFunction



Function p_GetCell(list, position)
Local cell
Local d

Local listsize = ListItems(position)

d = position[0]  
cell = list[d]

For n = 1 To listsize-1     /* -1 necessary, because list indicators go [0], [1], [2]... while listsize starts counting from 1 */
    d = position[n]
    cell = cell[d]
Next

Return(cell)

EndFunction
And a short test program:

Code: Select all

mylist = {}
mylist[0] = {}

mylist[0][0] = { position={0, 0} }


position = {0, 0}
data = p_GetCell(mylist, position)


data.newdata = "works"

DebugPrint(mylist[0][0].newdata)
Reason why I am using mylist[0][0], instead of some other numbers like 1 and 2, is because zeros are problematic numbers oftem times since they might be recognised as Nils. Hence testing with 0 can find out Nil problems.

Naturally, for these functions to work, you do need to initialise value "position" to each cell.

While "p_SetCellPosition" function might at quick look look quite useless, it does have its use in case your program goes in such a way that you are not aware in which cell you are going on, and are perhaps copying other cells info to new one or something similar.

My own usage is that in my current program each layer presents a cell in my tablelist, and i want to have that position info be attached to that layer so that when that layer is clicked by user, i can simply take the position info, and then manipulate that cells info using that position info.

notice also, that position is always a table. with the idea that there are as many numbers inside it, as there are dimensions. as example position = {2, 4, 5}, would be looking from place tablelist[2][4][5].

Naturally position dont have to be numbers, but can also be strings. ie. position = {1, "start", 3, 4}, in which case it would look from tablelist[1]["start"][3][4]

Notice also, that this command simply returns that cell into a variable. Hence the manipulation is done to that cell.

ie.

Code: Select all

cell = GetCell(1, 2, 3)
After which you can manipulate this cells items:

Code: Select all

cell.x = 100
cell.y = 200
or even subcells:

Code: Select all

cell[1].x = 100
cell[2].x = 150
Post Reply