|
1 | | -import { useState } from 'react' |
| 1 | +import { useEffect, useState } from 'react' |
2 | 2 | import { contro as controEx } from '../controls' |
| 3 | +import { customCommandsConfig } from '../customCommands' |
3 | 4 | import { AwaitingInputOverlay, ButtonWithMatchesAlert } from './KeybindingsScreenApp' |
4 | 5 | import Button from './Button' |
5 | 6 | import PixelartIcon from './PixelartIcon' |
6 | 7 | import styles from './KeybindingsScreen.module.css' |
7 | | - |
| 8 | +import Input from './Input' |
8 | 9 |
|
9 | 10 | export default ( |
10 | 11 | { |
@@ -35,47 +36,88 @@ export default ( |
35 | 36 | isPS: boolean | undefined |
36 | 37 | } |
37 | 38 | ) => { |
38 | | - const [, forceUpdate] = useState(false) |
| 39 | + type CustomCommand = { |
| 40 | + keys: string[], |
| 41 | + gamepad: string[] |
| 42 | + type: string |
| 43 | + inputs: any[] |
| 44 | + } |
| 45 | + // todo need to save custom actions to localstorage in the upper component and pass it down parsed to keybindings with config inputs |
| 46 | + const [customConfig, setCustomConfig] = useState([] as CustomCommand[]/* userConfig.custom */) |
| 47 | + useEffect(() => { |
| 48 | + localStorage.setItem('customConfig', JSON.stringify(customConfig)) |
| 49 | + // userConfig.custom = customConfig |
| 50 | + }, [customConfig]) |
39 | 51 |
|
40 | | - const addNewChatCommand = (e) => { |
41 | | - let chatCommands |
42 | | - if (userConfig.custom) { |
43 | | - chatCommands = Object.keys(userConfig.custom) |
44 | | - } else { |
45 | | - chatCommands = [] as string[] |
46 | | - } |
47 | | - let commandName = 'chat_command_' + generateCode() |
48 | | - while (chatCommands.includes(commandName)) { |
49 | | - commandName = 'chat_command_' + generateCode() |
50 | | - } |
51 | | - setBinding({}, 'custom', commandName, 0) |
52 | | - forceUpdate(prev => !prev) |
| 52 | + const addNewCommand = (type) => { |
| 53 | + setCustomConfig(prev => [...prev, { |
| 54 | + keys: [], |
| 55 | + gamepad: [], |
| 56 | + type, |
| 57 | + inputs: [] |
| 58 | + }]) |
| 59 | + |
| 60 | + // setBinding({}, 'custom', commandName, 0) |
| 61 | + } |
| 62 | + |
| 63 | + const setInputValue = (indexOption, indexInput, value) => { |
| 64 | + setCustomConfig(prev => { |
| 65 | + const newConfig = [...prev] |
| 66 | + newConfig[indexOption].inputs[indexInput] = value |
| 67 | + return newConfig |
| 68 | + }) |
53 | 69 | } |
54 | 70 |
|
55 | 71 | return <> |
56 | 72 | <div className={styles.group}> |
| 73 | + {Object.entries(customCommandsConfig).map(([group, { input }]) => ( |
| 74 | + <><div key={group} className={styles['group-category']}>{group}</div> |
| 75 | + {customConfig.filter(x => x.type === group).map(({ keys, gamepad, inputs }, indexOption) => { |
| 76 | + return <div key={indexOption}> |
| 77 | + {input.map((obj, indexInput) => { |
| 78 | + const config = typeof obj === 'function' ? obj(inputs) : obj |
| 79 | + if (!config) return null |
| 80 | + |
| 81 | + return config.type === 'select' |
| 82 | + ? <select key={indexInput} onChange={(e) => { |
| 83 | + setInputValue(indexOption, indexInput, e.target.value) |
| 84 | + }}>{config.options.map((option) => <option key={option} value={option}>{option}</option>)}</select> |
| 85 | + : <Input key={indexInput} placeholder={config.placeholder} value={inputs[indexInput]} onChange={(e) => setInputValue(indexOption, indexInput, e.target.value)} /> |
| 86 | + })} |
| 87 | + </div> |
| 88 | + })} |
| 89 | + <Button |
| 90 | + onClick={() => addNewCommand(group)} |
| 91 | + icon={'pixelarticons:add-box'} |
| 92 | + style={{ |
| 93 | + alignSelf: 'center' |
| 94 | + }} |
| 95 | + /> |
| 96 | + </> |
| 97 | + ))} |
57 | 98 | <div className={styles['group-category']}>Chat commands</div> |
58 | 99 | {userConfig.custom && |
59 | 100 | Object.entries(userConfig.custom) |
60 | | - .map(([action, { keys, gamepadButtons }]) => <ChatCommandBind |
61 | | - key={`${action}`} |
62 | | - group={'custom'} |
63 | | - action={action} |
64 | | - parseBindingName={parseBindingName} |
65 | | - handleClick={handleClick} |
66 | | - keys={keys} |
67 | | - userConfig={userConfig} |
68 | | - gamepadButtons={gamepadButtons} |
69 | | - resetBinding={resetBinding} |
70 | | - setActionName={setActionName} |
71 | | - setGroupName={setGroupName} |
72 | | - forceUpdate={forceUpdate} |
73 | | - isPS={isPS} |
74 | | - /> |
| 101 | + .map(([action, { keys, gamepadButtons }]) => |
| 102 | + <ChatCommandBind |
| 103 | + key={`${action}`} |
| 104 | + group={'custom'} |
| 105 | + action={action} |
| 106 | + parseBindingName={parseBindingName} |
| 107 | + handleClick={handleClick} |
| 108 | + keys={keys} |
| 109 | + userConfig={userConfig} |
| 110 | + gamepadButtons={gamepadButtons} |
| 111 | + resetBinding={resetBinding} |
| 112 | + setActionName={setActionName} |
| 113 | + setGroupName={setGroupName} |
| 114 | + forceUpdate={forceUpdate} |
| 115 | + isPS={isPS} |
| 116 | + /> |
75 | 117 | )} |
76 | 118 |
|
77 | 119 | <Button |
78 | | - onClick={addNewChatCommand} |
| 120 | + onClick={addNewCommand} |
79 | 121 | icon={'pixelarticons:add-box'} |
80 | 122 | style={{ |
81 | 123 | alignSelf: 'center' |
@@ -195,18 +237,3 @@ const ChatCommandBind = ({ |
195 | 237 | placeholder='Chat command' /> |
196 | 238 | </> |
197 | 239 | } |
198 | | - |
199 | | -const generateCode = () => { |
200 | | - const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
201 | | - const digits = '0123456789' |
202 | | - |
203 | | - let code = '' |
204 | | - for (let i = 0; i < 4; i++) { |
205 | | - const randomLetter = letters.charAt(Math.floor(Math.random() * letters.length)) |
206 | | - const randomDigit = digits.charAt(Math.floor(Math.random() * digits.length)) |
207 | | - code += (i < 3) ? randomLetter : '_' + randomDigit |
208 | | - } |
209 | | - |
210 | | - return code |
211 | | -} |
212 | | - |
|
0 commit comments