Sign in to follow this  
namingway

selecting a portion of an image help

Recommended Posts

I have a map editor I'm writing and its coming along well, but I'm not sure how to select certain portions of the loaded tileset. The tile sizes are 32x32 and the tileset is loaded into a picture box, do I need a different component to be able to do this? Tilesets are a fix size of 8 across and 20 down or 256x640.

Share this post


Link to post
Share on other sites
You could probably split the loaded image into multiple images, one for each tile, and display each with a picture box of it's own, or you could write some custom selection logic for the single picture box approach. What language and tools are you using to create this editor with?

Share this post


Link to post
Share on other sites
This isn't something a API does for you, this is something you'll need to implement yourself in your map editor.

Here's how it might be done: First, store a bool somewhere, that's true when the user is selecting multiple tiles, and false when he's only grabbing one at a time. Next, have a button the user can turn on and off that controls that bool.
Then, when selecting tiles from the tileset, if the bool is false, get the tile as normal, but if the bool is true store wherever the player clicked, in terms of tiles. (Example: the player clicked on tilesheet X4 Y8) Don't select the tile yet, but wait for the player to click a second time, and store the second tile selected also. (Example: tilesheet X4 Y6)

If the second tile is less than the first tile, swap them, so the lowest numbered tile is first.(The tile higher up on the tilesheet)

Then, when the player clicks on the map to place a tile, check the bool. If the bool is false, place the single tile like normal, but if the bool is true, starting where the player clicked, cycle through the tiles from the tilesheet starting at the first place(in the tilesheet) the player clicked, and place them in the map until the second place(in the tilesheet) the player clicked is reached.

Share this post


Link to post
Share on other sites
Cant beleive I forgot to say the language... It's C#, Splitting different tiles into separat pictureboxs would be a nice approach but I still get the probelm of cutting out the individual tiles to do so. My initial Idea was to check a click in the picture box and then detect the coordinates to see which tile was clicked but that will use alot of if elses...
Also an interesting idea Servant, I'll look into that as well.

Share this post


Link to post
Share on other sites
It shouldn't use a lot of if-elses since your tiles are of a consistent size.

TileX = MouseX \ TileWidth
TileY = MouseY \ TileHeight

This will give you a 0-based X & Y indicator for which tile was clicked-on.

-Matt

Share this post


Link to post
Share on other sites
I'm trying your way but how do I get the mouse position from within the picturebox itself, my current attempt gets the global mouse position.

private void pictureBox1_Click(object sender, EventArgs e)
{
_tileX = MousePosition.X / 32;
_tileY = MousePosition.Y / 32;
if (_tileX == 1 && _tileY == 1)
{
_tileID = 1;
this.Text = "Mappy " + _tileID;
}
}

Share this post


Link to post
Share on other sites
I'm not familiar with how C# uses the normal Windows Form stuff, like cursor position per control, etc. Perhaps someone else can offer help in that area.

However, if MousePosition.X and .Y are the global positions ... can you subtract your Picturebox's top and left from those values to get the in-PictureBox value?

Once you have that, then instead of an If/Then or Switch to determine the Tile ID, you could do something like this ...

TileID = TileY * NumberOfTilesPerRow + TileX

I hope this is helpful.

-Matt

Share this post


Link to post
Share on other sites
I needed to use a mouse_click event then use the mouse args to track the position. Then by using code similiar to your suggestion it turned out perfectly. The problem I have now though is making something to put the tiles onto for a map. The code for its ready I just need to find out a way of making an array of picture boxs or something.

Share this post


Link to post
Share on other sites
Quote:
Original post by namingway
The problem I have now though is making something to put the tiles onto for a map. The code for its ready I just need to find out a way of making an array of picture boxs or something.


You could have a second picture box that displays the map. Have the user click on the first picture box to select the tile they wish to use ... then click on the second picture box to choose where to draw that tile.

You could use a similar way of finding the right X,Y location in your 2D map ... then store the updated map information (assumes you're using an array, or something similar, to hold your map content information) and draw the selected tile graphic to the correct point on your map picture box.

-Matt

Share this post


Link to post
Share on other sites
Actually I've already got that working, the problem was the second picture box could only store 1 image as far as I know. I'm thinking I may need to make a panel and use that to draw the map to.

Share this post


Link to post
Share on other sites
You can draw to different locations within a picture box using the System.Drawing namespace methods.

What were you using to get the tile image on the second picture box?

-Matt

Share this post


Link to post
Share on other sites
Okay I've pretty much solved that issue, although how can I make a dynamic array that can be changed? Should I use something like a vector?

Also how can I add vertical and horizontal scrollbars to the panel to view the tiles that arent visible due to the panel size?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this