Probleme mit Eingaberoutine

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

Hubsi

Betreff: Probleme mit Eingaberoutine

BeitragSa, Dez 26, 2009 18:49
Antworten mit Zitat
Benutzer-Profile anzeigen
Grüße beisammen.

Ich habe mit unten stehendem Code ein kleines Problem. Ich suche eine Lösung dafür Text in einer Eingaberoutine mit der Maus zu markieren (wie man es eben von Windows kennt). Konkretes Problem ist jetzt das ich das erste Zeichen im String nicht markieren kann. Ich eier da jetzt schon eine ganze Weile rum und weiß nicht mehr ob es schon Betriebsblindheit ist oder der Fehler wirklich so versteckt ist. Zum antesten und zwecks der Übersicht ist die Funktion in einem kleinen hingeklatschten Versuchsprogramm untergebracht. Der Code drumherum soll also nicht Gegenstand der Diskussion sein Very Happy Vielleicht kann mir ja jemand die Augen öffnen.

BlitzBasic: [AUSKLAPPEN]
Graphics 640,480,32,1
Restore IMAGE_DATAS_mouse_bmp
Read imgW%
Read imgH%
img=CreateImage(imgW,imgH)
SetBuffer ImageBuffer(img)
LockBuffer ImageBuffer(img)
For x=0 To imgW-1
For y=0 To imgH-1
Read rgb%
WritePixelFast x,y,rgb%
Next
Next
UnlockBuffer ImageBuffer(img)
SetBuffer BackBuffer()
MaskImage img,255,0,255

Global input_mark_x=-1,input_mark_w,input_cursor_pos,input_timer,msecs

font=LoadFont("Comic Sans MS",24,1)

t$="ABCdefGHIjklMNOpqrSTUvwxYZ1234567890" ; Teststring


Repeat
If KeyHit(1) End
Cls
SetFont font
msecs=MilliSecs()
t$=Input$(t$,20,20,0,0,1)
Color 120,255,0
Text 20,20,t$
Text 20,70,input_cursor_pos

DrawImage img,MouseX(),MouseY()

Flip
Forever

Function Input$(t$,x,y,size_x,size_y,cursor=False,mark_color=128,maxlength=999999,Lengthtype=0)
; Global input_mark_x,input_mark_w,input_cursor_pos,input_timer
Input_key=GetKey()
If KeyHit(82) input_key=48
If KeyHit(79) input_key=49
If KeyHit(80) input_key=50
If KeyHit(81) input_key=51
If KeyHit(75) input_key=52
If KeyHit(76) input_key=53
If KeyHit(77) input_key=54
If KeyHit(71) input_key=55
If KeyHit(72) input_key=56
If KeyHit(73) input_key=57
If KeyHit(181) input_key=47
If KeyHit(55) input_key=42
If KeyHit(74) input_key=45
If KeyHit(78) input_key=43
If KeyHit(83) input_key=44
If KeyHit(156) input_key=13
If KeyDown(203) And input_cursor_pos>0 And msecs>Input_timer+100
input_cursor_pos=input_cursor_pos-1
Input_timer=msecs
Input_key=0
EndIf
If KeyDown(205) And input_cursor_pos<Len(t$) And msecs>Input_timer+100
input_cursor_pos=input_cursor_pos+1
Input_timer=msecs
Input_key=0
EndIf
If KeyDown(211) And msecs>input_timer+100
Input_timer=msecs
Input_key=0
If Input_mark_x>-1
t$=Left$(t$,Input_mark_x)+Mid$(t$,Input_mark_w+input_mark_x+1)
Else
t$=Left$(t$,input_cursor_pos)+Mid$(t$,input_cursor_pos+2)
EndIf
Input_mark_x=-1
EndIf
If KeyDown(14) And Input_cursor_pos>0 And msecs>Input_timer+100
Input_timer=msecs
Input_key=0
If Input_mark_x>-1
t$=Left$(t$,Input_mark_x)+Mid$(t$,Input_mark_w+input_mark_x+1)
Else
t$=Left$(t$,input_cursor_pos-1)+Mid$(t$,input_cursor_pos+1)
input_cursor_pos=input_cursor_pos-1
EndIf
Input_mark_x=-1
EndIf
If (KeyDown(29) Or KeyDown(157)) And KeyHit(47)
input_key=0
tmp$=ReadClipboardText$()
t$=Left$(t$,input_cursor_pos)+tmp$+Mid$(t$,input_cursor_pos+1)
input_cursor_pos=input_cursor_pos+Len(tmp$)
EndIf
If KeyHit(207) input_cursor_pos=Len(t$):Input_key=0
If KeyHit(199) input_cursor_pos=0:Input_key=0

