Results 1 to 6 of 6

Thread: map editor

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    map editor

    Hello,

    I'm making a map editor but I have a few problems.

    I dont know how to make it that when you click the 'picMap' it will draw the tile-image on the x and y you clicked.

    can someone help me?

    thanks in advanced

    (http://www.mediafire.com/?bdgoa79fce3377e)

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: map editor

    Here is a very simple example:
    Code:
    Public Class Form1
    
        '//fields
        Private grid As Grid
    
        '//event handlers
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            Me.grid = New Grid(10, 10)
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As Object, _
                                    ByVal e As MouseEventArgs) _
                                    Handles MyBase.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim point = Me.PointToClient(Cursor.Position)
                For Each b As Block In Me.grid.Blocks
                    If b.Rectangle.Contains(point) Then
                        b.Draw = Not b.Draw
                        Exit For
                    End If
                Next
                Me.Invalidate()
            End If
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, _
                                ByVal e As PaintEventArgs) _
                                Handles MyBase.Paint
            For Each b As Block In Me.grid.Blocks
                If b.Draw Then
                    e.Graphics.FillRectangle(Brushes.Black, b.Rectangle)
                Else
                    e.Graphics.DrawRectangle(Pens.Black, b.Rectangle)
                End If
            Next
        End Sub
    
    End Class
    
    Public Class Grid
    
        '//constants
        Public Const BlockSize As Integer = 32
    
        '//fields
        Public Blocks(,) As Block
    
        '//constructors
        Public Sub New(ByVal width As Integer, ByVal height As Integer)
            Me.Blocks = New Block(width - 1, height - 1) {}
            For x = 0 To Me.Blocks.GetUpperBound(0)
                For y = 0 To Me.Blocks.GetUpperBound(1)
                    Me.Blocks(x, y) = New Block(x * Grid.BlockSize, _
                                                y * Grid.BlockSize)
                Next
            Next
        End Sub
    
    End Class
    
    Public Class Block
    
        '//fields
        Public Draw As Boolean
        Public Rectangle As Rectangle
    
        '//constructors
        Public Sub New(ByVal x As Integer, ByVal y As Integer)
            Me.Rectangle = New Rectangle(x, y, Grid.BlockSize, Grid.BlockSize)
        End Sub
    
    End Class
    Last edited by ForumAccount; Aug 9th, 2011 at 12:43 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: map editor

    Quote Originally Posted by ForumAccount View Post
    Here is a very simple example:
    Code:
    Public Class Form1
    
        '//fields
        Private grid As Grid
    
        '//event handlers
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            Me.grid = New Grid(10, 10)
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub Form1_MouseDown(ByVal sender As Object, _
                                    ByVal e As MouseEventArgs) _
                                    Handles MyBase.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim point = Me.PointToClient(Cursor.Position)
                For Each b As Block In Me.grid.Blocks
                    If b.Rectangle.Contains(point) Then
                        b.Draw = Not b.Draw
                        Exit For
                    End If
                Next
                Me.Invalidate()
            End If
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, _
                                ByVal e As PaintEventArgs) _
                                Handles MyBase.Paint
            For Each b As Block In Me.grid.Blocks
                If b.Draw Then
                    e.Graphics.FillRectangle(Brushes.Black, b.Rectangle)
                Else
                    e.Graphics.DrawRectangle(Pens.Black, b.Rectangle)
                End If
            Next
        End Sub
    
    End Class
    
    Public Class Grid
    
        '//constants
        Public Const BlockSize As Integer = 32
    
        '//fields
        Public Blocks(,) As Block
    
        '//constructors
        Public Sub New(ByVal width As Integer, ByVal height As Integer)
            Me.Blocks = New Block(width - 1, height - 1) {}
            For x = 0 To Me.Blocks.GetUpperBound(0)
                For y = 0 To Me.Blocks.GetUpperBound(1)
                    Me.Blocks(x, y) = New Block(x * Grid.BlockSize, _
                                                y * Grid.BlockSize)
                Next
            Next
        End Sub
    
    End Class
    
    Public Class Block
    
        '//fields
        Public Draw As Boolean
        Public Rectangle As Rectangle
    
        '//constructors
        Public Sub New(ByVal x As Integer, ByVal y As Integer)
            Me.Rectangle = New Rectangle(x, y, Grid.BlockSize, Grid.BlockSize)
        End Sub
    
    End Class
    thanks, but it need to draw or fill the grid from an imagefile, how will the code look then? It will need to 'surf' in the image files, like if you selected the 2th tile of the tileset you're using it should draw line X 32-64 and Y 0-32 of the tileset


    thanks in advanced

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: map editor

    I could re-code the entire example to suit your exact specifications, or you could look at how I determine what Block is clicked and then adapt to your specifications.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: map editor

    Quote Originally Posted by ForumAccount View Post
    I could re-code the entire example to suit your exact specifications, or you could look at how I determine what Block is clicked and then adapt to your specifications.
    Welll ill try to figure out but how can I select what part of the image it will draw?

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: map editor

    You would use this overload of the Graphics.DrawImage method. This overload allows you to specify the location inside the specified image that you want to draw.

    You mentioned:
    X 32-64 and Y 0-32 of the tileset
    This would translate into a Rectangle object that looks like this:
    Code:
    Dim sourceRect = New Rectangle(32, 0, 32, 32)
    Take a look at the example in the link I provided.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width