repeat while (mouseLoc() is within myRect) and (optionKey() = down)
if (mouse() = down) then
put myLoc into mySaveLoc
-- drag the main field around until mouse is released
put mouseLoc() into saveLoc
repeat while (mouse() = down)
put mouseLoc() into curLoc
add (item 1 of curLoc - item 1 of saveLoc) to item 1 of myLoc
add (item 2 of curLoc - item 2 of saveLoc) to item 2 of myLoc
set the loc of me to myLoc
put curLoc into saveLoc
end repeat
-- calculate change in position of main field
put (item 1 of myLoc - item 1 of mySaveLoc) into diff1
put (item 2 of myLoc - item 2 of mySaveLoc) into diff2
-- move all the other objects in the group
if (diff1 Γëá 0) or (diff2 Γëá 0) then
--set lockScreen to true
set cursor to watch
repeat with i=1 to the number of items in objects
put the loc of item i of objects into curLoc
add diff1 to item 1 of curLoc
add diff2 to item 2 of curLoc
set the loc of item i of objects to curLoc
end repeat
end if
end if
end repeat
end mouseEnter
-- part 1 (field)
-- low flags: 00
-- high flags: 4002
-- rect: left=298 top=75 right=195 bottom=480
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Array
----- HyperTalk script -----
on enterInField
get SetArray()
exit enterInField
-- get start and end words of current selection
put word 2 of selectedChunk() into start
put word 4 of selectedChunk() into stop
put char 1 to start of card field arrayName() into text
put the number of words in text into startIndex
if (stop < start) then
put startIndex into stopIndex
else
put char start to stop of card field arrayName() into text
put startIndex-1+the number of words in text into stopIndex
end if
select word startIndex to stopIndex of card field arrayName()
-- get text of selected words and set into array
put word 2 of selectedChunk() into start
put word 4 of selectedChunk() into stop
put char start to stop of card field arrayName() into text
get alist(set, arrayName()&"["&startIndex&"]", text, space)
end enterInField
on returnInField
enterInField
end returnInField
on closeField
enterInField
end closeField
-- part 2 (field)
-- low flags: 01
-- high flags: 4002
-- rect: left=279 top=75 right=195 bottom=296
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 65535
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: ArrayRows
----- HyperTalk script -----
on setHeight newHeight
set cursor to watch
-- get current rectangle and text height
put rect of the target into theRect
put textHeight of the target into txtHt
put height of the target into oldHeight
-- set height and resize rectangle
put (txtHt * newHeight) into newHeight
put (item 2 of theRect + newHeight) into item 4 of theRect
set rect of the target to theRect
-- set height of array rectangle
put the rect of cd fld arrayName() into arrayRect
put (item 2 of theRect + newHeight) into item 4 of arrayRect
set the rect of cd fld arrayName() to arrayRect
-- set height of group field
put rect of cd fld "group" into groupRect
put height of cd fld "group" into groupHeight
put (groupHeight - oldHeight + newHeight) into groupHeight
put (item 2 of groupRect + groupHeight) into item 4 of groupRect
set rect of cd fld "group" to groupRect
get SizeArray()
end setHeight
-- part 3 (field)
-- low flags: 01
-- high flags: 0002
-- rect: left=298 top=61 right=74 bottom=480
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 4
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: ArrayCols
----- HyperTalk script -----
on setWidth newWidth
set cursor to watch
-- get current rectangle and text width
put rect of the target into theRect
put textSize of the target into txtWidth
put width of the target into oldWidth
-- set width and resize rectangle
put (txtWidth * newWidth * 2 + 2) into newWidth
put (item 1 of theRect + newWidth) into item 3 of theRect
set rect of the target to theRect
-- set width of array rectangle
put the rect of cd fld arrayName() into arrayRect
put (item 1 of arrayRect + newWidth) into item 3 of arrayRect
set the rect of cd fld arrayName() to arrayRect
-- set width of group field
put rect of cd fld "group" into groupRect
put width of cd fld "group" into groupWidth
put (groupWidth - oldWidth + newWidth) into groupWidth
put (item 1 of groupRect + groupWidth) into item 3 of groupRect
set rect of cd fld "group" to groupRect
get SizeArray()
end setWidth
-- part 4 (button)
-- low flags: 00
-- high flags: A003
-- rect: left=284 top=287 right=309 bottom=384
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Randomize
----- HyperTalk script -----
on mouseUp
get RandomizeArray()
end mouseUp
-- part 5 (button)
-- low flags: 00
-- high flags: A003
-- rect: left=284 top=257 right=279 bottom=384
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Display
----- HyperTalk script -----
on mouseUp
get DisplayArray()
end mouseUp
-- part 6 (button)
-- low flags: 00
-- high flags: A003
-- rect: left=385 top=258 right=280 bottom=485
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Set
----- HyperTalk script -----
on mouseUp
get SetArray()
end mouseUp
-- part 7 (button)
-- low flags: 00
-- high flags: A003
-- rect: left=386 top=287 right=309 bottom=486
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: Sort
----- HyperTalk script -----
on mouseUp
get SortArray()
end mouseUp
-- part 12 (button)
-- low flags: 00
-- high flags: 0000
-- rect: left=256 top=87 right=100 bottom=276
-- title width / last selected line: 0
-- icon id / first selected line: 3584 / 3584
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: IncreaseRows
----- HyperTalk script -----
on mouseDown
put arrayRows() into size
put size into oldSize
repeat while (size < 10)
add 1 to size
put size into cd fld "Rows"
if (mouse() Γëá down) then exit repeat
end repeat
if (size Γëá oldSize) then send "setHeight " & size to cd fld "ArrayRows"
end mouseDown
-- part 13 (button)
-- low flags: 00
-- high flags: 0000
-- rect: left=256 top=61 right=74 bottom=276
-- title width / last selected line: 0
-- icon id / first selected line: 16692 / 16692
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: DecreaseRows
----- HyperTalk script -----
on mouseDown
put arrayRows() into size
put size into oldSize
repeat while (size > 1)
subtract 1 from size
put size into cd fld "Rows"
if (mouse() Γëá down) then exit repeat
end repeat
if (size Γëá oldSize) then send "setHeight " & size to cd fld "ArrayRows"
end mouseDown
-- part 15 (field)
-- low flags: 01
-- high flags: 0002
-- rect: left=256 top=75 right=87 bottom=276
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Rows
-- part 16 (button)
-- low flags: 00
-- high flags: 0000
-- rect: left=279 top=46 right=60 bottom=296
-- title width / last selected line: 0
-- icon id / first selected line: 15420 / 15420
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: DecreaseCols
----- HyperTalk script -----
on mouseDown
put arrayCols() into size
put size into oldSize
repeat while (size > 1)
subtract 1 from size
put size into cd fld "Cols"
if (mouse() Γëá down) then exit repeat
end repeat
if (size Γëá oldSize) then send "setWidth " & size to cd fld "ArrayCols"
end mouseDown
-- part 17 (field)
-- low flags: 01
-- high flags: 0002
-- rect: left=298 top=47 right=59 bottom=318
-- title width / last selected line: 0
-- icon id / first selected line: 0 / 0
-- text alignment: 0
-- font id: 3
-- text size: 9
-- style flags: 0
-- line height: 12
-- part name: Cols
-- part 18 (button)
-- low flags: 00
-- high flags: 0000
-- rect: left=318 top=46 right=60 bottom=335
-- title width / last selected line: 0
-- icon id / first selected line: 16560 / 16560
-- text alignment: 1
-- font id: 0
-- text size: 12
-- style flags: 0
-- line height: 16
-- part name: IncreaseCols
----- HyperTalk script -----
on mouseDown
put arrayCols() into size
put size into oldSize
repeat while (size < 10)
add 1 to size
put size into cd fld "Cols"
if (mouse() Γëá down) then exit repeat
end repeat
if (size Γëá oldSize) then send "setWidth " & size to cd fld "ArrayCols"
end mouseDown
-- part contents for card part 3
----- text -----
1 2 3 4 5 6 7 8 9 10
-- part contents for background part 9
----- text -----
DemoStack 0.9
-- part contents for background part 1
----- text -----
Array
-- part contents for background part 12
----- text -----
Card #4
-- part contents for background part 13
----- text -----
Description
-- part contents for background part 14
----- text -----
This is a demonstration of how ArrayList may be used to maintain two-dimensional arrays. A complete set of handlers implements all operations on the arrays.
________________________________
Using the Card
The big grid on the right represents an example array of 10 rows by 10 columns. By using the buttons below the grid you can perform several operations on this array. The Display button redraws the array. The Set button reads and saves the data in the array. The Randomize button fills the array with newly generated random numbers. Finally, the Sort button sorts the elements of the array.
You can also enter new values to the array by typing over the current values. The changes will be saved when you either press return, or press enter, or click outside of the field. Be careful not to enter more than one space between values since this will result in an empty item being set in the array.
________________________________
About the Script
Two-dimensional arrays are stored as one-dimensional arrays in a list with the same name as the array. When an item with some row and column is requested the script calculates the index into the one dimensional array (this is similar to what the C programming language does). Thus, if an array is created with dimensions 10 rows by 10 columns, then the item at (1, 1) has an index of 1, while the item at (3, 4) has an index of 34.
Since the dimensions of the array are needed to calculate the actual index into the list the script must store and retrieve the dimensions of every two-dimensional array. This is accomplished using two auxillary lists. The first is a sorted list of the names of all of the arrays. The second contains the dimensions of the corresponding array. For instance, if the array named "exampleArray" is stored in item 4 of the auxillary names list, then item 4 of the auxillary dimensions list contains the dimensions for "exampleArray".