Graph Gadget

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
Droopy
Messages : 1151
Inscription : lun. 19/juil./2004 22:31

Graph Gadget

Message par Droopy »

Gadget Histogram

Code : Tout sélectionner

; PureBasic 3.93 
; Idea from Localmotion34 

Structure Graph 
  x.l 
  y.l 
  width.l 
  height.l 
  medium.f 
  hight.f 
  full.f 
  Image.l 
  Gadget.l 
  value.f 
  color1.l 
  color2.l 
  color3.l 
  Segment.f
EndStructure 

ProcedureDLL Graph(x,y,width,height,Segment,medium.f,hight.f,full.f,color1,color2,color3) 
  
  ; Initialise the LinkedList the first call 
  Static Init 
  If Init=0 
    NewList GraphLList.Graph() 
  EndIf 
  Init=1 
  
  ; Fill the Structure 
  AddElement(GraphLList()) 
  GraphLList()\x=x 
  GraphLList()\y=y 
  GraphLList()\width=width 
  GraphLList()\height=height 
  GraphLList()\medium=medium 
  GraphLList()\hight=hight 
  GraphLList()\full=full 
  GraphLList()\color1=color1 
  GraphLList()\color2=color2 
  GraphLList()\color3=color3 
  GraphLList()\Segment=width/Segment
  GraphLList()\Image=CreateImage(#PB_Any,width,height) 
  
  ; create the gadget & show the image 
  GraphLList()\Gadget=ImageGadget(#PB_Any,x,y,width,height,UseImage(GraphLList()\Image),#PB_Image_Border) 
  
  ; Return the gadget id 
  ProcedureReturn ListIndex(GraphLList()) 
  
EndProcedure 

ProcedureDLL GraphSet(id,value.f) 
  
  SelectElement(GraphLList(),id) 
   
  GraphLList()\value=value 
  
  ; Scrolling
  temp=CopyImage(GraphLList()\Image,1365)
  UseImage(GraphLList()\Image) 
  StartDrawing(ImageOutput()) 
  Box(0,0,GraphLList()\width,GraphLList()\height,0 ) ; Efface 
  DrawImage(temp,-(GraphLList()\Segment),0)
  StopDrawing()
  FreeImage(1365)
  
  ; First Color 
  color=GraphLList()\color1 
  
  ; Second Color
  If value > GraphLList()\medium 
    color=color=GraphLList()\color2 
  EndIf 
  
  ; Third Color
  If value > GraphLList()\hight 
    color=color=GraphLList()\color3 
  EndIf 
  
  ; Draw
  UseImage(GraphLList()\Image) 
  StartDrawing(ImageOutput()) 
  
  Ptr.f= ( value /  GraphLList()\full )*GraphLList()\height 
  Box(GraphLList()\width-GraphLList()\Segment,GraphLList()\height,GraphLList()\width,-(GraphLList()\height),0) ; Black
  Box(GraphLList()\width-GraphLList()\Segment,GraphLList()\height,GraphLList()\width,-(Ptr),color) 
  StopDrawing() 
  
  SetGadgetState(GraphLList()\Gadget,UseImage(GraphLList()\Image)) 
  
EndProcedure 

ProcedureDLL GraphGet(id) 
  SelectElement(GraphLList(),id) 
  ProcedureReturn GraphLList()\value 
EndProcedure 

ProcedureDLL GraphEvent(id) 
  SelectElement(GraphLList(),id) 
  ProcedureReturn GraphLList()\Gadget 
EndProcedure  


;/ Test 
OpenWindow(0, 0, 0, 280, 360, #PB_Window_SystemMenu | #PB_Window_ScreenCentered , "Graph") 
CreateGadgetList(WindowID()) 
a=Graph(10,10,250,250,40,3000,6000,9000,RGB(255,0,0),color=RGB(255,255,0),color=RGB(0,255,0)) 
b=Graph(10,280,250,60,40,3000,6000,9000,RGB(255,0,0),color=RGB(255,255,0),color=RGB(0,255,0)) 

For n= 0 To 50000
  toto+1
  If toto=200 
    GraphSet(a,Random(9000)) 
    GraphSet(b,Random(9000)) 
    toto=0 
  EndIf
  
  
  Delay(1) 
  Event=WindowEvent() 
  If Event=#PB_Event_CloseWindow  : End : EndIf 
  If Event=#PB_Event_Gadget 
    Select EventGadgetID() 
      Case GraphEvent(a) 
        Beep_(400,25) 
    EndSelect 
  EndIf      
Next