From: Svjatoslav Agejenko Date: Sun, 29 Jun 2025 16:18:21 +0000 (+0300) Subject: Better code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=2c7b3457c99aaee5549f2994f2170e73a83c4cb1;p=qbasicapps.git Better code readability --- diff --git a/Miscellaneous/wsystem.bas b/Miscellaneous/wsystem.bas index c5c9332..625b09e 100755 --- a/Miscellaneous/wsystem.bas +++ b/Miscellaneous/wsystem.bas @@ -8,245 +8,266 @@ ' Changelog: ' 2003, Initial version -' 2024, Improved program readability using AI +' 2024-2025, Improved program readability DECLARE SUB demo () -DECLARE FUNCTION getline$ (w%, l%) -DECLARE SUB loadfile (file$, d%) -DECLARE SUB sendline (w%, l%, newstring$) -DECLARE FUNCTION getflin% () -DECLARE SUB refresh () -DECLARE FUNCTION addpage% (x%, y%, xs%, ys%, title$) -DECLARE SUB box (x%, y%, xl%, yl%, e$) +DECLARE FUNCTION GetLineFromWindow$ (windowNum%, lineNum%) +DECLARE SUB LoadFileIntoWindow (fileName$, windowNum%) +DECLARE SUB SendLineToWindow (windowNum%, lineNum%, newString$) +DECLARE FUNCTION GetFreeLineIndex% () +DECLARE SUB RefreshAllWindows () +DECLARE FUNCTION AddWindow% (x%, y%, widt%, haigh%, title$) +DECLARE SUB DrawBox (x%, y%, widt%, haigh%, edgeStyle$) DEFINT A-Z -DECLARE SUB shpage (a) -DECLARE SUB start () +DECLARE SUB ShowWindow (windowNum) +DECLARE SUB InitializeSystem () -DIM SHARED stamo -stamo = 5000 -DIM SHARED st$(1 TO stamo) -DIM SHARED stpn +' Global variables for text storage +DIM SHARED maxStorage% +maxStorage% = 5000 +DIM SHARED textStorage$(1 TO maxStorage%) +DIM SHARED storagePointer% -DIM SHARED pag(1 TO 10, 1 TO 1000) -DIM SHARED pagx(1 TO 10), pagy(1 TO 10), pagxs(1 TO 10), pagys(1 TO 10) -DIM SHARED pagon(1 TO 10) -DIM SHARED pagtitle$(1 TO 10) +' Window-related global arrays +DIM SHARED windowData(1 TO 10, 1 TO 1000) ' Stores line indices for each window +DIM SHARED windowX(1 TO 10), windowY(1 TO 10) ' Position of each window +DIM SHARED windowWidth(1 TO 10), windowHeight(1 TO 10) ' Size of each window +DIM SHARED windowActiveStatus(1 TO 10) ' Whether window is active +DIM SHARED windowTitle$(1 TO 10) ' Title of each window -DIM SHARED pagshx(1 TO 10) ' x & y shift -DIM SHARED pagshy(1 TO 10) +' Window scrolling/shifting +DIM SHARED windowShiftX(1 TO 10) ' Horizontal shift +DIM SHARED windowShiftY(1 TO 10) ' Vertical shift -DIM SHARED pageactive ' active page +DIM SHARED currentActiveWindow% ' Currently active window for display -start +InitializeSystem demo -FUNCTION addpage% (x, y, xs, ys, title$) - FOR a = 1 TO 10 - IF pagon(a) = 0 THEN - b = a - GOTO 1 - END IF - NEXT a -1 - - pagon(b) = 1 - pagx(b) = x - pagy(b) = y - pagxs(b) = xs - pagys(b) = ys - pagtitle$(b) = title$ +FUNCTION AddWindow% (x%, y%, widt%, heigh%, title$) + ' Adds a new window to the system - addpage% = b + ' Find an empty window slot + FOR windowNum% = 1 TO 10 + IF windowActiveStatus(windowNum%) = 0 THEN + foundWindow% = windowNum% + GOTO FoundEmptySlot + END IF + NEXT windowNum% + +FoundEmptySlot: + ' Initialize the window properties + windowActiveStatus(foundWindow%) = 1 + windowX(foundWindow%) = x% + windowY(foundWindow%) = y% + windowWidth(foundWindow%) = widt% + windowHeight(foundWindow%) = heigh% + windowTitle$(foundWindow%) = title$ + + ' Return the window number + AddWindow% = foundWindow% END FUNCTION -SUB clrwnd (w) - FOR a = 1 TO 1000 - IF pag(w, a) > 0 THEN - st$(pag(w, a)) = "" - pag(w, a) = 0 +SUB ClearWindowContent (windowNum%) + ' Clears all content from a window + + FOR lineNum% = 1 TO 1000 + IF windowData(windowNum%, lineNum%) > 0 THEN + textStorage$(windowData(windowNum%, lineNum%)) = "" + windowData(windowNum%, lineNum%) = 0 END IF - NEXT a + NEXT lineNum% END SUB SUB demo ' Create three windows with different sizes and titles - w1 = addpage%(1, 1, 30, 10, "window 1.") - w2 = addpage%(1, 12, 80, 30, "second window") - w3 = addpage%(31, 2, 30, 10, "last window") + window1% = AddWindow%(1, 1, 30, 10, "window 1.") + window2% = AddWindow%(1, 12, 80, 30, "second window") + window3% = AddWindow%(31, 2, 30, 10, "last window") ' Load the same file into all windows - loadfile "wsystem.bas", w2 - loadfile "wsystem.bas", w1 - loadfile "wsystem.bas", w3 + LoadFileIntoWindow "wsystem.bas", window2% + LoadFileIntoWindow "wsystem.bas", window1% + LoadFileIntoWindow "wsystem.bas", window3% -4 - pageactive = INT(RND * 3) + 1 - refresh +AnimateWindows: + ' Randomly select an active window to animate + currentActiveWindow% = INT(RND * 3) + 1 + RefreshAllWindows ' Animate the windows by shifting their content - FOR a = 1 TO 100 - pagshx(pageactive) = SIN(a / 10) * 10 + 10 - pagshy(pageactive) = a - shpage (pageactive) + FOR animationFrame% = 1 TO 100 + windowShiftX(currentActiveWindow%) = SIN(animationFrame% / 10) * 10 + 10 + windowShiftY(currentActiveWindow%) = animationFrame% + ShowWindow currentActiveWindow% + ' split-second delay SOUND 0, 1 IF INKEY$ <> "" THEN SYSTEM - NEXT a + NEXT animationFrame% - GOTO 4 + GOTO AnimateWindows END SUB -FUNCTION getflin% ' Get free line -2 - IF stpn > 1000 THEN - stpn = 1 +FUNCTION GetFreeLineIndex% + ' Finds a free line in text storage + +FindNextLine: + IF storagePointer% > 1000 THEN + storagePointer% = 1 END IF - IF st$(stpn) = "" THEN - getflin% = stpn - stpn = stpn + 1 + + IF textStorage$(storagePointer%) = "" THEN + GetFreeLineIndex% = storagePointer% + storagePointer% = storagePointer% + 1 ELSE - stpn = stpn + 1 - GOTO 2 + storagePointer% = storagePointer% + 1 + GOTO FindNextLine END IF END FUNCTION -FUNCTION getline$ (w, l) - ' Retrieve the line from the window memory - IF pag(w, l) = 0 THEN - getline$ = "" +FUNCTION GetLineFromWindow$ (windowNum%, lineNum%) + ' Retrieve a line from a window's memory + + IF windowData(windowNum%, lineNum%) = 0 THEN + GetLineFromWindow$ = "" ELSE - getline$ = st$(pag(w, l)) + GetLineFromWindow$ = textStorage$(windowData(windowNum%, lineNum%)) END IF END FUNCTION -SUB loadfile (file$, d) - ' Load a file into the window memory +SUB LoadFileIntoWindow (fileName$, windowNum%) + ' Load a file into a window's memory - OPEN file$ FOR INPUT AS #1 - FOR a = 1 TO 1000 + OPEN fileName$ FOR INPUT AS #1 + FOR lineNum% = 1 TO 1000 IF EOF(1) <> 0 THEN - GOTO 3 + GOTO FileLoaded END IF - LINE INPUT #1, a$ - sendline d, a, a$ - NEXT a -3 + LINE INPUT #1, lineContent$ + SendLineToWindow windowNum%, lineNum%, lineContent$ + NEXT lineNum% +FileLoaded: CLOSE #1 ' Fill the remaining lines with empty strings - FOR b = a TO 1000 - sendline d, b, "" - NEXT b + FOR blankLineNum% = lineNum% TO 1000 + SendLineToWindow windowNum%, blankLineNum%, "" + NEXT blankLineNum% END SUB -SUB refresh +SUB RefreshAllWindows ' Redraw all active windows + CLS - FOR a = 1 TO 10 - IF pagon(a) > 0 THEN - shpage (a) + FOR windowNum% = 1 TO 10 + IF windowActiveStatus(windowNum%) > 0 THEN + ShowWindow windowNum% END IF - NEXT a + NEXT windowNum% END SUB -SUB sendline (w, l, newstring$) ' window, lineNum, lineItself - ' send string into window memory - a$ = newstring$ +SUB SendLineToWindow (windowNum%, lineNum%, newString$) + ' Stores a string in a window's memory + + lineContent$ = newString$ ' Remove trailing spaces from the string - IF a$ = SPACE$(LEN(a$)) THEN - a$ = "" + IF lineContent$ = SPACE$(LEN(lineContent$)) THEN + lineContent$ = "" END IF - IF LEN(a$) > 0 THEN -5 - IF RIGHT$(a$, 1) = " " THEN - a$ = LEFT$(a$, LEN(a$) - 1) - GOTO 5 + IF LEN(lineContent$) > 0 THEN +TrimRight: + IF RIGHT$(lineContent$, 1) = " " THEN + lineContent$ = LEFT$(lineContent$, LEN(lineContent$) - 1) + GOTO TrimRight END IF END IF ' Update the window memory with the new string - IF a$ = "" THEN - IF pag(w, l) > 0 THEN - st$(pag(w, l)) = "": pag(w, l) = 0 + IF lineContent$ = "" THEN + IF windowData(windowNum%, lineNum%) > 0 THEN + textStorage$(windowData(windowNum%, lineNum%)) = "": windowData(windowNum%, lineNum%) = 0 END IF ELSE - IF pag(w, l) = 0 THEN - pag(w, l) = getflin% + IF windowData(windowNum%, lineNum%) = 0 THEN + windowData(windowNum%, lineNum%) = GetFreeLineIndex% END IF - st$(pag(w, l)) = a$ + textStorage$(windowData(windowNum%, lineNum%)) = lineContent$ END IF END SUB -SUB shpage (page) - ' Draw the specified window on the screen +SUB ShowWindow (windowNum%) + ' Draws a window on the screen - ' Determine background color based on active page - IF page = pageactive THEN - bg = 1 + ' Determine background color based on active window + IF windowNum% = currentActiveWindow% THEN + bgColor% = 1 ELSE - bg = 0 + bgColor% = 0 END IF - x = pagx(page) - y = pagy(page) - xl = pagxs(page) - yl = pagys(page) - e$ = pagtitle$(page) - - COLOR 11, bg - - ' Draw the window border - a$ = "" - d$ = "" - FOR a = 1 TO xl - 2 - a$ = a$ + CHR$(205) - NEXT a - b$ = CHR$(201) + a$ + CHR$(187) - c$ = CHR$(200) + a$ + CHR$(188) - - LOCATE y, x - PRINT b$ - LOCATE y + yl - 1, x - PRINT c$ - - ' Draw the window content - FOR a = 1 TO yl - 2 - LOCATE y + a, x - d$ = getline$(page, a + pagshy(page)) - d$ = d$ + SPACE$(300) - d$ = RIGHT$(d$, LEN(d$) - pagshx(page)) - d$ = LEFT$(d$, xl - 2) - PRINT CHR$(186) + d$ + CHR$(186) - NEXT a - - ' Draw the window title - xt = INT(x + (xl / 2) - (LEN(e$) / 2) - 2) - LOCATE y, xt + x% = windowX(windowNum%) + y% = windowY(windowNum%) + widt% = windowWidth(windowNum%) + heigh% = windowHeight(windowNum%) + title$ = windowTitle$(windowNum%) + + COLOR 11, bgColor% + + ' Create border components + FOR borderSegment% = 1 TO widt% - 2 + topBottom$ = topBottom$ + CHR$(205) + NEXT borderSegment% + borderTop$ = CHR$(201) + topBottom$ + CHR$(187) + borderBottom$ = CHR$(200) + topBottom$ + CHR$(188) + + ' Draw top border + LOCATE y%, x% + PRINT borderTop$ + + ' Draw bottom border + LOCATE y% + heigh% - 1, x% + PRINT borderBottom$ + + ' Draw window content + FOR lineNum% = 1 TO heigh% - 2 + LOCATE y% + lineNum%, x% + lineContent$ = GetLineFromWindow$(windowNum%, lineNum% + windowShiftY(windowNum%)) + lineContent$ = lineContent$ + SPACE$(300) + lineContent$ = RIGHT$(lineContent$, LEN(lineContent$) - windowShiftX(windowNum%)) + lineContent$ = LEFT$(lineContent$, widt% - 2) + PRINT CHR$(186) + lineContent$ + CHR$(186) + NEXT lineNum% + + ' Draw window title + titleX% = INT(x% + (widt% / 2) - (LEN(title$) / 2) - 2) + LOCATE y%, titleX% PRINT "[ " - xt = xt + 2 + titleX% = titleX% + 2 COLOR 10 - LOCATE y, xt - PRINT e$ + LOCATE y%, titleX% + PRINT title$ - xt = xt + LEN(e$) + titleX% = titleX% + LEN(title$) COLOR 11 - LOCATE y, xt + LOCATE y%, titleX% PRINT " ]" COLOR 7, 0 END SUB -SUB start +SUB InitializeSystem ' Initialize the screen and shared memory + WIDTH 80, 50 VIEW PRINT 1 TO 50 - FOR a = 1 TO stamo - st$(a) = "" - NEXT a + FOR storageIndex% = 1 TO maxStorage% + textStorage$(storageIndex%) = "" + NEXT storageIndex% - stpn = 1 + storagePointer% = 1 END SUB \ No newline at end of file diff --git a/Miscellaneous/wsystem.webm b/Miscellaneous/wsystem.webm new file mode 100644 index 0000000..1a456bb Binary files /dev/null and b/Miscellaneous/wsystem.webm differ