draft_texture_button #2

Merged
hzhang merged 5 commits from draft_texture_button into master 2025-02-18 14:29:28 +00:00
15 changed files with 320 additions and 112 deletions

View File

@@ -0,0 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PoloniumTemplateTargetPath>$(ProjectDir)script_templates/</PoloniumTemplateTargetPath>
<PoloniumTemplateManifest>$(ProjectDir)embedded/templates.polonium.manifest</PoloniumTemplateManifest>
</PropertyGroup>
</Project>

View File

@@ -1,38 +1,29 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PoloniumTemplateTargetPath>$(ProjectDir)script_templates/</PoloniumTemplateTargetPath>
<PoloniumTemplateManifest>$(PoloniumTemplateTargetPath).polonium.manifest</PoloniumTemplateManifest>
</PropertyGroup>
<ItemGroup Label="Scene">
<ItemGroup>
<Folder Include="Concepts"/>
<Folder Include="Assets"/>
<Folder Include="Registries"/>
<Folder Include="Data"/>
<Folder Include="Resources"/>
<None Include="script_templates/**/*.*"/>
<Compile Remove="script_templates/**/*.*"/>
<Compile Remove="embedded/polonium_templates/**/*.cs"/>
<AdditionalFiles Include="Assets/**/*.tscn"/>
</ItemGroup>
<Target Name="EnsureFolders" BeforeTargets="BeforeBuild">
<Target Name="PoloniumPrepare" BeforeTargets="PoloniumClean">
<Message Text="Executing EnsureFolders target" Importance="High"/>
<Message Text="$(ProjectDir)" Importance="High"/>
<MakeDir Directories="$(ProjectDir)Assets" Condition="!Exists('$(ProjectDir)Assets')"/>
<MakeDir Directories="$(ProjectDir)Concepts" Condition="!Exists('$(ProjectDir)Concepts')"/>
<MakeDir Directories="$(ProjectDir)Registries" Condition="!Exists('$(ProjectDir)Registries')"/>
<MakeDir Directories="$(ProjectDir)Data" Condition="!Exists('$(ProjectDir)Data')"/>
<MakeDir Directories="$(ProjectDir)Resources" Condition="!Exists('$(ProjectDir)Resources')"/>
<MakeDir Directories="$(ProjectDir)Resources/ButtonTextureSet" Condition="!Exists('$(ProjectDir)Resources/ButtonTextureSet')"/>
<MakeDir Directories="$(ProjectDir)script_templates" Condition="!Exists('$(ProjectDir)script_templates')"/>
<Exec
Command='rmdir "$(ProjectDir)GlobalClasses"'
Condition="'$(OS)'=='Windows_NT' and Exists('$(ProjectDir)GlobalClasses')"
/>
<Exec
Command='mklink /D "$(ProjectDir)GlobalClasses" "$(MSBuildThisFileDirectory)GlobalClasses"'
Condition="'$(OS)'=='Windows_NT' and !Exists('$(ProjectDir)GlobalClasses')"
/>
<Exec
Command='rm -rf "$(ProjectDir)GlobalClasses"'
Condition="'$(OS)'!='Windows_NT' and Exists('$(ProjectDir)GlobalClasses')"
/>
<Exec
Command='ln -s "$(MSBuildThisFileDirectory)GlobalClasses" "$(ProjectDir)GlobalClasses"'
Condition="'$(OS)'!='Windows_NT' and !Exists('$(ProjectDir)GlobalClasses')"
/>
</Target>
<Target Name="CleanPoloniumTemplates" BeforeTargets="CopyPoloniumTemplates">
<Target Name="PoloniumClean" DependsOnTargets="PoloniumPrepare" BeforeTargets="PoloniumRegenerate">
<ReadLinesFromFile
File="$(PoloniumTemplateManifest)"
Condition="Exists('$(PoloniumTemplateManifest)')"
@@ -40,21 +31,27 @@
<Output TaskParameter="Lines" ItemName="PreviousTemplateFiles"/>
</ReadLinesFromFile>
<Delete Files="@(PreviousTemplateFiles)" Condition="@(PreviousTemplateFiles) != ''" />
<Delete
Files="$(PoloniumTemplateManifest)"
Condition="Exists('$(PoloniumTemplateManifest)')"
/>
<RemoveDir Directories="$(ProjectDir)embedded" Condition="Exists('$(ProjectDir)embedded')"/>
</Target>
<Target Name="CopyPoloniumTemplates" DependsOnTargets="CleanPoloniumTemplates" AfterTargets="Build">
<Target Name="PoloniumRegenerate" DependsOnTargets="PoloniumClean" BeforeTargets="CoreCompile">
<MakeDir Directories="$(ProjectDir)" Condition="!Exists('$(ProjectDir)embedded')"/>
<ItemGroup>
<PoloniumTemplateFiles Include="$(MSBuildThisFileDirectory)polonium_templates/**/*.*"/>
<EmbeddedFiles Include="$(MSBuildThisFileDirectory)embedded/**/*.*"/>
</ItemGroup>
<Copy
SourceFiles="@(EmbeddedFiles)"
DestinationFolder="$(ProjectDir)embedded/%(RecursiveDir)"
/>
<ItemGroup>
<PoloniumTemplateFiles Include="$(MSBuildThisFileDirectory)embedded/polonium_templates/**/*.*" />
</ItemGroup>
<Message Text="Executing Copy Task" Importance="high"/>
<Copy
SourceFiles="@(PoloniumTemplateFiles)"
DestinationFiles="@(PoloniumTemplateFiles->'$(PoloniumTemplateTargetPath)%(RecursiveDir)%(FileName)%(Extension)')"
>
<Output TaskParameter="CopiedFiles" ItemName="CopiedTemplates"/>
<Output TaskParameter="CopiedFiles" ItemName="CopiedTemplates" />
</Copy>
<WriteLinesToFile
File="$(PoloniumTemplateManifest)"
@@ -62,14 +59,11 @@
Overwrite="true"
Encoding="UTF-8"
/>
<GenerateTextureSetTask
RootPath="$(ProjectDir)Resources/ButtonTextureSet/"
OutputPath="$(ProjectDir)embedded/"
/>
</Target>
<ItemGroup>
<Folder Include="Concepts"/>
<Folder Include="Assets"/>
<Folder Include="Registries"/>
<Folder Include="Data"/>
<None Include="script_templates/**/*.*"/>
<Compile Remove="script_templates/**/*.*"/>
<PackageReference Include="Polonium.Generators" Version="0.1.1-x" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,61 @@
#pragma warning disable IDE0130
using Godot;
using Polonium.Attributes;
using Polonium.DataStructures;
using System.Collections.Generic;
using Polonium.Interfaces;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[GlobalClass]
[Tool]
[ProxyNode]
public partial class PoloniumTextureButton : TextureButton
{
[ProxyProperty]
public virtual TextureSet TextureSet => null;
private GlobalRegistry.TextureSetName PrivateTextureSetName { get; set; }
[Export]
public GlobalRegistry.TextureSetName TextureSetName
{
get => PrivateTextureSetName;
set
{
PrivateTextureSetName = value;
t = GlobalRegistry.TextureSetMap.GetValueOrDefault(value, null);
if (t is not null)
{
TextureNormal = t.Normal;
TextureHover = t.Hover;
TexturePressed = t.Pressed;
TextureDisabled = t.Disabled;
TextureFocused = t.Focused;
}
}
}
public sealed override void _Ready()
{
TextureSet t = TextureSet;
if (t is null)
t = GlobalRegistry.TextureSetMap.GetValueOrDefault(TextureSetName, null);
if (t is not null)
{
TextureNormal = t.Normal;
TextureHover = t.Hover;
TexturePressed = t.Pressed;
TextureDisabled = t.Disabled;
TextureFocused = t.Focused;
}
__Ready();
base._Ready();
}
[ProxyMethod]
public virtual void __Ready()
{
}
}
#pragma warning restore IDE0130

