_TITLE "Array Window: Ctrl + C for sample array, Ctrl + F for this file, then <h>elp for message box, <esc> for exits" ' B+ started 2019-05-15 as test demo for SUB scrollWindow
' save this file as "Array window.bas"
' so you can load it and view it in a scrollable window with Ctrl + F
'Modified from "getArrayItemClicked v4 mbox"
' Main testing and demo of the FUNCTION getArrayItemNumber%
'started 2018-08-31 B+ developing a general purpose display and select app of a string array
'2018-09-04 post v2 bak 9-3_9PM
'2018-09-07 getArrayItem v4 mbox - time to test mbox with help window
' spotted nasty text distortion, Fellippe point me to place to fix.
' press h while an Array Window is up and check out my mBox routine!
'2019-05-15 modified code to simulate scrollable window
' added "Beating Cardiod" 'B+ 2019-02-16 for some background program to run
' 2019-05-16 man what a B trying to clear keypresses and clicks!!!
' mainly the escape key to exit one window without
' falling through and exiting the next and the next...
'2019-05-17 get rid of _KEYDOWN(27) for exit of sub's
CONST nArr
= 92 'ubound of array = actual amount of items if LBound = 1 CONST LB
= 10 'try different lower bounds not just 0, 1 CONST WW
= 1200 'Window Width CONST WH
= 600 'Window Height
'center of screen for background Cardiod graphics
'test string array, use indexes in lines for alignment to code for function
arr
(i
) = "This is arr item:" + STR$(i
)
'some background graphics program running...
loopcount
= (loopcount
+ 1) MOD 2 x = CX + magnify * xCard(a)
y = CY - magnify * yCard(a)
LINE (x
, y
)-(lastx
, lasty
), _RGB(140, 0, 0) lastx = x: lasty = y
'access to array windows which will act modally
locRow = 5: locCol = 150 - 25 - 5: boxWidth = 25: boxHeight = 17 '< displays 15 lines of array items
''boxHeight is actual screen space in character units, the display uses 2 of the lines for control bars.
''boxWidth will include item numbers displayed to left of array string item
scrollWindow locRow, locCol, boxWidth, boxHeight, arr()
lnCnt = fLines("Array window.bas", fArr()) '<<<<<<<<<<<<<<<<<<< or whatever name you gave this file AND SAVED!
scrollWindow 26, 1, 150, 12, fArr()
'attempting to create a scrollable view port for displaying an array
' locateRow and locateCol are the same as if using LOCATE row, col and will get same errors as LOCATE
' boxWidth is in characters 8 pixels wide,
' boxheight uses 2 rows (16 pixels per) to draw Page Up and Page Down Bars, so add 2 more to height than rows of text desired
' arr() is the array we desire to display and scroll
'this sub uses a Help sub which in turn uses mBox
SUB scrollWindow
(locateRow
, locateColumn
, boxWidth
, boxHeight
, arr
() AS STRING) 'Notes: locateRow, locateColumn for top right corner of selection box on screen in characters for LOCATE.
'boxWidth and boxHeight are in character units, again for locate and print at correct places.
'All displaying is restricted to inside the box, which has PgUP and PgDn as top and bottom lines in the display.
IF boxHeight
< 3 THEN INPUT "boxHeight is too small for call on scrollWindow.", wate$:
EXIT SUB maxWidth = boxWidth ' number of characters in box
maxHeight = boxHeight - 2 ' number of lines displayed of array at one time = 1 page
page = 0
hlite = 0 ' line in display ready for selection by spacebar or if no number is started, enter
clrStr
= SPACE$(maxWidth
) ' clearing a display line
GOSUB update
' show the beginning of the array items for selection
WHILE 1 'until get a selection or demand exit
'handle the key stuff
SELECT CASE kh
'choosing sections of array to display and highlighted item IF (page
+ 1) * maxHeight
+ lba
<= uba
THEN page
= page
+ 1:
GOSUB update
IF (page
- 1) * maxHeight
+ lba
>= lba
THEN page
= page
- 1:
GOSUB update
page
= page
- 1: hlite
= maxHeight
- 1:
GOSUB update
hlite
= hlite
- 1:
GOSUB update
IF (hlite
+ 1) + page
* maxHeight
+ lba
<= uba
THEN 'ok to move up IF hlite
+ 1 > maxHeight
- 1 THEN page
= page
+ 1: hlite
= 0:
GOSUB update
hlite
= hlite
+ 1:
GOSUB update
page
= 0: hlite
= 0:
GOSUB update
page
= INT((uba
- lba
) / maxHeight
): hlite
= maxHeight
- 1:
GOSUB update
'handle the mouse stuff
page
= page
- 1: hlite
= maxHeight
- 1:
GOSUB update
hlite
= hlite
- 1:
GOSUB update
IF (hlite
+ 1) + page
* maxHeight
+ lba
<= uba
THEN 'ok to move up IF hlite
+ 1 > maxHeight
- 1 THEN page
= page
+ 1: hlite
= 0:
GOSUB update
hlite
= hlite
+ 1:
GOSUB update
IF mb
THEN 'click contols or select array item '_delay .2 'this sucks but won't clear mouse click
IF mx
>= 1 AND mx
<= maxWidth
AND my
= 0 THEN 'page up or exit IF my
= 0 AND (mx
<= maxWidth
AND mx
>= maxWidth
- 2) THEN 'exit sign EXIT WHILE 'escape plan for mouse click top right corner of display box IF (page
- 1) * maxHeight
+ lba
>= lba
THEN page
= page
- 1:
GOSUB update
ELSEIF mx
>= 1 AND mx
<= maxWidth
AND my
= maxHeight
+ 1 THEN 'page down bar clicked IF (page
+ 1) * maxHeight
+ lba
<= uba
THEN page
= page
+ 1:
GOSUB update
ELSE ' mouse over highlighting, only if mouse has moved! IF my
- 1 <> hlite
AND (my
- 1 + page
* maxHeight
+ lba
<= uba
) THEN hlite = my - 1
lastmx = mx: lastmy = my
'restore previous color settings
'attempt to clear all key presses before exit
'display of array sections and controls on screen
update:
'fix hlite if it has dropped below last array item
WHILE hlite
+ page
* maxHeight
+ lba
> uba
hlite = hlite - 1
'main display of array items at page * maxHeight (lines high)
FOR row
= 0 TO maxHeight
- 1 IF hlite
= row
THEN COLOR _RGB(200, 200, 255), _RGB32(0, 0, 88) ELSE COLOR _RGB32(0, 0, 88), _RGB(200, 200, 255) LOCATE locateRow
+ row
+ 1, locateColumn:
PRINT clrStr$;
index = row + page * maxHeight + lba
LOCATE locateRow
+ row
+ 1, locateColumn
'make page up and down bars to click, print PgUp / PgDn if available
'make exit sign for mouse click
LOCATE locateRow
, locateColumn
+ maxWidth
- 3
t$ = "*** Mouse and Key Instructions ***"
m$ = "Mouse, mouse wheel, and arrow keys should work as expected for navigation."
m$ = m$ + " Home starts you at lowest array item, End highlights the highest item."
m$ = m$ + " Use PgUp and PgDn keys or Click Green bars to flip through pages of array items."
m$ = m$ + " Escape to exit."
mBox m$, t$
'title$ limit is 57 chars, all lines are 58 chars max
' version bak 2018-09-07_10P
'first screen dimensions items to restore at exit
'screen snapshot
'setup t() to store strings with ti as index, linit 58 chars per line max, b is for build
'are there any new line signals, CR, LF or both? take CRLF or LFCR as one break but dbl LF or CR means blank line
tail = "": ff = 0
FOR j
= LEN(b
) TO 1 STEP -1 'backup until find a space, save the tail end for next line ff = 1 'found space flag
tail = d + tail 'the tail grows!
b = b + c 'just keep building the line
t(ti) = b
bxH = ti + 3: bxW = limit + 2
'draw message box
'now for the action
'convert to pixels the top left corner of box at moment
bxW = bxW * 8: bxH = bxH * 16
tlx = (sw - bxW) / 2: tly = (sh - bxH) / 2
lastx = tlx: lasty = tly
'now allow user to move it around or just read it
IF mx
>= tlx
AND mx
<= tlx
+ bxW
AND my
>= tly
AND my
<= tly
+ 16 THEN 'mouse down on title bar grabx = mx - tlx: graby = my - tly
IF mx
- grabx
>= 0 AND mx
- grabx
<= sw
- bxW
AND my
- graby
>= 0 AND my
- graby
<= sh
- bxH
THEN 'attempt to speed up with less updates
IF ((lastx
- (mx
- grabx
)) ^ 2 + (lasty
- (my
- graby
)) ^ 2) ^ .5 > 10 THEN tlx = mx - grabx: tly = my - graby
lastx = tlx: lasty = tly
'put things back
'clear key presses
'clear mouse clicks
' these subs are used for the background graphics program testing usage of ScrollWindow
'Reference and thanks to:
' http://mathworld.wolfram.com/HeartCurve.html
' find the 6th heart curve equations #7, 8
yCard
= 13 * COS(t
) - 5 * COS(2 * t
) - 2 * COS(3 * t
) - COS(4 * t
)
' this is used to load a file into an array and returns the number of lines in the file
'PRINT filecount%, arr(filecount)
fileCount = fileCount + 1
fLines& = fileCount 'this file returns the number of lines loaded, 0 means file did not exist