item selection, callback skeleton

develop
Myk Taylor 2023-02-17 14:24:21 -08:00
parent c59ad78f40
commit daf691839f
No known key found for this signature in database
1 changed files with 109 additions and 30 deletions

@ -182,6 +182,10 @@ end
ItemLine = defclass(ItemLine, widgets.Panel) ItemLine = defclass(ItemLine, widgets.Panel)
ItemLine.ATTRS{ ItemLine.ATTRS{
idx=DEFAULT_NIL, idx=DEFAULT_NIL,
is_selected_fn=DEFAULT_NIL,
on_select=DEFAULT_NIL,
on_filter=DEFAULT_NIL,
on_clear_filter=DEFAULT_NIL,
} }
function ItemLine:init() function ItemLine:init()
@ -189,16 +193,38 @@ function ItemLine:init()
self.visible = function() return #get_cur_filters() >= self.idx end self.visible = function() return #get_cur_filters() >= self.idx end
self:addviews{ self:addviews{
widgets.Label{ widgets.Label{
frame={t=0, l=23}, frame={t=0, l=0},
text='*',
auto_width=true,
visible=self.is_selected_fn,
},
widgets.Label{
frame={t=0, r=0},
text='*',
auto_width=true,
visible=self.is_selected_fn,
on_click=self.on_filter,
},
widgets.Label{
frame={t=0, l=25},
text={ text={
{tile=get_button_start_pen}, {tile=get_button_start_pen},
{gap=6, tile=get_button_end_pen}, {gap=6, tile=get_button_end_pen},
},
auto_width=true,
on_click=function() self.on_filter(self.idx) end,
},
widgets.Label{
frame={t=0, l=33},
text={
{tile=get_button_start_pen}, {tile=get_button_start_pen},
{gap=1, tile=get_button_end_pen}, {gap=1, tile=get_button_end_pen},
}, },
auto_width=true,
on_click=function() self.on_clear_filter(self.idx) end,
}, },
widgets.Label{ widgets.Label{
frame={t=0, l=0}, frame={t=0, l=2},
text={ text={
{width=21, text=self:callback('get_item_line_text')}, {width=21, text=self:callback('get_item_line_text')},
{gap=3, text='filter', pen=COLOR_GREEN}, {gap=3, text='filter', pen=COLOR_GREEN},
@ -215,6 +241,13 @@ function ItemLine:reset()
self.available = nil self.available = nil
end end
function ItemLine:onInput(keys)
if keys._MOUSE_L_DOWN and self:getMousePos() then
self.on_select(self.idx)
end
return ItemLine.super.onInput(self, keys)
end
function get_desc(filter) function get_desc(filter)
local desc = 'Unknown' local desc = 'Unknown'
if filter.has_tool_use and filter.has_tool_use > -1 then if filter.has_tool_use and filter.has_tool_use > -1 then
@ -301,6 +334,8 @@ PlannerOverlay.ATTRS{
} }
function PlannerOverlay:init() function PlannerOverlay:init()
self.selected = 1
local main_panel = widgets.Panel{ local main_panel = widgets.Panel{
view_id='main', view_id='main',
frame={t=0, l=0, r=0, h=14}, frame={t=0, l=0, r=0, h=14},
@ -308,6 +343,14 @@ function PlannerOverlay:init()
frame_background=gui.CLEAR_PEN, frame_background=gui.CLEAR_PEN,
} }
local function make_is_selected_fn(idx)
return function() return self.selected == idx end
end
local function on_select_fn(idx)
self.selected = idx
end
main_panel:addviews{ main_panel:addviews{
widgets.Label{ widgets.Label{
frame={}, frame={},
@ -315,10 +358,22 @@ function PlannerOverlay:init()
text='No items required.', text='No items required.',
visible=function() return #get_cur_filters() == 0 end, visible=function() return #get_cur_filters() == 0 end,
}, },
ItemLine{view_id='item1', frame={t=0, l=0, r=0}, idx=1}, ItemLine{view_id='item1', frame={t=0, l=0, r=0}, idx=1,
ItemLine{view_id='item2', frame={t=2, l=0, r=0}, idx=2}, is_selected_fn=make_is_selected_fn(1), on_select=on_select_fn,
ItemLine{view_id='item3', frame={t=4, l=0, r=0}, idx=3}, on_filter=self:callback('filter'),
ItemLine{view_id='item4', frame={t=6, l=0, r=0}, idx=4}, on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item2', frame={t=2, l=0, r=0}, idx=2,
is_selected_fn=make_is_selected_fn(2), on_select=on_select_fn,
on_filter=self:callback('filter'),
on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item3', frame={t=4, l=0, r=0}, idx=3,
is_selected_fn=make_is_selected_fn(3), on_select=on_select_fn,
on_filter=self:callback('filter'),
on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item4', frame={t=6, l=0, r=0}, idx=4,
is_selected_fn=make_is_selected_fn(4), on_select=on_select_fn,
on_filter=self:callback('filter'),
on_clear_filter=self:callback('clear_filter')},
widgets.CycleHotkeyLabel{ widgets.CycleHotkeyLabel{
view_id='stairs_top_subtype', view_id='stairs_top_subtype',
frame={t=4, l=4}, frame={t=4, l=4},
@ -354,31 +409,42 @@ function PlannerOverlay:init()
}, },
visible=is_choosing_area, visible=is_choosing_area,
}, },
widgets.CycleHotkeyLabel{ widgets.Panel{
view_id='safety', visible=function() return #get_cur_filters() > 0 end,
frame={b=0, l=2}, subviews={
key='CUSTOM_G',
label='Safety: ',
options={
{label='None', value='none'},
{label='Magma', value='magma'},
{label='Fire', value='fire'},
},
},
widgets.HotkeyLabel{ widgets.HotkeyLabel{
frame={b=1, l=0}, frame={b=1, l=0},
key='SELECT', key='SELECT',
label='Choose item', label='Choose item',
on_activate=function() self:choose(self.selected) end,
enabled=function()
return (self.subviews['item'..self.selected].available or 0) > 0
end,
}, },
widgets.HotkeyLabel{ widgets.HotkeyLabel{
frame={b=1, l=21}, frame={b=1, l=21},
key='CUSTOM_F', key='CUSTOM_F',
label='Filter', label='Filter',
on_activate=function() self:filter(self.selected) end,
}, },
widgets.HotkeyLabel{ widgets.HotkeyLabel{
frame={b=1, l=33}, frame={b=1, l=33},
key='CUSTOM_X', key='CUSTOM_X',
label='Clear filter', label='Clear filter',
on_activate=function() self:clear_filter(self.selected) end,
},
widgets.CycleHotkeyLabel{
view_id='safety',
frame={b=0, l=2},
key='CUSTOM_G',
label='Safety: ',
options={
{label='None', value='none'},
{label='Magma', value='magma'},
{label='Fire', value='fire'},
},
},
},
}, },
} }
@ -418,6 +484,18 @@ function PlannerOverlay:reset()
reset_counts_flag = false reset_counts_flag = false
end end
function PlannerOverlay:choose(idx)
print('choose', idx)
end
function PlannerOverlay:filter(idx)
print('filter', idx)
end
function PlannerOverlay:clear_filter(idx)
print('clear_filter', idx)
end
function PlannerOverlay:onInput(keys) function PlannerOverlay:onInput(keys)
if not is_plannable() then return false end if not is_plannable() then return false end
if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
@ -425,6 +503,7 @@ function PlannerOverlay:onInput(keys)
uibs.selection_pos:clear() uibs.selection_pos:clear()
return true return true
end end
self.selected = 1
self:reset() self:reset()
return false return false
end end