Page 1 of 1

Graph Gadget

Posted: Tue May 31, 2005 3:31 pm
by Droopy
Code updated For 5.20+

Code: Select all

; 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
    Global  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,ImageID(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)
  
  StartDrawing(ImageOutput(GraphLList()\Image))
  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=GraphLList()\color2
  EndIf
  
  ; Third Color
  If value > GraphLList()\hight
    color=GraphLList()\color3
  EndIf
  
  ; Draw
  
  StartDrawing(ImageOutput(GraphLList()\Image))
  
  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,ImageID(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 , "Graph", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

a=Graph(10,10,250,250,40,3000,6000,9000,RGB(255,0,0),RGB(255,255,0),RGB(0,255,0))
b=Graph(10,280,250,60,40,3000,6000,9000,RGB(255,0,0),RGB(255,255,0),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 EventGadget()
      Case GraphEvent(a)
        Beep_(400,25)
    EndSelect
  EndIf     
Next


Posted: Wed Jun 01, 2005 3:58 pm
by dell_jockey
just what the doctor ordered! thanks!

Posted: Thu Jun 02, 2005 12:15 pm
by Fred
Nice piece of code. Don't forget to flush the event loop with a code similar to

Code: Select all

Repeat 
  Event = WindowEvent()
  ....
Until Event = 0
else your message queue won't be handled correctly (try to move a window above your gadgets to see what i mean, it will be slow to refresh).

Posted: Thu Jun 02, 2005 12:32 pm
by Kale
Fred wrote:Nice piece of code. Don't forget to flush the event loop with a code similar to

Code: Select all

Repeat 
  Event = WindowEvent()
  ....
Until Event = 0
else your message queue won't be handled correctly (try to move a window above your gadgets to see what i mean, it will be slow to refresh).
Could this be included as a new command?

Code: Select all

FlushEvents()
:?:

Posted: Thu Jun 02, 2005 12:57 pm
by traumatic
droopy! :D
Kale wrote:Could this be included as a new command?

Code: Select all

FlushEvents()
:?:
Sorry, isn't WindowEvent() enough?

Posted: Thu Jun 02, 2005 1:14 pm
by Droopy
you can easily manage event with :D

Code: Select all

If EventGadgetID()=GraphEvent(id)
...
...
The example is just to test the lib, i don't manage event sorry :oops: [/code]

Posted: Thu Jun 02, 2005 1:28 pm
by Fred
Kale: yes, but in this case you can't handle the event at all, it will be similar to While WindowEvent() : Wend.

Posted: Thu Jun 02, 2005 3:53 pm
by Bonne_den_kule
Make a thread to change the graph values, and let the main program handle the events