CP - Alien Breed Remake - 4

Miscellaneous Forums/Blitz Showcase/CP - Alien Breed Remake - 4

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

I've updated the InputBox to have proper character wrap, no word wrap yet.
It has key repeat for backspace (and the cursor keys) and now has a CaratPos so that you can go back and forward and insert text.

The GetCharWidth function can probably be removed now as it is obsolete (doh!).

in editor.bb:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; FUNCTION InputBox									;
; Parameters:                                       ;
; msg$ : any message to display above the input box ;
; x,y : top left coords                             ;
; width, height: outer width and height of inputbox ;
; rtnSubmit : if the return key is pressed, then    ;
;             automatically submit the text			;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function InputBox$(Msg$,x,y,width,height,rtnSubmit=True, DefaultText$="")

	SetFont Font
	
;;;;;;
;vars;
;;;;;;

	Local leave = False
	Local InnerWidth = Width - 6
	Local InnerHeight = Height - 40
	Local InnerX = 6
	Local InnerY = 20
	Local retString$ = ""
	Local fntWidth = FontWidth()/4
	Local fntHeight = FontHeight()
	Local EditWin = CreateImage(width, height)
	
;;;;;;
;main;
;;;;;;

If rtnSubmit Then InnerHeight = Height - 6
FlushKeys()
CaratPos = Len(DefaultText)
retString = retString+DefaultText

Repeat
	;quit the function without returning any text if escape is pressed
	If KeyHit(1) Then 
		leave = True
		retString$ = ""
	EndIf
	
	;;;;;;;;;;;;;;;;;;;;;
	;get character input;
	;;;;;;;;;;;;;;;;;;;;;
	get = GetKey()

	If Get = 13 Then
		If rtnSubmit Then 
			leave = True ;if return is pressed and rtnSubmit is True then leave
		Else
			retString$ = retString$ + Chr(get)
			CaratPos = CaratPos + 1
		EndIf
	ElseIf KeyDown(14); backspace
		If MilliSecs()-LastInput > KeyRepeat Then
			If Len(retString$) > 0 Then retString$ = Left$(retString$, Len(retString$) -1)		
			LastInput = MilliSecs()
			CaratPos = CaratPos - 1
			If CaratPos < 0 Then CaratPos = 0
		EndIf
	ElseIf KeyDown(203) ; Left Cursor
		If MilliSecs()-LastInput > KeyRepeat Then
			CaratPos = CaratPos - 1
			If CaratPos < 0 Then CaratPos = 0
			LastInput = MilliSecs()
			;DebugLog CaratPos
		EndIf
	ElseIf KeyDown(205) ; Right Cursor
		If MilliSecs()-LastInput > KeyRepeat Then
			CaratPos = CaratPos + 1
			If CaratPos > Len(retString$) Then CaratPos = Len(retString$)
			LastInput = MilliSecs()
			;DebugLog CaratPos
		EndIf
	ElseIf (get > 31) ;is a letter, number or symbol
		LeftSide$ = Left$(retString$,CaratPos)
		RightSide$ = Right$(retString$, Len(RetString)-CaratPos)
		retString$ = LeftSide$ + Chr(get) + RightSide$;retString$ + Chr(get)
		CaratPos = CaratPos + 1
		;DebugLog CaratPos
	EndIf

		

		oldbuffer = GraphicsBuffer()
		SetBuffer ImageBuffer(EditWin)

			Color 160,160,160
			Rect 0,0,width,height

			Color 255,255,255
			Rect 2, 17, InnerWidth , InnerHeight

			Color 0,0,0
			
			Text width/2,8,msg,True,True
			

			;Draw Edit Content
			Local WCount% = 0
			Local LineCount% = 0
			Local CharCount% = 0
			Local tempString$ = ""
			Local tempString2$ = ""
			Local StringLength% = Len(retString$)
			Local CurrentChar$ = ""
			Local LineLength% = 0

			While CharCount < StringLength
				Repeat
					CharCount = CharCount + 1
					Currentchar$ = Mid$(retString$, CharCount,1)	
					If Charcount = CaratPos+1 Then tempString$ = tempString$ + CaratSymbol$
					tempString$ = tempString$ + CurrentChar$
					
					;DebugLog "CharCount: " + CharCount
					;DebugLog "CaratPos: " + CaraPos + "  " + CaratSymbol
					
					charWidth = CharWidths(Asc(CurrentChar))
					WCount = WCount + charWidth
				Until (WCount > InnerWidth - 8) Or (CharCount = StringLength) Or (Asc(CurrentChar) = 13)
				

				If Right$(tempString,1) = Chr(13) Then 
					tempString$ = Left(TempString$,Len(TempString$)-1)
				EndIf
				
				LineLength = LineLength + Len(tempString$)
				
				Color 0,0,0
				If (CaratPos = StringLength) And (CaratPos = CharCount) Then tempString = tempString + CaratSymbol
				Text InnerX,InnerY + (LineCount * fntHeight),tempString$,False,False
				

				LineCount = LineCount + 1

				tempString$  = ""
				tempString2$ = ""

				WCount = 0

			Wend
		
		SetBuffer OldBuffer
	
	DrawImage EditWin,x,y	
	
	If Not rtnSubmit Then
		If Button(x+(Width/2)-30,y+Height-20,60,"OK") Then leave = True
	EndIf
	
	Flip

Until leave = True

	FlushKeys()
	FlushMouse()
	FreeImage EditWin
	
	Return retString$


End Function


add the following after SetFont font:
;INPUT BOX SETUP
;Get the character widths
Dim charwidths(512)
	charwidths(32) = 4
	For c = 33 To 512
		charwidths(c) = StringWidth(Chr$(c))
	Next

Const KeyRepeat% = 100
Const CaratSymbol$ = "¦"

Global SelStart% = 0 
Global SelEnd% = 0
Global CaratPos% = 0