Page 1 of 1

My StringGadget2D() and TextGadget2D()

Posted: Tue Nov 09, 2010 11:53 am
by VeryVeryWell
From Russia with love! :D

My GUI-library with 2 gadgets. Without 3D!
Some procedures for working with that gadgets (FreeGadget2D(), HideGadget2D(), SetGadgetText2D, GetGadgetText2D).
In future I will add EventGadget2D() :D

File TextLibrary.pbi

Code: Select all

Structure Gadgets
  Gadget.l
  x.l
  y.l
  Width.l
  Height.l
  Text$
  BackColor.l
  TextColor.l
  Font.l
  Type.l
  Focus.l
  Hide.l
  Maxlength.l
EndStructure

Global NewList Text.Gadgets()
Global NewList String.Gadgets()

Procedure HideGadget2D(Gadget, State)
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Gadget = Gadget
      Text.Gadgets()\Hide = State
      Break
    EndIf
  Wend
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Gadget = Gadget
      String.Gadgets()\Hide = State
      Break
    EndIf
  Wend
EndProcedure
Procedure TextGadget2D(Gadget, x, y, Width, Height, Text$, BackColor, TextColor, Font = 0)
  Protected NewElement
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Gadget = Gadget
      DeleteElement(Text.Gadgets())
      NewElement = 1
      Break
    EndIf
  Wend
  If NewElement <> 1 : LastElement(Text.Gadgets()) : AddElement(Text.Gadgets()) : EndIf
  Text.Gadgets()\Gadget = Gadget
  Text.Gadgets()\x = x
  Text.Gadgets()\y = y
  Text.Gadgets()\Width = Width
  Text.Gadgets()\Height = Height
  Text.Gadgets()\Text$ = Text$
  Text.Gadgets()\BackColor = BackColor
  Text.Gadgets()\TextColor = TextColor
  Text.Gadgets()\Font = Font
  Text.Gadgets()\Type = 1
  Text.Gadgets()\Focus = 0
  Text.Gadgets()\Maxlength = -1
  Text.Gadgets()\Hide = 0
  ProcedureReturn Gadget
EndProcedure
Procedure StringGadget2D(Gadget, x, y, Width, Height, String$, BackColor, TextColor, Maxlength = -1, Font = 0)
  Protected NewElement
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Gadget = Gadget
      DeleteElement(String.Gadgets())
      NewElement = 1
      Break
    EndIf
  Wend
  If NewElement <> 1 : LastElement(String.Gadgets()) : AddElement(String.Gadgets()) : EndIf
  String.Gadgets()\Gadget = Gadget
  String.Gadgets()\x = x
  String.Gadgets()\y = y
  String.Gadgets()\Width = Width
  String.Gadgets()\Height = Height
  String.Gadgets()\Text$ = String$
  String.Gadgets()\BackColor = BackColor
  String.Gadgets()\TextColor = TextColor
  String.Gadgets()\Font = Font
  String.Gadgets()\Type = 1
  String.Gadgets()\Focus = 0
  String.Gadgets()\Maxlength = Maxlength
  String.Gadgets()\Hide = 0
  ProcedureReturn Gadget
EndProcedure
Procedure FreeGadget2D(Gadget)
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Gadget = Gadget
      DeleteElement(Text.Gadgets())
      Break
    EndIf
  Wend
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Gadget = Gadget
      DeleteElement(String.Gadgets())
      Break
    EndIf
  Wend
EndProcedure
Procedure.s GetGadgetText2D(Gadget)
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Gadget = Gadget
      ProcedureReturn Text.Gadgets()\Text$
      Break
    EndIf
  Wend
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Gadget = Gadget
      ProcedureReturn String.Gadgets()\Text$
      Break
    EndIf
  Wend
EndProcedure
Procedure SetGadgetText2D(Gadget, Text$)
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Gadget = Gadget
      Text.Gadgets()\Text$ = Text$
      Break
    EndIf
  Wend
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Gadget = Gadget
      String.Gadgets()\Text$ = Text$
      Break
    EndIf
  Wend
