[MENUDEF] Flag-based option item.

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: [MENUDEF] Flag-based option item.

Re: [MENUDEF] Flag-based option item.

by Graf Zahl » Sat Oct 24, 2020 9:50 am

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

Re: [MENUDEF] Flag-based option item.

by Accensus » Sat Oct 17, 2020 5:17 am

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.

Re: [MENUDEF] Flag-based option item.

by Graf Zahl » Sat Oct 17, 2020 4:56 am

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.

[MENUDEF] Flag-based option item.

by Accensus » Sat Oct 17, 2020 4:38 am

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

Top