# browse CoCo 2B VDG modes
# Jerry Stratton hoboes.com/coco

#case names
data !cases%
	lower
	reverse
enddata

#border/foreground names
data !foregrounds%
	black
	green
	orange
enddata

#background names
data !backgrounds%
	green
	orange
	cyan
	brown
enddata

%caseCount% = !cases%
%borderCount% = !foregrounds%
%foregroundCount% = !foregrounds%
%backgroundCount% = !backgrounds%

%currentCase% = 1
%currentBorder% = 1
%currentForeground% = 1
%currentBackground% = 1

for i = 1 to %caseCount%
	read %caseList$(i)
next i
for i = 1 to %borderCount%
	read i$
	%borderList$(i) = i$
	%foregroundList$(i) = i$
next i
for i = 1 to %backgroundCount%
	read %backgroundList$(i)
next i

cls
#all printable characters
for i = 32 to 255
	print chr$(i);
next

#initialize toggle positions
%casePosition% = 32*15+1
%foregroundPosition% = %casePosition%+9
%backgroundPosition% = %foregroundPosition%+8
%borderPosition% = %backgroundPosition%+8
%currentToggle% = 1

print@%casePosition%-32,"CASE";
print@%foregroundPosition%-32,"FORE";
print@%backgroundPosition%-32,"BACK";
print@%borderPosition%-32,"BORDER";

#main loop
loop
	print@%casePosition%-1,string$(31," ");
	%lastCharacter$=" ":gosub printLastCharacter
	gosub changeVideo

	print@%casePosition%,%caseList$(%currentCase%);
	print@%foregroundPosition%,%foregroundList$(%currentForeground%);
	print@%backgroundPosition%,%backgroundList$(%currentBackground%);

	print@%borderPosition%,left$(%borderList$(%currentBorder%), len(%borderList$(%currentBorder%))-1);
	%lastCharacter$=right$(%borderList$(%currentBorder%),1):gosub printLastCharacter

	on %currentToggle% gosub changeCase, changeForeground, changeBackground, changeBorder
endloop

sub changeCase
	print@%casePosition%-1,"*";
	%choice% = %currentCase%
	%maximum% = %caseCount%
	gosub userInput
	%currentCase%=%choice%
endsub

sub changeForeground
	print@%foregroundPosition%-1,"*";
	%choice% = %currentForeground%
	%maximum% = %foregroundCount%
	gosub userInput
	%currentForeground% = %choice%

	if %currentForeground% <> 1 then %currentBorder% = 1
	if %currentForeground% = 1 and %currentBackground% > 2 then %currentBackground% = 1
	if %currentForeground% = 2 then %currentBackground% = 3
	if %currentForeground% = 3 then %currentBackground% = 4
endsub

sub changeBackground
	print@%backgroundPosition%-1,"*";
	%choice% = %currentBackground%
	%maximum% = %backgroundCount%
	gosub userInput
	%currentBackground% = %choice%

	if %currentBackground% = 1 then %currentForeground% = 1:if %currentBorder% = 3 then %currentBorder% = 1
	if %currentBackground% = 2 then %currentForeground% = 1:if %currentBorder% = 2 then %currentBorder% = 1
	if %currentBackground% = 3 then %currentForeground% = 2:%currentBorder% = 1
	if %currentBackground% = 4 then %currentForeground% = 3:%currentBorder% = 1
endsub

sub changeBorder
	print@%borderPosition%-1,"*";
	%choice% = %currentBorder%
	%maximum% = %borderCount%
	gosub userInput
	%currentBorder% = %choice%

	if %currentBorder% <> 1 then %currentForeground% = 1
	if %currentBorder% = 2 then %currentBackground% = 1
	if %currentBorder% = 3 then %currentBackground% = 2
endsub

#change video mode
sub changeVideo
	%mode% = 0

	#case
	if %currentCase%=1 then %mode% = %mode% or 16

	#border
	if %currentBorder%>1 then %mode%=%mode% or 64
	if %currentBorder%=3 then %mode%=%mode% or 8

	#foreground
	if %currentForeground%>1 then %mode%=%mode% or 32
	if %currentForeground%=3 then %mode%=%mode% or 8

	#background
	if %currentBackground%=2 or %currentBackground%=4 then %mode%=%mode% or 8

	print@32*12+14,%mode%;"   ";
	poke 359,57
	poke 65314,%mode%
endsub

#wait for arrow press
sub userInput
	loop
		loop
			a$=inkey$
		endloop unless (a$="")

		a = asc(a$)
		#left/right arrows change toggle, up/down arrows change value
		switch
		case (a = 8 or a = 9) # change toggle
			if a=8 then %currentToggle%-- else %currentToggle%++
			if %currentToggle%<1 then %currentToggle% = 4
			if %currentToggle%>4 then %currentToggle% = 1
			return
		case (a = 94 or a = 10) # change toggle value
			if a=94 then a=1 else a=-1
			%choice%=%choice%+a
			if %choice%<1 then %choice%=%maximum% else if %choice%>%maximum% then %choice%=1
			return
		endswitch
	endloop
endsub

#print at last space on screen
sub printLastCharacter
	#space displays reversed when poked?
	if %lastCharacter$=" " then %lastCharacter$=CHR$(96)

	%cursorAddress% = peek(136)*256+peek(137)
	poke %cursorAddress%, asc(%lastCharacter$)
endsub