View File

@@ -1,6 +1,6 @@
using Godot;
using Polonium.Attributes;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
@@ -20,9 +20,16 @@ public partial class CameraScene : Scene
get => Camera.Zoom.X;
set => Camera.Zoom = value * Vector2.One;
}
public override void _Ready()
[ProxyMethod]
public virtual void __Ready()
{
}
public sealed override void _Ready()
{
Camera = GetNode<Camera2D>("Camera");
__Ready();
base._Ready();
}
protected void ZoomIn() => Zoom = Mathf.Max(Zoom * (1 + ZoomRate), MaxZoom);

View File

@@ -1,6 +1,7 @@
#pragma warning disable IDE0130
using Godot;
using Polonium.Attributes;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
@@ -12,7 +13,7 @@ public partial class DissolveScene : Scene
private bool Finished { get; set; } = false;
private bool Terminated { get; set; } = false;
public override void _Process(double delta)
public sealed override void _Process(double delta)
{
if(Finished && !Terminated)
{
@@ -21,15 +22,15 @@ public partial class DissolveScene : Scene
return;
}
Process(delta);
__Process(delta);
base._Process(delta);
}
[ProxyMethod]
public virtual void Enter()
public virtual void __Enter()
{
}
[ProxyMethod]
public virtual void Process(double delta)
public virtual void __Process(double delta)
{
}
@@ -37,7 +38,8 @@ public partial class DissolveScene : Scene
{
Finished = false;
Terminated = false;
Enter();
__Enter();
base._EnterTree();
}
}
#pragma warning restore IDE0130

