PDA

View Full Version : Yet Another Work-around by Centauri Soldier


Centauri Soldier
12-05-2008, 10:25 AM
Hey fellas,

ever get tired of sifting through millions of lines of code in your On Menu section. No worries here's the trick.

--this table creates your menu responses--best if it's in your globals and menu items can be in any order
tAFMenuItem = {};
tAFMenuItem[1] = {};
tAFMenuItem[1].Text = "&Exit";
tAFMenuItem[1].Code = [[
Application.Exit(0);
]]
tAFMenuItem[1].ID = 199; --this can be set to nil if you dont need a menu ID identifier. Use for duplicate Menu Item Names

--Put this table on startup somewhere
tAFMenuButtonProp = {};
tAFMenuButtonProp.Width = 1;
tAFMenuButtonProp.Height = 1;
tAFMenuButtonProp.Visible = false;

--put this function in your globals
--======================>>>>>>>>>>>>>>>>>>>>>>>>>>>
function Application.ClickMenu(tAFMenuTable) --[[>>
<< >>
<< >>
<<<<<<<<<<<<<<<<<<<<<==========================--]]
for nAFMenuIndex = 1, Table.Count(tAFMenuItem) do

if tAFMenuItem[nAFMenuIndex].ID then

if tAFMenuTable.Text == tAFMenuItem[nAFMenuIndex].Text and tAFMenuTable.ID == tAFMenuItem[nAFMenuIndex].ID then
Page.CreateObject(OBJECT_BUTTON, "btn AF menu code", tAFMenuButtonProp);
Page.SetObjectScript("btn AF menu code", "On Click", tAFMenuItem[nAFMenuIndex].Code);
Page.ClickObject("btn AF menu code");
Page.CreateObject(OBJECT_BUTTON, "btn AF menu code", tAFMenuButtonProp);
break;
end

else

if tAFMenuTable.Text == tAFMenuItem[nAFMenuIndex].Text then
Page.CreateObject(OBJECT_BUTTON, "btn AF menu code", tAFMenuButtonProp);
Page.SetObjectScript("btn AF menu code", "On Click", tAFMenuItem[nAFMenuIndex].Code);
Page.ClickObject("btn AF menu code");
Page.CreateObject(OBJECT_BUTTON, "btn AF menu code", tAFMenuButtonProp);
break;
end

end

end
-->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end--|||||||||||||END FUNCTION|||||||||||||||||
-->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

--call the function in your On Menu section
Application.ClickMenu(e_ItemInfo);

Please let me know if you find a bug,
Enjoy!

~CS

Dermot
12-05-2008, 12:21 PM
But doesn't that mean I end up with more lines of code, except they are now in my Globals which is worse because Globals usually already has 1000's of lines of code. You have to do this for every menu item.
tAFMenuItem = {};
tAFMenuItem[1] = {};
tAFMenuItem[1].Text = "&Exit";
tAFMenuItem[1].Code = [[
Application.Exit(0);
]]
tAFMenuItem[1].ID = 199; --this can be set to nil if you dont need a menu ID identifier. Use for duplicate Menu Item Names

Instead of just this.
if e_ID == 199 then
Application.Exit(0);
end
It just makes no sense to create a button and put the code in the button and then call the button click when you can just call the code from the menu. Maybe I am not getting it but it seems like more work to me and I would rather have all my menu code in the On Menu section instead of cluttering up my Globals.

Centauri Soldier
12-05-2008, 01:53 PM
Hi Dermot,
But doesn't that mean I end up with more lines of code, except they are now in my Globals...

If all you have for your menu code is a couple if lines then this function is not for you. This is for projects (like the one for which I built this) that require hundreds of lines of code for the menu. It is easier to find my section when they are in this table and easier to edit them.

Also, I don't need to worry about the menu IDs (as long as there are no duplicate names, in which case I can put the id into the tAFMenuITem[index].ID table), I can simply list the menu text and then type the code.

As far as your example with the one line of code, you are correct...this would be a complete waste of time. But for those huge projects it works great.

Also, I store all my added functions (such as this one) in a script folder and call them all on start up with a single command line. So for me, the function is not listed in my globals but in a lua file. As far as the table, you can put this anywhere before you call the Application.ClickMenu() function.

I have another function for the table called Application.MenuEdit(). I Put Application.MenuEdit("Exit",[[Application.Exit();]],nil); and the function does the rest. It dynamically adds code to the menu.

Of course this works well with my other function that builds the menu for me too. This is my attempt to shorten the time needed to code a thing. Menus have always been my bane so when I see a chance to strike a blow against them for all sane coders out there I do so. Alas, at some point I'm sure your signature statement will apply to my efforts.

ShadowUK
12-05-2008, 02:12 PM
Application.MenuEdit("Exit",[[Application.Exit();]],nil);

You don't need to say nil, If you don't specify it. It would be nil.

Dermot
12-05-2008, 02:12 PM
I was referring to apps with a lot of menu items. My example was just to show that the normal way takes 3 lines of code per menu item but yours takes 6 lines. Your solution means more lines of code per menu item because of having to build the table. Also if you edit the text of a menu item, you have to edit the table also.

But if you find it easier then go for it. I just have a very hard time seeing how it makes things easier.

Centauri Soldier
12-05-2008, 03:06 PM
You don't need to say nil, If you don't specify it. It would be nil.
True, I just put it in there to be pedantic (or thorough if you prefer that word :D).

Here Dermot, try these (no more tables). Just call them all on start up (run script file) and you will see the beauty of the code. Obviously, I have much to do to this code as far as adding more functions but these ones in the rar file are finished and bug tested with no errors (so far :rolleyes). You should be able to use Menu.AddCode pretty much anywhere but the Menu.Click has to be in the On Menu section with e_ItemInfo as the function's table. This is how it should look. Menu.Click(e_ItemInfo);

Also if you edit the text of a menu item, you have to edit the table also.
Well, you have to do this in any case. But in the case of these tables, there is no specific order required.

Let me know what you think of this approach.