gzdoom-gles/wadsrc/static/zscript/ui/menu/search/menu.zs
Alexander dc666203b7 added option search menu
# Conflicts:
#	src/g_cvars.cpp
#	wadsrc/static/menudef.txt

# Conflicts:
#	src/menu/menu.cpp
2019-05-14 18:59:10 +02:00

212 lines
7.5 KiB
Text

//=============================================================================
//
// Option Search Menu class.
// This menu contains search field, and is dynamically filled with search
// results.
//
//=============================================================================
class os_Menu : OptionMenu
{
override void Init(Menu parent, OptionMenuDescriptor desc)
{
Super.Init(parent, desc);
mDesc.mItems.clear();
addSearchField();
mDesc.mScrollPos = 0;
mDesc.mSelectedItem = 0;
mDesc.CalcIndent();
}
void search(os_Query query)
{
mDesc.mItems.clear();
addSearchField(query.getText());
string path = StringTable.Localize("$OPTMNU_TITLE");
bool isAnyTermMatches = mIsAnyOfItem.mCVar.GetBool();
bool found = listOptions(mDesc, "MainMenu", query, path, isAnyTermMatches);
if (!found) { addNoResultsItem(mDesc); }
mDesc.CalcIndent();
}
private void addSearchField(string query = "")
{
string searchLabel = StringTable.Localize("$OS_LABEL");
let searchField = new("os_SearchField").Init(searchLabel, self, query);
mIsAnyOfItem = new("OptionMenuItemOption").Init("", "os_isanyof", "os_isanyof_values");
mDesc.mItems.push(searchField);
mDesc.mItems.push(mIsAnyOfItem);
addEmptyLine(mDesc);
}
private static bool listOptions(OptionMenuDescriptor targetDesc,
string menuName,
os_Query query,
string path,
bool isAnyTermMatches)
{
let desc = MenuDescriptor.GetDescriptor(menuName);
let listMenuDesc = ListMenuDescriptor(desc);
if (listMenuDesc)
{
return listOptionsListMenu(listMenuDesc, targetDesc, query, path, isAnyTermMatches);
}
let optionMenuDesc = OptionMenuDescriptor(desc);
if (optionMenuDesc)
{
return listOptionsOptionMenu(optionMenuDesc, targetDesc, query, path, isAnyTermMatches);
}
return false;
}
private static bool listOptionsListMenu(ListMenuDescriptor sourceDesc,
OptionMenuDescriptor targetDesc,
os_Query query,
string path,
bool isAnyTermMatches)
{
int nItems = sourceDesc.mItems.size();
bool found = false;
for (int i = 0; i < nItems; ++i)
{
let item = sourceDesc.mItems[i];
string actionN = item.GetAction();
string newPath = (actionN == "Optionsmenu")
? StringTable.Localize("$OPTMNU_TITLE")
: StringTable.Localize("$OS_MAIN");
found |= listOptions(targetDesc, actionN, query, newPath, isAnyTermMatches);
}
return found;
}
private static bool listOptionsOptionMenu(OptionMenuDescriptor sourceDesc,
OptionMenuDescriptor targetDesc,
os_Query query,
string path,
bool isAnyTermMatches)
{
if (sourceDesc == targetDesc) { return false; }
int nItems = sourceDesc.mItems.size();
bool first = true;
bool found = false;
for (int i = 0; i < nItems; ++i)
{
let item = sourceDesc.mItems[i];
if (item is "OptionMenuItemStaticText") { continue; }
string label = StringTable.Localize(item.mLabel);
if (!query.matches(label, isAnyTermMatches)) { continue; }
found = true;
if (first)
{
addEmptyLine(targetDesc);
addPathItem(targetDesc, path);
first = false;
}
let itemOptionBase = OptionMenuItemOptionBase(item);
if (itemOptionBase)
{
itemOptionBase.mCenter = false;
}
targetDesc.mItems.push(item);
}
for (int i = 0; i < nItems; ++i)
{
let item = sourceDesc.mItems[i];
string label = StringTable.Localize(item.mLabel);
string optionSearchTitle = StringTable.Localize("$OS_TITLE");
if (label == optionSearchTitle) { continue; }
if (item is "OptionMenuItemSubMenu")
{
string newPath = makePath(path, label);
found |= listOptions(targetDesc, item.GetAction(), query, newPath, isAnyTermMatches);
}
}
return found;
}
private static string makePath(string path, string label)
{
int pathWidth = SmallFont.StringWidth(path .. "/" .. label);
int screenWidth = Screen.GetWidth();
bool isTooWide = (pathWidth > screenWidth / 3);
string newPath = isTooWide
? path .. "/" .. "\n" .. label
: path .. "/" .. label;
return newPath;
}
private static void addPathItem(OptionMenuDescriptor desc, string path)
{
Array<String> lines;
path.split(lines, "\n");
int nLines = lines.size();
for (int i = 0; i < nLines; ++i)
{
OptionMenuItemStaticText text = new("OptionMenuItemStaticText").Init(lines[i], 1);
desc.mItems.push(text);
}
}
private static void addEmptyLine(OptionMenuDescriptor desc)
{
int nItems = desc.mItems.size();
if (nItems > 0)
{
let staticText = OptionMenuItemStaticText(desc.mItems[nItems - 1]);
if (staticText != null && staticText.mLabel == "") { return; }
}
let item = new("OptionMenuItemStaticText").Init("");
desc.mItems.push(item);
}
private static void addNoResultsItem(OptionMenuDescriptor desc)
{
string noResults = StringTable.Localize("$OS_NO_RESULTS");
let text = new("OptionMenuItemStaticText").Init(noResults, 0);
addEmptyLine(desc);
desc.mItems.push(text);
}
private OptionMenuItemOption mIsAnyOfItem;
}