Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 11:04 05 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : [MMBasic] LineEdit & more (2 functions)

Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 10:57am 28 Sep 2014
Copy link to clipboard 
Print this post

Hi all.

This is are code snippet (2 functions)for Maximite MMBasic. I use it in a current project. Because I did not find something like this in the MMBasic library I post it here. Maybe it's useful for this nice guys here.

'* Demo code:

Cls
Print @(0,0);
a$= LineEdit$("The Backshed")
Print
Print a$
End

'*************************************************
'*
'* LineEdit$ V.90
'*
'* MMBasic 4.5/Maximite
'*
'* by twofingers 09-2014 at TBS
'* line editor for small strings (e.g. file names)
'*------------------------------------------------
'* Limit:
'* The string must not exceed the width of screen
'*
'* Supported keys:
'* left arrow, right arrow
'* Enter
'* ESC
'* Home
'* End
'* Del
'* Backspace
'* all ASCII chars (char 32-126)
'*
'*------------------------------------------------
'* Known issues (just to remember):
'* 1. EXIT from do:loop
'* workaround: Flag (enterPressed)
'* in LineEdit$
'* seems to be a bug
'* 2. keydown without waitstates
'* workaround: 200ms pause
'* in LineEdit$
'* seems to be a bug?
'*
'* possible improvments:
'* 1. insertmode
'* 2. a fixed minimum string size
'*************************************************
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'*********************************************************** *
Function LineEdit$ text$
Local hpos, vpos, p, ep, keyvalue, enterPressed
Local ltext$,mtext$,rtext$
Local TRUE, FALSE
'Local insertMode

TRUE=1
FALSE=0
hpos=MM.HPos:vpos=MM.VPos ' save print position
enterPressed=FALSE ' Flag to EXIT the loop
p=Len(text$) ' cursor pointer
ep=p ' end pointer
LineEdit$=text$ ' backup text$ for Esc

Pause 200 ' es gibt einen Zeitfaktor fuer keyboard input buffer ~140ms

Print @(hpos,vpos)Left$(text$,p-1);
Font#1,,1:Print Right$(text$,1);:Font#1

Do
Do:keyvalue=KeyDown:Pause 20:Loop While keyvalue=0

If keyvalue=130 And p>1 Then p=p-1 'left arrow
If keyvalue=131 And p<ep Then p=p+1 'right arrow
If keyvalue=13 Then
enterPressed=TRUE 'Enter
EndIf
If keyvalue=27 Then
Print @(hpos,vpos)LineEdit$;
Exit Function 'ESC, leave LineEdit$ untouched
EndIf
If keyvalue=134 Then p=1 'Home
If keyvalue=135 Then p=ep 'End
'>>>If keyvalue=132 Then insertMode=Not insertMode
'Insert toggle (still dummy)
If keyvalue>=32 And keyvalue<127 Then 'all ASCII chars
If p<ep Then
p=p+1
ltext$=ltext$+Chr$(keyvalue)
mtext$=Mid$(text$,p,1)
rtext$=Mid$(text$,p+1)
Else
ltext$=Left$(text$,p-1)
mtext$=Chr$(keyvalue)
rtext$=""
EndIf
ElseIf keyvalue=8 And p>1 Then 'Backspace
p=p-1
ltext$=Mid$(text$,1,p-1)
mtext$=" " 'blank replacement
rtext$=Mid$(text$,p+1)
ElseIf keyvalue=127 Then 'Del
ltext$=Mid$(text$,1,p-1)
mtext$=" " 'blank replacement
rtext$=Mid$(text$,p+1)
Else
ltext$=Mid$(text$,1,p-1) 'move cursor
mtext$=Mid$(text$,p,1)
rtext$=Mid$(text$,p+1,ep-p)
EndIf

Print @(hpos,vpos)ltext$;
Font#1,,1
Print mtext$;
Font#1
Print rtext$;

text$=ltext$+mtext$+rtext$

Pause 150 ' some wait states to slow down key input
Loop While Not enterPressed
LineEdit$=ltext$+mtext$+rtext$
Print @(hpos,vpos)LineEdit$;
End Function
'*****************************************************




'*************************************************
'*
'* Sec2HMS$
'*------------------------------------------------
'* a function to convert seconds (positive integer)
'* into a "00:00:00" time string
'*
'* limit = 359999 sec ~ 4,17 days
'*
'* MMBasic 4.5/Maximite
'* Note: This code should also work with µMites
'* when you replace the "Format$" like this:
'* Str$(Hour,2,,"0")+":" instead of
'* Format$(Hour,"%02g")+":"
'*------------------------------------------------
'* by twofingers 09-2014 at TBS
'-------------------------------------------------
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'**************************************************

'* Demo code:
i= 9
do
Print Sec2Hms$(i),i
i=i*10
loop while i <=90000

end
'**************************************************

Function Sec2HMS$ sec ' convert sec (number) into "00:00:00" time string
Local Hour,Mins,Secs

hms.days= Int(sec/86400) ' just if you need to know
Hour= Int(sec/3600)
Mins= Int((sec Mod 3600)/60)
Secs= Int((sec Mod 3600) Mod 60)
Sec2Hms$=Format$(Hour,"%02g")+":"+Format$(Mins,"%02g")+":"+F ormat$(Secs,"%02g")
End Function



Edit:
Download (both functions):
2014-09-28_220412_Lineedit.zip

Edit2:
a improved version with INSERT MODE
2014-10-14_142507_LineEdit2.zip


Any comments, questions, bug reports and suggestions are welcome.


MichaelEdited by twofingers 2014-10-15
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024