News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Drawing Messages using custom markup

Started by kevin, February 25, 2015, 08:56:37 PM

Previous topic - Next topic

kevin

  Drawing Messages using custom markup

    This example takes a user defined input message and skims through it, looking  to dynamic insert/replacement tags which are substituted with users information at runtime.   The tags  in this example is limited to the stuff like today's DATE / TIME  and the mouse position, but you can of course trap your own TAGS and insert them with whatever you want.  


     So our sample message looks like this,

     Message$= "The date today is [date][eol] The current time is [time][eol] Mouse Position [MouseX]x - [MouseY]y[eol]"
   
   
    ----------------------------------------------------------------------
    Tags supported in this mock up
    ----------------------------------------------------------------------

    [time] = insert current time  
    [date] = insert current date   
    [MouseX] = the current x cord of the mouse
    [MouseY] = the current Y cord of the mouse
    [eol] = end of line


        The parser skims through the message looking any of the above tags, when it finds one, it inserts the tags value and continues on. Creating a customized response message for the viewer.  Simple but effective none the less.


 
PlayBASIC Code: [Select]
   ; limit the thing form being a complete system hog
setfps 100

; load a bigger font
loadfont "varanda",1,48

// ----------------------------------------------------------------------
// Tags support in this mock up
// ----------------------------------------------------------------------

// [time] = insert current time
// [date] = insert current date
// [MouseX] = the current x cord of the mouse
// [MouseY] = the current Y cord of the mouse
// [eol] = end of line

// build our markup message
Message$ =" The date today is [date][eol]"
Message$+=" The current time is [time][eol]"
Message$+=" Mouse Position [MouseX]x - [MouseY]y[eol]"

// ----------------------------------------------------------------------
// -- [ Main LOOP ]------------------------------------------------------
// ----------------------------------------------------------------------
Do
cls
RenderMarkUpMessage(100,200,Message$)

Sync
loop


// ----------------------------------------------------------------------
// ---[ RENDER MARK UP MESSAGE ]-----------------------------------------
// ----------------------------------------------------------------------


Function RenderMarkUpMessage(Xpos,Ypos,Message$)

// skim through and replace square braket tags with user information
for lp=1 to len(Message$)

ThisChr=mid(Message$,lp)

if ThisChr=asc("[")

// loop for closing bracket
EndBracketPos=instring(Message$,"]",lp)

if EndBracketPos>lp

lp++
Tag$=upper$(mid$(Message$,lp,EndBracketPos-lp))

// TRAP what type of tag this is and replace it with out output
select tag$
case "TIME"
OutputMessage$+=currenttime$()

case "DATE"
OutputMessage$+=replace$(currentdate$()," ","/")

case "EOL"
OutputMessage$+=chr$(1)

case "MOUSEX"
OutputMessage$+=str$(mousex())
case "MOUSEY"
OutputMessage$+=str$(mousey())

endselect

; skip to end
lp=EndBracketPos


else
OutputMessage$+="<> MISSING closing bracket"
exitfor lp
endif


else
; dump the character to the output
OutputMessage$+=Chr$(ThisCHr)
endif

next


// split the message to an array and draw fragments
Dim Rows$(100)
Count=SplitTOArray(Outputmessage$,chr$(1),Rows$())

// draw fragements
TextHeight=GetTExtHeight("|")
For lp=0 to count
text Xpos,Ypos,Rows$(lp)
Ypos+=TExtHeight
next

EndFunction




kevin

#1
  Square Bracket Parsing with Instring - Drawing Messages with custom markup

    This is another similar example, expect this one includes a parser that uses the instring to step through the original text.   This particular code is actually use to the translate square bracket markup to HTML in various little site builder apps i've written over the years.   You just drop new decoders into the select statement and off you go.  While the example is doing it real time, it wasn't originally intend to.  If that was case I'd probably tokenize the input text and work with binary form if I needed to render dynamic information.      

PlayBASIC Code: [Select]
   Page$=" The date is [date] And the mouse is currently at [MOUSECORDS]Mouse X[xpos] MouseY[ypos] MouseZ[zpos][/MOUSECORDS]   "

setfps 30

do
cls

result$=Parse_All_Square_Bracket_Tags(Page$, Templates$)

print page$
print result$

sync
loop




; -------------------------------------------------------------------------------
; -------------------------------------------------------------------------------
; -----[ PARSE TAGS (GENERAL) ]-------------------------------------------------
; -------------------------------------------------------------------------------
; -------------------------------------------------------------------------------


Function Parse_All_Square_Bracket_Tags(Page$, Templates$)

LastFoundPosition=1

JAvaScript$=""

repeat

; search for square bracket elements
StartBracketPos=instring(page$,"[",LastFoundPosition)
if StartBracketPos >0
CloseBracketPos=instring(page$,"]",StartBracketPos+1)

; found possible tag..
if CloseBracketPOs>StartBracketPos

; set the next search position to location of this find
LastFoundPosition=StartBracketPOs

RebuildPage=false

; get tag name
TagName$=mid$(page$,STartBracketPOs,(CloseBracketPos+1)-STartBracketPos)

EqualsPOs=instring(TagNAme$,"=")

if EqualsPos=0
TagProperties$=""
else
TagProperties$ =mid$(TagName$,EqualsPOs+1,len(TagName$)-(Equalspos+1))
TagName$ =left$(TagName$,EqualsPOs)
endif

#print TagName$

; All Tags should return tags code in OUTPUT$

Select Upper$(TagName$)

; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------
case "[MOUSECORDS]"
; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------

CloseTag$ ="[/MOUSECORDS]"
CloseTagPos =instring(Page$,CloseTag$,CloseBracketPOs)

if CLoseTagPos>CloseBracketPos

; get the characters between the closing bracket and the closing tag
TagData$=mid$(page$,CloseBracketPOs+1,(CloseTagPos-1)-CloseBracketPos)


// Since this tag is meant to insert the mouse cords, we do some replaces on the TagDATA$
// and output it to OUTPUT$. So the parser inserts the key data back into the output text for display

Output$=Replace$(TagData$, "[xpos]",str$(mousex()))
Output$=Replace$(Output$, "[ypos]",str$(mousey()))
Output$=Replace$(Output$, "[zpos]",str$(mousez()))


; rebuild the page using method #1
RebuildPage=1

else
// FAILED.. DUMP error
#print "Missing closing "+closetag$)
LastFoundPosition++
endif


; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------
case "[DATE]"
; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------

Output$=currentdate$()

; rebuild the page using method #2
RebuildPage=2
LastFoundPosition=StartBracketPOs+1



; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------
case "[YOUR DECODER GOES HERE]"
; ---------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------


; ------------------------------------------------------------------
default
; ------------------------------------------------------------------


; No supported DECODER for this tag, so step the last search position
; pass the open bracket, so we don't get stuck in a loop
LastFoundPosition=StartBracketPOs+1


EndSelect


// -------------------------------------
// Rebuild resulting page
// -------------------------------------
if RebuildPage=1
// ; build the new version of page with the filled in FAQ chunk in it's place
Page$=Left$(Page$,StartBracketPos-1)+Output$+CutLeft$(Page$,CLoseTagPos+(Len(CloseTag$)-1))
endif


; -------------------------------------
; rebuild page around single tags
; -------------------------------------
if RebuildPage=2
// ; build the new version of page with the filled in FAQ chunk in it's place
Page$=Left$(Page$,StartBracketPos-1)+Output$+CutLeft$(Page$,StartBracketPos+Len(TagNAme$)-1)
endif
Login required to view complete source code