[MENUDEF] Flag-based option item.

Moderator: GZDoom Developers

Post Reply
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

[MENUDEF] Flag-based option item.

Post by Accensus »

Something like compatibility flags but not hardcoded. I'm using a modified version of OptionMenuItemOption here, but as of the time of this post I've had to blatantly copy-paste this class over four times in the time span of a month to use it in other mods. Can something like this be added natively?

Example of the code above used in MENUDEF:

Code: Select all

StaticText "----- Visibility Options -----", "Teal"
VisibilityOption "Player Name", "wt_lite_adv_visibility", "OnOff", 0
VisibilityOption "Total Kills", "wt_lite_adv_visibility", "OnOff", 1
VisibilityOption "Genocide Rank", "wt_lite_adv_visibility", "OnOff", 2
VisibilityOption "Genocide Medal", "wt_lite_adv_visibility", "OnOff", 3
VisibilityOption "Player Rank", "wt_lite_adv_visibility", "OnOff", 4
VisibilityOption "Player Experience", "wt_lite_adv_visibility", "OnOff", 5
VisibilityOption "Player Medal", "wt_lite_adv_visibility", "OnOff", 6
StaticText ""
VisibilityOption "Weapon Name", "wt_lite_adv_visibility", "OnOff", 7
VisibilityOption "Weapon Rank", "wt_lite_adv_visibility", "OnOff", 8
VisibilityOption "Weapon Kills", "wt_lite_adv_visibility", "OnOff", 9
VisibilityOption "Weapon Medals", "wt_lite_adv_visibility", "OnOff", 10
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: [MENUDEF] Flag-based option item.

Post by Graf Zahl »

If you can clean up your code to be generically usable, there's no reason not to add it.
The entire menu is script code anyway, so there's no reason to redo it all over again.
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

Re: [MENUDEF] Flag-based option item.

Post by Accensus »

How's this?

Code: Select all

class OptionMenuItemFlagOption : OptionMenuItemOption
{
	int mBitShift;

	OptionMenuItemFlagOption Init(String label, Name command, Name values, int bitShift, CVar greycheck = null, int center = 0)
	{
		Super.Init(label, command, values, greycheck, center);
		mBitShift = bitShift;

		return self;
	}

	override int GetSelection()
	{
		int Selection = 0;
		int cnt = OptionValues.GetCount(mValues);
		if (cnt > 0 && mCVar != null)
		{
			if (OptionValues.GetTextValue(mValues, 0).Length() == 0)
			{
				int CurrentFlags = mCVar.GetInt();

				for (int i = 0; i < cnt; i++)
				{
					int OptionValue = int(OptionValues.GetValue(mValues, i));
					if (CurrentFlags & OptionValue << mBitShift)
					{
						Selection = i;
						break;
					}
				}
			}
		}
		return Selection;
	}

	override void SetSelection(int Selection)
	{
		int cnt = OptionValues.GetCount(mValues);
		if (cnt > 0 && mCVar != null)
		{
			if (OptionValues.GetTextValue(mValues, 0).Length() == 0)
			{
				int OptionValue = int(OptionValues.GetValue(mValues, Selection));
				int CurrentFlags = mCVar.GetInt();

				switch (OptionValue)
				{
					case 0: CurrentFlags &= ~(1 << mBitShift); break;
					case 1: CurrentFlags |= (1 << mBitShift); break;
				}
				mCVar.SetInt(CurrentFlags);
			}
		}
	}
}
What I did was remove the comment and made the name more generic. Also added "graycheck" and "center" because those were missing for some reason.
Not sure if there's anything else I could do.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: [MENUDEF] Flag-based option item.

Post by Graf Zahl »

It would have been easier if you had made a PR and attached a small demo to check it out.
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”