; Text markieren
If MouseHit(1)
Input_mark_w=0
Input_mark_x=-1
For i=1 To Len(t$)
If StringWidth(Left$(t$,i))+x>=MouseX() Input_mark_x=i:Exit
Next
Input_cursor_pos=i
EndIf
If MouseDown(1) And Input_mark_x>-1
If MouseX()>=x And MouseX()<=StringWidth(t$)+x
If MouseY()>=y And MouseY()<=FontHeight()+y
; Maus in Textbereich
For i=1 To Len(t$)
If MouseX()<StringWidth(Left$(t$,i))+x Input_mark_w=Len(Mid$(t$,Input_mark_x,i-Input_mark_x)):Exit
Next
EndIf
EndIf
EndIf

If Input_key>=32 ; Eingabe getätigt
t$=Left$(t$,input_cursor_pos)+Chr$(Input_key)+Mid$(t$,input_cursor_pos+1)
input_cursor_pos=input_cursor_pos+1
EndIf


If Len(t$)>maxlength t$=Left$(t$,maxlength)
If cursor
If einf c_width=FontWidth() Else c_width=2
Rect x+StringWidth(Left$(t$,input_cursor_pos)),y-1,c_width,FontHeight()+2,1
EndIf
If Input_mark_x>-1
Color mark_color,mark_color,mark_color
Rect StringWidth(Left$(t$,Input_mark_x))+x,y,StringWidth(Mid$(t$,Len(Left$(t$,Input_mark_x)+1),Input_mark_w)),FontHeight(),1
EndIf
Return t$
End Function

Function ReadClipboardText$()
Local cb_TEXT=1
Local txt$=""
OpenClipboard 0
If ExamineClipboard(cb_TEXT)
txt$=GetClipboardData$(cb_TEXT)
EndIf
CloseClipboard
Return txt$
End Function



