Mouse management with OpenWindow and OpenWindowedScreen (HELP)

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Mouse management with OpenWindow and OpenWindowedScreen (HELP)

Post by jak64 »

Hello,
For 1 year that I use Purebasic, I still did not understand how to manage the mouse in front of an "OpenWindowedScreen" screen included in an "OpenWindow" window. In the small program that I put below, I cannot click on the X which is at the top right to close the program or on _ to minimize the window.
I want to redefine the mouse cursor with a sprite that will be used in the "OpenWindowedScreen" window but I want to be able to click on the X of the "OpenWindow" window to close the game or on _ to minimize the window.

Thank you for your invaluable help.

Regards,
Jacques Joly

Code: Select all

;========== Gestion de la souris avec fenêtre "OpenWindow" et fenêtre "OpenWindowedScreen"
;= QUESTION :
;= Comment gérer la souris pour pouvoir cliquer sur le X de la fenêtre Windows afin de fermer la fenêtre ???????????

EnableExplicit
Global event.i
Global x_pave.w, y_pave.w
Global police.w

;=---------- Initialisation des environnements nécessaires
If InitSprite()=0 : MessageRequester("Erreur", "InitSprite() non initialisé !"):End:EndIf
If InitMouse()=0 : MessageRequester("Erreur", "InitMouse() non initialisé !"):End:EndIf

