WindowMouseButton()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

WindowMouseButton()

Post by infratec »

Hi,

I'm just programming something where I need a small stripe of a screen window insode a normal window.
Inside this stripe are sprites which are working as buttons.

WindowMouseX() and Y() are working to get the position.
But I have no chance to get the state of the buttons.

For that I need InitMouse(), ExamineMouse() and MouseButton()

Unfortunately if I use InitMouse(), the mouse cursor is off when the first call of ExamineMouse()
happens.

So a WindowMouseButton() procedure would be very nice.

Bernd
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: WindowMouseButton()

Post by PB »

> the mouse cursor is off when the first call of ExamineMouse() happens

Why not do a single dummy call to ExamineMouse() to prime it?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: WindowMouseButton()

Post by DK_PETER »

Why not use the canvas? I'm an avid fan of screen(), but sometimes there's a better way.

Perhaps something like this? Then you got all the eventtypes available from the canvasgadget too.
Edit: added the selected state to code

Code: Select all

Structure my_buttons
  x.i
  y.i
  im.i[3]
  selected.i
EndStructure

Global Dim bt.my_buttons(8)
Global bw.i = 638/8

Procedure.i buttoncanvas()
  Protected newnum.i, et.i
  newnum = GetGadgetAttribute(0, #PB_Canvas_MouseX)
  
  et = EventType()
  
  Select et
    Case #PB_EventType_MouseMove
      StartDrawing(CanvasOutput(0))
      For x = 0 To 7
        If newnum > bt(x)\x And newnum < bt(x)\x+bw
          If bt(x)\selected = 1
            DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
          Else            
            DrawImage(ImageID(bt(x)\im[1]),bt(x)\x,0)
          EndIf
        Else
          If bt(x)\selected = 1
            DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
          Else            
            DrawImage(ImageID(bt(x)\im[0]),bt(x)\x,0)
          EndIf
        EndIf
      Next x
      StopDrawing()
    Case #PB_EventType_LeftClick
      StartDrawing(CanvasOutput(0))
      For x = 0 To 7
        If newnum > bt(x)\x And newnum < bt(x)\x+bw
          If et = #PB_EventType_LeftClick And bt(x)\selected = 0
            bt(x)\selected = 1
            DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
          Else
            DrawImage(ImageID(bt(x)\im[1]),bt(x)\x,0)
          EndIf
        Else
          DrawImage(ImageID(bt(x)\im[0]),bt(x)\x,0)
          bt(x)\selected = 0
        EndIf
      Next x
      StopDrawing()
  EndSelect
  
EndProcedure

Procedure.i ButtonEvents()
  If EventType() = #PB_EventType_LeftClick
    For x = 0 To 7
      If bt(x)\selected = 1
        Debug "Button " + Str(x)
      EndIf
    Next x
  EndIf
EndProcedure

Procedure.i CreateButtons()
  For x = 0 To 7
    With bt(x)
      \im[0] = CreateImage(#PB_Any, bw, 20) ;Normal
      \im[1] = CreateImage(#PB_Any, bw, 20) ;Hovered
      \im[2] = CreateImage(#PB_Any, bw, 20) ;Selected---Not used in example
      StartDrawing(ImageOutput(\im[0]))
      Box(0, 0, bw, 20, $777777)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $0)
      LineXY(1, 1, bw-2, 1, $AAAAAA)
      LineXY(1, 1, 1, 18, $AAAAAA)
      LineXY(1,18, bw-2, 18, $555555)
      LineXY(bw-2, 1, bw-2, 18, $555555)
      StopDrawing()
      
      StartDrawing(ImageOutput(\im[1]))
      Box(0, 0, bw, 20, $999999)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $0)
      LineXY(1, 1, bw-1, 1, $AAAAAA)
      LineXY(1, 1, 1, 18, $AAAAAA)
      LineXY(1,18, bw-1, 18, $555555)
      LineXY(bw-2, 1, bw-2, 18, $555555)
      StopDrawing()
      bt(x)\x = x*bw
      
      StartDrawing(ImageOutput(\im[2]))
      Box(0, 0, bw, 20, $CCCCCC)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $333333)
      LineXY(1, 1, bw-2, 0, $AAAAAA)
      LineXY(1, 1, 0, 18, $AAAAAA)
      LineXY(1,18, bw-2, 18, $555555)
      LineXY(bw-2, 1, bw-2, 18, $555555)
      StopDrawing()
      bt(x)\x = x*bw
      
    EndWith
  Next x
EndProcedure

CreateButtons()
OpenWindow(0, 0, 0, 640, 480, "Small buttons", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0, 2, 2,638,20)
BindGadgetEvent(0,@buttoncanvas())
buttoncanvas()
ButtonGadget(1, 40, 40, 150, 20, "Check selected button")
BindGadgetEvent(1, @ButtonEvents())

Repeat
  
  ev = WaitWindowEvent()
  
Until ev = #PB_Event_CloseWindow
Last edited by DK_PETER on Tue Sep 09, 2014 5:59 pm, edited 2 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: WindowMouseButton()

Post by Bisonte »

whats about the normal window events ?

Code: Select all

...

Event = WaitWindowEvent()

If Event = #PB_Event_LeftClick
  mx = WindowMouseX(Window)
  my = WindowMouseY(Window)
  If mx > Bx And mx < Bx + Bw 
  ....
But to create his own Gadgets, better use CanvasGadget().
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: WindowMouseButton()

Post by infratec »

A single call to ExamineMouse() switches off the mouse cursor.

Bernd
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: WindowMouseButton()

Post by PB »

> A single call to ExamineMouse() switches off the mouse cursor

Ah... when I read "mouse cursor is off" in your original post,
I thought you meant the mouse cursor's POSITION was off.
LOL! Now I understand what you mean.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: WindowMouseButton()

Post by IdeasVacuum »

...I thought that too :mrgreen:
Certainly, owner-drawn buttons etc on a CanvasGadget is the way to go - it's much easier.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: WindowMouseButton()

Post by netmaestro »

Your request is a good one, for sure. In the meantime though, is this any help?

http://purebasic.fr/english/viewtopic.p ... 68#p187468
BERESHEIT
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: WindowMouseButton()

Post by DK_PETER »

@netmaestro
There's always been the option to use ReleaseMouse().
Using mouseX() and mouseY() - you could simply release the mouse from screen, when the coords
are greater or lesser than - and simply grab the mouse again when coords (using WindowMouseX() and WindowMouseY())
are within screen coordinate area.

But how would you create a mouse function, which works on screen and window at the same time?
The Screen and Window are two completely different things.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: WindowMouseButton()

Post by netmaestro »

But how would you create a mouse function, which works on screen and window at the same time?
I'd gird up my loins, call on everything I've learned over the years, do a lot of research, consult with experts in the area, then I'd sit down, focus in and let Fred do it.
BERESHEIT
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: WindowMouseButton()

Post by DK_PETER »

netmaestro wrote:
But how would you create a mouse function, which works on screen and window at the same time?
I'd gird up my loins, call on everything I've learned over the years, do a lot of research, consult with experts in the area, then I'd sit down, focus in and let Fred do it.
You got off easy there.Not quite the answer I expected, but an answer nevertheless. :lol: :wink:
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Post Reply