Page 1 of 1

Reading mouse Coords while still seeing Windows pointer.

Posted: Wed Nov 08, 2023 10:27 pm
by matalog
I am just starting a program that will have a large grid which the user can click, a square will alternate from black to white and back, and basically pixel art aid.

At this early stage, I cannot seem to get the mouse to stay visible, while doing what I require.

With the following program it will keep the mouse pointer visible, but not read its inputs, and if I comment out line 45 (the Releasemouse(#True) line) then the mouse inputs are read correctly, but I no longer see where the mouse is, and won't be able to see which squares I am hovering over.

Code: Select all

#width=1000
#height=1000

spwidth=24
spheight=21
Global.i spBsiz=24

InitMouse()
InitSprite()
InitKeyboard()

OpenWindow(0,10,10,#width,#height,"Sprite Creator")

OpenWindowedScreen(WindowID(0),0,0,#width,#height)

StartDrawing(ScreenOutput())
Box(0,0,#width,#height,#White)

For y=1 To spheight*spBsiz Step 24
  For x=1 To spwidth*spBsiz Step 24
    Line(x,0,1,y,RGB(0,0,0))
    Line(0,y,x,1,RGB(0,0,0))
  Next
Next

If MouseButton(#PB_MouseButton_Left  )
  Circle(MouseX(),MouseY(),100)
EndIf
StopDrawing()

Repeat
  ExamineMouse()
  mb=MouseButton(#PB_MouseButton_Left)
  mx=MouseX()
  my=MouseY()
  
  StartDrawing(ScreenOutput())
If mb
  Debug mx
  Debug my
  Circle(MouseX(),MouseY(),100)
EndIf
StopDrawing()
  ReleaseMouse(#True)
  ExamineKeyboard()                                                        
  
  Repeat                                                                
    Event = WindowEvent()
    Select Event 
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
      Case #PB_Event_CloseWindow
        End 
    EndSelect
  Until Event = 0
  
  ; FlipBuffers()                                                             


 If KeyboardPushed(#PB_Key_Escape):quit=1:EndIf

Until quit=1
Any ideas on how to see the mouse pointer moving and also be able to read the inputs from it?

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Wed Nov 08, 2023 11:06 pm
by firace
Here's another approach, if you're OK with Windows-only:

Code: Select all

Procedure GetMouseX(gadget)    ;;  by griz
  GetCursorPos_(mouse.POINT) 
  MapWindowPoints_(0,GadgetID(gadget),mouse,1) 
  ProcedureReturn mouse\x 
EndProcedure 

Procedure GetMouseY(gadget) 
  GetCursorPos_(mouse.POINT) 
  MapWindowPoints_(0,GadgetID(gadget),mouse,1) 
  ProcedureReturn mouse\y 
EndProcedure 


#CELLSIZE = 16
#XXX = 16
#YYY = 20

AreaX  = #CELLSIZE * #XXX
AreaY  = #CELLSIZE * #YYY


OpenWindow( 0,0,0,AreaX+160,AreaY+2,"PixelToy 00",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 
SetWindowColor (0,0)

hImg = CreateImage(0,AreaX,AreaY) ; ,24,#White) 

ImageGadget(2,2,2,AreaX,AreaY,hImg)  

;; init palette 
CanvasGadget(3,AreaX + 50 , 4, 100 ,AreaY - 8)  
StartDrawing(CanvasOutput(3))

Box(0,0, 100 , AreaY , $191919)

For gg = 0 To 8
  R = Random(255)
  G = Random(255)
  B = Random(255)
  color = (RGB(R, G, B))
  Box(20, 18 + gg*30 , 20, 20, color)
Next 

For gg = 0 To 8
  R = Random(255)
  G = Random(255)
  B = Random(255)
  color = (RGB(R, G, B))
  Box(50, 18 + gg*30 , 20, 20, color)
Next 

selectedColor = color 
Box(1, 1, 3, WindowHeight(0), selectedColor)

StopDrawing()

;; init palette end 

Procedure DrawPixel(selectedColor)
  mx=GetMouseX(2) : my=GetMouseY(2) 
  mxx=(mx/#CELLSIZE) * #CELLSIZE : myy=(my/#CELLSIZE) * #CELLSIZE 
  StartDrawing(ImageOutput(0))  
  RoundBox(mxx,myy, #CELLSIZE - 3 ,#CELLSIZE - 3,2,2, selectedColor) 
  StopDrawing() 
  SetGadgetState(2, ImageID(0))
EndProcedure   

Repeat 
  Select WaitWindowEvent(): 
    Case #PB_Event_CloseWindow: End 
      
    Case #WM_LBUTTONDOWN
      DrawPixel(selectedColor)
      pendown=1
      
    Case #WM_RBUTTONDOWN
      delmode=1
      DrawPixel(0)
      pendown=1
      
    Case #WM_LBUTTONUP, #WM_RBUTTONUP
      pendown=0
      delmode=0
      
    Case #WM_MOUSEMOVE 
      If pendown=1
        mx=GetMouseX(2) : my=GetMouseY(2) 
        mxx=(mx/#CELLSIZE) * #CELLSIZE : myy=(my/#CELLSIZE) * #CELLSIZE 
        
        StartDrawing(ImageOutput(0)) 
        If delmode
          RoundBox(mxx,myy, #CELLSIZE - 3, #CELLSIZE - 3,2,2, 0) 
        Else          
          RoundBox(mxx,myy, #CELLSIZE - 3, #CELLSIZE - 3,2,2, selectedColor) 
        EndIf 
        
        StopDrawing() 
        SetGadgetState(2, ImageID(0))
      EndIf
      
    Case #PB_Event_Gadget 
      Select EventGadget() 
        Case 3
          If EventType() = #PB_EventType_LeftClick
            StartDrawing(CanvasOutput(3))
            selectedColor = Point(GetMouseX(3), GetMouseY(3))
            Box(1, 1, 3, WindowHeight(0), selectedColor)
            StopDrawing()
          EndIf 
        Case 5 
          End 
      EndSelect 
  EndSelect 
ForEver 

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Thu Nov 09, 2023 1:39 am
by matalog
Thanks. Yes, I will be using Windows only, but I would like to know what I did wrong with my code. Of course!

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Thu Nov 09, 2023 12:51 pm
by Caronte3D
With your code modified:

Code: Select all

#width  = 1000
#height = 1000

spwidth         = 24
spheight        = 21
Global.i spBsiz = 24

;InitMouse()
InitSprite()
InitKeyboard()

OpenWindow(0, 10, 10, #width, #height, "Sprite Creator")

OpenWindowedScreen(WindowID(0), 0, 0, #width, #height)

For f = 1 To 2
  
  StartDrawing(ScreenOutput())
  Box(0, 0, #width, #height, #White)
  
  For y = 1 To spheight * spBsiz Step 24
    For x = 1 To spwidth * spBsiz Step 24
      Line(x, 0, 1, y, RGB(0, 0, 0))
      Line(0, y, x, 1, RGB(0, 0, 0))
    Next
  Next
  StopDrawing()
  FlipBuffers()
Next


done = 0

Repeat
  
  
  If mb And Not done
    
    For f = 1 To 2
      StartDrawing(ScreenOutput())
      Debug mx
      Debug my
      px = (12 + (mx - mx % 24))
      py = (12 + (my - my % 24))
      If Point(px, py) = #Black
        Circle(px, py, 10, #White)
      Else
        Circle(px, py, 10, #Black)
      EndIf
      StopDrawing()
      
      FlipBuffers()
    Next
    done = 1
    mb   = 0
  Else
    If Not mb
      done = 0
    EndIf
  EndIf
  ExamineKeyboard()
  
  
  Repeat
    Event = WindowEvent()
    
    If Event = #PB_Event_LeftClick
      mb = 1;MouseButton(#PB_MouseButton_Left)
      mx = WindowMouseX(0)
      my = WindowMouseY(0)
    EndIf
        
    Select Event
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  FlipBuffers()

  If KeyboardPushed(#PB_Key_Escape):quit = 1:EndIf
  
Until quit = 1

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Thu Nov 09, 2023 3:58 pm
by AZJIO
Wow, this is a level editor for a game where there is a board and a ball flying from below and you have to catch it with the board and it flies and knocks down bricks.

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Thu Nov 09, 2023 4:06 pm
by Caronte3D
:lol: :lol:

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Thu Nov 09, 2023 10:05 pm
by matalog
Thanks Caronte, I see you have added a few steps too, thanks.

What was wrong with my usage of the mouse commands? You see, I want to be able to hold the mouse button down and drag along, then cells will flip to other colour, and the method you have used with If Event = #PB_Event_LeftClick doesn't allow to check if mousebutton is currently down, which is why I was using Mousebutton().

Does anyone know how to properly use MouseButton() and Mousex() while having the mouse pointer visible?

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Fri Nov 10, 2023 4:35 am
by Jeff8888
You may need to use binding (see pb help file)
Here is a demo program I wrote that does this along x axis

Code: Select all

;Demo program to plot x,y data using scaling 
;Uses keyboard and scroll bar input to change plot

Global xsize.f,ysize.f,xleft.f,xright.f,ylow.f,yhigh.f,xratio.f,yratio.f,BottomMargin.f
Global scrx,scry,scrw,scrh

Enumeration 100
  #HBar
  #HScrollPoint
  #xytext
  #tbox
EndEnumeration


Procedure ScrnScale(xl.f,xh.f,yl.f,yh.f)  ;Calculate Scaling Parameters, call before ScrnX or ScrnY and after opening a screen
                                          ;Parameters are x1,x2,y1,y2 of Screen to plot on
  xsize=ScreenWidth()-1                   ;Note screen width is number of pixels
  ysize=ScreenHeight()-BottomMargin-1     ;-1 because OpenWindowedScreen(100,100,10,10) screen is 10 wide
  xleft=xl : xright=xh                    ;but there will be 11 pixels 0,1,...10
  ylow=yl : yhigh=yh
  xratio=xsize/(xright-xleft)
  yratio=ysize/(yhigh-ylow)
EndProcedure

Procedure.f ScrnX(x.f)   ;Return scaled x value of Screen
  ProcedureReturn (x-xleft)*xratio
EndProcedure

Procedure.f ScrnY(y.f)    ;Return scaled y value of Screen
  ProcedureReturn ysize-(y-ylow)*yratio
EndProcedure

Procedure BindHScroll()              ;Needed to display value while scrolling x axis
  If IsGadget(#HBar)=0 :ListViewGadget(#HBar,300,620,125,20) : EndIf
  ClearGadgetItems(#HBar)
  AddGadgetItem(#HBar,0,"New End Point = "+Str(GetGadgetState(#HScrollPoint)))
EndProcedure

NumberofElements=2000                 ;max 10,000 due to scroll bar limit
NumberofCycles=4
Dim SineWave.f(NumberofElements)
PiScale.f=2*3.1416*NumberofCycles/NumberofElements
For i=0 To NumberofElements          ;Generate a sine wave
  SineWave(i)=Sin(i*PiScale)
Next
endpoint=NumberofElements
ymin=-1: ymax=1 
BottomMargin=25           ;this space can be used for botttom tick marks and x axis notation

Font1 = LoadFont(#PB_Any, "Arial"  ,  10)
Font2 = LoadFont(#PB_Any, "Arial"  ,  14)

If InitSprite() = 0 Or InitKeyboard()=0
  MessageRequester("Error", "Can't open screen & sprite environment!", 0)
  End
EndIf

OpenWindow(0, 0, 0, 1000, 800, "Show plotting using scaling", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(0,$E9F2C7)
TextGadget(50,300,50,400,35,"Hit D,Y,R and see what happens"+#CRLF$+"click on graph to display x,y data")
SetGadgetColor(50,#PB_Gadget_BackColor,$E9F2C7):SetGadgetFont(50,font2)
TextGadget(51,250,650,400,15,"Move slider and observe")
SetGadgetColor(51,#PB_Gadget_BackColor,$E9F2C7):SetGadgetFont(51,font2)

OpenWindowedScreen(WindowID(0), 100, 100, 501, 501)
ScrollBarGadget(#HScrollPoint,100, 600, 500, 20, 0, endpoint, 0)
SetGadgetState(#HScrollPoint,endpoint)
BindGadgetEvent(#HScrollPoint, @BindHScroll())                          ;so can read scroll bar while scrolling
TextGadget(#tbox,50,50,20,15,"00")
AddWindowTimer(0,1,1000)
Gosub plot                                   ;draw first plot, then wait for input
Repeat
  WaitWindowEvent()
  Event = WindowEvent()
  If event= #PB_Event_CloseWindow
    End
  EndIf
  If event = #PB_Event_Gadget
    If EventGadget() = #HScrollPoint         ;xaxis scroll moved change lastpoint
      If IsGadget(#HBar)                     ;only display mass box while scrolling
        FreeGadget(#HBar)
      EndIf                           
      endpoint=GetGadgetState(#HScrollPoint) ;move end of x axis to scroll point
      Gosub plot                            
    EndIf
  EndIf
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_D)            ;half x range
    endpoint=endpoint/2
    SetGadgetAttribute(#HScrollPoint,#PB_ScrollBar_Maximum,endpoint)
    SetGadgetState(#HScrollPoint,endpoint)
    Gosub plot
  ElseIf KeyboardReleased(#PB_Key_Y)        ;double y range
    ymin=ymin*2 :ymax=ymax*2
    Gosub plot
  ElseIf KeyboardReleased(#PB_Key_R)        ;reset x,y ranges
    ymin=-1:ymax=1 :endpoint=NumberofElements
    SetGadgetAttribute(#HScrollPoint,#PB_ScrollBar_Maximum,endpoint)
    SetGadgetState(#HScrollPoint,endpoint)
    Gosub plot
  ElseIf Event=#PB_Event_LeftClick      ;Show info about point clicked on graph display
    x.f=WindowMouseX(0)-100                                       
    pt=Round(endpoint*x/ScreenWidth(),#PB_Round_Nearest)
    If  pt>=0 And pt=<endpoint
      TextGadget(#xytext, 300, 110, 50, 50,"X= "+Str(pt)+#CRLF$+"Y= "+StrD(sinewave(pt),3))
     SetGadgetColor(#xytext,#PB_Gadget_BackColor,$FFFFFF)
    EndIf
  EndIf
  If event=#PB_Event_Timer   ;note: no updates during scrolling
    SetGadgetText(#tbox,Str(a))
    a=a+1
  EndIf
ForEver

Plot:
ClearScreen($FFFFFF)
StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_Transparent | #PB_2DDrawing_Outlined)
LineXY(0,0,500,500,0)               ;draw line not using scaling procedures, note lower right corner is 500,500
ScrnScale(0,500,0,500)
LineXY(ScrnX(0),Scrny(0),Scrnx(500),Scrny(0),0)  ;Draw axes, lower right corner is  now 500,0
LineXY(ScrnX(0),Scrny(0),Scrnx(0),ScrnY(500),0)
LineXY(ScrnX(0),Scrny(500),Scrnx(500),Scrny(500),0)
LineXY(ScrnX(500),Scrny(0),Scrnx(500),Scrny(500),0)

Ticksize=0.015*xsize                       ;draw tick marks
For i=1 To 9
  xpoint.f=i*xsize/10.0: ypoint.f=i*ysize/10.0 
  LineXY(xpoint,ysize+TickSize,xpoint,ysize,0)    ;bottom ticks
  LineXY(0,ypoint,TickSize,ypoint,0)              ;left ticks
  LineXY(xpoint,0,xpoint,TickSize,0)              ;top ticks
  LineXY(xsize,ypoint,xsize-TickSize,ypoint,0)    ;right ticks
Next
ScrnScale(0,100,0,100) 
LineXY(ScrnX(0),Scrny(0),Scrnx(100),Scrny(100),$41FC32)  ;Draw a diagonal line
ScrnScale(0,endpoint,ymin,ymax)                         ;set scale to plot sine wave values
lastx=scrnx(0):lasty=scrny(SineWave(0))
FrontColor($FF0000)
For i=0 To endpoint
  xpoint=scrnx(i):ypoint=scrny(SineWave(i))
  LineXY(lastx,lasty,xpoint,ypoint)
  lastx=xpoint:lasty=ypoint
Next
DrawingFont(FontID(font1))                      ;label axes
tsize=TextWidth("1234")
DrawText(3,480,"0",0)
DrawText(250-tsize/2,480,RSet(Str(endpoint/2),4),0)
DrawText(495-tsize,480,RSet(Str(endpoint),5),0)
DrawText(3,3,StrF(ymax,2),0)
DrawText(3,460,StrF(ymin,2),0)
StopDrawing()
FlipBuffers()
Return
End

Re: Reading mouse Coords while still seeing Windows pointer.

Posted: Fri Nov 10, 2023 3:08 pm
by Caronte3D
matalog wrote: Thu Nov 09, 2023 10:05 pm ...I want to be able to hold the mouse button down and drag along, then cells will flip to other colour, and the method you have used with If Event = #PB_Event_LeftClick doesn't allow to check if mousebutton is currently down, which is why I was using Mousebutton().
Well... it's called programming, so... you have to find a way to make your program do what you want.
Amyway here is a (dirty) starting point:

Code: Select all

#width  = 1000
#height = 1000

spwidth         = 24
spheight        = 21
Global.i spBsiz = 24

;InitMouse()
InitSprite()
InitKeyboard()

OpenWindow(0, 10, 10, #width, #height, "Sprite Creator")

OpenWindowedScreen(WindowID(0), 0, 0, #width, #height)

For f = 1 To 2
  
  StartDrawing(ScreenOutput())
  Box(0, 0, #width, #height, #White)
  
  For y = 1 To spheight * spBsiz Step 24
    For x = 1 To spwidth * spBsiz Step 24
      Line(x, 0, 1, y, RGB(0, 0, 0))
      Line(0, y, x, 1, RGB(0, 0, 0))
    Next
  Next
  StopDrawing()
  FlipBuffers()
Next


Repeat
  
    mx = WindowMouseX(0)
    my = WindowMouseY(0)  
    
    If spheight * (spBsiz-1)<=my Or spwidth * (spBsiz-1)<=mx
      mx=0
      my=0
    EndIf
  
  If mb And mx>0 And my>0
    
    px = (12 + (mx - mx % 24))
    py = (12 + (my - my % 24))
    If (lastPx <> px Or lastPy <> py)
      For f = 1 To 2
        
        StartDrawing(ScreenOutput())        
        If Point(px, py) = #Black
          Circle(px, py, 10, #White)
        Else
          Circle(px, py, 10, #Black)
        EndIf
        StopDrawing()
        
        FlipBuffers()
      Next
    EndIf
    lastPx = px
    lastPy = py
    
  EndIf
  ExamineKeyboard()
  
  
  Repeat
    Event = WindowEvent()
    
    If Event = #WM_LBUTTONDOWN 
      mb = 1   
      
    EndIf
    If Event = #WM_LBUTTONUP
      mb = 0   
    EndIf
    
    Select Event
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  FlipBuffers()
  
  If KeyboardPushed(#PB_Key_Escape):quit = 1:EndIf
  
Until quit = 1