diff options
| author | RblSb <msrblsb@gmail.com> | 2021-09-27 20:48:29 +0300 |
|---|---|---|
| committer | RblSb <msrblsb@gmail.com> | 2021-09-27 20:49:05 +0300 |
| commit | 98c1f6fa4ec7537ec979a1ae8d68cb6ec11c3fbf (patch) | |
| tree | cf16a26097b00226c8f9fedbb9c1a5ecfb2211a3 /src/VideoList.hx | |
| parent | 1dc2722d509e578b243bfda1a675dcfdb6003941 (diff) | |
Rework videolist structure
Diffstat (limited to 'src/VideoList.hx')
| -rw-r--r-- | src/VideoList.hx | 119 |
1 files changed, 81 insertions, 38 deletions
diff --git a/src/VideoList.hx b/src/VideoList.hx index ef1d113..2ed1d8f 100644 --- a/src/VideoList.hx +++ b/src/VideoList.hx @@ -2,73 +2,116 @@ package; import Types.VideoItem; -// TODO move itemPos to abstract -// typedef VideoListData = { -// items:Array<VideoItem>, -// itemPos:Int -// } -@:forward -abstract VideoList(Array<VideoItem>) from Array<VideoItem> to Array<VideoItem> { - public function new() { - this = []; +using Lambda; + +class VideoList { + public var length(get, never):Int; + public var pos(default, null) = 0; + public var isOpen = true; + + final items:Array<VideoItem> = []; + + public function new() {} + + inline function get_length():Int { + return items.length; + } + + public inline function getCurrentItem():VideoItem { + return items[pos]; } - @:arrayAccess - public inline function get(i:Int):VideoItem { - return this[i]; + public inline function getItem(i:Int):VideoItem { + return items[i]; } - @:arrayAccess - public inline function set(k:Int, v:VideoItem):VideoItem { - return this[k] = v; + public inline function setItem(i:Int, item:VideoItem):Void { + items[i] = item; + } + + public inline function getItems():Array<VideoItem> { + return items; + } + + public function setItems(items:Array<VideoItem>):Void { + clear(); + for (item in items) + this.items.push(item); + } + + public function setPos(i:Int):Void { + if (i < 0 || i > length - 1) i = 0; + pos = i; + } + + public function exists(f:(item:VideoItem) -> Bool):Bool { + return items.exists(f); } public function findIndex(f:(item:VideoItem) -> Bool):Int { var i = 0; - for (v in this) { + for (v in items) { if (f(v)) return i; i++; } return -1; } - public function addItem(item:VideoItem, atEnd:Bool, itemPos:Int):Void { - if (atEnd) this.push(item); - else this.insert(itemPos + 1, item); + public function addItem(item:VideoItem, atEnd:Bool):Void { + if (atEnd) items.push(item); + else items.insert(pos + 1, item); } - public function setNextItem(pos:Int, itemPos:Int):Int { - final next = this[pos]; - this.remove(next); - if (pos < itemPos) itemPos--; - this.insert(itemPos + 1, next); - return itemPos; + public function setNextItem(nextPos:Int):Void { + final next = items[nextPos]; + items.remove(next); + if (nextPos < pos) pos--; + items.insert(pos + 1, next); } public function toggleItemType(pos:Int):Void { - this[pos].isTemp = !this[pos].isTemp; + items[pos].isTemp = !items[pos].isTemp; } - public function removeItem(index:Int, itemPos:Int):Int { - if (index < itemPos) itemPos--; - this.remove(this[index]); - if (itemPos >= this.length) itemPos = 0; - return itemPos; + public function removeItem(index:Int):Void { + if (index < pos) pos--; + items.remove(items[index]); + if (pos >= items.length) pos = 0; } - public function skipItem(itemPos:Int):Int { - final item = this[itemPos]; - if (!item.isTemp) itemPos++; - else this.remove(item); - if (itemPos >= this.length) itemPos = 0; - return itemPos; + public function skipItem():Void { + final item = items[pos]; + if (!item.isTemp) pos++; + else items.remove(item); + if (pos >= items.length) pos = 0; } public function itemsByUser(client:Client):Int { var i = 0; - for (item in this) { + for (item in items) { if (item.author == client.name) i++; } return i; } + + public inline function clear():Void { + items.resize(0); + pos = 0; + } + + public function shuffle() { + final current = items[pos]; + items.remove(current); + shuffleArray(items); + items.insert(pos, current); + } + + function shuffleArray<T>(arr:Array<T>):Void { + for (i => a in arr) { + final n = Std.random(arr.length); + final b = arr[n]; + arr[i] = b; + arr[n] = a; + } + } } |
