Skip to content

Commit 0f6729a

Browse files
committed
Use TabBar from widgets
1 parent e4ba126 commit 0f6729a

File tree

3 files changed

+9
-270
lines changed

3 files changed

+9
-270
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ that repo.
2727
- Collapsing all bins or a single bin
2828
- Collapsing all categories
2929
-@ `devel/hello-world`: updated to use ZScreen
30+
-@ Update `gui/control-panel` and `gui/launcher` to use TabBar from widgets.lua
3031

3132
## Removed
3233

gui/control-panel.lua

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -746,143 +746,6 @@ function RepeatAutostart:on_submit()
746746
self:refresh()
747747
end
748748

749-
--
750-
-- Tab
751-
---
752-
753-
local to_pen = dfhack.pen.parse
754-
local active_tab_pens = {
755-
text_mode_tab_pen=to_pen{fg=COLOR_YELLOW},
756-
text_mode_label_pen=to_pen{fg=COLOR_WHITE},
757-
lt=to_pen{tile=1005, write_to_lower=true},
758-
lt2=to_pen{tile=1006, write_to_lower=true},
759-
t=to_pen{tile=1007, fg=COLOR_BLACK, write_to_lower=true, top_of_text=true},
760-
rt2=to_pen{tile=1008, write_to_lower=true},
761-
rt=to_pen{tile=1009, write_to_lower=true},
762-
lb=to_pen{tile=1015, write_to_lower=true},
763-
lb2=to_pen{tile=1016, write_to_lower=true},
764-
b=to_pen{tile=1017, fg=COLOR_BLACK, write_to_lower=true, bottom_of_text=true},
765-
rb2=to_pen{tile=1018, write_to_lower=true},
766-
rb=to_pen{tile=1019, write_to_lower=true},
767-
}
768-
769-
local inactive_tab_pens = {
770-
text_mode_tab_pen=to_pen{fg=COLOR_BROWN},
771-
text_mode_label_pen=to_pen{fg=COLOR_DARKGREY},
772-
lt=to_pen{tile=1000, write_to_lower=true},
773-
lt2=to_pen{tile=1001, write_to_lower=true},
774-
t=to_pen{tile=1002, fg=COLOR_WHITE, write_to_lower=true, top_of_text=true},
775-
rt2=to_pen{tile=1003, write_to_lower=true},
776-
rt=to_pen{tile=1004, write_to_lower=true},
777-
lb=to_pen{tile=1010, write_to_lower=true},
778-
lb2=to_pen{tile=1011, write_to_lower=true},
779-
b=to_pen{tile=1012, fg=COLOR_WHITE, write_to_lower=true, bottom_of_text=true},
780-
rb2=to_pen{tile=1013, write_to_lower=true},
781-
rb=to_pen{tile=1014, write_to_lower=true},
782-
}
783-
784-
Tab = defclass(Tabs, widgets.Widget)
785-
Tab.ATTRS{
786-
id=DEFAULT_NIL,
787-
label=DEFAULT_NIL,
788-
on_select=DEFAULT_NIL,
789-
get_pens=DEFAULT_NIL,
790-
}
791-
792-
function Tab:preinit(init_table)
793-
init_table.frame = init_table.frame or {}
794-
init_table.frame.w = #init_table.label + 4
795-
init_table.frame.h = 2
796-
end
797-
798-
function Tab:onRenderBody(dc)
799-
local pens = self.get_pens()
800-
dc:seek(0, 0)
801-
if dfhack.screen.inGraphicsMode() then
802-
dc:char(nil, pens.lt):char(nil, pens.lt2)
803-
for i=1,#self.label do
804-
dc:char(self.label:sub(i,i), pens.t)
805-
end
806-
dc:char(nil, pens.rt2):char(nil, pens.rt)
807-
dc:seek(0, 1)
808-
dc:char(nil, pens.lb):char(nil, pens.lb2)
809-
for i=1,#self.label do
810-
dc:char(self.label:sub(i,i), pens.b)
811-
end
812-
dc:char(nil, pens.rb2):char(nil, pens.rb)
813-
else
814-
local tp = pens.text_mode_tab_pen
815-
dc:char(' ', tp):char('/', tp)
816-
for i=1,#self.label do
817-
dc:char('-', tp)
818-
end
819-
dc:char('\\', tp):char(' ', tp)
820-
dc:seek(0, 1)
821-
dc:char('/', tp):char('-', tp)
822-
dc:string(self.label, pens.text_mode_label_pen)
823-
dc:char('-', tp):char('\\', tp)
824-
end
825-
end
826-
827-
function Tab:onInput(keys)
828-
if Tab.super.onInput(self, keys) then return true end
829-
if keys._MOUSE_L_DOWN and self:getMousePos() then
830-
self.on_select(self.id)
831-
return true
832-
end
833-
end
834-
835-
--
836-
-- TabBar
837-
--
838-
839-
TabBar = defclass(TabBar, widgets.ResizingPanel)
840-
TabBar.ATTRS{
841-
labels=DEFAULT_NIL,
842-
on_select=DEFAULT_NIL,
843-
get_cur_page=DEFAULT_NIL,
844-
}
845-
846-
function TabBar:init()
847-
for idx,label in ipairs(self.labels) do
848-
self:addviews{
849-
Tab{
850-
frame={t=0, l=0},
851-
id=idx,
852-
label=label,
853-
on_select=self.on_select,
854-
get_pens=function()
855-
return self.get_cur_page() == idx and
856-
active_tab_pens or inactive_tab_pens
857-
end,
858-
}
859-
}
860-
end
861-
end
862-
863-
function TabBar:postComputeFrame(body)
864-
local t, l, width = 0, 0, body.width
865-
for _,tab in ipairs(self.subviews) do
866-
if l > 0 and l + tab.frame.w > width then
867-
t = t + 2
868-
l = 0
869-
end
870-
tab.frame.t = t
871-
tab.frame.l = l
872-
l = l + tab.frame.w
873-
end
874-
end
875-
876-
function TabBar:onInput(keys)
877-
if TabBar.super.onInput(self, keys) then return true end
878-
if keys.CUSTOM_CTRL_T then
879-
local zero_idx = self.get_cur_page() - 1
880-
local next_zero_idx = (zero_idx + 1) % #self.labels
881-
self.on_select(next_zero_idx + 1)
882-
return true
883-
end
884-
end
885-
886749
--
887750
-- ControlPanel
888751
--
@@ -899,7 +762,7 @@ ControlPanel.ATTRS {
899762

900763
function ControlPanel:init()
901764
self:addviews{
902-
TabBar{
765+
widgets.TabBar{
903766
frame={t=0},
904767
labels={
905768
'Fort',
@@ -910,7 +773,9 @@ function ControlPanel:init()
910773
'Autostart',
911774
},
912775
on_select=self:callback('set_page'),
913-
get_cur_page=function() return self.subviews.pages:getSelected() end
776+
get_cur_page=function() return self.subviews.pages:getSelected() end,
777+
key='CUSTOM_ALT_T',
778+
key_back='CUSTOM_ALT_R',
914779
},
915780
widgets.Pages{
916781
view_id='pages',

gui/launcher.lua

Lines changed: 4 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -326,135 +326,6 @@ end
326326
-- HelpPanel
327327
--
328328

329-
local to_pen = dfhack.pen.parse
330-
local active_tab_pens = {
331-
text_mode_tab_pen=to_pen{fg=COLOR_YELLOW},
332-
text_mode_label_pen=to_pen{fg=COLOR_WHITE},
333-
lt=to_pen{tile=1005, write_to_lower=true},
334-
lt2=to_pen{tile=1006, write_to_lower=true},
335-
t=to_pen{tile=1007, fg=COLOR_BLACK, write_to_lower=true, top_of_text=true},
336-
rt2=to_pen{tile=1008, write_to_lower=true},
337-
rt=to_pen{tile=1009, write_to_lower=true},
338-
lb=to_pen{tile=1015, write_to_lower=true},
339-
lb2=to_pen{tile=1016, write_to_lower=true},
340-
b=to_pen{tile=1017, fg=COLOR_BLACK, write_to_lower=true, bottom_of_text=true},
341-
rb2=to_pen{tile=1018, write_to_lower=true},
342-
rb=to_pen{tile=1019, write_to_lower=true},
343-
}
344-
345-
local inactive_tab_pens = {
346-
text_mode_tab_pen=to_pen{fg=COLOR_BROWN},
347-
text_mode_label_pen=to_pen{fg=COLOR_DARKGREY},
348-
lt=to_pen{tile=1000, write_to_lower=true},
349-
lt2=to_pen{tile=1001, write_to_lower=true},
350-
t=to_pen{tile=1002, fg=COLOR_WHITE, write_to_lower=true, top_of_text=true},
351-
rt2=to_pen{tile=1003, write_to_lower=true},
352-
rt=to_pen{tile=1004, write_to_lower=true},
353-
lb=to_pen{tile=1010, write_to_lower=true},
354-
lb2=to_pen{tile=1011, write_to_lower=true},
355-
b=to_pen{tile=1012, fg=COLOR_WHITE, write_to_lower=true, bottom_of_text=true},
356-
rb2=to_pen{tile=1013, write_to_lower=true},
357-
rb=to_pen{tile=1014, write_to_lower=true},
358-
}
359-
360-
Tab = defclass(Tabs, widgets.Widget)
361-
Tab.ATTRS{
362-
id=DEFAULT_NIL,
363-
label=DEFAULT_NIL,
364-
on_select=DEFAULT_NIL,
365-
get_pens=DEFAULT_NIL,
366-
}
367-
368-
function Tab:preinit(init_table)
369-
init_table.frame = init_table.frame or {}
370-
init_table.frame.w = #init_table.label + 4
371-
init_table.frame.h = 2
372-
end
373-
374-
function Tab:onRenderBody(dc)
375-
local pens = self.get_pens()
376-
dc:seek(0, 0)
377-
if dfhack.screen.inGraphicsMode() then
378-
dc:char(nil, pens.lt):char(nil, pens.lt2)
379-
for i=1,#self.label do
380-
dc:char(self.label:sub(i,i), pens.t)
381-
end
382-
dc:char(nil, pens.rt2):char(nil, pens.rt)
383-
dc:seek(0, 1)
384-
dc:char(nil, pens.lb):char(nil, pens.lb2)
385-
for i=1,#self.label do
386-
dc:char(self.label:sub(i,i), pens.b)
387-
end
388-
dc:char(nil, pens.rb2):char(nil, pens.rb)
389-
else
390-
local tp = pens.text_mode_tab_pen
391-
dc:char(' ', tp):char('/', tp)
392-
for i=1,#self.label do
393-
dc:char('-', tp)
394-
end
395-
dc:char('\\', tp):char(' ', tp)
396-
dc:seek(0, 1)
397-
dc:char('/', tp):char('-', tp)
398-
dc:string(self.label, pens.text_mode_label_pen)
399-
dc:char('-', tp):char('\\', tp)
400-
end
401-
end
402-
403-
function Tab:onInput(keys)
404-
if Tab.super.onInput(self, keys) then return true end
405-
if keys._MOUSE_L_DOWN and self:getMousePos() then
406-
self.on_select(self.id)
407-
return true
408-
end
409-
end
410-
411-
TabBar = defclass(TabBar, widgets.ResizingPanel)
412-
TabBar.ATTRS{
413-
labels=DEFAULT_NIL,
414-
on_select=DEFAULT_NIL,
415-
get_cur_page=DEFAULT_NIL,
416-
}
417-
418-
function TabBar:init()
419-
for idx,label in ipairs(self.labels) do
420-
self:addviews{
421-
Tab{
422-
frame={t=0, l=0},
423-
id=idx,
424-
label=label,
425-
on_select=self.on_select,
426-
get_pens=function()
427-
return self.get_cur_page() == idx and
428-
active_tab_pens or inactive_tab_pens
429-
end,
430-
}
431-
}
432-
end
433-
end
434-
435-
function TabBar:postComputeFrame(body)
436-
local t, l, width = 0, 0, body.width
437-
for _,tab in ipairs(self.subviews) do
438-
if l > 0 and l + tab.frame.w > width then
439-
t = t + 2
440-
l = 0
441-
end
442-
tab.frame.t = t
443-
tab.frame.l = l
444-
l = l + tab.frame.w
445-
end
446-
end
447-
448-
function TabBar:onInput(keys)
449-
if TabBar.super.onInput(self, keys) then return true end
450-
if keys.CUSTOM_CTRL_T then
451-
local zero_idx = self.get_cur_page() - 1
452-
local next_zero_idx = (zero_idx + 1) % #self.labels
453-
self.on_select(next_zero_idx + 1)
454-
return true
455-
end
456-
end
457-
458329
HelpPanel = defclass(HelpPanel, widgets.Panel)
459330
HelpPanel.ATTRS{
460331
autoarrange_subviews=true,
@@ -475,14 +346,16 @@ function HelpPanel:init()
475346
self.cur_entry = ''
476347

477348
self:addviews{
478-
TabBar{
349+
widgets.TabBar{
479350
frame={t=0, l=0},
480351
labels={
481352
'Help',
482353
'Output',
483354
},
484355
on_select=function(idx) self.subviews.pages:setSelected(idx) end,
485-
get_cur_page=function() return self.subviews.pages:getSelected() end
356+
get_cur_page=function() return self.subviews.pages:getSelected() end,
357+
key='CUSTOM_ALT_T',
358+
key_back='CUSTOM_ALT_R',
486359
},
487360
widgets.Pages{
488361
view_id='pages',

0 commit comments

Comments
 (0)