EndProcedure
Procedure ExamineGadget2D(MouseX, MouseY, LeftButton)
  Protected NextStringFocus.l
  ResetList(Text.Gadgets())
  While NextElement(Text.Gadgets())
    If Text.Gadgets()\Hide <> 1
      If CreateSprite(Text.Gadgets()\Gadget, Text.Gadgets()\Width, Text.Gadgets()\Height)
        If StartDrawing(SpriteOutput(Text.Gadgets()\Gadget))
          Box(0, 0, Text.Gadgets()\Width, Text.Gadgets()\Height, Text.Gadgets()\BackColor)
          If IsFont(Text.Gadgets()\Font)
            DrawingFont(FontID(Text.Gadgets()\Font))
          EndIf
          DrawText(3, 2, Text.Gadgets()\Text$, Text.Gadgets()\TextColor, Text.Gadgets()\BackColor)
          StopDrawing()
        EndIf
      EndIf
      DisplaySprite(Text.Gadgets()\Gadget, Text.Gadgets()\x, Text.Gadgets()\y)
    EndIf
  Wend
  ResetList(String.Gadgets())
  While NextElement(String.Gadgets())
    If String.Gadgets()\Hide <> 1
      If CreateSprite(String.Gadgets()\Gadget, String.Gadgets()\Width, String.Gadgets()\Height)
        If StartDrawing(SpriteOutput(String.Gadgets()\Gadget))
          Box(1, 1, String.Gadgets()\Width - 1, String.Gadgets()\Height - 1, String.Gadgets()\BackColor)
          If IsFont(String.Gadgets()\Font)
            DrawingFont(FontID(String.Gadgets()\Font))
          EndIf
          If NextStringFocus
            String.Gadgets()\Focus = 1
            NextStringFocus = 0
          EndIf
          If LeftButton
            If MouseX >= String.Gadgets()\x And MouseX <= String.Gadgets()\Width + String.Gadgets()\x
              If MouseY >= String.Gadgets()\y And MouseY <= String.Gadgets()\Height + String.Gadgets()\y
                String.Gadgets()\Focus = 1
              Else
                String.Gadgets()\Focus = 0
              EndIf
            Else
              String.Gadgets()\Focus = 0
            EndIf
          EndIf
          If String.Gadgets()\Focus = 1
            KeyboardMode(#PB_Keyboard_International)
            If KeyboardReleased(#PB_Key_Tab)
              String.Gadgets()\Focus = 0
              NextStringFocus = 1
            ElseIf KeyboardReleased(#PB_Key_Back) And Len(String.Gadgets()\Text$) > 0
              String.Gadgets()\Text$ = Left(String.Gadgets()\Text$, Len(String.Gadgets()\Text$) - 1)
            Else
              If Len(String.Gadgets()\Text$) <= String.Gadgets()\Maxlength Or String.Gadgets()\Maxlength = -1
                String.Gadgets()\Text$ + KeyboardInkey()
              EndIf
            EndIf
            DrawText(3, 2, String.Gadgets()\Text$ + "|", String.Gadgets()\TextColor, String.Gadgets()\BackColor)
          Else
            DrawText(3, 2, String.Gadgets()\Text$, String.Gadgets()\TextColor, String.Gadgets()\BackColor)
          EndIf
          LineXY(0, 0, String.Gadgets()\Width - 1, 0, RGB(133, 187, 221))
          LineXY(0, 0, 0, String.Gadgets()\Height - 1, RGB(133, 187, 221))
          LineXY(0, String.Gadgets()\Height - 1, String.Gadgets()\Width - 1, String.Gadgets()\Height - 1, RGB(133, 187, 221))
          LineXY(String.Gadgets()\Width - 1, 0, String.Gadgets()\Width - 1, String.Gadgets()\Height, RGB(133, 187, 221))
          StopDrawing()
        EndIf
      EndIf
      DisplaySprite(String.Gadgets()\Gadget, String.Gadgets()\x, String.Gadgets()\y)
    EndIf
  Wend
EndProcedure
then small example:

Code: Select all

XIncludeFile "TextLibrary.pbi"

LoadFont(1, "Tahoma", 10)

InitSprite() : InitKeyboard()
If OpenWindow(0, 0, 0, 500, 500, "TextLibrary", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  If OpenWindowedScreen(WindowID(0), 0, 0, 500, 500, 0, 0, 0)
    TextGadget2D(2, 10, 10, 200, 20, "Hello! It is 2D Gadget!", RGB(255, 255, 255), RGB(0, 0, 0), 1)
    TextGadget2D(3, 10, 40, 200, 20, "It is again 2D TextGadget!", RGB(255, 255, 255), RGB(0, 0, 0), 1)
    StringGadget2D(4, 10, 70, 200, 20, "some text for edit...", RGB(255, 255, 255), RGB(0, 0, 0), -1, 1)
  EndIf
  Repeat
    Repeat
      WindowEvent = WindowEvent()
      Select WindowEvent
        Case #PB_Event_CloseWindow
          QUIT = 1
        Case #WM_LBUTTONDOWN
          LeftButtonStatus = 1
      EndSelect
    Until WindowEvent = 0
    FlipBuffers()
    ClearScreen(RGB(255, 255, 255))
    ExamineKeyboard()
    KeyboardMode(#PB_Keyboard_International)
    ExamineGadget2D(WindowMouseX(0), WindowMouseY(0), LeftButtonStatus)
    LeftButtonStatus = 0
  Until QUIT = 1
EndIf
End