WindowMenu - Better way to add checkmarks?

Posted by Wuggly on Mon 28 Mar 2016 10:01 PM — 3 posts, 14,242 views.

USA #0
I was wondering if anyone has come up with a better way to put that checkmark next to menu items in the WindowMenu function.

I have different settings that can be changed and when they are changed, it puts a checkmark in the menu next to whatever you changed, however it is making the code massive.

Currently I have it so it runs through a bunch of if statements before doing the WindowMenu function so it can build the menu with the checkmarks in the right spots depending on what all they've selected.

Here's a small part of it so you can see what I mean.

 elseif GetVariable("avatar_checked") == "one" and GetVariable("castle_checked") == "two" and GetVariable("bgColour") == nil then
    menustring_settings ="! >Avatar Image | +Default | - | Browse... | < | >Background Image | Castle 1 | +Castle 2 | Castle 3 | - | Browse... | < | - | Background Color | - | List equipment stats "  
 elseif GetVariable("avatar_checked") == "one" and GetVariable("castle_checked") == "one" and GetVariable("bgColour") == nil then
    menustring_settings ="! >Avatar Image | +Default | - | Browse... | < | >Background Image | +Castle 1 | Castle 2 | Castle 3 | - | Browse... | < | - | Background Color | - | List equipment stats " 
 elseif GetVariable("avatar_checked") == "one" and GetVariable("castle_checked") == "three" and GetVariable("bgColour") == nil then
    menustring_settings ="! >Avatar Image | +Default | - | Browse... | < | >Background Image | Castle 1 | Castle 2 | +Castle 3 | - | Browse... | < | - | Background Color | - | List equipment stats "  
 elseif GetVariable("avatar_checked") == "one" and GetVariable("castle_checked") == "browse" and GetVariable("bgColour") == nil then
    menustring_settings ="! >Avatar Image | +Default | - | Browse... | < | >Background Image | Castle 1 | Castle 2 | Castle 3 | - | +Browse... | < | - | Background Color | - | List equipment stats "  


I just cannot think of an efficient way to do this with the checkmarks. I have tried a few different ways for it to know what all has a checkmark, but still ended up with a massive code.

Any ideas?
Australia Forum Administrator #1
*boggle*

No, that is very inefficient. Try:


menu = {
  [1] = "! >Avatar Image",
  [2] = "Default",
  [3] = "-",
  [4] = "Browse...",
  [5] = "<",
  [6] = ">Background Image",
  [7] = "Castle 1",
  [8] = "Castle 2",
  [9] = "Castle 3",
  [10] = "-",
  [11] = "Browse...",
  [12] = "<",
  [13] = "-",
  [14] = "Background Color",
  [15] = "-",
  [16] = "List equipment stats",
  }

function checkItem (which)
  menu [which] = "+" .. menu [which]
end -- checkItem

if GetVariable("avatar_checked") then
   checkItem (2)
end -- if

if GetVariable("castle_checked") == "one" then
  checkItem (7)
elseif GetVariable("castle_checked") == "two" then
  checkItem (8)
elseif GetVariable("castle_checked") == "three" then
  checkItem (9)
end -- if 

print (table.concat (menu, "|"))


Output:


! >Avatar Image|+Default|-|Browse...|<|>Background Image|Castle 1|+Castle 2|Castle 3|-|Browse...|<|-|Background Color|-|List equipment stats


You could improve on the above, but that shows the general idea.
USA #2
Works perfectly.

Thanks for helping making this menu much more efficient and now I can add more menu items to it easily like adding more default avatars =)

Also doing it that way, putting the checkmarks after checking the variables makes it so it can remember where the checkmarks are for the next session which is perfect and made it simple for the first time run setting the defaults.

Very grateful!

Here's the beginning of the menu code now

function settings_click_menu ()
 menu = {
  [1] = "! >Avatar Image",
  [2] = "Default",
  [3] = "-",
  [4] = "Browse...",
  [5] = "<",
  [6] = ">Background Image",
  [7] = "Castle 1",
  [8] = "Castle 2",
  [9] = "Castle 3",
  [10] = "-",
  [11] = "Browse...",
  [12] = "<",
  [13] = "-",
  [14] = "Background Color",
  [15] = "-",
  [16] = "List equipment stats",
  }
  
 if GetVariable("castle_checked") == "one" or GetVariable("castle_checked") == nil then
  checkItem (7)
 elseif GetVariable("castle_checked") == "two" then
  checkItem (8)
 elseif GetVariable("castle_checked") == "three" then
  checkItem (9)
 elseif GetVariable("castle_checked") == "browse" then
  checkItem (11)
 end -- if 
 
 if GetVariable("avatar_checked") == "one" or GetVariable("avatar_checked") == nil then
   checkItem (2)
 elseif GetVariable("avatar_checked") == "browse" then
   checkItem (4)
 end -- if

 if GetVariable("bgColour") ~= nil then
  checkItem (14)
  menu [14] = menu [14] .. " [" .. RGBColourToName(bgColour) .. "]"
 end -- if
 result = WindowMenu (titlebar_window, 65, 15, table.concat (menu, "|"))


Don't know why I originally had the browse part of the avatar_checked called two but have changed it to browse like the castle_checked one. In fact naming it castle_checked seems kind of dumb too, should probably call it like background_checked or bg_checked lol.

And doing the menu that way, has allowed me to do cool things like when they select a background color instead of an image, it tells them the color they have picked in the menu. =)

Anyways, thanks again!