Skip to content

Commit

Permalink
Renaming or moving a component to a folder now refreshes the grid
Browse files Browse the repository at this point in the history
  • Loading branch information
vchelaru committed Jan 25, 2025
1 parent 3faa305 commit 987cb30
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 71 deletions.
117 changes: 59 additions & 58 deletions Gum/Managers/DragDropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,6 @@ public DragDropManager()
_elementCommands = ElementCommands.Self;
}

internal void HandleDragDropEvent(object sender, DragEventArgs e)
{
List<TreeNode> treeNodesToDrop = GetTreeNodesToDrop();
mDraggedItem = null;
TreeNode targetTreeNode = ElementTreeViewManager.Self.GetTreeNodeOver();
foreach(var draggedTreeNode in treeNodesToDrop )
{
object draggedObject = draggedTreeNode.Tag;

if (targetTreeNode != draggedTreeNode)
{
HandleDroppedItemOnTreeView(draggedObject, targetTreeNode);
}
}

string[] files = (string[])e.Data?.GetData(DataFormats.FileDrop);

if(files != null)
{
var isTargetRootScreenTreeNode = targetTreeNode == ElementTreeViewManager.Self.RootScreensTreeNode;
foreach(FilePath file in files)
{
if(file.Extension == GumProjectSave.ScreenExtension && isTargetRootScreenTreeNode)
{
ImportLogic.ImportScreen(file);
}
}
}
}

#region Drag+drop File (from windows explorer)

internal void HandleFileDragDrop(object sender, DragEventArgs e)
Expand Down Expand Up @@ -147,6 +117,31 @@ internal void HandleFileDragDrop(object sender, DragEventArgs e)
SaveAndRefresh();
}

private void AddNewInstanceForDrop(string fileName, float worldX, float worldY)
{
string nameToAdd = FileManager.RemovePath(FileManager.RemoveExtension(fileName));

var element = SelectedState.Self.SelectedElement;

IEnumerable<string> existingNames = element.Instances.Select(i => i.Name);
nameToAdd = StringFunctions.MakeStringUnique(nameToAdd, existingNames);

InstanceSave instance =
_elementCommands.AddInstance(element, nameToAdd);
instance.BaseType = "Sprite";

SetInstanceToPosition(worldX, worldY, instance);

var variableName = instance.Name + ".SourceFile";

var oldValue = SelectedState.Self.SelectedStateSave.GetValueOrDefault<string>(variableName);

SelectedState.Self.SelectedStateSave.SetValue(variableName, fileName, instance);

SetVariableLogic.Self.ReactToPropertyValueChanged("SourceFile", oldValue, element, instance, SelectedState.Self.SelectedStateSave, refresh: false);

}

private void TryHandleFileDropOnInstance(float worldX, float worldY, string[] files, ref bool handled, ref bool shouldUpdate)
{
// This only supports drag+drop on an instance, but what if dropping on a component
Expand Down Expand Up @@ -694,6 +689,36 @@ internal void HandleKeyPress(KeyPressEventArgs e)

#region General Functions

internal void HandleDragDropEvent(object sender, DragEventArgs e)
{
List<TreeNode> treeNodesToDrop = GetTreeNodesToDrop();
mDraggedItem = null;
TreeNode targetTreeNode = ElementTreeViewManager.Self.GetTreeNodeOver();
foreach(var draggedTreeNode in treeNodesToDrop )
{
object draggedObject = draggedTreeNode.Tag;

if (targetTreeNode != draggedTreeNode)
{
HandleDroppedItemOnTreeView(draggedObject, targetTreeNode);
}
}

string[] files = (string[])e.Data?.GetData(DataFormats.FileDrop);

if(files != null)
{
var isTargetRootScreenTreeNode = targetTreeNode == ElementTreeViewManager.Self.RootScreensTreeNode;
foreach(FilePath file in files)
{
if(file.Extension == GumProjectSave.ScreenExtension && isTargetRootScreenTreeNode)
{
ImportLogic.ImportScreen(file);
}
}
}
}

public void OnItemDrag(object item)
{
mDraggedItem = item;
Expand Down Expand Up @@ -859,34 +884,6 @@ internal void HandleKeyDown(System.Windows.Forms.KeyEventArgs e)
}
}

#endregion


