Skip to content

Commit

Permalink
Add episode & episode unique search queries (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonko0493 authored Sep 17, 2023
1 parent fb0b7d1 commit a29612b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
62 changes: 62 additions & 0 deletions src/SerialLoops.Lib/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -946,11 +946,73 @@ private bool ItemMatches(ItemDescription item, string term, SearchQuery.DataHold
}
return false;

case SearchQuery.DataHolder.Episode_Number:
if (int.TryParse(term, out int episodeNum))
{
return ItemIsInEpisode(item, episodeNum, unique: false);
}
return false;

case SearchQuery.DataHolder.Episode_Unique:
if (int.TryParse(term, out int episodeNumUnique))
{
return ItemIsInEpisode(item, episodeNumUnique, unique: true);
}
return false;

default:
logger.LogError($"Unimplemented search scope: {scope}");
return false;
}
}

private bool ItemIsInEpisode(ItemDescription item, int episodeNum, bool unique)
{
int scenarioEpIndex = Scenario.Commands.FindIndex(c => c.Verb == ScenarioCommand.ScenarioVerb.NEW_GAME && c.Parameter == episodeNum);
if (scenarioEpIndex >= 0)
{
int scenarioNextEpIndex = Scenario.Commands.FindIndex(c => c.Verb == ScenarioCommand.ScenarioVerb.NEW_GAME && c.Parameter == episodeNum + 1);
if (item is ScriptItem script)
{
return ScriptIsInEpisode(script, scenarioEpIndex, scenarioNextEpIndex);
}
else
{
List<ItemDescription> references = item.GetReferencesTo(this);
if (unique)
{

return references.Where(r => r.Type == ItemDescription.ItemType.Script).Any() &&
references.Where(r => r.Type == ItemDescription.ItemType.Script)
.All(r => ScriptIsInEpisode((ScriptItem)r, scenarioEpIndex, scenarioNextEpIndex));
}
else
{
return references.Any(r => r.Type == ItemDescription.ItemType.Script && ScriptIsInEpisode((ScriptItem)r, scenarioEpIndex, scenarioNextEpIndex));
}
}
}
return false;
}

private bool ScriptIsInEpisode(ScriptItem script, int scenarioEpIndex, int scenarioNextEpIndex)
{
int scriptFileScenarioIndex = Scenario.Commands.FindIndex(c => c.Verb == ScenarioCommand.ScenarioVerb.LOAD_SCENE && c.Parameter == script.Event.Index);
if (scriptFileScenarioIndex < 0)
{
List<ItemDescription> references = script.GetReferencesTo(this);
ItemDescription groupSelection = references.Find(r => r.Type == ItemDescription.ItemType.Group_Selection);
if (groupSelection is not null)
{
scriptFileScenarioIndex = Scenario.Commands.FindIndex(c => c.Verb == ScenarioCommand.ScenarioVerb.ROUTE_SELECT && c.Parameter == ((GroupSelectionItem)groupSelection).Index);
}
}
if (scenarioNextEpIndex < 0)
{
scenarioNextEpIndex = int.MaxValue;
}

return scriptFileScenarioIndex > scenarioEpIndex && scriptFileScenarioIndex < scenarioNextEpIndex;
}
}
}
4 changes: 3 additions & 1 deletion src/SerialLoops.Lib/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum DataHolder
Script_Flag = 12,
Speaker_Name = 13,
Conditional = 14,
Background_Type = 15
Background_Type = 15,
Episode_Number = 16,
Episode_Unique = 17,
}

public static SearchQuery Create(string text)
Expand Down
14 changes: 7 additions & 7 deletions src/SerialLoops/Dialogs/SearchDialog.eto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ partial class SearchDialog : FindItemsWindow
private SearchBox _searchInput;

public string Text { get => _searchInput.Text; set => _searchInput.Text = value; }
private HashSet<SearchQuery.DataHolder> _scopes = new() { SearchQuery.DataHolder.Title };
private HashSet<ItemDescription.ItemType> _types = Enum.GetValues<ItemDescription.ItemType>().ToHashSet();
private Label _searchWarningLabel = new()
private readonly HashSet<SearchQuery.DataHolder> _scopes = new() { SearchQuery.DataHolder.Title };
private readonly HashSet<ItemDescription.ItemType> _types = Enum.GetValues<ItemDescription.ItemType>().ToHashSet();
private readonly Label _searchWarningLabel = new()
{
Text = "Press ENTER to execute search.",
TextAlignment = TextAlignment.Center,
Expand All @@ -33,10 +33,10 @@ partial class SearchDialog : FindItemsWindow
VerticalAlignment = VerticalAlignment.Center,
Visible = false,
};
private SearchQuery _query

private SearchQuery GetQuery()
{
get => new()
return new()
{
Term = Text,
Scopes = _scopes,
Expand Down Expand Up @@ -71,7 +71,7 @@ void InitializeComponent()

private void Search(bool force = false)
{
var query = _query;
SearchQuery query = GetQuery();
_searchWarningLabel.Visible = !query.QuickSearch;
_resultsLabel.Visible = false;
if (!query.QuickSearch && !force)
Expand Down

0 comments on commit a29612b

Please sign in to comment.