Murga-Projects Forums
another menu thing - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: another menu thing (/showthread.php?tid=384)


another menu thing - mikshaw - 05-02-2010 01:47 AM

Playing around with homemade menu options again. This one is kinda visually similar to the collapsable browser I was working on earlier. It makes use of a pack of buttons that "expand" when clicked. This example only changes the label text and size a little, but it could be easily modified to load a thumbnail image or other such thing. It's heavily commented, so that should help in customizing. I haven't yet added the ability for the buttons to actually DO anything other than change their own appearance =o)

Screenshot attached.

EDIT: added a conditional placeholder for a subsequent click on the expanded button.

Code:
#!/usr/bin/env murgaLua

-- Creates a vertical scroll containing an arbitrary number of
-- buttons which grow and change labels when clicked, and collapse
-- when another button is clicked
-- Additional button behavior, such as secondary action by clicking
-- the active button (a logical next step), is not implemented yet

mbw=64 -- menu button width
mbnh=30 -- button normal height
mbeh=200 -- expanded height

-- These labels are displayed on buttons in their inactive state.
-- Another table of equal size could be created to store info for
-- active state, or tables of arbitrary size could be generated
-- and used for data in both the build_menu() loop and butt_cb()
menu_labels={"one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
"nineteen", "twenty", "twenty-one", "twenty-two"}

-- Notes on customizing this function:
-- active_butt = previously selected button
-- self = currently selected button
-- (1) This line is auxiliary and disposable
-- (2) Try setting the label to an image or text from a table/file
-- (3) See (1)
function butt_cb(self)
if self:h()==mbeh then -- if self is already expanded
-- put stuff to do on second click here
else
-- expand new button
  active_butt:size(mbw,mbnh) -- collapse previous button
  active_butt:label(prev_label) -- reset to initial label
  active_butt:labelsize(def_sz) -- reset to initial label size (1)
  prev_label=self:label() -- backup the initial label of current button
  self:label("BIG\n"..prev_label) -- change the label text of current button (2)
  self:labelsize(def_sz*2) -- increase label size of current button (3)
  self:size(mbw,mbeh) -- expand current button
  active_butt=self -- let everyone know which button to collapse next time
  menu_scroll:redraw() -- visual clean up
end
end

function build_menu(x,y,w,h)
menu_button={}
menu_scroll=fltk:Fl_Scroll(x,y,w,h)
menu_scroll:type(2) -- FL_VERTICAL doesn't seem to work
menu_scroll:box(fltk.FL_DOWN_BOX) -- just for looks
menu_pack=fltk:Fl_Pack(x,y,w-20,h-20) -- note 20 pixel space for scrollbar
fltk:Fl_End() -- end pack
fltk:Fl_End() -- end scroll
-- create buttons from menu_labels table
-- This could be made dynamic by, for example,
-- populating the table with a list of files
for i,v in pairs(menu_labels) do
  menu_button[i]=fltk:Fl_Button(0,0,mbw,mbnh,v)
  menu_button[i]:callback(butt_cb)
  menu_pack:add(menu_button[i])
end
end

w=fltk:Fl_Window(300,600,"menu test 2")
-- x,y,w,h (note: w doesn't need to be mbeh)
build_menu(10,10,mbeh,500)
-- set a default font size (needed only if butt_cb uses it)
def_sz=menu_button[1]:labelsize()
-- set first button active
active_butt=menu_button[1]
-- initialize a prev_label for first use in butt_cb
prev_label=menu_button[1]:label()
--active_butt:do_callback() -- optional
w:show()
Fl:run()