|
-
Jun 12th, 2011, 01:17 AM
#1
Thread Starter
Lively Member
vb 2010 Mapeditor
Hello,
I have a few problems with making a map editor, if someone knows how to make one please reply and ill give you my msn
well actually this problem: its not making the grid invisible if you de-deselect the option "Grid On" because the rect's have already been drawn. So I need to know how you can delete them.
Code:
Private Sub GridOnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridOnToolStripMenuItem.Click
If GridOnToolStripMenuItem.Checked = True Then
GridOnToolStripMenuItem.Checked = False
GRID_ON = False
Else
GridOnToolStripMenuItem.Checked = True
GRID_ON = True
End If
UpdateMapEditorGraphics()
End Sub
Code:
Public Sub UpdateMapEditorGraphics()
If GRID_ON = True Then
For x = 0 To MAX_GRID_X
For y = 0 To MAX_GRID_Y
sRect = New Rectangle(x * TILE_SIZE_X, y * TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y)
g.DrawRectangle(Pens.Black, sRect)
Next
Next
Else
'GRAPHICS SHOULD BE MADE INVISIBLE HERE'
End If
End Sub
-
Jun 12th, 2011, 04:01 AM
#2
Re: vb 2010 Mapeditor
When using GDI+ to draw on controls, you draw only on the Paint event. The drawing then gets done every time the control is repainted. You store the data that describes the drawing in member variables and you read those variables in the Paint event handler. If you want to control whether the grid gets drawn or not, you would use a Boolean variable for that. Whenever you want to show or hide the grid, you simply toggle the variable and force a repaint, which you do by calling Refresh on the control.
-
Jun 12th, 2011, 04:18 AM
#3
Re: vb 2010 Mapeditor
Like Mr. McIlhinney said you need to draw under the Paint event.
Say you declare some classes for a Map and Elements within a map:
vb Code:
'This class represents a single square on a map
Public Class MapElement
Public ElementColor As Color
'more info about elements of a map.
Public Sub New(ByVal sColor As Color)
ElementColor = sColor
End Sub
End Class
'This class represents an entire map made up of MapElements
Public Class Map
'An array of elements within the map
'The array can be resized to any size with ReDim
Private vMapElements(,) As MapElement
Public Sub New(ByVal sSizeX As Integer, ByVal sSizeY As Integer)
ReDim vMapElements(sSizeX, sSizeY)
For i As Integer = 0 To sSizeX - 1
For j As Integer = 0 To sSizeY - 1
vMapElements(i, j) = Nothing
Next
Next
End Sub
'Gets/sets the current element at (X, Y).
'Note that these may be Nothing with the current implementation
Default Public Property Element(ByVal X As Integer, ByVal Y As Integer) As MapElement
Get
Return vMapElements(X, Y)
End Get
Set(ByVal value As MapElement)
vMapElements(X, Y) = value
End Set
End Property
Public ReadOnly Property MapSizeX As Integer
Get
Return vMapElements.GetUpperBound(0)
End Get
End Property
Public ReadOnly Property MapSizeY As Integer
Get
Return vMapElements.GetUpperBound(1)
End Get
End Property
'Draws the current contents of the map into sGraphics using sOffset as
'the upper, left corner.
Public Sub RenderMap(ByVal sGraphics As Graphics, ByVal sOffset As Point, ByVal sSize As Size)
Dim R As Rectangle = New Rectangle
Dim B As New SolidBrush(Color.AliceBlue)
Dim vLocation As Point = sOffset
R.Size = sSize
For i As Integer = 0 To Me.MapSizeX - 1
For j As Integer = 0 To Me.MapSizeY - 1
R.Location = vLocation
'If the element is present then draw it.
If vMapElements(i, j) IsNot Nothing Then
B.Color = vMapElements(i, j).ElementColor
sGraphics.FillRectangle(B, R)
End If
'Update the current location
vLocation.Y += sSize.Height
Next
'Update the current location
vLocation.Y = sOffset.Y
vLocation.X += sSize.Width
Next
End Sub
End Class
declare an instance of this Map with:
vb Code:
'Declare a new map of size 20 by 10 elements.
Private MyMap As New Map(20, 10)
and place this (modified version of your grid routine) in the Paint event of your form:
vb Code:
Dim PtStart, PtEnd As Point
Dim LeftMargin As Integer = 8
Dim TopMargin As Integer = 32
e.Graphics.Clear(Me.BackColor)
If MyMap IsNot Nothing Then
MyMap.RenderMap(e.Graphics, New Point(LeftMargin, TopMargin), New Size(TILE_SIZE_X, TILE_SIZE_Y))
If GRID_ON = True Then
'1. Draw some vertical lines
PtStart = New Point(LeftMargin, TopMargin)
PtEnd = New Point(LeftMargin, TopMargin + MyMap.MapSizeY * TILE_SIZE_Y)
For x As Integer = 0 To MyMap.MapSizeX
e.Graphics.DrawLine(Pens.Black, PtStart, PtEnd)
PtStart.X += TILE_SIZE_X
PtEnd.X += TILE_SIZE_X
Next
'2. Draw some horizontal lines
PtStart = New Point(LeftMargin, TopMargin)
PtEnd = New Point(LeftMargin + MyMap.MapSizeX * TILE_SIZE_X, TopMargin)
For y As Integer = 0 To MyMap.MapSizeY
e.Graphics.DrawLine(Pens.Black, PtStart, PtEnd)
PtStart.Y += TILE_SIZE_Y
PtEnd.Y += TILE_SIZE_Y
Next
End If
End If
you should be able to draw maps.
Single elements within the map can be set with:
vb Code:
MyMap(0, 0) = New MapElement(Color.Green)
MyMap(0, 1) = New MapElement(Color.Yellow)
MyMap(19, 9) = New MapElement(Color.Red)
MyMap(10, 7) = New MapElement(Color.Plum)
and the entire map can be redrawn at any time with a call to Me.Refresh.
NOTE: This is a bare bones type example and many of the things can be made differently and more efficiently. But it should be easy enough to fiddle a bit around with and later modify to your needs.
Regards
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jun 13th, 2011, 02:06 AM
#4
Thread Starter
Lively Member
Re: vb 2010 Mapeditor
thanks, worked fine
-
Jun 13th, 2011, 01:58 PM
#5
Re: vb 2010 Mapeditor
I'd make the Map class a control (by inheriting Control, or Panel, or PictureBox, or...) and put the painting in the OnPaint method override. This way you can just drop the Map onto any form you want instead of having to repeat the drawing code in each form's Paint event...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|