Merge pull request #198 from chrox/page_button

add page up/down buttons in menus that have more than one pages
pull/201/merge
{Qingping,Dave} Hou 11 years ago
commit 68af46f495

@ -1,10 +1,12 @@
require "ui/widget/image"
require "ui/widget/container" require "ui/widget/container"
--[[ --[[
a button widget a button widget that shows text or a icon and handles callback when tapped
--]] --]]
Button = InputContainer:new{ Button = InputContainer:new{
text = nil, -- mandatory text = nil, -- mandatory
icon = nil,
preselect = false, preselect = false,
callback = nil, callback = nil,
enabled = true, enabled = true,
@ -19,15 +21,22 @@ Button = InputContainer:new{
} }
function Button:init() function Button:init()
self.text_widget = TextWidget:new{ if self.text then
text = self.text, self.label_widget = TextWidget:new{
bgcolor = 0.0, text = self.text,
fgcolor = self.enabled and 1.0 or 0.5, bgcolor = 0.0,
face = Font:getFace(self.text_font_face, self.text_font_size) fgcolor = self.enabled and 1.0 or 0.5,
} face = Font:getFace(self.text_font_face, self.text_font_size)
local text_size = self.text_widget:getSize() }
else
self.label_widget = ImageWidget:new{
file = self.icon,
dim = self.enabled,
}
end
local widget_size = self.label_widget:getSize()
if self.width == nil then if self.width == nil then
self.width = text_size.w self.width = widget_size.w
end end
-- set FrameContainer content -- set FrameContainer content
self[1] = FrameContainer:new{ self[1] = FrameContainer:new{
@ -39,9 +48,9 @@ function Button:init()
CenterContainer:new{ CenterContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = self.width,
h = text_size.h h = widget_size.h
}, },
self.text_widget, self.label_widget,
} }
} }
if self.preselect then if self.preselect then
@ -75,12 +84,48 @@ end
function Button:enable() function Button:enable()
self.enabled = true self.enabled = true
self.text_widget.fgcolor = 1.0 if self.text then
self.label_widget.fgcolor = self.enabled and 1.0 or 0.5
else
self.label_widget.dim = not self.enabled
end
end end
function Button:disable() function Button:disable()
self.enabled = false self.enabled = false
self.text_widget.fgcolor = 0.5 if self.text then
self.label_widget.fgcolor = self.enabled and 1.0 or 0.5
else
self.label_widget.dim = not self.enabled
end
end
function Button:enableDisable(enable)
if enable then
self:enable()
else
self:disable()
end
end
function Button:hide()
if self.icon then
self.label_widget.hide = true
end
end
function Button:show()
if self.icon then
self.label_widget.hide = false
end
end
function Button:showHide(show)
if show then
self:show()
else
self:hide()
end
end end
function Button:onTapSelect() function Button:onTapSelect()

@ -6,8 +6,10 @@ require "ui/image"
ImageWidget shows an image from a file ImageWidget shows an image from a file
--]] --]]
ImageWidget = Widget:new{ ImageWidget = Widget:new{
invert = nil,
file = nil, file = nil,
invert = nil,
dim = nil,
hide = nil,
_bb = nil _bb = nil
} }
@ -34,10 +36,14 @@ function ImageWidget:paintTo(bb, x, y)
w = size.w, w = size.w,
h = size.h h = size.h
} }
if self.hide then return end
bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h) bb:blitFrom(self._bb, x, y, 0, 0, size.w, size.h)
if self.invert then if self.invert then
bb:invertRect(x, y, size.w, size.h) bb:invertRect(x, y, size.w, size.h)
end end
if self.dim then
bb:dimRect(x, y, size.w, size.h)
end
end end
function ImageWidget:free() function ImageWidget:free()

@ -1,6 +1,7 @@
require "ui/widget/container" require "ui/widget/container"
require "ui/widget/focusmanager" require "ui/widget/focusmanager"
require "ui/widget/infomessage" require "ui/widget/infomessage"
require "ui/widget/button"
require "ui/widget/text" require "ui/widget/text"
require "ui/widget/group" require "ui/widget/group"
require "ui/widget/span" require "ui/widget/span"
@ -287,8 +288,27 @@ function Menu:init()
end end
-- group for items -- group for items
self.item_group = VerticalGroup:new{} self.item_group = VerticalGroup:new{}
self.page_info = TextWidget:new{ -- group for page info
face = self.fface, self.page_info_left_chev = Button:new{
icon = "resources/icons/appbar.chevron.left.png",
callback = function() self:onPrevPage() end,
bordersize = 0,
}
self.page_info_right_chev = Button:new{
icon = "resources/icons/appbar.chevron.right.png",
callback = function() self:onNextPage() end,
bordersize = 0,
}
self.page_info_left_chev:hide()
self.page_info_right_chev:hide()
self.page_info_text = TextWidget:new{
text = "",
face = self.fface,
}
self.page_info = HorizontalGroup:new{
self.page_info_left_chev,
self.page_info_text,
self.page_info_right_chev
} }
-- group for menu layout -- group for menu layout
local content = VerticalGroup:new{ local content = VerticalGroup:new{
@ -428,9 +448,13 @@ function Menu:updateItems(select_number)
self.item_group[select_number]:onFocus() self.item_group[select_number]:onFocus()
end end
-- update page information -- update page information
self.page_info.text = _("page ")..self.page.."/"..self.page_num self.page_info_text.text = _("page ")..self.page.."/"..self.page_num
self.page_info_left_chev:showHide(self.page_num > 1)
self.page_info_right_chev:showHide(self.page_num > 1)
self.page_info_left_chev:enableDisable(self.page > 1)
self.page_info_right_chev:enableDisable(self.page < self.page_num)
else else
self.page_info.text = _("no choices available") self.page_info_text.text = _("no choices available")
end end
-- FIXME: this is a dirty hack to clear previous menus -- FIXME: this is a dirty hack to clear previous menus
@ -568,5 +592,3 @@ function Menu:onSwipe(arg, ges_ev)
self:onPrevPage() self:onPrevPage()
end end
end end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 410 B

After

Width:  |  Height:  |  Size: 729 B

Loading…
Cancel
Save