Page 1 of 1

Draw on an imagegadget

Posted: Tue May 06, 2008 10:33 am
by Hurga
Ok, this is not "advanced" knowledge or so, but I was looking for such a piece of code and didnt find it, so I trued to write it on my own.

It allows to draw with the mouse on an Imagegadget.
If someone has a better code or suggestions how to improve, you´re welcome...

(@Mod: If this code does exist anywhere, maybe delete this post)

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)


;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Image_0
EndEnumeration

;- Image Plugins

;- Image Globals
Global Image0

;- Catch Images
Image0 = CreateImage(#PB_Any, 160, 160)
If StartDrawing(ImageOutput(Image0))
    Box(0, 0, 160, 160, $FFFFFF)
  StopDrawing()
EndIf

Procedure.l MouseOverGadget(WindowID_L.l, GadgetID_L.l) ; returns 1, if the mouse is over the gadget
  Back_L.l
  wx = WindowMouseX(#Window_0)
  wy = WindowMouseY(#Window_0)
  gx = GadgetX(GadgetID_L)
  gy = GadgetY(GadgetID_L)
  
  If wx > -1 And wy > -1
    If wx- gx > 0 And wx- gx <= GadgetWidth(GadgetID_L)
      If wy- gy > 0 And wy- gy <= GadgetHeight(GadgetID_L)
        Back_L = 1
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn Back_L
EndProcedure

Procedure Open_Window_0()
  LBtnDwn_L.l
  
  If OpenWindow(#Window_0, 216, 0, 579, 493, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ImageGadget(#Image_0, 200, 80, 160, 160, ImageID(Image0), #PB_Image_Border)
      
      
      Repeat ; Start of the event loop
        
        Event = WindowEvent()
        WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures 
        GadgetID = EventGadget() ; Is it a gadget event? 
        EventType = EventType() ; The event type
        
        
        If GetSystemMetrics_(#SM_SWAPBUTTON)  
          LBtnDwn_L = GetAsyncKeyState_(#VK_RBUTTON) & $8000 
        Else
          LBtnDwn_L = GetAsyncKeyState_(#VK_LBUTTON) & $8000
        EndIf
        
        If GadgetID = #Image_0
          If MouseOverGadget(#Window_0, #Image_0)
            If LBtnDwn_L
              If StartDrawing(ImageOutput(Image0))
                  wx = WindowMouseX(#Window_0)
                  wy = WindowMouseY(#Window_0)
                  gx = GadgetX(#Image_0)
                  gy = GadgetY(#Image_0)
                  
                  Circle(wx- gx, wy- gy, 3, $000000)
                StopDrawing()
              EndIf
              SetGadgetState(#Image_0, ImageID(Image0))
              
            EndIf
          EndIf
        EndIf
        
        Delay(1)
      Until Event = #PB_Event_CloseWindow ; End of the event loop
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()

End


Posted: Tue May 06, 2008 3:36 pm
by Rook Zimbabwe
3.95 lives!

Posted: Tue May 06, 2008 4:02 pm
by Fluid Byte
The coords of the "brush" don't get interpolated when you're moving the mouse to fast. You may have a look at this:

http://www.purebasic.fr/english/viewtop ... magegadget

Posted: Tue May 06, 2008 7:00 pm
by Hurga
Thx...
I searched for drawing on imagegadget... not painting on imagegadget... So i didnt found that thread.

I changed to lines and use some API to draw thicker (is this correct to say so?) lines.
Green dots are used to show where it started.

Code: Select all

; PureBasic Visual Designer v3.95 build 1485 (PB4Code)


;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Image_0
EndEnumeration

;- Image Plugins

;- Image Globals
Global Image0

;- Catch Images
Image0 = CreateImage(#PB_Any, 160, 160)
If StartDrawing(ImageOutput(Image0))
    Box(0, 0, 160, 160, $FFFFFF)
  StopDrawing()
EndIf

Procedure Max(Value1_L.l, Value2_L.l) ; gibt den grösseren der beiden Werte zurück
  Back_L.l
  If Value1_L > Value2_L
    Back_L = Value1_L
  Else
    Back_L = Value2_L
  EndIf
  ProcedureReturn Back_L
EndProcedure
Procedure.l AbsL(Value_L.l) ; returns the abs of a long
  Back_L.l = Value_L
  
  If Value_L < 0
    Back_L = -Value_L
  EndIf
  
  ProcedureReturn Back_L
EndProcedure 
Procedure.l MouseOverGadget(WindowID_L.l, GadgetID_L.l) ; returns 1, if the mouse is over the gadget
  Back_L.l
  wx = WindowMouseX(#Window_0)
  wy = WindowMouseY(#Window_0)
  gx = GadgetX(GadgetID_L)
  gy = GadgetY(GadgetID_L)
  
  If wx > -1 And wy > -1
    If wx- gx > 0 And wx- gx <= GadgetWidth(GadgetID_L)
      If wy- gy > 0 And wy- gy <= GadgetHeight(GadgetID_L)
        Back_L = 1
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn Back_L
EndProcedure


Procedure Lin(DCHandle_L.l, x,y,x1,y1,Width,Color)
  pen=CreatePen_(#PS_SOLID,Width,Color) 
  hPenOld=SelectObject_(DCHandle_L,pen)
  MoveToEx_(DCHandle_L,x,y,0):LineTo_(DCHandle_L,x1,y1)
  DeleteObject_(pen)
  DeleteObject_(hPenOld)
EndProcedure 

Procedure Open_Window_0()
  LBtnDwn_L.l
  LastX_L.l = -1
  LastY_L.l = -1
  
  If OpenWindow(#Window_0, 216, 0, 579, 493, "New window ( 0 )",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ImageGadget(#Image_0, 200, 80, 160, 160, ImageID(Image0), #PB_Image_Border)
      
      
      Repeat ; Start of the event loop
        
        Event = WindowEvent()
        WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures 
        GadgetID = EventGadget() ; Is it a gadget event? 
        EventType = EventType() ; The event type
        
        
        If GetSystemMetrics_(#SM_SWAPBUTTON)  
          LBtnDwn_L = GetAsyncKeyState_(#VK_RBUTTON) & $8000 
        Else
          LBtnDwn_L = GetAsyncKeyState_(#VK_LBUTTON) & $8000
        EndIf
        
        If GadgetID = #Image_0
          If MouseOverGadget(#Window_0, #Image_0)
            If LBtnDwn_L
              IDC = StartDrawing(ImageOutput(Image0))
                If IDC
                  wx = WindowMouseX(#Window_0)
                  wy = WindowMouseY(#Window_0)
                  gx = GadgetX(#Image_0)
                  gy = GadgetY(#Image_0)
                  
                  If LastX_L = -1 Or LastY_L = -1
                    Circle(wx - gx, wy - gy, 4, $00FF00)
                  Else
                    Lin(IDC, LastX_L, LastY_L, wx- gx, wy- gy, 3, $000000)
                  EndIf
                  LastX_L = wx- gx
                  LastY_L = wy- gy
                StopDrawing()
              EndIf
              SetGadgetState(#Image_0, ImageID(Image0))
              
            Else
              LastX_L = -1
              LastY_L = -1
            EndIf
          EndIf
        EndIf
        
        Delay(1)
      Until Event = #PB_Event_CloseWindow ; End of the event loop
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()

End