Suggestion about mouse coordinates capture in a viewport

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Suggestion about mouse coordinates capture in a viewport

Post by Tourist Trap »

Hello,

When we set a viewport with VIEW command, the coordinates for all the functions that draws graphics are translated, and some clipping occurs. This is quite efficient, useful, nice. But the mouse coordinates keep relative to the main application window.
This has not to change, but it could be refined to make avaiable without user computation the coordinates of the mouse in any given user defined viewport without disallowing the standard getmouse. I was thinking of something like what is done with FILES but it can be in fact simpler because we can only have one user defined viewport besides the application screen.

Code: Select all

VIEW    (...)-(...), color1, color2

GETMOUSE [viewportNum = 0 or 1,] gmx, gmy........
I dont know if this would be easy but having the coordinates of the mouse calculated in the viewport would make the life easier. And even more, if it was able to take into account the other factors introduced by using WINDOW.

And in any case I don't see an easy way to obtain this behaviour by hand.

Thanks whatever for paying any attention to this.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Suggestion about mouse coordinates capture in a viewport

Post by D.J.Peters »

Code: Select all

dim as integer x1=100,y1=100,x2=440,y2=280

dim as integer mx,my,mb

screenres 640,480

view (x1,y1)-(x2,y2),2,7

while inkey()=""
  if GetMouse(mx,my,,mb) = 0 then
    if mx>=x1 andalso my>=y1 andalso mx<=x2 andalso my<=y2 then
      mx-=x1 : my-=y1
      locate 1,1 : print "x: " & mx & "  "
      locate 2,1 : print "y: " & my & "  "
    end if
  end if
  sleep 10
wend
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Suggestion about mouse coordinates capture in a viewport

Post by Tourist Trap »

D.J.Peters wrote:mx-=x1 : my-=y1
Yes but this is what I would like to have automated. It's in fact because if you add to this the extra management for a scrolling image within the viewport, this starts being busy. And with many viewport each with active content like buttons, it's just very much of thinking ;)

Getmouse 1, ... would maybe also have to call getmouse automatically at each VIEW bloc starting and after each viewport terminated, to keep always the variables up to date. The trick would be to bind VIEW and GETMOUSE - in this mode at least...
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Suggestion about mouse coordinates capture in a viewport

Post by D.J.Peters »

This works independent are VIEW enabled or not.

Joshy

Code: Select all

declare function GetContext cdecl alias "fb_hGetContext" as long ptr

sub getView(byref x1 as integer,byref y1 as integer, byref x2 as integer, byref y2 as integer)
  dim as long ptr p=GetContext()
  if p then
    dim as integer i=9
    #ifdef __FB_64BIT__
      i += 2
    #endif
    ' get view coords
    x1=p[i+0]:y1=p[i+1]:x2=x1+p[i+2]:y2=y1+p[i+3]
  else
    x1=0:y1=0
    screeninfo x2,y2
  end if
end sub


Function GetViewMouse ( ByRef x As Integer, ByRef y As Integer, ByRef wheel As Integer = 0, ByRef buttons As Integer = 0, ByRef clip As Integer = 0 ) As Long
  var result = getMouse(x,y,wheel,buttons,clip)
  ' is mouse in window ?
  if result=0 then
    dim as integer x1,y1,x2,y2
    getView(x1,y1,x2,y2)
    if x>=x1 andalso y>=y1  andalso x<=x2 andalso y<=y2 then
      x-=x1 : y-=y1
    else
      ' not inside the view or window
      x=-1 : y=-1
      result = 1
    end if
  end if
  return result
end function


dim as integer mx,my,mb

screenres 640,480

view (100,100)-(440,280),2,7

while inkey()=""
  if GetViewMouse(mx,my,,mb) = 0 then
    locate 1,1 : print "x: " & mx & "   "
    locate 2,1 : print "y: " & my & "   "
  else
    locate 1,1 : print "       "
    locate 2,1 : print "       "
  end if
  sleep 10
wend
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Suggestion about mouse coordinates capture in a viewport

Post by Tourist Trap »

D.J.Peters wrote:This works independent are VIEW enabled or not.
Really thanks, I'll test it soon.
It seems to be as needed! I'll try to rewrite my file reader dialog box with this. I don't get the trick but it seems the kind of magic that fits exactly.
Post Reply