added config manager (closes #18) and fixed crash at win
	
		
	
				
					
				
			
							parent
							
								
									9fb228ca3d
								
							
						
					
					
						commit
						04032044dc
					
				| @ -1,20 +1,20 @@ | ||||
| { | ||||
| 	"touchscreen": { | ||||
| 		"enabled": true, | ||||
| 		"mode": "crosspad", | ||||
| 		"deadzone": 50, | ||||
| 		"buffer": false | ||||
| 	}, | ||||
| 	"keyboard": { | ||||
| 		"enabled": true, | ||||
| 		"buffer": false | ||||
| 	}, | ||||
| 	"gamepad": { | ||||
| 		"enabled": true, | ||||
| 		"deadzone": 0.5, | ||||
| 		"buffer": true | ||||
| 	}, | ||||
| 	"appearance": { | ||||
| 		"grid": "none" | ||||
| 	} | ||||
| 	"input.touchscreen.crosspad.enabled": true, | ||||
| 	"input.touchscreen.crosspad.overlay": true, | ||||
| 
 | ||||
| 	"input.touchscreen.joystick.enabled": false, | ||||
| 	"input.touchscreen.joystick.overlay": true, | ||||
| 	"input.touchscreen.joystick.deadzone": 10, | ||||
| 
 | ||||
| 	"input.touchscreen.swipe.enabled": false, | ||||
| 	"input.touchscreen.swipe.deadzone": 50, | ||||
| 
 | ||||
| 	"input.gamepad.enabled": true, | ||||
| 	"input.gamepad.deadzone": 0.5, | ||||
| 
 | ||||
| 	"input.keyboard.enabled": true, | ||||
| 
 | ||||
| 	"input.buffer": false, | ||||
| 
 | ||||
| 	"appearance.grid": "none" | ||||
| } | ||||
|  | ||||
| @ -1,26 +1,121 @@ | ||||
| { | ||||
| 	"touchscreen": { | ||||
| 		"mode": [ | ||||
| 			"crosspad", | ||||
| 			"joystick", | ||||
| 			"swipe" | ||||
| 		], | ||||
| 		"deadzone": { | ||||
| 	"input": { | ||||
| 		"name": "Input settings" | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.touchscreen": { | ||||
| 		"name": "Touchscreen settings" | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.touchscreen.crosspad": { | ||||
| 		"name": "Crosspad mode" | ||||
| 	}, | ||||
| 	"input.touchscreen.crosspad.enabled": { | ||||
| 		"name": "Enable crosspad", | ||||
| 		"type": "boolean", | ||||
| 		"excludes": [ | ||||
| 			"input.touchscreen.joystick.enabled", | ||||
| 			"input.touchscreen.swipe.enabled" | ||||
| 		] | ||||
| 	}, | ||||
| 	"input.touchscreen.crosspad.overlay": { | ||||
| 		"name": "Show overlay", | ||||
| 		"type": "boolean", | ||||
| 		"parent": "input.touchscreen.crosspad.enabled" | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.touchscreen.joystick": { | ||||
| 		"name": "Joystick mode" | ||||
| 	}, | ||||
| 	"input.touchscreen.joystick.enabled": { | ||||
| 		"name": "Enable joystick", | ||||
| 		"type": "boolean", | ||||
| 		"excludes": [ | ||||
| 			"input.touchscreen.crosspad.enabled", | ||||
| 			"input.touchscreen.swipe.enabled" | ||||
| 		] | ||||
| 	}, | ||||
| 	"input.touchscreen.joystick.overlay": { | ||||
| 		"name": "Show overlay", | ||||
| 		"type": "boolean", | ||||
| 		"parent": "input.touchscreen.joystick.enabled" | ||||
| 	}, | ||||
| 	"input.touchscreen.joystick.deadzone": { | ||||
| 		"name": "Deadzone", | ||||
| 		"type": "number", | ||||
| 		"parent": "input.touchscreen.joystick.enabled", | ||||
| 		"bounds": { | ||||
| 			"min": 1, | ||||
| 			"max": 100, | ||||
| 			"inc": 1 | ||||
| 		} | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.touchscreen.swipe": { | ||||
| 		"name": "Swipe mode" | ||||
| 	}, | ||||
| 	"input.touchscreen.swipe.enabled": { | ||||
| 		"name": "Enable swipe", | ||||
| 		"type": "boolean", | ||||
| 		"excludes": [ | ||||
| 			"input.touchscreen.crosspad.enabled", | ||||
| 			"input.touchscreen.joystick.enabled" | ||||
| 		] | ||||
| 	}, | ||||
| 	"input.touchscreen.swipe.deadzone": { | ||||
| 		"name": "Deadzone", | ||||
| 		"type": "number", | ||||
| 		"parent": "input.touchscreen.swipe.enabled", | ||||
| 		"bounds": { | ||||
| 			"min": 1, | ||||
| 			"max": 100 | ||||
| 			"max": 100, | ||||
| 			"inc": 1 | ||||
| 		} | ||||
| 	}, | ||||
| 	"gamepad": { | ||||
| 		"deadzone": { | ||||
| 
 | ||||
| 	"input.gamepad": { | ||||
| 		"name": "Gamepad settings" | ||||
| 	}, | ||||
| 	"input.gamepad.enabled": { | ||||
| 		"name": "Enable gamepad", | ||||
| 		"type": "boolean" | ||||
| 	}, | ||||
| 	"input.gamepad.deadzone": { | ||||
| 		"name": "Deadzone", | ||||
| 		"type": "number", | ||||
| 		"parent": "input.gamepad.enabled", | ||||
| 		"bounds": { | ||||
| 			"min": 0, | ||||
| 			"max": 1 | ||||
| 			"max": 1, | ||||
| 			"inc": 0.1 | ||||
| 		} | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.keyboard": { | ||||
| 		"name": "Keyboard settings" | ||||
| 	}, | ||||
| 	"input.keyboard.enabled": { | ||||
| 		"name": "Enable keyboard", | ||||
| 		"type": "boolean" | ||||
| 	}, | ||||
| 
 | ||||
| 	"input.buffer": { | ||||
| 		"name": "Enable input buffering", | ||||
| 		"type": "boolean" | ||||
| 	}, | ||||
| 
 | ||||
| 	"appearance": { | ||||
| 		"grid": [ | ||||
| 			"grid", | ||||
| 			"checkerboard", | ||||
| 			"none" | ||||
| 		] | ||||
| 		"name": "Appearance" | ||||
| 	}, | ||||
| 	"appearance.grid": { | ||||
| 		"name": "Grid type", | ||||
| 		"type": "choice", | ||||
| 		"bounds": { | ||||
| 			"choices": [ | ||||
| 				"none", | ||||
| 				"grid", | ||||
| 				"checkerboard" | ||||
| 			] | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,76 @@ | ||||
| const assets=require('assets'); | ||||
| 
 | ||||
| let watchers=Object.create(null); | ||||
| let lastWatchCode=1; | ||||
| 
 | ||||
| const toBoolean=v => { | ||||
| 	if(v=='false' || v==false) return false; | ||||
| 	return true; | ||||
| }; | ||||
| 
 | ||||
| const get=key => { | ||||
| 	let confVal=localStorage.getItem('config.'+key); | ||||
| 	if(confVal===null) return assets.get('config')[key]; | ||||
| 	return confVal; | ||||
| }; | ||||
| const getB=key => toBoolean(get(key)); | ||||
| const getN=key => +get(key); | ||||
| const getS=key => ''+get(key); | ||||
| 
 | ||||
| const set=(key, value) => { | ||||
| 	localStorage.setItem('config.'+key, value); | ||||
| 	let interested=watchers[key]; | ||||
| 	if(interested) interested.forEach(watcher => watcher(key, value)); | ||||
| }; | ||||
| 
 | ||||
| const remove=key => { | ||||
| 	localStorage.removeItem('config.'+key, value); | ||||
| 	let interested=watchers[key]; | ||||
| 	if(interested) interested.forEach(watcher => watcher(key, assets.get('config')[key])); | ||||
| }; | ||||
| const clear=() => | ||||
| 	Object | ||||
| 		.keys(assets.get('config')) | ||||
| 		.forEach(remove); | ||||
| 
 | ||||
| const watch=(key, fn) => { | ||||
| 	if(!watchers[key]) watchers[key]=[]; | ||||
| 	const code='w'+lastWatchCode++; | ||||
| 	watchers[key][code]=fn; | ||||
| 	return code; | ||||
| }; | ||||
| const watchB=(key, fn) => watch(key, (k, v) => fn(k, toBoolean(v))); | ||||
| const watchN=(key, fn) => watch(key, (k, v) => fn(k, +v)); | ||||
| const watchS=(key, fn) => watch(key, (k, v) => fn(k, ''+v)); | ||||
| 
 | ||||
| const unwatch=(key, code) => { | ||||
| 	if(!watchers[key]) return; | ||||
| 	delete watchers[key][code]; | ||||
| }; | ||||
| 
 | ||||
| const list=() => | ||||
| 	Object | ||||
| 		.keys(assets.get('config')); | ||||
| const dict=() => { | ||||
| 	let dict=Object.create(null); | ||||
| 	Object | ||||
| 		.keys(assets.get('config')) | ||||
| 		.forEach( | ||||
| 			key => dict[key]={ | ||||
| 				raw: get(key), | ||||
| 				b: getB(key), | ||||
| 				n: getN(key), | ||||
| 				s: getS(key) | ||||
| 			} | ||||
| 		); | ||||
| 	return dict; | ||||
| }; | ||||
| 
 | ||||
| return module.exports={ | ||||
| 	get, getB, getN, getS, | ||||
| 	set, | ||||
| 	remove, clear, | ||||
| 	watch, watchB, watchN, watchS, | ||||
| 	unwatch, | ||||
| 	list, dict | ||||
| }; | ||||
					Loading…
					
					
				
		Reference in new issue