Skip to content

Commit

Permalink
revert formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjamuffin99 committed Aug 14, 2024
1 parent 5770baf commit 307b431
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 101 deletions.
81 changes: 41 additions & 40 deletions flixel/system/debug/completion/CompletionList.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,65 @@ import flixel.math.FlxMath;
import openfl.display.Sprite;
import openfl.events.KeyboardEvent;
import openfl.ui.Keyboard;
using StringTools;

using flixel.util.FlxStringUtil;
using StringTools;

class CompletionList extends Sprite
{
public var completed:String->Void;
public var selectionChanged:String->Void;
public var closed:Void->Void;

public var filter(default, set):String;

public var items(default, null):Array<String>;

var entries:Array<CompletionListEntry> = [];
var originalItems:Array<String>;
var selectedIndex:Int = 0;
var lowerVisibleIndex:Int = 0;
var upperVisibleIndex:Int = 0;
var scrollBar:CompletionListScrollBar;
var actualHeight:Int;

public function new(capacity:Int)
{
super();

visible = false;
upperVisibleIndex = capacity - 1;
actualHeight = capacity * CompletionListEntry.HEIGHT;

createPopupEntries(capacity);
createScrollBar();
updateSelectedItem();
}

public function show(x:Float, items:Array<String>)
{
visible = true;
this.x = x;
this.originalItems = items;
filter = "";

updateEntries();
}

public function setY(y:Float)
{
this.y = y - actualHeight;
}

public function close()
{
visible = false;
filter = null;

if (closed != null)
closed();
}

function createPopupEntries(amount:Int)
{
for (i in 0...amount)
Expand All @@ -72,73 +73,73 @@ class CompletionList extends Sprite
entry.y = CompletionListEntry.HEIGHT * i;
}
}

function createScrollBar()
{
scrollBar = new CompletionListScrollBar(CompletionListEntry.WIDTH, 0, 5, actualHeight);
addChild(scrollBar);
}

public function onKeyDown(e:KeyboardEvent)
{
if (!visible)
return;

switch (e.keyCode)
{
case Keyboard.DOWN:
updateIndices(1);

case Keyboard.UP:
updateIndices(-1);

case Keyboard.ENTER | Keyboard.TAB:
if (completed != null)
completed(items[selectedIndex]);
close();
return;

case Keyboard.ESCAPE:
close();
return;
}

updateEntries();
}

function updateIndices(modifier:Int)
{
selectedIndex = bound(selectedIndex + modifier);
if (FlxMath.inBounds(selectedIndex, lowerVisibleIndex, upperVisibleIndex))
return;

// scroll visible area
lowerVisibleIndex = bound(lowerVisibleIndex + modifier);
upperVisibleIndex = bound(upperVisibleIndex + modifier);
var range = upperVisibleIndex - lowerVisibleIndex;

if (range == items.length)
return;

// hit lower or upper end
if (lowerVisibleIndex == 0)
upperVisibleIndex = entries.length - 1;
else if (upperVisibleIndex == items.length - 1)
lowerVisibleIndex = items.length - entries.length;
}

function bound(index:Int)
{
return Std.int(FlxMath.bound(index, 0, items.length - 1));
}

function updateEntries()
{
updateLabels();
updateSelectedItem();
scrollBar.updateHandle(lowerVisibleIndex, items.length, entries.length);
}

function updateLabels()
{
for (i in 0...entries.length)
Expand All @@ -149,43 +150,43 @@ class CompletionList extends Sprite
entries[i].setItem(selectedItem);
}
}

function updateSelectedItem()
{
for (entry in entries)
entry.selected = false;
entries[selectedIndex - lowerVisibleIndex].selected = true;

if (selectionChanged != null)
selectionChanged(items[selectedIndex]);
}

function setItems(items:Array<String>)
{
if (items == null)
return;
if (items.length == 0)
close();

this.items = items;

selectedIndex = 0;
lowerVisibleIndex = 0;
upperVisibleIndex = entries.length - 1;
updateEntries();
}

function filterItems(filter:String):Array<String>
{
if (filter == null)
filter = "";

return sortItems(filter, originalItems.filter(function(item)
{
return item.toLowerCase().contains(filter.toLowerCase());
}));
}

/**
* sort items so that:
* - strings starting with the filter have a higher priority than those only containing it
Expand All @@ -195,7 +196,7 @@ class CompletionList extends Sprite
{
if (filter == "")
return items;

items.sort(function(a, b)
{
var valueA = startsWithExt(a, filter);
Expand All @@ -204,14 +205,14 @@ class CompletionList extends Sprite
return -valueA;
if (valueB > valueA)
return valueB;

if (valueA == valueB)
return Std.int(a.length - b.length);
return 0;
});
return items;
}

/**
* Custom startsWith() that ignores leading underscores.
*/
Expand All @@ -223,12 +224,12 @@ class CompletionList extends Sprite
return 1;
return 0;
}

function set_filter(filter:String):String
{
if (filter == this.filter)
return filter;

setItems(filterItems(filter));
return this.filter = filter;
}
Expand Down
Loading

0 comments on commit 307b431

Please sign in to comment.