diff --git a/include/ui.bm b/include/ui.bm index 3116103..d7d61d4 100644 --- a/include/ui.bm +++ b/include/ui.bm @@ -1,136 +1,128 @@ -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 +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 + if not (_mousex > x and _mousey > y AND _mousex < x + w AND _mousey < y + h) then drawtextinput x, y, w, h, __text, 0 - EXIT FUNCTION - END IF + exit function + end if drawtextinput x, y, w, h, __text, 1 - IF NOT mouseclicked THEN EXIT FUNCTION + if not mouseclicked then exit function text="" - cursor = LEN(text) + 1 - dim relativeX AS INTEGER - DO - keyin = INKEY$ + cursor = len(text) + 1 + dim relativeX as integer + do + keyin = inkey$ ' Handle Extended Keys (Arrows, Home, End, Delete) - IF LEN(keyin) = 2 THEN - SELECT CASE ASC(RIGHT$(keyin, 1)) - CASE 75 ' Left Arrow - IF cursor > 1 THEN cursor = cursor - 1 - CASE 77 ' Right Arrow - IF cursor <= LEN(text) THEN cursor = cursor + 1 - CASE 71 ' Home Key + if len(keyin) = 2 then + select case asc(right$(keyin, 1)) + case 75 ' Left Arrow + if cursor > 1 then cursor = cursor - 1 + case 77 ' Right Arrow + if cursor <= len(text) then cursor = cursor + 1 + case 71 ' Home Key cursor = 1 - CASE 79 ' End Key - cursor = LEN(text) + 1 - CASE 83 ' Delete Key - IF cursor <= LEN(text) THEN - text = LEFT$(text, cursor - 1) + MID$(text, cursor + 1) - END IF - END SELECT - 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) - CASE 32 TO 126 ' Regular Typing - text = LEFT$(text, cursor - 1) + keyin + MID$(text, cursor) + case 79 ' End Key + cursor = len(text) + 1 + case 83 ' Delete Key + if cursor <= len(text) then + text = left$(text, cursor - 1) + mid$(text, cursor + 1) + end if + end select + 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) + case 32 to 126 ' Regular Typing + 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) + case 8 ' Backspace + if cursor > 1 THEN + text = left$(text, cursor - 2) + mid$(text, cursor) cursor = cursor - 1 - END IF - CASE 13 ' Enter + end if + case 13 ' Enter done = -1 - CASE 27 ' Escape (Cancel) + case 27 ' Escape (Cancel) text = __text done = -1 - END SELECT - END IF + 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 _MOUSEX <= x + w AND _MOUSEY >= y AND _MOUSEY <= y + h) THEN - relativeX = _MOUSEX - x + 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 + 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 - _LIMIT 60 - _DISPLAY - LOOP UNTIL done + end if + end if + _limit 60 + _display + loop until done textinput = text -END FUNCTION +end function -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 ' Standard font width - DIM cursorX AS INTEGER, textX AS INTEGER, textY 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 - ' Background Drawing - IF state > 0 THEN ' If state is cursor pos, it's "active" - COLOR backgroundcolor1 + if state > 0 then + color backgroundcolor1 + else + color backgroundcolor2 + end if + line (x, y)-(x + w, y + h), , bf + + if state > 0 then + color highlightcolor ELSE - COLOR backgroundcolor2 - END IF - LINE (x, y)-(x + w, y + h), , BF - - ' Border Drawing - IF state > 0 THEN - COLOR highlightcolor - ELSE - COLOR textcolor - END IF - LINE (x, y)-(x + w, y + h), , B - - ' Text Alignment Calculation - _PRINTMODE _KEEPBACKGROUND - ' We keep your original logic for scrolling/clipping text - outtext = RIGHT$(text, MIN(w / charWidth, LEN(text))) - - ' Calculate the starting X/Y for the text to center it - textX = 2 + x' + w / 2 - LEN(outtext) * charWidth / 2 + color textcolor + end if + 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 - + _printstring (textX, textY), outtext ' --- Cursor Logic --- - IF state > 0 THEN + 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 + 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 - DIM relativeCursor AS INTEGER - relativeCursor = state - (LEN(text) - LEN(outtext)) + 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 + 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 - END IF - END IF - END IF -END SUB + line (cursorX, textY)-(cursorX, textY + 14), highlightcolor + end if + end if + end if +end sub function min(a,b) if a<=b then min=a else min=b end function + function button (x as integer,y as integer,w as integer,h as integer,caption as string) if _mousex>x and _mousey>y and _mousex