diff --git a/include/ui.bm b/include/ui.bm index d7d61d4..be4ddc7 100644 --- a/include/ui.bm +++ b/include/ui.bm @@ -1,119 +1,99 @@ -function textinput$ (x as integer, y as integer, w as integer, h as integer, __text as string) +function textinput$ (x as integer,y as integer,w as integer,h as integer,__text as string) dim text as string, keyin as string dim cursor as integer, done as integer - text = __text - ' Initial Hover Check - if not (_mousex > x and _mousey > y AND _mousex < x + w AND _mousey < y + h) then - drawtextinput x, y, w, h, __text, 0 + text=__text + if not (_mousex>x and _mousey>y AND _mousex 1 then cursor = cursor - 1 + if cursor>1 then cursor=cursor-1 case 77 ' Right Arrow - if cursor <= len(text) then cursor = cursor + 1 + if cursor<=len(text) then cursor=cursor+1 case 71 ' Home Key - cursor = 1 + cursor=1 case 79 ' End Key - cursor = len(text) + 1 + cursor=len(text)+1 case 83 ' Delete Key - if cursor <= len(text) then - text = left$(text, cursor - 1) + mid$(text, cursor + 1) + if cursor<=len(text) then + text=left$(text,cursor-1)+mid$(text,cursor+1) end if end select - elseif LEN(keyin) = 1 then + elseif LEN(keyin)=1 then select case asc(keyin) case 22 ' Ctrl + V (Paste) - text = left$(text, cursor - 1) + _clipboard$ + mid$(text, cursor) - cursor = cursor + len(pasteData) + text=left$(text,cursor-1)+_clipboard$+mid$(text,cursor) + cursor=cursor+len(pasteData) case 32 to 126 ' Regular Typing - text = left$(text, cursor - 1) + keyin + mid$(text, cursor) - cursor = cursor + 1 + text=left$(text,cursor-1)+keyin+mid$(text,cursor) + cursor=cursor+1 case 8 ' Backspace - if cursor > 1 THEN - text = left$(text, cursor - 2) + mid$(text, cursor) - cursor = cursor - 1 + if cursor>1 THEN + text=left$(text,cursor-2)+mid$(text,cursor) + cursor=cursor-1 end if case 13 ' Enter - done = -1 + done=-1 case 27 ' Escape (Cancel) - text = __text - done = -1 + text=__text + done=-1 end select end if - - ' Send the cursor position to your drawing routine - drawtextinput x, y, w, h, text, cursor - - ' Check for clicks inside the box to reposition cursor - while _mouseinput: wend - if _mousebutton(1) then - IF (_mousex > x and _mousey > y AND _mousex < x + w AND _mousey < y + h) THEN - relativeX = _mousex - x - cursor = (relativeX \ 8) + 1 - - ' Constrain cursor to text boundaries - if cursor < 1 then cursor = 1 - if cursor > len(text) + 1 then cursor = len(text) + 1 - else - done = -1 ' Clicked outside, exit focus - end if - end if + while _mouseinput: wend + if _mousebutton(1) then + IF (_mousex>x and _mousey>y AND _mousexlen(text)+1 then cursor=len(text)+1 + else + done=-1 ' Clicked outside, exit focus + end if + end if + drawtextinput x,y,w,h,text,cursor _limit 60 _display loop until done - textinput = text + textinput=text end function -sub drawtextinput (x as integer, y as integer, w as integer, h as integer, text as string, state as integer) +sub drawtextinput (x as integer,y as integer,w as integer,h as integer,text as string,state as integer) dim outtext as string - dim charWidth as integer: charWidth = 8 - dim cursorX as integer, textX as integer, textY as integer - - if state > 0 then + dim charWidth as integer:charWidth = 8 + dim cursorX as integer,textX as integer,textY as integer + if state>0 then color backgroundcolor1 else color backgroundcolor2 end if - line (x, y)-(x + w, y + h), , bf - - if state > 0 then + line(x,y)-(x+w,y+h),,bf + if state>0 then color highlightcolor ELSE color textcolor end if - line (x, y)-(x + w, y + h), , B + line(x,y)-(x+w,y+h),,b _printmode _keepbackground - outtext = right$(text, min(w / charWidth, len(text))) - textX = 2 + x - textY = 1 + y + h / 2 - 8 - _printstring (textX, textY), outtext - ' --- Cursor Logic --- - if state > 0 then - ' Calculate cursor position relative to the visible text - ' We use (TIMER * 2) to make it blink - if int(timer * 2) mod 2 = 0 then - ' state is the cursor index (1-based) - ' We need to find where that character is relative to our clipped outtext + outtext=right$(text,min(w/charWidth,len(text))) + textX=2+x + textY=1+y+h/2-8 + _printstring(textX,textY),outtext + if state>0 then + if int(timer*2) mod 2=0 then dim relativeCursor as integer - relativeCursor = state - (len(text) - len(outtext)) - - ' Only draw if the cursor is actually within the visible box - if relativeCursor >= 1 and relativeCursor <= len(outtext) + 1 then - cursorX = textX + (relativeCursor - 1) * charWidth - ' Draw a vertical line for the cursor - line (cursorX, textY)-(cursorX, textY + 14), highlightcolor + relativeCursor=state-(len(text)-len(outtext)) + if relativeCursor>=1 and relativeCursor<=len(outtext)+1 then + cursorX=textX+(relativeCursor-1)*charWidth + line(cursorX,textY)-(cursorX,textY+14),highlightcolor end if end if end if