mirror of
https://github.com/4ian/GDevelop.git
synced 2025-10-15 10:19:04 +00:00
Compare commits
3 Commits
v5.4.209
...
variable-r
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2b539f981c | ||
![]() |
eb382269fc | ||
![]() |
53d27c1b3c |
@@ -198,7 +198,24 @@ void Variable::MoveChildInArray(const size_t oldIndex, const size_t newIndex) {
|
||||
childrenArray.insert(childrenArray.begin() + newIndex, std::move(object));
|
||||
}
|
||||
|
||||
Variable& Variable::PushNew() { return GetAtIndex(GetChildrenCount()); };
|
||||
Variable& Variable::PushNew() {
|
||||
const size_t count = GetChildrenCount();
|
||||
auto& variable = GetAtIndex(count);
|
||||
if (type == Type::Array && count > 0) {
|
||||
const auto childType = GetAtIndex(count - 1).type;
|
||||
variable.type = childType;
|
||||
if (childType == Type::Number) {
|
||||
variable.SetValue(0);
|
||||
}
|
||||
else if (childType == Type::String) {
|
||||
variable.SetString("");
|
||||
}
|
||||
else if (childType == Type::Boolean) {
|
||||
variable.SetBool(false);
|
||||
}
|
||||
}
|
||||
return variable;
|
||||
};
|
||||
|
||||
void Variable::RemoveAtIndex(const size_t index) {
|
||||
if (index >= childrenArray.size()) return;
|
||||
|
@@ -65,7 +65,13 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
|
||||
open={editorOpen}
|
||||
variablesContainer={project.getVariables()}
|
||||
onCancel={() => setEditorOpen(false)}
|
||||
onApply={() => {
|
||||
onApply={(selectedVariableName: string | null) => {
|
||||
if (
|
||||
selectedVariableName &&
|
||||
selectedVariableName.startsWith(props.value)
|
||||
) {
|
||||
props.onChange(selectedVariableName);
|
||||
}
|
||||
setEditorOpen(false);
|
||||
if (field.current) field.current.updateAutocompletions();
|
||||
}}
|
||||
|
@@ -151,7 +151,13 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
|
||||
helpPagePath={'/all-features/variables/object-variables'}
|
||||
onComputeAllVariableNames={onComputeAllVariableNames}
|
||||
onCancel={() => setEditorOpen(false)}
|
||||
onApply={() => {
|
||||
onApply={(selectedVariableName: string | null) => {
|
||||
if (
|
||||
selectedVariableName &&
|
||||
selectedVariableName.startsWith(props.value)
|
||||
) {
|
||||
props.onChange(selectedVariableName);
|
||||
}
|
||||
setEditorOpen(false);
|
||||
if (field.current) field.current.updateAutocompletions();
|
||||
}}
|
||||
|
@@ -75,7 +75,13 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
|
||||
open
|
||||
variablesContainer={layout.getVariables()}
|
||||
onCancel={() => setEditorOpen(false)}
|
||||
onApply={() => {
|
||||
onApply={(selectedVariableName: string | null) => {
|
||||
if (
|
||||
selectedVariableName &&
|
||||
selectedVariableName.startsWith(props.value)
|
||||
) {
|
||||
props.onChange(selectedVariableName);
|
||||
}
|
||||
setEditorOpen(false);
|
||||
if (field.current) field.current.updateAutocompletions();
|
||||
}}
|
||||
|
@@ -63,8 +63,6 @@ export const insertInVariablesContainer = (
|
||||
if (serializedVariable) {
|
||||
unserializeFromJSObject(newVariable, serializedVariable);
|
||||
newVariable.resetPersistentUuid();
|
||||
} else {
|
||||
newVariable.setString('');
|
||||
}
|
||||
const variable = variablesContainer.insert(newName, newVariable, index);
|
||||
newVariable.delete();
|
||||
|
@@ -11,12 +11,26 @@ import useDismissableTutorialMessage from '../Hints/useDismissableTutorialMessag
|
||||
import { Column, Line } from '../UI/Grid';
|
||||
import VariablesList from './VariablesList';
|
||||
import HelpButton from '../UI/HelpButton';
|
||||
import { getVariableContextFromNodeId } from './VariableToTreeNodeHandling';
|
||||
|
||||
const gd: libGDevelop = global.gd;
|
||||
|
||||
const getVariablePathFromNodeId = (
|
||||
nodeId: string,
|
||||
variablesContainer: gdVariablesContainer
|
||||
): string => {
|
||||
const variableContext = getVariableContextFromNodeId(
|
||||
nodeId,
|
||||
variablesContainer
|
||||
);
|
||||
const variablePath = variableContext.lineage.map(variable => variable.name);
|
||||
variablePath.push(variableContext.name);
|
||||
return variablePath.join('.');
|
||||
};
|
||||
|
||||
type Props = {|
|
||||
onCancel: () => void,
|
||||
onApply: () => void,
|
||||
onApply: (selectedVariableName: string | null) => void,
|
||||
open: boolean,
|
||||
onEditObjectVariables?: () => void,
|
||||
title: React.Node,
|
||||
@@ -73,6 +87,12 @@ const VariablesEditorDialog = ({
|
||||
'intro-variables'
|
||||
);
|
||||
|
||||
const lastSelectedVariableNodeId = React.useRef<string | null>(null);
|
||||
const onSelectedVariableChange = React.useCallback((nodes: Array<string>) => {
|
||||
lastSelectedVariableNodeId.current =
|
||||
nodes.length > 0 ? nodes[nodes.length - 1] : null;
|
||||
}, []);
|
||||
|
||||
const onRefactorAndApply = React.useCallback(
|
||||
async () => {
|
||||
if (inheritedVariablesContainer) {
|
||||
@@ -106,7 +126,14 @@ const VariablesEditorDialog = ({
|
||||
}
|
||||
|
||||
variablesContainer.clearPersistentUuid();
|
||||
onApply();
|
||||
|
||||
onApply(
|
||||
lastSelectedVariableNodeId.current &&
|
||||
getVariablePathFromNodeId(
|
||||
lastSelectedVariableNodeId.current,
|
||||
variablesContainer
|
||||
)
|
||||
);
|
||||
},
|
||||
[
|
||||
onApply,
|
||||
@@ -175,6 +202,7 @@ const VariablesEditorDialog = ({
|
||||
onComputeAllVariableNames={onComputeAllVariableNames}
|
||||
helpPagePath={helpPagePath}
|
||||
onVariablesUpdated={notifyOfChange}
|
||||
onSelectedVariableChange={onSelectedVariableChange}
|
||||
/>
|
||||
</Column>
|
||||
</Dialog>
|
||||
|
@@ -109,6 +109,7 @@ type Props = {|
|
||||
/** If set to small, will collapse variable row by default. */
|
||||
size?: 'small',
|
||||
onVariablesUpdated?: () => void,
|
||||
onSelectedVariableChange?: (Array<string>) => void,
|
||||
|};
|
||||
|
||||
const variableRowStyles = {
|
||||
@@ -523,12 +524,28 @@ const VariablesList = (props: Props) => {
|
||||
);
|
||||
|
||||
const [searchText, setSearchText] = React.useState<string>('');
|
||||
const { onComputeAllVariableNames } = props;
|
||||
const { onComputeAllVariableNames, onSelectedVariableChange } = props;
|
||||
const allVariablesNames = React.useMemo<?Array<string>>(
|
||||
() => (onComputeAllVariableNames ? onComputeAllVariableNames() : null),
|
||||
[onComputeAllVariableNames]
|
||||
);
|
||||
const [selectedNodes, setSelectedNodes] = React.useState<Array<string>>([]);
|
||||
const [selectedNodes, doSetSelectedNodes] = React.useState<Array<string>>([]);
|
||||
const setSelectedNodes = React.useCallback(
|
||||
(nodes: Array<string> | ((nodes: Array<string>) => Array<string>)) => {
|
||||
// Use the functional update form to access the current state directly
|
||||
doSetSelectedNodes(currentSelectedNodes => {
|
||||
const newNodes = Array.isArray(nodes)
|
||||
? nodes
|
||||
: nodes(currentSelectedNodes);
|
||||
if (onSelectedVariableChange) {
|
||||
onSelectedVariableChange(newNodes);
|
||||
}
|
||||
return newNodes;
|
||||
});
|
||||
},
|
||||
[onSelectedVariableChange]
|
||||
);
|
||||
|
||||
const [searchMatchingNodes, setSearchMatchingNodes] = React.useState<
|
||||
Array<string>
|
||||
>([]);
|
||||
@@ -627,7 +644,7 @@ const VariablesList = (props: Props) => {
|
||||
historyRef.current = undo(historyRef.current, props.variablesContainer);
|
||||
setSelectedNodes([]);
|
||||
},
|
||||
[historyRef, historyHandler, props.variablesContainer]
|
||||
[historyHandler, historyRef, props.variablesContainer, setSelectedNodes]
|
||||
);
|
||||
|
||||
const _redo = React.useCallback(
|
||||
@@ -637,7 +654,7 @@ const VariablesList = (props: Props) => {
|
||||
historyRef.current = redo(historyRef.current, props.variablesContainer);
|
||||
setSelectedNodes([]);
|
||||
},
|
||||
[historyRef, historyHandler, props.variablesContainer]
|
||||
[historyHandler, historyRef, props.variablesContainer, setSelectedNodes]
|
||||
);
|
||||
|
||||
const _canUndo = (): boolean =>
|
||||
@@ -812,6 +829,7 @@ const VariablesList = (props: Props) => {
|
||||
props.inheritedVariablesContainer,
|
||||
props.variablesContainer,
|
||||
selectedNodes,
|
||||
setSelectedNodes,
|
||||
]
|
||||
);
|
||||
|
||||
@@ -866,7 +884,7 @@ const VariablesList = (props: Props) => {
|
||||
setSelectedNodes([]);
|
||||
}
|
||||
},
|
||||
[_onChange, _deleteNode, selectedNodes]
|
||||
[selectedNodes, _deleteNode, _onChange, setSelectedNodes]
|
||||
);
|
||||
|
||||
const updateExpandedAndSelectedNodesFollowingNameChange = React.useCallback(
|
||||
@@ -884,7 +902,7 @@ const VariablesList = (props: Props) => {
|
||||
);
|
||||
}
|
||||
},
|
||||
[searchText]
|
||||
[searchText, setSelectedNodes]
|
||||
);
|
||||
|
||||
const updateExpandedAndSelectedNodesFollowingNodeMove = React.useCallback(
|
||||
@@ -896,7 +914,7 @@ const VariablesList = (props: Props) => {
|
||||
forceUpdate();
|
||||
}
|
||||
},
|
||||
[forceUpdate, searchText, triggerSearch]
|
||||
[forceUpdate, searchText, setSelectedNodes, triggerSearch]
|
||||
);
|
||||
|
||||
const canDrop = React.useCallback(
|
||||
@@ -1131,10 +1149,7 @@ const VariablesList = (props: Props) => {
|
||||
const type = variable.getType();
|
||||
|
||||
if (type === gd.Variable.Structure) {
|
||||
const name = newNameGenerator('ChildVariable', name =>
|
||||
variable.hasChild(name)
|
||||
);
|
||||
variable.getChild(name).setString('');
|
||||
newNameGenerator('ChildVariable', name => variable.hasChild(name));
|
||||
} else if (type === gd.Variable.Array) variable.pushNew();
|
||||
_onChange();
|
||||
if (variable.isFolded()) variable.setFolded(false);
|
||||
@@ -1169,7 +1184,12 @@ const VariablesList = (props: Props) => {
|
||||
setSelectedNodes([inheritedVariableName]);
|
||||
newVariable.delete();
|
||||
},
|
||||
[_onChange, props.inheritedVariablesContainer, props.variablesContainer]
|
||||
[
|
||||
_onChange,
|
||||
props.inheritedVariablesContainer,
|
||||
props.variablesContainer,
|
||||
setSelectedNodes,
|
||||
]
|
||||
);
|
||||
|
||||
const onAdd = React.useCallback(
|
||||
@@ -1223,6 +1243,7 @@ const VariablesList = (props: Props) => {
|
||||
props.variablesContainer,
|
||||
refocusNameField,
|
||||
selectedNodes,
|
||||
setSelectedNodes,
|
||||
]
|
||||
);
|
||||
|
||||
@@ -1248,7 +1269,7 @@ const VariablesList = (props: Props) => {
|
||||
}
|
||||
});
|
||||
},
|
||||
[]
|
||||
[setSelectedNodes]
|
||||
);
|
||||
|
||||
const renderVariableAndChildrenRows = (
|
||||
@@ -1606,6 +1627,7 @@ const VariablesList = (props: Props) => {
|
||||
props.inheritedVariablesContainer,
|
||||
props.variablesContainer,
|
||||
refocusValueField,
|
||||
setSelectedNodes,
|
||||
]
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user