Einfache Texteingabebox Graphics-Modus

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Mathias-Kwiatkowski

Betreff: Einfache Texteingabebox Graphics-Modus

BeitragSa, Jan 12, 2013 18:46
Antworten mit Zitat
Benutzer-Profile anzeigen
Komplettes BSP.

mit:
Code: [AUSKLAPPEN]
Global Eingabe:TTextField = ttextfield.Create(50, 50, 100, 20)

ein Textfeld erstellen...

Code: [AUSKLAPPEN]
Eingabe.Draw (Activ%,X%,Y%)

Textfeld malen und Schreiben
Activ% = 0 nicht Aktiv 1 Activ
x% = X + die erstellungs X Achse
y% = Y + die erstellungs Y Achse

Code: [AUSKLAPPEN]
Meintext$=Eingabe.GetText()

nachschaun was im textfeld steht.-




Code: [AUSKLAPPEN]
Global TextField:TTextfield
Type TTextField
   Field Eingabe:Int, Activ:Int, X:Int, Y:Int, W:Int, H:Int, Text:String, Pos:Int, Minus:Int, Timer:Int
   
   Function Create:TTextField (X:Int, Y:Int, W:Int, H:Int)
      TextField:TTextField = New TTextField
      TextField.X = X
      TextField.Y = Y
      TextField.W = W
      TextField.H = H
      TextField.Timer = MilliSecs()
      
      Return TextField
   End Function
   
   Method Draw(Active:Int, x1:Int, y1:Int)
      If Active = 0 Then Self.Activ = 0
      TmpX:Int = x1 + Self.X
      TmpY:Int = y1 + Self.Y
      TmpW:Int = Self.W
      TmpH:Int = Self.H
      SetColor 255, 255, 255
      DrawRect TmpX + 1, TmpY + 1, TmpW - 1, TmpH - 1
      SetColor 0, 0, 0
      If Self.Activ = 1 Then SetColor 0, 255, 0
      DrawLine TmpX, Tmpy, Tmpx + TmpW, Tmpy
      DrawLine TmpX, Tmpy + TmpH, Tmpx + TmpW, Tmpy + TmpH
      DrawLine TmpX, Tmpy, Tmpx, Tmpy + TmpH
      DrawLine TmpX + TmpW, Tmpy, Tmpx + TmpW, Tmpy + TmpH
      SetColor 0, 0, 0
      SetViewport tmpx + 1, tmpy + 1, tmpw - 1, tmph - 1

      
      DrawText Self.Text, tmpx + 1 - Self.Minus, tmpy + tmph / 2 - TextHeight("W") / 2
      If Active > 0 Then
         If Self.Eingabe = 1 Then
            SetColor 255, 0, 0
            If Self.Pos < Len(Self.Text) Then
               Weite:Int = TextWidth(Mid(Self.Text, Self.Pos, 1))
               SetBlend ALPHABLEND
               SetAlpha 0.3
               DrawRect tmpx + 1 + TextWidth(Mid(Self.Text, 1, Self.Pos)) - Self.Minus, tmpy + tmph / 2 - TextHeight("W") / 2, Weite, TextHeight("W")
               SetAlpha 1
               DrawText "|", tmpx + 1 - 2 + TextWidth(Mid(Self.Text, 1, Self.Pos)) - Self.Minus, tmpy + tmph / 2 - TextHeight("W") / 2
            Else
               DrawText "|", tmpx + 1 - 2 + TextWidth(Mid(Self.Text, 1, Self.Pos)) - Self.Minus, tmpy + tmph / 2 - TextHeight("W") / 2
            End If
         End If
         SetViewport 0, 0, GraphicsWidth(), GraphicsHeight()
         
         If MouseX() > Tmpx And MouseX() < Tmpx + Tmpw And MouseY() > Tmpy And MouseY() < Tmpy + Tmph Then
            Self.Activ = 1
            If MouseDown(1) Then Self.Eingabe = 1
         Else
            Self.Activ = 0
            If MouseDown(1) Then Self.Eingabe = 0
         EndIf
         
         If Self.Eingabe = 1 Then
            a = GetChar()
            If a <> 0 Then
               If a = 8 Then
                  If Self.Pos > 0 Then
                     Weite:Int = TextWidth(Mid(Self.Text, Len(Self.Text) - 1, 1))
                     text1:String = Mid(Self.Text, 1, Self.Pos - 1)
                     text3:String = Mid(Self.Text, Self.Pos + 1, Len(Self.Text) - Self.Pos)
                     Self.Pos = Self.Pos - 1
                     Self.Text = text1 + text3
                     If Self.Minus > 0 Then Self.Minus = Self.Minus - Weite
                     If Self.Pos < 0 Then Self.Pos = 0
                  EndIf
               Else
                  text1:String = Mid(Self.Text, 1, Self.Pos)
                  text2:String = Chr(a)
                  text3:String = Mid(Self.Text, Self.Pos + 1, Len(Self.Text) - Self.Pos)
                  
                  Self.text = text1 + text2 + text3
                  Self.Pos = Self.Pos + 1
                  If TextWidth(Mid(Self.Text, 1, Self.Pos)) > Self.W - 5 Then Self.Minus = Self.Minus + TextWidth(Chr(a))
               EndIf
            End If
            
            If (MilliSecs() - Self.Timer) > 69 Then
               If KeyDown(KEY_LEFT) Then
                  Self.Pos = Self.Pos - 1
                  If Self.Pos > 0 Then
                     Weite:Int = TextWidth(Mid(Self.Text, Self.Pos - 1, 1))
                     If Self.Minus > 0 Then Self.Minus = Self.Minus - Weite
                  End If
                  If Self.Pos < 0 Then Self.Pos = 0
               End If
               If KeyDown(KEY_RIGHT) Then
                  If Self.Pos < Len(Self.Text) Then
                     Weite:Int = TextWidth(Mid(Self.Text, Self.Pos, 1))
                     If Self.Minus > 0 Self.Minus = Self.Minus + weite
                     Self.Pos = Self.Pos + 1
                     If Self.Pos > Len(Self.Text) Then Self.Pos = Len(Self.Text)
                  End If
               End If
               If KeyDown(KEY_DELETE) Then
                  text1:String = Mid(Self.Text, 1, Self.Pos)
                  text3:String = Mid(Self.Text, Self.Pos + 2, Len(Self.Text) - Self.Pos - 1)
                  Self.Text = text1 + text3
               End If
               Self.Timer = MilliSecs()
            EndIf
         End If
      EndIf
   End Method
   
   Method GetText:String()
      Return Self.Text
   End Method
End Type

Global Eingabe:TTextField = ttextfield.Create(50, 50, 100, 20)








Graphics 640, 480

Repeat
   Cls
   SetColor 192, 192, 192
   DrawRect 0, 0, 640, 480
   Eingabe.Draw (1, 0, 0)
   
   SetColor 0, 0, 0
   DrawText "Eingabe:" + Eingabe.GetText(), 50, 100
   Flip
Until KeyDown(KEY_ESCAPE)
End

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group