News:

Function Finder  Find all the functions within source code files

Main Menu

Display Text the underlined parts

Started by kevin, May 24, 2023, 10:35:29 AM

Previous topic - Next topic

kevin

Display Text the underlined parts


This PlayBASIC code you provided allows users to print messages with markup in them, which will underline specific keywords within the message. Here's a breakdown of how the code works:

1. Two fonts are loaded using the "loadfont" command. The first font is named "courier" with a size of 10, and the second font is also named "courier" but with a size of 11 and an underlined style (specified by the "30,4" parameters). These fonts will be used to display text in different styles (normal and underlined).


2. The "Message" function is defined, which takes three parameters: Xpos (X-coordinate of the message), Ypos (Y-coordinate of the message), and Text$ (the text message with markup).


3. Inside the "Message" function:
   - The font is set to the 10-point "courier" font using the "setfont" command.
   - A loop iterates through each character of the "Text$" string.
   - For each character, it checks if it's an opening curly brace '{' (ASCII code 123). If it is, it looks for the corresponding closing curly brace '}' in the message.
   - If the closing brace is found, the code inside the braces is extracted into the "Tag$" variable, and a "select" statement is used to determine the type of tag (in this case, either "{U}" for underline or "{/U}" to end the underline). The font is then set accordingly (font 11 for underline or font 10 for normal).
   - The loop variable "lp" is updated to the position of the closing brace, and the loop continues.
   - If a character is not an opening brace, it is added to the "Output$" string.

4. After processing the entire "Text$" string, the code checks if there is any content left in the "Output$" variable (i.e., text outside of the markup tags). If there is content, it is displayed at the specified (Xpos, Ypos) coordinates using the current font (either normal or underlined).

5. The code snippet at the end contains an example message with markup: "This is {u}my page{/u} of stuff."

In summary, this code processes a text message with markup tags (specifically "{U}" for underline and "{/U}" to end the underline). It changes the font style based on these tags and displays the message with underlined keywords as specified in the markup.


PlayBASIC Code: [Select]
      loadfont  "courier",10,30,0
loadfont "courier",11,30,4

Message(100,300,ReadData$())

Sync
waitkey



Function Message(Xpos,Ypos,Text$)
setfont 10

for lp=1 to len(Text$)
ThisCHR=mid(Text$,lp)

if ThisCHR=asc("{")

ClosingPOS =instring(Text$,"}",lp)
if ClosingPOS>lp

Text Xpos,ypos,Output$
Xpos+=GetTextWidth(Output$)
output$=""

// Decode the tag
Tag$=Mid$(Text$,lp,(ClosingPOS-lp)+1)
select upper$(Tag$)
case "{U}"
setfont 11

case "{/U}"
setfont 10
endselect

// Move to the end of this tag
lp=ClosingPOS
continue
endif

else
Output$+=chr$(ThisCHR)
endif
next

if len(output$)
text xpos,ypos,output$
endif

EndFunction


Data "This is {u}my page{/u} of stuff"







  PlayBASIC HELP FILES

  Learn to code;  Beginners Coding ; Programming For Kids