PDA

View Full Version : Need some help with map editor (drawing cursor rects).



Chesso
09-01-2009, 05:58 AM
I am trying to calculate and draw rectangles under the cursor at the appropriate size (as the map is 1024x768 and tiles are 32x32).

I created myself a little record TMouse with x and y as integer which I update in my TPanel's (where I output Asphyre drawing routines) OnMouseMove event.

I then have another basic record with X, Y for drawing the rectangle which I try to calculate in the processing event (I may put this in the OnMouseMove event seen as I don't need to calculate until the mouse moves really).

So far I do it like this:


if (sMouse.x > 0) AND
(sMouse.x <32>= 0) AND
(sMouse.y <32> 32) AND
(sMouse.x <64>= 0) AND
(sMouse.y <32> 64) AND
(sMouse.x <96>= 0) AND
(sMouse.y <= 32) then
begin
sRect.x := 64;
sRect.y := 0;
end;


This obviously only covers 3 tiles.... out of the possible 768 tiles, it would be a rather large conditional branch lol.

I am constantly trying to think of ideas to shorten this up, so by the time somebody replies I may have figured out a solution, but any help is definitely and greatly appreciated.

I'm sure I will have plenty more to come haha![/code]

Chesso
09-01-2009, 06:03 AM
Wow maybe the site is a bit broken... it just will note take all of my code in either the code or pascal tags.

I wonder if it will take it straight:

if (sMouse.x > 0) AND
(sMouse.x <32>= 0) AND
(sMouse.y <32> 32) AND
(sMouse.x <64>= 0) AND
(sMouse.y <32> 64) AND
(sMouse.x <96>= 0) AND
(sMouse.y <= 32) then
begin
sRect.x := 64;
sRect.y := 0;
end;


What the hell...... lol, umm hmm can attachments be done I wonder sheesh....

Traveler
09-01-2009, 08:58 AM
Hmm, if I understand you correctly than you are looking for something like this:

pos.x := sMouse.x div 32;
pos.y := sMouse.y div 32;

drawRect ( pos.x*32, pos.y*32, (pos.x*32)+32, (pos.y*32)+32)

efilnukefesin
09-01-2009, 09:12 AM
there are of course several solutions to this.

personally, in one of my last projects i created a class 'ttile' and stored the coordinates in it (very useful if you haven't rects as tiles) and with this very helpful function i tested if my mouse was in a tile:
http://swissdelphicenter.ch/de/showcode.php?id=1778

so the used code lookes something like this:


point.x&#58;= mousex;
point.y&#58;= mousey;

for i&#58;= low&#40;tiles&#41; to high&#40;tiles&#41; do begin
if PtInRgn&#40;tiles&#91;i&#93;.poly, point&#41; then begin
//tile&#91;i&#93; is hovered
//do the hovering thing
break;
end;
end;


i'm quite sure it's not the fastest way to do that but it's quite simple and effective.

Chesso
09-01-2009, 09:13 AM
Holy crap.... to think it's that simple.....

I adjusted a bit and placed most of it in the OnMouseMove event like so:


sRect.x := (x Div 32) * 32;
sRect.y := (y Div 32) * 32;


And yeah it works great now. I really must learn these little math functions, very handy.

It just leaves me with one issue I think, how to actually know the tile I am on (some sort of reference, like a numbering of 1 to 768 or something like that), for the purpose of knowing where the tiles are being placed (so internal records can be modified on the fly).

Well I will have a muck about and see what I can do about it anyway. Thank you very much for the assistance so far!!!

Chesso
09-01-2009, 09:18 AM
Just saw your post there elf.

Yes I see what you mean there, that would effectively knock both of my issues out in one foul swoop.

The thing is, my tTile record does not actually have any X or Y information, the engine automatically calculates (with a basic function) the X and Y of each tile for my 4 layers at this stage, so all the map has specified (well what I have planned at least) is the tile number.

Well I could just as easily push this function over and have it setup on run so it's all ready to go.

But what kind of function is there, or will I have to create to check if the mouse X and Y is within the set boundaries (PtInRgn isn't a real delphi function is it? lol).


EDIT: I think I have the right idea (just testing something out).

You guys are awesome, I think I have it all sorted out now:


rocedure TfrmMain.mapMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
i: Integer;
begin

for i := LOW(layer1) to HIGH(layer1) do
begin
if (x >= layer1[i].x) AND
(x <layer1>= layer1[i].y) AND
(y <= layer1[i].y +32) then
begin
sRect.x := layer1[i].x;
sRect.y := layer1[i].y;
sRect.tile := i;
exit;
end;
end;

end;


Ok so some of the code still seems to get garbled when I post, but you get the idea, it's mostly completely shown.

Hopefully the above code all shows properly, it's working great now ^_^.