SimpleButton

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Trust

Betreff: SimpleButton

BeitragSa, Okt 13, 2012 16:28
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo Miteinander!

Manchmal gibt es Situationen, in denen man keine komplette Gui-Lib braucht, sondern einfach nur ein paar Buttons.
Wie ich zur Zeit in meinem Projekt.
Also habe ich einen kleinen Buttontype geschrieben und dachte mir, vielleicht können es ja welche gebrauchen.

Die Buttongrösse passt sich automatisch am Buttontext an.
Jeder Button kann unsichtbar/sichtbar gemacht werden und/oder aktiviert/deaktiviert werden.


Hier der Type um einen einfachen Button im Grafikmodus zu erstellen.

Der Button-Type "TButton"
BlitzMax: [AUSKLAPPEN]



Type TButton


Field _x:Int, _y:Int
Field _width:Int, _height:Int
Field _label:String

Field _visible:Byte
Field _active:Byte

Function Create:TButton(label:String, x:Int, y:Int, active:Byte = True, visible:Byte = True)
Local b:TButton = New TButton
b._label = label
b._x = x
b._y = y
b._visible = visible
b._active = active
b._width = TextWidth(b._label) + 10
b._height = TextHeight(b._label)
Return b
End Function

Method IsPressed:Byte(mouseButton:Int)
If Self._visible = True And Self._active = True
If IsMouseOver() = True
If mouseButton = True
FlushMouse()
Return True
End If
End If
End If
End Method


Method Draw()
If Self._visible = True
If Self._active = True
If IsMouseOver() = True
SetColor( 255, 255, 255)
DrawRect(Self._x, Self._y, Self._width+1, Self._height+1)
SetColor( 140, 140, 140)
DrawRect(Self._x-1, Self._y-1, Self._width+1, Self._height+1)

SetColor( 170, 170, 170)
DrawRect(Self._x, Self._y, Self._width, Self._height)
SetColor(0, 0, 0)
DrawText(Self._label, Self._x + 5, Self._y+1)
Else
SetColor( 140, 140, 140)
DrawRect(Self._x, Self._y, Self._width+1, Self._height+1)
SetColor( 255, 255, 255)
DrawRect(Self._x-1, Self._y-1, Self._width+1, Self._height+1)

SetColor( 180, 180, 180)
DrawRect(Self._x, Self._y, Self._width, Self._height)
SetColor(10, 10, 10)
DrawText(Self._label, Self._x + 6, Self._y+2)
End If
Else
SetColor( 140, 140, 140)
DrawRect(Self._x, Self._y, Self._width+1, Self._height+1)
SetColor( 255, 255, 255)
DrawRect(Self._x-1, Self._y-1, Self._width+1, Self._height+1)

SetColor( 200, 200, 200)
DrawRect(Self._x, Self._y, Self._width, Self._height)
SetColor(160, 160, 160)
DrawText(Self._label, Self._x + 6, Self._y+2)
End If
End If
SetColor(255, 255, 255)
End Method


Method IsMouseOver:Byte()
If MouseX() >= Self._x And MouseX() <= Self._x + Self._width
If MouseY() >= Self._y And MouseY() <= Self._y + Self._height
Return True
End If
End If
End Method

Method IsActive:Int()
If Self._active Then Return True
End Method

Method IsVisible:Int()
If Self._visible Then Return True
End Method

Method SetActive()
Self._visible = True
Self._active = True
End Method

Method SetVisible()
Self._visible = True
Self._active = True
End Method

Method SetInactive()
Self._active = False
End Method

Method SetInvisible()
Self._visible = False
Self._active = False
End Method

Method SetLabel(str:String)
Self._label = str
Self._width = TextWidth(Self._label) + 10
Self._height = TextHeight(Self._label)
End Method

End Type






Und hier ein kleines Beispiel:
BlitzMax: [AUSKLAPPEN]

SuperStrict

AppTitle = "SimpleButton"

' SimpleButton includieren
Include "TButton.bmx"



Graphics 320, 240
SetClsColor 220, 220, 220

' Buttons erstellen
'TButton.Create("ButtonText", x, y)
Local btn_1:TButton = TButton.Create("Button 1", 100, 50)
Local btn_2:TButton = TButton.Create("Button 2", 100, 80)

Local btn_SetActive:TButton = TButton.Create("Button1 is active", 100, 110)
Local btn_SetVisible:TButton = TButton.Create("Button2 is visible", 100, 140)

' Variable für die linke Maustaste
Local mh1:Int

Repeat
Cls
' Maustastendruck speichern
mh1 = MouseHit(1)


' Buttons Zeichnen
btn_1.Draw()
btn_2.Draw()
btn_SetActive.Draw()
btn_SetVisible.Draw()


' Buttons auf interaktion mit der linken Maustaste (mh1) prüfen und aktionen ausführen
' .IsPressed(maustaste)
If btn_1.IsPressed(mh1) Then Notify "Button 1 pressed!"
If btn_2.IsPressed(mh1) Then Notify "Button 2 pressed!"

If btn_SetActive.IsPressed(mh1)
If btn_1.IsActive()
btn_1.SetInactive()
btn_SetActive.SetLabel("Button 1 is inactive")
Else
btn_1.SetActive()
btn_SetActive.SetLabel("Button 1 is active")
End If
End If

If btn_SetVisible.IsPressed(mh1)
If btn_2.IsVisible()
btn_2.SetInvisible()
btn_SetVisible.SetLabel("Button 2 is invisible")
Else
btn_2.SetVisible()
btn_SetVisible.SetLabel("Button 2 is visible")
End If
End If



Flip
If KeyHit(KEY_ESCAPE) Or AppTerminate() Then End
Forever

Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group