Jump to content
  • entries
    939
  • comments
    5,894
  • views
    863,525

Leadwerks 5 Editor Extension Example


Josh

888 views

 Share

Our new editor is being designed to support user-created extensions written in Lua. I want Lua to work in our new editor the way MaxScript works in 3ds Max, to allow an endless assortment of new tools you can create and use.

Now that the editor GUI system is well underway, I want to start thinking about how user-created extensions will work with our new editor. I'm going to lay out some theoretical code for how a road creation tool might integrate into the editor.

First we declare a start function that is run when the extension is loaded. This will add a toolbar and menu item so the tool can be selected, as well as create a new event listen function:

function extension:Start()
  
  	--Load the tool icon
	local icon = LoadPixmap("Icons/RoadTool.svg")
  
  	--Add a toolbar button
	self.toolbarbutton = application.mainwindow.toolbar:InsertButton(icon)

  	--Add a menu button
  	self.menuitem = application.mainwindow.menu["Tools"]:InsertItem("Road Tool")
	self.menuitem:SetPixmap(icon)
      
	--Listen for events. EVENT_NONE will process all events:
	ListenEvent(EVENT_NONE, application.viewportgrid.viewport[1], self.ProcessEvent, self)
	ListenEvent(EVENT_NONE, application.viewportgrid.viewport[2], self.ProcessEvent, self)
	ListenEvent(EVENT_NONE, application.viewportgrid.viewport[3], self.ProcessEvent, self)
	ListenEvent(EVENT_NONE, application.viewportgrid.viewport[4], self.ProcessEvent, self)
end

Now we need to declare a function to process events. If the function returns false, the event will not be further processed, so the default mouse tool will be overridden.

function extension:ProcessEvent(event)

	--Return if the road tool is not active
	if self.toolbarbutton:GetState() == false then return true end

  	--Evaluate widget events - keep the menu and toolbar button in sync
	if event.id == EVENT_WIDGETACTION then  
  		if event.source == self.menu then
  			self.toolbarbutton:SetState(event.data)
  		elseif event.source == self.toolbarbutton
  			self.menuitem:SetState(event.data)
  		end
    
  	--Evaluate mouse events
	elseif event.id == EVENT_MOUSEDOWN then

		local viewport = Viewport(event.source)
		if viewport ~= nil then
			local pickinfo = PickInfo()
			if viewport.camera:Pick(viewport.framebuffer, event.x, event.y, pickinfo, 0, true) then
				self:AddNode(pickinfo.position)
			end
      		return false
		end

	--Evaluate key hits
	elseif event.id == EVENT_KEYDOWN then

		if event.data == KEY_ENTER then

			if #self.splinepoints > 1 then

				--Create our road
				self:CreateRoad()

				--Update the undo system
				application:CreateUndoStep()

				--Tell the editor the scene is modified
				application:ModifyScene()

				--Refresh the viewports
				application.viewportgrid:Redraw()

				return false
			end
		end
	end

	return true
end

 

  • Like 4
 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...