Page 1 of 1

WindowMouseButton()

Posted: Tue Sep 09, 2014 9:56 am
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

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 10:40 am
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?

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 11:41 am
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

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 11:47 am
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().

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 11:58 am
by infratec
A single call to ExamineMouse() switches off the mouse cursor.

Bernd

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 12:05 pm
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.

Re: WindowMouseButton()

Posted: Tue Sep 09, 2014 1:15 pm
by IdeasVacuum
...I thought that too :mrgreen:
Certainly, owner-drawn buttons etc on a CanvasGadget is the way to go - it's much easier.

Re: WindowMouseButton()

Posted: Wed Sep 10, 2014 6:00 am
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

Re: WindowMouseButton()

Posted: Wed Sep 10, 2014 6:26 am
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.

Re: WindowMouseButton()

Posted: Wed Sep 10, 2014 6:31 am
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.

Re: WindowMouseButton()

Posted: Wed Sep 10, 2014 6:34 am
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: