Author Topic: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse  (Read 3803 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« on: June 28, 2019, 08:07:26 am »
Is it possible to get relative coordinates with a mouse over from a smaller _putimage on a bigger screen?

I have a larger main display which I place a smaller view port on the right side which is created with VIEW and WINDOW and has coordinate system thus:   WINDOW (-1000, 1000)-(1000, -1000)  I'm looking to be able to get those coordinates when the mouse is over that viewport

The project in question is: https://www.qb64.org/forum/index.php?topic=1429.0

In searching the wiki and forum I came across PMAP, which seems to be the key, but the coordinates returned are only for the main screen and won't register the relative coordinates contained in the _PUTIMAGE. It works well only when working with the image in isolation, but not once it is put to the bigger display.

Is this even possible, and/or am I missing something....other than my marbles... ;)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #1 on: June 28, 2019, 08:40:34 am »
Hi. Why not use directly _PUTIMAGE own? Something as this:

Code: QB64: [Select]
  1. image = _NEWIMAGE(1024, 768, 256) 'create image
  2. image2 = _NEWIMAGE(50, 50, 32)
  3. _DEST image
  4. CLS , 14
  5. CIRCLE (512, 384), 300, 1
  6. PAINT (512, 384), 1, 1
  7. _DEST image2
  8. CLS , &HFFFF0000
  9. LINE (15, 15)-(35, 35), &HFF00FF00, BF
  10.  
  11. SCREEN _NEWIMAGE(640, 480, 32) 'your screen
  12. _PUTIMAGE (220, 140)-(420, 340), image 'insert image to position 220,140
  13. PRINT "Bigger image than your screen is inserted. Press key..."
  14.  
  15. _PUTIMAGE (220, 140)-(420, 340), image2 'insert image to position 220,140
  16. PRINT "Smaller image than your screen is inserted. Press key..."
  17.  
  18.  
« Last Edit: June 28, 2019, 08:41:54 am by Petr »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #2 on: June 28, 2019, 01:59:03 pm »
Hi. Why not use directly _PUTIMAGE own? Something as this:

I'm looking to preserve the cartesian coordinate scheme in the graphics window to make the math simpler.

Now that you mention it, I suppose there's nothing preventing me from making the smaller view port 2000x2000, bypassing the whole WINDOW approach, and just using the _PUTIMAGE command to size it down to the main screen, but I assume that would still not address the mouseover issue.

Marked as best answer by OldMoses on June 28, 2019, 01:59:04 pm

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #3 on: June 28, 2019, 02:20:19 pm »
Sometimes you just have to do the math yourself.

Let’s say I’m running a program in a 1000x1000 window.  (Goofy size, but easy numbers.). Now, let’s say I designate the top right 250x250 area as a mini “preview” window of sorts, but I want to interact with it as a 1000x1000 coordinate window, as well...

The mouse position is relative to the main window, so if it’s halfway in that 250x250 area, it’d be at point (875,125).

To calculate relative position, I’d just:

WindowMouseX = (_MOUSEX - WindowLeftPosition) * (WindowCoordinateWidth/ ActualWindowWidth)
WindowMouseY = (_MOUSEY - WindowTopPosition) * (WindowCoordinateHeight / ActualWindowHeight)

So the WindowMouseX would be (875 - 750) * (1000 / 250) = (125) * (4) = 500
And WindowMouseY would be (125 - 0) * (1000 / 250) = (125) * (4) = 500

And, in the mini-window, point (500,500) is indeed the center of the designated 1000x1000 coordinate system, which overall would be at (875, 125) on the main screen.

Just don’t forget to error check...

IF WindowMouseX < 0 OR WindowMouseX > 1000 OR WindowMouseY < 0 OR WindowMouseY > 1000 THEN ‘Result is not inside preview window.  Skip any preview click results...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #4 on: June 28, 2019, 02:35:06 pm »
Hi. I look at it again, maybe this help you, it is the same as Steve wrote:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. LINE (100, 100)-(300, 300), , B
  3. VIEW (100, 100)-(300, 300)
  4. CIRCLE (100, 100), 100
  5.  
  6.  
  7.     WINDOW (100, 100)-(300, 300)
  8.     LOCATE 1, 1,: PRINT _MOUSEX, _MOUSEY
  9.     GET_Window_Coordinates 100, 300, 100, 300, Wx, Wy
  10.     LOCATE 1, 1: PRINT "Absolute coordinates:"; _MOUSEX, _MOUSEY
  11.     LOCATE 2, 1: PRINT "Realtive coordinates:"; Wx, Wy
  12.  
  13. SUB GET_Window_Coordinates (xstart, xend, ystart, yend, getX, getY)
  14.     getX = 0: getY = 0
  15.     IF _MOUSEX >= xstart AND _MOUSEX <= xend THEN
  16.         IF _MOUSEY >= ystart AND _MOUSEY <= yend THEN
  17.             getX = _MOUSEX - xstart
  18.             getY = _MOUSEY - ystart
  19.         END IF
  20.     END IF
  21.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #5 on: June 28, 2019, 05:37:52 pm »
