so benutze ich die include:
um eine Toggle Button aus einer Sprite Grafik zu holen clippe ich den bereich raus welcher definiert wurde in der map.
Funktionen momentan:
CREATE_BUTTON() | Erstellt einen Button einstellungen werden aus einer Map geholt.
SET_BUTTON_STATE() | Setzt einen neuen State für den genannten Button , optional ButtonBlink (blinken),und ButtonSwitchTime (delay)
SET_BUTTON_STATE_default() | Setzt den default state für einen Button neu. ? fraglich ob man das braucht.
Hauptfunktionen:
RENDER_BUTTONS() | Zeichnet sie.
ExamineButton() | gibt die ButtonID zurück vom gedrückten button. (Mouse oder Tastatur)
Probleme:
Tasten können nicht doppelt belegt werden da ansonsten nur eine Taste von beiden funktioniert.
Bekannte Fehler:
hab den verdacht das der Clipbereich nicht 100% stimmt , vielleicht liegts auch an meiner Grafik.
Todo:
Mehr funktionen bei den Buttons.
Optimieren.
Achtung
So demo code unten inkl code der include .
bild ist auch dabei.
button_demo.pb
Code: Alles auswählen
EnableExplicit
UsePNGImageDecoder()
;// gfx
#Sprite_buttons = 0
;// Button SETTINGS
#BUTTON_WIDTH = 70
#BUTTON_HEIGHT = 70
;// Custom Settings
#BUTTON_BLINKTIME = 200 ;// 1000 ms blink time
#BUTTON_SWITCHTIME = 200;// after 500 go back to state default
#BUTTON_DURATION_SPIN = 3000
IncludeFile("buttoninclude.pbi")
;// templates buttons
Procedure INIT_ButtonTemplates()
With ButtonTemplates("ButtonStart")
\Sound = #PB_Ignore
\ButtonState = #BUTTON_ON
\ButtonStateDefault = #BUTTON_ON
\ButtonID = #BUTTON_START
\SpriteClip = #Sprite_buttons
\ButtonKey = #PB_Key_A
\ButtonBlink = #False
\posx = 0
\posy = 0
\buttonwidth = #BUTTON_WIDTH
\buttonheight = #BUTTON_HEIGHT
\Sprite\x = #BUTTON_WIDTH
\Sprite\y = 0
\Sprite\width = #BUTTON_WIDTH
\Sprite\height = #BUTTON_HEIGHT
\SpritePushed\x = #BUTTON_WIDTH
\SpritePushed\y = #BUTTON_HEIGHT
\SpritePushed\width = #BUTTON_WIDTH
\SpritePushed\height = #BUTTON_HEIGHT
EndWith
With ButtonTemplates("ButtonStart2")
\Sound = #PB_Ignore
\ButtonState = #BUTTON_OFF
\ButtonStateDefault = #BUTTON_ON
\ButtonID = #BUTTON_START
\SpriteClip = #Sprite_buttons
\ButtonKey = #PB_Key_B
\ButtonBlink = #False
\posx = 80
\posy = 0
\buttonwidth = #BUTTON_WIDTH
\buttonheight = #BUTTON_HEIGHT
\Sprite\x = #BUTTON_WIDTH
\Sprite\y = 0
\Sprite\width = #BUTTON_WIDTH
\Sprite\height = #BUTTON_HEIGHT
\SpritePushed\x = #BUTTON_WIDTH
\SpritePushed\y = #BUTTON_HEIGHT
\SpritePushed\width = #BUTTON_WIDTH
\SpritePushed\height = #BUTTON_HEIGHT
EndWith
EndProcedure
INIT_ButtonTemplates()
InitSprite()
initsprite3d()
InitKeyboard()
InitMouse()
If OpenWindow(0,0,0,200,200,"inlcude test")
If OpenWindowedScreen(WindowID(0),0,0,200,200)
If LoadSprite(0,"test.png")
CREATE_BUTTON(buttons(),ButtonTemplates("ButtonStart"))
CREATE_BUTTON(buttons(),ButtonTemplates("ButtonStart2"))
;// hauptschleife
Repeat
FlipBuffers()
Start3D()
ClearScreen(RGB(0,0,0))
RENDER_BUTTONS(Buttons())
Stop3D()
Debug MouseX()
ExamineKeyboard()
ExamineMouse()
Select ExamineButton(buttons(),WindowEvent(),MouseX(),MouseY())
;Case #BUTTON_START
; SET_BUTTON_STATE(#BUTTON_START,#BUTTON_OFF,#False)
EndSelect
Until KeyboardReleased(#PB_Key_Escape)
EndIf
EndIf
EndIf
buttoninclude.pbi
Code: Alles auswählen
;******************************************************************************************************
;- *** Include Button ***
;******************************************************************************************************
;// Include Button
;// Johannes Meyer
;// version 1
;// PB 5.11
;// 27.1.2015
;******************************************************************************************************
;- *** ***
;******************************************************************************************************
;enum
;// button states
Enumeration
#BUTTON_ON
#BUTTON_OFF
#BUTTON_BLINK
EndEnumeration
;// button types
Enumeration
#BUTTON_START
#BUTTON_LEVEL
#BUTTON_STOP
#BUTTON_1zu1
EndEnumeration
;// structures
Structure ClipArea
x.f
y.f
width.f
height.f
EndStructure
Structure Button
posx.i
posy.i
posdifx.i
posdify.i
height.i
width.i
distance.i
transparenz.i
text.s
Color.i
Size.f
TypeFace.i
;// Buttons
ButtonKey.i
ButtonState.i
ButtonStateDefault.i
ButtonSwitch.i ;// true / false
ButtonSwitchTime.i
ButtonBlink.i
ButtonID.i
buttonheight.i
buttonwidth.i
SpriteClip.i
Sprite.ClipArea
SpritePushed.ClipArea
BlinkTimer.i
BlinkStartTime.f
Sound.i
SoundPushed.i
SoundHover.i
AlignX.f
AlignY.f
EndStructure
;// list maps
Global NewMap ButtonTemplates.Button()
Global NewList Buttons.Button()
Procedure IsInBox(fromX.i,fromY.i,X.i, Y.i, Width.i, Height.i)
If fromX >= x And fromX < x+Width And fromY >= y And fromY < y+Height
ProcedureReturn #True
EndIf
EndProcedure
;// prozedures
;// PB_ANY all buttons
Procedure SET_BUTTON_STATE(ButtonID.i,ButtonState.i,ButtonBlink = #False,ButtonSwitchTime.i = #PB_Ignore)
ForEach Buttons()
With Buttons()
If \ButtonID = ButtonID Or ButtonID = #PB_Any
\ButtonBlink = ButtonBlink
\ButtonState = ButtonState
If ButtonSwitchTime = #PB_Ignore
\ButtonSwitch = #False
\ButtonSwitchTime = ElapsedMilliseconds()
Else
\ButtonSwitchTime = ButtonSwitchTime
\ButtonSwitch = #True
EndIf
EndIf
EndWith
Next
EndProcedure
Procedure SET_BUTTON_STATE_default(*button.Button,ButtonStateDefault.i)
*button\ButtonStateDefault = ButtonStateDefault
EndProcedure
Procedure CREATE_BUTTON(List Button.button(),*Buttontemplate.Button)
If AddElement(Button())
With Button()
\spriteclip.i = *Buttontemplate\SpriteClip
\Sound = *Buttontemplate\Sound
\ButtonState = *Buttontemplate\ButtonState
\ButtonStateDefault = *Buttontemplate\ButtonStateDefault
\posx = *Buttontemplate\posx
\posy = *Buttontemplate\posy
\buttonwidth = *Buttontemplate\buttonwidth
\buttonheight = *Buttontemplate\buttonheight
\ButtonID = *Buttontemplate\ButtonID
\ButtonKey = *Buttontemplate\ButtonKey
\Sprite\x = *Buttontemplate\Sprite\x
\Sprite\y = *Buttontemplate\Sprite\y
\Sprite\width = *Buttontemplate\Sprite\width
\Sprite\height = *Buttontemplate\Sprite\height
\SpritePushed\x = *Buttontemplate\SpritePushed\x
\SpritePushed\y = *Buttontemplate\SpritePushed\y
\SpritePushed\width = *Buttontemplate\SpritePushed\width
\SpritePushed\height = *Buttontemplate\SpritePushed\height
\ButtonBlink = *Buttontemplate\ButtonBlink
\BlinkStartTime = ElapsedMilliseconds()
EndWith
EndIf
EndProcedure
Procedure RENDER_BUTTONS(List Buttons.Button())
ForEach Buttons()
With Buttons()
;// change state
If \ButtonState = #BUTTON_OFF
If \Sprite\x <> #PB_Ignore
ClipSprite(\SpriteClip,\Sprite\x,\Sprite\y,\Sprite\width,\Sprite\height)
EndIf
ElseIf \ButtonState = #BUTTON_ON
If \Sprite\x <> #PB_Ignore
ClipSprite(\SpriteClip,\SpritePushed\x,\SpritePushed\y,\SpritePushed\width,\SpritePushed\height)
EndIf
ElseIf \ButtonState = #BUTTON_BLINK
ClipSprite(\SpriteClip,\SpritePushed\x,\SpritePushed\y,\SpritePushed\width,\SpritePushed\height)
EndIf
DisplaySprite(\SpriteClip,\posx,\posy)
ClipSprite(\SpriteClip,#PB_Default,#PB_Default,#PB_Default,#PB_Default)
EndWith
Next
EndProcedure
Procedure ExamineButton(List buttons.button(),WindowsEvent,MouseX.i,MouseY.i)
ForEach Buttons()
With Buttons()
;// blink
If \ButtonBlink = #True
If ElapsedMilliseconds() - \BlinkStartTime > #BUTTON_BLINKTIME
\BlinkStartTime = ElapsedMilliseconds()
If \ButtonState = #BUTTON_ON
\ButtonState = #BUTTON_OFF
Else
\ButtonState = #BUTTON_ON
EndIf
EndIf
EndIf
;// switch button back
If \ButtonSwitch And ElapsedMilliseconds() - \ButtonSwitchTime > #BUTTON_SWITCHTIME
\ButtonSwitch = #False
\ButtonState = \ButtonStateDefault
;// setze button auf non blink
If \ButtonBlink = #True
\ButtonBlink = #False
EndIf
If \ButtonState = #BUTTON_BLINK
\ButtonBlink = #True
EndIf
EndIf
If (KeyboardReleased(\ButtonKey) And \ButtonBlink = #True) Or (KeyboardReleased(\ButtonKey) And \ButtonState = #BUTTON_ON)
;// play sound
If \Sound <> #PB_Ignore
PlaySound(\Sound)
EndIf
If \ButtonBlink = #False
;// change state
If \ButtonState = #BUTTON_ON
\ButtonState = #BUTTON_OFF
Else
\ButtonState = #BUTTON_ON
EndIf
EndIf
;// if blink stop it
\ButtonBlink = #False
;// bring button back to normal state
\ButtonSwitch = #True
\ButtonSwitchTime = ElapsedMilliseconds()
;// start event
ProcedureReturn \ButtonID
EndIf
If IsInBox(MouseX,MouseY,\posx,\posy,\buttonwidth,\buttonheight)
If (WindowsEvent = #PB_Event_LeftClick And \ButtonBlink = #True) Or (WindowsEvent = #PB_Event_LeftClick And \ButtonState = #BUTTON_ON)
;// play sound
If \Sound <> #PB_Ignore
PlaySound(\Sound)
EndIf
If \ButtonBlink = #False
;// change state
If \ButtonState = #BUTTON_ON
\ButtonState = #BUTTON_OFF
Else
\ButtonState = #BUTTON_ON
EndIf
EndIf
;// if blink stop it
\ButtonBlink = #False
;// bring button back to normal state
\ButtonSwitch = #True
\ButtonSwitchTime = ElapsedMilliseconds()
;// start event
ProcedureReturn \ButtonID
EndIf
EndIf
EndWith
Next
ProcedureReturn -1
EndProcedure
bei fragen , stehe ich gerne zur verfügung.