Seite 2 von 2

Verfasst: 09.09.2006 19:41
von Falko
Dann kauf man schön :twisted:

http://www.purebasic.fr/english/viewtop ... t=progress

[edit]
Hab mal für einen Balken den Prozentwert hinzugefügt, der sich bei 50% ändert.

Code: Alles auswählen

;Author: Dare2 aus dem englischen Forum
;Etwas mit Prozente erweitert von Falko
;Läuft unter PB4
EnableExplicit

#progBar_Reverse = 1

Structure UDT_GadData
  grp.l                                   ; Grouping for bulk operations
  base.l                                  ; A base value, gadget dependent
  first.l                                 ; A first value, gadget dependent
  last.l                                  ; A last value, gadget dependent
  max.l                                   ; A max value, gadget dependent
  min.l                                   ; A min value, gadget dependent
  assocA.l                                ; An associated gadget or object
  assocB.l                                ; Another
  state.l                                 ; An overall state
  x.l                                     ; Overall x position
  y.l                                     ; Overall y position
  wide.l                                  ; Overall width
  high.l                                  ; An overall height
  flagA.l                                 ; A flag
  flagB.l                                 ; Another, in case of value conflict
  flagC.l                                 ; Another, in case of value conflict
  value.l                                 ; A general value
EndStructure

Procedure create_ProgressBar(gi,ii,x,y,w,h,mx,bg,bc,ec,flg=0)

  Protected *p.UDT_gadData                       ; Gadget data

  *p = AllocateMemory(SizeOf(UDT_gadData))       ; Get some memory

  *p\base = bg                                   ; Base  = background colour
  *p\first = bc                                  ; First = starting colour
  *p\last = ec                                   ; Last  = ending colour
  *p\max = mx                                    ; max   = Highest value
  *p\assocA = ii                                 ; assoc = PB image number
  *p\wide = w
  *p\high = h
  *p\state = 0                                   ; state = current progress value
  *p\flagA = flg & #progBar_Reverse              ; flagA = Back to front?
  *p\flagB = flg & #PB_ProgressBar_Vertical      ; flagA = Vertical?
  *p\flagC = flg & #PB_Image_Border              ; flagC = Border?

  CreateImage(ii,w,h)                            ; Create the image
  StartDrawing(ImageOutput(ii))
  Box(0,0,w,h,bg)
  StopDrawing()

  ImageGadget(gi,x,y,w,h,ImageID(ii),*p\flagC)   ; Create the gadget
  SetGadgetData(gi,*p)
EndProcedure

Procedure update_ProgressBar(id,v)

  Protected *p.UDT_gadData                       ; Gadget data
  Protected rb.l, re.l                           ; First,Last red element
  Protected gb.l, ge.l                           ; First,Last green
  Protected bb.l, be.l                           ; First,Last blue
  Protected pix.l                                ; Pixel max offset
  Protected i.l                                  ; iterator
  Protected r.l, g.l, b.l                        ; Workhorses, red/green/blue
  Protected oset.l                               ; Workhorse, pix offset for line
  Protected vDist.l                              ; Distance of progress bar
  Protected pos.l                                ; position made safe

  *p = GetGadgetData(id)

  If v > *p\max
    pos = *p\max
  ElseIf v < 0
    pos = 0
  Else
    pos = v
  EndIf

  rb = Red(*p\first)   : re = Red(*p\last)
  gb = Green(*p\first) : ge = Green(*p\last)
  bb = Blue(*p\first)  : be = Blue(*p\last)

  If *p\flagB
    vDist = *p\high
  Else
    vDist = *p\wide
  EndIf

  pix = (vDist * pos) / *p\max

  StartDrawing(ImageOutput(*p\assocA))
  Box(0,0, *p\wide,*p\high, *p\base)

  For i = 0 To pix
    r = rb + ((re - rb) * i) / vDist
    g = gb + ((ge - gb) * i) / vDist
    b = bb + ((be - bb) * i) / vDist
    oset = (vDist * *p\flagA) - i              ; Direction to move
    If oset < 0 : oset * -1 : EndIf
    If *p\flagB
      LineXY(0,oset, *p\wide,oset, RGB(r,g,b))
    Else
      LineXY(oset,0, oset,*p\high, RGB(r,g,b))
    EndIf
  Next

  StopDrawing()

  *p\state = pos
  SetGadgetState(id,ImageID(*p\assocA))
EndProcedure

Procedure release_ProgressBar(id)
  Protected *p.UDT_gadData                       ; Gadget data
  *p=GetGadgetData(id)
  FreeImage(*p\assocA)
  FreeGadget(id)
  FreeMemory(*p)
EndProcedure

; =============================================
; Dirty quick test

Define p.l                                     ; workhorse, window colour
Define winEvent.l

Define progA.l                                 ; Progress values
Define progB.l
Define progC.l
Define progD.l
Define prozB.l

OpenWindow(0,#PB_Ignore,0,500,60,"Test")
If CreateGadgetList(WindowID(0))
  TextGadget(4, 200,45 , 20, 25,"") 
EndIf
StartDrawing(WindowOutput(0))
p=Point(1,1)
StopDrawing()

SmartWindowRefresh(0,#True)

CreateGadgetList(WindowID(0))
create_ProgressBar(0,0,  10,10, 400,10, 255, p,      $0000FF,$FFFF00, #PB_Image_Border)
create_ProgressBar(1,1,  10,30, 400,10,1000, $000000,$00FFFF,$00FF00, #PB_Image_Border|#progBar_Reverse)
create_ProgressBar(2,3, 440,10,  10,30, 100, $FFFFFF,$FF00FF,$0000FF, #PB_ProgressBar_Vertical)
create_ProgressBar(3,2, 460,10,  10,30, 500, $800000,$00FFFF,$0000FF, #PB_ProgressBar_Vertical|#progBar_Reverse)

Repeat
  winEvent = WaitWindowEvent(5)
  progA + 1
  progB + 1
  progC + 1
  progD + 1
  If progA > 255  : progA = 0 : EndIf
  If progB > 1000 : progB = 0 : EndIf
  prozB=progB%1001/10
  
  If prozB<50
     SetGadgetColor(4,#PB_Gadget_FrontColor,$000000)
  Else
     SetGadgetColor(4,#PB_Gadget_FrontColor,$FF0000)
  EndIf
  SetGadgetText(4,Str(prozB)+"%")
  
  If progC > 100  : progC = 0 : EndIf
  If progD > 500  : progD = 0 : EndIf
  update_ProgressBar(0,progA)
  update_ProgressBar(1,progB)
  update_ProgressBar(2,progC)
  update_ProgressBar(3,progD)
Until winEvent = #PB_Event_CloseWindow

release_ProgressBar(0)
release_ProgressBar(1)
release_ProgressBar(2)
release_ProgressBar(3)
 
End
[/edit]

Gruß Falko