View File

@@ -1,7 +1,8 @@
#pragma warning disable IDE0130
using Godot;
using Polonium.Attributes;
using Polonium.Interfaces;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
@@ -19,28 +20,44 @@ public partial class RootScene : Scene
CurrentScene = scene;
}
public override void _Ready()
[ProxyMethod]
public virtual void __Enter()
{
GlobalRegistry.RootScene = this;
base._Ready();
}
[ProxyMethod]
public virtual void Enter()
public virtual void __Ready()
{
}
public override void _EnterTree()
[ProxyMethod]
public virtual void __Process(double delta)
{
Enter();
}
public sealed override void _EnterTree()
{
GlobalRegistry.Prepare();
GlobalRegistry.Start();
__Enter();
base._EnterTree();
}
public override void _Process(double delta)
public sealed override void _Process(double delta)
{
if(!GlobalRegistry.Paused)
foreach (ITimeConsumer tc in GlobalRegistry.TimeConsumers)
tc.Process(delta);
__Process(delta);
base._Process(delta);
}
public sealed override void _Ready()
{
GlobalRegistry.RootScene = this;
__Ready();
base._Ready();
}
}
#pragma warning restore IDE0130

View File

@@ -1,6 +1,7 @@
#pragma warning disable IDE0130
using Godot;
using Polonium.Attributes;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
@@ -9,3 +10,4 @@ public partial class Scene : Node2D
{
}
#pragma warning restore IDE0130

View File

@@ -0,0 +1,57 @@
#pragma warning disable IDE0130
#pragma warning disable CA1000
using Godot;
using System;
using System.Linq;
using System.Reflection;
using Polonium.Attributes;
using Polonium;
using Polonium.Interfaces;
using System.Collections.Generic;
// ReSharper disable once CheckNamespace
public static partial class GlobalRegistry
{
public static void Start()
{
PoloniumRegistry.Prepare();
Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> registerTypes = Utils.GetLoadableTypes(assembly);
registerTypes = registerTypes.Where(t => t.IsClass && t.GetCustomAttributes(typeof(AutoRegister), false).Any());
foreach (Type t in registerTypes)
{
MethodInfo registerMethod = t.GetMethod("Register", BindingFlags.Static | BindingFlags.Public);
if (registerMethod != null)
registerMethod.Invoke(null, null);
}
}
public static class Asset<T> where T : Node
{
private static readonly Queue<T> Pool = new();
// ReSharper disable once StaticMemberInGenericType
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public static PackedScene Scene { get; set; }
private static T Instance => Scene.Instantiate<T>();
public static T Get() => Pool.Count > 0 ? Pool.Dequeue() : Instance;
public static void Return(T obj)
{
if(Pool.Count < 10)
Pool.Enqueue(obj);
else
obj.QueueFree();
}
}
public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;
public static bool Paused { get; set; }
public static HashSet<ITimeConsumer> TimeConsumers { get; } = [];
public static void Prepare() => PoloniumRegistry.Prepare();
// ReSharper disable once PartialTypeWithSinglePart
public static partial class TextureSets
{
}
}
#pragma warning restore IDE0130
#pragma warning restore CA1000

View File

