Add on_click/on_rclick handlers to widgets.Label

Closes #919
develop
lethosor 2016-05-09 21:29:04 -04:00
parent 8b8b58faef
commit c1e44c178f
4 changed files with 33 additions and 9 deletions

@ -33,6 +33,10 @@ Changelog
DFHack future DFHack future
============= =============
Lua
---
- Label widgets can now easily register handlers for mouse clicks
New Features New Features
------------ ------------
- `gui/gm-editor` it's now possible to insert default types to containers. For primitive types leave the type entry empty, for references use ``*``. - `gui/gm-editor` it's now possible to insert default types to containers. For primitive types leave the type entry empty, for references use ``*``.

@ -3051,10 +3051,13 @@ It has the following attributes:
:text_pen: Specifies the pen for active text. :text_pen: Specifies the pen for active text.
:text_dpen: Specifies the pen for disabled text. :text_dpen: Specifies the pen for disabled text.
:text_hpen: Specifies the pen for text hovered over by the mouse, if a click handler is registered.
:disabled: Boolean or a callback; if true, the label is disabled. :disabled: Boolean or a callback; if true, the label is disabled.
:enabled: Boolean or a callback; if false, the label is disabled. :enabled: Boolean or a callback; if false, the label is disabled.
:auto_height: Sets self.frame.h from the text height. :auto_height: Sets self.frame.h from the text height.
:auto_width: Sets self.frame.w from the text width. :auto_width: Sets self.frame.w from the text width.
:on_click: A callback called when the label is clicked (optional)
:on_rclick: A callback called when the label is right-clicked (optional)
The text itself is represented as a complex structure, and passed The text itself is represented as a complex structure, and passed
to the object via the ``text`` argument of the constructor, or via to the object via the ``text`` argument of the constructor, or via

@ -330,15 +330,21 @@ Label = defclass(Label, Widget)
Label.ATTRS{ Label.ATTRS{
text_pen = COLOR_WHITE, text_pen = COLOR_WHITE,
text_dpen = COLOR_DARKGREY, text_dpen = COLOR_DARKGREY, -- disabled
text_hpen = DEFAULT_NIL, -- highlight - default is text_pen with reversed brightness
disabled = DEFAULT_NIL, disabled = DEFAULT_NIL,
enabled = DEFAULT_NIL, enabled = DEFAULT_NIL,
auto_height = true, auto_height = true,
auto_width = false, auto_width = false,
on_click = DEFAULT_NIL,
on_rclick = DEFAULT_NIL,
} }
function Label:init(args) function Label:init(args)
self:setText(args.text) self:setText(args.text)
if not self.text_hpen then
self.text_hpen = ((tonumber(self.text_pen) or tonumber(self.text_pen.fg) or 0) + 8) % 16
end
end end
function Label:setText(text) function Label:setText(text)
@ -374,11 +380,21 @@ function Label:getTextWidth()
end end
function Label:onRenderBody(dc) function Label:onRenderBody(dc)
render_text(self,dc,0,0,self.text_pen,self.text_dpen,is_disabled(self)) local text_pen = self.text_pen
if self:getMousePos() and (self.on_click or self.on_rclick) then
text_pen = self.text_hpen
end
render_text(self,dc,0,0,text_pen,self.text_dpen,is_disabled(self))
end end
function Label:onInput(keys) function Label:onInput(keys)
if not is_disabled(self) then if not is_disabled(self) then
if keys._MOUSE_L_DOWN and self:getMousePos() and self.on_click then
self:on_click()
end
if keys._MOUSE_R_DOWN and self:getMousePos() and self.on_rclick then
self:on_rclick()
end
return check_text_keys(self, keys) return check_text_keys(self, keys)
end end
end end

@ -136,7 +136,8 @@ function GmEditorUi:init(args)
subviews={ subviews={
mainList, mainList,
widgets.Label{text={{text="<no item>",id="name"},{gap=1,text="Help",key=keybindings.help.key,key_sep = '()'}}, view_id = 'lbl_current_item',frame = {l=1,t=1,yalign=0}}, widgets.Label{text={{text="<no item>",id="name"},{gap=1,text="Help",key=keybindings.help.key,key_sep = '()'}}, view_id = 'lbl_current_item',frame = {l=1,t=1,yalign=0}},
widgets.Label{text={{text="Search",key=keybindings.start_filter.key,key_sep = '()'},{text=": "}},frame={l=1,t=2}}, widgets.Label{text={{text="Search",key=keybindings.start_filter.key,key_sep = '()'},{text=": "}},frame={l=1,t=2},
on_click=function() self:enable_input(true) end},
widgets.EditField{frame={l=12,t=2},active=false,on_change=self:callback('text_input'),on_submit=self:callback("enable_input",false),view_id="filter_input"}, widgets.EditField{frame={l=12,t=2},active=false,on_change=self:callback('text_input'),on_submit=self:callback("enable_input",false),view_id="filter_input"},
--widgets.Label{text="BLAH2"} --widgets.Label{text="BLAH2"}
} }