Plugin Example
This is the source code for a Gump Studio plugin. It is actually 2 plugins working together to create the snap to grid feature.
The first plugin, SnapToGrid, Adds a menu "Show Grid" that toggles on or off the grid of dots in the designer. It also hooks into the mouse movement code to watch for element dragging, and alters the mouse coord to snap it to the grid when the shift key is held down. It also installs another plugin, SnapToGridExtender, which gets placed into each type of Element. This plugin adds the "snap to grid" context menu to elements when they are right clicked.
The code would be compiled into a DLL, and placed into the "Plugins" folder next to the GumpDesigner.exe.
I'll try to come up with a better tutorial on creating plugins once I officially release the edit.
[code:1]Imports Gump_Designer
Imports Gump_Designer.Plugins
Imports System.Drawing
Public Class SnapToGrid
Inherits BasePlugin
Protected mDesigner As DesignerForm
Protected mExtender As SnapToGridExtender
Protected mShowGrid As Boolean = False
Protected mShowGridMenu As System.Windows.Forms.MenuItem
Public Overrides Function GetPluginInfo() As Gump_Designer.Plugins.PluginInfo
Dim Info As New PluginInfo
Info.AuthorEmail = "**********"
Info.AuthorName = "Bradley Uffner"
Info.Description = "Allows elements to be snapped to an 8x8 grid."
Info.PluginName = "SnapToGrid"
Info.Version = "1.0"
Return Info
End Function
Public Overrides Sub Load(ByVal frmDesigner As Gump_Designer.DesignerForm)
mDesigner = frmDesigner
If mExtender Is Nothing Then mExtender = New SnapToGridExtender(mDesigner)
mShowGridMenu = New System.Windows.Forms.MenuItem("Show Grid", AddressOf DoToggleGridMenu)
frmDesigner.mnuPlugins.MenuItems.Add(mShowGridMenu)
AddHandler mDesigner.PreRender, AddressOf RenderGrid
End Sub
Public Overrides ReadOnly Property Name() As String
Get
Return GetPluginInfo.PluginName
End Get
End Property
Public Overrides Sub InitializeElementExtenders(ByVal Element As Gump_Designer.Elements.BaseElement)
Element.AddExtender(mExtender)
End Sub
Public Overrides Sub MouseMoveHook(ByRef e As Gump_Designer.Plugins.MouseMoveHookEventArgs)
If e.MoveMode = MoveModeType.Move AndAlso CBool(e.Keys And Windows.Forms.Keys.Shift) Then
e.MouseLocation = mExtender.SnapToGrid(e.MouseLocation, mExtender.GridSize)
End If
End Sub
Public Sub DoToggleGridMenu(ByVal sender As System.Object, ByVal e As System.EventArgs)
mShowGrid = Not mShowGrid
mShowGridMenu.Checked = mShowGrid
mDesigner.picCanvas.Refresh()
End Sub
Public Sub RenderGrid(ByVal Target As Bitmap)
If mShowGrid Then
For x As Integer = 0 To mDesigner.picCanvas.Width - 1 Step mExtender.GridSize.Width 'DrawGird
For y As Integer = 0 To mDesigner.picCanvas.Height - 1 Step mExtender.GridSize.Height
Target.SetPixel(x, y, Color.LightGray)
Next
Next
End If
End Sub
End Class
Public Class SnapToGridExtender
Inherits Plugins.ElementExtender
Public GridSize As Size = New Size(8, 8)
Protected mDesigner As DesignerForm
Public Sub New(ByVal Designer As DesignerForm)
mDesigner = Designer
End Sub
Public Overridable Sub DoSnapToGridMenu(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each o As Object In mDesigner.ElementStack.GetSelectedElements
Dim Element As Elements.BaseElement = CType(o, Elements.BaseElement)
Element.Location = SnapToGrid(Element.Location, GridSize)
Next
End Sub
Public Function SnapToGrid(ByVal Position As Point, ByVal GridSize As Size) As Point
Dim NewPoint As Point = Position
NewPoint.X = CInt(NewPoint.X \ GridSize.Width) * GridSize.Width
NewPoint.Y = CInt(NewPoint.Y \ GridSize.Height) * GridSize.Height
Return NewPoint
End Function
Public Overrides Sub AddContextMenus(ByRef GroupMenu As System.Windows.Forms.MenuItem, ByRef PositionMenu As System.Windows.Forms.MenuItem, ByRef OrderMenu As System.Windows.Forms.MenuItem, ByRef MiscMenu As System.Windows.Forms.MenuItem)
If PositionMenu.MenuItems.Count > 1 Then
PositionMenu.MenuItems.Add(New System.Windows.Forms.MenuItem("-"
)
End If
PositionMenu.MenuItems.Add(New System.Windows.Forms.MenuItem("Snap to Grid", AddressOf DoSnapToGridMenu))
End Sub
End Class[/code:1]