Page 1 of 2

How to manage WindowedScreen+GUI Gadgets together

Posted: Thu Mar 22, 2007 2:20 pm
by netmaestro
Many questions recently have related to the management of WindowedScreens with the mouse captured together with GUI gadgets requiring mouse eventhandling. How to do both?

Like this (fully cross-platform):

Code: Select all

;**************************************************************** 
; Program:         WindowedScreen + GUI Eventhandling Demo 
; Author:          netmaestro 
; Date:            March 22, 2007 
; Updated:       September 9, 2014 (for PB 5.31)
; License:         Free, unrestricted, credit appreciated 
;                  but not required 
;**************************************************************** 

UseJCALG1Packer()

*unpacked = AllocateMemory(6984) 
UncompressMemory(?PicPak, ?PicPakEnd-?PicPak, *unpacked, 6984, #PB_PackerPlugin_JCALG1) 
Global img0 = CatchImage(#PB_Any, *unpacked, 6984) 
Global img1 = GrabImage(img0, #PB_Any, 0,42,15,28) 
StartDrawing(ImageOutput(img0)) 
  Box(0,42,15,28,#Black) 
StopDrawing() 
FreeMemory(*unpacked) 

#LeftOffset = 20 
#TopOffset  = 20 
#ScreenW    = 440 
#ScreenH    = 440 

OpenWindow(0,0,0,800,480,"Screen Mouse Capture/Release Demo - Test mouse buttons while in screen",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
ButtonGadget(1,570,420,120,24,"Push Me!") 
InitSprite():InitMouse() 
OpenWindowedScreen(WindowID(0),20,20,440,440,0,0,0) 

CreateSprite(0,32,32) 
StartDrawing(SpriteOutput(0)) 
  DrawImage(ImageID(img1),0,0) 
StopDrawing() 

Procedure DoScreenDisplay(inscreen) 
  ClearScreen(0) 
  ; 
  ; Display your screen stuff 
  StartDrawing(ScreenOutput()) 
    DrawImage(ImageID(img0),170,300) 
    DrawText(120,200,"Screen is not capturing mouse      ",#Gray, #Black) 
    DrawText(180,230,"( 000, 000 )", #Gray, #Black)    
  StopDrawing()  
  ; 
  If inscreen  ; manage mouse events only if mouse is inside screen 
    WindowEvent()
    ExamineMouse() 
    StartDrawing(ScreenOutput()) 
      DrawText(120,200,"  Screen is capturing mouse      ",#Yellow, #Black) 
      DrawText(180,230,"( "+RSet(Str(MouseX()),3,"0")+", "+RSet(Str(MouseY()),3,"0")+" )",#White,#Black) 

      If MouseButton(#PB_MouseButton_Left) 
        FillArea(186,316,0,#Red) 
      EndIf 
  
      If MouseButton(#PB_MouseButton_Middle) 
        FillArea(202,311,0,#Red) 
      EndIf 
      If MouseButton(#PB_MouseButton_Right) 
        FillArea(216,306,0,#Red) 
      EndIf 
    StopDrawing() 
    DisplayTransparentSprite(0,MouseX(),MouseY()) 
  EndIf 
  FlipBuffers() 
EndProcedure 

Repeat 
  ev = WaitWindowEvent() 
  If inscreen 
    If MouseX()>#ScreenW-2 Or MouseY()>#ScreenH-2 Or MouseX()<1 Or MouseY()<1 
      ReleaseMouse(1) 
      inscreen = #False 
    EndIf  
  Else 
    ; 
    ;************************************ 
    ;    Handle #PB_Event_Gadget Here 
    ;************************************ 
    ; 
    If ev = #PB_Event_Gadget 
      If EventGadget() = 1 
        MessageRequester("","You pressed the button!") 
      EndIf 
    EndIf 
    ; 
    mx = WindowMouseX(0):my = WindowMouseY(0) 
    If mx < #ScreenW+#LeftOffset And mx > #LeftOffset And my > #TopOffset And my < #TopOffset+#ScreenH 
      ReleaseMouse(0) 
      MouseLocate(mx-#LeftOffset,my-#TopOffset) 
      inscreen = #True 
    EndIf 
  EndIf 
  DoScreenDisplay(inscreen) 
Until ev = #PB_Event_CloseWindow 

DataSection 
  PicPak: 
  Data.b $4A,$43,$48,$1B,$00,$00,$16,$6A,$D7,$16,$B4,$A9,$D0,$20,$6D,$14,$D9,$88,$12,$CA 
  Data.b $08,$B0,$4A,$44,$25,$B2,$0C,$10,$46,$02,$11,$D2,$28,$79,$12,$02,$5B,$28,$09,$80 
  Data.b $29,$02,$E2,$1F,$01,$FF,$09,$BE,$74,$5F,$A5,$E0,$52,$77,$95,$DC,$4A,$A6,$51,$F2 
  Data.b $28,$09,$FE,$83,$84,$14,$68,$7F,$84,$00,$47,$A0,$FA,$11,$22,$1F,$81,$D8,$11,$C8 
  Data.b $7A,$04,$6E,$47,$A0,$E3,$04,$12,$1D,$81,$C0,$11,$48,$71,$81,$C0,$46,$20,$66,$04 
  Data.b $2A,$1A,$A0,$91,$11,$48,$18,$81,$2C,$46,$00,$30,$02,$6A,$0F,$68,$60,$84,$51,$80 
  Data.b $7D,$DC,$57,$80,$F9,$82,$08,$69,$EC,$C8,$46,$0E,$20,$C2,$EC,$5D,$CC,$E3,$68,$1F 
  Data.b $99,$45,$16,$2F,$66,$E4,$20,$A9,$68,$00,$BC,$58,$00,$0E,$A0,$37,$9A,$61,$47,$EE 
  Data.b $C8,$4C,$2D,$0A,$43,$A3,$56,$D1,$03,$6C,$77,$6C,$DC,$67,$3E,$CC,$32,$FA,$23,$46 
  Data.b $0B,$30,$91,$AA,$C8,$AC,$80,$59,$BC,$C8,$68,$9B,$99,$75,$91,$3E,$32,$1B,$AE,$83 
  Data.b $C2,$D8,$A2,$64,$22,$45,$2F,$06,$00,$31,$F3,$6C,$24,$12,$8E,$19,$B9,$6D,$C6,$CC 
  Data.b $17,$B6,$32,$D4,$29,$EE,$10,$25,$62,$5C,$30,$2C,$80,$86,$79,$28,$39,$94,$5C,$24 
  Data.b $25,$30,$52,$9A,$14,$69,$B3,$32,$A1,$32,$8A,$CD,$2E,$32,$18,$14,$15,$33,$46,$A8 
  Data.b $78,$78,$E8,$B8,$31,$23,$58,$1F,$18,$03,$0D,$19,$3A,$8C,$7E,$3C,$33,$88,$11,$8B 
  Data.b $CC,$3A,$01,$80,$50,$47,$0C,$AA,$D8,$3A,$48,$9C,$60,$44,$E8,$E0,$3E,$9D,$02,$6C 
  Data.b $12,$09,$D3,$23,$C1,$01,$25,$91,$36,$29,$4C,$F3,$12,$28,$5F,$14,$B1,$C8,$52,$27 
  Data.b $14,$27,$44,$9A,$41,$49,$03,$6A,$F3,$F4,$36,$2B,$C0,$5A,$0B,$34,$33,$C5,$A6,$35 
  Data.b $5B,$26,$69,$46,$93,$19,$41,$B1,$53,$FC,$12,$31,$2E,$6B,$5A,$05,$98,$25,$61,$82 
  Data.b $B7,$49,$A1,$2A,$23,$11,$BA,$C6,$CC,$83,$B4,$D8,$29,$4D,$A8,$49,$90,$70,$F3,$12 
  Data.b $33,$2A,$86,$A2,$34,$67,$CC,$68,$9C,$E4,$98,$59,$66,$93,$79,$D9,$C9,$D5,$A4,$44 
  Data.b $E9,$64,$60,$95,$CB,$A6,$C0,$20,$C9,$1D,$5E,$52,$60,$52,$3C,$8F,$CC,$30,$E7,$B7 
  Data.b $E8,$75,$D9,$69,$AC,$0B,$03,$CC,$22,$B9,$EA,$92,$12,$E8,$94,$D4,$04,$5B,$73,$FE 
  Data.b $63,$E6,$3B,$35,$C9,$61,$25,$12,$D3,$A2,$3E,$FA,$07,$58,$00,$86,$92,$54,$B0,$2C 
  Data.b $4F,$64,$B0,$39,$4F,$BA,$49,$C0,$26,$B1,$02,$8C,$82,$59,$47,$BA,$2C,$AF,$32,$DB 
  Data.b $A4,$8C,$B3,$2A,$D6,$62,$4D,$C7,$20,$63,$B7,$FC,$A2,$47,$94,$44,$D9,$00,$84,$32 
  Data.b $32,$D3,$92,$C7,$CC,$FB,$C7,$4B,$4C,$29,$12,$B3,$AC,$23,$44,$66,$97,$13,$54,$D6 
  Data.b $C4,$54,$C8,$CD,$71,$C3,$0B,$B0,$0A,$63,$2A,$29,$1D,$AD,$2B,$36,$67,$F2,$2B,$B1 
  Data.b $C2,$F4,$2E,$93,$41,$D2,$32,$B3,$34,$4A,$AA,$73,$D2,$BB,$76,$8A,$FB,$81,$23,$C7 
  Data.b $C7,$4C,$2E,$99,$90,$D0,$68,$5F,$80,$55,$CD,$E2,$95,$F9,$9D,$68,$A5,$69,$30,$86 
  Data.b $DD,$1C,$09,$80,$6B,$C2,$13,$2A,$32,$28,$68,$6D,$2B,$BF,$CE,$A8,$D8,$A5,$3D,$66 
  Data.b $17,$4C,$C5,$49,$B2,$51,$53,$26,$DF,$09,$4A,$1A,$AD,$45,$01,$56,$B9,$6C,$D1,$27 
  Data.b $26,$D7,$78,$61,$6E,$7A,$2B,$76,$61,$47,$99,$55,$28,$37,$55,$B1,$F1,$15,$49,$49 
  Data.b $17,$1C,$C8,$AC,$22,$99,$CC,$4B,$93,$AF,$4A,$2F,$38,$DF,$CC,$2C,$17,$CD,$30,$3B 
  Data.b $ED,$8A,$BB,$F0,$31,$E9,$3D,$66,$99,$16,$99,$34,$99,$F9,$51,$FA,$3A,$74,$60,$5E 
  Data.b $DA,$E9,$D1,$EF,$31,$BB,$BF,$7F,$6F,$F3,$32,$15,$03,$10,$8B,$8D,$84,$D6,$46,$F9 
  Data.b $2D,$66,$CF,$4E,$19,$82,$83,$B6,$4C,$45,$58,$3F,$66,$8C,$D8,$9C,$22,$01,$B8,$58 
  Data.b $00,$7E,$1E,$52,$94,$85,$FB,$31,$C3,$38,$B2,$BC,$15,$8A,$0C,$85,$B2,$7A,$0C,$21 
  Data.b $71,$A7,$5A,$D7,$B1,$55,$E1,$85,$42,$2D,$0A,$1B,$2F,$94,$24,$F4,$7E,$CC,$65,$6D 
  Data.b $2C,$7A,$3B,$55,$48,$07,$EF,$C7,$0C,$83,$F5,$38,$47,$43,$02,$AD,$E6,$A8,$CC,$12 
  Data.b $59,$A4,$A3,$91,$F2,$5E,$68,$25,$F1,$31,$26,$86,$A0,$ED,$CA,$9D,$C2,$0C,$3F,$AE 
  Data.b $22,$A8,$C3,$7B,$A4,$E8,$2E,$82,$B2,$14,$D3,$8A,$BE,$5C,$38,$0E,$8E,$FA,$E2,$90 
  Data.b $CA,$B5,$20,$17,$07,$D7,$CC,$AC,$AA,$D8,$A4,$E6,$29,$6F,$3B,$18,$3C,$4C,$31,$2B 
  Data.b $4C,$82,$8F,$51,$D0,$FE,$31,$D1,$6A,$92,$08,$B4,$7C,$0F,$06,$27,$9D,$20,$E4,$FC 
  Data.b $CB,$D8,$3B,$A4,$12,$0D,$07,$2E,$92,$40,$85,$39,$EB,$B0,$7D,$8D,$19,$61,$B0,$4D 
  Data.b $15,$22,$9F,$C0,$DE,$1A,$EA,$42,$68,$26,$AA,$D0,$FF,$47,$33,$45,$BF,$C6,$48,$69 
  Data.b $08,$09,$94,$30,$94,$85,$A3,$A1,$FD,$2D,$5C,$FE,$BE,$D5,$9A,$80,$A0,$B0,$1C,$5A 
  Data.b $96,$25,$A3,$DF,$85,$EA,$0D,$32,$F8,$31,$7F,$4F,$D1,$84,$D0,$39,$0E,$69,$AB,$C7 
  Data.b $2C,$90,$77,$AF,$47,$44,$BF,$42,$45,$3A,$B8,$30,$AB,$1F,$0C,$B4,$EF,$CC,$BD,$66 
  Data.b $E8,$5A,$F4,$BB,$A0,$16,$40,$43,$3B,$38,$F5,$CD,$54,$92,$FB,$9E,$07,$18,$88,$84 
  Data.b $81,$D4,$C5,$66,$47,$6E,$57,$8D,$85,$DE,$C0,$14,$5F,$05,$53,$DC,$6C,$F6,$7E,$90 
  Data.b $D4,$B9,$0F,$59,$7E,$67,$C0,$93,$04,$5A,$CC,$C4,$E4,$A8,$31,$43,$B4,$CA,$47,$8A 
  Data.b $12,$BB,$59,$97,$55,$E8,$B9,$86,$3F,$5E,$83,$54,$D9,$71,$77,$38,$57,$8C,$DA,$14 
  Data.b $20,$A2,$6B,$85,$81,$5C,$FB,$04,$0F,$FD,$C7,$43,$FB,$66,$91,$9F,$81,$2E,$02,$6D 
  Data.b $18,$0A,$D6,$35,$A3,$E4,$A4,$19,$6A,$D0,$40,$6A,$2A,$4C,$CA,$9A,$AE,$C4,$66,$0B 
  Data.b $44,$F4,$10,$51,$31,$AC,$04,$3B,$1E,$6A,$91,$AE,$9D,$F2,$F9,$02,$E0,$AB,$45,$20 
  Data.b $08,$15,$62,$0D,$AA,$0B,$4D,$91,$6E,$8E,$B9,$D8,$AC,$E8,$2E,$13,$BF,$22,$45,$16 
  Data.b $CD,$D1,$EF,$10,$41,$AA,$4F,$82,$62,$61,$F0,$90,$69,$90,$45,$FA,$CD,$4E,$1C,$AD 
  Data.b $12,$B3,$29,$CA,$44,$AE,$D0,$4F,$9B,$F2,$94,$B2,$16,$2E,$15,$51,$20,$A4,$68,$65 
  Data.b $51,$83,$A2,$57,$62,$A6,$54,$BC,$60,$5D,$F5,$70,$51,$89,$8E,$B5,$C4,$71,$64,$80 
  Data.b $00,$3C,$52,$A4,$E8,$A2,$13,$AE,$1E,$F9,$2A,$57,$F9,$22,$E0,$12,$A9,$18,$B7,$20 
  Data.b $D0,$6F,$0E,$19,$C2,$04,$36,$43,$73,$B4,$44,$AE,$EC,$2A,$1C,$17,$19,$43,$8E,$F2 
  Data.b $5D,$A8,$0B,$B3,$40,$7A,$0D,$00,$88,$84,$AA,$00,$E3,$9A,$16,$E3,$72,$D3,$1F,$E9 
  Data.b $58,$A8,$A3,$42,$B8,$18,$12,$D2,$8B,$C9,$F8,$7A,$A5,$CE,$E5,$2E,$4D,$A4,$A4,$C4 
  Data.b $A4,$0E,$81,$CE,$22,$71,$72,$D2,$91,$4E,$EA,$8E,$0D,$F3,$2E,$93,$42,$3B,$8B,$E4 
  Data.b $B7,$F2,$CA,$48,$AE,$61,$CE,$93,$BB,$CC,$DB,$BC,$C4,$E4,$01,$2C,$7B,$12,$C3,$A4 
  Data.b $84,$F4,$12,$FE,$4C,$E8,$46,$57,$A2,$77,$59,$09,$69,$67,$75,$01,$F6,$89,$5A,$47 
  Data.b $9F,$E5,$51,$52,$24,$00,$DE,$D9,$5D,$98,$B3,$68,$52,$F4,$48,$C4,$EA,$00,$00,$00 
  Data.b $00,$80 
  PicPakEnd:
EndDataSection 

Posted: Thu Mar 22, 2007 2:39 pm
by IceSoft
Nice example!

But I have found a bug in OpenWindowedScreen(...,AutoStretch,...) again.
AutoStretch should not resize to the whole window! But It did it!

Change this lines:

Code: Select all

OpenWindow(0,0,0,800,480,"Screen Mouse Capture/Release Demo",#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
...
OpenWindowedScreen(WindowID(0),20,20,440,440,1,0,0)

Posted: Thu Mar 22, 2007 2:44 pm
by netmaestro
This example doesn't address resizing, that requires a few minor adjustments.

Posted: Thu Mar 22, 2007 3:01 pm
by Derek
Nice one, netmaestro.

Looks really good the way the cursor changes, very professional.

Posted: Mon Mar 26, 2007 11:46 am
by dell_jockey
Veni, vidi, vici - indeed! Well done!

Posted: Fri Apr 06, 2007 4:48 pm
by netmaestro
Code updated, new version in first post.

Posted: Fri Apr 06, 2007 5:04 pm
by rsts
Nice.

(Now if it just included a ruler)

:lol: :lol:

cheers

Posted: Thu Apr 19, 2007 3:03 pm
by Criss
Hi!
Is there a way, to do the same with the windowedscreen inside a containergadget?

Posted: Thu Apr 19, 2007 8:40 pm
by netmaestro
Is there a way, to do the same with the windowedscreen inside a containergadget?
It's manageable in a container and can be made to work but afaik not without resorting to using API for the mouse messaging, which takes it outside the scope of this tip. In this thread we're staying within the confines of a crossplatform solution. I'd like to be wrong, if I am and someone has code I hope they'll share it here.

Posted: Fri Apr 20, 2007 10:23 am
by thyphoon
Thanks netmaestro It's great exemple !

But a little question if you have an animation in the screen...if the mouse is not on the screen..the screen is not updated ! isn't it ?

How Do you if you want to update all time the screen ?

Posted: Fri Apr 20, 2007 3:16 pm
by netmaestro
Just replace WaitWindowEvent() with WaitWindowEvent(1) and it will update the screen each timeslice.

Posted: Fri Apr 20, 2007 4:32 pm
by thyphoon
netmaestro wrote:Just replace WaitWindowEvent() with WaitWindowEvent(1) and it will update the screen each timeslice.
Great thanks !! :)

Posted: Sun May 27, 2007 3:48 pm
by Yewklid
Thanks NetMaestro. A very helpful example.

One question: If the mouse is over the windowedscreen when the window opens, the button is not visible. The button appears when you move the mouse off the screen. Is it possible to make sure the button is visible fron the start?

Posted: Mon May 28, 2007 3:03 am
by netmaestro
Fixed.

Posted: Mon May 28, 2007 10:25 am
by Yewklid
I see - WindowEvent(). Thanks for the quick reply.