My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
grid2d  
coordinate based 2d grid for games and applications
Featured, Phase-Deploy
Updated Feb 6, 2012 by wiebo.de.wit

grid2d.mod

Repo location

Grid2D is a coordinate-based grid that can be used in games and editors. The appearance and feel is based on the Lightwave 3D modeler grid.

Variable grid sizes and zoom levels are supported, as is snap-to-grid. All grid position related math is vector based. The grid is event driven, no event setup is needed; applications can listen to the update event to re-render the grid.

This is an example of how it looks on a maxgui canvas:

Types

The module consists of a single type: TGrid2D.

Requirements

This module requires the TVector2D module, which is included in the download, but not in the grid2d repository.

Events

The grid emits a EVENT_GADGETPAINT event. The eventsource is the canvas used to render the grid on.

How to use

The grid can be controlled with:

  • right mouse button to drag
  • mouse wheel to zoom in or out
  • [ to lower grid size, ] to increase grid size

Code Example

What follows is an example thatshows how the grid is used with maxgui.

First, import the module:

Import wdw.grid2d

Then, create an application type. In this example the New() method also creates the grid. The application also has a GridRender() method in which the grid is rendered and on top of that the application objects can be rendered.

Local app:TMyApp = New TMyApp
app.GridRender()

Run the update() method when there is an event.

While WaitEvent()
	app.Update()
Wend

This is the application definition.

The New() method creates all objects and also registers an event hook. This is not required but it will enable the application to also act on modal events (resize window, for example)

Type TMyApp

	Field window:TGadget
	Field canvas:TGadget
	Field grid:TGrid2D
		
	Method New()	
		window = CreateWindow("grid",0,0,400,400,Null, ..
				WINDOW_CENTER|WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS)
		canvas = CreateCanvas(0,0,400,400,w)
		SetGadgetLayout(canvas, EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED)
		grid = New TGrid2D
		grid.SetCanvas(canvas)
		grid.SetLabel("Example Grid")
		
		AddHook(EmitEventHook, MyEventHandler, Self)
	End Method
	
	
	Method Update()
		Select EventID()
			Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE
			End
		End Select
	End Method

	
	Function MyEventHandler:Object(id:Int, data:Object, context:Object)
		If data Then TMyApp(context).OnMyEvent(TEvent(data))
		Return data
	End Function

The OnMyEvent() method will call the GridRender() method if a re-draw is needed. The EVENT_GADGETPAINT is generated by the grid.

	Method OnMyEvent(event:TEvent)
		If event.source = grid.GetCanvas() And event.id = EVENT_GADGETPAINT
			GridRender()
		EndIf
	End Method
	
	
	Method GridRender()
		grid.Render()
		SetStatusText(w, grid.GetStatusText())	
		grid.UpdateStatusText()
	
		Flip
	End Method
End Type

Sign in to add a comment
Powered by Google Project Hosting