[Solved + Screenshot!] Tracking mouse coords in a grid?

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

[Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Nash »

Image

I need some help. I've been trying to figure this out on my own for a few days but I've come to the point where I realize I just don't know how to do this.

In the screenshot above, you can see a grid-based inventory window which is 10 x 10 in size. Each grid is 32 x 32 pixels. What I'm trying to do is, I want to know which grid is the mouse currently hovered over. I want the result to return a number from 0 to 99 (well, to be more specific, there are 100 slots, but since ACS arrays are 0-based, that would mean that the results would be from 0 to 99).

The grid is also ordered from left to right, then top to bottom. That means... as you retrieve items, the grid will first be filled from left to right... once it reaches the last grid on the right, it moves down to the next line and starts from the left again.

I haven't the slightest clue on how to achieve this. I'm horrible at maths. Please help. :(
Last edited by Nash on Thu Mar 05, 2009 9:54 am, edited 1 time in total.
User avatar
Isle
Posts: 687
Joined: Fri Nov 21, 2003 1:30 am
Location: Arizona, USA

Re: [Help] Tracking mouse coords in a grid?

Post by Isle »

subtract the pointer coords from the top right of the top right square. IE if its at 32, 32: x-32 y-32. this makes the top right zero.
now, since its a nice grid all you have to do is divide x and y by the cell size, then multiply y times 10 and add x;
so if x = 270 and y = 165,
x = x - 32,
y = y - 32,
x = x/32 = 7, (ACS integer math rounds everthing down)
y = y/32 = 4,
index = (y*10)+x = 47,
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [Help] Tracking mouse coords in a grid?

Post by Nash »

Had to alter it around a bit, but finally got it to work. Thanks Isle, I knew I could count on you for all of this maths-y stuff in ACS. :D You'll be credited!

Code: Select all

	// calculate which grid the mouse is currently in
	int index, index_x, index_y;
	index_x = mousex - GetHUDLeft(640);
	index_y = mousey - 32;
	index_x = index_x / 32;
	index_y = index_y / 32;
	index = (index_y * 10) + index_x;
User avatar
Cutmanmike
Posts: 11336
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: [Help] Tracking mouse coords in a grid?

Post by Cutmanmike »

Looks great btw Nash. I long for a decent rpg zdoom adventure, even if it's just a mod to use with doom2 maps.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [Help] Tracking mouse coords in a grid?

Post by Nash »

Thanks Cutty.

Behold! Thanks to Isle, we now have a working mouse-based inventory, and hovering the mouse over an item will correctly display the item's data! The sword in the screenshot was defined in the master items database to have a base damage of 25, and durability of 50. As you can see in the shot, that one sword in particular that has been pointed over, has its stats randomized. If I were to point the mouse over the other swords in that screenshot, they'd all have different values. Diablo-style items anyone?

Image
User avatar
Cutmanmike
Posts: 11336
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Cutmanmike »

As a Diablo nut, colour me interested.
User avatar
Osiris Kalev
Posts: 290
Joined: Sun Aug 20, 2006 11:06 pm
Location: Australia, Elsewhere Within
Contact:

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Osiris Kalev »

That's shiny Nash.
Gez
 
 
Posts: 17835
Joined: Fri Jul 06, 2007 3:22 pm

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Gez »

Everybody loves the Daggerfall dagger. ;)
User avatar
Captain Proof
Posts: 1028
Joined: Wed Aug 27, 2008 8:51 am
Location: Henderson
Contact:

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Captain Proof »

Wasn't that an Ebony Dagger?I can't remember,I tried to load Daggerfall on Dosbox but it's being a bitch and saying that it can't detect the CD even after I mounted my damn CDrom.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Nash »

As usual for me, the Daggerfall dagger is a temporary placeholder sprite. This will have 100% original graphics, this effort demonstrated by the new status bar...
Gez
 
 
Posts: 17835
Joined: Fri Jul 06, 2007 3:22 pm

Re: [Solved + Screenshot!] Tracking mouse coords in a grid?

Post by Gez »

Captain Proof wrote:Wasn't that an Ebony Dagger?
Yes and no. Daggerfall used translations for the various exotic metals. Iron, steel, silver, dwarven, ebony, adamantium, mithril, orcish, daedric... They all used the same sprites. Just with different colors (green for orcish, black for ebony, red for daedric, etc.). The default color (the one you get if you rip the sprites) corresponds to steel IIRC.
Nash wrote:As usual for me, the Daggerfall dagger is a temporary placeholder sprite. This will have 100% original graphics, this effort demonstrated by the new status bar...
Sorry for the digression. :3:
Locked

Return to “Editing (Archive)”