Skip to content

Commit

Permalink
File scoped namespaces (release 0.2.12) (#67)
Browse files Browse the repository at this point in the history
* Convert all scripts to use file-scoped namespaces

* Update .gitignore

* release 0.2.12

---------

Co-authored-by: valkyrienyanko <[email protected]>
  • Loading branch information
dogboydog and valkyrienyanko committed Sep 15, 2024
1 parent 2a82989 commit b12dac5
Show file tree
Hide file tree
Showing 41 changed files with 6,630 additions and 6,661 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
!.vscode/extensions.json
*.code-workspace

## VisualStudio

.vs/

# Local History for Visual Studio Code
.history/

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [0.2.12] 2024-09-15
* Use file-scoped namespaces in all plugin scripts to reduce indentation, by @valkyrienyanko

## [0.2.11] 2024-09-13
* Fix an issue where the OptionsListView did not fade in properly and instead suddenly appeared at the end of the fade time. (Fix #62)
* `Effects.Fade` now uses a Godot Tween.
Expand Down
99 changes: 49 additions & 50 deletions addons/YarnSpinner-Godot/Editor/YarnCompileErrorsPropertyEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,68 @@
using Godot;
using Array = Godot.Collections.Array;

namespace YarnSpinnerGodot.Editor
namespace YarnSpinnerGodot.Editor;

[Tool]
public partial class YarnCompileErrorsPropertyEditor : EditorProperty
{
[Tool]
public partial class YarnCompileErrorsPropertyEditor : EditorProperty
{
// The main control for editing the property.
private Label _propertyControl;
// The main control for editing the property.
private Label _propertyControl;

// An internal value of the property.
private Array _currentValue;
// An internal value of the property.
private Array _currentValue;

[Signal]
public delegate void OnErrorsUpdateEventHandler(GodotObject yarnProject);
[Signal]
public delegate void OnErrorsUpdateEventHandler(GodotObject yarnProject);

public YarnCompileErrorsPropertyEditor()
{
_propertyControl = new Label();
Label = "Project Errors";
// Add the control as a direct child of EditorProperty node.
AddChild(_propertyControl);
// Make sure the control is able to retain the focus.
AddFocusable(_propertyControl);
// Setup the initial state and connect to the signal to track changes.
RefreshControlText();
}

public YarnCompileErrorsPropertyEditor()
public override void _UpdateProperty()
{
// Read the current value from the property.
var newVariantValue = GetEditedObject().Get(GetEditedProperty());
var newValue = (Array) newVariantValue;
if (newValue == _currentValue)
{
_propertyControl = new Label();
Label = "Project Errors";
// Add the control as a direct child of EditorProperty node.
AddChild(_propertyControl);
// Make sure the control is able to retain the focus.
AddFocusable(_propertyControl);
// Setup the initial state and connect to the signal to track changes.
RefreshControlText();
return;
}

public override void _UpdateProperty()
{
// Read the current value from the property.
var newVariantValue = GetEditedObject().Get(GetEditedProperty());
var newValue = (Array) newVariantValue;
if (newValue == _currentValue)
{
return;
}
_currentValue = newValue;
RefreshControlText();
EmitSignal(SignalName.OnErrorsUpdate);
}

_currentValue = newValue;
RefreshControlText();
EmitSignal(SignalName.OnErrorsUpdate);
private void RefreshControlText()
{
if (_currentValue == null)
{
_propertyControl.Text = "";
}

private void RefreshControlText()
else if (_currentValue.Count == 0)
{
if (_currentValue == null)
{
_propertyControl.Text = "";
}
else if (_currentValue.Count == 0)
{
_propertyControl.Text = "None";
}
else
{
_propertyControl.Text =
$"{_currentValue.Count} error{(_currentValue.Count > 1 ? "s" : "")}";
}
_propertyControl.Text = "None";
}

public void Refresh()
else
{
EmitChanged(GetEditedProperty(),
GetEditedObject().Get(GetEditedProperty()));
_propertyControl.Text =
$"{_currentValue.Count} error{(_currentValue.Count > 1 ? "s" : "")}";
}
}

public void Refresh()
{
EmitChanged(GetEditedProperty(),
GetEditedObject().Get(GetEditedProperty()));
}
}
#endif
173 changes: 86 additions & 87 deletions addons/YarnSpinner-Godot/Editor/YarnEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,109 @@
using File = System.IO.File;
using Path = System.IO.Path;

namespace YarnSpinnerGodot.Editor
namespace YarnSpinnerGodot.Editor;

/// <summary>
/// Contains utility methods for working with Yarn Spinner content in
/// the Godot editor.
/// </summary>
public static class YarnEditorUtility
{


const string TemplateFilePath = "res://addons/YarnSpinner-Godot/Editor/YarnScriptTemplate.txt";

/// <summary>
/// Contains utility methods for working with Yarn Spinner content in
/// the Godot editor.
/// </summary>
public static class YarnEditorUtility
/// Menu Item "Tools > YarnSpinner > Create Yarn Script"
///
/// </summary>
public static void CreateYarnScript(string scriptPath)
{
GD.Print($"Creating new yarn script at {scriptPath}");
CreateYarnScriptAssetFromTemplate(scriptPath);
}


const string TemplateFilePath = "res://addons/YarnSpinner-Godot/Editor/YarnScriptTemplate.txt";

/// <summary>
/// Menu Item "Tools > YarnSpinner > Create Yarn Script"
///
/// </summary>
public static void CreateYarnScript(string scriptPath)
/// <summary>
/// Menu Item "Tools > YarnSpinner > Create Yarn Script"
/// </summary>
/// <param name="projectPath">res:// path of the YarnProject resource to create</param>
public static void CreateYarnProject(string projectPath)
{
var jsonProject = new Yarn.Compiler.Project();
var absPath = ProjectSettings.GlobalizePath(projectPath);
jsonProject.SaveToFile(absPath);
}
/// <summary>
/// Menu Item "Tools > YarnSpinner > Create Markup Palette"
/// </summary>
/// <param name="palettePath">res:// path to the markup palette to create</param>
public static void CreateMarkupPalette(string palettePath)
{
var newPalette = new MarkupPalette();
var absPath = ProjectSettings.GlobalizePath(palettePath);
newPalette.ResourceName = Path.GetFileNameWithoutExtension(absPath);
newPalette.ResourcePath = palettePath;
var saveErr = ResourceSaver.Save(newPalette, palettePath);
if (saveErr != Error.Ok)
{
GD.Print($"Creating new yarn script at {scriptPath}");
CreateYarnScriptAssetFromTemplate(scriptPath);
GD.Print($"Failed to save markup palette to {palettePath}");
}
}

/// <summary>
/// Menu Item "Yarn Spinner/Create Yarn Script"
///
/// </summary>
/// <param name="localizationPath"></param>
public static void CreateYarnLocalization(string localizationPath)
{
var newLocalization = new Localization();
var absPath = ProjectSettings.GlobalizePath(localizationPath);
newLocalization.ResourceName = Path.GetFileNameWithoutExtension(absPath);
newLocalization.ResourcePath = localizationPath;
ResourceSaver.Save( newLocalization , localizationPath);
GD.Print($"Saved new yarn localization to {localizationPath}");
}

/// <summary>
/// Menu Item "Tools > YarnSpinner > Create Yarn Script"
/// </summary>
/// <param name="projectPath">res:// path of the YarnProject resource to create</param>
public static void CreateYarnProject(string projectPath)
{
var jsonProject = new Yarn.Compiler.Project();
var absPath = ProjectSettings.GlobalizePath(projectPath);
jsonProject.SaveToFile(absPath);
}
/// <summary>
/// Menu Item "Tools > YarnSpinner > Create Markup Palette"
/// </summary>
/// <param name="palettePath">res:// path to the markup palette to create</param>
public static void CreateMarkupPalette(string palettePath)
private static void CreateYarnScriptAssetFromTemplate(string pathName)
{
// Read the contents of the template file
string templateContent;
try
{
var newPalette = new MarkupPalette();
var absPath = ProjectSettings.GlobalizePath(palettePath);
newPalette.ResourceName = Path.GetFileNameWithoutExtension(absPath);
newPalette.ResourcePath = palettePath;
var saveErr = ResourceSaver.Save(newPalette, palettePath);
if (saveErr != Error.Ok)
{
GD.Print($"Failed to save markup palette to {palettePath}");
}
templateContent = File.ReadAllText(ProjectSettings.GlobalizePath(TemplateFilePath));
}

/// <summary>
/// Menu Item "Yarn Spinner/Create Yarn Script"
///
/// </summary>
/// <param name="localizationPath"></param>
public static void CreateYarnLocalization(string localizationPath)
catch
{
var newLocalization = new Localization();
var absPath = ProjectSettings.GlobalizePath(localizationPath);
newLocalization.ResourceName = Path.GetFileNameWithoutExtension(absPath);
newLocalization.ResourcePath = localizationPath;
ResourceSaver.Save( newLocalization , localizationPath);
GD.Print($"Saved new yarn localization to {localizationPath}");
GD.PrintErr("Failed to find the Yarn script template file. Creating an empty file instead.");
// the minimal valid Yarn script - no headers, no body
templateContent = "---\n===\n";
}

private static void CreateYarnScriptAssetFromTemplate(string pathName)
{
// Read the contents of the template file
string templateContent;
try
{
templateContent = File.ReadAllText(ProjectSettings.GlobalizePath(TemplateFilePath));
}
catch
{
GD.PrintErr("Failed to find the Yarn script template file. Creating an empty file instead.");
// the minimal valid Yarn script - no headers, no body
templateContent = "---\n===\n";
}

// Figure out the 'file name' that the user entered
// The script name is the name of the file, sans extension.
string scriptName = Path.GetFileNameWithoutExtension(pathName);
// Figure out the 'file name' that the user entered
// The script name is the name of the file, sans extension.
string scriptName = Path.GetFileNameWithoutExtension(pathName);

// Replace any spaces with underscores - these aren't allowed
// in node names
scriptName = scriptName.Replace(" ", "_");
// Replace any spaces with underscores - these aren't allowed
// in node names
scriptName = scriptName.Replace(" ", "_");

// Replace the placeholder with the script name
templateContent = templateContent.Replace("#SCRIPTNAME#", scriptName);
// Replace the placeholder with the script name
templateContent = templateContent.Replace("#SCRIPTNAME#", scriptName);

string lineEndings = "\n";
string lineEndings = "\n";

// Replace every line ending in the template (this way we don't
// need to keep track of which line ending the asset was last
// saved in)
templateContent = System.Text.RegularExpressions.Regex.Replace(templateContent, @"\r\n?|\n", lineEndings);
// Replace every line ending in the template (this way we don't
// need to keep track of which line ending the asset was last
// saved in)
templateContent = System.Text.RegularExpressions.Regex.Replace(templateContent, @"\r\n?|\n", lineEndings);

// Write it all out to disk as UTF-8
var fullPath = Path.GetFullPath(ProjectSettings.GlobalizePath(pathName));
File.WriteAllText(fullPath, templateContent, System.Text.Encoding.UTF8);
GD.Print($"Wrote new file {pathName}");
YarnSpinnerPlugin.editorInterface.GetResourceFilesystem().ScanSources();
}

// Write it all out to disk as UTF-8
var fullPath = Path.GetFullPath(ProjectSettings.GlobalizePath(pathName));
File.WriteAllText(fullPath, templateContent, System.Text.Encoding.UTF8);
GD.Print($"Wrote new file {pathName}");
YarnSpinnerPlugin.editorInterface.GetResourceFilesystem().ScanSources();
}

}
#endif
Loading

0 comments on commit b12dac5

Please sign in to comment.