OH CRAP I DIDN'T MAKE THIS PUBLIC
Button=defclass(Button,widgets.Widget)
Button.ATTRS={
on_click = DEFAULT_NIL,
graphic = DEFAULT_NIL, --refers to the name of a tilepage
label = DEFAULT_NIL
}
function Button:preUpdateLayout()
self.frame=self.frame or {}
self.frame.w=self.page.page_dim_x
self.frame.h=self.page.page_dim_y
end
function Button:onRenderBody(dc)
for k,v in ipairs(self.page.texpos) do
dc:seek(k%self.frame.w,math.floor(k/self.frame.w)):tile(32,v)
end
end
function Button:onInput(keys)
if keys._MOUSE_L_DOWN and self:getMousePos() and self.on_click then
self.on_click()
end
end
function Button:init(args)
for k,v in ipairs(df.global.texture.page) do
if v.token==self.graphic then self.page=v break end
end
end
This is a button that uses an arbitrary graphics image as its display (with GRAPHICS:ON, otherwise will display ☺) that does some arbitrary thing when clicked. Example:
Button{
graphic='MINE',
frame={t=0,l=0},
on_click=function()
self:sendInputToParent('D_DESIGNATE')
self:sendInputToParent('DESIGNATE_DIG')
end,
label='Mining',
view_id='mining_button'
}
graphic='MINE' refers to [TILE_PAGE:MINE] in raw/graphics; it will load this tile page if possible (at least one creature must use part of this tile page for the tile page to load; I use GRIFFON or one of the other fanciful creatures). How large the button will be depends on PAGE_DIM (4:4 will make a 4x4 button). TILE_DIM and PAGE_DIM rules are same as creature graphics.
frame, view_id are as in normal widgets.
label is a thing that I added so that a highlighted button can have text describing what it is, such as in the renderSubviews(dc) method of my specialized button screen:
local highlighted=false
for _,child in ipairs(self.subviews) do
if child:getMousePos() then self.subviews.highlight_label:setText(child.label) highlighted=true end
if child.visible then
child:render(dc)
end
end
if not highlighted then self.subviews.highlight_label:setText('Highlight a button!') end
end
Where highlight_label is a widgets.Label.
on_click is the function that fires when the button is clicked. Here, it opens the designations menu with the "mine" designation (assuming this button is on the main dwarf mode screen, which I recommend not doing for issues with OPTIONS and autosaves)