gzdoom/wadsrc/static/zscript/ui/menu/search/menu.zs

219 lines
5.5 KiB
Text
Raw Normal View History

2019-03-16 07:46:48 +00:00
//=============================================================================
//
// 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();
}
2019-03-16 07:46:48 +00:00
void search()
{
string text = mSearchField.GetText();
let query = os_Query.fromString(text);
bool isAnyTermMatches = mIsAnyOfItem.mCVar.GetBool();
2019-03-16 07:46:48 +00:00
mDesc.mItems.clear();
2019-03-16 07:46:48 +00:00
addSearchField(text);
2019-03-16 07:46:48 +00:00
bool found = listOptions(mDesc, "MainMenu", query, "", isAnyTermMatches);
2019-03-16 07:46:48 +00:00
if (!found) { addNoResultsItem(mDesc); }
2019-03-16 07:46:48 +00:00
mDesc.CalcIndent();
}
2019-03-16 07:46:48 +00:00
private void addSearchField(string query = "")
{
string searchLabel = StringTable.Localize("$OS_LABEL");
2019-03-16 07:46:48 +00:00
mSearchField = new("os_SearchField").Init(searchLabel, self, query);
mIsAnyOfItem = new("os_AnyOrAllOption").Init(self);
2019-03-16 07:46:48 +00:00
mDesc.mItems.push(mSearchField);
mDesc.mItems.push(mIsAnyOfItem);
addEmptyLine(mDesc);
}
2019-03-16 07:46:48 +00:00
private static bool listOptions(OptionMenuDescriptor targetDesc,
string menuName,
os_Query query,
string path,
bool isAnyTermMatches)
{
let desc = MenuDescriptor.GetDescriptor(menuName);
let listMenuDesc = ListMenuDescriptor(desc);
2019-03-16 07:46:48 +00:00
if (listMenuDesc)
{
return listOptionsListMenu(listMenuDesc, targetDesc, query, path, isAnyTermMatches);
}
2019-03-16 07:46:48 +00:00
let optionMenuDesc = OptionMenuDescriptor(desc);
2019-03-16 07:46:48 +00:00
if (optionMenuDesc)
{
return listOptionsOptionMenu(optionMenuDesc, targetDesc, query, path, isAnyTermMatches);
}
2019-03-16 07:46:48 +00:00
return false;
}
2019-03-16 07:46:48 +00:00
private static bool listOptionsListMenu(ListMenuDescriptor sourceDesc,
OptionMenuDescriptor targetDesc,
os_Query query,
string path,
bool isAnyTermMatches)
{
int nItems = sourceDesc.mItems.size();
bool found = false;
2019-03-16 07:46:48 +00:00
for (int i = 0; i < nItems; ++i)
{
let item = sourceDesc.mItems[i];
string actionN = item.GetAction();
let textItem = ListMenuItemTextItem(item);
string newPath = textItem
? makePath(path, StringTable.Localize(textItem.mText))
: path;
2019-03-16 07:46:48 +00:00
found |= listOptions(targetDesc, actionN, query, newPath, isAnyTermMatches);
}
2019-03-16 07:46:48 +00:00
return found;
}
2019-03-16 07:46:48 +00:00
private static bool listOptionsOptionMenu(OptionMenuDescriptor sourceDesc,
OptionMenuDescriptor targetDesc,
os_Query query,
string path,
bool isAnyTermMatches)
{
if (sourceDesc == targetDesc) { return false; }
2019-03-16 07:46:48 +00:00
int nItems = sourceDesc.mItems.size();
bool first = true;
bool found = false;
2019-03-16 07:46:48 +00:00
for (int i = 0; i < nItems; ++i)
{
let item = sourceDesc.mItems[i];
2019-03-16 07:46:48 +00:00
if (item is "OptionMenuItemStaticText") { continue; }
2019-03-16 07:46:48 +00:00
string label = StringTable.Localize(item.mLabel);
2019-03-16 07:46:48 +00:00
if (!query.matches(label, isAnyTermMatches)) { continue; }
2019-03-16 07:46:48 +00:00
found = true;
2019-03-16 07:46:48 +00:00
if (first)
{
addEmptyLine(targetDesc);
addPathItem(targetDesc, path);
2019-03-16 07:46:48 +00:00
first = false;
}
2019-03-16 07:46:48 +00:00
let itemOptionBase = OptionMenuItemOptionBase(item);
2019-03-16 07:46:48 +00:00
if (itemOptionBase)
{
itemOptionBase.mCenter = false;
}
2019-03-16 07:46:48 +00:00
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);
}
}
2019-03-16 07:46:48 +00:00
return found;
}
2019-03-16 07:46:48 +00:00
private static string makePath(string path, string label)
{
if (path.length() == 0) { return label; }
2019-03-16 07:46:48 +00:00
int pathWidth = SmallFont.StringWidth(path .. "/" .. label);
int screenWidth = Screen.GetWidth();
bool isTooWide = (pathWidth > screenWidth / 3);
string newPath = isTooWide
? path .. "/" .. "\n" .. label
: path .. "/" .. label;
2019-03-16 07:46:48 +00:00
return newPath;
}
2019-03-16 07:46:48 +00:00
private static void addPathItem(OptionMenuDescriptor desc, string path)
{
Array<String> lines;
path.split(lines, "\n");
2019-03-16 07:46:48 +00:00
int nLines = lines.size();
2019-03-16 07:46:48 +00:00
for (int i = 0; i < nLines; ++i)
{
OptionMenuItemStaticText text = new("OptionMenuItemStaticText").Init(lines[i], 1);
2019-03-16 07:46:48 +00:00
desc.mItems.push(text);
}
}
2019-03-16 07:46:48 +00:00
private static void addEmptyLine(OptionMenuDescriptor desc)
{
int nItems = desc.mItems.size();
2019-03-16 07:46:48 +00:00
if (nItems > 0)
{
let staticText = OptionMenuItemStaticText(desc.mItems[nItems - 1]);
2019-03-16 07:46:48 +00:00
if (staticText != null && staticText.mLabel == "") { return; }
}
2019-03-16 07:46:48 +00:00
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 os_AnyOrAllOption mIsAnyOfItem;
private os_SearchField mSearchField;
2019-03-16 07:46:48 +00:00
}