From 59be4ecd08f8012f743bd412dd7d10abac19b53e Mon Sep 17 00:00:00 2001 From: NeeEoo Date: Wed, 6 Sep 2023 16:46:52 +0200 Subject: [PATCH] Added autocomplete --- project.xml | 2 +- .../backend/system/macros/ScriptsMacro.hx | 1 + .../editors/charter/CharterStrumlineScreen.hx | 5 +- .../editors/ui/UIAutoCompleteTextBox.hx | 97 +++++++++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 source/funkin/editors/ui/UIAutoCompleteTextBox.hx diff --git a/project.xml b/project.xml index 1a008240..d8bc062f 100644 --- a/project.xml +++ b/project.xml @@ -113,7 +113,7 @@ - + diff --git a/source/funkin/backend/system/macros/ScriptsMacro.hx b/source/funkin/backend/system/macros/ScriptsMacro.hx index db0f9f55..c6509335 100644 --- a/source/funkin/backend/system/macros/ScriptsMacro.hx +++ b/source/funkin/backend/system/macros/ScriptsMacro.hx @@ -42,6 +42,7 @@ class ScriptsMacro { Compiler.addGlobalMetadata('haxe.xml', '@:build(hscript.UsingHandler.build())'); Compiler.addGlobalMetadata('haxe.CallStack', '@:build(hscript.UsingHandler.build())'); #if HL HashLinkFixer.init(); #end + // Todo rewrite this to use if(Context.defined("")) } } #end \ No newline at end of file diff --git a/source/funkin/editors/charter/CharterStrumlineScreen.hx b/source/funkin/editors/charter/CharterStrumlineScreen.hx index da7e369c..56b33e0a 100644 --- a/source/funkin/editors/charter/CharterStrumlineScreen.hx +++ b/source/funkin/editors/charter/CharterStrumlineScreen.hx @@ -158,7 +158,7 @@ class CharterStrumlineScreen extends UISubstateWindow { class CharacterButton extends UIButton { public var charIcon:HealthIcon; - public var textBox:UITextBox; + public var textBox:UIAutoCompleteTextBox; public var deleteButton:UIButton; public var deleteIcon:FlxSprite; @@ -174,7 +174,8 @@ class CharacterButton extends UIButton { members.push(charIcon); - members.push(textBox = new UITextBox(charIcon.x + charIcon.width + 16, bHeight/2 - (32/2), char, 115)); + members.push(textBox = new UIAutoCompleteTextBox(charIcon.x + charIcon.width + 16, bHeight/2 - (32/2), char, 115)); + textBox.suggestItems = Character.getList(false); textBox.antialiasing = true; textBox.onChange = function(char:String) { char = Character.getIconFromCharName(char); diff --git a/source/funkin/editors/ui/UIAutoCompleteTextBox.hx b/source/funkin/editors/ui/UIAutoCompleteTextBox.hx new file mode 100644 index 00000000..c2cb4f73 --- /dev/null +++ b/source/funkin/editors/ui/UIAutoCompleteTextBox.hx @@ -0,0 +1,97 @@ +package funkin.editors.ui; + +import flixel.math.FlxPoint; +import lime.ui.KeyModifier; +import lime.ui.KeyCode; +import openfl.desktop.Clipboard; +import openfl.geom.Rectangle; + +class UIAutoCompleteTextBox extends UITextBox { + public var suggestionText:UIText; + + public var suggestItems(default, set):Array = []; + var suggestIndex = 0; + + public function new(x:Float, y:Float, text:String = "", width:Int = 320, height:Int = 32, multiline:Bool = false) { + super(x, y, text, width, height, multiline); + + suggestionText = new UIText(0, 0, width, ""); + suggestionText.color = 0xFF888888; + suggestionText.visible = false; + members.insert(members.indexOf(label), suggestionText); + } + + public override function update(elapsed:Float) { + super.update(elapsed); + + var selected = selectable && focused; + + suggestionText.exists = selected; + + suggestionText.follow(label, 0, 0); + } + + public override function onKeyDown(e:KeyCode, modifier:KeyModifier) { + super.onKeyDown(e, modifier); + switch(e) { + //case RETURN: + // focused = false; + case TAB: + if(_suggestions.length > 0) { + label.text = suggestionText.text; + position = label.text.length; + updateSuggestion(true); + } + case BACKSPACE: + updateSuggestion(true); + case UP: + suggestIndex--; + updateSuggestion(false); + case DOWN: + suggestIndex++; + updateSuggestion(false); + default: + // nothing + } + } + + var _suggestions:Array = []; + + public function updateSuggestion(refreshSuggestions:Bool = true) { + if(refreshSuggestions) { + var text = label.text; + _suggestions = []; + if(text.length > 0) { + for(i in suggestItems) if(!_suggestions.contains(i) && i.startsWith(text)) _suggestions.push(i); + //for(i in suggestItems) if(!_suggestions.contains(i) && i.contains(text)) _suggestions.push(i); + _suggestions.remove(text); + } + } + + if(suggestionText.visible = (_suggestions.length != 0)) { + if(suggestIndex >= _suggestions.length) + suggestIndex = 0; + suggestIndex = FlxMath.wrap(suggestIndex, 0, _suggestions.length - 1); + + suggestionText.text = _suggestions[suggestIndex]; + } + } + + public override function onTextInput(text:String):Void { + super.onTextInput(text); + + updateSuggestion(); + } + + public override function onTextEdit(text:String, start:Int, end:Int):Void { + super.onTextEdit(text, start, end); + + updateSuggestion(); + } + + function set_suggestItems(v:Array) { + suggestItems = v; + updateSuggestion(); + return v; + } +} \ No newline at end of file