Skip to content

Mod example: a button

AUTOMATIC1111 edited this page Mar 19, 2018 · 1 revision

This mod draws a button with text "Click me!" in the upper left corner of new game screen. When user clicks the button, it disappears. This mod uses the UI code from the squad selection screen. Unlike the simple button, the button is not drawn as a picture, and has a fancy interaction mode, just like buttons in original game: when you hover over the button with mouse, it changes color, you can press the button and release mouse button outside of it and the click won't register, etc.

Download: https://drive.google.com/open?id=1zLhJs5SiFNq_vhc83O2bUFj42sgbCLf4

Here is the entire source code the mod:

local ui
local visible = true
local function createUi(screen)
	if ui ~= nul then return end
	
	ui = UiRoot():widthpx(screen:w()):heightpx(screen:h())
	local button = Ui():pospx(10,10):widthpx(120):heightpx(36):caption("Click me!"):decorate({DecoButton(),DecoCaption()}):addTo(ui)
	button.onclicked = function()
		visible = false
	end
end

local function init(self)
	local hangarGraphic=sdl.surface(self.resourcePath.."hangar_main.png")

	AUTO_HOOK_Draw_Hangar2	 = sdl.drawHook(function(screen)
		if hangarGraphic:wasDrawn() and visible then
			createUi(screen)
			
			ui:draw(screen)
		end
	end)
	
	AUTO_HOOK_Event_Hangar2 = sdl.eventHook(function(event)
		if ui == nil or not visible then return false end
		
		return ui:event(event)
	end)
end

local function load(self,options,version)

end

return {
	id = "C_AButton",
	name = "A Button",
	version = "1.0.0",
	requirements = {},
	init = init,
	load = load,
}
Clone this wiki locally