Skip to content

Commit

Permalink
add yet another utils (#1)
Browse files Browse the repository at this point in the history
* state-machine

* another iteration

* remove state machine utils
  • Loading branch information
decembrist-revolt authored Oct 3, 2021
1 parent e828a88 commit 17d4a1f
Show file tree
Hide file tree
Showing 14 changed files with 724 additions and 40 deletions.
7 changes: 6 additions & 1 deletion Example/Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Decembrist.Example
{
public static class Assertions
{
public static void AssertTrue(bool expression, string test)
public static void AssertTrue(bool expression, string test = "true assertion failed")
{
if (expression)
{
Expand All @@ -33,5 +33,10 @@ public static void AssertNotNull(object? expression, string test = "null asserti
throw new Exception(message);
}
}

public static void AssertEquals(object? expected, object? actual, string test = "equals assertion failed")
{
AssertTrue(expected == actual, test);
}
}
}
47 changes: 30 additions & 17 deletions addons/decembrist_plugin/DecembristPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
#if TOOLS

using Godot;

[Tool]
public class DecembristPlugin : EditorPlugin
namespace Decembrist
{
public override void EnablePlugin()
[Tool]
public class DecembristPlugin : EditorPlugin
{
AddAutoloadSingleton("DecembristAutoload", "res://addons/decembrist_plugin/Autoload/DecembristAutoload.cs");
CheckSetting(DecembristSettings.ConfigClass, "DecembristConfiguration");
CheckSetting(DecembristSettings.EventBusEnabled, true);
CheckSetting(DecembristSettings.LanEventsEnabled, false);
}
public override void EnablePlugin()
{
AddAutoloadSingleton("DecembristAutoload", "res://addons/decembrist_plugin/Autoload/DecembristAutoload.cs");
CheckSetting(DecembristSettings.ConfigClass, "DecembristConfiguration");
CheckSetting(DecembristSettings.EventBusEnabled, true);
CheckSetting(DecembristSettings.LanEventsEnabled, false);
CheckSetting(DecembristSettings.LanEventsEnabled, false);
}

public override void DisablePlugin()
{
RemoveAutoloadSingleton("DecembristAutoload");
}
public override void _EnterTree()
{
}

private void CheckSetting(string name, object @default)
{
var setting = ProjectSettings.GetSetting(name);
if (setting == null)
public override void DisablePlugin()
{
RemoveAutoloadSingleton("DecembristAutoload");
}

public override void _ExitTree()
{
}

private void CheckSetting(string name, object @default)
{
ProjectSettings.SetSetting(name, @default);
var setting = ProjectSettings.GetSetting(name);
if (setting == null)
{
ProjectSettings.SetSetting(name, @default);
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions addons/decembrist_plugin/DecembristSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
public static class DecembristSettings
namespace Decembrist
{
public const string ConfigClass = "decembrist_plugin/commons/config_class";
public const string EventBusEnabled = "decembrist_plugin/commons/event_bus_enabled";
public const string LanEventsEnabled = "decembrist_plugin/commons/lan_events_enabled";
public static class DecembristSettings
{
public const string ConfigClass = "decembrist_plugin/commons/config_class";
public const string EventBusEnabled = "decembrist_plugin/commons/event_bus_enabled";
public const string LanEventsEnabled = "decembrist_plugin/commons/lan_events_enabled";
}
}
35 changes: 34 additions & 1 deletion addons/decembrist_plugin/Utils/Buttons.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Decembrist.Utils.Callback;
using System;
using Decembrist.Utils.Callback;
using Godot;
using VoidFunc = System.Action;

Expand Down Expand Up @@ -40,4 +41,36 @@ public static VoidFunc OnButtonUp(this Button button, VoidFunc action)
return () => button.Unsubscribe(ButtonUpSignal, callback);
}
}

public static class OptionButtons
{
public const string ItemFocusedSignal = "item_focused";
public const string ItemSelectedSignal = "item_selected";

/// <summary>
/// Emitted when the user navigates to an item using the ui_up or ui_down actions.
/// The index of the item selected is passed as argument.
/// </summary>
/// <param name="button">This button</param>
/// <param name="action">action( int index )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnItemFocused(this OptionButton button, Action<int> action)
{
var callback = button.Subscribe(ItemFocusedSignal, action);
return () => button.Unsubscribe(ItemFocusedSignal, callback);
}

/// <summary>
/// Emitted when the current item has been changed by the user.
/// The index of the item selected is passed as argument.
/// </summary>
/// <param name="button">This button</param>
/// <param name="action">action( int index )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnItemSelected(this OptionButton button, Action<int> action)
{
var callback = button.Subscribe(ItemSelectedSignal, action);
return () => button.Unsubscribe(ItemSelectedSignal, callback);
}
}
}
141 changes: 127 additions & 14 deletions addons/decembrist_plugin/Utils/Controls.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,151 @@
using System;
using Decembrist.Utils.Callback;
using Godot;
using VoidFunc = System.Action;

namespace Decembrist.Utils
{
public static class Controls
{
public static AbstractCallback OnPointerEnter(this Control control, Action callback)
public const string FocusEnteredSignal = "focus_entered";
public const string FocusExitedSignal = "focus_exited";
public const string GuiInputSignal = "gui_input";
public const string MinimumSizeChangedSignal = "minimum_size_changed";
public const string ModalClosedSignal = "modal_closed";
public const string MouseEnteredSignal = "mouse_entered";
public const string MouseExitedSignal = "mouse_exited";
public const string ResizedSignal = "resized";
public const string SizeFlagsChangedSignal = "size_flags_changed";

/// <summary>
/// Emitted when the node gains keyboard focus.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnFocusEntered(this Control control, VoidFunc action)
{
return control.Subscribe("mouse_entered", callback);
var callback = control.Subscribe(FocusEnteredSignal, action);
return () => control.Unsubscribe(FocusEnteredSignal, callback);
}

public static AbstractCallback OnPointerExit(this Control control, Action callback)

/// <summary>
/// Emitted when the node loses keyboard focus.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnFocusExited(this Control control, VoidFunc action)
{
return control.Subscribe("mouse_exited", callback);
var callback = control.Subscribe(FocusExitedSignal, action);
return () => control.Unsubscribe(FocusExitedSignal, callback);
}

public static AbstractCallback OnGuiInput(this Control control, Action<InputEvent> callback)

/// <summary>
/// Emitted when the node receives an InputEvent.
/// </summary>
/// <param name="control">This control</param>
/// <param name="action">action ( InputEvent event )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnGuiInput(this Control control, Action<InputEvent> action)
{
return control.Subscribe("gui_input", callback);
var callback = control.Subscribe(GuiInputSignal, action);
return () => control.Unsubscribe(GuiInputSignal, callback);
}

public static AbstractCallback OnGuiInput(
/// <summary>
/// Emitted when the node receives an InputEvent.
/// </summary>
/// <param name="control">This control</param>
/// <param name="inputPredicate">Invokes only if the event satisfies the predicate</param>
/// <param name="action">action ( InputEvent event )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnGuiInput(
this Control control,
Func<InputEvent, bool> eventPredicate,
Action<InputEvent> callback)
Func<InputEvent, bool> inputPredicate,
Action<InputEvent> action)
{
return control.Subscribe("gui_input", (InputEvent @event) =>
var callback = control.Subscribe(GuiInputSignal, (InputEvent @event) =>
{
if (eventPredicate(@event))
if (inputPredicate(@event))
{
callback(@event);
action(@event);
}
});
return () => control.Unsubscribe(GuiInputSignal, callback);
}

/// <summary>
/// Emitted when the node receives an mouse InputEvent.
/// </summary>
/// <param name="control">This control</param>
/// <param name="action">action ( InputEventMouseButton event )</param>
/// <param name="inputPredicate">Invokes only if the event satisfies the predicate</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnMouseInput(
this Control control,
Action<InputEventMouseButton> action,
Func<InputEventMouseButton, bool> inputPredicate = null) => control.OnGuiInput(
@event => @event is InputEventMouseButton mouseEvent
&& (inputPredicate == null || inputPredicate(mouseEvent)),
@event => action(@event as InputEventMouseButton)
);


/// <summary>
/// Emitted when the node's minimum size changes.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnMinimumSizeChanged(this Control control, Action action)
{
var callback = control.Subscribe(MinimumSizeChangedSignal, action);
return () => control.Unsubscribe(MinimumSizeChangedSignal, callback);
}

/// <summary>
/// Emitted when a modal Control is closed. See show_modal.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnModalClosed(this Control control, Action action)
{
var callback = control.Subscribe(ModalClosedSignal, action);
return () => control.Unsubscribe(ModalClosedSignal, callback);
}

/// <summary>
/// Emitted when the mouse enters the control's Rect area, provided its mouse_filter lets the event reach it.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnMouseEntered(this Control control, Action action)
{
var callback = control.Subscribe(MouseEnteredSignal, action);
return () => control.Unsubscribe(MouseEnteredSignal, callback);
}

/// <summary>
/// Emitted when the mouse leaves the control's Rect area, provided its mouse_filter lets the event reach it.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnMouseExited(this Control control, Action action)
{
var callback = control.Subscribe(MouseExitedSignal, action);
return () => control.Unsubscribe(MouseExitedSignal, callback);
}

/// <summary>
/// Emitted when the control changes size.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnResized(this Control control, Action action)
{
var callback = control.Subscribe(ResizedSignal, action);
return () => control.Unsubscribe(ResizedSignal, callback);
}

/// <summary>
/// Emitted when one of the size flags changes. See size_flags_horizontal and size_flags_vertical.
/// </summary>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnSizeFlagsChanged(this Control control, Action action)
{
var callback = control.Subscribe(SizeFlagsChangedSignal, action);
return () => control.Unsubscribe(SizeFlagsChangedSignal, callback);
}
}
}
61 changes: 61 additions & 0 deletions addons/decembrist_plugin/Utils/EditorPlugins.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using Decembrist.Utils.Callback;
using Godot;
using VoidFunc = System.Action;

namespace Decembrist.Utils
{
public static class EditorPlugins
{
public const string MainScreenChangedSignal = "main_screen_changed";
public const string ResourceSavedSignal = "resource_saved";
public const string SceneChangedSignal = "scene_changed";
public const string SceneClosedSignal = "scene_closed";

/// <summary>
/// Emitted when user changes the workspace (2D, 3D, Script, AssetLib). Also works with custom screens defined by plugins.
/// </summary>
/// <param name="plugin">EditorPlugin object</param>
/// <param name="action">action(String screen_name)</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnMainScreenChanged(this EditorPlugin plugin, Action<string> action)
{
var callback = plugin.Subscribe(MainScreenChangedSignal, action);
return () => plugin.Unsubscribe(MainScreenChangedSignal, callback);
}

/// <param name="plugin">EditorPlugin object</param>
/// <param name="action">action( Resource resource )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnResourceSaved(this EditorPlugin plugin, Action<Resource> action)
{
var callback = plugin.Subscribe(ResourceSavedSignal, action);
return () => plugin.Unsubscribe(ResourceSavedSignal, callback);
}

/// <summary>
/// Emitted when the scene is changed in the editor. The argument will return the root node of the scene that
/// has just become active. If this scene is new and empty, the argument will be null.
/// </summary>
/// <param name="plugin">EditorPlugin object</param>
/// <param name="action">action( Node scene_root )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnSceneChanged(this EditorPlugin plugin, Action<Node> action)
{
var callback = plugin.Subscribe(SceneChangedSignal, action);
return () => plugin.Unsubscribe(SceneChangedSignal, callback);
}

/// <summary>
/// Emitted when user closes a scene. The argument is file path to a closed scene.
/// </summary>
/// <param name="plugin">EditorPlugin object</param>
/// <param name="action">action( String filepath )</param>
/// <returns>Unsubscribe callback</returns>
public static VoidFunc OnSceneClosed(this EditorPlugin plugin, Action<Node> action)
{
var callback = plugin.Subscribe(SceneClosedSignal, action);
return () => plugin.Unsubscribe(SceneClosedSignal, callback);
}
}
}
Loading

0 comments on commit 17d4a1f

Please sign in to comment.