package ui; import flixel.FlxG; import flixel.FlxSprite; import flixel.group.FlxGroup; import flixel.util.FlxColor; import ui.AtlasText.AtlasFont; import ui.TextMenuList.TextMenuItem; class PreferencesMenu extends ui.OptionsState.Page { public static var preferences:Map = new Map(); var items:TextMenuList; var checkboxes:Array = []; public function new() { super(); add(items = new TextMenuList()); createPrefItem('naughtyness', 'censor-naughty', false); createPrefItem('downscroll', 'downscroll', false); createPrefItem('flashing menu', 'flashing-menu', true); createPrefItem('Camera Zooming on Beat', 'camera-zoom', true); createPrefItem('FPS Counter', 'fps-counter', true); } public static function getPref(pref:String):Dynamic { return preferences.get(pref); } public static function initPrefs():Void { preferenceCheck('censor-naughty', false); preferenceCheck('downscroll', true); preferenceCheck('flashing-menu', true); preferenceCheck('camera-zoom', true); preferenceCheck('fps-counter', true); if (!getPref('fps-counter')) FlxG.stage.removeChild(Main.fpsCounter); } private function createPrefItem(prefName:String, prefString:String, prefValue:Dynamic):Void { items.createItem(120, (120 * items.length) + 30, prefName, AtlasFont.Bold, function() { preferenceCheck(prefString, prefValue); switch (Type.typeof(prefValue).getName()) { case 'TBool': prefToggle(prefString); default: trace('swag'); } }); switch (Type.typeof(prefValue).getName()) { case 'TBool': createCheckbox(prefString); default: trace('swag'); } trace(Type.typeof(prefValue).getName()); } function createCheckbox(prefString:String) { var checkbox:CheckboxThingie = new CheckboxThingie(0, 120 * (items.length - 1), preferences.get(prefString)); checkboxes.push(checkbox); add(checkbox); } /** * Assumes that the preference has already been checked/set? */ private function prefToggle(prefName:String) { var daSwap:Bool = preferences.get(prefName); daSwap = !daSwap; preferences.set(prefName, daSwap); checkboxes[items.selectedIndex].daValue = daSwap; trace('toggled? ' + preferences.get(prefName)); if (prefName == 'fps-counter') { if (getPref('fps-counter')) FlxG.stage.addChild(Main.fpsCounter); else FlxG.stage.removeChild(Main.fpsCounter); } } override function update(elapsed:Float) { super.update(elapsed); items.forEach(function(daItem:TextMenuItem) { if (items.selectedItem == daItem) daItem.x = 150; else daItem.x = 120; }); } private static function preferenceCheck(prefString:String, prefValue:Dynamic):Void { if (preferences.get(prefString) == null) { preferences.set(prefString, prefValue); trace('set preference!'); } else { trace('found preference: ' + preferences.get(prefString)); } } } class CheckboxThingie extends FlxSprite { public var daValue(default, set):Bool; public function new(x:Float, y:Float, daValue:Bool = false) { super(x, y); frames = Paths.getSparrowAtlas('checkboxThingie'); animation.addByPrefix('static', 'Check Box unselected', 24, false); animation.addByPrefix('checked', 'Check Box selecting animation', 24, false); antialiasing = true; setGraphicSize(Std.int(width * 0.7)); updateHitbox(); this.daValue = daValue; } override function update(elapsed:Float) { super.update(elapsed); switch (animation.curAnim.name) { case 'static': offset.set(); case 'checked': offset.set(17, 70); } } function set_daValue(value:Bool):Bool { if (value) animation.play('checked', true); else animation.play('static'); return value; } }