;mouse.bmp (11x19)
.IMAGE_DATAS_mouse_bmp
Data 11
Data 19
Data $181C73, $7375BD, $7375BD, $7375BD, $7375BD, $7375BD, $7375BD, $7375BD
Data $7375BD, $7375BD, $7375BD, $7375BD, $7375BD, $7375BD, $181C73, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $42517B, $7375BD, $84B6EF
Data $B5EBEF, $B5EBEF, $B5EBEF, $B5EBEF, $B5EBEF, $B5EBEF, $B5EBEF, $7375BD
Data $181C73, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $181C73, $42517B, $7375BD, $7375BD, $84B6EF, $84B6EF, $84B6EF, $84B6EF
Data $84B6EF, $7375BD, $181C73, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $42517B, $4259DE, $7375BD
Data $7375BD, $84B6EF, $84B6EF, $7375BD, $181C73, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $181C73, $42517B, $4259DE, $4259DE, $7375BD, $7375BD, $181C73, $7375BD
Data $7375BD, $7375BD, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $42517B, $4259DE, $4259DE
Data $4259DE, $181C73, $4259DE, $84B6EF, $84B6EF, $7375BD, $7375BD, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $181C73, $42517B, $4259DE, $4259DE, $181C73, $181C73, $4259DE, $4259DE
Data $84B6EF, $84B6EF, $7375BD, $7375BD, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $42517B, $4259DE, $181C73
Data $FF1CFF, $181C73, $181C73, $4259DE, $4259DE, $84B6EF, $84B6EF, $181C73
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $181C73, $42517B, $181C73, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $181C73
Data $4259DE, $4259DE, $181C73, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $181C73, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $181C73, $181C73, $181C73, $FF1CFF, $FF1CFF
Data $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $181C73, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF, $FF1CFF
Data $FF1CFF
Den ganzen Doag im Bett umanandflagga und iaz daherkema und meine Hendl`n fressn...

Midimaster

BeitragSa, Dez 26, 2009 21:26
Antworten mit Zitat
Benutzer-Profile anzeigen
wenn bis jetzt noch keiner geantwortet hat, könnte das an Deinem Spaghetti-Code liegen. Da wird es ehrlich hart, sich hier durchzuarbeiten.

Köntest Du nicht die für die "Markierung" relevanten Teile in eine eigene Funktion ausgliedern? (Da ist auch noch mittendrin in dem "Markierungs"-Teil eine "If Input_key>=32")

Wenn Du das "Markieren" ausgegliedert hast, kannst Du es mit beliebigen Strings testen oder dies wieder hier einstellen

TimBo

BeitragSa, Dez 26, 2009 21:59
Antworten mit Zitat
Benutzer-Profile anzeigen
noch als Tipp:

schau dir mal die ScanCodes für die Buchstaben an.

Und Benutze Chr$ (chr das ist eine Funktion, die einen Buchstaben zurück gibt. Daher ist dies eine StringFunktion (erkennt man am $)

Grüße
TimBo
mfg Tim Borowski // CPU: Ryzen 2700x GPU: Nvidia RTX 2070 OC (Gigabyte) Ram: 16GB DDR4 @ 3000MHz OS: Windows 10
Stolzer Gewinner des BCC 25 & BCC 31
hat einen ersten Preis in der 1. Runde beim BWInf 2010/2011 & 2011/12 mit BlitzBasic erreicht.

Midimaster

BeitragSa, Dez 26, 2009 23:26
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich hab den Fehler gefunden:

Deine unlesbare Zeile...

BlitzBasic: [AUSKLAPPEN]
If StringWidth(Left$(t$,i))+x>=MouseX() Input_mark_x=i:Exit


hab ich mal in diese umgewandelt...

BlitzBasic: [AUSKLAPPEN]
If StringWidth(Left$(t$,i))+x>=MouseX() Then
Input_mark_x=i
Exit
EndIf


und jetzt sieht man auch den Fehler. Es muss heißen:
BlitzBasic: [AUSKLAPPEN]
Input_mark_x=i-1


Da Du ja erst dann abbrichst, wenn der String zu lang wurde, muss i wieder um 1 reduziert werden, da ja sonst erst NACH dem (i)-ten Zeichen der DrawRect anfängt, aber Du willst ja, dass er schon vor diesem, also nach dem (i-1)-ten Zeichen beginnt


Mann, mach mal dieser "3 Befehle auf einer Zeile"-Ungetüme raus!


Ich habe mal den Marker-Teil in eine Funktion gepackt (ohne viel von Deinem Code zu ändern) Den Cursor-Zeichner und die Input>32 habe ich rausgenommen. Die bleiben im Hauptprogramm

BlitzBasic: [AUSKLAPPEN]
Function TextMarkieren(t$)
Local i%

If MouseHit(1)
; erster Marker-Pos festlegen
Input_mark_w=0
Input_mark_x=-1
For i=1 To Len(t$)
If StringWidth(Left$(t$,i))+x>=MouseX() Then
Input_mark_x=i-1
Exit
EndIf
Next
Input_cursor_pos=i
EndIf

If MouseDown(1) And Input_mark_x>-1
; Markerbereich noch variabel
If MouseX()>=x And MouseX()<=StringWidth(t$)+x
If MouseY()>=y And MouseY()<=FontHeight()+y
; Maus in Textbereich
For i=1 To Len(t$)
If MouseX()<StringWidth(Left$(t$,i))+x
Input_mark_w=Len(Mid$(t$,Input_mark_x,i-Input_mark_x))
Exit
EndIf
Next
EndIf
EndIf
EndIf

If Input_mark_x>-1
;Marker-Bereich zeichnen
Color mark_color,mark_color,mark_color
Von%= StringWidth(Left$(t$,Input_mark_x))
Bis%=StringWidth(Mid$(t$,Len(Left$(t$,Input_mark_x)+1),Input_mark_w))
Rect x+Von, y, Bis,FontHeight(),1
EndIf

End Function

Hubsi

BeitragSo, Dez 27, 2009 9:45
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke erstmal für die Antworten Ihr beiden. Ich war gestern Abend aus, von daher die realtiv späte Reaktion Very Happy

TimBo, ich glaube ich verstehe nicht was Du mir damit sagen willst:

TimBo hat Folgendes geschrieben:
noch als Tipp:

schau dir mal die ScanCodes für die Buchstaben an.



Midimaster, Dir besonderen Dank. Deine gepostete Version funktioniert zwar so nicht, aber "Augen öffnend" war es und das haut mich am meisten nach vorn Mr. Green
Den ganzen Doag im Bett umanandflagga und iaz daherkema und meine Hendl`n fressn...

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group