@@ -13,37 +13,26 @@
<DisableImplicitRestore>true</DisableImplicitRestore>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GodotSharp" Version="4.4.0-beta.3" />
<PackageReference Include="Polonium.Tasks" Version="0.1.1-x" />
</ItemGroup>
<Target Name="GenerateProxyNodes" BeforeTargets="BeforeBuild">
<Target Name="Prepare" BeforeTargets="BeforeBuild">
<RemoveDir Directories="$(ProjectDir)Package/embedded/polonium_templates" Condition="Exists('$(ProjectDir)Package/polonium_templates')"/>
<MakeDir Directories="$(ProjectDir)Package/embedded/polonium_templates"/>
<GenerateProxyNodesTask
SourceDirectory="$(ProjectDir)GlobalClasses"
TemplateDirectory="$(ProjectDir)polonium_templates"
SourceDirectory="$(ProjectDir)Package/embedded/GlobalClasses"
TemplateDirectory="$(ProjectDir)Package/embedded/polonium_templates"
AttributeName="ProxyNode"
/>
</Target>
<ItemGroup>
<None Include="NuGet.config" />
<None
Include="Package/build/$(AssemblyName).targets"
Pack="true"
PackagePath="build"
/>
<None
Include="GlobalClasses/**/*.cs"
Pack="true"
PackagePath="build\GlobalClasses"
/>
<None
Include="polonium_templates/**/*.cs"
Pack="true"
PackagePath="build/polonium_templates"
/>
<None Include="Package/build/$(AssemblyName).targets" Pack="true" PackagePath="build" />
<None Include="Package/build/$(AssemblyName).props" Pack="true" PackagePath="build" />
<None Include="Package/embedded/**/*.*" Pack="true" PackagePath="build/embedded" />
<None Include="publish" />
<Compile Remove="polonium_templates/**/*.*" />
<Compile Remove="GlobalClasses/**/*.cs" />
<Folder Include="polonium_templates" />
<None Include="build" />
<Compile Remove="Package/**/*.*"/>
</ItemGroup>
<Target Name="CleanPreviousPackages" BeforeTargets="Build">
@@ -61,14 +50,13 @@
<ItemGroup>
<NuGetPackages Include="$(OutputPath)*.nupkg" />
</ItemGroup>
<Message Text="Printing pkgs--------------------" Importance="high"/>
<Message Text="Pkgs: @(NuGetPackages)" Importance="high"/>
<Copy SourceFiles="@(NuGetPackages)" DestinationFolder="/NuGetFeed"/>
<Message Text="Printing pkgs--------------------" Importance="high" />
<Message Text="Pkgs: @(NuGetPackages)" Importance="high" />
<Copy SourceFiles="@(NuGetPackages)" DestinationFolder="/NuGetFeed" />
</Target>
<Target Name="RestoreNoCache" BeforeTargets="Build">
<Message Text="Restoring packages with no chache" Importance="high"/>
<Exec Command="dotnet restore --no-cache"/>
<Message Text="Restoring packages with no chache" Importance="high" />
<Exec Command="dotnet restore --no-cache" />
</Target>
</Project>

View File

@@ -2,12 +2,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polonium", "Polonium.csproj", "{5BA39DF8-7098-4348-A670-B3F34269F48F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polonium.Generators", "..\Polonium.Generators\Polonium.Generators.csproj", "{41B784D2-841C-44D6-90F3-9219BC264B19}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polonium.Generators.Test", "..\Polonium.Generators.Test\Polonium.Generators.Test.csproj", "{3BADB215-214B-41C1-B94E-89AF4A972F30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polonium.Tasks", "..\Polonium.Tasks\Polonium.Tasks.csproj", "{5F0664A8-563F-408A-9EB6-05B2F25F5DC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -16,28 +10,12 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5BA39DF8-7098-4348-A670-B3F34269F48F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5BA39DF8-7098-4348-A670-B3F34269F48F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BA39DF8-7098-4348-A670-B3F34269F48F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BA39DF8-7098-4348-A670-B3F34269F48F}.Release|Any CPU.Build.0 = Release|Any CPU
{41B784D2-841C-44D6-90F3-9219BC264B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41B784D2-841C-44D6-90F3-9219BC264B19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41B784D2-841C-44D6-90F3-9219BC264B19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41B784D2-841C-44D6-90F3-9219BC264B19}.Release|Any CPU.Build.0 = Release|Any CPU
{3BADB215-214B-41C1-B94E-89AF4A972F30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BADB215-214B-41C1-B94E-89AF4A972F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BADB215-214B-41C1-B94E-89AF4A972F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BADB215-214B-41C1-B94E-89AF4A972F30}.Release|Any CPU.Build.0 = Release|Any CPU
{1B24F8AC-B185-48D0-835B-59857AF907E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B24F8AC-B185-48D0-835B-59857AF907E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B24F8AC-B185-48D0-835B-59857AF907E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B24F8AC-B185-48D0-835B-59857AF907E5}.Release|Any CPU.Build.0 = Release|Any CPU
{D2BA966D-140F-4C04-8EE1-88FFE5FEB67C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2BA966D-140F-4C04-8EE1-88FFE5FEB67C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2BA966D-140F-4C04-8EE1-88FFE5FEB67C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2BA966D-140F-4C04-8EE1-88FFE5FEB67C}.Release|Any CPU.Build.0 = Release|Any CPU
{25595747-BE10-4F36-BDE0-9400576EA921}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25595747-BE10-4F36-BDE0-9400576EA921}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25595747-BE10-4F36-BDE0-9400576EA921}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25595747-BE10-4F36-BDE0-9400576EA921}.Release|Any CPU.Build.0 = Release|Any CPU
{5F0664A8-563F-408A-9EB6-05B2F25F5DC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F0664A8-563F-408A-9EB6-05B2F25F5DC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F0664A8-563F-408A-9EB6-05B2F25F5DC5}.Release|Any CPU.ActiveCfg = Release|Any CPU