Hi Guys
here an example with PMAP, VIEW and WINDOW

Code: QB64: [Select]
  1. CONST HScreen = 500, WScreen = 500
  2. CONST T = 1, B = 100
  3. SCREEN _NEWIMAGE(WScreen, HScreen, 32)
  4. ActiveMenu
  5. ActiveMap
  6.  
  7.     k = _KEYHIT
  8.     mX = _MOUSEX
  9.     mY = _MOUSEY
  10.     j = WhereIsMouse(mX, mY)
  11.     showdata mX, mY, j
  12.  
  13.     _LIMIT 30
  14. LOOP UNTIL k = 13 OR k = 27 ' enter or Esc to quit
  15.  
  16. SUB showdata (mX AS INTEGER, mY AS INTEGER, j AS INTEGER)
  17.     IF j = 1 THEN
  18.         ActiveMenu
  19.         ' here you put all the instruction for manage Active menu
  20.     ELSEIF j = 2 THEN
  21.         ActiveMap
  22.         ' here you put all the instruction for manage ActiveMaP
  23.     ELSE
  24.         VIEW
  25.         ' here you put all the instruction for manage main view of the program
  26.     END IF
  27.     '    the functions PAMP work on the last view activated!
  28.  
  29.     LOCATE 1, 15
  30.     PRINT "Viewport  MouseX MouseY  ViewMx ViewMy"
  31.     LOCATE 2, 20
  32.     PRINT SPACE$(40)
  33.     LOCATE 2, 20
  34.     PRINT j; "  "; mX; "  "; mY; "  "; PMAP(mX, 2); "  "; PMAP(mY, 3)
  35.  
  36.    ' restore main viewport    
  37.     VIEW
  38.     WINDOW
  39.  
  40. FUNCTION WhereIsMouse (mx AS INTEGER, my AS INTEGER)
  41.     IF mx > T AND mx < B THEN
  42.         IF my > T AND my < B THEN
  43.             WhereIsMouse = 1
  44.             EXIT FUNCTION
  45.         END IF
  46.     END IF
  47.     IF mx > WScreen - 200 AND mx < WScreen THEN
  48.         IF my > HScreen - 200 AND my < HScreen THEN
  49.             WhereIsMouse = 2
  50.             EXIT FUNCTION
  51.         END IF
  52.     END IF
  53.     WhereIsMouse = 3 ' value for main viewport
  54.  
  55. SUB ActiveMap
  56.     ' view port map
  57.     VIEW (HScreen - 200, WScreen - 200)-(HScreen - 2, WScreen - 2), _RGB32(200, 100, 200), _RGB32(220, 200, 50)
  58.     WINDOW SCREEN(-500, -500)-(500, 500) ' personalized cohordinates
  59.  
  60. SUB ActiveMenu
  61.     ' view port menu
  62.     VIEW (T, T)-(B, B), _RGB32(0, 100, 200), _RGB32(220, 200, 50)

Thanks to read
Good Coding
Programming isn't difficult, only it's  consuming time and coffee

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #6 on: June 28, 2019, 06:14:59 pm »
Thanks guys, I figured that once I put the image in the main screen it was part of the main screen and would react with mainscreen coordinates. When I'm doing the display updates I'm working with the "sub image", but once it's put in the main display I've lost the option of direct mouse interaction. I'm always keen to avoid the math, but as Steve says, sometimes you just have to bite that bullet.

What I'm essentially flirting with is an AutoCAD like system that has WCS and UCS based references. I want to be able to go into the screen and click on a particular "radar blip" and have it come up as an active viewpoint. I'm also flirting with a way to do a "zoom extents" in the window. It's quite a hunk for someone at my level to be chewing on. Maybe it'll work, since with things like this I approach it with the attitude that I'm too stupid to realize I can't do it, and it often happens that I can after all...

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Getting coordinates from a WINDOW _PUTIMAGE w/ mouse
« Reply #7 on: June 29, 2019, 07:35:33 pm »
Success! It's working as I envisioned it would. Steve's equations and a little patient tweaking did the trick.