private void AddNewInstanceForDrop(string fileName, float worldX, float worldY)
{
string nameToAdd = FileManager.RemovePath(FileManager.RemoveExtension(fileName));

var element = SelectedState.Self.SelectedElement;

IEnumerable<string> existingNames = element.Instances.Select(i => i.Name);
nameToAdd = StringFunctions.MakeStringUnique(nameToAdd, existingNames);

InstanceSave instance =
_elementCommands.AddInstance(element, nameToAdd);
instance.BaseType = "Sprite";

SetInstanceToPosition(worldX, worldY, instance);

var variableName = instance.Name + ".SourceFile";

var oldValue = SelectedState.Self.SelectedStateSave.GetValueOrDefault<string>(variableName);

SelectedState.Self.SelectedStateSave.SetValue(variableName, fileName, instance);

SetVariableLogic.Self.ReactToPropertyValueChanged("SourceFile", oldValue, element, instance, SelectedState.Self.SelectedStateSave, refresh:false);

}

private void SetInstanceToPosition(float worldX, float worldY, InstanceSave instance)
{
var component = SelectedState.Self.SelectedComponent;
Expand Down Expand Up @@ -932,4 +929,8 @@ private void SetInstanceToPosition(float worldX, float worldY, InstanceSave inst
SelectedState.Self.SelectedStateSave.SetValue(instance.Name + ".X", xToSet);
SelectedState.Self.SelectedStateSave.SetValue(instance.Name + ".Y", yToSet);
}

#endregion


}
33 changes: 22 additions & 11 deletions Gum/Plugins/InternalPlugins/VariableGrid/MainVariableGridPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ namespace Gum.Plugins.InternalPlugins.VariableGrid;
[Export(typeof(PluginBase))]
public class MainVariableGridPlugin : InternalPlugin
{
PropertyGridManager _propertyGridManager;

public MainVariableGridPlugin()
{
_propertyGridManager = PropertyGridManager.Self;
}

public override void StartUp()
{
Expand All @@ -32,32 +37,38 @@ private void AssignEvents()
this.InstanceSelected += HandleInstanceSelected;
this.ElementSelected += HandleElementSelected;
this.ElementDelete += HandleElementDeleted;
this.ElementRename += HandleElementRenamed;
this.BehaviorSelected += HandleBehaviorSelected;
this.VariableSelected += HandleVariableSelected;
this.RefreshVariableView += HandleRefreshVariableView;
this.AfterUndo += HandleAfterUndo;
this.VariableSet += HandleVariableSet;
}

private void HandleElementRenamed(ElementSave save, string arg2)
{
PropertyGridManager.Self.RefreshVariablesDataGridValues();
}

private void HandleVariableSet(ElementSave element, InstanceSave instance, string strippedName, object oldValue)
{
if(strippedName == "VariableReferences")
{
// force refresh:
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}
}

private void HandleElementDeleted(ElementSave save)
{
PropertyGridManager.Self.RefreshUI(force:false);
_propertyGridManager.RefreshEntireGrid(force:false);
}

private void HandleAfterUndo()
{
// An undo can result in variables added or removed, so let's
// do a full refresh
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void HandleVariableSelected(IStateContainer container, VariableSave save)
Expand All @@ -67,33 +78,33 @@ private void HandleVariableSelected(IStateContainer container, VariableSave save

private void HandleElementSelected(ElementSave save)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void HandleBehaviorSelected(BehaviorSave save)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void HandleInstanceSelected(ElementSave save1, InstanceSave save2)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void MainVariableGridPlugin_ReactToStateSaveCategorySelected(StateSaveCategory obj)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);

}

private void HandleStateMovedToCategory(StateSave save, StateSaveCategory category1, StateSaveCategory category2)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void HandleStateSelected(StateSave save)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}

private void HandleCustomStateSelected(StateSave save)
Expand All @@ -116,12 +127,12 @@ private void HandleTreeNodeSelected(TreeNode node)

if(selectedState.SelectedBehavior == null && selectedState.SelectedInstance == null && selectedState.SelectedElement == null)
{
PropertyGridManager.Self.RefreshUI(force: true);
_propertyGridManager.RefreshEntireGrid(force: true);
}
}

private void HandleRefreshVariableView(bool force)
{
PropertyGridManager.Self.RefreshUI(force);
_propertyGridManager.RefreshEntireGrid(force);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ private void HandleAddVariable(object sender, EventArgs e)
bool isInRefresh = false;


[Obsolete("Use GuiCommands.RefreshVariables")]
public void RefreshUI(bool force = false)
public void RefreshEntireGrid(bool force = false)
{
if (isInRefresh || ObjectsSuppressingRefresh.Count > 0)
return;
Expand Down

0 comments on commit 987cb30

Please sign in to comment.