imagegadget / eventloop problem

Just starting out? Need help? Post your questions and find answers here.
kepu
User
User
Posts: 20
Joined: Mon Jul 13, 2009 8:00 am

changed code

Post by kepu »

Hi, Demivec.
Sorry to say, but this works exactly same way as earlier versions, it stuck same place and then like actually jump over procedure.
I try to explain more what happen:
When I click draw / line ,
the line menu does not dissappear and statusbartext does not appear,
but mouse is visible without crosshair and I can move it ,
when I position it somewhere and then click left-mouse-button down and keep down
and move mouse to other position (crosshair and proposed line are not visible only mouse)
and then when I release left-mouse-button the line-menu disappears and proposed line appear to window ,
so everything happen but does not show in window before everything is over.
And again in crosshair() procedure, if I add a line "ImageGadget(gadget1_n,0,0,800,500,bitmap1_h)"
just before setgadgetstate(.. command , everything works again as they should and as they work in Windows.
Hope this gives better hint of the problem to you guys :?:
kepu
User
User
Posts: 20
Joined: Mon Jul 13, 2009 8:00 am

cont....

Post by kepu »

I forgot to mention , the crosshair works all the time in your version and in my versions,
it actually only stuck as descriped in previous message ,when used through other sub or procedure like 'doline()'
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

@kepu: modifying the version I posted earlier, this one breaks the doLine() procedure into stages. It executes a portion of the stage it is on then returns to the eventLoop. If a stage completes it sets the drawingLineStatus to advance to the next stage. These modifications are based on your description of the problem (that it isn't updating graphic elements until the line drawing completes.).

It works in windows, try this for Linux.

Code: Select all

;code modified by Demivec (8/3/2009)
; additional changes to make doLine() procedure to work in stages so that events can be updated in a more timely manner

Declare crosshair()
Declare doLine()

Procedure handleError(p_result.l, p_text.s) ;display error message if result=0 and then END program.
  ;sample use HandleError(OpenWindow(#WINDOW_ROOT,0,0,120,120,#APP_NAME,#PB_Window_SystemMenu),"Main window could not be created.")
  ;Procedure borrowed from "Kale" in PureBasic forum
If p_result = 0
  MessageRequester("Error", p_text, #PB_MessageRequester_Ok)
  End
EndIf
EndProcedure

Procedure WindowMouseButton(Wnd, ButtonNr)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    ;Linux Version
    
    Protected gdkWnd.l, x.l, y.l, mask.l
    
    If Wnd
      *Window.GTKWindow = Wnd
      gdkWnd = *Window\bin\child\Window
      gdk_window_get_pointer_(gdkWnd, @x, @y, @mask)
      
      Select ButtonNr
        Case 0
          If (mask & #GDK_BUTTON1_MASK)
            ProcedureReturn 1
          EndIf
        Case 1
          If (mask & #GDK_BUTTON3_MASK)
            ProcedureReturn 1
          EndIf
        Case 2
          If (mask & #GDK_BUTTON2_MASK)
            ProcedureReturn 1
          EndIf
      EndSelect
    EndIf
    
  CompilerElse
    ;Windows Version
    
    If Wnd And GetForegroundWindow_() = Wnd
      Select ButtonNr
        Case 0
          If GetAsyncKeyState_(#VK_LBUTTON) > 0
            ProcedureReturn 1
          EndIf
        Case 1
          If GetAsyncKeyState_(#VK_RBUTTON) > 0
            ProcedureReturn 1
          EndIf
        Case 2
          If GetAsyncKeyState_(#VK_MBUTTON) > 0
            ProcedureReturn 1
          EndIf
      EndSelect
    EndIf
    
  CompilerEndIf
  
  ProcedureReturn 0
EndProcedure

;********************************************************************
Define WindowWidth = 1000 
Define WindowHeight = 700
handleError(OpenWindow(0, 10, 10, WindowWidth, WindowHeight, "PureQCad.018 "),"Couldn't setup main Window")
  
Define hwnd = WindowID(0)
handleError(hwnd,"Couldn't obtain window handle")
  
If CreateMenu(0, WindowID(0))
  MenuTitle("File")
  MenuItem( 1, "&Load...")
  MenuItem( 2, "Save")
  MenuBar()
  MenuItem( 5, "Exit")
  
  MenuTitle("Edit")
  MenuTitle("Draw")
  MenuItem( 18, "Line")
  MenuTitle("Mode")
  MenuTitle("Help")
  MenuItem( 44, "Help")
  MenuItem( 45, "About")
  
EndIf 

If CreateStatusBar(0, WindowID(0))
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  AddStatusBarField(100)
  StatusBarText(0, 0, "prog_name")
  StatusBarText(0, 1, " xx= ")
  StatusBarText(0, 2, " yy= ") 
  StatusBarText(0, 3, " orx= ")
  StatusBarText(0, 4, " ory= ")
  StatusBarText(0, 5, " DrawC ")
  StatusBarText(0, 6, " x= " )
  StatusBarText(0, 7, " y= " )
  StatusBarText(0, 8, "     ")
  StatusBarText(0, 9, "     ")
  StatusBarText(0, 10, "     ")
  Define statusbarCreated = #True
EndIf

;********  init imagegadget ***********************************************
Define bitmap1_n = 1                                                             ; image number 1
Define bitmap1_h = CreateImage(bitmap1_n,800,550)      ; create the image and store the handle
Define gadget1_n = 1
ImageGadget(gadget1_n,0,0,800,500,bitmap1_h) 

; ******** init cross hair ***************************************************
StartDrawing(ImageOutput(bitmap1_n))
  FillArea(10,10,-1,$FFFFFF)
  DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
  Define ox=10
  Define oy=10
  Define oox=ox
  Define ooy=oy
  Define hwidth=WindowWidth-40
  Define hheight=WindowHeight-45
  Line(ox,10, 0, hheight , RGB(55,55,55))
  Line(10,oy, hwidth , 0, RGB(55,55,55))
StopDrawing()
SetGadgetState(gadget1_n,bitmap1_h)

Delay(1)
;********* menuloop ******************************************************************************
Define drawingLineStatus = 0
Repeat
  Repeat
    Define event= WaitWindowEvent(5)
    Select event
      Case #PB_Event_Menu
        Select EventMenu() 
          Case 18     ;Line
            doLine()
          Case 45 ; About
            MessageRequester("About", "image_gadget_test", 0)
          Case 5 ; exit
            Define Quit=#True     ;  End
          Default
            MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
        EndSelect
        
      Case #PB_Event_CloseWindow
        Quit=#True
    EndSelect
    
    If drawingLineStatus
      doLine()
    EndIf 
  Until event = 0 Or Quit = #True
  
  crosshair()    ;this does not work properly  in Linux if :SetGadgetState).... is used incrosshair sub
Until  Quit = #True
End

;'****** end of menuloop ****************************************
Procedure crosshair()
  Shared statusbarCreated, bitmap1_h, bitmap1_n, gadget1_n, hheight, hwidth, ox, oy, oox, ooy
  StartDrawing(ImageOutput(bitmap1_n))             
    DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
    ox=WindowMouseX(0)
    oy=WindowMouseY(0)         
    Line ( oox,  10, 0,  hheight, RGB(55,55,55))
    Line (10, ooy, hwidth, 0, RGB(55,55,55))
    Line ( ox, 10, 0, hheight, RGB(55,55,55))
    Line ( 10, oy, hwidth,0 , RGB(55,55,55))
  StopDrawing()   
  SetGadgetState(gadget1_n,bitmap1_h)   
  oox=ox
  ooy=oy
  If statusbarCreated
    StatusBarText(0, 1, " xx= "+Str(ox))
    StatusBarText(0, 2, " yy= "+Str(oy)) 
    StatusBarText(0, 3, " orx= "+Str(oox))
    StatusBarText(0, 4, " ory= "+Str(ooy))
  EndIf 
EndProcedure

Procedure doLine()
  Shared statusbarCreated, bitmap1_h, bitmap1_n, gadget1_n, hwnd, ox, oy, drawingLineStatus
  Static startX, startY, omousX, omousY
  
  Select drawingLineStatus
    Case 0
      drawingLineStatus = 1
    Case 1 ;setLine
        crosshair()
        If statusbarCreated
          StatusBarText(0, 0, "Line") 
        EndIf 
        WindowMouseButton(hwnd, 0) 
        Delay(1)
        If WindowMouseButton(hwnd, 0)=1
          drawingLineStatus = 2
          startX = ox
          startY = oy
        EndIf 
    Case 2  ;startLine
      crosshair()
      StartDrawing(ImageOutput(bitmap1_n))             
        DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
        Protected mousX = WindowMouseX(0)
        Protected mousY = WindowMouseY(0)
        Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200)) 
        omousX = mousX
        omousY = mousY
        If statusbarCreated
          StatusBarText(0, 6, Str(startX) ) 
          StatusBarText(0, 7, Str(startY)) 
        EndIf 
      StopDrawing()
      SetGadgetState(gadget1_n,bitmap1_h)
      Delay(1)
      drawingLineStatus = 3
    Case 3  ;drawLine
      If WindowMouseButton(hwnd, 0) <> 0
        crosshair()
        mousX =ox    ; WindowMouseX(0)
        mousY = oy    ;WindowMouseY(0)
        StartDrawing(ImageOutput(bitmap1_n))             
          DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
          Line(startX,startY,omousX-startX, omousY-startY,RGB(5,5,200)) 
          Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200)) 
          omousX=mousX
          omousY=mousY 
          If statusbarCreated
            StatusBarText(0, 8, Str(mousX-startX)) 
            StatusBarText(0, 9, Str(mousY-startY)) 
          EndIf 
        StopDrawing()
        SetGadgetState(gadget1_n,bitmap1_h)
      Else
        drawingLineStatus = 4
      EndIf 
    Case 4  ;finishLine
      mousX =ox    ; WindowMouseX(0)
      mousY = oy    ;WindowMouseY(0)
      StartDrawing(ImageOutput(bitmap1_n))             
        DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_XOr)
        Line(startX,startY,omousX-startX, omousY-startY,RGB(5,5,200)) 
        Line(startX,startY,mousX-startX,mousY-startY,RGB(5,5,200))
        If statusbarCreated
          StatusBarText(0, 8, Str(mousX-startX)) 
          StatusBarText(0, 9, Str(mousY-startY)) 
          StatusBarText(0, 0, "         ")     
        EndIf 
      StopDrawing()
      SetGadgetState(gadget1_n,bitmap1_h)
      drawingLineStatus = 0
  EndSelect
EndProcedure
kepu
User
User
Posts: 20
Joined: Mon Jul 13, 2009 8:00 am

It works...!

Post by kepu »

:D
Thanks, Demivec
That worked in Linux ,
So it seems , that at least in Linux, a imagegadget has to be refreshed in the eventloop everytime it has changed, .?
:shock:
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: It works...!

Post by Demivec »

kepu wrote::D
Thanks, Demivec
That worked in Linux ,
So it seems , that at least in Linux, a imagegadget has to be refreshed in the eventloop everytime it has changed, .?
:shock:
Your welcome. :)

The general cause would seem to be that more events are generated than were handled each time through the event loop. Those that commented earlier noticed this also. I don't think it matters as much what it is specifically that is generating them if the event loop is properly constructed. Hopefully the way it is constructed now will be usable with the additional things you needed to add. :wink:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

so it seems it was exactly the problem I presumed.

.... I just hadn't much time and wasn't keen on cleaning up a bunch of GOSUB-code.
thus I said "don't produce more events then you process, only produce events if the quere is empty, if actual event is #Null"
- shouldn't following this just have done the job?

all Information was in my postings, trying to integrate them could have already solved the problem.
cleaning up the code instead of three times asking back and the same would have been good.

but why... Demivec took the work on his shoulder.

nice move, good work, Demivec!
oh... and have a nice day.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Kaeru Gaman wrote:so it seems it was exactly the problem I presumed.

.... I just hadn't much time and wasn't keen on cleaning up a bunch of GOSUB-code.
thus I said "don't produce more events then you process, only produce events if the quere is empty, if actual event is #Null"
- shouldn't following this just have done the job?

all Information was in my postings, trying to integrate them could have already solved the problem.
cleaning up the code instead of three times asking back and the same would have been good.

but why... Demivec took the work on his shoulder.

nice move, good work, Demivec!
@Kaeru Gaman: Thanks for the compliment. I did give you and others credit for your observations. :wink:

It would have been a greater chore to make the changes to the original Gosub/Goto code layout. Once that was converted to procedures the process was much simpler. It would of been even easier if I had Linux.
kepu
User
User
Posts: 20
Joined: Mon Jul 13, 2009 8:00 am

three times....

Post by kepu »

Sorry , I was so stupid , ..had to ask three times..
and as I said, gosub staff was not actual main problem although it was not good programming.
Forum index says: "Just starting out? Need help? ", ..I am not pro in PB,
I will keep it on my mind next time.
Post Reply