Simple Text Menu
While tinkering last night, I wrote a simple menu library that lets the programmer define a list of text options that can have a key and mousing bindings as well. The library is more a starting point than a end product, but it shows a way how we can wrap a system into library that help us separate that functionality from the main program. The user can tag a function (by name) that is called when any option is clicked or the key binding is pressed.
Example:
So our processing loop looks like this..
COMPLETE EXAMPLE:
While tinkering last night, I wrote a simple menu library that lets the programmer define a list of text options that can have a key and mousing bindings as well. The library is more a starting point than a end product, but it shows a way how we can wrap a system into library that help us separate that functionality from the main program. The user can tag a function (by name) that is called when any option is clicked or the key binding is pressed.
Example:
So our processing loop looks like this..
PlayBASIC Code:
loadfont "verdana",1 , 48 cr$=chr$(10)+chr$(13) Options$ =" L) Load File [BindKey=38][OnClick=LOAD_FUNCTION]"+cr$ Options$+=" S) Save File [BindKey=31][OnClick=SAVE_FUNCTION]"+cr$ Options$+=" D) Delete File [BindKey=32][OnClick=DELETE_FUNCTION]"+cr$ // GUI_CREATE_SIMPLE_MENU(Options$) Do cls $304050 GUI_UPDATE_SIMPLE_MENU_INPUTSTATE() GUI_DRAW_SIMPLE_MENU() action$=GUI_Get_SIMPLE_MENU_CLICKS() if Len(Action$) Index=FunctionIndex(Action$) if Index CallFunction Action$ endif endif sync loop Function LOad_Function() cls 255 print "LOAD FUNCTION" EndFunction Function SAVE_Function() cls $00ff00 Print "SAVE FUNCTION" EndFunction Function DELETE_Function() cls $ff0000 Print "DELETE FUNCTION" EndFunction
COMPLETE EXAMPLE:
PlayBASIC Code:
loadfont "verdana",1 , 48 cr$=chr$(10)+chr$(13) Options$ =" L) Load File [BindKey=38][OnClick=LOAD_FUNCTION]"+cr$ Options$+=" S) Save File [BindKey=31][OnClick=SAVE_FUNCTION]"+cr$ Options$+=" D) Delete File [BindKey=32][OnClick=DELETE_FUNCTION]"+cr$ // GUI_CREATE_SIMPLE_MENU(Options$) Do cls $304050 GUI_UPDATE_SIMPLE_MENU_INPUTSTATE() GUI_DRAW_SIMPLE_MENU() action$=GUI_Get_SIMPLE_MENU_CLICKS() if Len(Action$) Index=FunctionIndex(Action$) if Index CallFunction Action$ endif endif sync loop Function LOad_Function() cls 255 print "LOAD FUNCTION" EndFunction Function SAVE_Function() cls $00ff00 Print "SAVE FUNCTION" EndFunction Function DELETE_Function() cls $ff0000 Print "DELETE FUNCTION" EndFunction //------------------------------------------------------------------ //------------------------------------------------------------------ //------------------------------------------------------------------ //------------------------------------------------------------------ //------------------------------------------------------------------ Type tSimpleTextMenuInputState Status MX MY MB ScanCode EndType Type tSimpleTextMenu Caption$ KeyBindingScanCode FunctionToCall$ x1,y1,x2,y2; render position of this on screen MouseOver EndType Dim SimpleTextMenu(256) as tSimpleTextmenu Dim SimpleTextmenuInputState as tSimpleTextMenuInputState function GUI_CREATE_SIMPLE_MENU(MenuOptions$) // Redim the Global Memu Dim SimpleTextMenu(256) as tSimpleTextmenu // GUI_INIT_SIMPLE_MENU_INPUTSTATE() dim Rows$(1024) Options$=Replace$(MenuOptions$,chr$(10),"") LinesOfText = SplitToArray(options$,chr$(13),rows$(),0,0) MenuIndex =1 for lp=0 to LinesOftext-1 s$=Rows$(lp) // Check for empty line if len(trim$(S$))>0 Param_KeyBind$="" Param_Onclick$="" Current_Pos =1 do // Look for square brackets Left_pos=instring(s$,"[",Current_pos) if Left_Pos Right_pos=instring(s$,"]",Current_pos) If Right_Pos>Left_Pos Opcode$=Mid$(s$,Left_pos+1,(Right_Pos-Left_Pos)-1) Current_Pos=Left_pos S1$= Left$(s$,Left_pos-1) S2$= CutLeft$(s$,Right_pos) s$=s1$+s2$ // get Opcode and Value Value$="" Equals_pos = instring(opcode$,"=") if Equals_pos Value$= CutLeft$(Opcode$,Equals_pos) Opcode$= Left$(Opcode$,Equals_pos-1) endif Select upper$(trim$(opcode$)) // process embedded opcodes for this menu option case "ONCLICK" Param_Onclick$ = value$ case "KEYBIND", "BINDKEY", "KEY" Param_KEYBIND$ = value$ EndSelect // reset the next search Current_pos=Left_pos else Current_Pos=Left_pos+1 endif else exitdo endif loop // ----------------------------------------------- if Len(s$) // ----------------------------------------------- SimpleTextMenu(MenuIndex)=New tSimpleTextMenu SimpleTextMenu(MenuIndex).Caption =s$ SimpleTextMenu(MenuIndex).KeyBindingScanCode =val(Param_KeyBind$) SimpleTextMenu(MenuIndex).FunctionToCall =Param_OnClick$ MenuIndex ++ endif endif next EndFunction function GUI_DRAW_SIMPLE_MENU() GUI_INIT_SIMPLE_MENU_INPUTSTATE() // Message includes the ONCLICK= MouseStatus = SimpleTextmenuInputState.Status Mx= SimpleTextmenuInputState.MX My = SimpleTextmenuInputState.MY Mb = SimpleTextmenuInputState.MB CursorX=GetCursorX() CursorY=GetCursorY() TH =GettextHeight("|") for lp=0 to getArrayElements(SimpleTextMenu()) if SimpleTextMenu(lp) T$=SimpleTextMenu(lp).Caption$ CursorX2 = CursorX1+GettextWidth(t$) CursorY2 = CursorY1+TH SimpleTextMenu(lp).x1=CursorX1 SimpleTextMenu(lp).X2=CursorX2 SimpleTextMenu(lp).Y1=CursorY1 SimpleTextMenu(lp).Y2=CursorY2 MouseOverState = 0 if MouseStatus if range(My,CursorY1,CursorY2-1) inkmode 1+32 c=GetInk() BoxC 0,CursorY1,getSurfaceWidth(),CursorY2,true,RgbFade(c,50) inkmode 1 MouseOverState = 1 endif endif SimpleTextMenu(lp).MouseOver=MouseOverState // Check if mouse over text CursorX1,CursorY1, t$ CursorY1=CursorY2 endif next EndFunction Function GUI_Get_SIMPLE_MENU_CLICKS() GUI_INIT_SIMPLE_MENU_INPUTSTATE() MouseStatus = SimpleTextmenuInputState.Status Mx= SimpleTextmenuInputState.MX My = SimpleTextmenuInputState.MY Mb = SimpleTextmenuInputState.MB if MouseStatus if MB for lp=0 to getArrayElements(SimpleTextMenu()) if SimpleTextMenu(lp) if SimpleTextMenu(lp).MouseOver if Mb & 1 Action$ = SimpleTextMenu(lp).FunctionToCall endif endif endif next endif endif SC = SimpleTextmenuInputState.ScanCode if SC for lp=0 to getArrayElements(SimpleTextMenu()) if SimpleTextMenu(lp) if SimpleTextMenu(lp).KeyBindingScanCode=SC Action$ = SimpleTextMenu(lp).FunctionToCall endif endif next endif EndFunction Action$ Function GUI_UPDATE_SIMPLE_MENU_INPUTSTATE() GUI_INIT_SIMPLE_MENU_INPUTSTATE() SimpleTextmenuInputState.Status = true SimpleTextmenuInputState.Mx =MouseX() SimpleTextmenuInputState.MY =MouseY() SimpleTextmenuInputState.MB =MouseButton() SimpleTextmenuInputState.ScanCode =ScanCode() EndFunction Function GUI_INIT_SIMPLE_MENU_INPUTSTATE() if GetArrayStatus(SimpleTextmenuInputState())=false Dim SimpleTextmenuInputState as tSimpleTextMenuInputState endif EndFunction