17
build Executable file
View File

@@ -0,0 +1,17 @@
#! /bin/bash
rm -rf ~/.nuget/packages/polonium*
cd ../Polonium.Tasks
dotnet clean Polonium.Tasks.csproj
dotnet restore Polonium.Tasks.csproj
dotnet build Polonium.Tasks.csproj
cd ../Polonium.Generators
dotnet clean Polonium.Generators.csproj
dotnet restore Polonium.Generators.csproj
dotnet build Polonium.Generators.csproj
cd ../Polonium
rm -rf ~/.nuget/packages/polonium*
dotnet clean Polonium.csproj
dotnet restore Polonium.csproj
dotnet build Polonium.csproj

View File

@@ -0,0 +1,6 @@
namespace Polonium.Attributes;
[AttributeUsage(AttributeTargets.Property)]
public class ProxyProperty : Attribute
{
}

View File

@@ -0,0 +1,33 @@
using Godot;
using FileAccess = Godot.FileAccess;
namespace Polonium.DataStructures;
public class TextureSet
{
public TextureSet(string path)
{
Normal = LoadTexture($"{path}/Normal");
Pressed = LoadTexture($"{path}/Pressed");
Disabled = LoadTexture($"{path}/Disabled");
Hover = LoadTexture($"{path}/Hover");
Focused = LoadTexture($"{path}/Focused");
}
public Texture2D Normal { get; init; }
public Texture2D Pressed { get; init; }
public Texture2D Hover { get; init; }
public Texture2D Disabled { get; init; }
public Texture2D Focused { get; init; }
private static Texture2D LoadTexture(string path)
{
Texture2D res = new();
if(FileAccess.FileExists($"{path}.png"))
res = ResourceLoader.Load<Texture2D>($"{path}.png");
else if(DirAccess.DirExistsAbsolute($"{path}.at_dir"))
res = Utils.LoadAnimatedTextureFromDirectory($"{path}.at_dir");
return res;
}
}

View File

@@ -0,0 +1,8 @@
namespace Polonium.Interfaces;
public interface IGlobalRegistry
{
public bool Paused { get; set; }
public void Start();
public void Prepare();
}

View File

@@ -1,4 +1,7 @@
#pragma warning disable CS0618
#pragma warning disable CS0168
using System.Reflection;
using Godot;
namespace Polonium;
@@ -15,4 +18,32 @@ public static class Utils
return e.Types.Where(t => t != null);
}
}
private static Dictionary<StringName, AnimatedTexture> AnimatedTextureCache { get; } = new();
public static AnimatedTexture LoadAnimatedTextureFromDirectory(StringName path)
{
try
{
if (!AnimatedTextureCache.ContainsKey(path))
{
AnimatedTextureCache[path] = new AnimatedTexture();
int f = 0;
foreach (string fname in DirAccess.GetFilesAt(path))
{
if (!fname.EndsWith(".png"))
continue;
AnimatedTextureCache[path].SetFrameTexture(f, ResourceLoader.Load<Texture2D>(fname));
}
}
return AnimatedTextureCache[path];
}
catch (Exception e)
{
return null;
}
}
}
#pragma warning restore CS0618
#pragma warning restore CS0168