Skip to content

Commit

Permalink
chore(release): 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Apr 25, 2022
0 parents commit 6d88e77
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0 [2022-04-25]

- Initial release
7 changes: 7 additions & 0 deletions CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Core.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Core/LayoutAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using JetBrains.Annotations;

namespace UIComponents.Core
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[BaseTypeRequired(typeof(UIComponent))]
public class LayoutAttribute : PathAttribute
{
public LayoutAttribute(string path)
{
Path = path;
}
}
}
3 changes: 3 additions & 0 deletions Core/LayoutAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Core/PathAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace UIComponents.Core
{
public abstract class PathAttribute : Attribute
{
public string Path { get; protected set; }

public string RelativeTo { get; set; }

public string GetAssetPath()
{
if (Path == null)
Path = string.Empty;

if (!string.IsNullOrEmpty(RelativeTo))
return string.Join("/", RelativeTo, Path);

return Path;
}
}
}
3 changes: 3 additions & 0 deletions Core/PathAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Core/StylesheetAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using JetBrains.Annotations;

namespace UIComponents.Core
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
[BaseTypeRequired(typeof(UIComponent))]
public class StylesheetAttribute : PathAttribute
{
public StylesheetAttribute(string path)
{
Path = path;
}
}
}
3 changes: 3 additions & 0 deletions Core/StylesheetAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions Core/UIComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace UIComponents.Core
{
public abstract class UIComponent : VisualElement
{
private readonly Type _componentType;

private readonly LayoutAttribute _layoutAttribute;

private readonly StylesheetAttribute[] _stylesheetAttributes;

protected UIComponent()
{
_componentType = GetType();
_layoutAttribute = GetLayoutAttribute();
_stylesheetAttributes = GetStylesheetAttributes();
LoadLayout();
LoadStyles();
}

[CanBeNull]
private LayoutAttribute GetLayoutAttribute()
{
var layoutAttributes =
(LayoutAttribute[]) _componentType.GetCustomAttributes(typeof(LayoutAttribute), true);

if (layoutAttributes.Length == 0)
return null;

return layoutAttributes[0];
}

private StylesheetAttribute[] GetStylesheetAttributes()
{
return (StylesheetAttribute[])
_componentType.GetCustomAttributes(typeof(StylesheetAttribute), true);
}

[CanBeNull]
protected virtual VisualTreeAsset GetLayout()
{
if (_layoutAttribute == null)
return null;

return AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(_layoutAttribute.GetAssetPath());
}

protected virtual StyleSheet[] GetStyleSheets()
{
var loadedStyleSheets = new StyleSheet[_stylesheetAttributes.Length];

for (var i = 0; i < _stylesheetAttributes.Length; i++)
{
var assetPath = _stylesheetAttributes[i].GetAssetPath();
loadedStyleSheets[i] = AssetDatabase.LoadAssetAtPath<StyleSheet>(assetPath);
}

return loadedStyleSheets;
}

private void LoadLayout()
{
var layoutAsset = GetLayout();
if (layoutAsset)
layoutAsset.CloneTree(this);
else
Debug.LogWarningFormat("Could not find layout for {0}", _componentType.Name);
}

private void LoadStyles()
{
var loadedStyleSheets = GetStyleSheets();

if (loadedStyleSheets.Length == 0)
{
Debug.LogWarningFormat("Could not find styles for {0}", _componentType.Name);
return;
}

foreach (var sheet in loadedStyleSheets)
styleSheets.Add(sheet);
}
}
}
3 changes: 3 additions & 0 deletions Core/UIComponent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Core/UIComponents.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "UIComponents",
"rootNamespace": "UIComponents",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Core/UIComponents.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Joni Savolainen <joni@savolainen.io>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# UIComponents
#### A microframework for creating reusable components for Unity's UIElements.

```c#
[Layout("Assets/Editor/Components/MyComponent/MyComponent.uxml")]
[Stylesheet("Assets/Editor/Components/MyComponent/MyComponent.style.uss")]
[Stylesheet("Assets/Editor/Common.uss")]
class MyComponent : UIComponent
{
// The layout and stylesheets are loaded in the inherited
// constructor.
}
```

```c#
var container = new VisualElement();
container.Add(new MyComponent());
```

If you have a common asset path to your files, you can use `RelativeTo`:

```c#
[Layout("MyComponent/MyComponent.uxml", RelativeTo = AssetPaths.Components)]
[Stylesheet("MyComponent/MyComponent.style.uss", RelativeTo = AssetPaths.Components)]
[Stylesheet("Assets/Editor/Common.uss")]
class MyComponent : UIComponent {}
```

```c#
public static class AssetPaths
{
public const string Components = "Assets/Editor/Components";
}
```

## API

### `UIComponent`

Automatically loads the specified layout and stylesheets in the constructor.

Offers two protected, virtual methods which can be overridden:

#### `VisualTreeAsset GetLayout()`

Returns the VisualTreeAsset used for the component. Called in `UIComponent`'s constructor.

#### `StyleSheet[] GetStyleSheets()`

Returns the StyleSheets used in the component. Called in `UIComponent`'s constructor.

### `LayoutAttribute`

Used to define the asset path for the `UIComponent`'s `VisualTreeAsset`.
The `RelativeTo` property can be used to prepend to the file path.

Only a single `LayoutAttribute` can be used.

```c#
[Layout("Assets/UIComponents/ComponentA.uxml")]
class ComponentA : UIComponent {}

[Layout("ComponentB.uxml", RelativeTo = AssetPaths.Components)]
class ComponentB : UIComponent {}

public static class AssetPaths
{
public const string Components = "Assets/UIComponents";
}
```

### `StylesheetAttribute`

Used to define the asset path for a `StyleSheet` used by the `UIComponent`.
The `RelativeTo` property can be used to prepend to the file path.

Multiple `StylesheetAttribute`s can be used.

```c#
[Stylesheet("Assets/UIComponents/ComponentA.style.uss")]
[Stylesheet("Assets/Styles/MyStyle.uss")]
class ComponentA : UIComponent {}

[Stylesheet("ComponentB.style.uss", RelativeTo = AssetPaths.Components)]
[Stylesheet("MyStyle.uss", RelativeTo = AssetPaths.Styles)]
class ComponentB : UIComponent {}

public static class AssetPaths
{
public const string Components = "Assets/UIComponents";
public const string Styles = "Assets/Styles";
}
```
3 changes: 3 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "io.savolainen.uicomponents",
"displayName": "UIComponents",
"version": "0.1.0",
"description": "A microframework for creating reusable components with UIElements.",
"unity": "2019.3",
"author": {
"name": "Joni Savolainen",
"email": "joni@savolainen.io",
"url": "https://savolainen.io"
},
"keywords": ["ui", "uitoolkit", "uielements", "library"],
"license": "MIT"
}
7 changes: 7 additions & 0 deletions package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d88e77

Please sign in to comment.