;=---------- Création de la fenêtre Windows
OpenWindow(0, 0, 0, 800,600, "Gestion souris avec fenêtre (OpenWindow) et fenêtre (OpenWindowedScreen) avec sprites", #PB_Window_ScreenCentered | #PB_Window_SystemMenu |  #PB_Window_MinimizeGadget)

;=---------- Création de la fenêtre de jeu
OpenWindowedScreen(WindowID(0),0,0,800,600,1,0,0)

;=---------- Création du sprite cercle
CreateSprite(0,50,50,#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(0))
Circle(25, 25, 24, #Yellow)
StopDrawing()

;=---------- Création du sprite pavé
CreateSprite(1,15,15,#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(1))
Box(0, 0, 15,15, #White)
StopDrawing()

;=---------- Chargement de la police
police=LoadFont(0, "Calibri", 24)

;=---------- Boucle
  Repeat    
    Repeat
      event = WindowEvent()
      Select event 
        Case #PB_Event_CloseWindow
          End 
      EndSelect
    Until event = 0
    
    ClearScreen(#Blue)
    ExamineMouse()
    x_pave=MouseX()
    y_pave=MouseY()
    DisplayTransparentSprite(0,100,100)
    DisplayTransparentSprite(1,x_pave,y_pave)
    ;=---------- Vérifier si collision entre le curseur (pavé) et le cercle
    If SpriteCollision(0,100,100,1,x_pave,y_pave)
      StartDrawing(ScreenOutput()) 
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(police)
      DrawText(100,300,"Collision",#Black)
      StopDrawing()
    EndIf
    FlipBuffers()
  ForEver
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Mouse management with OpenWindow and OpenWindowedScreen (HELP)

Post by STARGÅTE »

It is not as easy if the windows cursor should not has been shown over the sprite cursor.
Then you have to use WIn-API to control the visibility of the windows cursor like here:

Code: Select all

Enumeration
	#Window
	#Sprite
EndEnumeration

UsePNGImageDecoder()
InitSprite()

OpenWindow(#Window, 0, 0, 960, 540, "Mouse", #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window))
CatchSprite(#Sprite, ?draw_calligraphic, #PB_Sprite_AlphaBlending)

Repeat
	
	Repeat
		
		Select WindowEvent()
			Case #PB_Event_None
				Break
			Case #PB_Event_CloseWindow
				Break 2
		EndSelect
		
		Define CursorInfo.CURSORINFO\cbSize = SizeOf(CURSORINFO)
		GetCursorInfo_(@CursorInfo)
		If WindowMouseX(#Window) = -1 And WindowMouseY(#Window) = -1 And CursorInfo\flags = 0
			ShowCursor_(#True)
		ElseIf WindowMouseX(#Window) <> -1 And WindowMouseY(#Window) <> -1 And CursorInfo\flags <> 0
			ShowCursor_(#False)
		EndIf
		
	ForEver
	
	ClearScreen($000000)
	
	DisplayTransparentSprite(#Sprite, WindowMouseX(#Window)-6, WindowMouseY(#Window)-28)
	
	FlipBuffers()
	
ForEver

End

DataSection
	draw_calligraphic:
	Data.q $0A1A0A0D474E5089,$524448490D000000,$2000000020000000,$7A7A730000000608,$58457419000000F4
	Data.q $72617774666F5374,$2065626F64410065,$6165526567616D49,$00003C65C9717964,$DA78544144498E05
	Data.q $7E1867534C6B57B4,$A82C1685E873DB4E,$323710E8711960A0,$D188D13454E73274,$9908EA25BA19234D
	Data.q $B2CB6FF659350FDB,$B659668FF19B8B2D,$8A1B25E3441965CC,$0803321B2CB08288,$A162E502BB372A13
	Data.q $57DB7397A72D0BD0,$25F74650B10C3F3C,$7DE7DE6F7CE69A5F,$92492873BE7DE7DE,$662C058514517330
	Data.q $37E868C09F4699CB,$76E8851D6F280CB2,$133C94BBBD3E64FD,$FD85EB862053E666,$CC2F6BDECE3B7427
	Data.q $50AC8B9C5A7F495F,$38A8A08CBCF64A37,$005313159E6B1E4A,$B371AEB0559EBF5D,$8EF688227D85AF51
	Data.q $1A74C568340BB007,$ECCFCC315462B3F5,$1A3175353275B6FC,$4C4971FBE8944A5D,$747AA5A60E49A2C3
	Data.q $CE7B940056362846,$3E03172FB93E5C35,$981565750E1A3911,$7569D022F42EFD58,$EFED8C80098C0C44
	Data.q $63FBCDE326B2BD24,$D42681E9115E6C02,$082540D6AB41562A,$0D0B1B148D814490,$5ECC6FD64F8A1BD0
	Data.q $6A3C4C470E31E68E,$860603BD9FC0EFEA,$E4CCF80F81F3EF02,$B2FD6C50007FF89C,$3C34E5571BBDC3FA
	Data.q $AB777D0A8A73610E,$8240429D085E94D0,$FEC7BC13A8F7095A,$E1B5001AC7CB3927,$529CE5D65B5937F8
	Data.q $8CD36C247AB0B4EB,$A16B69704EBDF2E3,$1FEA1831C435CD27,$6307CE4B52D6DF81,$FCECC617C95D600A
	Data.q $C38D8D7D427CAE82,$5F605FAADA889341,$44A302444A5A187F,$2DBDFA7761C1251C,$B4004B170849BA57
	Data.q $68594DE2EFF0BF29,$768FD05032CBC222,$54893E29089BDAA2,$063C0FB0D1C7FE4F,$2EC3FBCE45BD1DEF
	Data.q $D6AD8FD174002B18,$A18139B5A4D7DBA3,$AEAB28FC933B0929,$A78FABA6FB4BD4C7,$0208EB561A7FD229
	Data.q $002C520065E735DD,$64148F59B7CE07D0,$1444BB3054FB75A7,$99478E56BC680BA5,$BD20AE491C085085
	Data.q $5B1E9E93AE4D4157,$CFFEA46746E3AA60,$0B5974AFF7E807D7,$571E8D2C2CD332F3,$B8CDDAD09593EB08
	Data.q $F14741326823463E,$AC07EA9037BD2788,$537123BF7A38218F,$AEC7D00C3E602691,$2D9A7579BDBDF574
	Data.q $6C464890E1E2C0C8,$7D81AE2A171FB277,$85467A12BD5CB0F5,$57707D9D1D128D04,$B47F448D9FA61C7D
	Data.q $95D9DF343F455400,$3C3DFA06ECDCB997,$AEEA3CA9BD9EA4FC,$005084314E6B0CFE,$78C4940027BC221A
	Data.q $44B1237E3B9AF478,$CD1973246ACC0825,$B733720A4BABAF8E,$4101015E830629F8,$A87D9CE20562FE44
	Data.q $0289409925C0671A,$C2AFD30E8EB9240C,$213FA7F57C2B0E83,$B8802D180E48BEF1,$091BA556E715B494
	Data.q $894BC644828212ED,$B82254552E2C89AD,$4E461A1050B830F1,$DB41FF5B93D7E84E,$1FAB2837D13F0B43
	Data.q $88F3DE7891A0010B,$5AEF64B025E01F50,$B959E8ABEA8FE1C2,$944F57B48A7A9A67,$C0E2C8FF549E63AF
	Data.q $6CEF58939F155FF0,$E954FB7880168BE2,$BB5A39E9D6ED7BC1,$F30F6E18C0848148,$CAE9F9E210339210
	Data.q $4B5340B413F2B2F5,$FAA68C6F2E6D804F,$C5CDBE7FD990C6B9,$492DAD063AAF46BA,$BD71E14A6DD652CB
	Data.q $85C703DEF2A622EC,$78C6061A401254A0,$7409BEE68FD64435,$0EC92762433EDC35,$E852C6756E80142D
	Data.q $9E071E58268A33B2,$359A4D09B0F431A3,$CFDA083EA90C9410,$5EFEFEFDC16BD6C2,$83736181AD3FF0F7
	Data.q $A1693E2CAD42F9B7,$62263460A44E6800,$D0D6513BC8903259,$2A1658E0E3F5F10C,$CB7B70C4E1CE014A
	Data.q $3DE722BE0FF281CD,$CDF39B35C0B51364,$EA25EAFEE4BD09CB,$8C3ADDEB43E2943F,$441A64710F801464
	Data.q $548D9F5F2C2A4462,$7DB1997B496EFC63,$D5B47C3A6C9156FF,$AD7D9B01A1C229CF,$187BBEA176A84FC6
	Data.q $AB4F332211178AE2,$78C04541C6D44854,$3B6F92A8A2885927,$380F8062E7C93E6E,$BC6D2F9E1AE46E06
	Data.q $1C1F82B6CB430B79,$3ED0860DD8482044,$2D5D7AD88F716832,$EBFFF7F55DBD1715,$979B13B8006CC57B
	Data.q $BF9535750F949578,$2E9DD4F53B1E56D7,$65CCF7BDCAF67DCA,$D334F966AB5F11B9,$CA80EB549A4D7A1F
	Data.q $A9E59771F1F6E356,$7512064E6BDC9224,$A3F280C48BE667DF,$B98340C3DCCFD1D2,$8B1AA57E1E4900E4
	Data.q $EE6688C2BDE6D9C6,$4D7F0A18389C07B0,$8B1559F48A6D91E4,$D9463F27A05A917C,$D1002B14AFF29CCD
	Data.q $2E63DC911B0E8068,$006015FEBD3F8CD6,$CCC42186CD87926C,$444E454900000000,$00000000826042AE
EndDataSection
An other way is to use the ReleaseMouse() function, and switch between ScreenMouse and windows mouse each time, when the cursor enters or leaves the windowed screen.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Mouse management with OpenWindow and OpenWindowedScreen (HELP)

Post by jak64 »

Hi,
Thanks for the code but I don't understand a thing ...
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Mouse management with OpenWindow and OpenWindowedScreen (HELP)

Post by jak64 »

Hi,
I added some of your code to my program. I also added the variable "display_cursor_game".
The program works fine but it does not detect mouse clicks.
I have commented out the lines which should detect the mouse click. If we uncomment these lines, then the "wondows" cursor (the arrow) no longer appears when we go to the top of the window.

Thank you for your help.

Code: Select all

;========== Gestion de la souris avec fenêtre "OpenWindow" et fenêtre "OpenWindowedScreen"
;= QUESTION :
;= Comment gérer la souris pour pouvoir cliquer sur le X de la fenêtre Windows afin de fermer la fenêtre ???????????

EnableExplicit
Global event.i
Global x_pave.w, y_pave.w
Global police.w
Global display_cursor_game.b

;=---------- Initialisation des environnements nécessaires
If InitSprite()=0 : MessageRequester("Erreur", "InitSprite() non initialisé !"):End:EndIf
If InitMouse()=0 : MessageRequester("Erreur", "InitMouse() non initialisé !"):End:EndIf

;=---------- Création de la fenêtre Windows
OpenWindow(0, 0, 0, 800,600, "Gestion souris avec fenêtre (OpenWindow) et fenêtre (OpenWindowedScreen) avec sprites", #PB_Window_ScreenCentered | #PB_Window_SystemMenu |  #PB_Window_MinimizeGadget)

;=---------- Création de la fenêtre de jeu
OpenWindowedScreen(WindowID(0),0,0,800,600,1,0,0)

;=---------- Création du sprite cercle
CreateSprite(0,50,50,#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(0))
Circle(25, 25, 24, #Yellow)
StopDrawing()

;=---------- Création du sprite pavé
CreateSprite(1,15,15,#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(1))
Box(0, 0, 15,15, #White)
StopDrawing()

;=---------- Chargement de la police
police=LoadFont(0, "Calibri", 24)

;=---------- Boucle
Repeat    
  
  ;=---------- Début du code transmis par STARGÅTE sur le forum anglais
  Repeat
    Select WindowEvent()
      Case #PB_Event_None
        Break
      Case #PB_Event_CloseWindow
        Break 2
    EndSelect
    Define CursorInfo.CURSORINFO\cbSize = SizeOf(CURSORINFO)
    GetCursorInfo_(@CursorInfo)
    If WindowMouseX(0) = -1 And WindowMouseY(0) = -1 And CursorInfo\flags = 0
      ShowCursor_(#True)
      display_cursor_game=#False ; Flag que j'ai ajouté
    ElseIf WindowMouseX(0) <> -1 And WindowMouseY(0) <> -1 And CursorInfo\flags <> 0
      ShowCursor_(#False)
      display_cursor_game=#True ; Flag que j'ai ajouté
    EndIf
  ForEver
  ;=---------- Fin du code transmis par STARGÅTE sur le forum anglais
  
  ClearScreen(#Blue)
  DisplayTransparentSprite(0, 100, 100)
  If display_cursor_game ; test que j'ai ajouté
    DisplayTransparentSprite(1, WindowMouseX(0), WindowMouseY(0))
  EndIf
  
  ;=---------- Vérifier si collision entre le curseur (pavé) et le cercle
  If SpriteCollision(0,100,100,1,WindowMouseX(0),WindowMouseY(0))
    StartDrawing(ScreenOutput()) 
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(police)
    DrawText(100,300,"collision with the circle",#Black)
    StopDrawing()
  EndIf
  
  ;=---------- Vérifier si clic sur le cercle
  ;ExamineMouse()
  ;If SpritePixelCollision(0,100,100,1,WindowMouseX(0),WindowMouseY(0)) And MouseButton(#PB_MouseButton_Left)
  ;  StartDrawing(ScreenOutput()) 
  ;  DrawingMode(#PB_2DDrawing_Transparent)
  ;  DrawingFont(police)
  ;  DrawText(100,400,"You clicked on the circle",#Black)
  ;  StopDrawing()
  ;EndIf

  FlipBuffers()
ForEver
Post Reply