protected override void DidActivate(bool firstActivation, ActivationType activationType) { if (firstActivation) { this.name = "SearchCompactKeyboardViewController"; this.rectTransform.anchorMin = new Vector2(0.5f, 0.5f); this.rectTransform.anchorMax = this.rectTransform.anchorMin; this.rectTransform.pivot = this.rectTransform.anchorMin; this.rectTransform.anchoredPosition = Vector2.zero; this.rectTransform.sizeDelta = new Vector2(80f, 70f); _predictionBar = new GameObject("EnhancedSearchPredictionBar").AddComponent <PredictionBar>(); _predictionBar.Initialize(this.transform, 3.5f, 19f, -35f, 45f); _predictionBar.PredictionPressed += delegate(string query, SuggestionType type) { _searchText = query; _textDisplayComponent.text = _searchText.ToUpper() + CursorText; _predictionBar.ClearAndSetPredictionButtons(_searchText); PredictionPressed?.Invoke(query, type); }; var keyboardGO = Instantiate(Resources.FindObjectsOfTypeAll <UIKeyboard>().First(x => x.name != "CustomUIKeyboard" && x.name != "EnhancedSearchKeyboard"), this.transform, false).gameObject; Destroy(keyboardGO.GetComponent <UIKeyboard>()); _keyboard = keyboardGO.AddComponent <CompactSearchKeyboard>(); keyboardGO.name = "EnhancedSearchKeyboard"; var rt = _keyboard.transform as RectTransform; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.pivot = new Vector2(0.5f, 0.5f); rt.anchoredPosition = new Vector2(OffsetX, 5f); rt.sizeDelta = Vector2.zero; _keyboard.TextKeyPressed += delegate(char key) { _searchText += key.ToString(); _textDisplayComponent.text = _searchText.ToUpper() + CursorText; _predictionBar.ClearAndSetPredictionButtons(_searchText); TextKeyPressed?.Invoke(key); }; _keyboard.DeleteButtonPressed += delegate { if (_searchText.Length > 0) { _searchText = _searchText.Substring(0, _searchText.Length - 1); } if (_searchText.Length > 0) { _textDisplayComponent.text = _searchText.ToUpper() + CursorText; } else { _textDisplayComponent.text = PlaceholderText; } _predictionBar.ClearAndSetPredictionButtons(_searchText); DeleteButtonPressed?.Invoke(); }; _keyboard.ClearButtonPressed += delegate { _searchText = ""; _textDisplayComponent.text = PlaceholderText; _predictionBar.ClearAndSetPredictionButtons(_searchText); ClearButtonPressed?.Invoke(); }; _textDisplayComponent = BeatSaberUI.CreateText(this.rectTransform, "", new Vector2(OffsetX, 28f), new Vector2(4f, 4f)); _textDisplayComponent.fontSize = 6f; _textDisplayComponent.alignment = TextAlignmentOptions.Center; _textDisplayComponent.enableWordWrapping = false; } _searchText = ""; _textDisplayComponent.text = PlaceholderText; _keyboard.SymbolButtonInteractivity = !PluginConfig.StripSymbols; _keyboard.ResetSymbolMode(); _predictionBar.ClearPredictionButtons(); }
public void ClearAndSetPredictionButtons(string searchText) { // NOTE: searchText should be lower-cased (keyboard sends lowercase characters) if (!_initialized) { return; } ClearPredictionButtons(); if (string.IsNullOrEmpty(searchText)) { return; } // create new buttons Button btn = null; float currentX = 0f; var predictions = WordPredictionEngine.Instance.GetSuggestedWords(searchText); for (int i = 0; i < predictions.Count && currentX < _xEndPos - _xStartPos; ++i) { var word = predictions[i]; btn = Instantiate(_buttonPrefab, _parent, false); var rt = (btn.transform as RectTransform); rt.anchorMin = new Vector2(0.5f, 0.5f); rt.anchorMax = new Vector2(0.5f, 0.5f); rt.pivot = new Vector2(0f, 0.5f); btn.GetComponentsInChildren <HorizontalLayoutGroup>().First(x => x.name == "Content").padding = new RectOffset(0, 0, 0, 0); btn.GetComponentsInChildren <Image>().FirstOrDefault(x => x.name == "Stroke").color = new Color(0.6f, 0.6f, 0.8f); btn.onClick.RemoveAllListeners(); btn.onClick.AddListener(delegate() { if (!char.IsLetterOrDigit(searchText[searchText.Length - 1]) && !(searchText[searchText.Length - 1] == '\'')) { searchText += word; } else { var searchTextWords = WordPredictionEngine.RemoveSymbolsRegex.Replace(searchText, " ").Split(new char[] { ' ' }); if (searchTextWords.Length == 0) { // this should never be able to happen // implies we got a suggested word from empty search query searchText = word; } else { var lastSearchQueryWord = searchTextWords[searchTextWords.Length - 1]; if (word != lastSearchQueryWord && word.StartsWith(lastSearchQueryWord)) { var space = searchTextWords.Length == 1 ? "" : " "; searchText = searchText.Remove(searchText.Length - lastSearchQueryWord.Length) + space + word; } else { searchText += " " + word; } } } PredictionPressed?.Invoke(searchText); }); var text = btn.GetComponentInChildren <TextMeshProUGUI>(); text.fontSize = _fontSize; text.text = word.ToUpper(); text.enableWordWrapping = false; var width = text.preferredWidth + 8f; rt.sizeDelta = new Vector2(width, 7f); rt.anchoredPosition = new Vector2(_xStartPos + currentX, _yPos); currentX += width + 1.5f; _predictionButtons.Add(btn); } // remove the last button created, since it goes past the end of the screen // we have to do this here, since we don't know the width of the strings to be displayed before button creation if (btn != null && currentX >= _xEndPos - _xStartPos) { _predictionButtons.Remove(btn); Destroy(btn.gameObject); } }
public void ClearAndSetPredictionButtons(string searchText) { // NOTE: searchText should be lower-cased (keyboard sends lowercase characters) if (!_initialized) { return; } ClearPredictionButtons(); if (string.IsNullOrEmpty(searchText)) { return; } // create new or re-use old buttons PredictionButton btn = default; float currentX = 0f; var predictions = WordPredictionEngine.instance.GetSuggestedWords(searchText); for (int i = 0; i < predictions.Count && currentX < _xEndPos - _xStartPos; ++i) { var word = predictions[i].Word; var type = predictions[i].Type; if (_unusedButtons.Any()) { btn = _unusedButtons.Pop(); btn.SetActive(true); } else { btn = new PredictionButton(_parent); } btn.Button.onClick.RemoveAllListeners(); btn.Button.onClick.AddListener(delegate() { string[] searchTextWords = WordPredictionEngine.RemoveSymbolsRegex.Replace(searchText, " ").Split(WordPredictionEngine.SpaceCharArray); if (searchTextWords.Length == 0) { // this should never be able to happen // implies we got a suggested word from an empty search query searchText = word; } else { char lastChar = searchText[searchText.Length - 1]; string lastSearchWord = searchTextWords[searchTextWords.Length - 1]; if (type == SuggestionType.Prefixed || type == SuggestionType.FuzzyMatch) { int index = searchText.LastIndexOf(lastSearchWord); searchText = searchText.Remove(index) + word; } else if (type == SuggestionType.FollowUp) { string space = lastChar == ' ' ? "" : " "; searchText = searchText + space + word; } else { searchText = word; } } PredictionPressed?.Invoke(searchText, type); }); btn.SetText(word.ToUpper(), _fontSize); btn.Type = type; var width = btn.PreferredTextWidth + 8f; var rt = btn.Button.transform as RectTransform; rt.sizeDelta = new Vector2(width, 7f); rt.anchoredPosition = new Vector2(_xStartPos + currentX, _yPos); currentX += width + 1.5f; _predictionButtons.Add(btn); } // remove the last button created, since it goes past the end of the screen // we have to do this here, since we don't know the width of the strings to be displayed before button creation if (PredictionButton.IsValid(btn) && currentX >= _xEndPos - _xStartPos) { _predictionButtons.Remove(btn); btn.SetActive(false); _unusedButtons.Push(btn); } }
protected override void DidActivate(bool firstActivation, ActivationType activationType) { if (firstActivation) { _predictionBar = new GameObject("EnhancedSearchPredictionBar").AddComponent <PredictionBar>(); _predictionBar.Initialize(this.transform, 4f, 19f, -50f, 50f); _predictionBar.PredictionPressed += delegate(string query) { _searchText = query; _textDisplayComponent.SetText(_searchText.ToUpper()); _predictionBar.ClearAndSetPredictionButtons(_searchText); PredictionPressed?.Invoke(query); }; var keyboardGO = Instantiate(Resources.FindObjectsOfTypeAll <UIKeyboard>().First(x => x.name != "CustomUIKeyboard"), this.rectTransform, false).gameObject; Destroy(keyboardGO.GetComponent <UIKeyboard>()); _keyboard = keyboardGO.AddComponent <SearchKeyboard>(); keyboardGO.name = "EnhancedSearchKeyboard"; _keyboard.TextKeyPressed += delegate(char key) { _searchText += key.ToString(); _textDisplayComponent.text = _searchText.ToUpper(); _predictionBar.ClearAndSetPredictionButtons(_searchText); TextKeyPressed?.Invoke(key); }; _keyboard.DeleteButtonPressed += delegate { if (_searchText.Length > 0) { _searchText = _searchText.Substring(0, _searchText.Length - 1); } if (_searchText.Length > 0) { _textDisplayComponent.text = _searchText.ToUpper(); } else { _textDisplayComponent.text = PlaceholderText; } _predictionBar.ClearAndSetPredictionButtons(_searchText); DeleteButtonPressed?.Invoke(); }; _keyboard.ClearButtonPressed += delegate { _searchText = ""; _textDisplayComponent.text = PlaceholderText; _predictionBar.ClearAndSetPredictionButtons(_searchText); ClearButtonPressed?.Invoke(); }; _textDisplayComponent = BeatSaberUI.CreateText(this.rectTransform, "", new Vector2(0f, 28f), new Vector2(4f, 4f)); _textDisplayComponent.fontSize = 7.5f; _textDisplayComponent.alignment = TextAlignmentOptions.Center; _textDisplayComponent.enableWordWrapping = false; } _searchText = ""; _textDisplayComponent.text = PlaceholderText; _keyboard.SymbolButtonInteractivity = !PluginConfig.StripSymbols; _keyboard.ResetSymbolMode(); _predictionBar.ClearPredictionButtons(); }