mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 14:38:34 +01:00
Compare commits
No commits in common. "30160e821024ddea913442b9459c7c15dd2d7b3f" and "9b090c954f0b26b6270ecf64c59907b047ba5d18" have entirely different histories.
30160e8210
...
9b090c954f
133 changed files with 2210 additions and 24492 deletions
|
@ -3,7 +3,7 @@
|
||||||
"isRoot": true,
|
"isRoot": true,
|
||||||
"tools": {
|
"tools": {
|
||||||
"cake.tool": {
|
"cake.tool": {
|
||||||
"version": "3.0.0",
|
"version": "1.3.0",
|
||||||
"commands": [
|
"commands": [
|
||||||
"dotnet-cake"
|
"dotnet-cake"
|
||||||
]
|
]
|
||||||
|
|
|
@ -87,9 +87,8 @@ resharper_indent_nested_for_stmt = true
|
||||||
resharper_indent_nested_lock_stmt = true
|
resharper_indent_nested_lock_stmt = true
|
||||||
resharper_indent_nested_usings_stmt = true
|
resharper_indent_nested_usings_stmt = true
|
||||||
resharper_indent_nested_while_stmt = true
|
resharper_indent_nested_while_stmt = true
|
||||||
resharper_indent_preprocessor_if = no_indent
|
resharper_indent_preprocessor_if = usual_indent
|
||||||
resharper_indent_preprocessor_other = no_indent
|
resharper_indent_preprocessor_other = usual_indent
|
||||||
resharper_indent_preprocessor_region = no_indent
|
|
||||||
resharper_keep_existing_declaration_parens_arrangement = false
|
resharper_keep_existing_declaration_parens_arrangement = false
|
||||||
resharper_keep_existing_embedded_arrangement = false
|
resharper_keep_existing_embedded_arrangement = false
|
||||||
resharper_keep_existing_expr_member_arrangement = false
|
resharper_keep_existing_expr_member_arrangement = false
|
||||||
|
@ -112,7 +111,3 @@ resharper_wrap_object_and_collection_initializer_style = wrap_if_long
|
||||||
resharper_xmldoc_attribute_indent = align_by_first_attribute
|
resharper_xmldoc_attribute_indent = align_by_first_attribute
|
||||||
resharper_xmldoc_attribute_style = on_single_line
|
resharper_xmldoc_attribute_style = on_single_line
|
||||||
resharper_xmldoc_pi_attribute_style = on_single_line
|
resharper_xmldoc_pi_attribute_style = on_single_line
|
||||||
|
|
||||||
[*.md]
|
|
||||||
trim_trailing_whitespace = false
|
|
||||||
indent_size = 2
|
|
||||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,4 +4,4 @@ obj
|
||||||
packages
|
packages
|
||||||
*.user
|
*.user
|
||||||
tools
|
tools
|
||||||
TestResults*
|
TestResults
|
127
CHANGELOG.md
127
CHANGELOG.md
|
@ -1,135 +1,14 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
MLEM tries to adhere to [semantic versioning](https://semver.org/). Potentially breaking changes are written in **bold**.
|
MLEM tries to adhere to [semantic versioning](https://semver.org/). Breaking changes are written in **bold**.
|
||||||
|
|
||||||
Jump to version:
|
Jump to version:
|
||||||
- [6.1.0](#610)
|
|
||||||
- [6.0.0](#600)
|
- [6.0.0](#600)
|
||||||
- [5.3.0](#530)
|
- [5.3.0](#530)
|
||||||
- [5.2.0](#520)
|
- [5.2.0](#520)
|
||||||
- [5.1.0](#510)
|
- [5.1.0](#510)
|
||||||
- [5.0.0](#500)
|
- [5.0.0](#500)
|
||||||
|
|
||||||
## 6.1.0
|
|
||||||
|
|
||||||
### MLEM
|
|
||||||
Additions
|
|
||||||
- Added TokenizedString.Realign
|
|
||||||
- Added GetFlags and GetUniqueFlags to EnumHelper
|
|
||||||
- Added GetDownTime, GetUpTime, GetTimeSincePress, WasModifierDown and WasDown to Keybind and Combination
|
|
||||||
- Added the ability for UniformTextureAtlases to have padding for each region
|
|
||||||
- Added UniformTextureAtlas methods ToList and ToDictionary
|
|
||||||
- Added SingleRandom and SeedSource
|
|
||||||
- Added TokenizedString.GetArea
|
|
||||||
- Added InputHandler.WasPressedForLess and related methods as well as InputHandler.IsPressedIgnoreRepeats
|
|
||||||
- Added RandomExtensions.NextSingle with minimum and maximum values
|
|
||||||
- Added subscript and superscript formatting codes
|
|
||||||
- **Added the ability to find paths to one of multiple goals using AStar**
|
|
||||||
|
|
||||||
Improvements
|
|
||||||
- Improved EnumHelper.GetValues signature to return an array
|
|
||||||
- Allow using external gesture handling alongside InputHandler through ExternalGestureHandling
|
|
||||||
- Discard old data when updating a StaticSpriteBatch
|
|
||||||
- Multi-target net452, making MLEM compatible with MonoGame for consoles
|
|
||||||
- Allow retrieving the cost of a calculated path when using AStar
|
|
||||||
- Added trimming and AOT annotations and made MLEM trimmable
|
|
||||||
- Allow specifying percentage-based padding for a NinePatch
|
|
||||||
- Improved the way InputHandler down time calculation works
|
|
||||||
- Allow explicitly specifying each region for extended auto tiles
|
|
||||||
- Added a generic version of IGenericDataHolder.SetData
|
|
||||||
- Allow formatting codes to have an arbitrary custom width
|
|
||||||
- Allow initializing text formatters without default codes and macros
|
|
||||||
- **Drastically improved StaticSpriteBatch batching performance**
|
|
||||||
- **Made GenericFont and TokenizedString support UTF-32 characters like emoji**
|
|
||||||
|
|
||||||
Fixes
|
|
||||||
- Fixed TokenizedString handling trailing spaces incorrectly in the last line of non-left aligned text
|
|
||||||
- Fixed some TokenizedString tokens starting with a line break not being split correctly
|
|
||||||
- Fixed InputHandler maintaining old input states when input types are toggled off
|
|
||||||
- Fixed Combination.IsModifierDown querying one of its modifiers instead of all of them
|
|
||||||
|
|
||||||
Removals
|
|
||||||
- Removed DataContract attribute from GenericDataHolder
|
|
||||||
- Marked EnumHelper as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
|
|
||||||
- Marked Code.GetReplacementString as obsolete
|
|
||||||
- Marked TokenizedString.Measure as obsolete in favor of GetArea
|
|
||||||
- Marked non-GenericInput versions of IsDown, IsUp, IsPressed and related methods as obsolete in favor of GenericInput ones
|
|
||||||
|
|
||||||
### MLEM.Ui
|
|
||||||
Additions
|
|
||||||
- Added some extension methods for querying Anchor types
|
|
||||||
- Added Element.AutoSizeAddedAbsolute to allow for more granular control of auto-sizing
|
|
||||||
- Added Element.OnAddedToUi and Element.OnRemovedFromUi
|
|
||||||
- Added ScrollBar.MouseDragScrolling
|
|
||||||
- Added Panel.ScrollToElement
|
|
||||||
- Added ElementHelper.MakeGrid
|
|
||||||
- Added Button.AutoDisableCondition
|
|
||||||
|
|
||||||
Improvements
|
|
||||||
- Allow elements to auto-adjust their size even when their children are aligned oddly
|
|
||||||
- Close other dropdowns when opening a dropdown
|
|
||||||
- Generified UiMarkdownParser by adding abstract UiParser
|
|
||||||
- Multi-target net452, making MLEM compatible with MonoGame for consoles
|
|
||||||
- Added trimming and AOT annotations and made MLEM.Ui trimmable
|
|
||||||
- Ensure paragraphs display up-to-date versions of their text callbacks
|
|
||||||
- Set cornflower blue as the default link color
|
|
||||||
- Added TextField.OnCopyPasteException to allow handling exceptions thrown by TextCopy
|
|
||||||
- Avoid paragraphs splitting or truncating their text unnecessarily
|
|
||||||
- Automatically mark elements dirty when various member values are changed
|
|
||||||
- Allow initializing a ui system's text formatter without default codes and macros
|
|
||||||
|
|
||||||
Fixes
|
|
||||||
- Fixed parents of elements that prevent spill not being notified properly
|
|
||||||
- Fixed paragraphs sometimes not updating their position properly when hidden because they're empty
|
|
||||||
- Fixed panels sometimes not drawing children that came into view when their positions changed unexpectedly
|
|
||||||
- Fixed UiMarkdownParser not parsing formatting in headings and blockquotes
|
|
||||||
- Fixed Element.OnChildAdded and Element.OnChildRemoved being called for grandchildren when a child is added
|
|
||||||
- Fixed an exception when trying to force-update the area of an element without a ui system
|
|
||||||
- Fixed the scroll bar of an empty panel being positioned incorrectly
|
|
||||||
- Fixed UiControls maintaining old input states when input types are toggled off
|
|
||||||
- Fixed an occasional deadlock when a game is disposed with a scrolling Panel present
|
|
||||||
- Fixed UiStyle.LinkColor not being applied to the ui system when changed
|
|
||||||
|
|
||||||
Removals
|
|
||||||
- Marked Element.OnDisposed as obsolete in favor of the more predictable OnRemovedFromUi
|
|
||||||
|
|
||||||
### MLEM.Data
|
|
||||||
Additions
|
|
||||||
- Added data, from, and copy instructions to DataTextureAtlas
|
|
||||||
- Added the ability to add additional regions to a RuntimeTexturePacker after packing
|
|
||||||
- Added GetFlags, GetUniqueFlags and IsDefined to DynamicEnum
|
|
||||||
- Added DataTextureAtlas.ToDictionary
|
|
||||||
|
|
||||||
Improvements
|
|
||||||
- Allow data texture atlas pivots and offsets to be negative
|
|
||||||
- Made RuntimeTexturePacker restore texture region name and pivot when packing
|
|
||||||
- Multi-target net452, making MLEM compatible with MonoGame for consoles
|
|
||||||
- Added trimming and AOT annotations and made MLEM.Data trimmable
|
|
||||||
- Store a RuntimeTexturePacker packed texture region's source region
|
|
||||||
- Use JSON.NET attributes in favor of DataContract and DataMember
|
|
||||||
- Made JsonTypeSafeWrapper.Of generic to potentially avoid reflective instantiation
|
|
||||||
|
|
||||||
Fixes
|
|
||||||
- Fixed data texture atlases not allowing most characters in their region names
|
|
||||||
|
|
||||||
Removals
|
|
||||||
- Marked DynamicEnum as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
|
|
||||||
|
|
||||||
## MLEM.Extended
|
|
||||||
Additions
|
|
||||||
- Added Range extension methods GetPercentage and FromPercentage
|
|
||||||
|
|
||||||
Improvements
|
|
||||||
- Multi-target net452, making MLEM compatible with MonoGame for consoles
|
|
||||||
- Added trimming and AOT annotations and made MLEM.Extended trimmable
|
|
||||||
- **Made GenericBitmapFont and GenericStashFont support UTF-32 characters like emoji**
|
|
||||||
|
|
||||||
## MLEM.Startup
|
|
||||||
Improvements
|
|
||||||
- Multi-target net452, making MLEM compatible with MonoGame for consoles
|
|
||||||
- Added trimming and AOT annotations and made MLEM.Startup trimmable
|
|
||||||
|
|
||||||
## 6.0.0
|
## 6.0.0
|
||||||
|
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
- Added consuming variants of IsPressed methods to InputHandler and Keybind
|
- Added consuming variants of IsPressed methods to InputHandler and Keybind
|
||||||
|
@ -216,7 +95,6 @@ Improvements
|
||||||
- Updated to MonoGame 3.8.1
|
- Updated to MonoGame 3.8.1
|
||||||
|
|
||||||
## 5.3.0
|
## 5.3.0
|
||||||
|
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
- Added StringBuilder overloads to GenericFont
|
- Added StringBuilder overloads to GenericFont
|
||||||
|
@ -301,7 +179,6 @@ Removals
|
||||||
- Marked CopyExtensions as obsolete
|
- Marked CopyExtensions as obsolete
|
||||||
|
|
||||||
## 5.2.0
|
## 5.2.0
|
||||||
|
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
- Added a strikethrough formatting code
|
- Added a strikethrough formatting code
|
||||||
|
@ -376,7 +253,6 @@ Additions
|
||||||
- Added PreDraw and PreUpdate events and coroutine events
|
- Added PreDraw and PreUpdate events and coroutine events
|
||||||
|
|
||||||
## 5.1.0
|
## 5.1.0
|
||||||
|
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
- Added RotateBy to Direction2Helper
|
- Added RotateBy to Direction2Helper
|
||||||
|
@ -427,7 +303,6 @@ Fixes
|
||||||
- Fixed DynamicEnum AddFlag going into an infinite loop
|
- Fixed DynamicEnum AddFlag going into an infinite loop
|
||||||
|
|
||||||
## 5.0.0
|
## 5.0.0
|
||||||
|
|
||||||
### MLEM
|
### MLEM
|
||||||
Additions
|
Additions
|
||||||
- Added some Collection extensions, namely for dealing with combinations
|
- Added some Collection extensions, namely for dealing with combinations
|
||||||
|
|
|
@ -2,8 +2,6 @@ using Android.Content;
|
||||||
using Android.Content.PM;
|
using Android.Content.PM;
|
||||||
using Android.OS;
|
using Android.OS;
|
||||||
using Android.Views;
|
using Android.Views;
|
||||||
using Microsoft.Xna.Framework;
|
|
||||||
using Microsoft.Xna.Framework.Input;
|
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using Uri = Android.Net.Uri;
|
using Uri = Android.Net.Uri;
|
||||||
|
|
|
@ -7,12 +7,11 @@
|
||||||
<ApplicationVersion>1</ApplicationVersion>
|
<ApplicationVersion>1</ApplicationVersion>
|
||||||
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
|
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
|
||||||
<ImplicitUsings>true</ImplicitUsings>
|
<ImplicitUsings>true</ImplicitUsings>
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.263" />
|
||||||
<PackageReference Include="MonoGame.Framework.Android" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Framework.Android" Version="3.8.1.263" />
|
||||||
|
|
||||||
<ProjectReference Include="..\Demos\Demos.csproj" />
|
<ProjectReference Include="..\Demos\Demos.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<ApplicationIcon>Icon.ico</ApplicationIcon>
|
<ApplicationIcon>Icon.ico</ApplicationIcon>
|
||||||
<AssemblyName>MLEM Desktop Demos</AssemblyName>
|
<AssemblyName>MLEM Desktop Demos</AssemblyName>
|
||||||
<RootNamespace>Demos.DesktopGL</RootNamespace>
|
<RootNamespace>Demos.DesktopGL</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
<!-- We still use the MG content builder for ease of compatibility between the MG and FNA demo projects -->
|
<!-- We still use the MG content builder for ease of compatibility between the MG and FNA demo projects -->
|
||||||
<MonoGamePlatform>DesktopGL</MonoGamePlatform>
|
<MonoGamePlatform>DesktopGL</MonoGamePlatform>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -20,7 +19,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.0.1641" />
|
||||||
<ProjectReference Include="..\FNA\FNA.Core.csproj" />
|
<ProjectReference Include="..\FNA\FNA.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -5,15 +5,14 @@
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<ApplicationIcon>Icon.ico</ApplicationIcon>
|
<ApplicationIcon>Icon.ico</ApplicationIcon>
|
||||||
<AssemblyName>MLEM Desktop Demos</AssemblyName>
|
<AssemblyName>MLEM Desktop Demos</AssemblyName>
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.263" />
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.263" />
|
||||||
|
|
||||||
<ProjectReference Include="..\Demos\Demos.csproj" />
|
<ProjectReference Include="..\Demos\Demos.csproj" />
|
||||||
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
|
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
using MLEM.Misc;
|
using System;
|
||||||
#if !FNA
|
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
#else
|
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
#endif
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace Demos.DesktopGL;
|
|
||||||
|
|
||||||
|
namespace Demos.DesktopGL {
|
||||||
public static class Program {
|
public static class Program {
|
||||||
|
|
||||||
public static void Main() {
|
public static void Main() {
|
||||||
|
@ -20,3 +17,4 @@ public static class Program {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<RootNamespace>Demos</RootNamespace>
|
<RootNamespace>Demos</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -12,7 +11,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.263">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -43,7 +43,7 @@ namespace Demos {
|
||||||
this.GraphicsDevice.Clear(Color.CornflowerBlue);
|
this.GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||||
base.DoDraw(time);
|
base.DoDraw(time);
|
||||||
|
|
||||||
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
|
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
|
||||||
var view = this.GraphicsDevice.Viewport;
|
var view = this.GraphicsDevice.Viewport;
|
||||||
|
|
||||||
// graph the easing function
|
// graph the easing function
|
||||||
|
|
|
@ -9,6 +9,9 @@ using MLEM.Textures;
|
||||||
using MLEM.Ui;
|
using MLEM.Ui;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
|
#if !FNA
|
||||||
|
using MonoGame.Framework.Utilities;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Demos {
|
namespace Demos {
|
||||||
public class GameImpl : MlemGame {
|
public class GameImpl : MlemGame {
|
||||||
|
@ -91,8 +94,9 @@ namespace Demos {
|
||||||
TextScale = 0.1F,
|
TextScale = 0.1F,
|
||||||
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
|
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
|
||||||
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4),
|
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4),
|
||||||
ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 0.25F, paddingPercent: true),
|
ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 1, 1, 2, 2),
|
||||||
ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 0.25F, paddingPercent: true)
|
ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2),
|
||||||
|
LinkColor = Color.CornflowerBlue
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
|
@ -52,7 +51,7 @@ namespace Demos {
|
||||||
|
|
||||||
// Now find a path from the top left to the bottom right corner and store it in a variable
|
// Now find a path from the top left to the bottom right corner and store it in a variable
|
||||||
// If no path can be found after the maximum amount of tries (10000 by default), the pathfinder will abort and return no path (null)
|
// If no path can be found after the maximum amount of tries (10000 by default), the pathfinder will abort and return no path (null)
|
||||||
var foundPath = await Task.Run(() => this.pathfinder.FindPath(Point.Zero, new Point(49, 49)));
|
var foundPath = await this.pathfinder.FindPathAsync(Point.Zero, new Point(49, 49));
|
||||||
this.path = foundPath != null ? foundPath.ToList() : null;
|
this.path = foundPath != null ? foundPath.ToList() : null;
|
||||||
|
|
||||||
// print out some info
|
// print out some info
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input;
|
|
||||||
using MLEM.Extensions;
|
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Formatting;
|
using MLEM.Formatting;
|
||||||
using MLEM.Formatting.Codes;
|
using MLEM.Formatting.Codes;
|
||||||
using MLEM.Misc;
|
|
||||||
using MLEM.Startup;
|
using MLEM.Startup;
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
|
|
||||||
|
@ -18,7 +15,7 @@ namespace Demos {
|
||||||
"You can write in <b>bold</i>, <i>italics</i>, <u>with an underline</u>, <st>strikethrough</st>, with a <s #000000 4>drop shadow</s> whose <s #ff0000 4>color</s> and <s #000000 10>offset</s> you can modify in each application of the code, or with various types of <b>combined <c Pink>formatting</c> codes</b>.\n\n" +
|
"You can write in <b>bold</i>, <i>italics</i>, <u>with an underline</u>, <st>strikethrough</st>, with a <s #000000 4>drop shadow</s> whose <s #ff0000 4>color</s> and <s #000000 10>offset</s> you can modify in each application of the code, or with various types of <b>combined <c Pink>formatting</c> codes</b>.\n\n" +
|
||||||
"You can apply <c CornflowerBlue>custom</c> <c Yellow>colors</c> to text, including all default <c Orange>MonoGame colors</c> and <c #aabb00>inline custom colors</c>.\n\n" +
|
"You can apply <c CornflowerBlue>custom</c> <c Yellow>colors</c> to text, including all default <c Orange>MonoGame colors</c> and <c #aabb00>inline custom colors</c>.\n\n" +
|
||||||
"You can also use animations like <a wobbly>a wobbly one</a>, as well as create custom ones using the <a wobbly>Code class</a>.\n\n" +
|
"You can also use animations like <a wobbly>a wobbly one</a>, as well as create custom ones using the <a wobbly>Code class</a>.\n\n" +
|
||||||
"You can also display <i grass> icons in your text, and use super<sup>script</sup> or sub<sub>script</sub> formatting!\n\n" +
|
"You can also display <i grass> icons in your text!\n\n" +
|
||||||
"Additionally, the text formatter has various methods for interacting with the text, like custom behaviors when hovering over certain parts, and more.";
|
"Additionally, the text formatter has various methods for interacting with the text, like custom behaviors when hovering over certain parts, and more.";
|
||||||
private const float Scale = 0.5F;
|
private const float Scale = 0.5F;
|
||||||
private const float Width = 0.9F;
|
private const float Width = 0.9F;
|
||||||
|
@ -26,7 +23,6 @@ namespace Demos {
|
||||||
private TextFormatter formatter;
|
private TextFormatter formatter;
|
||||||
private TokenizedString tokenizedText;
|
private TokenizedString tokenizedText;
|
||||||
private GenericFont font;
|
private GenericFont font;
|
||||||
private bool drawBounds;
|
|
||||||
|
|
||||||
public TextFormattingDemo(MlemGame game) : base(game) {}
|
public TextFormattingDemo(MlemGame game) : base(game) {}
|
||||||
|
|
||||||
|
@ -54,24 +50,12 @@ namespace Demos {
|
||||||
|
|
||||||
public override void DoDraw(GameTime time) {
|
public override void DoDraw(GameTime time) {
|
||||||
this.GraphicsDevice.Clear(Color.DarkSlateGray);
|
this.GraphicsDevice.Clear(Color.DarkSlateGray);
|
||||||
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
|
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
|
||||||
|
|
||||||
// we draw the tokenized text in the center of the screen
|
// we draw the tokenized text in the center of the screen
|
||||||
// since the text is already center-aligned, we only need to align it on the y axis here
|
// since the text is already center-aligned, we only need to align it on the y axis here
|
||||||
var size = this.tokenizedText.GetArea(Vector2.Zero, TextFormattingDemo.Scale).Size;
|
var size = this.tokenizedText.Measure(this.font) * TextFormattingDemo.Scale;
|
||||||
var pos = new Vector2(this.GraphicsDevice.Viewport.Width / 2, (this.GraphicsDevice.Viewport.Height - size.Y) / 2);
|
var pos = new Vector2(this.GraphicsDevice.Viewport.Width / 2, (this.GraphicsDevice.Viewport.Height - size.Y) / 2);
|
||||||
|
|
||||||
// draw bounds, which can be toggled with B in this demo
|
|
||||||
if (this.drawBounds) {
|
|
||||||
var blank = this.SpriteBatch.GetBlankTexture();
|
|
||||||
this.SpriteBatch.Draw(blank, new RectangleF(pos - new Vector2(size.X / 2, 0), size), Color.Red * 0.25F);
|
|
||||||
foreach (var token in this.tokenizedText.Tokens) {
|
|
||||||
foreach (var area in token.GetArea(pos, TextFormattingDemo.Scale))
|
|
||||||
this.SpriteBatch.Draw(blank, area, Color.Black * 0.25F);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw the text itself
|
|
||||||
this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, TextFormattingDemo.Scale, 0);
|
this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, TextFormattingDemo.Scale, 0);
|
||||||
|
|
||||||
this.SpriteBatch.End();
|
this.SpriteBatch.End();
|
||||||
|
@ -80,8 +64,6 @@ namespace Demos {
|
||||||
public override void Update(GameTime time) {
|
public override void Update(GameTime time) {
|
||||||
// update our tokenized string to animate the animation codes
|
// update our tokenized string to animate the animation codes
|
||||||
this.tokenizedText.Update(time);
|
this.tokenizedText.Update(time);
|
||||||
if (this.InputHandler.IsPressed(Keys.B))
|
|
||||||
this.drawBounds = !this.drawBounds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Clear() {
|
public override void Clear() {
|
||||||
|
|
|
@ -51,7 +51,8 @@ namespace Demos {
|
||||||
CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8),
|
CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8),
|
||||||
RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3),
|
RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3),
|
||||||
RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8),
|
RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8),
|
||||||
AdditionalFonts = {{"Monospaced", new GenericSpriteFont(Demo.LoadContent<SpriteFont>("Fonts/MonospacedFont"))}}
|
AdditionalFonts = {{"Monospaced", new GenericSpriteFont(Demo.LoadContent<SpriteFont>("Fonts/MonospacedFont"))}},
|
||||||
|
LinkColor = Color.CornflowerBlue
|
||||||
};
|
};
|
||||||
var untexturedStyle = new UntexturedStyle(this.SpriteBatch) {
|
var untexturedStyle = new UntexturedStyle(this.SpriteBatch) {
|
||||||
TextScale = style.TextScale,
|
TextScale = style.TextScale,
|
||||||
|
@ -68,6 +69,7 @@ namespace Demos {
|
||||||
|
|
||||||
// create the root panel that all the other components sit on and add it to the ui system
|
// create the root panel that all the other components sit on and add it to the ui system
|
||||||
this.root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true);
|
this.root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true);
|
||||||
|
this.root.ScrollBar.SmoothScrolling = true;
|
||||||
// add the root to the demos' ui
|
// add the root to the demos' ui
|
||||||
this.UiRoot.AddChild(this.root);
|
this.UiRoot.AddChild(this.root);
|
||||||
|
|
||||||
|
@ -92,18 +94,11 @@ namespace Demos {
|
||||||
// a paragraph with formatting codes. To see them all or to add more, check the TextFormatting class
|
// a paragraph with formatting codes. To see them all or to add more, check the TextFormatting class
|
||||||
this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain <c Blue>formatting codes</c>, including colors and <i>text styles</i>. For more info, check out the <b>text formatting demo</b>!"));
|
this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain <c Blue>formatting codes</c>, including colors and <i>text styles</i>. For more info, check out the <b>text formatting demo</b>!"));
|
||||||
|
|
||||||
this.root.AddChild(new VerticalSpace(3));
|
|
||||||
this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Text input:", true));
|
|
||||||
this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10)) {
|
|
||||||
PositionOffset = new Vector2(0, 1),
|
|
||||||
PlaceholderText = "Click here to input text"
|
|
||||||
});
|
|
||||||
|
|
||||||
this.root.AddChild(new VerticalSpace(3));
|
this.root.AddChild(new VerticalSpace(3));
|
||||||
this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Multiline text input:", true));
|
this.root.AddChild(new Paragraph(Anchor.AutoCenter, 1, "Multiline text input:", true));
|
||||||
this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 50), multiline: true) {
|
this.root.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 50), multiline: true) {
|
||||||
PositionOffset = new Vector2(0, 1),
|
PositionOffset = new Vector2(0, 1),
|
||||||
PlaceholderText = "Click here to input a lot"
|
PlaceholderText = "Click here to input text"
|
||||||
});
|
});
|
||||||
|
|
||||||
this.root.AddChild(new VerticalSpace(3));
|
this.root.AddChild(new VerticalSpace(3));
|
||||||
|
@ -210,24 +205,12 @@ namespace Demos {
|
||||||
dropdown.AddElement(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Buttons"));
|
dropdown.AddElement(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Buttons"));
|
||||||
|
|
||||||
this.root.AddChild(new VerticalSpace(3));
|
this.root.AddChild(new VerticalSpace(3));
|
||||||
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button", "This button can't be clicked or moved to using automatic navigation") {
|
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button", "This button can't be clicked or moved to using automatic navigation") {IsDisabled = true}).PositionOffset = new Vector2(0, 1);
|
||||||
IsDisabled = true,
|
this.root.AddChild(new Checkbox(Anchor.AutoLeft, new Vector2(1, 10), "Disabled checkbox") {IsDisabled = true}).PositionOffset = new Vector2(0, 1);
|
||||||
PositionOffset = new Vector2(0, 1)
|
|
||||||
});
|
|
||||||
this.root.AddChild(new Checkbox(Anchor.AutoLeft, new Vector2(1, 10), "Disabled checkbox") {
|
|
||||||
IsDisabled = true,
|
|
||||||
PositionOffset = new Vector2(0, 1)
|
|
||||||
});
|
|
||||||
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled tooltip button", "This button can't be clicked, but can be moved to using automatic navigation, and will display its tooltip even when done so.") {
|
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled tooltip button", "This button can't be clicked, but can be moved to using automatic navigation, and will display its tooltip even when done so.") {
|
||||||
CanSelectDisabled = true,
|
CanSelectDisabled = true,
|
||||||
IsDisabled = true,
|
IsDisabled = true,
|
||||||
PositionOffset = new Vector2(0, 1),
|
Tooltip = {DisplayInAutoNavMode = true},
|
||||||
Tooltip = {
|
|
||||||
DisplayInAutoNavMode = true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Button with <b>far</b> too much text which will automatically be cut off, hi!") {
|
|
||||||
TruncateTextIfLong = true,
|
|
||||||
PositionOffset = new Vector2(0, 1)
|
PositionOffset = new Vector2(0, 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,3 @@ if (this.InputHandler.GetGesture(GestureType.Tap, out var sample)) {
|
||||||
// The gesture did not happen this frame
|
// The gesture did not happen this frame
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### External gesture handling
|
|
||||||
If your game already handles gestures through some other means, you might notice that one of the gesture handling methods stops working correctly. This is due to the fact that MonoGame's gesture querying system only supports each gesture to be queried once before it is removed from the queue.
|
|
||||||
|
|
||||||
If you want to continue using your own gesture handling, but still allow the `InputHandler` to use gestures (for [MLEM.Ui](ui.md), for example), you can set `GesturesExternal` to true in your `InputHandler`. Then, you can use `AddExternalGesture` to make the input handler aware of a gesture for the duration of the update frame that you added it on.
|
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
"metadata": [{
|
"metadata": [{
|
||||||
"src": [{
|
"src": [{
|
||||||
"src": "../",
|
"src": "../",
|
||||||
"files": ["**/MLEM**.csproj"],
|
"files": ["**/MLEM**.csproj"]
|
||||||
"exclude": ["**.FNA.**"]
|
|
||||||
}],
|
}],
|
||||||
"dest": "api"
|
"dest": "api"
|
||||||
}],
|
}],
|
||||||
|
@ -45,7 +44,8 @@
|
||||||
"fileMetadataFiles": [],
|
"fileMetadataFiles": [],
|
||||||
"template": [
|
"template": [
|
||||||
"default",
|
"default",
|
||||||
"templates/darkfx"
|
"templates/darkfx",
|
||||||
|
"templates/custom"
|
||||||
],
|
],
|
||||||
"postProcessors": [],
|
"postProcessors": [],
|
||||||
"markdownEngineName": "markdig",
|
"markdownEngineName": "markdig",
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
**MLEM Library for Extending MonoGame and FNA** is a set of multipurpose libraries for the game frameworks [MonoGame](https://www.monogame.net/) and [FNA](https://fna-xna.github.io/) that provides abstractions, quality of life improvements and additional features like an extensive ui system and easy input handling.
|
**MLEM Library for Extending MonoGame and FNA** is a set of multipurpose libraries for the game frameworks [MonoGame](https://www.monogame.net/) and [FNA](https://fna-xna.github.io/) that provides abstractions, quality of life improvements and additional features like an extensive ui system and easy input handling.
|
||||||
|
|
||||||
MLEM is platform-agnostic and multi-targets .NET Standard 2.0, .NET 6.0 and .NET Framework 4.5.2, which makes it compatible with MonoGame and FNA on Desktop, mobile devices and consoles.
|
|
||||||
|
|
||||||
# What next?
|
# What next?
|
||||||
- Get it on [NuGet](https://www.nuget.org/packages?q=mlem)
|
- Get it on [NuGet](https://www.nuget.org/packages?q=mlem)
|
||||||
- Get prerelease builds on [BaGet](https://nuget.ellpeck.de/?q=mlem)
|
- Get prerelease builds on [BaGet](https://nuget.ellpeck.de/?q=mlem)
|
||||||
|
@ -46,4 +44,3 @@ There are several other libraries and tools that work well in combination with M
|
||||||
- [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended), a package that also provides several additional features for MonoGame
|
- [MonoGame.Extended](https://github.com/craftworkgames/MonoGame.Extended), a package that also provides several additional features for MonoGame
|
||||||
- [Coroutine](https://github.com/Ellpeck/Coroutine), a package that implements Unity-style coroutines for any project
|
- [Coroutine](https://github.com/Ellpeck/Coroutine), a package that implements Unity-style coroutines for any project
|
||||||
- [Illumilib](https://github.com/Ellpeck/Illumilib), a simple keyboard and mouse lighting library with support for Razer, Logitech and Corsair devices
|
- [Illumilib](https://github.com/Ellpeck/Illumilib), a simple keyboard and mouse lighting library with support for Razer, Logitech and Corsair devices
|
||||||
- [DynamicEnums](https://github.com/Ellpeck/DynamicEnums), which provides enum-like single-instance values with additional capabilities, including dynamic addition of new arbitrary values and flags
|
|
||||||
|
|
32
Docs/templates/custom/partials/head.tmpl.partial
Normal file
32
Docs/templates/custom/partials/head.tmpl.partial
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
{{!Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.}}
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||||
|
<title>{{#title}}{{title}}{{/title}}{{^title}}{{>partials/title}}{{/title}} {{#_appTitle}}| {{_appTitle}} {{/_appTitle}}</title>
|
||||||
|
<meta name="viewport" content="width=device-width">
|
||||||
|
<meta name="title" content="{{#title}}{{title}}{{/title}}{{^title}}{{>partials/title}}{{/title}} {{#_appTitle}}| {{_appTitle}} {{/_appTitle}}">
|
||||||
|
<meta name="generator" content="docfx {{_docfxVersion}}">
|
||||||
|
{{#_description}}<meta name="description" content="{{_description}}">{{/_description}}
|
||||||
|
<link rel="shortcut icon" href="{{_rel}}{{{_appFaviconPath}}}{{^_appFaviconPath}}favicon.ico{{/_appFaviconPath}}">
|
||||||
|
<link rel="stylesheet" href="{{_rel}}styles/docfx.vendor.css">
|
||||||
|
<link rel="stylesheet" href="{{_rel}}styles/docfx.css">
|
||||||
|
<link rel="stylesheet" href="{{_rel}}styles/main.css">
|
||||||
|
<meta property="docfx:navrel" content="{{_navRel}}">
|
||||||
|
<meta property="docfx:tocrel" content="{{_tocRel}}">
|
||||||
|
{{#_noindex}}<meta name="searchOption" content="noindex">{{/_noindex}}
|
||||||
|
{{#_enableSearch}}<meta property="docfx:rel" content="{{_rel}}">{{/_enableSearch}}
|
||||||
|
{{#_enableNewTab}}<meta property="docfx:newtab" content="true">{{/_enableNewTab}}
|
||||||
|
|
||||||
|
<!-- Global site tag (gtag.js) - Google Analytics -->
|
||||||
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-85JBDH1P3Z"></script>
|
||||||
|
<script>
|
||||||
|
window.dataLayer = window.dataLayer || [];
|
||||||
|
|
||||||
|
let gtag = function () {
|
||||||
|
dataLayer.push(arguments);
|
||||||
|
}
|
||||||
|
gtag('js', new Date());
|
||||||
|
gtag('config', 'G-85JBDH1P3Z');
|
||||||
|
</script>
|
||||||
|
</head>
|
2
FNA
2
FNA
|
@ -1 +1 @@
|
||||||
Subproject commit 9029e149358197612509e2ee4893870a3b5d590e
|
Subproject commit 102990f514f1e5bfac07d33f7c33e2e712946da4
|
|
@ -1,15 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<PropertyGroup>
|
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<!-- include reference assemblies so we can build for old framework versions without having to install them -->
|
|
||||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<!-- dummy pack target to allow for this (non-SDK-style) project to be included in dotnet pack -->
|
|
||||||
<Target Name="Pack" />
|
|
||||||
</Project>
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit c50bf544bcfb217b518727a0a38eb71fc5725092
|
Subproject commit f0774130cad6cec0b790a58bc7c811a186443fb3
|
|
@ -5,52 +5,33 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Content;
|
using Microsoft.Xna.Framework.Content;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Data.Content {
|
namespace MLEM.Data.Content {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a version of <see cref="ContentManager"/> that doesn't load content binary <c>xnb</c> files, but rather as their regular formats.
|
/// Represents a version of <see cref="ContentManager"/> that doesn't load content binary <c>xnb</c> files, but rather as their regular formats.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class RawContentManager : ContentManager, IGameComponent {
|
public class RawContentManager : ContentManager, IGameComponent {
|
||||||
|
|
||||||
|
private static List<RawContentReader> readers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The graphics device that this content manager uses
|
/// The graphics device that this content manager uses
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly GraphicsDevice GraphicsDevice;
|
public readonly GraphicsDevice GraphicsDevice;
|
||||||
|
|
||||||
private readonly List<IDisposable> disposableAssets = new List<IDisposable>();
|
private readonly List<IDisposable> disposableAssets = new List<IDisposable>();
|
||||||
private readonly List<RawContentReader> readers;
|
|
||||||
|
|
||||||
#if FNA
|
#if FNA
|
||||||
private Dictionary<string, object> LoadedAssets { get; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
private Dictionary<string, object> LoadedAssets { get; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new content manager with an optionally specified root directory.
|
/// Creates a new content manager with an optionally specified root directory.
|
||||||
/// Each <see cref="RawContentReader"/> required for asset loading is gathered and instantiated automatically from the loaded assemblies.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="serviceProvider">The service provider of your game</param>
|
/// <param name="serviceProvider">The service provider of your game</param>
|
||||||
/// <param name="rootDirectory">The root directory. Defaults to "Content"</param>
|
/// <param name="rootDirectory">The root directory. Defaults to "Content"</param>
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[RequiresUnreferencedCode("Automatically gathered RawContentReader types might be removed, use other constructor to add readers manually")]
|
|
||||||
#endif
|
|
||||||
public RawContentManager(IServiceProvider serviceProvider, string rootDirectory = "Content") :
|
public RawContentManager(IServiceProvider serviceProvider, string rootDirectory = "Content") :
|
||||||
this(serviceProvider, RawContentManager.CollectContentReaders(), rootDirectory) {}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new content manager with an optionally specified root directory.
|
|
||||||
/// Each <see cref="RawContentReader"/> required for asset loading has to be passed using <paramref name="readers"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceProvider">The service provider of your game</param>
|
|
||||||
/// <param name="readers">The raw content readers to use, which can be modified externally afterwards to add additional readers if desired.</param>
|
|
||||||
/// <param name="rootDirectory">The root directory. Defaults to "Content"</param>
|
|
||||||
public RawContentManager(IServiceProvider serviceProvider, List<RawContentReader> readers, string rootDirectory) :
|
|
||||||
base(serviceProvider, rootDirectory) {
|
base(serviceProvider, rootDirectory) {
|
||||||
if (serviceProvider.GetService(typeof(IGraphicsDeviceService)) is IGraphicsDeviceService s)
|
if (serviceProvider.GetService(typeof(IGraphicsDeviceService)) is IGraphicsDeviceService s)
|
||||||
this.GraphicsDevice = s.GraphicsDevice;
|
this.GraphicsDevice = s.GraphicsDevice;
|
||||||
this.readers = readers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -82,7 +63,9 @@ namespace MLEM.Data.Content {
|
||||||
|
|
||||||
private T Read<T>(string assetName, T existing) {
|
private T Read<T>(string assetName, T existing) {
|
||||||
var triedFiles = new List<string>();
|
var triedFiles = new List<string>();
|
||||||
foreach (var reader in this.readers) {
|
if (RawContentManager.readers == null)
|
||||||
|
RawContentManager.readers = RawContentManager.CollectContentReaders();
|
||||||
|
foreach (var reader in RawContentManager.readers) {
|
||||||
if (!reader.CanRead(typeof(T)))
|
if (!reader.CanRead(typeof(T)))
|
||||||
continue;
|
continue;
|
||||||
foreach (var ext in reader.GetFileExtensions()) {
|
foreach (var ext in reader.GetFileExtensions()) {
|
||||||
|
@ -121,9 +104,6 @@ namespace MLEM.Data.Content {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Initialize() {}
|
public void Initialize() {}
|
||||||
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[RequiresUnreferencedCode("Automatically gathered RawContentReader types might be removed, use other constructor to add readers manually")]
|
|
||||||
#endif
|
|
||||||
private static List<RawContentReader> CollectContentReaders() {
|
private static List<RawContentReader> CollectContentReaders() {
|
||||||
var ret = new List<RawContentReader>();
|
var ret = new List<RawContentReader>();
|
||||||
var assemblyExceptions = new List<Exception>();
|
var assemblyExceptions = new List<Exception>();
|
||||||
|
|
|
@ -13,9 +13,9 @@ namespace MLEM.Data.Content {
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
existing.Reload(stream);
|
existing.Reload(stream);
|
||||||
return existing;
|
return existing;
|
||||||
}
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
// premultiply the texture's color to be in line with the pipeline's texture reader
|
// premultiply the texture's color to be in line with the pipeline's texture reader
|
||||||
using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream)) {
|
using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream)) {
|
||||||
var ret = new Texture2D(manager.GraphicsDevice, texture.Width, texture.Height);
|
var ret = new Texture2D(manager.GraphicsDevice, texture.Width, texture.Height);
|
||||||
|
@ -30,6 +30,7 @@ namespace MLEM.Data.Content {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string[] GetFileExtensions() {
|
public override string[] GetFileExtensions() {
|
||||||
|
|
|
@ -12,10 +12,6 @@ namespace MLEM.Data.Content {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026",
|
|
||||||
Justification = "RawContentManager does not support XmlReader in a trimmed or AOT context, so this method is not expected to be called.")]
|
|
||||||
#endif
|
|
||||||
public override object Read(RawContentManager manager, string assetPath, Stream stream, Type t, object existing) {
|
public override object Read(RawContentManager manager, string assetPath, Stream stream, Type t, object existing) {
|
||||||
return new XmlSerializer(t).Deserialize(stream);
|
return new XmlSerializer(t).Deserialize(stream);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,11 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Data {
|
namespace MLEM.Data {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A set of extensions for dealing with copying objects.
|
/// A set of extensions for dealing with copying objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
|
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[UnconditionalSuppressMessage("Aot", "IL3050"), UnconditionalSuppressMessage("Aot", "IL2070"), UnconditionalSuppressMessage("Aot", "IL2090")]
|
|
||||||
#endif
|
|
||||||
public static class CopyExtensions {
|
public static class CopyExtensions {
|
||||||
|
|
||||||
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
|
|
|
@ -7,7 +7,6 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Content;
|
using Microsoft.Xna.Framework.Content;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using MLEM.Misc;
|
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
|
|
||||||
namespace MLEM.Data {
|
namespace MLEM.Data {
|
||||||
|
@ -18,20 +17,15 @@ namespace MLEM.Data {
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// Data texture atlases are designed to be easy to write by hand. Because of this, their structure is very simple.
|
/// Data texture atlases are designed to be easy to write by hand. Because of this, their structure is very simple.
|
||||||
/// Each texture region defined in the atlas consists of its names (where multiple names can be separated by whitespace), followed by a set of possible instructions and their arguments, also separated by whitespace.
|
/// Each texture region defined in the atlas consists of its name, followed by a set of possible keywords and their arguments, separated by spaces.
|
||||||
/// <list type="bullet">
|
/// The <c>loc</c> keyword defines the <see cref="TextureRegion.Area"/> of the texture region as a rectangle whose origin is its top-left corner. It requires four arguments: x, y, width and height of the rectangle.
|
||||||
/// <item><description>The <c>loc</c> (or <c>location</c>) instruction defines the <see cref="TextureRegion.Area"/> of the texture region as a rectangle whose origin is its top-left corner. It requires four arguments: x, y, width and height of the rectangle.</description></item>
|
/// The (optional) <c>piv</c> keyword defines the <see cref="TextureRegion.PivotPixels"/> of the texture region. It requires two arguments: x and y. If it is not supplied, the pivot defaults to the top-left corner of the texture region.
|
||||||
/// <item><description>The (optional) <c>piv</c> (or <c>pivot</c>) instruction defines the <see cref="TextureRegion.PivotPixels"/> of the texture region. It requires two arguments: x and y. If it is not supplied, the pivot defaults to the top-left corner of the texture region.</description></item>
|
/// The (optional) <c>off</c> keyword defines an offset that is added onto the location and pivot of this texture region. This is useful when copying and pasting a previously defined texture region to create a second region that has a constant offset. It requires two arguments: The x and y offset.
|
||||||
/// <item><description>The (optional) <c>off</c> (of <c>offset</c>) instruction defines an offset that is added onto the location and pivot of this texture region. This is useful when duplicating a previously defined texture region to create a second region that has a constant offset. It requires two arguments: The x and y offset.</description></item>
|
|
||||||
/// <item><description>The (optional and repeatable) <c>cpy</c> (or <c>copy</c>) instruction defines an additional texture region that should also be generated from the same data, but with a given offset that will be applied to the location and pivot. It requires three arguments: the copy region's name and the x and y offsets.</description></item>
|
|
||||||
/// <item><description>The (optional and repeatable) <c>dat</c> (or <c>data</c>) instruction defines a custom data point that can be added to the resulting <see cref="TextureRegion"/>'s <see cref="GenericDataHolder"/> data. It requires two arguments: the data point's name and the data point's value, the latter of which is also stored as a string value.</description></item>
|
|
||||||
/// <item><description>The (optional) <c>frm</c> (or <c>from</c>) instruction defines a texture region (defined before the current region) whose data should be copied. All data from the region will be copied, but adding additional instructions afterwards modifies the data. It requires one argument: the name of the region whose data to copy. If this instruction is used, the <c>loc</c> instruction is not required.</description></item>
|
|
||||||
/// </list>
|
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <example>
|
/// <example>
|
||||||
/// The following entry defines a texture region with the names <c>LongTableRight</c> and <c>LongTableUp</c>, whose <see cref="TextureRegion.Area"/> will be a rectangle with X=32, Y=30, Width=64, Height=48, and whose <see cref="TextureRegion.PivotPixels"/> will be a vector with X=80, Y=46.
|
/// The following entry defines a texture region with the name <c>LongTableRight</c>, whose <see cref="TextureRegion.Area"/> will be a rectangle with X=32, Y=30, Width=64, Height=48, and whose <see cref="TextureRegion.PivotPixels"/> will be a vector with X=80, Y=46.
|
||||||
/// <code>
|
/// <code>
|
||||||
/// LongTableRight LongTableUp
|
/// LongTableRight
|
||||||
/// loc 32 30 64 48
|
/// loc 32 30 64 48
|
||||||
/// piv 80 46
|
/// piv 80 46
|
||||||
/// </code>
|
/// </code>
|
||||||
|
@ -72,18 +66,8 @@ namespace MLEM.Data {
|
||||||
this.Texture = texture;
|
this.Texture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts this data texture atlas to a <see cref="Dictionary{TKey,TValue}"/> and returns the result.
|
|
||||||
/// The resulting dictionary will contain all named regions that this data texture atlas contains.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The dictionary representation of this data texture atlas.</returns>
|
|
||||||
public Dictionary<string, TextureRegion> ToDictionary() {
|
|
||||||
return new Dictionary<string, TextureRegion>(this.regions);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads a <see cref="DataTextureAtlas"/> from the given loaded texture and texture data file.
|
/// Loads a <see cref="DataTextureAtlas"/> from the given loaded texture and texture data file.
|
||||||
/// For more information on data texture atlases, see the <see cref="DataTextureAtlas"/> type documentation.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="texture">The texture to use for this data texture atlas</param>
|
/// <param name="texture">The texture to use for this data texture atlas</param>
|
||||||
/// <param name="content">The content manager to use for loading</param>
|
/// <param name="content">The content manager to use for loading</param>
|
||||||
|
@ -93,124 +77,45 @@ namespace MLEM.Data {
|
||||||
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) {
|
public static DataTextureAtlas LoadAtlasData(TextureRegion texture, ContentManager content, string infoPath, bool pivotRelative = false) {
|
||||||
var info = Path.Combine(content.RootDirectory, infoPath);
|
var info = Path.Combine(content.RootDirectory, infoPath);
|
||||||
string text;
|
string text;
|
||||||
try {
|
|
||||||
if (Path.IsPathRooted(info)) {
|
if (Path.IsPathRooted(info)) {
|
||||||
text = File.ReadAllText(info);
|
text = File.ReadAllText(info);
|
||||||
} else {
|
} else {
|
||||||
using (var reader = new StreamReader(TitleContainer.OpenStream(info)))
|
using (var reader = new StreamReader(TitleContainer.OpenStream(info)))
|
||||||
text = reader.ReadToEnd();
|
text = reader.ReadToEnd();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ContentLoadException($"Couldn't load data texture atlas data from {info}", e);
|
|
||||||
}
|
|
||||||
var atlas = new DataTextureAtlas(texture);
|
var atlas = new DataTextureAtlas(texture);
|
||||||
var words = Regex.Split(text, @"\s+");
|
|
||||||
|
|
||||||
var namesOffsets = new List<(string, Vector2)>();
|
// parse each texture region: "<names> loc <u> <v> <w> <h> [piv <px> <py>] [off <ox> <oy>]"
|
||||||
var customData = new Dictionary<string, string>();
|
foreach (Match match in Regex.Matches(text, @"(.+)\W+loc\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)\W+([0-9]+)\W*(?:piv\W+([0-9.]+)\W+([0-9.]+))?\W*(?:off\W+([0-9.]+)\W+([0-9.]+))?")) {
|
||||||
var location = Rectangle.Empty;
|
// offset
|
||||||
var pivot = Vector2.Zero;
|
var off = !match.Groups[8].Success ? Vector2.Zero : new Vector2(
|
||||||
var offset = Vector2.Zero;
|
float.Parse(match.Groups[8].Value, CultureInfo.InvariantCulture),
|
||||||
for (var i = 0; i < words.Length; i++) {
|
float.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture));
|
||||||
var word = words[i];
|
|
||||||
try {
|
|
||||||
switch (word) {
|
|
||||||
case "loc":
|
|
||||||
case "location":
|
|
||||||
location = new Rectangle(
|
|
||||||
int.Parse(words[i + 1], CultureInfo.InvariantCulture), int.Parse(words[i + 2], CultureInfo.InvariantCulture),
|
|
||||||
int.Parse(words[i + 3], CultureInfo.InvariantCulture), int.Parse(words[i + 4], CultureInfo.InvariantCulture));
|
|
||||||
i += 4;
|
|
||||||
break;
|
|
||||||
case "piv":
|
|
||||||
case "pivot":
|
|
||||||
pivot = new Vector2(
|
|
||||||
float.Parse(words[i + 1], CultureInfo.InvariantCulture),
|
|
||||||
float.Parse(words[i + 2], CultureInfo.InvariantCulture));
|
|
||||||
i += 2;
|
|
||||||
break;
|
|
||||||
case "off":
|
|
||||||
case "offset":
|
|
||||||
offset = new Vector2(
|
|
||||||
float.Parse(words[i + 1], CultureInfo.InvariantCulture),
|
|
||||||
float.Parse(words[i + 2], CultureInfo.InvariantCulture));
|
|
||||||
i += 2;
|
|
||||||
break;
|
|
||||||
case "cpy":
|
|
||||||
case "copy":
|
|
||||||
var copyOffset = new Vector2(
|
|
||||||
float.Parse(words[i + 2], CultureInfo.InvariantCulture),
|
|
||||||
float.Parse(words[i + 3], CultureInfo.InvariantCulture));
|
|
||||||
namesOffsets.Add((words[i + 1], copyOffset));
|
|
||||||
i += 3;
|
|
||||||
break;
|
|
||||||
case "dat":
|
|
||||||
case "data":
|
|
||||||
customData.Add(words[i + 1], words[i + 2]);
|
|
||||||
i += 2;
|
|
||||||
break;
|
|
||||||
case "frm":
|
|
||||||
case "from":
|
|
||||||
var fromRegion = atlas[words[i + 1]];
|
|
||||||
customData.Clear();
|
|
||||||
foreach (var key in fromRegion.GetDataKeys())
|
|
||||||
customData.Add(key, fromRegion.GetData<string>(key));
|
|
||||||
// our main texture might be a sub-region already, so we have to take that into account
|
|
||||||
location = fromRegion.Area.OffsetCopy(new Point(-texture.U, -texture.V));
|
|
||||||
pivot = fromRegion.PivotPixels;
|
|
||||||
if (pivot != Vector2.Zero && !pivotRelative)
|
|
||||||
pivot += location.Location.ToVector2();
|
|
||||||
offset = Vector2.Zero;
|
|
||||||
i += 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// if we have data for the previous regions, they're valid so we add them
|
|
||||||
AddCurrentRegions();
|
|
||||||
|
|
||||||
// we're starting a new region (or adding another name for a new region), so clear old data
|
|
||||||
namesOffsets.Add((word.Trim(), Vector2.Zero));
|
|
||||||
customData.Clear();
|
|
||||||
location = Rectangle.Empty;
|
|
||||||
pivot = Vector2.Zero;
|
|
||||||
offset = Vector2.Zero;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ContentLoadException($"Couldn't parse data texture atlas instruction {word} for region(s) {string.Join(", ", namesOffsets)}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add the last region that was started on
|
|
||||||
AddCurrentRegions();
|
|
||||||
return atlas;
|
|
||||||
|
|
||||||
void AddCurrentRegions() {
|
|
||||||
// the location is the only mandatory information, which is why we check it here
|
|
||||||
if (location == Rectangle.Empty || namesOffsets.Count <= 0)
|
|
||||||
return;
|
|
||||||
foreach (var (name, addedOff) in namesOffsets) {
|
|
||||||
var loc = location;
|
|
||||||
var piv = pivot;
|
|
||||||
var off = offset + addedOff;
|
|
||||||
|
|
||||||
|
// location
|
||||||
|
var loc = new Rectangle(
|
||||||
|
int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value),
|
||||||
|
int.Parse(match.Groups[4].Value), int.Parse(match.Groups[5].Value));
|
||||||
loc.Offset(off.ToPoint());
|
loc.Offset(off.ToPoint());
|
||||||
if (piv != Vector2.Zero) {
|
|
||||||
piv += off;
|
|
||||||
if (!pivotRelative)
|
|
||||||
piv -= loc.Location.ToVector2();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// pivot
|
||||||
|
var piv = !match.Groups[6].Success ? Vector2.Zero : off + new Vector2(
|
||||||
|
float.Parse(match.Groups[6].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.X),
|
||||||
|
float.Parse(match.Groups[7].Value, CultureInfo.InvariantCulture) - (pivotRelative ? 0 : loc.Y));
|
||||||
|
|
||||||
|
foreach (var name in Regex.Split(match.Groups[1].Value, @"\W")) {
|
||||||
|
var trimmed = name.Trim();
|
||||||
|
if (trimmed.Length <= 0)
|
||||||
|
continue;
|
||||||
var region = new TextureRegion(texture, loc) {
|
var region = new TextureRegion(texture, loc) {
|
||||||
PivotPixels = piv,
|
PivotPixels = piv,
|
||||||
Name = name
|
Name = trimmed
|
||||||
};
|
};
|
||||||
foreach (var kv in customData)
|
atlas.regions.Add(trimmed, region);
|
||||||
region.SetData(kv.Key, kv.Value);
|
|
||||||
atlas.regions.Add(name, region);
|
|
||||||
}
|
}
|
||||||
// we only clear names offsets if the location was valid, otherwise we ignore multiple names for a region
|
|
||||||
namesOffsets.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -222,7 +127,6 @@ namespace MLEM.Data {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads a <see cref="DataTextureAtlas"/> from the given texture and texture data file.
|
/// Loads a <see cref="DataTextureAtlas"/> from the given texture and texture data file.
|
||||||
/// For more information on data texture atlases, see the <see cref="DataTextureAtlas"/> type documentation.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="content">The content manager to use for loading</param>
|
/// <param name="content">The content manager to use for loading</param>
|
||||||
/// <param name="texturePath">The path to the texture file</param>
|
/// <param name="texturePath">The path to the texture file</param>
|
||||||
|
|
|
@ -13,6 +13,9 @@ namespace MLEM.Data {
|
||||||
/// A dynamic enum uses <see cref="BigInteger"/> as its underlying type, allowing for an arbitrary number of enum values to be created, even when a <see cref="FlagsAttribute"/>-like structure is used that would only allow for up to 64 values in a regular enum.
|
/// A dynamic enum uses <see cref="BigInteger"/> as its underlying type, allowing for an arbitrary number of enum values to be created, even when a <see cref="FlagsAttribute"/>-like structure is used that would only allow for up to 64 values in a regular enum.
|
||||||
/// All enum operations including <see cref="And{T}"/>, <see cref="Or{T}"/>, <see cref="Xor{T}"/> and <see cref="Neg{T}"/> are supported and can be implemented in derived classes using operator overloads.
|
/// All enum operations including <see cref="And{T}"/>, <see cref="Or{T}"/>, <see cref="Xor{T}"/> and <see cref="Neg{T}"/> are supported and can be implemented in derived classes using operator overloads.
|
||||||
/// To create a custom dynamic enum, simply create a class that extends <see cref="DynamicEnum"/>. New values can then be added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
/// To create a custom dynamic enum, simply create a class that extends <see cref="DynamicEnum"/>. New values can then be added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
||||||
|
///
|
||||||
|
/// This class, and its entire concept, are extremely terrible. If you intend on using this, there's probably at least one better solution available.
|
||||||
|
/// Though if, for some weird reason, you need a way to have more than 64 distinct flags, this is a pretty good solution.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// To include enum-like operator overloads in a dynamic enum named MyEnum, the following code can be used:
|
/// To include enum-like operator overloads in a dynamic enum named MyEnum, the following code can be used:
|
||||||
|
@ -25,10 +28,7 @@ namespace MLEM.Data {
|
||||||
/// public static MyEnum operator ~(MyEnum value) => Neg(value);
|
/// public static MyEnum operator ~(MyEnum value) => Neg(value);
|
||||||
/// </code>
|
/// </code>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
[Obsolete("DynamicEnum has been moved into the DynamicEnums library: https://www.nuget.org/packages/DynamicEnums"), JsonConverter(typeof(DynamicEnumConverter))]
|
[JsonConverter(typeof(DynamicEnumConverter))]
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Aot", "IL2067")]
|
|
||||||
#endif
|
|
||||||
public abstract class DynamicEnum {
|
public abstract class DynamicEnum {
|
||||||
|
|
||||||
private static readonly Dictionary<Type, Storage> Storages = new Dictionary<Type, Storage>();
|
private static readonly Dictionary<Type, Storage> Storages = new Dictionary<Type, Storage>();
|
||||||
|
@ -134,7 +134,7 @@ namespace MLEM.Data {
|
||||||
/// <returns>The newly created enum value</returns>
|
/// <returns>The newly created enum value</returns>
|
||||||
public static T AddValue<T>(string name) where T : DynamicEnum {
|
public static T AddValue<T>(string name) where T : DynamicEnum {
|
||||||
BigInteger value = 0;
|
BigInteger value = 0;
|
||||||
while (DynamicEnum.IsDefined(typeof(T), value))
|
while (DynamicEnum.GetStorage(typeof(T)).Values.ContainsKey(value))
|
||||||
value++;
|
value++;
|
||||||
return DynamicEnum.Add<T>(name, value);
|
return DynamicEnum.Add<T>(name, value);
|
||||||
}
|
}
|
||||||
|
@ -149,7 +149,7 @@ namespace MLEM.Data {
|
||||||
/// <returns>The newly created enum value</returns>
|
/// <returns>The newly created enum value</returns>
|
||||||
public static T AddFlag<T>(string name) where T : DynamicEnum {
|
public static T AddFlag<T>(string name) where T : DynamicEnum {
|
||||||
BigInteger value = 1;
|
BigInteger value = 1;
|
||||||
while (DynamicEnum.IsDefined(typeof(T), value))
|
while (DynamicEnum.GetStorage(typeof(T)).Values.ContainsKey(value))
|
||||||
value <<= 1;
|
value <<= 1;
|
||||||
return DynamicEnum.Add<T>(name, value);
|
return DynamicEnum.Add<T>(name, value);
|
||||||
}
|
}
|
||||||
|
@ -174,42 +174,6 @@ namespace MLEM.Data {
|
||||||
return DynamicEnum.GetStorage(type).Values.Values;
|
return DynamicEnum.GetStorage(type).Values.Values;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns all of the defined values from the given dynamic enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
|
|
||||||
/// Note that, if combined flags are defined in <typeparamref name="T"/>, and <paramref name="combinedFlag"/> contains them, they will also be returned.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
|
|
||||||
/// <param name="includeZero">Whether the enum value 0 should also be returned, if <typeparamref name="T"/> contains one.</param>
|
|
||||||
/// <typeparam name="T">The type of enum.</typeparam>
|
|
||||||
/// <returns>All of the flags that make up <paramref name="combinedFlag"/>.</returns>
|
|
||||||
public static IEnumerable<T> GetFlags<T>(T combinedFlag, bool includeZero = true) where T : DynamicEnum {
|
|
||||||
foreach (var flag in DynamicEnum.GetValues<T>()) {
|
|
||||||
if (combinedFlag.HasFlag(flag) && (includeZero || DynamicEnum.GetValue(flag) != BigInteger.Zero))
|
|
||||||
yield return flag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns all of the defined unique flags from the given dynamic enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
|
|
||||||
/// Any combined flags (flags that aren't powers of two) which are defined in <typeparamref name="T"/> will not be returned.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
|
|
||||||
/// <typeparam name="T">The type of enum.</typeparam>
|
|
||||||
/// <returns>All of the unique flags that make up <paramref name="combinedFlag"/>.</returns>
|
|
||||||
public static IEnumerable<T> GetUniqueFlags<T>(T combinedFlag) where T : DynamicEnum {
|
|
||||||
// we can't use the same method here as EnumHelper.GetUniqueFlags since DynamicEnum doesn't guarantee sorted values
|
|
||||||
var max = DynamicEnum.GetValues<T>().Max(DynamicEnum.GetValue);
|
|
||||||
var uniqueFlag = BigInteger.One;
|
|
||||||
while (uniqueFlag <= max) {
|
|
||||||
if (DynamicEnum.IsDefined(typeof(T), uniqueFlag)) {
|
|
||||||
var uniqueFlagValue = DynamicEnum.GetEnumValue<T>(uniqueFlag);
|
|
||||||
if (combinedFlag.HasFlag(uniqueFlagValue))
|
|
||||||
yield return uniqueFlagValue;
|
|
||||||
}
|
|
||||||
uniqueFlag <<= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the bitwise OR (|) combination of the two dynamic enum values
|
/// Returns the bitwise OR (|) combination of the two dynamic enum values
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -328,8 +292,7 @@ namespace MLEM.Data {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses the given <see cref="string"/> into a dynamic enum value and returns the result.
|
/// Parses the given <see cref="string"/> into a dynamic enum value and returns the result.
|
||||||
/// This method supports defined enum values as well as values combined using the pipe (|) character and any number of spaces.
|
/// This method supports defined enum values as well as values combined using the pipe (|) character and any number of spaces.
|
||||||
/// If no enum value can be parsed, null is returned.
|
/// If no enum value can be parsed, null is returned. /// </summary>
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The type of the dynamic enum value to parse</param>
|
/// <param name="type">The type of the dynamic enum value to parse</param>
|
||||||
/// <param name="strg">The string to parse into a dynamic enum value</param>
|
/// <param name="strg">The string to parse into a dynamic enum value</param>
|
||||||
/// <returns>The parsed enum value, or null if parsing fails</returns>
|
/// <returns>The parsed enum value, or null if parsing fails</returns>
|
||||||
|
@ -352,27 +315,6 @@ namespace MLEM.Data {
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given <paramref name="value"/> is defined in the given dynamic enum <paramref name="type"/>.
|
|
||||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The dynamic enum type to query.</param>
|
|
||||||
/// <param name="value">The value to query.</param>
|
|
||||||
/// <returns>Whether the <paramref name="value"/> is defined.</returns>
|
|
||||||
public static bool IsDefined(Type type, BigInteger value) {
|
|
||||||
return DynamicEnum.GetStorage(type).Values.ContainsKey(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given <paramref name="value"/> is defined in its dynamic enum type.
|
|
||||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value to query.</param>
|
|
||||||
/// <returns>Whether the <paramref name="value"/> is defined.</returns>
|
|
||||||
public static bool IsDefined(DynamicEnum value) {
|
|
||||||
return value != null && DynamicEnum.IsDefined(value.GetType(), DynamicEnum.GetValue(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Storage GetStorage(Type type) {
|
private static Storage GetStorage(Type type) {
|
||||||
if (!DynamicEnum.Storages.TryGetValue(type, out var storage)) {
|
if (!DynamicEnum.Storages.TryGetValue(type, out var storage)) {
|
||||||
storage = new Storage();
|
storage = new Storage();
|
||||||
|
|
|
@ -5,7 +5,6 @@ namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts a <see cref="DynamicEnum"/> to and from JSON
|
/// Converts a <see cref="DynamicEnum"/> to and from JSON
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("DynamicEnum has been moved into the DynamicEnums library: https://www.nuget.org/packages/DynamicEnums"), JsonConverter(typeof(DynamicEnumConverter))]
|
|
||||||
public class DynamicEnumConverter : JsonConverter<DynamicEnum> {
|
public class DynamicEnumConverter : JsonConverter<DynamicEnum> {
|
||||||
|
|
||||||
/// <summary>Writes the JSON representation of the object.</summary>
|
/// <summary>Writes the JSON representation of the object.</summary>
|
||||||
|
|
|
@ -11,16 +11,9 @@ namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An array of all of the <see cref="JsonConverter"/>s that are part of MLEM.Data
|
/// An array of all of the <see cref="JsonConverter"/>s that are part of MLEM.Data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly JsonConverter[] Converters = {
|
public static readonly JsonConverter[] Converters = typeof(JsonConverters).Assembly.GetExportedTypes()
|
||||||
new Direction2Converter(),
|
.Where(t => t.IsSubclassOf(typeof(JsonConverter)) && !t.IsGenericType)
|
||||||
#pragma warning disable CS0618
|
.Select(Activator.CreateInstance).Cast<JsonConverter>().ToArray();
|
||||||
new DynamicEnumConverter(),
|
|
||||||
#pragma warning restore CS0618
|
|
||||||
new PointConverter(),
|
|
||||||
new RectangleConverter(),
|
|
||||||
new RectangleFConverter(),
|
|
||||||
new Vector2Converter()
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds all of the <see cref="JsonConverter"/> objects that are part of MLEM.Data to the given <see cref="JsonSerializer"/>
|
/// Adds all of the <see cref="JsonConverter"/> objects that are part of MLEM.Data to the given <see cref="JsonSerializer"/>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
@ -7,27 +8,16 @@ namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An <see cref="IGenericDataHolder"/> represents an object that can hold generic key-value based data.
|
/// An <see cref="IGenericDataHolder"/> represents an object that can hold generic key-value based data.
|
||||||
/// This class uses <see cref="JsonTypeSafeWrapper"/> for each object stored to ensure that objects with a custom <see cref="JsonConverter"/> get deserialized as an instance of their original type if <see cref="JsonSerializer.TypeNameHandling"/> is not set to <see cref="TypeNameHandling.None"/>.
|
/// This class uses <see cref="JsonTypeSafeWrapper"/> for each object stored to ensure that objects with a custom <see cref="JsonConverter"/> get deserialized as an instance of their original type if <see cref="JsonSerializer.TypeNameHandling"/> is not set to <see cref="TypeNameHandling.None"/>.
|
||||||
/// Note that, using <see cref="SetData"/>, adding <see cref="JsonTypeSafeWrapper{T}"/> instances directly is also possible.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
#if NET7_0_OR_GREATER
|
[DataContract]
|
||||||
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for instantiation of JsonTypeSafeWrapper instances might not be available at runtime.")]
|
|
||||||
#endif
|
|
||||||
public class JsonTypeSafeGenericDataHolder : IGenericDataHolder {
|
public class JsonTypeSafeGenericDataHolder : IGenericDataHolder {
|
||||||
|
|
||||||
private static readonly string[] EmptyStrings = new string[0];
|
[DataMember(EmitDefaultValue = false)]
|
||||||
|
|
||||||
[JsonProperty]
|
|
||||||
private Dictionary<string, JsonTypeSafeWrapper> data;
|
private Dictionary<string, JsonTypeSafeWrapper> data;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
|
|
||||||
public void SetData(string key, object data) {
|
public void SetData(string key, object data) {
|
||||||
this.SetData<object>(key, data);
|
if (data == default) {
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void SetData<T>(string key, T data) {
|
|
||||||
if (EqualityComparer<T>.Default.Equals(data, default)) {
|
|
||||||
if (this.data != null)
|
if (this.data != null)
|
||||||
this.data.Remove(key);
|
this.data.Remove(key);
|
||||||
} else {
|
} else {
|
||||||
|
@ -45,9 +35,9 @@ namespace MLEM.Data.Json {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IEnumerable<string> GetDataKeys() {
|
public IReadOnlyCollection<string> GetDataKeys() {
|
||||||
if (this.data == null)
|
if (this.data == null)
|
||||||
return JsonTypeSafeGenericDataHolder.EmptyStrings;
|
return Array.Empty<string>();
|
||||||
return this.data.Keys;
|
return this.data.Keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace MLEM.Data.Json {
|
namespace MLEM.Data.Json {
|
||||||
|
@ -14,7 +15,6 @@ namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns this json type-safe wrapper's value as an <see cref="object"/>.
|
/// Returns this json type-safe wrapper's value as an <see cref="object"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonIgnore]
|
|
||||||
public abstract object Value { get; }
|
public abstract object Value { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -29,37 +29,30 @@ namespace MLEM.Data.Json {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new <see cref="JsonTypeSafeWrapper{T}"/> from the given value.
|
/// Creates a new <see cref="JsonTypeSafeWrapper{T}"/> from the given value.
|
||||||
/// The type parameter of the returned wrapper will be equal to the <see cref="Type"/> of the <paramref name="value"/> passed, even if it is a subtype of <typeparamref name="T"/>.
|
/// The type parameter of the returned wrapper will be equal to the <see cref="Type"/> of the <paramref name="value"/> passed.
|
||||||
/// If a <see cref="JsonTypeSafeWrapper{T}"/> for a specific type, known at compile type, should be created, you can use <see cref="JsonTypeSafeWrapper{T}(T)"/>.
|
/// If a <see cref="JsonTypeSafeWrapper{T}"/> for a specific type, known at comepile type, should be created, you can use <see cref="JsonTypeSafeWrapper{T}(T)"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to wrap</param>
|
/// <param name="value">The value to wrap</param>
|
||||||
/// <returns>A <see cref="JsonTypeSafeWrapper{T}"/> with a type matching the type of <paramref name="value"/></returns>
|
/// <returns>A <see cref="JsonTypeSafeWrapper{T}"/> with a type matching the type of <paramref name="value"/></returns>
|
||||||
#if NET7_0_OR_GREATER
|
public static JsonTypeSafeWrapper Of(object value) {
|
||||||
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for this instantiation might not be available at runtime if the value's type is a subtype of T.")]
|
|
||||||
#endif
|
|
||||||
public static JsonTypeSafeWrapper Of<T>(T value) {
|
|
||||||
if (value.GetType() == typeof(T)) {
|
|
||||||
return new JsonTypeSafeWrapper<T>(value);
|
|
||||||
} else {
|
|
||||||
var type = typeof(JsonTypeSafeWrapper<>).MakeGenericType(value.GetType());
|
var type = typeof(JsonTypeSafeWrapper<>).MakeGenericType(value.GetType());
|
||||||
return (JsonTypeSafeWrapper) Activator.CreateInstance(type, value);
|
return (JsonTypeSafeWrapper) Activator.CreateInstance(type, value);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
[DataContract]
|
||||||
public class JsonTypeSafeWrapper<T> : JsonTypeSafeWrapper {
|
public class JsonTypeSafeWrapper<T> : JsonTypeSafeWrapper {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override object Value => this.value;
|
public override object Value => this.value;
|
||||||
|
[DataMember]
|
||||||
[JsonProperty]
|
|
||||||
private readonly T value;
|
private readonly T value;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new json type-safe wrapper instance that wraps the given <paramref name="value"/>.
|
/// Creates a new json type-safe wrapper instance that wraps the given <paramref name="value"/>.
|
||||||
/// If the type of the value is unknown at compile time, <see cref="JsonTypeSafeWrapper.Of{T}"/> can be used instead.
|
/// If the type of the value is unknown at compile time, <see cref="JsonTypeSafeWrapper.Of"/> can be used instead.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to wrap</param>
|
/// <param name="value">The value to wrap</param>
|
||||||
public JsonTypeSafeWrapper(T value) {
|
public JsonTypeSafeWrapper(T value) {
|
||||||
|
|
|
@ -4,10 +4,6 @@ using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Data.Json {
|
namespace MLEM.Data.Json {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A <see cref="JsonConverter{T}"/> that doesn't actually serialize the object, but instead serializes the name given to it by the underlying <see cref="Dictionary{T,T}"/>.
|
/// A <see cref="JsonConverter{T}"/> that doesn't actually serialize the object, but instead serializes the name given to it by the underlying <see cref="Dictionary{T,T}"/>.
|
||||||
|
@ -33,11 +29,7 @@ namespace MLEM.Data.Json {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">The type that the dictionary is declared in</param>
|
/// <param name="type">The type that the dictionary is declared in</param>
|
||||||
/// <param name="memberName">The name of the dictionary itself</param>
|
/// <param name="memberName">The name of the dictionary itself</param>
|
||||||
public StaticJsonConverter(
|
public StaticJsonConverter(Type type, string memberName) : this(StaticJsonConverter<T>.GetEntries(type, memberName)) {}
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
|
|
||||||
#endif
|
|
||||||
Type type, string memberName) : this(StaticJsonConverter<T>.GetEntries(type, memberName)) {}
|
|
||||||
|
|
||||||
/// <summary>Writes the JSON representation of the object.</summary>
|
/// <summary>Writes the JSON representation of the object.</summary>
|
||||||
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
|
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
|
||||||
|
@ -64,11 +56,7 @@ namespace MLEM.Data.Json {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<string, T> GetEntries(
|
private static Dictionary<string, T> GetEntries(Type type, string memberName) {
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
|
|
||||||
#endif
|
|
||||||
Type type, string memberName) {
|
|
||||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
var value = type.GetProperty(memberName, flags)?.GetValue(null) ?? type.GetField(memberName, flags)?.GetValue(null);
|
var value = type.GetProperty(memberName, flags)?.GetValue(null) ?? type.GetField(memberName, flags)?.GetValue(null);
|
||||||
if (value == null)
|
if (value == null)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<RootNamespace>MLEM.Data</RootNamespace>
|
<RootNamespace>MLEM.Data</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
<NoWarn>NU1701</NoWarn>
|
<NoWarn>NU1701</NoWarn>
|
||||||
|
@ -28,14 +27,13 @@
|
||||||
<PackageReference Include="Lidgren.Network" Version="1.0.2">
|
<PackageReference Include="Lidgren.Network" Version="1.0.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2">
|
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<ProjectReference Include="..\FNA\FNA.Core.csproj">
|
||||||
<ProjectReference Include="..\FNA\FNA.csproj">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<NoWarn>NU1701</NoWarn>
|
<NoWarn>NU1701</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@
|
||||||
<PackageReference Include="Lidgren.Network" Version="1.0.2">
|
<PackageReference Include="Lidgren.Network" Version="1.0.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2">
|
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2">
|
||||||
|
|
|
@ -11,9 +11,6 @@ namespace MLEM.Data {
|
||||||
/// Before serializing and deserializing an object, each of the object's fields has to have a handler. New handlers can be added using <see cref="AddHandler{T}(System.Action{Lidgren.Network.NetBuffer,T},System.Func{Lidgren.Network.NetBuffer,T})"/> or <see cref="AddHandler{T}(Newtonsoft.Json.JsonSerializer)"/>.
|
/// Before serializing and deserializing an object, each of the object's fields has to have a handler. New handlers can be added using <see cref="AddHandler{T}(System.Action{Lidgren.Network.NetBuffer,T},System.Func{Lidgren.Network.NetBuffer,T})"/> or <see cref="AddHandler{T}(Newtonsoft.Json.JsonSerializer)"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
[Obsolete("Lidgren.Network support is deprecated. Consider using LiteNetLib or a custom implementation instead.")]
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Aot", "IL2070")]
|
|
||||||
#endif
|
|
||||||
public class NetBufferSerializer {
|
public class NetBufferSerializer {
|
||||||
|
|
||||||
private readonly Dictionary<Type, Action<NetBuffer, object>> writeFunctions = new Dictionary<Type, Action<NetBuffer, object>>();
|
private readonly Dictionary<Type, Action<NetBuffer, object>> writeFunctions = new Dictionary<Type, Action<NetBuffer, object>>();
|
||||||
|
|
|
@ -35,8 +35,8 @@ namespace MLEM.Data {
|
||||||
public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime;
|
public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime;
|
||||||
|
|
||||||
private readonly List<Request> texturesToPack = new List<Request>();
|
private readonly List<Request> texturesToPack = new List<Request>();
|
||||||
private readonly List<Request> packedTextures = new List<Request>();
|
private readonly List<Request> alreadyPackedTextures = new List<Request>();
|
||||||
private readonly Dictionary<Point, Point> firstPossiblePosForSize = new Dictionary<Point, Point>();
|
private readonly Dictionary<Point, Point> firstPossiblePosForSizeCache = new Dictionary<Point, Point>();
|
||||||
private readonly Dictionary<Texture2D, TextureData> dataCache = new Dictionary<Texture2D, TextureData>();
|
private readonly Dictionary<Texture2D, TextureData> dataCache = new Dictionary<Texture2D, TextureData>();
|
||||||
private readonly bool autoIncreaseMaxWidth;
|
private readonly bool autoIncreaseMaxWidth;
|
||||||
private readonly bool forcePowerOfTwo;
|
private readonly bool forcePowerOfTwo;
|
||||||
|
@ -71,7 +71,7 @@ namespace MLEM.Data {
|
||||||
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
||||||
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
||||||
/// <param name="ignoreTransparent">Whether completely transparent texture regions in the <paramref name="atlas"/> should be ignored. If this is true, they will not be part of the <paramref name="result"/> collection either.</param>
|
/// <param name="ignoreTransparent">Whether completely transparent texture regions in the <paramref name="atlas"/> should be ignored. If this is true, they will not be part of the <paramref name="result"/> collection either.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when trying to add a texture width a width greater than the defined max width.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when trying to add data to a packer that has already been packed, or when trying to add a texture width a width greater than the defined max width.</exception>
|
||||||
public void Add(UniformTextureAtlas atlas, Action<Dictionary<Point, TextureRegion>> result, int padding = 0, bool padWithPixels = false, bool ignoreTransparent = false) {
|
public void Add(UniformTextureAtlas atlas, Action<Dictionary<Point, TextureRegion>> result, int padding = 0, bool padWithPixels = false, bool ignoreTransparent = false) {
|
||||||
var addedRegions = new List<TextureRegion>();
|
var addedRegions = new List<TextureRegion>();
|
||||||
var resultRegions = new Dictionary<Point, TextureRegion>();
|
var resultRegions = new Dictionary<Point, TextureRegion>();
|
||||||
|
@ -104,7 +104,7 @@ namespace MLEM.Data {
|
||||||
/// <param name="result">The result callback which will receive the resulting texture regions.</param>
|
/// <param name="result">The result callback which will receive the resulting texture regions.</param>
|
||||||
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
||||||
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when trying to add a texture width a width greater than the defined max width.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when trying to add data to a packer that has already been packed, or when trying to add a texture width a width greater than the defined max width.</exception>
|
||||||
public void Add(DataTextureAtlas atlas, Action<Dictionary<string, TextureRegion>> result, int padding = 0, bool padWithPixels = false) {
|
public void Add(DataTextureAtlas atlas, Action<Dictionary<string, TextureRegion>> result, int padding = 0, bool padWithPixels = false) {
|
||||||
var atlasRegions = atlas.RegionNames.ToArray();
|
var atlasRegions = atlas.RegionNames.ToArray();
|
||||||
var resultRegions = new Dictionary<string, TextureRegion>();
|
var resultRegions = new Dictionary<string, TextureRegion>();
|
||||||
|
@ -125,7 +125,7 @@ namespace MLEM.Data {
|
||||||
/// <param name="result">The result callback which will receive the resulting texture region.</param>
|
/// <param name="result">The result callback which will receive the resulting texture region.</param>
|
||||||
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
||||||
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when trying to add a texture width a width greater than the defined max width.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when trying to add data to a packer that has already been packed, or when trying to add a texture width a width greater than the defined max width.</exception>
|
||||||
public void Add(Texture2D texture, Action<TextureRegion> result, int padding = 0, bool padWithPixels = false) {
|
public void Add(Texture2D texture, Action<TextureRegion> result, int padding = 0, bool padWithPixels = false) {
|
||||||
this.Add(new TextureRegion(texture), result, padding, padWithPixels);
|
this.Add(new TextureRegion(texture), result, padding, padWithPixels);
|
||||||
}
|
}
|
||||||
|
@ -138,8 +138,10 @@ namespace MLEM.Data {
|
||||||
/// <param name="result">The result callback which will receive the resulting texture region.</param>
|
/// <param name="result">The result callback which will receive the resulting texture region.</param>
|
||||||
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
/// <param name="padding">The padding that the texture should have around itself. This can be useful if texture bleeding issues occur due to texture coordinate rounding.</param>
|
||||||
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
/// <param name="padWithPixels">Whether the texture's padding should be filled with a copy of the texture's border, rather than transparent pixels. This value only has an effect if <paramref name="padding"/> is greater than 0.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when trying to add a texture width a width greater than the defined max width.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when trying to add data to a packer that has already been packed, or when trying to add a texture width a width greater than the defined max width.</exception>
|
||||||
public void Add(TextureRegion texture, Action<TextureRegion> result, int padding = 0, bool padWithPixels = false) {
|
public void Add(TextureRegion texture, Action<TextureRegion> result, int padding = 0, bool padWithPixels = false) {
|
||||||
|
if (this.PackedTexture != null)
|
||||||
|
throw new InvalidOperationException("Cannot add texture to a texture packer that is already packed");
|
||||||
var paddedWidth = texture.Width + 2 * padding;
|
var paddedWidth = texture.Width + 2 * padding;
|
||||||
if (paddedWidth > this.maxWidth) {
|
if (paddedWidth > this.maxWidth) {
|
||||||
if (this.autoIncreaseMaxWidth) {
|
if (this.autoIncreaseMaxWidth) {
|
||||||
|
@ -152,80 +154,68 @@ namespace MLEM.Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Packs all of the textures and texture regions added using <see cref="Add(Microsoft.Xna.Framework.Graphics.Texture2D,System.Action{MLEM.Textures.TextureRegion},int,bool)"/> into one texture, which can be retrieved using <see cref="PackedTexture"/>.
|
/// Packs all of the textures and texture regions added using <see cref="Add(Microsoft.Xna.Framework.Graphics.Texture2D,System.Action{MLEM.Textures.TextureRegion},int,bool)"/> into one texture.
|
||||||
|
/// The resulting texture will be stored in <see cref="PackedTexture"/>.
|
||||||
/// All of the result callbacks that were added will also be invoked.
|
/// All of the result callbacks that were added will also be invoked.
|
||||||
/// This method can be called multiple times if regions are added after <see cref="Pack"/> has already been called. When doing so, result callbacks of previous regions may be invoked again if the resulting <see cref="PackedTexture"/> has to be resized to accommodate newly added regions.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="device">The graphics device to use for texture generation</param>
|
/// <param name="device">The graphics device to use for texture generation</param>
|
||||||
|
/// <exception cref="InvalidOperationException">Thrown when calling this method on a texture packer that has already been packed</exception>
|
||||||
public void Pack(GraphicsDevice device) {
|
public void Pack(GraphicsDevice device) {
|
||||||
|
if (this.PackedTexture != null)
|
||||||
|
throw new InvalidOperationException("Cannot pack a texture packer that is already packed");
|
||||||
|
|
||||||
// set pack areas for each request
|
// set pack areas for each request
|
||||||
// we pack larger textures first, so that smaller textures can fit in the gaps that larger ones leave
|
// we pack larger textures first, so that smaller textures can fit in the gaps that larger ones leave
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
foreach (var request in this.texturesToPack.OrderByDescending(t => t.Texture.Width * t.Texture.Height)) {
|
foreach (var request in this.texturesToPack.OrderByDescending(t => t.Texture.Width * t.Texture.Height)) {
|
||||||
request.PackedArea = this.FindFreeArea(request);
|
request.PackedArea = this.FindFreeArea(request);
|
||||||
// if this is the first position that this request fit in, no other requests of the same size will find a position before it
|
// if this is the first position that this request fit in, no other requests of the same size will find a position before it
|
||||||
this.firstPossiblePosForSize[new Point(request.PackedArea.Width, request.PackedArea.Height)] = request.PackedArea.Location;
|
this.firstPossiblePosForSizeCache[new Point(request.PackedArea.Width, request.PackedArea.Height)] = request.PackedArea.Location;
|
||||||
this.packedTextures.Add(request);
|
this.alreadyPackedTextures.Add(request);
|
||||||
}
|
}
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
this.LastCalculationTime = stopwatch.Elapsed;
|
this.LastCalculationTime = stopwatch.Elapsed;
|
||||||
|
|
||||||
// figure out texture size and regenerate texture if necessary
|
// figure out texture size and generate texture
|
||||||
var width = this.packedTextures.Max(t => t.PackedArea.Right);
|
var width = this.alreadyPackedTextures.Max(t => t.PackedArea.Right);
|
||||||
var height = this.packedTextures.Max(t => t.PackedArea.Bottom);
|
var height = this.alreadyPackedTextures.Max(t => t.PackedArea.Bottom);
|
||||||
if (this.forcePowerOfTwo) {
|
if (this.forcePowerOfTwo) {
|
||||||
width = RuntimeTexturePacker.ToPowerOfTwo(width);
|
width = RuntimeTexturePacker.ToPowerOfTwo(width);
|
||||||
height = RuntimeTexturePacker.ToPowerOfTwo(height);
|
height = RuntimeTexturePacker.ToPowerOfTwo(height);
|
||||||
}
|
}
|
||||||
if (this.forceSquare)
|
if (this.forceSquare)
|
||||||
width = height = Math.Max(width, height);
|
width = height = Math.Max(width, height);
|
||||||
|
|
||||||
// if we don't need to regenerate, we only need to add newly added regions
|
|
||||||
IEnumerable<Request> texturesToCopy = this.texturesToPack;
|
|
||||||
if (this.PackedTexture == null || this.PackedTexture.Width != width || this.PackedTexture.Height != height) {
|
|
||||||
this.PackedTexture?.Dispose();
|
|
||||||
this.PackedTexture = new Texture2D(device, width, height);
|
this.PackedTexture = new Texture2D(device, width, height);
|
||||||
// if we need to regenerate, we need to copy all regions since the old ones were deleted
|
|
||||||
texturesToCopy = this.packedTextures;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy texture data onto the packed texture
|
// copy texture data onto the packed texture
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
using (var data = this.PackedTexture.GetTextureData()) {
|
using (var data = this.PackedTexture.GetTextureData()) {
|
||||||
foreach (var request in texturesToCopy)
|
foreach (var request in this.alreadyPackedTextures)
|
||||||
this.CopyRegion(data, request);
|
this.CopyRegion(data, request);
|
||||||
}
|
}
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
this.LastPackTime = stopwatch.Elapsed;
|
this.LastPackTime = stopwatch.Elapsed;
|
||||||
|
|
||||||
// invoke callbacks for textures we copied
|
// invoke callbacks
|
||||||
foreach (var request in texturesToCopy) {
|
foreach (var request in this.alreadyPackedTextures) {
|
||||||
var packedArea = request.PackedArea.Shrink(new Point(request.Padding, request.Padding));
|
var packedArea = request.PackedArea.Shrink(new Point(request.Padding, request.Padding));
|
||||||
request.Result.Invoke(new TextureRegion(this.PackedTexture, packedArea) {
|
request.Result.Invoke(new TextureRegion(this.PackedTexture, packedArea));
|
||||||
Pivot = request.Texture.Pivot,
|
|
||||||
Name = request.Texture.Name,
|
|
||||||
Source = request.Texture
|
|
||||||
});
|
|
||||||
if (this.disposeTextures)
|
if (this.disposeTextures)
|
||||||
request.Texture.Texture.Dispose();
|
request.Texture.Texture.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.texturesToPack.Clear();
|
this.ClearTempCollections();
|
||||||
this.dataCache.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets this texture packer entirely, disposing its <see cref="PackedTexture"/>, clearing all previously added requests, and readying it to be re-used.
|
/// Resets this texture packer, disposing its <see cref="PackedTexture"/> and readying it to be re-used
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Reset() {
|
public void Reset() {
|
||||||
this.PackedTexture?.Dispose();
|
this.PackedTexture?.Dispose();
|
||||||
this.PackedTexture = null;
|
this.PackedTexture = null;
|
||||||
this.LastCalculationTime = TimeSpan.Zero;
|
this.LastCalculationTime = TimeSpan.Zero;
|
||||||
this.LastPackTime = TimeSpan.Zero;
|
this.LastPackTime = TimeSpan.Zero;
|
||||||
this.texturesToPack.Clear();
|
this.ClearTempCollections();
|
||||||
this.packedTextures.Clear();
|
|
||||||
this.firstPossiblePosForSize.Clear();
|
|
||||||
this.dataCache.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
||||||
|
@ -238,12 +228,12 @@ namespace MLEM.Data {
|
||||||
size.X += request.Padding * 2;
|
size.X += request.Padding * 2;
|
||||||
size.Y += request.Padding * 2;
|
size.Y += request.Padding * 2;
|
||||||
|
|
||||||
var pos = this.firstPossiblePosForSize.TryGetValue(size, out var first) ? first : Point.Zero;
|
var pos = this.firstPossiblePosForSizeCache.TryGetValue(size, out var first) ? first : Point.Zero;
|
||||||
var lowestY = int.MaxValue;
|
var lowestY = int.MaxValue;
|
||||||
while (true) {
|
while (true) {
|
||||||
var intersected = false;
|
var intersected = false;
|
||||||
var area = new Rectangle(pos.X, pos.Y, size.X, size.Y);
|
var area = new Rectangle(pos.X, pos.Y, size.X, size.Y);
|
||||||
foreach (var tex in this.packedTextures) {
|
foreach (var tex in this.alreadyPackedTextures) {
|
||||||
if (tex.PackedArea.Intersects(area)) {
|
if (tex.PackedArea.Intersects(area)) {
|
||||||
pos.X = tex.PackedArea.Right;
|
pos.X = tex.PackedArea.Right;
|
||||||
// when we move down, we want to move down by the smallest intersecting texture's height
|
// when we move down, we want to move down by the smallest intersecting texture's height
|
||||||
|
@ -274,7 +264,7 @@ namespace MLEM.Data {
|
||||||
srcColor = Color.Transparent;
|
srcColor = Color.Transparent;
|
||||||
} else {
|
} else {
|
||||||
// otherwise, we just use the closest pixel that is actually in bounds, causing the border pixels to be doubled up
|
// otherwise, we just use the closest pixel that is actually in bounds, causing the border pixels to be doubled up
|
||||||
var src = new Point((int) MathHelper.Clamp(x, 0F, request.Texture.Width - 1), (int) MathHelper.Clamp(y, 0F, request.Texture.Height - 1));
|
var src = new Point((int) MathHelper.Clamp(x, 0, request.Texture.Width - 1), (int) MathHelper.Clamp(y, 0, request.Texture.Height - 1));
|
||||||
srcColor = data[request.Texture.Position + src];
|
srcColor = data[request.Texture.Position + src];
|
||||||
}
|
}
|
||||||
destination[location + new Point(x, y)] = srcColor;
|
destination[location + new Point(x, y)] = srcColor;
|
||||||
|
@ -303,6 +293,13 @@ namespace MLEM.Data {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ClearTempCollections() {
|
||||||
|
this.texturesToPack.Clear();
|
||||||
|
this.alreadyPackedTextures.Clear();
|
||||||
|
this.firstPossiblePosForSizeCache.Clear();
|
||||||
|
this.dataCache.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
private static int ToPowerOfTwo(int value) {
|
private static int ToPowerOfTwo(int value) {
|
||||||
var ret = 1;
|
var ret = 1;
|
||||||
while (ret < value)
|
while (ret < value)
|
||||||
|
|
|
@ -40,53 +40,5 @@ namespace MLEM.Extended.Extensions {
|
||||||
return rect.ToMlem().Penetrate(other.ToMlem(), out normal, out penetration);
|
return rect.ToMlem().Penetrate(other.ToMlem(), out normal, out penetration);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns how far between the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> value the given <paramref name="value"/> is, as a number between 0 and 1.
|
|
||||||
/// Note that, if the <paramref name="value"/> is outside the given <paramref name="range"/>, a correct proportional value outside the 0 to 1 range will still be returned.
|
|
||||||
/// This method is the reverse action of <see cref="FromPercentage(MonoGame.Extended.Range{float},float)"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="range">The range to query.</param>
|
|
||||||
/// <param name="value">The value to query.</param>
|
|
||||||
/// <returns>The percentage.</returns>
|
|
||||||
public static float GetPercentage(this Range<float> range, float value) {
|
|
||||||
return (value - range.Min) / (range.Max - range.Min);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns how far between the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> value the given <paramref name="value"/> is, as a number between 0 and 1.
|
|
||||||
/// Note that, if the <paramref name="value"/> is outside the given <paramref name="range"/>, a correct proportional value outside the 0 to 1 range will still be returned.
|
|
||||||
/// This method is the reverse action of <see cref="FromPercentage(MonoGame.Extended.Range{int},float)"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="range">The range to query.</param>
|
|
||||||
/// <param name="value">The value to query.</param>
|
|
||||||
/// <returns>The percentage.</returns>
|
|
||||||
public static float GetPercentage(this Range<int> range, float value) {
|
|
||||||
return (value - range.Min) / (range.Max - range.Min);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a value within the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> values based on the given <paramref name="percentage"/> into the range.
|
|
||||||
/// Note that, if the <paramref name="percentage"/> is outside the 0 to 1 range, a correct value outside the <paramref name="range"/> will still be returned.
|
|
||||||
/// This method is the reverse action of <see cref="GetPercentage(MonoGame.Extended.Range{float},float)"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="range">The range to query.</param>
|
|
||||||
/// <param name="percentage">The percentage to query.</param>
|
|
||||||
/// <returns>The value.</returns>
|
|
||||||
public static float FromPercentage(this Range<float> range, float percentage) {
|
|
||||||
return (range.Max - range.Min) * percentage + range.Min;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a value within the given <paramref name="range"/>'s <see cref="Range{T}.Min"/> and <see cref="Range{T}.Max"/> values based on the given <paramref name="percentage"/> into the range.
|
|
||||||
/// Note that, if the <paramref name="percentage"/> is outside the 0 to 1 range, a correct value outside the <paramref name="range"/> will still be returned.
|
|
||||||
/// This method is the reverse action of <see cref="GetPercentage(MonoGame.Extended.Range{int},float)"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="range">The range to query.</param>
|
|
||||||
/// <param name="percentage">The percentage to query.</param>
|
|
||||||
/// <returns>The value.</returns>
|
|
||||||
public static float FromPercentage(this Range<int> range, float percentage) {
|
|
||||||
return (range.Max - range.Min) * percentage + range.Min;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,14 +32,14 @@ namespace MLEM.Extended.Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override float MeasureCharacter(int codePoint) {
|
protected override float MeasureChar(char c) {
|
||||||
var region = this.Font.GetCharacterRegion(codePoint);
|
var region = this.Font.GetCharacterRegion(c);
|
||||||
return region != null ? new Vector2(region.XAdvance, region.Height).X : 0;
|
return region != null ? new Vector2(region.XAdvance, region.Height).X : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, character, position, color, rotation, Vector2.Zero, scale, effects, layerDepth);
|
batch.DrawString(this.Font, cString, position, color, rotation, Vector2.Zero, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using FontStashSharp;
|
using FontStashSharp;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
|
|
||||||
namespace MLEM.Extended.Font {
|
namespace MLEM.Extended.Font {
|
||||||
|
@ -32,13 +33,13 @@ namespace MLEM.Extended.Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override float MeasureCharacter(int codePoint) {
|
protected override float MeasureChar(char c) {
|
||||||
return this.Font.MeasureString(CodePointSource.ToString(codePoint)).X;
|
return this.Font.MeasureString(c.ToCachedString()).X;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
this.Font.DrawText(batch, character, position, color, scale, rotation, Vector2.Zero, layerDepth);
|
this.Font.DrawText(batch, cString, position, color, scale, rotation, Vector2.Zero, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<RootNamespace>MLEM.Extended</RootNamespace>
|
<RootNamespace>MLEM.Extended</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
<NoWarn>NU1702</NoWarn>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -24,10 +22,10 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
||||||
|
|
||||||
<ProjectReference Include="..\FontStashSharp\src\XNA\FontStashSharp.FNA.csproj">
|
<ProjectReference Include="..\FontStashSharp\src\XNA\FontStashSharp.FNA.Core.csproj">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\FNA\FNA.csproj">
|
<ProjectReference Include="..\FNA\FNA.Core.csproj">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -27,7 +26,7 @@
|
||||||
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0">
|
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="FontStashSharp.MonoGame" Version="1.2.8">
|
<PackageReference Include="FontStashSharp.MonoGame" Version="1.1.6">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
||||||
|
|
26
MLEM.FNA.sln
26
MLEM.FNA.sln
|
@ -16,11 +16,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.FNA", "Tests\Tests.FN
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Extended.FNA", "MLEM.Extended\MLEM.Extended.FNA.csproj", "{A5B22930-DF4B-4A62-93ED-A6549F7B666B}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Extended.FNA", "MLEM.Extended\MLEM.Extended.FNA.csproj", "{A5B22930-DF4B-4A62-93ED-A6549F7B666B}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FNA", "FNA\FNA.csproj", "{35253CE1-C864-4CD3-8249-4D1319748E8F}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FNA.Core", "FNA\FNA.Core.csproj", "{06459F72-CEAA-4B45-B2B1-708FC28D04F8}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FontStashSharp.FNA", "FontStashSharp\src\XNA\FontStashSharp.FNA.csproj", "{39249E92-EBF2-4951-A086-AB4951C3CCE1}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FontStashSharp.FNA.Core", "FontStashSharp\src\XNA\FontStashSharp.FNA.Core.csproj", "{0B410591-3AED-4C82-A07A-516FF493709B}"
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FNA.Core", "FNA\FNA.Core.csproj", "{458FFA5E-A1C4-4B23-A5D8-259385FEECED}"
|
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -60,17 +58,13 @@ Global
|
||||||
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Release|Any CPU.Build.0 = Release|Any CPU
|
{A5B22930-DF4B-4A62-93ED-A6549F7B666B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{35253CE1-C864-4CD3-8249-4D1319748E8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{06459F72-CEAA-4B45-B2B1-708FC28D04F8}.Debug|Any CPU.ActiveCfg = Debug|x64
|
||||||
{35253CE1-C864-4CD3-8249-4D1319748E8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{06459F72-CEAA-4B45-B2B1-708FC28D04F8}.Debug|Any CPU.Build.0 = Debug|x64
|
||||||
{35253CE1-C864-4CD3-8249-4D1319748E8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{06459F72-CEAA-4B45-B2B1-708FC28D04F8}.Release|Any CPU.ActiveCfg = Release|x64
|
||||||
{35253CE1-C864-4CD3-8249-4D1319748E8F}.Release|Any CPU.Build.0 = Release|Any CPU
|
{06459F72-CEAA-4B45-B2B1-708FC28D04F8}.Release|Any CPU.Build.0 = Release|x64
|
||||||
{39249E92-EBF2-4951-A086-AB4951C3CCE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{0B410591-3AED-4C82-A07A-516FF493709B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{39249E92-EBF2-4951-A086-AB4951C3CCE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0B410591-3AED-4C82-A07A-516FF493709B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{39249E92-EBF2-4951-A086-AB4951C3CCE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0B410591-3AED-4C82-A07A-516FF493709B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{39249E92-EBF2-4951-A086-AB4951C3CCE1}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0B410591-3AED-4C82-A07A-516FF493709B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{458FFA5E-A1C4-4B23-A5D8-259385FEECED}.Debug|Any CPU.ActiveCfg = Debug|x64
|
|
||||||
{458FFA5E-A1C4-4B23-A5D8-259385FEECED}.Debug|Any CPU.Build.0 = Debug|x64
|
|
||||||
{458FFA5E-A1C4-4B23-A5D8-259385FEECED}.Release|Any CPU.ActiveCfg = Release|x64
|
|
||||||
{458FFA5E-A1C4-4B23-A5D8-259385FEECED}.Release|Any CPU.Build.0 = Release|x64
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<RootNamespace>MLEM.Startup</RootNamespace>
|
<RootNamespace>MLEM.Startup</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -22,11 +21,11 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Coroutine" Version="2.1.4" />
|
<PackageReference Include="Coroutine" Version="2.1.3" />
|
||||||
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.FNA.csproj" />
|
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.FNA.csproj" />
|
||||||
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
||||||
|
|
||||||
<ProjectReference Include="..\FNA\FNA.csproj">
|
<ProjectReference Include="..\FNA\FNA.Core.csproj">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -20,7 +19,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Coroutine" Version="2.1.4" />
|
<PackageReference Include="Coroutine" Version="2.1.3" />
|
||||||
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
|
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
|
||||||
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<IncludeContentInPack>true</IncludeContentInPack>
|
<IncludeContentInPack>true</IncludeContentInPack>
|
||||||
<IncludeBuildOutput>false</IncludeBuildOutput>
|
<IncludeBuildOutput>false</IncludeBuildOutput>
|
||||||
<ContentTargetFolders>content</ContentTargetFolders>
|
<ContentTargetFolders>content</ContentTargetFolders>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<NoWarn>NU5128</NoWarn>
|
<NoWarn>NU5128</NoWarn>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Graphics;
|
using MLEM.Graphics;
|
||||||
|
@ -52,12 +51,8 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this property to true to mark the button as disabled.
|
/// Set this property to true to mark the button as disabled.
|
||||||
/// A disabled button cannot be moused over, selected or pressed.
|
/// A disabled button cannot be moused over, selected or pressed.
|
||||||
/// If this value changes often, consider using <see cref="AutoDisableCondition"/>.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool IsDisabled {
|
public virtual bool IsDisabled { get; set; }
|
||||||
get => this.isDisabled || this.AutoDisableCondition?.Invoke(this) == true;
|
|
||||||
set => this.isDisabled = value;
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether this button's <see cref="Text"/> should be truncated if it exceeds this button's width.
|
/// Whether this button's <see cref="Text"/> should be truncated if it exceeds this button's width.
|
||||||
/// Defaults to false.
|
/// Defaults to false.
|
||||||
|
@ -74,19 +69,12 @@ namespace MLEM.Ui.Elements {
|
||||||
/// If this is true, <see cref="CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
|
/// If this is true, <see cref="CanBeSelected"/> will be able to return true even if <see cref="IsDisabled"/> is true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanSelectDisabled;
|
public bool CanSelectDisabled;
|
||||||
/// <summary>
|
|
||||||
/// An optional function that can be used to modify the result of <see cref="IsDisabled"/> automatically based on a user-defined condition. This removes the need to disable a button based on a condition in <see cref="Element.OnUpdated"/> or manually.
|
|
||||||
/// Note that, if <see cref="IsDisabled"/>'s underlying value is set to <see langword="true"/> using <see cref="set_IsDisabled"/>, this function's result will be ignored.
|
|
||||||
/// </summary>
|
|
||||||
public Func<Button, bool> AutoDisableCondition;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
|
public override bool CanBeSelected => base.CanBeSelected && (this.CanSelectDisabled || !this.IsDisabled);
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
|
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
|
||||||
|
|
||||||
private bool isDisabled;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new button with the given settings
|
/// Creates a new button with the given settings
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -46,13 +46,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The width of the space between this checkbox and its <see cref="Label"/>
|
/// The width of the space between this checkbox and its <see cref="Label"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<float> TextOffsetX {
|
public StyleProp<float> TextOffsetX;
|
||||||
get => this.textOffsetX;
|
|
||||||
set {
|
|
||||||
this.textOffsetX = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether or not this checkbox is currently checked.
|
/// Whether or not this checkbox is currently checked.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -86,7 +80,6 @@ namespace MLEM.Ui.Elements {
|
||||||
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
|
public override bool CanBePressed => base.CanBePressed && !this.IsDisabled;
|
||||||
|
|
||||||
private bool isChecked;
|
private bool isChecked;
|
||||||
private StyleProp<float> textOffsetX;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new checkbox with the given settings
|
/// Creates a new checkbox with the given settings
|
||||||
|
|
|
@ -42,16 +42,7 @@ namespace MLEM.Ui.Elements {
|
||||||
});
|
});
|
||||||
this.OnAreaUpdated += e => this.Panel.PositionOffset = new Vector2(0, e.Area.Height / this.Scale);
|
this.OnAreaUpdated += e => this.Panel.PositionOffset = new Vector2(0, e.Area.Height / this.Scale);
|
||||||
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
|
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
|
||||||
this.OnPressed += e => {
|
this.OnPressed += e => this.IsOpen = !this.IsOpen;
|
||||||
this.IsOpen = !this.IsOpen;
|
|
||||||
// close other dropdowns in the same root when we open
|
|
||||||
if (this.IsOpen) {
|
|
||||||
this.Root.Element.AndChildren(o => {
|
|
||||||
if (o != this && o is Dropdown d && d.IsOpen)
|
|
||||||
d.IsOpen = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.GetGamepadNextElement = (dir, usualNext) => {
|
this.GetGamepadNextElement = (dir, usualNext) => {
|
||||||
// Force navigate down to our first child if we're open
|
// Force navigate down to our first child if we're open
|
||||||
if (this.IsOpen && dir == Direction2.Down)
|
if (this.IsOpen && dir == Direction2.Down)
|
||||||
|
|
|
@ -10,9 +10,9 @@ using MLEM.Extensions;
|
||||||
using MLEM.Graphics;
|
using MLEM.Graphics;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using MLEM.Sound;
|
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
|
using SoundEffectInfo = MLEM.Sound.SoundEffectInfo;
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -31,7 +31,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public UiSystem System {
|
public UiSystem System {
|
||||||
get => this.system;
|
get => this.system;
|
||||||
private set {
|
internal set {
|
||||||
this.system = value;
|
this.system = value;
|
||||||
this.Controls = value?.Controls;
|
this.Controls = value?.Controls;
|
||||||
this.Style = this.Style.OrStyle(value?.Style);
|
this.Style = this.Style.OrStyle(value?.Style);
|
||||||
|
@ -50,7 +50,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// This element's <see cref="RootElement"/>.
|
/// This element's <see cref="RootElement"/>.
|
||||||
/// Note that this value is set even if this element has a <see cref="Parent"/>. To get the element that represents the root element, use <see cref="RootElement.Element"/>.
|
/// Note that this value is set even if this element has a <see cref="Parent"/>. To get the element that represents the root element, use <see cref="RootElement.Element"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RootElement Root { get; private set; }
|
public RootElement Root { get; internal set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The scale that this ui element renders with
|
/// The scale that this ui element renders with
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -71,10 +71,9 @@ namespace MLEM.Ui.Elements {
|
||||||
/// The size of this element, where X represents the width and Y represents the height.
|
/// The size of this element, where X represents the width and Y represents the height.
|
||||||
/// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value.
|
/// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value.
|
||||||
/// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width).
|
/// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width).
|
||||||
/// Additionally, if auto-sizing is used, <see cref="AutoSizeAddedAbsolute"/> can be set to add or subtract an absolute value from the automatically calculated size.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>
|
/// <example>
|
||||||
/// The following example, ignoring <see cref="Scale"/>, combines both types of percentage-based sizing.
|
/// The following example combines both types of percentage-based sizing.
|
||||||
/// If this element is inside of a <see cref="Parent"/> whose width is 20, this element's width will be set to <c>0.5 * 20 = 10</c>, and its height will be set to <c>2.5 * 10 = 25</c>.
|
/// If this element is inside of a <see cref="Parent"/> whose width is 20, this element's width will be set to <c>0.5 * 20 = 10</c>, and its height will be set to <c>2.5 * 10 = 25</c>.
|
||||||
/// <code>
|
/// <code>
|
||||||
/// element.Size = new Vector2(0.5F, -2.5F);
|
/// element.Size = new Vector2(0.5F, -2.5F);
|
||||||
|
@ -94,26 +93,6 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Vector2 ScaledSize => this.size * this.Scale;
|
public Vector2 ScaledSize => this.size * this.Scale;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If auto-sizing is used by setting <see cref="Size"/> less than or equal to 1, this property allows adding or subtracting an additional, absolute value from the automatically calculated size.
|
|
||||||
/// If this element is not using auto-sizing, this property is ignored.
|
|
||||||
/// </summary>
|
|
||||||
/// <example>
|
|
||||||
/// Ignoring <see cref="Scale"/>, if this element's <see cref="Size"/> is set to <c>0.5, 0.75</c> and its <see cref="Parent"/> has a size of <c>200, 100</c>, then an added absolute size of <c>-50, 25</c> will result in a final <see cref="Area"/> size of <c>0.5 * 200 - 50, 0.75 * 100 + 25</c>, or <c>50, 100</c>.
|
|
||||||
/// </example>
|
|
||||||
public Vector2 AutoSizeAddedAbsolute {
|
|
||||||
get => this.autoSizeAddedAbsolute;
|
|
||||||
set {
|
|
||||||
if (this.autoSizeAddedAbsolute == value)
|
|
||||||
return;
|
|
||||||
this.autoSizeAddedAbsolute = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// The <see cref="AutoSizeAddedAbsolute"/>, but with <see cref="Scale"/> applied.
|
|
||||||
/// </summary>
|
|
||||||
public Vector2 ScaledAutoSizeAddedAbsolute => this.autoSizeAddedAbsolute * this.Scale;
|
|
||||||
/// <summary>
|
|
||||||
/// This element's offset from its default position, which is dictated by its <see cref="Anchor"/>.
|
/// This element's offset from its default position, which is dictated by its <see cref="Anchor"/>.
|
||||||
/// Note that, depending on the side that the element is anchored to, this offset moves it in a different direction.
|
/// Note that, depending on the side that the element is anchored to, this offset moves it in a different direction.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -233,82 +212,34 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this field to false to cause auto-anchored siblings to ignore this element as a possible anchor point.
|
/// Set this field to false to cause auto-anchored siblings to ignore this element as a possible anchor point.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool CanAutoAnchorsAttach {
|
public virtual bool CanAutoAnchorsAttach { get; set; } = true;
|
||||||
get => this.canAutoAnchorsAttach;
|
|
||||||
set {
|
|
||||||
if (this.canAutoAnchorsAttach != value) {
|
|
||||||
this.canAutoAnchorsAttach = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this field to true to cause this element's width to be automatically calculated based on the area that its <see cref="Children"/> take up.
|
/// Set this field to true to cause this element's width to be automatically calculated based on the area that its <see cref="Children"/> take up.
|
||||||
/// To use this element's <see cref="Size"/>'s X coordinate as a minimum or maximum width rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
|
/// To use this element's <see cref="Size"/>'s X coordinate as a minimum or maximum width rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool SetWidthBasedOnChildren {
|
public virtual bool SetWidthBasedOnChildren { get; set; }
|
||||||
get => this.setWidthBasedOnChildren;
|
|
||||||
set {
|
|
||||||
if (this.setWidthBasedOnChildren != value) {
|
|
||||||
this.setWidthBasedOnChildren = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this field to true to cause this element's height to be automatically calculated based on the area that its <see cref="Children"/> take up.
|
/// Set this field to true to cause this element's height to be automatically calculated based on the area that its <see cref="Children"/> take up.
|
||||||
/// To use this element's <see cref="Size"/>'s Y coordinate as a minimum or maximum height rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
|
/// To use this element's <see cref="Size"/>'s Y coordinate as a minimum or maximum height rather than ignoring it, set <see cref="TreatSizeAsMinimum"/> or <see cref="TreatSizeAsMaximum"/> to true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool SetHeightBasedOnChildren {
|
public virtual bool SetHeightBasedOnChildren { get; set; }
|
||||||
get => this.setHeightBasedOnChildren;
|
|
||||||
set {
|
|
||||||
if (this.setHeightBasedOnChildren != value) {
|
|
||||||
this.setHeightBasedOnChildren = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled, the resulting width or height will always be greather than or equal to this element's <see cref="Size"/>.
|
/// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled, the resulting width or height will always be greather than or equal to this element's <see cref="Size"/>.
|
||||||
/// For example, if an element's <see cref="Size"/>'s Y coordinate is set to 20, but there is only one child with a height of 10 in it, the element's height would be shrunk to 10 if this value was false, but would remain at 20 if it was true.
|
/// For example, if an element's <see cref="Size"/>'s Y coordinate is set to 20, but there is only one child with a height of 10 in it, the element's height would be shrunk to 10 if this value was false, but would remain at 20 if it was true.
|
||||||
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
|
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool TreatSizeAsMinimum {
|
public virtual bool TreatSizeAsMinimum { get; set; }
|
||||||
get => this.treatSizeAsMinimum;
|
|
||||||
set {
|
|
||||||
if (this.treatSizeAsMinimum != value) {
|
|
||||||
this.treatSizeAsMinimum = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/>are enabled, the resulting width or height weill always be less than or equal to this element's <see cref="Size"/>.
|
/// If this field is set to true, and <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/>are enabled, the resulting width or height weill always be less than or equal to this element's <see cref="Size"/>.
|
||||||
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
|
/// Note that this value only has an effect if <see cref="SetWidthBasedOnChildren"/> or <see cref="SetHeightBasedOnChildren"/> are enabled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool TreatSizeAsMaximum {
|
public virtual bool TreatSizeAsMaximum { get; set; }
|
||||||
get => this.treatSizeAsMaximum;
|
|
||||||
set {
|
|
||||||
if (this.treatSizeAsMaximum != value) {
|
|
||||||
this.treatSizeAsMaximum = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set this field to true to cause this element's final display area to never exceed that of its <see cref="Parent"/>.
|
/// Set this field to true to cause this element's final display area to never exceed that of its <see cref="Parent"/>.
|
||||||
/// If the resulting area is too large, the size of this element is shrunk to fit the target area.
|
/// If the resulting area is too large, the size of this element is shrunk to fit the target area.
|
||||||
/// This can be useful if an element should fill the remaining area of a parent exactly.
|
/// This can be useful if an element should fill the remaining area of a parent exactly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool PreventParentSpill {
|
public virtual bool PreventParentSpill { get; set; }
|
||||||
get => this.preventParentSpill;
|
|
||||||
set {
|
|
||||||
if (this.preventParentSpill != value) {
|
|
||||||
this.preventParentSpill = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The transparency (alpha value) that this element is rendered with.
|
/// The transparency (alpha value) that this element is rendered with.
|
||||||
/// Note that, when <see cref="Draw(Microsoft.Xna.Framework.GameTime,Microsoft.Xna.Framework.Graphics.SpriteBatch,float,MLEM.Graphics.SpriteBatchContext)"/> is called, this alpha value is multiplied with the <see cref="Parent"/>'s alpha value and passed down to this element's <see cref="Children"/>.
|
/// Note that, when <see cref="Draw(Microsoft.Xna.Framework.GameTime,Microsoft.Xna.Framework.Graphics.SpriteBatch,float,MLEM.Graphics.SpriteBatchContext)"/> is called, this alpha value is multiplied with the <see cref="Parent"/>'s alpha value and passed down to this element's <see cref="Children"/>.
|
||||||
|
@ -456,27 +387,16 @@ namespace MLEM.Ui.Elements {
|
||||||
public GamepadNextElementCallback GetGamepadNextElement;
|
public GamepadNextElementCallback GetGamepadNextElement;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that is called when a child is added to this element using <see cref="AddChild{T}"/>
|
/// Event that is called when a child is added to this element using <see cref="AddChild{T}"/>
|
||||||
/// Note that, while this event is only called for immediate children of this element, <see cref="RootElement.OnElementAdded"/> is called for all children and grandchildren.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public OtherElementCallback OnChildAdded;
|
public OtherElementCallback OnChildAdded;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that is called when a child is removed from this element using <see cref="RemoveChild"/>.
|
/// Event that is called when a child is removed from this element using <see cref="RemoveChild"/>
|
||||||
/// Note that, while this event is only called for immediate children of this element, <see cref="RootElement.OnElementRemoved"/> is called for all children and grandchildren.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public OtherElementCallback OnChildRemoved;
|
public OtherElementCallback OnChildRemoved;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that is called when this element is added to a <see cref="UiSystem"/>, that is, when this element's <see cref="System"/> is set to a non-<see langword="null"/> value.
|
/// Event that is called when this element's <see cref="Dispose"/> method is called, which also happens in <see cref="Finalize"/>.
|
||||||
/// </summary>
|
|
||||||
public GenericCallback OnAddedToUi;
|
|
||||||
/// <summary>
|
|
||||||
/// Event that is called when this element is removed from a <see cref="UiSystem"/>, that is, when this element's <see cref="System"/> is set to <see langword="null"/>.
|
|
||||||
/// </summary>
|
|
||||||
public GenericCallback OnRemovedFromUi;
|
|
||||||
/// <summary>
|
|
||||||
/// Event that is called when this element's <see cref="Dispose"/> method is called.
|
|
||||||
/// This event is useful for unregistering global event handlers when this object should be destroyed.
|
/// This event is useful for unregistering global event handlers when this object should be destroyed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("OnDisposed will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
|
||||||
public GenericCallback OnDisposed;
|
public GenericCallback OnDisposed;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -501,7 +421,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// The <see cref="ChildPaddedArea"/> of this element's <see cref="Parent"/>, or the <see cref="UiSystem.Viewport"/> if this element has no parent.
|
/// The <see cref="ChildPaddedArea"/> of this element's <see cref="Parent"/>, or the <see cref="UiSystem.Viewport"/> if this element has no parent.
|
||||||
/// This value is the one that is passed to <see cref="CalcActualSize"/> during <see cref="ForceUpdateArea"/>.
|
/// This value is the one that is passed to <see cref="CalcActualSize"/> during <see cref="ForceUpdateArea"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected RectangleF ParentArea => this.Parent?.ChildPaddedArea ?? (RectangleF) this.System.Viewport;
|
protected RectangleF ParentArea => this.Parent?.ChildPaddedArea ?? (RectangleF) this.system.Viewport;
|
||||||
|
|
||||||
private readonly List<Element> children = new List<Element>();
|
private readonly List<Element> children = new List<Element>();
|
||||||
private readonly Stopwatch stopwatch = new Stopwatch();
|
private readonly Stopwatch stopwatch = new Stopwatch();
|
||||||
|
@ -510,7 +430,6 @@ namespace MLEM.Ui.Elements {
|
||||||
private UiSystem system;
|
private UiSystem system;
|
||||||
private Anchor anchor;
|
private Anchor anchor;
|
||||||
private Vector2 size;
|
private Vector2 size;
|
||||||
private Vector2 autoSizeAddedAbsolute;
|
|
||||||
private Vector2 offset;
|
private Vector2 offset;
|
||||||
private RectangleF area;
|
private RectangleF area;
|
||||||
private bool isHidden;
|
private bool isHidden;
|
||||||
|
@ -518,12 +437,6 @@ namespace MLEM.Ui.Elements {
|
||||||
private StyleProp<UiStyle> style;
|
private StyleProp<UiStyle> style;
|
||||||
private StyleProp<Padding> childPadding;
|
private StyleProp<Padding> childPadding;
|
||||||
private bool canBeSelected = true;
|
private bool canBeSelected = true;
|
||||||
private bool canAutoAnchorsAttach = true;
|
|
||||||
private bool setWidthBasedOnChildren;
|
|
||||||
private bool setHeightBasedOnChildren;
|
|
||||||
private bool treatSizeAsMinimum;
|
|
||||||
private bool treatSizeAsMaximum;
|
|
||||||
private bool preventParentSpill;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new element with the given anchor and size and sets up some default event reactions.
|
/// Creates a new element with the given anchor and size and sets up some default event reactions.
|
||||||
|
@ -535,15 +448,14 @@ namespace MLEM.Ui.Elements {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
this.Children = new ReadOnlyCollection<Element>(this.children);
|
this.Children = new ReadOnlyCollection<Element>(this.children);
|
||||||
this.GetTabNextElement = (backward, next) => next;
|
this.GetTabNextElement += (backward, next) => next;
|
||||||
this.GetGamepadNextElement = (dir, next) => next;
|
this.GetGamepadNextElement += (dir, next) => next;
|
||||||
|
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[Obsolete("Dispose will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
|
||||||
~Element() {
|
~Element() {
|
||||||
this.Dispose();
|
this.Dispose();
|
||||||
}
|
}
|
||||||
|
@ -560,8 +472,12 @@ namespace MLEM.Ui.Elements {
|
||||||
index = this.children.Count;
|
index = this.children.Count;
|
||||||
this.children.Insert(index, element);
|
this.children.Insert(index, element);
|
||||||
element.Parent = this;
|
element.Parent = this;
|
||||||
element.AndChildren(e => e.AddedToUi(this.System, this.Root));
|
element.AndChildren(e => {
|
||||||
this.OnChildAdded?.Invoke(this, element);
|
e.Root = this.Root;
|
||||||
|
e.System = this.System;
|
||||||
|
this.Root?.InvokeOnElementAdded(e);
|
||||||
|
this.OnChildAdded?.Invoke(this, e);
|
||||||
|
});
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
element.SetAreaDirty();
|
element.SetAreaDirty();
|
||||||
return element;
|
return element;
|
||||||
|
@ -579,8 +495,12 @@ namespace MLEM.Ui.Elements {
|
||||||
// upwards to us if the element is auto-positioned
|
// upwards to us if the element is auto-positioned
|
||||||
element.SetAreaDirty();
|
element.SetAreaDirty();
|
||||||
element.Parent = null;
|
element.Parent = null;
|
||||||
element.AndChildren(e => e.RemovedFromUi());
|
element.AndChildren(e => {
|
||||||
this.OnChildRemoved?.Invoke(this, element);
|
e.Root = null;
|
||||||
|
e.System = null;
|
||||||
|
this.Root?.InvokeOnElementRemoved(e);
|
||||||
|
this.OnChildRemoved?.Invoke(this, e);
|
||||||
|
});
|
||||||
this.SetSortedChildrenDirty();
|
this.SetSortedChildrenDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -647,7 +567,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void ForceUpdateArea() {
|
public virtual void ForceUpdateArea() {
|
||||||
this.AreaDirty = false;
|
this.AreaDirty = false;
|
||||||
if (this.IsHidden || this.System == null)
|
if (this.IsHidden)
|
||||||
return;
|
return;
|
||||||
// if the parent's area is dirty, it would get updated anyway when querying its ChildPaddedArea,
|
// if the parent's area is dirty, it would get updated anyway when querying its ChildPaddedArea,
|
||||||
// which would cause our ForceUpdateArea code to be run twice, so we only update our parent instead
|
// which would cause our ForceUpdateArea code to be run twice, so we only update our parent instead
|
||||||
|
@ -713,7 +633,7 @@ namespace MLEM.Ui.Elements {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.Anchor.IsAuto()) {
|
if (this.Anchor >= Anchor.AutoLeft) {
|
||||||
Element previousChild;
|
Element previousChild;
|
||||||
if (this.Anchor == Anchor.AutoInline || this.Anchor == Anchor.AutoInlineIgnoreOverflow) {
|
if (this.Anchor == Anchor.AutoInline || this.Anchor == Anchor.AutoInlineIgnoreOverflow) {
|
||||||
previousChild = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach);
|
previousChild = this.GetOlderSibling(e => !e.IsHidden && e.CanAutoAnchorsAttach);
|
||||||
|
@ -767,13 +687,11 @@ namespace MLEM.Ui.Elements {
|
||||||
if (this.SetHeightBasedOnChildren) {
|
if (this.SetHeightBasedOnChildren) {
|
||||||
var lowest = this.GetLowestChild(e => !e.IsHidden);
|
var lowest = this.GetLowestChild(e => !e.IsHidden);
|
||||||
if (lowest != null) {
|
if (lowest != null) {
|
||||||
if (lowest.Anchor.IsTopAligned()) {
|
|
||||||
autoSize.Y = lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom;
|
autoSize.Y = lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom;
|
||||||
} else {
|
|
||||||
autoSize.Y = lowest.UnscrolledArea.Height + this.ScaledChildPadding.Height;
|
|
||||||
}
|
|
||||||
foundChild = lowest;
|
foundChild = lowest;
|
||||||
} else {
|
} else {
|
||||||
|
if (this.Children.Any(e => !e.IsHidden))
|
||||||
|
throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its height based on children but it only has visible children anchored too low ({string.Join(", ", this.Children.Where(c => !c.IsHidden).Select(c => c.Anchor))})");
|
||||||
autoSize.Y = 0;
|
autoSize.Y = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -781,13 +699,11 @@ namespace MLEM.Ui.Elements {
|
||||||
if (this.SetWidthBasedOnChildren) {
|
if (this.SetWidthBasedOnChildren) {
|
||||||
var rightmost = this.GetRightmostChild(e => !e.IsHidden);
|
var rightmost = this.GetRightmostChild(e => !e.IsHidden);
|
||||||
if (rightmost != null) {
|
if (rightmost != null) {
|
||||||
if (rightmost.Anchor.IsLeftAligned()) {
|
|
||||||
autoSize.X = rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right;
|
autoSize.X = rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right;
|
||||||
} else {
|
|
||||||
autoSize.X = rightmost.UnscrolledArea.Width + this.ScaledChildPadding.Width;
|
|
||||||
}
|
|
||||||
foundChild = rightmost;
|
foundChild = rightmost;
|
||||||
} else {
|
} else {
|
||||||
|
if (this.Children.Any(e => !e.IsHidden))
|
||||||
|
throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its width based on children but it only has visible children anchored too far right ({string.Join(", ", this.Children.Where(c => !c.IsHidden).Select(c => c.Anchor))})");
|
||||||
autoSize.X = 0;
|
autoSize.X = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -801,13 +717,15 @@ namespace MLEM.Ui.Elements {
|
||||||
// we want to leave some leeway to prevent float rounding causing an infinite loop
|
// we want to leave some leeway to prevent float rounding causing an infinite loop
|
||||||
if (!autoSize.Equals(this.UnscrolledArea.Size, Element.Epsilon)) {
|
if (!autoSize.Equals(this.UnscrolledArea.Size, Element.Epsilon)) {
|
||||||
recursion++;
|
recursion++;
|
||||||
if (recursion >= 16)
|
if (recursion >= 16) {
|
||||||
throw new ArithmeticException($"The area of {this} with root {this.Root.Name} has recursively updated too often. Does its child {foundChild} contain any conflicting auto-sizing settings?");
|
throw new ArithmeticException($"The area of {this} with root {this.Root.Name} has recursively updated too often. Does its child {foundChild} contain any conflicting auto-sizing settings?");
|
||||||
|
} else {
|
||||||
UpdateDisplayArea(autoSize);
|
UpdateDisplayArea(autoSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets this element's <see cref="Area"/> to the given <see cref="RectangleF"/> and invokes the <see cref="UiSystem.OnElementAreaUpdated"/> event.
|
/// Sets this element's <see cref="Area"/> to the given <see cref="RectangleF"/> and invokes the <see cref="UiSystem.OnElementAreaUpdated"/> event.
|
||||||
|
@ -831,12 +749,12 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <returns>The actual size of this element, taking <see cref="Scale"/> into account</returns>
|
/// <returns>The actual size of this element, taking <see cref="Scale"/> into account</returns>
|
||||||
protected virtual Vector2 CalcActualSize(RectangleF parentArea) {
|
protected virtual Vector2 CalcActualSize(RectangleF parentArea) {
|
||||||
var ret = new Vector2(
|
var ret = new Vector2(
|
||||||
this.size.X > 1 ? this.ScaledSize.X : parentArea.Width * this.size.X + this.ScaledAutoSizeAddedAbsolute.X,
|
this.size.X > 1 ? this.ScaledSize.X : parentArea.Width * this.size.X,
|
||||||
this.size.Y > 1 ? this.ScaledSize.Y : parentArea.Height * this.size.Y + this.ScaledAutoSizeAddedAbsolute.Y);
|
this.size.Y > 1 ? this.ScaledSize.Y : parentArea.Height * this.size.Y);
|
||||||
if (this.size.X < 0)
|
if (this.size.X < 0)
|
||||||
ret.X = -this.size.X * ret.Y + this.ScaledAutoSizeAddedAbsolute.X;
|
ret.X = -this.size.X * ret.Y;
|
||||||
if (this.size.Y < 0)
|
if (this.size.Y < 0)
|
||||||
ret.Y = -this.size.Y * ret.X + this.ScaledAutoSizeAddedAbsolute.Y;
|
ret.Y = -this.size.Y * ret.X;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -855,15 +773,13 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <returns>The lowest element, or null if no such element exists</returns>
|
/// <returns>The lowest element, or null if no such element exists</returns>
|
||||||
public Element GetLowestChild(Func<Element, bool> condition = null) {
|
public Element GetLowestChild(Func<Element, bool> condition = null) {
|
||||||
Element lowest = null;
|
Element lowest = null;
|
||||||
var lowestX = float.MinValue;
|
|
||||||
foreach (var child in this.Children) {
|
foreach (var child in this.Children) {
|
||||||
if (condition != null && !condition(child))
|
if (condition != null && !condition(child))
|
||||||
continue;
|
continue;
|
||||||
var x = !child.Anchor.IsTopAligned() ? child.UnscrolledArea.Height : child.UnscrolledArea.Bottom;
|
if (child.Anchor > Anchor.TopRight && child.Anchor < Anchor.AutoLeft)
|
||||||
if (x >= lowestX) {
|
continue;
|
||||||
|
if (lowest == null || child.UnscrolledArea.Bottom >= lowest.UnscrolledArea.Bottom)
|
||||||
lowest = child;
|
lowest = child;
|
||||||
lowestX = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return lowest;
|
return lowest;
|
||||||
}
|
}
|
||||||
|
@ -875,15 +791,13 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <returns>The rightmost element, or null if no such element exists</returns>
|
/// <returns>The rightmost element, or null if no such element exists</returns>
|
||||||
public Element GetRightmostChild(Func<Element, bool> condition = null) {
|
public Element GetRightmostChild(Func<Element, bool> condition = null) {
|
||||||
Element rightmost = null;
|
Element rightmost = null;
|
||||||
var rightmostX = float.MinValue;
|
|
||||||
foreach (var child in this.Children) {
|
foreach (var child in this.Children) {
|
||||||
if (condition != null && !condition(child))
|
if (condition != null && !condition(child))
|
||||||
continue;
|
continue;
|
||||||
var x = !child.Anchor.IsLeftAligned() ? child.UnscrolledArea.Width : child.UnscrolledArea.Right;
|
if (child.Anchor < Anchor.AutoLeft && child.Anchor != Anchor.TopLeft && child.Anchor != Anchor.CenterLeft && child.Anchor != Anchor.BottomLeft)
|
||||||
if (x >= rightmostX) {
|
continue;
|
||||||
|
if (rightmost == null || child.UnscrolledArea.Right >= rightmost.UnscrolledArea.Right)
|
||||||
rightmost = child;
|
rightmost = child;
|
||||||
rightmostX = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return rightmost;
|
return rightmost;
|
||||||
}
|
}
|
||||||
|
@ -1144,7 +1058,6 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
||||||
[Obsolete("Dispose will be removed in a future update. To unregister custom event handlers, use OnRemovedFromUi instead.")]
|
|
||||||
public virtual void Dispose() {
|
public virtual void Dispose() {
|
||||||
this.OnDisposed?.Invoke(this);
|
this.OnDisposed?.Invoke(this);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
@ -1206,7 +1119,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <param name="grandchild">Whether the <paramref name="child"/> is a grandchild of this element, rather than a direct child.</param>
|
/// <param name="grandchild">Whether the <paramref name="child"/> is a grandchild of this element, rather than a direct child.</param>
|
||||||
protected virtual void OnChildAreaDirty(Element child, bool grandchild) {
|
protected virtual void OnChildAreaDirty(Element child, bool grandchild) {
|
||||||
if (!grandchild) {
|
if (!grandchild) {
|
||||||
if (child.Anchor.IsAuto() || child.PreventParentSpill || this.SetWidthBasedOnChildren || this.SetHeightBasedOnChildren)
|
if (child.Anchor >= Anchor.AutoLeft || this.SetWidthBasedOnChildren || this.SetHeightBasedOnChildren)
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
}
|
}
|
||||||
this.Parent?.OnChildAreaDirty(child, true);
|
this.Parent?.OnChildAreaDirty(child, true);
|
||||||
|
@ -1234,31 +1147,6 @@ namespace MLEM.Ui.Elements {
|
||||||
return this.TransformInverse(position);
|
return this.TransformInverse(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when this element is added to a <see cref="UiSystem"/> and, optionally, a given <see cref="RootElement"/>.
|
|
||||||
/// This method is called in <see cref="AddChild{T}"/> and <see cref="UiSystem.Add"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="system">The ui system to add to.</param>
|
|
||||||
/// <param name="root">The root element to add to.</param>
|
|
||||||
protected internal virtual void AddedToUi(UiSystem system, RootElement root) {
|
|
||||||
this.Root = root;
|
|
||||||
this.System = system;
|
|
||||||
this.OnAddedToUi?.Invoke(this);
|
|
||||||
root?.InvokeOnElementAdded(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when this element is removed from a <see cref="UiSystem"/> and <see cref="RootElement"/>.
|
|
||||||
/// This method is called in <see cref="RemoveChild"/> and <see cref="UiSystem.Remove"/>.
|
|
||||||
/// </summary>
|
|
||||||
protected internal virtual void RemovedFromUi() {
|
|
||||||
var root = this.Root;
|
|
||||||
this.Root = null;
|
|
||||||
this.System = null;
|
|
||||||
this.OnRemovedFromUi?.Invoke(this);
|
|
||||||
root?.InvokeOnElementRemoved(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate used for the <see cref="Element.OnTextInput"/> event.
|
/// A delegate used for the <see cref="Element.OnTextInput"/> event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -68,31 +68,12 @@ namespace MLEM.Ui.Elements {
|
||||||
for (var i = 0; i < amount; i++) {
|
for (var i = 0; i < amount; i++) {
|
||||||
var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline;
|
var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline;
|
||||||
cols[i] = new Group(anchor, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
|
cols[i] = new Group(anchor, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren);
|
||||||
parent?.AddChild(cols[i]);
|
if (parent != null)
|
||||||
|
parent.AddChild(cols[i]);
|
||||||
}
|
}
|
||||||
return cols;
|
return cols;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates an array of groups with a fixed width and height that can be used to create a grid with equally sized boxes.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="parent">The element the groups should be added to, can be <see langword="null"/>.</param>
|
|
||||||
/// <param name="totalSize">The total size that the grid should take up.</param>
|
|
||||||
/// <param name="width">The width of the grid, or the amount of columns it should have.</param>
|
|
||||||
/// <param name="height">The height of the grid, or the amount of rows it should have.</param>
|
|
||||||
/// <returns>The created grid.</returns>
|
|
||||||
public static Group[,] MakeGrid(Element parent, Vector2 totalSize, int width, int height) {
|
|
||||||
var grid = new Group[width, height];
|
|
||||||
for (var y = 0; y < height; y++) {
|
|
||||||
for (var x = 0; x < width; x++) {
|
|
||||||
var anchor = x == 0 ? Anchor.AutoLeft : Anchor.AutoInlineIgnoreOverflow;
|
|
||||||
grid[x, y] = new Group(anchor, new Vector2(totalSize.X / width, totalSize.Y / height), false);
|
|
||||||
parent?.AddChild(grid[x, y]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="TextField"/> with a + and a - button next to it, to allow for easy number input.
|
/// Creates a <see cref="TextField"/> with a + and a - button next to it, to allow for easy number input.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -233,32 +214,5 @@ namespace MLEM.Ui.Elements {
|
||||||
return tooltip;
|
return tooltip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given <see cref="Anchor"/> is automatic. The anchors <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoCenter"/>, <see cref="Anchor.AutoRight"/>, <see cref="Anchor.AutoInline"/> and <see cref="Anchor.AutoInlineIgnoreOverflow"/> will return true.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="anchor">The anchor to query.</param>
|
|
||||||
/// <returns>Whether the given anchor is automatic.</returns>
|
|
||||||
public static bool IsAuto(this Anchor anchor) {
|
|
||||||
return anchor == Anchor.AutoLeft || anchor == Anchor.AutoCenter || anchor == Anchor.AutoRight || anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineIgnoreOverflow;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given <see cref="Anchor"/> is left-aligned for the purpose of <see cref="Element.GetRightmostChild"/>. The anchors <see cref="Anchor.TopLeft"/>, <see cref="Anchor.CenterLeft"/>, <see cref="Anchor.BottomLeft"/>, <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoInline"/> and <see cref="Anchor.AutoInlineIgnoreOverflow"/> will return true.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="anchor">The anchor to query.</param>
|
|
||||||
/// <returns>Whether the given anchor is left-aligned.</returns>
|
|
||||||
public static bool IsLeftAligned(this Anchor anchor) {
|
|
||||||
return anchor == Anchor.TopLeft || anchor == Anchor.CenterLeft || anchor == Anchor.BottomLeft || anchor == Anchor.AutoLeft || anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineIgnoreOverflow;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given <see cref="Anchor"/> is top-aligned for the purpose of <see cref="Element.GetLowestChild"/>. The anchors <see cref="Anchor.TopLeft"/>, <see cref="Anchor.TopCenter"/>, <see cref="Anchor.TopRight"/>, <see cref="Anchor.AutoLeft"/>, <see cref="Anchor.AutoCenter"/>, <see cref="Anchor.AutoRight"/>, <see cref="Anchor.AutoInline"/> and <see cref="Anchor.AutoInlineIgnoreOverflow"/> will return true.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="anchor">The anchor to query.</param>
|
|
||||||
/// <returns>Whether the given anchor is top-aligned.</returns>
|
|
||||||
public static bool IsTopAligned(this Anchor anchor) {
|
|
||||||
return anchor == Anchor.TopLeft || anchor == Anchor.TopCenter || anchor == Anchor.TopRight || anchor == Anchor.AutoLeft || anchor == Anchor.AutoCenter || anchor == Anchor.AutoRight || anchor == Anchor.AutoInline || anchor == Anchor.AutoInlineIgnoreOverflow;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Graphics;
|
using MLEM.Graphics;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
#if FNA
|
|
||||||
using MLEM.Extensions;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -30,13 +28,17 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TextureRegion Texture {
|
public TextureRegion Texture {
|
||||||
get {
|
get {
|
||||||
var ret = this.GetTextureCallback?.Invoke(this) ?? this.texture;
|
if (this.GetTextureCallback != null)
|
||||||
this.CheckTextureChange(ret);
|
this.Texture = this.GetTextureCallback(this);
|
||||||
return ret;
|
return this.texture;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
|
if (this.texture != value) {
|
||||||
this.texture = value;
|
this.texture = value;
|
||||||
this.CheckTextureChange(value);
|
this.IsHidden = this.texture == null;
|
||||||
|
if (this.scaleToImage)
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -71,12 +73,8 @@ namespace MLEM.Ui.Elements {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float ImageRotation;
|
public float ImageRotation;
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override bool IsHidden => base.IsHidden || this.Texture == null;
|
|
||||||
|
|
||||||
private bool scaleToImage;
|
private bool scaleToImage;
|
||||||
private TextureRegion texture;
|
private TextureRegion texture;
|
||||||
private TextureRegion lastTexture;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new image with the given settings
|
/// Creates a new image with the given settings
|
||||||
|
@ -123,15 +121,6 @@ namespace MLEM.Ui.Elements {
|
||||||
base.Draw(time, batch, alpha, context);
|
base.Draw(time, batch, alpha, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckTextureChange(TextureRegion newTexture) {
|
|
||||||
if (this.lastTexture == newTexture)
|
|
||||||
return;
|
|
||||||
var nullChanged = this.lastTexture == null != (newTexture == null);
|
|
||||||
this.lastTexture = newTexture;
|
|
||||||
if (nullChanged || this.scaleToImage)
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used for <see cref="Image.GetTextureCallback"/>
|
/// A delegate method used for <see cref="Image.GetTextureCallback"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -46,13 +46,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The amount of pixels of room there should be between the <see cref="ScrollBar"/> and the rest of the content
|
/// The amount of pixels of room there should be between the <see cref="ScrollBar"/> and the rest of the content
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<float> ScrollBarOffset {
|
public StyleProp<float> ScrollBarOffset;
|
||||||
get => this.scrollBarOffset;
|
|
||||||
set {
|
|
||||||
this.scrollBarOffset = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly List<Element> relevantChildren = new List<Element>();
|
private readonly List<Element> relevantChildren = new List<Element>();
|
||||||
private readonly bool scrollOverflow;
|
private readonly bool scrollOverflow;
|
||||||
|
@ -60,7 +54,6 @@ namespace MLEM.Ui.Elements {
|
||||||
private RenderTarget2D renderTarget;
|
private RenderTarget2D renderTarget;
|
||||||
private bool relevantChildrenDirty;
|
private bool relevantChildrenDirty;
|
||||||
private float scrollBarChildOffset;
|
private float scrollBarChildOffset;
|
||||||
private StyleProp<float> scrollBarOffset;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new panel with the given settings.
|
/// Creates a new panel with the given settings.
|
||||||
|
@ -91,7 +84,8 @@ namespace MLEM.Ui.Elements {
|
||||||
return;
|
return;
|
||||||
if (e == null || !e.GetParentTree().Contains(this))
|
if (e == null || !e.GetParentTree().Contains(this))
|
||||||
return;
|
return;
|
||||||
this.ScrollToElement(e);
|
var firstChild = this.Children.First(c => c != this.ScrollBar);
|
||||||
|
this.ScrollBar.CurrentValue = (e.Area.Center.Y - this.Area.Height / 2 - firstChild.Area.Top) / e.Scale + this.ChildPadding.Value.Height / 2;
|
||||||
};
|
};
|
||||||
this.AddChild(this.ScrollBar);
|
this.AddChild(this.ScrollBar);
|
||||||
}
|
}
|
||||||
|
@ -104,7 +98,7 @@ namespace MLEM.Ui.Elements {
|
||||||
if (this.SetHeightBasedOnChildren)
|
if (this.SetHeightBasedOnChildren)
|
||||||
throw new NotSupportedException("A panel can't both set height based on children and scroll overflow");
|
throw new NotSupportedException("A panel can't both set height based on children and scroll overflow");
|
||||||
foreach (var child in this.Children) {
|
foreach (var child in this.Children) {
|
||||||
if (child != this.ScrollBar && !child.Anchor.IsAuto())
|
if (child != this.ScrollBar && child.Anchor < Anchor.AutoLeft)
|
||||||
throw new NotSupportedException($"A panel that handles overflow can't contain non-automatic anchors ({child})");
|
throw new NotSupportedException($"A panel that handles overflow can't contain non-automatic anchors ({child})");
|
||||||
if (child is Panel panel && panel.scrollOverflow)
|
if (child is Panel panel && panel.scrollOverflow)
|
||||||
throw new NotSupportedException($"A panel that scrolls overflow cannot contain another panel that scrolls overflow ({child})");
|
throw new NotSupportedException($"A panel that scrolls overflow cannot contain another panel that scrolls overflow ({child})");
|
||||||
|
@ -121,6 +115,19 @@ namespace MLEM.Ui.Elements {
|
||||||
this.ScrollSetup();
|
this.ScrollSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ScrollChildren() {
|
||||||
|
if (!this.scrollOverflow)
|
||||||
|
return;
|
||||||
|
var offset = new Vector2(0, -this.ScrollBar.CurrentValue);
|
||||||
|
// we ignore false grandchildren so that the children of the scroll bar stay in place
|
||||||
|
foreach (var child in this.GetChildren(c => c != this.ScrollBar, true, true)) {
|
||||||
|
if (!child.ScrollOffset.Equals(offset, Element.Epsilon)) {
|
||||||
|
child.ScrollOffset = offset;
|
||||||
|
this.relevantChildrenDirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void ForceUpdateSortedChildren() {
|
public override void ForceUpdateSortedChildren() {
|
||||||
base.ForceUpdateSortedChildren();
|
base.ForceUpdateSortedChildren();
|
||||||
|
@ -140,6 +147,26 @@ namespace MLEM.Ui.Elements {
|
||||||
base.RemoveChildren(e => e != this.ScrollBar && (condition == null || condition(e)));
|
base.RemoveChildren(e => e != this.ScrollBar && (condition == null || condition(e)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override IList<Element> GetRelevantChildren() {
|
||||||
|
var relevant = base.GetRelevantChildren();
|
||||||
|
if (this.scrollOverflow) {
|
||||||
|
if (this.relevantChildrenDirty)
|
||||||
|
this.ForceUpdateRelevantChildren();
|
||||||
|
relevant = this.relevantChildren;
|
||||||
|
}
|
||||||
|
return relevant;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void OnChildAreaDirty(Element child, bool grandchild) {
|
||||||
|
base.OnChildAreaDirty(child, grandchild);
|
||||||
|
// we only need to scroll when a grandchild changes, since all of our children are forced
|
||||||
|
// to be auto-anchored and so will automatically propagate their changes up to us
|
||||||
|
if (grandchild)
|
||||||
|
this.ScrollChildren();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
||||||
// draw children onto the render target if we have one
|
// draw children onto the render target if we have one
|
||||||
|
@ -184,23 +211,11 @@ namespace MLEM.Ui.Elements {
|
||||||
return base.GetElementUnderPos(position);
|
return base.GetElementUnderPos(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private RectangleF GetRenderTargetArea() {
|
||||||
/// Scrolls this panel's <see cref="ScrollBar"/> to the given <see cref="Element"/> in such a way that its center is positioned in the center of this panel.
|
var area = this.ChildPaddedArea;
|
||||||
/// </summary>
|
area.X = this.DisplayArea.X;
|
||||||
/// <param name="element">The element to scroll to.</param>
|
area.Width = this.DisplayArea.Width;
|
||||||
public void ScrollToElement(Element element) {
|
return area;
|
||||||
this.ScrollToElement(element.Area.Center.Y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Scrolls this panel's <see cref="ScrollBar"/> to the given <paramref name="elementY"/> coordinate in such a way that the coordinate is positioned in the center of this panel.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="elementY">The y coordinate to scroll to, which should have this element's <see cref="Element.Scale"/> applied.</param>
|
|
||||||
public void ScrollToElement(float elementY) {
|
|
||||||
var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
|
|
||||||
if (firstChild == null)
|
|
||||||
return;
|
|
||||||
this.ScrollBar.CurrentValue = (elementY - this.Area.Height / 2 - firstChild.Area.Top) / this.Scale + this.ChildPadding.Value.Height / 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -214,58 +229,26 @@ namespace MLEM.Ui.Elements {
|
||||||
this.SetScrollBarStyle();
|
this.SetScrollBarStyle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override IList<Element> GetRelevantChildren() {
|
|
||||||
var relevant = base.GetRelevantChildren();
|
|
||||||
if (this.scrollOverflow) {
|
|
||||||
if (this.relevantChildrenDirty)
|
|
||||||
this.ForceUpdateRelevantChildren();
|
|
||||||
relevant = this.relevantChildren;
|
|
||||||
}
|
|
||||||
return relevant;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void OnChildAreaDirty(Element child, bool grandchild) {
|
|
||||||
base.OnChildAreaDirty(child, grandchild);
|
|
||||||
// we only need to scroll when a grandchild changes, since all of our children are forced
|
|
||||||
// to be auto-anchored and so will automatically propagate their changes up to us
|
|
||||||
if (grandchild)
|
|
||||||
this.ScrollChildren();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected internal override void RemovedFromUi() {
|
|
||||||
base.RemovedFromUi();
|
|
||||||
// we dispose our render target when removing so that it doesn't cause a memory leak
|
|
||||||
// if we're added back afterwards, it'll be recreated in ScrollSetup anyway
|
|
||||||
this.renderTarget?.Dispose();
|
|
||||||
this.renderTarget = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value.
|
/// Prepares the panel for auto-scrolling, creating the render target and setting up the scroll bar's maximum value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void ScrollSetup() {
|
protected virtual void ScrollSetup() {
|
||||||
if (!this.scrollOverflow || this.IsHidden)
|
if (!this.scrollOverflow || this.IsHidden)
|
||||||
return;
|
return;
|
||||||
|
// if there is only one child, then we have just the scroll bar
|
||||||
|
if (this.Children.Count == 1)
|
||||||
|
return;
|
||||||
|
|
||||||
float childrenHeight;
|
// the "real" first child is the scroll bar, which we want to ignore
|
||||||
if (this.Children.Count > 1) {
|
var firstChild = this.Children.First(c => c != this.ScrollBar);
|
||||||
var firstChild = this.Children.FirstOrDefault(c => c != this.ScrollBar);
|
|
||||||
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
|
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
|
||||||
childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
|
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
|
||||||
} else {
|
|
||||||
// if we only have one child (the scroll bar), then the children take up no visual height
|
|
||||||
childrenHeight = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
|
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
|
||||||
var scrollBarMax = (childrenHeight - this.ChildPaddedArea.Height) / this.Scale;
|
var scrollBarMax = (childrenHeight - this.ChildPaddedArea.Height) / this.Scale;
|
||||||
if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) {
|
if (!this.ScrollBar.MaxValue.Equals(scrollBarMax, Element.Epsilon)) {
|
||||||
this.ScrollBar.MaxValue = scrollBarMax;
|
this.ScrollBar.MaxValue = scrollBarMax;
|
||||||
this.relevantChildrenDirty = true;
|
this.relevantChildrenDirty = true;
|
||||||
}
|
|
||||||
|
|
||||||
// update child padding based on whether the scroll bar is visible
|
// update child padding based on whether the scroll bar is visible
|
||||||
var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset;
|
var childOffset = this.ScrollBar.IsHidden ? 0 : this.ScrollerSize.Value.X + this.ScrollBarOffset;
|
||||||
|
@ -274,6 +257,7 @@ namespace MLEM.Ui.Elements {
|
||||||
this.scrollBarChildOffset = childOffset;
|
this.scrollBarChildOffset = childOffset;
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// the scroller height has the same relation to the scroll bar height as the visible area has to the total height of the panel's content
|
// the scroller height has the same relation to the scroll bar height as the visible area has to the total height of the panel's content
|
||||||
var scrollerHeight = Math.Min(this.ChildPaddedArea.Height / childrenHeight / this.Scale, 1) * this.ScrollBar.Area.Height;
|
var scrollerHeight = Math.Min(this.ChildPaddedArea.Height / childrenHeight / this.Scale, 1) * this.ScrollBar.Area.Height;
|
||||||
|
@ -281,18 +265,25 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
// update the render target
|
// update the render target
|
||||||
var targetArea = (Rectangle) this.GetRenderTargetArea();
|
var targetArea = (Rectangle) this.GetRenderTargetArea();
|
||||||
if (targetArea.Width <= 0 || targetArea.Height <= 0) {
|
if (targetArea.Width <= 0 || targetArea.Height <= 0)
|
||||||
this.renderTarget?.Dispose();
|
|
||||||
this.renderTarget = null;
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
|
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
|
||||||
this.renderTarget?.Dispose();
|
if (this.renderTarget != null)
|
||||||
|
this.renderTarget.Dispose();
|
||||||
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height);
|
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height);
|
||||||
this.relevantChildrenDirty = true;
|
this.relevantChildrenDirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Dispose() {
|
||||||
|
if (this.renderTarget != null) {
|
||||||
|
this.renderTarget.Dispose();
|
||||||
|
this.renderTarget = null;
|
||||||
|
}
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
private void SetScrollBarStyle() {
|
private void SetScrollBarStyle() {
|
||||||
if (this.ScrollBar == null)
|
if (this.ScrollBar == null)
|
||||||
return;
|
return;
|
||||||
|
@ -319,21 +310,5 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private RectangleF GetRenderTargetArea() {
|
|
||||||
var area = this.ChildPaddedArea;
|
|
||||||
area.X = this.DisplayArea.X;
|
|
||||||
area.Width = this.DisplayArea.Width;
|
|
||||||
return area;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ScrollChildren() {
|
|
||||||
if (!this.scrollOverflow)
|
|
||||||
return;
|
|
||||||
// we ignore false grandchildren so that the children of the scroll bar stay in place
|
|
||||||
foreach (var child in this.GetChildren(c => c != this.ScrollBar, true, true))
|
|
||||||
child.ScrollOffset.Y = -this.ScrollBar.CurrentValue;
|
|
||||||
this.relevantChildrenDirty = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,13 +32,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The tokenized version of the <see cref="Text"/>
|
/// The tokenized version of the <see cref="Text"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TokenizedString TokenizedText {
|
public TokenizedString TokenizedText { get; private set; }
|
||||||
get {
|
|
||||||
this.CheckTextChange();
|
|
||||||
this.TokenizeIfNecessary();
|
|
||||||
return this.tokenizedText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The color that the text will be rendered with
|
/// The color that the text will be rendered with
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -47,78 +41,48 @@ namespace MLEM.Ui.Elements {
|
||||||
/// The scale that the text will be rendered with.
|
/// The scale that the text will be rendered with.
|
||||||
/// To add a multiplier rather than changing the scale directly, use <see cref="TextScaleMultiplier"/>.
|
/// To add a multiplier rather than changing the scale directly, use <see cref="TextScaleMultiplier"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<float> TextScale {
|
public StyleProp<float> TextScale;
|
||||||
get => this.textScale;
|
|
||||||
set {
|
|
||||||
this.textScale = value;
|
|
||||||
this.SetTextDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A multiplier that will be applied to <see cref="TextScale"/>.
|
/// A multiplier that will be applied to <see cref="TextScale"/>.
|
||||||
/// To change the text scale itself, use <see cref="TextScale"/>.
|
/// To change the text scale itself, use <see cref="TextScale"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float TextScaleMultiplier {
|
public float TextScaleMultiplier = 1;
|
||||||
get => this.textScaleMultiplier;
|
|
||||||
set {
|
|
||||||
if (this.textScaleMultiplier != value) {
|
|
||||||
this.textScaleMultiplier = value;
|
|
||||||
this.SetTextDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The text to render inside of this paragraph.
|
/// The text to render inside of this paragraph.
|
||||||
/// Use <see cref="GetTextCallback"/> if the text changes frequently.
|
/// Use <see cref="GetTextCallback"/> if the text changes frequently.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Text {
|
public string Text {
|
||||||
get {
|
get {
|
||||||
this.CheckTextChange();
|
this.QueryTextCallback();
|
||||||
return this.displayedText;
|
return this.text;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
this.explicitlySetText = value;
|
if (this.text != value) {
|
||||||
this.CheckTextChange();
|
this.text = value;
|
||||||
|
this.SetTextDirty();
|
||||||
|
|
||||||
|
var force = string.IsNullOrWhiteSpace(this.text);
|
||||||
|
if (this.forceHide != force) {
|
||||||
|
this.forceHide = force;
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If this paragraph should automatically adjust its width based on the width of the text within it
|
/// If this paragraph should automatically adjust its width based on the width of the text within it
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AutoAdjustWidth {
|
public bool AutoAdjustWidth;
|
||||||
get => this.autoAdjustWidth;
|
|
||||||
set {
|
|
||||||
if (this.autoAdjustWidth != value) {
|
|
||||||
this.autoAdjustWidth = value;
|
|
||||||
this.SetAreaDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether this paragraph should be truncated instead of split if the displayed <see cref="Text"/>'s width exceeds the provided width.
|
/// Whether this paragraph should be truncated instead of split if the displayed <see cref="Text"/>'s width exceeds the provided width.
|
||||||
/// When the string is truncated, the <see cref="Ellipsis"/> is added to its end.
|
/// When the string is truncated, the <see cref="Ellipsis"/> is added to its end.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool TruncateIfLong {
|
public bool TruncateIfLong;
|
||||||
get => this.truncateIfLong;
|
|
||||||
set {
|
|
||||||
if (this.truncateIfLong != value) {
|
|
||||||
this.truncateIfLong = value;
|
|
||||||
this.SetAlignSplitDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The ellipsis characters to use if <see cref="TruncateIfLong"/> is enabled and the string is truncated.
|
/// The ellipsis characters to use if <see cref="TruncateIfLong"/> is enabled and the string is truncated.
|
||||||
/// If this is set to an empty string, no ellipsis will be attached to the truncated string.
|
/// If this is set to an empty string, no ellipsis will be attached to the truncated string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Ellipsis {
|
public string Ellipsis = "...";
|
||||||
get => this.ellipsis;
|
|
||||||
set {
|
|
||||||
if (this.ellipsis != value) {
|
|
||||||
this.ellipsis = value;
|
|
||||||
this.SetAlignSplitDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An event that gets called when this paragraph's <see cref="Text"/> is queried.
|
/// An event that gets called when this paragraph's <see cref="Text"/> is queried.
|
||||||
/// Use this event for setting this paragraph's text if it changes frequently.
|
/// Use this event for setting this paragraph's text if it changes frequently.
|
||||||
|
@ -141,20 +105,12 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool IsHidden => base.IsHidden || string.IsNullOrWhiteSpace(this.Text);
|
public override bool IsHidden => base.IsHidden || this.forceHide;
|
||||||
|
|
||||||
private string displayedText;
|
private string text;
|
||||||
private string explicitlySetText;
|
|
||||||
private StyleProp<TextAlignment> alignment;
|
private StyleProp<TextAlignment> alignment;
|
||||||
private StyleProp<GenericFont> regularFont;
|
private StyleProp<GenericFont> regularFont;
|
||||||
private StyleProp<float> textScale;
|
private bool forceHide;
|
||||||
private TokenizedString tokenizedText;
|
|
||||||
private float? lastAlignSplitWidth;
|
|
||||||
private float? lastAlignSplitScale;
|
|
||||||
private string ellipsis = "...";
|
|
||||||
private bool truncateIfLong;
|
|
||||||
private float textScaleMultiplier = 1;
|
|
||||||
private bool autoAdjustWidth;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new paragraph with the given settings.
|
/// Creates a new paragraph with the given settings.
|
||||||
|
@ -178,17 +134,17 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override Vector2 CalcActualSize(RectangleF parentArea) {
|
protected override Vector2 CalcActualSize(RectangleF parentArea) {
|
||||||
var size = base.CalcActualSize(parentArea);
|
var size = base.CalcActualSize(parentArea);
|
||||||
this.CheckTextChange();
|
this.ParseText(size);
|
||||||
this.TokenizeIfNecessary();
|
var textSize = this.TokenizedText.Measure(this.RegularFont) * this.TextScale * this.TextScaleMultiplier * this.Scale;
|
||||||
this.AlignAndSplitIfNecessary(size);
|
|
||||||
var textSize = this.tokenizedText.GetArea(Vector2.Zero, this.TextScale * this.TextScaleMultiplier * this.Scale).Size;
|
|
||||||
return new Vector2(this.AutoAdjustWidth ? textSize.X + this.ScaledPadding.Width : size.X, textSize.Y + this.ScaledPadding.Height);
|
return new Vector2(this.AutoAdjustWidth ? textSize.X + this.ScaledPadding.Width : size.X, textSize.Y + this.ScaledPadding.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Update(GameTime time) {
|
public override void Update(GameTime time) {
|
||||||
this.TokenizedText?.Update(time);
|
this.QueryTextCallback();
|
||||||
base.Update(time);
|
base.Update(time);
|
||||||
|
if (this.TokenizedText != null)
|
||||||
|
this.TokenizedText.Update(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -209,22 +165,45 @@ namespace MLEM.Ui.Elements {
|
||||||
this.Alignment = this.Alignment.OrStyle(style.TextAlignment);
|
this.Alignment = this.Alignment.OrStyle(style.TextAlignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetTextDirty() {
|
/// <summary>
|
||||||
this.tokenizedText = null;
|
/// Parses this paragraph's <see cref="Text"/> into <see cref="TokenizedText"/>.
|
||||||
|
/// Additionally, this method adds any <see cref="Link"/> elements for tokenized links in the text.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="size">The paragraph's default size</param>
|
||||||
|
protected virtual void ParseText(Vector2 size) {
|
||||||
|
if (this.TokenizedText == null) {
|
||||||
|
// tokenize the text
|
||||||
|
this.TokenizedText = this.System.TextFormatter.Tokenize(this.RegularFont, this.Text, this.Alignment);
|
||||||
|
|
||||||
|
// add links to the paragraph
|
||||||
|
this.RemoveChildren(c => c is Link);
|
||||||
|
foreach (var link in this.TokenizedText.Tokens.Where(t => t.AppliedCodes.Any(c => c is LinkCode)))
|
||||||
|
this.AddChild(new Link(Anchor.TopLeft, link, this.TextScale * this.TextScaleMultiplier));
|
||||||
|
}
|
||||||
|
|
||||||
|
var width = size.X - this.ScaledPadding.Width;
|
||||||
|
var scale = this.TextScale * this.TextScaleMultiplier * this.Scale;
|
||||||
|
if (this.TruncateIfLong) {
|
||||||
|
this.TokenizedText.Truncate(this.RegularFont, width, scale, this.Ellipsis, this.Alignment);
|
||||||
|
} else {
|
||||||
|
this.TokenizedText.Split(this.RegularFont, width, scale, this.Alignment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A helper method that causes the <see cref="TokenizedText"/> to be reset.
|
||||||
|
/// Additionally, <see cref="Element.SetAreaDirty"/> if this paragraph's area has changed enough to warrant it, or if it has any <see cref="Link"/> children.
|
||||||
|
/// </summary>
|
||||||
|
protected void SetTextDirty() {
|
||||||
|
this.TokenizedText = null;
|
||||||
// only set our area dirty if our size changed as a result of this action
|
// only set our area dirty if our size changed as a result of this action
|
||||||
if (!this.AreaDirty && !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Element.Epsilon))
|
if (!this.AreaDirty && !this.CalcActualSize(this.ParentArea).Equals(this.DisplayArea.Size, Element.Epsilon))
|
||||||
this.SetAreaDirty();
|
this.SetAreaDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckTextChange() {
|
private void QueryTextCallback() {
|
||||||
var newText = this.GetTextCallback?.Invoke(this) ?? this.explicitlySetText;
|
if (this.GetTextCallback != null)
|
||||||
if (this.displayedText == newText)
|
this.Text = this.GetTextCallback(this);
|
||||||
return;
|
|
||||||
var emptyChanged = string.IsNullOrWhiteSpace(this.displayedText) != string.IsNullOrWhiteSpace(newText);
|
|
||||||
this.displayedText = newText;
|
|
||||||
if (emptyChanged)
|
|
||||||
this.SetAreaDirty();
|
|
||||||
this.SetTextDirty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private float GetAlignmentOffset() {
|
private float GetAlignmentOffset() {
|
||||||
|
@ -237,41 +216,6 @@ namespace MLEM.Ui.Elements {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TokenizeIfNecessary() {
|
|
||||||
if (this.tokenizedText != null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// tokenize the text
|
|
||||||
this.tokenizedText = this.System.TextFormatter.Tokenize(this.RegularFont, this.Text, this.Alignment);
|
|
||||||
this.SetAlignSplitDirty();
|
|
||||||
|
|
||||||
// add links to the paragraph
|
|
||||||
this.RemoveChildren(c => c is Link);
|
|
||||||
foreach (var link in this.tokenizedText.Tokens.Where(t => t.AppliedCodes.Any(c => c is LinkCode)))
|
|
||||||
this.AddChild(new Link(Anchor.TopLeft, link, this.TextScale * this.TextScaleMultiplier));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AlignAndSplitIfNecessary(Vector2 size) {
|
|
||||||
var width = size.X - this.ScaledPadding.Width;
|
|
||||||
var scale = this.TextScale * this.TextScaleMultiplier * this.Scale;
|
|
||||||
|
|
||||||
if (this.lastAlignSplitWidth == width && this.lastAlignSplitScale == scale)
|
|
||||||
return;
|
|
||||||
this.lastAlignSplitWidth = width;
|
|
||||||
this.lastAlignSplitScale = scale;
|
|
||||||
|
|
||||||
if (this.TruncateIfLong) {
|
|
||||||
this.tokenizedText.Truncate(this.RegularFont, width, scale, this.Ellipsis, this.Alignment);
|
|
||||||
} else {
|
|
||||||
this.tokenizedText.Split(this.RegularFont, width, scale, this.Alignment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetAlignSplitDirty() {
|
|
||||||
this.lastAlignSplitWidth = null;
|
|
||||||
this.lastAlignSplitScale = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A delegate method used for <see cref="Paragraph.GetTextCallback"/>
|
/// A delegate method used for <see cref="Paragraph.GetTextCallback"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -3,14 +3,12 @@ using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input.Touch;
|
using Microsoft.Xna.Framework.Input.Touch;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Graphics;
|
using MLEM.Graphics;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
#if FNA
|
|
||||||
using MLEM.Extensions;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -33,16 +31,6 @@ namespace MLEM.Ui.Elements {
|
||||||
/// The texture of this scroll bar's scroller indicator
|
/// The texture of this scroll bar's scroller indicator
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<NinePatch> ScrollerTexture;
|
public StyleProp<NinePatch> ScrollerTexture;
|
||||||
/// <summary>
|
|
||||||
/// Whether smooth scrolling should be enabled for this scroll bar.
|
|
||||||
/// Smooth scrolling causes the <see cref="CurrentValue"/> to change gradually rather than instantly when scrolling.
|
|
||||||
/// </summary>
|
|
||||||
public StyleProp<bool> SmoothScrolling;
|
|
||||||
/// <summary>
|
|
||||||
/// The factor with which <see cref="SmoothScrolling"/> happens.
|
|
||||||
/// </summary>
|
|
||||||
public StyleProp<float> SmoothScrollFactor;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The scroller's width and height
|
/// The scroller's width and height
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -96,7 +84,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This property is true while the user scrolls on the scroll bar using the mouse or touch input
|
/// This property is true while the user scrolls on the scroll bar using the mouse or touch input
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsBeingScrolled => this.isMouseScrolling || this.isMouseDragging || this.isTouchDragging || this.isTouchScrolling;
|
public bool IsBeingScrolled => this.isMouseHeld || this.isDragging || this.isTouchHeld;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This field determines if this scroll bar should automatically be hidden from a <see cref="Panel"/> if there aren't enough children to allow for scrolling.
|
/// This field determines if this scroll bar should automatically be hidden from a <see cref="Panel"/> if there aren't enough children to allow for scrolling.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -109,14 +97,18 @@ namespace MLEM.Ui.Elements {
|
||||||
!this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale),
|
!this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale),
|
||||||
this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale));
|
this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale));
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether this scroll bar should allow dragging the mouse over its attached <see cref="Panel"/>'s content while holding the left mouse button to scroll, similarly to how scrolling using touch input works.
|
/// Whether smooth scrolling should be enabled for this scroll bar.
|
||||||
|
/// Smooth scrolling causes the <see cref="CurrentValue"/> to change gradually rather than instantly when scrolling.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool MouseDragScrolling;
|
public StyleProp<bool> SmoothScrolling;
|
||||||
|
/// <summary>
|
||||||
|
/// The factor with which <see cref="SmoothScrolling"/> happens.
|
||||||
|
/// </summary>
|
||||||
|
public StyleProp<float> SmoothScrollFactor;
|
||||||
|
|
||||||
private bool isMouseScrolling;
|
private bool isMouseHeld;
|
||||||
private bool isMouseDragging;
|
private bool isDragging;
|
||||||
private bool isTouchScrolling;
|
private bool isTouchHeld;
|
||||||
private bool isTouchDragging;
|
|
||||||
private float maxValue;
|
private float maxValue;
|
||||||
private float scrollAdded;
|
private float scrollAdded;
|
||||||
private float currValue;
|
private float currValue;
|
||||||
|
@ -147,29 +139,18 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
// MOUSE INPUT
|
// MOUSE INPUT
|
||||||
var moused = this.Controls.MousedElement;
|
var moused = this.Controls.MousedElement;
|
||||||
var wasMouseUp = this.Input.WasUp(MouseButton.Left);
|
if (moused == this && this.Input.WasMouseButtonUp(MouseButton.Left) && this.Input.IsMouseButtonDown(MouseButton.Left)) {
|
||||||
var isMouseDown = this.Input.IsDown(MouseButton.Left);
|
this.isMouseHeld = true;
|
||||||
if (moused == this && wasMouseUp && isMouseDown) {
|
|
||||||
this.isMouseScrolling = true;
|
|
||||||
this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition;
|
this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition;
|
||||||
} else if (!isMouseDown) {
|
} else if (this.isMouseHeld && !this.Input.IsMouseButtonDown(MouseButton.Left)) {
|
||||||
this.isMouseScrolling = false;
|
this.isMouseHeld = false;
|
||||||
}
|
}
|
||||||
if (this.isMouseScrolling)
|
if (this.isMouseHeld)
|
||||||
this.ScrollToPos(this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()));
|
this.ScrollToPos(this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()));
|
||||||
if (!this.Horizontal) {
|
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
|
||||||
if (moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
|
|
||||||
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
|
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
|
||||||
if (scroll != 0)
|
if (scroll != 0)
|
||||||
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
||||||
|
|
||||||
if (this.MouseDragScrolling && moused != this && wasMouseUp && isMouseDown)
|
|
||||||
this.isMouseDragging = true;
|
|
||||||
}
|
|
||||||
if (!isMouseDown)
|
|
||||||
this.isMouseDragging = false;
|
|
||||||
if (this.isMouseDragging)
|
|
||||||
this.CurrentValue -= (this.Input.MousePosition.Y - this.Input.LastMousePosition.Y) / this.Scale;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TOUCH INPUT
|
// TOUCH INPUT
|
||||||
|
@ -179,29 +160,29 @@ namespace MLEM.Ui.Elements {
|
||||||
// if the element under the drag's start position is on top of the panel, start dragging
|
// if the element under the drag's start position is on top of the panel, start dragging
|
||||||
var touched = this.Parent.GetElementUnderPos(this.TransformInverseAll(drag.Position));
|
var touched = this.Parent.GetElementUnderPos(this.TransformInverseAll(drag.Position));
|
||||||
if (touched != null && touched != this)
|
if (touched != null && touched != this)
|
||||||
this.isTouchDragging = true;
|
this.isDragging = true;
|
||||||
|
|
||||||
// if we're dragging at all, then move the scroller
|
// if we're dragging at all, then move the scroller
|
||||||
if (this.isTouchDragging)
|
if (this.isDragging)
|
||||||
this.CurrentValue -= drag.Delta.Y / this.Scale;
|
this.CurrentValue -= drag.Delta.Y / this.Scale;
|
||||||
} else {
|
} else {
|
||||||
this.isTouchDragging = false;
|
this.isDragging = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.Input.ViewportTouchState.Count <= 0) {
|
if (this.Input.ViewportTouchState.Count <= 0) {
|
||||||
// if no touch has occured this tick, then reset the variable
|
// if no touch has occured this tick, then reset the variable
|
||||||
this.isTouchScrolling = false;
|
this.isTouchHeld = false;
|
||||||
} else {
|
} else {
|
||||||
foreach (var loc in this.Input.ViewportTouchState) {
|
foreach (var loc in this.Input.ViewportTouchState) {
|
||||||
var pos = this.TransformInverseAll(loc.Position);
|
var pos = this.TransformInverseAll(loc.Position);
|
||||||
// if we just started touching and are on top of the scroller, then we should start scrolling
|
// if we just started touching and are on top of the scroller, then we should start scrolling
|
||||||
if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) {
|
if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) {
|
||||||
this.isTouchScrolling = true;
|
this.isTouchHeld = true;
|
||||||
this.scrollStartOffset = pos - this.ScrollerPosition;
|
this.scrollStartOffset = pos - this.ScrollerPosition;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// scroll no matter if we're on the scroller right now
|
// scroll no matter if we're on the scroller right now
|
||||||
if (this.isTouchScrolling)
|
if (this.isTouchHeld)
|
||||||
this.ScrollToPos(pos);
|
this.ScrollToPos(pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,9 +7,7 @@ using MLEM.Input;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
|
|
||||||
using TextCopy;
|
using TextCopy;
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -33,14 +31,6 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <inheritdoc cref="TextInput.FileNames"/>
|
/// <inheritdoc cref="TextInput.FileNames"/>
|
||||||
public static readonly Rule FileNames = (field, add) => TextInput.FileNames(field.textInput, add);
|
public static readonly Rule FileNames = (field, add) => TextInput.FileNames(field.textInput, add);
|
||||||
|
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
|
|
||||||
/// <summary>
|
|
||||||
/// An event that is raised when an exception is thrown while trying to copy or paste clipboard contents using TextCopy.
|
|
||||||
/// If no event handlers are added, the exception is ignored.
|
|
||||||
/// </summary>
|
|
||||||
public static event Action<Exception> OnCopyPasteException;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The color that this text field's text should display with
|
/// The color that this text field's text should display with
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -154,24 +144,7 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <param name="text">The text that the text field should contain by default</param>
|
/// <param name="text">The text that the text field should contain by default</param>
|
||||||
/// <param name="multiline">Whether the text field should support multi-line editing</param>
|
/// <param name="multiline">Whether the text field should support multi-line editing</param>
|
||||||
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null, string text = null, bool multiline = false) : base(anchor, size) {
|
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null, string text = null, bool multiline = false) : base(anchor, size) {
|
||||||
this.textInput = new TextInput(null, Vector2.Zero, 1
|
this.textInput = new TextInput(null, Vector2.Zero, 1, null, ClipboardService.SetText, ClipboardService.GetText) {
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
|
|
||||||
, null, s => {
|
|
||||||
try {
|
|
||||||
ClipboardService.SetText(s);
|
|
||||||
} catch (Exception e) {
|
|
||||||
TextField.OnCopyPasteException?.Invoke(e);
|
|
||||||
}
|
|
||||||
}, () => {
|
|
||||||
try {
|
|
||||||
return ClipboardService.GetText();
|
|
||||||
} catch (Exception e) {
|
|
||||||
TextField.OnCopyPasteException?.Invoke(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
) {
|
|
||||||
OnTextChange = (i, s) => this.OnTextChange?.Invoke(this, s),
|
OnTextChange = (i, s) => this.OnTextChange?.Invoke(this, s),
|
||||||
InputRule = (i, s) => this.InputRule.Invoke(this, s)
|
InputRule = (i, s) => this.InputRule.Invoke(this, s)
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
#if FNA
|
|
||||||
using MLEM.Extensions;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui.Elements {
|
namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<RootNamespace>MLEM.Ui</RootNamespace>
|
<RootNamespace>MLEM.Ui</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -21,10 +20,10 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TextCopy" Version="6.2.0" Condition="'$(TargetFramework)'!='net452'" />
|
<PackageReference Include="TextCopy" Version="6.1.0" />
|
||||||
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.FNA.csproj" />
|
||||||
|
|
||||||
<ProjectReference Include="..\FNA\FNA.csproj">
|
<ProjectReference Include="..\FNA\FNA.Core.csproj">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -19,7 +18,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TextCopy" Version="6.2.0" Condition="'$(TargetFramework)'!='net452'" />
|
<PackageReference Include="TextCopy" Version="6.1.0" />
|
||||||
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
<ProjectReference Include="..\MLEM\MLEM.csproj" />
|
||||||
|
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
||||||
|
|
|
@ -1,12 +1,21 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Formatting;
|
||||||
|
using MLEM.Misc;
|
||||||
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
|
using MLEM.Ui.Style;
|
||||||
|
|
||||||
namespace MLEM.Ui.Parsers {
|
namespace MLEM.Ui.Parsers {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A class for parsing Markdown strings into a set of MLEM.Ui elements with styling for each individual <see cref="UiParser.ElementType"/>.
|
/// A class for parsing Markdown strings into a set of MLEM.Ui elements with styling for each individual <see cref="ElementType"/>.
|
||||||
/// To parse, use <see cref="UiParser.Parse"/> or <see cref="UiParser.ParseInto"/>. To style the parsed output, use <see cref="UiParser.Style{T}"/> before parsing.
|
/// To parse, use <see cref="Parse"/> or <see cref="ParseInto"/>. To style the parsed output, use <see cref="Style{T}"/> before parsing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Note that this parser is rather rudimentary and doesn't deal well with very complex Markdown documents. Missing features are as follows:
|
/// Note that this parser is rather rudimentary and doesn't deal well with very complex Markdown documents. Missing features are as follows:
|
||||||
|
@ -17,18 +26,106 @@ namespace MLEM.Ui.Parsers {
|
||||||
/// <item><description>Tables</description></item>
|
/// <item><description>Tables</description></item>
|
||||||
/// </list>
|
/// </list>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class UiMarkdownParser : UiParser {
|
public class UiMarkdownParser {
|
||||||
|
|
||||||
|
private static readonly ElementType[] ElementTypes = EnumHelper.GetValues<ElementType>().ToArray();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base path for markdown images, which is prepended to the image link.
|
||||||
|
/// </summary>
|
||||||
|
public string ImageBasePath;
|
||||||
|
/// <summary>
|
||||||
|
/// An action that is invoked when an image fails to load while parsing.
|
||||||
|
/// This action receives the expected location of the image, as well as the <see cref="Exception"/> that occured.
|
||||||
|
/// </summary>
|
||||||
|
public Action<string, Exception> ImageExceptionHandler;
|
||||||
|
/// <summary>
|
||||||
|
/// The graphics device that should be used when loading images and other graphics-dependent content.
|
||||||
|
/// </summary>
|
||||||
|
public GraphicsDevice GraphicsDevice;
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the font used for inline code as well as code blocks.
|
||||||
|
/// This only has an effect if a font with this name is added to the used <see cref="UiStyle"/>'s <see cref="UiStyle.AdditionalFonts"/>.
|
||||||
|
/// This defaults to "Monospaced" if default styling is applied in <see cref="UiMarkdownParser(bool)"/>.
|
||||||
|
/// </summary>
|
||||||
|
public string CodeFont;
|
||||||
|
|
||||||
|
private readonly Dictionary<ElementType, Action<Element>> elementStyles = new Dictionary<ElementType, Action<Element>>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new UI markdown parser and optionally initializes some default style settings.
|
/// Creates a new UI markdown parser and optionally initializes some default style settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="applyDefaultStyling">Whether default style settings should be applied.</param>
|
/// <param name="applyDefaultStyling">Whether default style settings should be applied.</param>
|
||||||
public UiMarkdownParser(bool applyDefaultStyling = true) : base(applyDefaultStyling) {}
|
public UiMarkdownParser(bool applyDefaultStyling = true) {
|
||||||
|
if (applyDefaultStyling) {
|
||||||
|
this.CodeFont = "Monospaced";
|
||||||
|
this.Style<VerticalSpace>(ElementType.VerticalSpace, v => v.Size = new Vector2(1, 5));
|
||||||
|
for (var i = 0; i < 6; i++) {
|
||||||
|
var level = i;
|
||||||
|
this.Style<Paragraph>(UiMarkdownParser.ElementTypes[Array.IndexOf(UiMarkdownParser.ElementTypes, ElementType.Header1) + i], p => {
|
||||||
|
p.Alignment = TextAlignment.Center;
|
||||||
|
p.TextScaleMultiplier = 2 - level * 0.15F;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
protected override IEnumerable<(ElementType, Element)> ParseUnstyled(string raw) {
|
/// Parses the given markdown string into a set of elements (using <see cref="Parse"/>) and adds them as children to the givem <paramref name="element"/>.
|
||||||
|
/// During this process, the element stylings specified using <see cref="Style{T}"/> are also applied.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="markdown">The markdown to parse.</param>
|
||||||
|
/// <param name="element">The element to add the parsed elements to.</param>
|
||||||
|
/// <returns>The <paramref name="element"/>, for chaining.</returns>
|
||||||
|
public Element ParseInto(string markdown, Element element) {
|
||||||
|
foreach (var (_, e) in this.Parse(markdown))
|
||||||
|
element.AddChild(e);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses the given markdown string into a set of elements and returns them along with their <see cref="ElementType"/>.
|
||||||
|
/// During this process, the element stylings specified using <see cref="Style{T}"/> are also applied.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="markdown">The markdown to parse.</param>
|
||||||
|
/// <returns>The parsed elements.</returns>
|
||||||
|
public IEnumerable<(ElementType, Element)> Parse(string markdown) {
|
||||||
|
foreach (var (t, e) in this.ParseUnstyled(markdown)) {
|
||||||
|
if (this.elementStyles.TryGetValue(t, out var style))
|
||||||
|
style.Invoke(e);
|
||||||
|
yield return (t, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies an action to be invoked when a new element with the given <see cref="ElementType"/> is parsed in <see cref="Parse"/> or <see cref="ParseInto"/>.
|
||||||
|
/// These actions can be used to modify the style properties of the created elements.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="types">The element types that should be styled. Can be a combined flag.</param>
|
||||||
|
/// <param name="style">The action that styles the elements with the given element type.</param>
|
||||||
|
/// <param name="add">Whether the <paramref name="style"/> function should be added to the existing style settings, or replace them.</param>
|
||||||
|
/// <typeparam name="T">The type of elements that the given <see cref="ElementType"/> flags are expected to be.</typeparam>
|
||||||
|
/// <returns>This parser, for chaining.</returns>
|
||||||
|
public UiMarkdownParser Style<T>(ElementType types, Action<T> style, bool add = false) where T : Element {
|
||||||
|
foreach (var type in UiMarkdownParser.ElementTypes) {
|
||||||
|
if (types.HasFlag(type)) {
|
||||||
|
if (add && this.elementStyles.ContainsKey(type)) {
|
||||||
|
this.elementStyles[type] += Action;
|
||||||
|
} else {
|
||||||
|
this.elementStyles[type] = Action;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
|
||||||
|
void Action(Element e) {
|
||||||
|
style.Invoke(e as T ?? throw new ArgumentException($"Expected {typeof(T)} for style action but got {e.GetType()}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<(ElementType, Element)> ParseUnstyled(string markdown) {
|
||||||
var inCodeBlock = false;
|
var inCodeBlock = false;
|
||||||
foreach (var line in raw.Split('\n')) {
|
foreach (var line in markdown.Split('\n')) {
|
||||||
// code blocks
|
// code blocks
|
||||||
if (line.Trim().StartsWith("```")) {
|
if (line.Trim().StartsWith("```")) {
|
||||||
inCodeBlock = !inCodeBlock;
|
inCodeBlock = !inCodeBlock;
|
||||||
|
@ -42,7 +139,7 @@ namespace MLEM.Ui.Parsers {
|
||||||
|
|
||||||
// quotes
|
// quotes
|
||||||
if (line.StartsWith(">")) {
|
if (line.StartsWith(">")) {
|
||||||
yield return (ElementType.Blockquote, new Paragraph(Anchor.AutoLeft, 1, this.ParseParagraph(line.Substring(1).Trim())));
|
yield return (ElementType.Blockquote, new Paragraph(Anchor.AutoLeft, 1, line.Substring(1).Trim()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +152,45 @@ namespace MLEM.Ui.Parsers {
|
||||||
// images
|
// images
|
||||||
var imageMatch = Regex.Match(line, @"!\[\]\(([^)]+)\)");
|
var imageMatch = Regex.Match(line, @"!\[\]\(([^)]+)\)");
|
||||||
if (imageMatch.Success) {
|
if (imageMatch.Success) {
|
||||||
yield return (ElementType.Image, this.ParseImage(imageMatch.Groups[1].Value));
|
if (this.GraphicsDevice == null)
|
||||||
|
throw new NullReferenceException("A markdown parser requires a GraphicsDevice for parsing images");
|
||||||
|
|
||||||
|
TextureRegion image = null;
|
||||||
|
LoadImageAsync();
|
||||||
|
yield return (ElementType.Image, new Image(Anchor.AutoLeft, new Vector2(1, -1), _ => image) {
|
||||||
|
OnDisposed = e => image?.Texture.Dispose()
|
||||||
|
});
|
||||||
|
|
||||||
|
async void LoadImageAsync() {
|
||||||
|
var loc = imageMatch.Groups[1].Value;
|
||||||
|
// only apply the base path for relative files
|
||||||
|
if (this.ImageBasePath != null && !loc.StartsWith("http") && !Path.IsPathRooted(loc))
|
||||||
|
loc = $"{this.ImageBasePath}/{loc}";
|
||||||
|
try {
|
||||||
|
Texture2D tex;
|
||||||
|
if (loc.StartsWith("http")) {
|
||||||
|
using (var client = new HttpClient()) {
|
||||||
|
using (var src = await client.GetStreamAsync(loc)) {
|
||||||
|
using (var memory = new MemoryStream()) {
|
||||||
|
// download the full stream before passing it to texture
|
||||||
|
await src.CopyToAsync(memory);
|
||||||
|
tex = Texture2D.FromStream(this.GraphicsDevice, memory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
using (var stream = Path.IsPathRooted(loc) ? File.OpenRead(loc) : TitleContainer.OpenStream(loc))
|
||||||
|
tex = Texture2D.FromStream(this.GraphicsDevice, stream);
|
||||||
|
}
|
||||||
|
image = new TextureRegion(tex);
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (this.ImageExceptionHandler != null) {
|
||||||
|
this.ImageExceptionHandler.Invoke(loc, e);
|
||||||
|
} else {
|
||||||
|
throw new Exception($"Couldn't parse image {loc}, and no ImageExceptionHandler was set", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +198,8 @@ namespace MLEM.Ui.Parsers {
|
||||||
var parsedHeader = false;
|
var parsedHeader = false;
|
||||||
for (var h = 6; h >= 1; h--) {
|
for (var h = 6; h >= 1; h--) {
|
||||||
if (line.StartsWith(new string('#', h))) {
|
if (line.StartsWith(new string('#', h))) {
|
||||||
var type = UiParser.ElementTypes[Array.IndexOf(UiParser.ElementTypes, ElementType.Header1) + h - 1];
|
var type = UiMarkdownParser.ElementTypes[Array.IndexOf(UiMarkdownParser.ElementTypes, ElementType.Header1) + h - 1];
|
||||||
yield return (type, new Paragraph(Anchor.AutoLeft, 1, this.ParseParagraph(line.Substring(h).Trim())));
|
yield return (type, new Paragraph(Anchor.AutoLeft, 1, line.Substring(h).Trim()));
|
||||||
parsedHeader = true;
|
parsedHeader = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -73,11 +208,7 @@ namespace MLEM.Ui.Parsers {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// parse everything else as a paragraph (with formatting)
|
// parse everything else as a paragraph (with formatting)
|
||||||
yield return (ElementType.Paragraph, new Paragraph(Anchor.AutoLeft, 1, this.ParseParagraph(line)));
|
var par = line;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private string ParseParagraph(string par) {
|
|
||||||
// replace links
|
// replace links
|
||||||
par = Regex.Replace(par, @"<([^>]+)>", "<l $1>$1</l>");
|
par = Regex.Replace(par, @"<([^>]+)>", "<l $1>$1</l>");
|
||||||
par = Regex.Replace(par, @"\[([^\]]+)\]\(([^)]+)\)", "<l $2>$1</l>");
|
par = Regex.Replace(par, @"\[([^\]]+)\]\(([^)]+)\)", "<l $2>$1</l>");
|
||||||
|
@ -89,7 +220,78 @@ namespace MLEM.Ui.Parsers {
|
||||||
par = Regex.Replace(par, @"~~([^~]+)~~", "<st>$1</st>");
|
par = Regex.Replace(par, @"~~([^~]+)~~", "<st>$1</st>");
|
||||||
// replace inline code with custom code font
|
// replace inline code with custom code font
|
||||||
par = Regex.Replace(par, @"`([^`]+)`", $"<f {this.CodeFont}>$1</f>");
|
par = Regex.Replace(par, @"`([^`]+)`", $"<f {this.CodeFont}>$1</f>");
|
||||||
return par;
|
yield return (ElementType.Paragraph, new Paragraph(Anchor.AutoLeft, 1, par));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A flags enumeration used by <see cref="UiMarkdownParser"/> that contains the types of elements that can be parsed and returned in <see cref="Parse"/> or <see cref="UiMarkdownParser.ParseInto"/>.
|
||||||
|
/// This is a flags enumeration so that <see cref="UiMarkdownParser.Style{T}"/> can have multiple element types being styled at the same time.
|
||||||
|
/// </summary>
|
||||||
|
[Flags]
|
||||||
|
public enum ElementType {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A blockquote.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Blockquote = 1,
|
||||||
|
/// <summary>
|
||||||
|
/// A vertical space, which is a gap between multiple markdown paragraphs.
|
||||||
|
/// This element type is a <see cref="VerticalSpace"/>.
|
||||||
|
/// </summary>
|
||||||
|
VerticalSpace = 2,
|
||||||
|
/// <summary>
|
||||||
|
/// An image.
|
||||||
|
/// This element type is an <see cref="Image"/>.
|
||||||
|
/// </summary>
|
||||||
|
Image = 4,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 1.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header1 = 8,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 2.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header2 = 16,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 3.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header3 = 32,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 4.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header4 = 64,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 5.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header5 = 128,
|
||||||
|
/// <summary>
|
||||||
|
/// A header with header level 6.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Header6 = 256,
|
||||||
|
/// <summary>
|
||||||
|
/// A combined flag that contains <see cref="Header1"/> through <see cref="Header6"/>.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Headers = ElementType.Header1 | ElementType.Header2 | ElementType.Header3 | ElementType.Header4 | ElementType.Header5 | ElementType.Header6,
|
||||||
|
/// <summary>
|
||||||
|
/// A paragraph, which is one line of markdown text.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
Paragraph = 512,
|
||||||
|
/// <summary>
|
||||||
|
/// A single line of a code block.
|
||||||
|
/// This element type is a <see cref="Paragraph"/>.
|
||||||
|
/// </summary>
|
||||||
|
CodeBlock = 1024
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,263 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using Microsoft.Xna.Framework;
|
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
|
||||||
using MLEM.Formatting;
|
|
||||||
using MLEM.Textures;
|
|
||||||
using MLEM.Ui.Elements;
|
|
||||||
using MLEM.Ui.Style;
|
|
||||||
|
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
|
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
#else
|
|
||||||
using System.Net;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui.Parsers {
|
|
||||||
/// <summary>
|
|
||||||
/// A base class for parsing various types of formatted strings into a set of MLEM.Ui elements with styling for each individual <see cref="ElementType"/>.
|
|
||||||
/// The only parser currently implemented is <see cref="UiMarkdownParser"/>.
|
|
||||||
/// </summary>
|
|
||||||
public abstract class UiParser {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// An array containing all of the <see cref="ElementType"/> enum values.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly ElementType[] ElementTypes =
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
Enum.GetValues<ElementType>();
|
|
||||||
#else
|
|
||||||
(ElementType[]) Enum.GetValues(typeof(ElementType));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The base path for images, which is prepended to the image link.
|
|
||||||
/// </summary>
|
|
||||||
public string ImageBasePath;
|
|
||||||
/// <summary>
|
|
||||||
/// An action that is invoked when an image fails to load while parsing.
|
|
||||||
/// This action receives the expected location of the image, as well as the <see cref="Exception"/> that occured.
|
|
||||||
/// </summary>
|
|
||||||
public Action<string, Exception> ImageExceptionHandler;
|
|
||||||
/// <summary>
|
|
||||||
/// The graphics device that should be used when loading images and other graphics-dependent content.
|
|
||||||
/// </summary>
|
|
||||||
public GraphicsDevice GraphicsDevice;
|
|
||||||
/// <summary>
|
|
||||||
/// The name of the font used for inline code as well as code blocks.
|
|
||||||
/// This only has an effect if a font with this name is added to the used <see cref="UiStyle"/>'s <see cref="UiStyle.AdditionalFonts"/>.
|
|
||||||
/// This defaults to "Monospaced" if default styling is applied in <see cref="UiParser(bool)"/>.
|
|
||||||
/// </summary>
|
|
||||||
public string CodeFont;
|
|
||||||
|
|
||||||
private readonly Dictionary<ElementType, Action<Element>> elementStyles = new Dictionary<ElementType, Action<Element>>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new UI parser and optionally initializes some default style settings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="applyDefaultStyling">Whether default style settings should be applied.</param>
|
|
||||||
protected UiParser(bool applyDefaultStyling) {
|
|
||||||
if (applyDefaultStyling) {
|
|
||||||
this.CodeFont = "Monospaced";
|
|
||||||
this.Style<VerticalSpace>(ElementType.VerticalSpace, v => v.Size = new Vector2(1, 5));
|
|
||||||
for (var i = 0; i < 6; i++) {
|
|
||||||
var level = i;
|
|
||||||
this.Style<Paragraph>(UiParser.ElementTypes[Array.IndexOf(UiParser.ElementTypes, UiParser.ElementType.Header1) + i], p => {
|
|
||||||
p.Alignment = TextAlignment.Center;
|
|
||||||
p.TextScaleMultiplier = 2 - level * 0.15F;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses the given raw formatted string into a set of elements and returns them along with their <see cref="ElementType"/>.
|
|
||||||
/// This method is used by implementors to parse specific text, and it is used by <see cref="Parse"/> and <see cref="ParseInto"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="raw">The raw string to parse.</param>
|
|
||||||
/// <returns>The parsed elements, without styling.</returns>
|
|
||||||
protected abstract IEnumerable<(ElementType, Element)> ParseUnstyled(string raw);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses the given raw formatted string into a set of elements and returns them along with their <see cref="ElementType"/>.
|
|
||||||
/// During this process, the element stylings specified using <see cref="Style{T}"/> are also applied.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="raw">The raw string to parse.</param>
|
|
||||||
/// <returns>The parsed elements.</returns>
|
|
||||||
public IEnumerable<(ElementType, Element)> Parse(string raw) {
|
|
||||||
foreach (var (t, e) in this.ParseUnstyled(raw)) {
|
|
||||||
if (this.elementStyles.TryGetValue(t, out var style))
|
|
||||||
style.Invoke(e);
|
|
||||||
yield return (t, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses the given raw formatted string into a set of elements (using <see cref="Parse"/>) and adds them as children to the givem <paramref name="element"/>.
|
|
||||||
/// During this process, the element stylings specified using <see cref="Style{T}"/> are also applied.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="raw">The raw string to parse.</param>
|
|
||||||
/// <param name="element">The element to add the parsed elements to.</param>
|
|
||||||
/// <returns>The <paramref name="element"/>, for chaining.</returns>
|
|
||||||
public Element ParseInto(string raw, Element element) {
|
|
||||||
foreach (var (_, e) in this.Parse(raw))
|
|
||||||
element.AddChild(e);
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Specifies an action to be invoked when a new element with the given <see cref="ElementType"/> is parsed in <see cref="Parse"/> or <see cref="ParseInto"/>.
|
|
||||||
/// These actions can be used to modify the style properties of the created elements.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="types">The element types that should be styled. Can be a combined flag.</param>
|
|
||||||
/// <param name="style">The action that styles the elements with the given element type.</param>
|
|
||||||
/// <param name="add">Whether the <paramref name="style"/> function should be added to the existing style settings, or replace them.</param>
|
|
||||||
/// <typeparam name="T">The type of elements that the given <see cref="ElementType"/> flags are expected to be.</typeparam>
|
|
||||||
/// <returns>This parser, for chaining.</returns>
|
|
||||||
public UiParser Style<T>(ElementType types, Action<T> style, bool add = false) where T : Element {
|
|
||||||
foreach (var type in UiParser.ElementTypes) {
|
|
||||||
if (types.HasFlag(type)) {
|
|
||||||
if (add && this.elementStyles.ContainsKey(type)) {
|
|
||||||
this.elementStyles[type] += Action;
|
|
||||||
} else {
|
|
||||||
this.elementStyles[type] = Action;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
|
|
||||||
void Action(Element e) {
|
|
||||||
style.Invoke(e as T ?? throw new ArgumentException($"Expected {typeof(T)} for style action but got {e.GetType()}"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parses the given path into a <see cref="Image"/> element by loading it from disk or downloading it from the internet.
|
|
||||||
/// Note that, for a <paramref name="path"/> that doesn't start with <c>http</c> and isn't rooted, the <see cref="ImageBasePath"/> is prepended automatically.
|
|
||||||
/// This method invokes an asynchronouns action, meaning the <see cref="Image"/>'s <see cref="Image.Texture"/> will likely not have loaded in when this method returns.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The absolute, relative or web path to the image.</param>
|
|
||||||
/// <returns>The loaded image.</returns>
|
|
||||||
/// <exception cref="NullReferenceException">Thrown if <see cref="GraphicsDevice"/> is null, or if there is an <see cref="Exception"/> loading the image and <see cref="ImageExceptionHandler"/> is unset.</exception>
|
|
||||||
protected Image ParseImage(string path) {
|
|
||||||
if (this.GraphicsDevice == null)
|
|
||||||
throw new NullReferenceException("A UI parser requires a GraphicsDevice for parsing images");
|
|
||||||
|
|
||||||
TextureRegion image = null;
|
|
||||||
return new Image(Anchor.AutoLeft, new Vector2(1, -1), _ => image) {
|
|
||||||
OnAddedToUi = e => {
|
|
||||||
if (image == null)
|
|
||||||
LoadImageAsync();
|
|
||||||
},
|
|
||||||
OnRemovedFromUi = e => {
|
|
||||||
image?.Texture.Dispose();
|
|
||||||
image = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
async void LoadImageAsync() {
|
|
||||||
// only apply the base path for relative files
|
|
||||||
if (this.ImageBasePath != null && !path.StartsWith("http") && !Path.IsPathRooted(path))
|
|
||||||
path = $"{this.ImageBasePath}/{path}";
|
|
||||||
try {
|
|
||||||
Texture2D tex;
|
|
||||||
if (path.StartsWith("http")) {
|
|
||||||
byte[] src;
|
|
||||||
#if NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
|
|
||||||
using (var client = new HttpClient())
|
|
||||||
src = await client.GetByteArrayAsync(path);
|
|
||||||
#else
|
|
||||||
using (var client = new WebClient())
|
|
||||||
src = await client.DownloadDataTaskAsync(path);
|
|
||||||
#endif
|
|
||||||
using (var memory = new MemoryStream(src))
|
|
||||||
tex = Texture2D.FromStream(this.GraphicsDevice, memory);
|
|
||||||
} else {
|
|
||||||
using (var stream = Path.IsPathRooted(path) ? File.OpenRead(path) : TitleContainer.OpenStream(path))
|
|
||||||
tex = Texture2D.FromStream(this.GraphicsDevice, stream);
|
|
||||||
}
|
|
||||||
image = new TextureRegion(tex);
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (this.ImageExceptionHandler != null) {
|
|
||||||
this.ImageExceptionHandler.Invoke(path, e);
|
|
||||||
} else {
|
|
||||||
throw new NullReferenceException($"Couldn't parse image {path}, and no ImageExceptionHandler was set", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A flags enumeration used by <see cref="UiParser"/> that contains the types of elements that can be parsed and returned in <see cref="Parse"/> or <see cref="ParseInto"/>.
|
|
||||||
/// This is a flags enumeration so that <see cref="Style{T}"/> can have multiple element types being styled at the same time.
|
|
||||||
/// </summary>
|
|
||||||
[Flags]
|
|
||||||
public enum ElementType {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A blockquote.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Blockquote = 1,
|
|
||||||
/// <summary>
|
|
||||||
/// A vertical space, which is a gap between multiple paragraphs.
|
|
||||||
/// This element type is a <see cref="VerticalSpace"/>.
|
|
||||||
/// </summary>
|
|
||||||
VerticalSpace = 2,
|
|
||||||
/// <summary>
|
|
||||||
/// An image.
|
|
||||||
/// This element type is an <see cref="Image"/>.
|
|
||||||
/// </summary>
|
|
||||||
Image = 4,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 1.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header1 = 8,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 2.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header2 = 16,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 3.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header3 = 32,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 4.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header4 = 64,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 5.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header5 = 128,
|
|
||||||
/// <summary>
|
|
||||||
/// A header with header level 6.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Header6 = 256,
|
|
||||||
/// <summary>
|
|
||||||
/// A combined flag that contains <see cref="Header1"/> through <see cref="Header6"/>.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Headers = ElementType.Header1 | ElementType.Header2 | ElementType.Header3 | ElementType.Header4 | ElementType.Header5 | ElementType.Header6,
|
|
||||||
/// <summary>
|
|
||||||
/// A paragraph, which is one line (or non-vertically spaced section) of text.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
Paragraph = 512,
|
|
||||||
/// <summary>
|
|
||||||
/// A single line of a code block.
|
|
||||||
/// This element type is a <see cref="Paragraph"/>.
|
|
||||||
/// </summary>
|
|
||||||
CodeBlock = 1024
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,9 +5,9 @@ using MLEM.Font;
|
||||||
using MLEM.Formatting;
|
using MLEM.Formatting;
|
||||||
using MLEM.Formatting.Codes;
|
using MLEM.Formatting.Codes;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
using MLEM.Sound;
|
|
||||||
using MLEM.Textures;
|
using MLEM.Textures;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
|
using SoundEffectInfo = MLEM.Sound.SoundEffectInfo;
|
||||||
|
|
||||||
namespace MLEM.Ui.Style {
|
namespace MLEM.Ui.Style {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -216,7 +216,7 @@ namespace MLEM.Ui.Style {
|
||||||
/// The color that a <see cref="Paragraph"/>'s <see cref="Paragraph.Link"/> codes should have.
|
/// The color that a <see cref="Paragraph"/>'s <see cref="Paragraph.Link"/> codes should have.
|
||||||
/// This value is passed to <see cref="LinkCode"/>.
|
/// This value is passed to <see cref="LinkCode"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color? LinkColor = Color.CornflowerBlue;
|
public Color? LinkColor;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A set of additional fonts that can be used for the <c><f FontName></c> formatting code
|
/// A set of additional fonts that can be used for the <c><f FontName></c> formatting code
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -9,10 +9,6 @@ using MLEM.Misc;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
|
|
||||||
#if NET452
|
|
||||||
using MLEM.Extensions;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Ui {
|
namespace MLEM.Ui {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UiControls holds and manages all of the controls for a <see cref="UiSystem"/>.
|
/// UiControls holds and manages all of the controls for a <see cref="UiSystem"/>.
|
||||||
|
@ -161,29 +157,29 @@ namespace MLEM.Ui {
|
||||||
this.Input.Update();
|
this.Input.Update();
|
||||||
this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanBeActive);
|
this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanBeActive);
|
||||||
|
|
||||||
|
// MOUSE INPUT
|
||||||
if (this.HandleMouse) {
|
if (this.HandleMouse) {
|
||||||
var mousedNow = this.GetElementUnderPos(new Vector2(this.Input.ViewportMousePosition.X, this.Input.ViewportMousePosition.Y));
|
var mousedNow = this.GetElementUnderPos(new Vector2(this.Input.ViewportMousePosition.X, this.Input.ViewportMousePosition.Y));
|
||||||
this.SetMousedElement(mousedNow);
|
this.SetMousedElement(mousedNow);
|
||||||
|
|
||||||
if (this.Input.IsPressedAvailable(MouseButton.Left)) {
|
if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Left)) {
|
||||||
this.IsAutoNavMode = false;
|
this.IsAutoNavMode = false;
|
||||||
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
|
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
|
||||||
this.SelectElement(this.ActiveRoot, selectedNow);
|
this.SelectElement(this.ActiveRoot, selectedNow);
|
||||||
if (mousedNow != null && mousedNow.CanBePressed) {
|
if (mousedNow != null && mousedNow.CanBePressed) {
|
||||||
this.System.InvokeOnElementPressed(mousedNow);
|
this.System.InvokeOnElementPressed(mousedNow);
|
||||||
this.Input.TryConsumePressed(MouseButton.Left);
|
this.Input.TryConsumeMouseButtonPressed(MouseButton.Left);
|
||||||
}
|
}
|
||||||
} else if (this.Input.IsPressedAvailable(MouseButton.Right)) {
|
} else if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Right)) {
|
||||||
this.IsAutoNavMode = false;
|
this.IsAutoNavMode = false;
|
||||||
if (mousedNow != null && mousedNow.CanBePressed) {
|
if (mousedNow != null && mousedNow.CanBePressed) {
|
||||||
this.System.InvokeOnElementSecondaryPressed(mousedNow);
|
this.System.InvokeOnElementSecondaryPressed(mousedNow);
|
||||||
this.Input.TryConsumePressed(MouseButton.Right);
|
this.Input.TryConsumeMouseButtonPressed(MouseButton.Right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.SetMousedElement(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KEYBOARD INPUT
|
||||||
if (this.HandleKeyboard) {
|
if (this.HandleKeyboard) {
|
||||||
if (this.KeyboardButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) {
|
if (this.KeyboardButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) {
|
||||||
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) {
|
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) {
|
||||||
|
@ -196,7 +192,7 @@ namespace MLEM.Ui {
|
||||||
}
|
}
|
||||||
this.KeyboardButtons.TryConsumePressed(this.Input, this.GamepadIndex);
|
this.KeyboardButtons.TryConsumePressed(this.Input, this.GamepadIndex);
|
||||||
}
|
}
|
||||||
} else if (this.Input.IsPressedAvailable(Keys.Tab)) {
|
} else if (this.Input.IsKeyPressedAvailable(Keys.Tab)) {
|
||||||
this.IsAutoNavMode = true;
|
this.IsAutoNavMode = true;
|
||||||
// tab or shift-tab to next or previous element
|
// tab or shift-tab to next or previous element
|
||||||
var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift);
|
var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift);
|
||||||
|
@ -205,11 +201,12 @@ namespace MLEM.Ui {
|
||||||
next = this.SelectedElement.GetTabNextElement(backward, next);
|
next = this.SelectedElement.GetTabNextElement(backward, next);
|
||||||
if (next != this.SelectedElement) {
|
if (next != this.SelectedElement) {
|
||||||
this.SelectElement(this.ActiveRoot, next);
|
this.SelectElement(this.ActiveRoot, next);
|
||||||
this.Input.TryConsumePressed(Keys.Tab);
|
this.Input.TryConsumeKeyPressed(Keys.Tab);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TOUCH INPUT
|
||||||
if (this.HandleTouch) {
|
if (this.HandleTouch) {
|
||||||
if (this.Input.GetViewportGesture(GestureType.Tap, out var tap)) {
|
if (this.Input.GetViewportGesture(GestureType.Tap, out var tap)) {
|
||||||
this.IsAutoNavMode = false;
|
this.IsAutoNavMode = false;
|
||||||
|
@ -237,10 +234,9 @@ namespace MLEM.Ui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.SetTouchedElement(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GAMEPAD INPUT
|
||||||
if (this.HandleGamepad) {
|
if (this.HandleGamepad) {
|
||||||
if (this.GamepadButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) {
|
if (this.GamepadButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) {
|
||||||
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) {
|
if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Ui.Elements;
|
using MLEM.Ui.Elements;
|
||||||
|
|
||||||
namespace MLEM.Ui {
|
namespace MLEM.Ui {
|
||||||
|
|
|
@ -206,12 +206,7 @@ namespace MLEM.Ui {
|
||||||
/// <param name="style">The style settings that this ui should have. Use <see cref="UntexturedStyle"/> for the default, untextured style.</param>
|
/// <param name="style">The style settings that this ui should have. Use <see cref="UntexturedStyle"/> for the default, untextured style.</param>
|
||||||
/// <param name="inputHandler">The input handler that this ui's <see cref="UiControls"/> should use. If none is supplied, a new input handler is created for this ui.</param>
|
/// <param name="inputHandler">The input handler that this ui's <see cref="UiControls"/> should use. If none is supplied, a new input handler is created for this ui.</param>
|
||||||
/// <param name="automaticViewport">If this value is set to true, the ui system's <see cref="Viewport"/> will be set automatically based on the <see cref="GameWindow"/>'s size. Defaults to true.</param>
|
/// <param name="automaticViewport">If this value is set to true, the ui system's <see cref="Viewport"/> will be set automatically based on the <see cref="GameWindow"/>'s size. Defaults to true.</param>
|
||||||
/// <param name="hasFontModifierFormatting">Whether default font modifier codes should be added to this ui system's <see cref="TextFormatter"/>, including bold, italic, strikethrough, shadow, subscript, and more.</param>
|
public UiSystem(Game game, UiStyle style, InputHandler inputHandler = null, bool automaticViewport = true) : base(game) {
|
||||||
/// <param name="hasColorFormatting">Whether default color codes should be added to this ui system's <see cref="TextFormatter"/>, including all <see cref="Color"/> values and the ability to use custom colors.</param>
|
|
||||||
/// <param name="hasAnimationFormatting">Whether default animation codes should be added to this ui system's <see cref="TextFormatter"/>, namely the wobbly animation.</param>
|
|
||||||
/// <param name="hasMacroFormatting">Whether default macros should be added to this ui system's <see cref="TextFormatter"/>, including TeX's ~ non-breaking space and more.</param>
|
|
||||||
/// <param name="hasUiFormatting">Whether <see cref="UiSystem"/>-based formatting codes should be added to this ui system's <see cref="TextFormatter"/>, including <see cref="Paragraph.Link"/> codes and font switching.</param>
|
|
||||||
public UiSystem(Game game, UiStyle style, InputHandler inputHandler = null, bool automaticViewport = true, bool hasFontModifierFormatting = true, bool hasColorFormatting = true, bool hasAnimationFormatting = true, bool hasMacroFormatting = true, bool hasUiFormatting = true) : base(game) {
|
|
||||||
this.Controls = new UiControls(this, inputHandler);
|
this.Controls = new UiControls(this, inputHandler);
|
||||||
this.style = style;
|
this.style = style;
|
||||||
|
|
||||||
|
@ -253,15 +248,13 @@ namespace MLEM.Ui {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.TextFormatter = new TextFormatter(hasFontModifierFormatting, hasColorFormatting, hasAnimationFormatting, hasMacroFormatting);
|
this.TextFormatter = new TextFormatter();
|
||||||
if (hasUiFormatting) {
|
|
||||||
this.TextFormatter.Codes.Add(new Regex("<l(?: ([^>]+))?>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F,
|
this.TextFormatter.Codes.Add(new Regex("<l(?: ([^>]+))?>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F,
|
||||||
t => this.Controls.MousedElement is Paragraph.Link l1 && l1.Token == t || this.Controls.TouchedElement is Paragraph.Link l2 && l2.Token == t,
|
t => this.Controls.MousedElement is Paragraph.Link l1 && l1.Token == t || this.Controls.TouchedElement is Paragraph.Link l2 && l2.Token == t,
|
||||||
d => this.Style.LinkColor));
|
this.Style.LinkColor));
|
||||||
this.TextFormatter.Codes.Add(new Regex("<f ([^>]+)>"), (_, m, r) => new FontCode(m, r,
|
this.TextFormatter.Codes.Add(new Regex("<f ([^>]+)>"), (_, m, r) => new FontCode(m, r,
|
||||||
f => this.Style.AdditionalFonts != null && this.Style.AdditionalFonts.TryGetValue(m.Groups[1].Value, out var c) ? c : f));
|
f => this.Style.AdditionalFonts != null && this.Style.AdditionalFonts.TryGetValue(m.Groups[1].Value, out var c) ? c : f));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update this ui system, querying the necessary events and updating each element's data.
|
/// Update this ui system, querying the necessary events and updating each element's data.
|
||||||
|
@ -352,7 +345,9 @@ namespace MLEM.Ui {
|
||||||
var root = new RootElement(name, element, this);
|
var root = new RootElement(name, element, this);
|
||||||
this.rootElements.Add(root);
|
this.rootElements.Add(root);
|
||||||
root.Element.AndChildren(e => {
|
root.Element.AndChildren(e => {
|
||||||
e.AddedToUi(this, root);
|
e.Root = root;
|
||||||
|
e.System = this;
|
||||||
|
root.InvokeOnElementAdded(e);
|
||||||
e.SetAreaDirty();
|
e.SetAreaDirty();
|
||||||
});
|
});
|
||||||
this.OnRootAdded?.Invoke(root);
|
this.OnRootAdded?.Invoke(root);
|
||||||
|
@ -372,7 +367,9 @@ namespace MLEM.Ui {
|
||||||
this.rootElements.Remove(root);
|
this.rootElements.Remove(root);
|
||||||
this.Controls.SelectElement(root, null);
|
this.Controls.SelectElement(root, null);
|
||||||
root.Element.AndChildren(e => {
|
root.Element.AndChildren(e => {
|
||||||
e.RemovedFromUi();
|
e.Root = null;
|
||||||
|
e.System = null;
|
||||||
|
root.InvokeOnElementRemoved(e);
|
||||||
e.SetAreaDirty();
|
e.SetAreaDirty();
|
||||||
});
|
});
|
||||||
this.OnRootRemoved?.Invoke(root);
|
this.OnRootRemoved?.Invoke(root);
|
||||||
|
@ -564,7 +561,7 @@ namespace MLEM.Ui {
|
||||||
/// This property returns <see langword="true"/> if <see cref="CanSelectContent"/> is <see langword="true"/> and the <see cref="Element"/> is not hidden, or if it has been set to <see langword="true"/> manually.
|
/// This property returns <see langword="true"/> if <see cref="CanSelectContent"/> is <see langword="true"/> and the <see cref="Element"/> is not hidden, or if it has been set to <see langword="true"/> manually.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanBeActive {
|
public bool CanBeActive {
|
||||||
get => this.canBeActive || !this.Element.IsHidden && this.CanSelectContent;
|
get => this.canBeActive || (!this.Element.IsHidden && this.CanSelectContent);
|
||||||
set => this.canBeActive = value;
|
set => this.canBeActive = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,7 +570,7 @@ namespace MLEM.Ui {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event Element.GenericCallback OnElementAdded;
|
public event Element.GenericCallback OnElementAdded;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that is invoked when a <see cref="Element"/> is removed rom this root element or any of its children.
|
/// Event that is invoked when a <see cref="Element"/> is removed rom this root element of any of its children.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event Element.GenericCallback OnElementRemoved;
|
public event Element.GenericCallback OnElementRemoved;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
12
MLEM.sln
12
MLEM.sln
|
@ -18,9 +18,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Data", "MLEM.Data\MLEM
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Templates", "MLEM.Templates\MLEM.Templates.csproj", "{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Templates", "MLEM.Templates\MLEM.Templates.csproj", "{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{53D52C3F-67FB-4F32-A794-EAB140BBFC11}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demos.Android", "Demos.Android\Demos.Android.csproj", "{410C0262-131C-4D0E-910D-D01B4F7143E0}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demos.Android", "Demos.Android\Demos.Android.csproj", "{DA234B13-AA46-4C03-8CFC-A969A2AEAEF2}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{53D52C3F-67FB-4F32-A794-EAB140BBFC11}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -64,13 +64,13 @@ Global
|
||||||
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Release|Any CPU.Build.0 = Release|Any CPU
|
{C2A2CFED-C9E8-4675-BD66-EFC3DB210977}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{410C0262-131C-4D0E-910D-D01B4F7143E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{410C0262-131C-4D0E-910D-D01B4F7143E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{410C0262-131C-4D0E-910D-D01B4F7143E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{410C0262-131C-4D0E-910D-D01B4F7143E0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.Build.0 = Release|Any CPU
|
{53D52C3F-67FB-4F32-A794-EAB140BBFC11}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{DA234B13-AA46-4C03-8CFC-A969A2AEAEF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{DA234B13-AA46-4C03-8CFC-A969A2AEAEF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{DA234B13-AA46-4C03-8CFC-A969A2AEAEF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{DA234B13-AA46-4C03-8CFC-A969A2AEAEF2}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MLEM.Extensions {
|
namespace MLEM.Extensions {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A set of extensions for dealing with <see cref="char"/>
|
/// A set of extensions for dealing with <see cref="char"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
|
|
||||||
public static class CharExtensions {
|
public static class CharExtensions {
|
||||||
|
|
||||||
private static readonly Dictionary<char, string> Cache = new Dictionary<char, string>();
|
private static readonly Dictionary<char, string> Cache = new Dictionary<char, string>();
|
||||||
|
@ -16,7 +14,6 @@ namespace MLEM.Extensions {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="c">The character to turn into a string</param>
|
/// <param name="c">The character to turn into a string</param>
|
||||||
/// <returns>A string representing the character</returns>
|
/// <returns>A string representing the character</returns>
|
||||||
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
|
|
||||||
public static string ToCachedString(this char c) {
|
public static string ToCachedString(this char c) {
|
||||||
if (!CharExtensions.Cache.TryGetValue(c, out var ret)) {
|
if (!CharExtensions.Cache.TryGetValue(c, out var ret)) {
|
||||||
ret = c.ToString();
|
ret = c.ToString();
|
||||||
|
|
|
@ -51,29 +51,5 @@ namespace MLEM.Extensions {
|
||||||
return combos;
|
return combos;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if NET452
|
|
||||||
/// <summary>Appends a value to the end of the sequence.</summary>
|
|
||||||
/// <param name="source">A sequence of values.</param>
|
|
||||||
/// <param name="element">The value to append to <paramref name="source"/>.</param>
|
|
||||||
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
|
|
||||||
/// <returns>A new sequence that ends with <paramref name="element"/>.</returns>
|
|
||||||
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T element) {
|
|
||||||
foreach (var src in source)
|
|
||||||
yield return src;
|
|
||||||
yield return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Prepends a value to the beginning of the sequence.</summary>
|
|
||||||
/// <param name="source">A sequence of values.</param>
|
|
||||||
/// <param name="element">The value to prepend to <paramref name="source"/>.</param>
|
|
||||||
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
|
|
||||||
/// <returns>A new sequence that begins with <paramref name="element"/>.</returns>
|
|
||||||
public static IEnumerable<T> Prepend<T>(this IEnumerable<T> source, T element) {
|
|
||||||
yield return element;
|
|
||||||
foreach (var src in source)
|
|
||||||
yield return src;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -304,41 +304,6 @@ namespace MLEM.Extensions {
|
||||||
public static Vector2 ToVector2(this Point point) {
|
public static Vector2 ToVector2(this Point point) {
|
||||||
return new Vector2(point.X, point.Y);
|
return new Vector2(point.X, point.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deconstruction method for <see cref="Point"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="point">The point to deconstruct.</param>
|
|
||||||
/// <param name="x"></param>
|
|
||||||
/// <param name="y"></param>
|
|
||||||
public static void Deconstruct(this Point point, out int x, out int y) {
|
|
||||||
x = point.X;
|
|
||||||
y = point.Y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deconstruction method for <see cref="Vector2"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="vector">The vector to deconstruct.</param>
|
|
||||||
/// <param name="x"></param>
|
|
||||||
/// <param name="y"></param>
|
|
||||||
public static void Deconstruct(this Vector2 vector, out float x, out float y) {
|
|
||||||
x = vector.X;
|
|
||||||
y = vector.Y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deconstruction method for <see cref="Vector3"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="vector">The vector to deconstruct.</param>
|
|
||||||
/// <param name="x"></param>
|
|
||||||
/// <param name="y"></param>
|
|
||||||
/// <param name="z"></param>
|
|
||||||
public static void Deconstruct(this Vector3 vector, out float x, out float y, out float z) {
|
|
||||||
x = vector.X;
|
|
||||||
y = vector.Y;
|
|
||||||
z = vector.Z;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,37 +55,5 @@ namespace MLEM.Extensions {
|
||||||
throw new IndexOutOfRangeException();
|
throw new IndexOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a random floating-point number that is greater than or equal to 0, and less than <paramref name="maxValue"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="random">The random.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <returns>A single-precision floating point number that is greater than or equal to 0, and less than <paramref name="maxValue"/>.</returns>
|
|
||||||
public static float NextSingle(this Random random, float maxValue) {
|
|
||||||
return maxValue * random.NextSingle();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a random floating-point number that is greater than or equal to <paramref name="minValue"/>, and less than <paramref name="maxValue"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="random">The random.</param>
|
|
||||||
/// <param name="minValue">The (inclusive) minimum value.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <returns>A single-precision floating point number that is greater than or equal to <paramref name="minValue"/>, and less than <paramref name="maxValue"/>.</returns>
|
|
||||||
public static float NextSingle(this Random random, float minValue, float maxValue) {
|
|
||||||
return (maxValue - minValue) * random.NextSingle() + minValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if !NET6_0_OR_GREATER
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a random floating-point number that is greater than or equal to 0, and less than 1.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="random">The random.</param>
|
|
||||||
/// <returns>A single-precision floating point number that is greater than or equal to 0, and less than 1.</returns>
|
|
||||||
public static float NextSingle(this Random random) {
|
|
||||||
return (float) random.NextDouble();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MLEM.Font {
|
|
||||||
/// <summary>
|
|
||||||
/// A code point source is a wrapper around a <see cref="string"/> or <see cref="StringBuilder"/> that allows retrieving UTF-32 code points at a given index using <see cref="GetCodePoint"/>. Additionally, it allows enumerating every code point in the underlying <see cref="string"/> or <see cref="StringBuilder"/>. This class also contains <see cref="ToString(int)"/>, which converts a code point into its <see cref="string"/> representation, but caches the result to avoid allocating excess memory.
|
|
||||||
/// </summary>
|
|
||||||
public readonly struct CodePointSource : IEnumerable<int> {
|
|
||||||
|
|
||||||
private static readonly Dictionary<int, string> StringCache = new Dictionary<int, string>();
|
|
||||||
|
|
||||||
private readonly string strg;
|
|
||||||
private readonly StringBuilder builder;
|
|
||||||
private char this[int index] => this.strg?[index] ?? this.builder[index];
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The length of this code point, in characters.
|
|
||||||
/// Note that this is not representative of the amount of code points in this source.
|
|
||||||
/// </summary>
|
|
||||||
public int Length => this.strg?.Length ?? this.builder.Length;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new code point source from the given <see cref="string"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="strg">The <see cref="string"/> whose code points to inspect.</param>
|
|
||||||
public CodePointSource(string strg) {
|
|
||||||
this.strg = strg;
|
|
||||||
this.builder = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new code point source from the given <see cref="StringBuilder"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder">The <see cref="StringBuilder"/> whose code points to inspect.</param>
|
|
||||||
public CodePointSource(StringBuilder builder) {
|
|
||||||
this.strg = null;
|
|
||||||
this.builder = builder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the code point at the given <paramref name="index"/> in this code point source's underlying string, where the index is measured in characters and not code points.
|
|
||||||
/// The resulting code point will either be a single <see cref="char"/> cast to an <see cref="int"/>, at which point the returned length will be 1, or a UTF-32 <see cref="int"/> character made up of two <see cref="char"/> values, at which point the returned length will be 2.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="index">The index at which to return the code point, which is measured in characters.</param>
|
|
||||||
/// <param name="indexLowSurrogate">Whether the <paramref name="index"/> represents a low surrogate. If this is <see langword="false"/>, the <paramref name="index"/> represents a high surrogate and the low surrogate will be looked for in the following character. If this is <see langword="true"/>, the <paramref name="index"/> represents a low surrogate and the high surrogate will be looked for in the previous character.</param>
|
|
||||||
/// <returns>The code point at the given location, as well as its length.</returns>
|
|
||||||
public (int CodePoint, int Length) GetCodePoint(int index, bool indexLowSurrogate = false) {
|
|
||||||
var curr = this[index];
|
|
||||||
if (indexLowSurrogate) {
|
|
||||||
if (index > 0) {
|
|
||||||
var high = this[index - 1];
|
|
||||||
if (char.IsSurrogatePair(high, curr))
|
|
||||||
return (char.ConvertToUtf32(high, curr), 2);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (index < this.Length - 1) {
|
|
||||||
var low = this[index + 1];
|
|
||||||
if (char.IsSurrogatePair(curr, low))
|
|
||||||
return (char.ConvertToUtf32(curr, low), 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (curr, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns an enumerator that iterates through the collection.</summary>
|
|
||||||
/// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
|
|
||||||
/// <filterpriority>1</filterpriority>
|
|
||||||
public IEnumerator<int> GetEnumerator() {
|
|
||||||
var index = 0;
|
|
||||||
while (index < this.Length) {
|
|
||||||
var (codePoint, length) = this.GetCodePoint(index);
|
|
||||||
yield return codePoint;
|
|
||||||
index += length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns an enumerator that iterates through a collection.</summary>
|
|
||||||
/// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
|
|
||||||
/// <filterpriority>2</filterpriority>
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() {
|
|
||||||
return this.GetEnumerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts the given UTF-32 <paramref name="codePoint"/> into a string using <see cref="char.ConvertFromUtf32"/>, but caches the result in a <see cref="Dictionary{TKey,TValue}"/> cache to avoid allocating excess memory.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="codePoint">The UTF-32 code point to convert.</param>
|
|
||||||
/// <returns>The string representation of the code point.</returns>
|
|
||||||
public static string ToString(int codePoint) {
|
|
||||||
if (!CodePointSource.StringCache.TryGetValue(codePoint, out var ret)) {
|
|
||||||
ret = char.ConvertFromUtf32(codePoint);
|
|
||||||
CodePointSource.StringCache.Add(codePoint, ret);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Font {
|
namespace MLEM.Font {
|
||||||
|
@ -50,36 +50,35 @@ namespace MLEM.Font {
|
||||||
public abstract float LineHeight { get; }
|
public abstract float LineHeight { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Measures the width of the given code point with the default scale for use in <see cref="MeasureString(string,bool)"/>.
|
/// Measures the width of the given character with the default scale for use in <see cref="MeasureString(string,bool)"/>.
|
||||||
/// Note that this method does not support <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/> for most generic fonts, which is why <see cref="MeasureString(string,bool)"/> should be used even for single characters.
|
/// Note that this method does not support <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/> for most generic fonts, which is why <see cref="MeasureString(string,bool)"/> should be used even for single characters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="codePoint">The code point whose width to calculate</param>
|
/// <param name="c">The character whose width to calculate</param>
|
||||||
/// <returns>The width of the given character with the default scale</returns>
|
/// <returns>The width of the given character with the default scale</returns>
|
||||||
protected abstract float MeasureCharacter(int codePoint);
|
protected abstract float MeasureChar(char c);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draws the given code point with the given data for use in <see cref="DrawString(Microsoft.Xna.Framework.Graphics.SpriteBatch,System.Text.StringBuilder,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Color,float,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Graphics.SpriteEffects,float)"/>.
|
/// Draws the given character with the given data for use in <see cref="DrawString(Microsoft.Xna.Framework.Graphics.SpriteBatch,System.Text.StringBuilder,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Color,float,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Graphics.SpriteEffects,float)"/>.
|
||||||
/// Note that this method is only called internally.
|
/// Note that this method is only called internally.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="batch">The sprite batch to draw with.</param>
|
/// <param name="batch">The sprite batch to draw with.</param>
|
||||||
/// <param name="codePoint">The code point which will be drawn.</param>
|
/// <param name="cString">A string representation of the character which will be drawn.</param>
|
||||||
/// <param name="character">A string representation of the character which will be drawn.</param>
|
|
||||||
/// <param name="position">The drawing location on screen.</param>
|
/// <param name="position">The drawing location on screen.</param>
|
||||||
/// <param name="color">A color mask.</param>
|
/// <param name="color">A color mask.</param>
|
||||||
/// <param name="rotation">A rotation of this character.</param>
|
/// <param name="rotation">A rotation of this character.</param>
|
||||||
/// <param name="scale">A scaling of this character.</param>
|
/// <param name="scale">A scaling of this character.</param>
|
||||||
/// <param name="effects">Modificators for drawing. Can be combined.</param>
|
/// <param name="effects">Modificators for drawing. Can be combined.</param>
|
||||||
/// <param name="layerDepth">A depth of the layer of this character.</param>
|
/// <param name="layerDepth">A depth of the layer of this character.</param>
|
||||||
protected abstract void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth);
|
protected abstract void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth);
|
||||||
|
|
||||||
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
this.DrawString(batch, new CodePointSource(text), position, color, rotation, origin, scale, effects, layerDepth);
|
this.DrawString(batch, new CharSource(text), position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
this.DrawString(batch, new CodePointSource(text), position, color, rotation, origin, scale, effects, layerDepth);
|
this.DrawString(batch, new CharSource(text), position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
///<inheritdoc cref="SpriteBatch.DrawString(SpriteFont,string,Vector2,Color,float,Vector2,float,SpriteEffects,float)"/>
|
||||||
|
@ -104,19 +103,19 @@ namespace MLEM.Font {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Measures the width of the given string when drawn with this font's underlying font.
|
/// Measures the width of the given string when drawn with this font's underlying font.
|
||||||
/// This method uses <see cref="MeasureCharacter"/> internally to calculate the size of known characters and calculates additional characters like <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/>.
|
/// This method uses <see cref="MeasureChar"/> internally to calculate the size of known characters and calculates additional characters like <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/>.
|
||||||
/// If the text contains newline characters (\n), the size returned will represent a rectangle that encompasses the width of the longest line and the string's full height.
|
/// If the text contains newline characters (\n), the size returned will represent a rectangle that encompasses the width of the longest line and the string's full height.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The text whose size to calculate</param>
|
/// <param name="text">The text whose size to calculate</param>
|
||||||
/// <param name="ignoreTrailingSpaces">Whether trailing whitespace should be ignored in the returned size, causing the end of each line to be effectively trimmed</param>
|
/// <param name="ignoreTrailingSpaces">Whether trailing whitespace should be ignored in the returned size, causing the end of each line to be effectively trimmed</param>
|
||||||
/// <returns>The size of the string when drawn with this font</returns>
|
/// <returns>The size of the string when drawn with this font</returns>
|
||||||
public Vector2 MeasureString(string text, bool ignoreTrailingSpaces = false) {
|
public Vector2 MeasureString(string text, bool ignoreTrailingSpaces = false) {
|
||||||
return this.MeasureString(new CodePointSource(text), ignoreTrailingSpaces);
|
return this.MeasureString(new CharSource(text), ignoreTrailingSpaces, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="MeasureString(string,bool)"/>
|
/// <inheritdoc cref="MeasureString(string,bool)"/>
|
||||||
public Vector2 MeasureString(StringBuilder text, bool ignoreTrailingSpaces = false) {
|
public Vector2 MeasureString(StringBuilder text, bool ignoreTrailingSpaces = false) {
|
||||||
return this.MeasureString(new CodePointSource(text), ignoreTrailingSpaces);
|
return this.MeasureString(new CharSource(text), ignoreTrailingSpaces, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -130,12 +129,12 @@ namespace MLEM.Font {
|
||||||
/// <param name="ellipsis">The characters to add to the end of the string if it is too long</param>
|
/// <param name="ellipsis">The characters to add to the end of the string if it is too long</param>
|
||||||
/// <returns>The truncated string, or the same string if it is shorter than the maximum width</returns>
|
/// <returns>The truncated string, or the same string if it is shorter than the maximum width</returns>
|
||||||
public string TruncateString(string text, float width, float scale, bool fromBack = false, string ellipsis = "") {
|
public string TruncateString(string text, float width, float scale, bool fromBack = false, string ellipsis = "") {
|
||||||
return GenericFont.TruncateString(Enumerable.Repeat(new DecoratedCodePointSource(new CodePointSource(text), this, 0), 1), width, scale, fromBack, ellipsis).First().ToString();
|
return this.TruncateString(new CharSource(text), width, scale, fromBack, ellipsis, null).ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="TruncateString(string,float,float,bool,string)"/>
|
/// <inheritdoc cref="TruncateString(string,float,float,bool,string)"/>
|
||||||
public StringBuilder TruncateString(StringBuilder text, float width, float scale, bool fromBack = false, string ellipsis = "") {
|
public StringBuilder TruncateString(StringBuilder text, float width, float scale, bool fromBack = false, string ellipsis = "") {
|
||||||
return GenericFont.TruncateString(Enumerable.Repeat(new DecoratedCodePointSource(new CodePointSource(text), this, 0), 1), width, scale, fromBack, ellipsis).First();
|
return this.TruncateString(new CharSource(text), width, scale, fromBack, ellipsis, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -166,23 +165,22 @@ namespace MLEM.Font {
|
||||||
/// <param name="scale">The scale to use for width measurements</param>
|
/// <param name="scale">The scale to use for width measurements</param>
|
||||||
/// <returns>The split string as an enumerable of split sections</returns>
|
/// <returns>The split string as an enumerable of split sections</returns>
|
||||||
public IEnumerable<string> SplitStringSeparate(string text, float width, float scale) {
|
public IEnumerable<string> SplitStringSeparate(string text, float width, float scale) {
|
||||||
return GenericFont.SplitStringSeparate(Enumerable.Repeat(new DecoratedCodePointSource(new CodePointSource(text), this, 0), 1), width, scale).First();
|
return this.SplitStringSeparate(new CharSource(text), width, scale, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="SplitStringSeparate(string,float,float)"/>
|
/// <inheritdoc cref="SplitStringSeparate(string,float,float)"/>
|
||||||
public IEnumerable<string> SplitStringSeparate(StringBuilder text, float width, float scale) {
|
public IEnumerable<string> SplitStringSeparate(StringBuilder text, float width, float scale) {
|
||||||
return GenericFont.SplitStringSeparate(Enumerable.Repeat(new DecoratedCodePointSource(new CodePointSource(text), this, 0), 1), width, scale).First();
|
return this.SplitStringSeparate(new CharSource(text), width, scale, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector2 MeasureString(CodePointSource text, bool ignoreTrailingSpaces) {
|
internal Vector2 MeasureString(CharSource text, bool ignoreTrailingSpaces, Func<int, GenericFont> fontFunction) {
|
||||||
var size = Vector2.Zero;
|
var size = Vector2.Zero;
|
||||||
if (text.Length <= 0)
|
if (text.Length <= 0)
|
||||||
return size;
|
return size;
|
||||||
var xOffset = 0F;
|
var xOffset = 0F;
|
||||||
var index = 0;
|
for (var i = 0; i < text.Length; i++) {
|
||||||
while (index < text.Length) {
|
var font = fontFunction?.Invoke(i) ?? this;
|
||||||
var (codePoint, length) = text.GetCodePoint(index);
|
switch (text[i]) {
|
||||||
switch (codePoint) {
|
|
||||||
case '\n':
|
case '\n':
|
||||||
xOffset = 0;
|
xOffset = 0;
|
||||||
size.Y += this.LineHeight;
|
size.Y += this.LineHeight;
|
||||||
|
@ -191,39 +189,107 @@ namespace MLEM.Font {
|
||||||
xOffset += this.LineHeight;
|
xOffset += this.LineHeight;
|
||||||
break;
|
break;
|
||||||
case GenericFont.Nbsp:
|
case GenericFont.Nbsp:
|
||||||
xOffset += this.MeasureCharacter(' ');
|
xOffset += font.MeasureChar(' ');
|
||||||
break;
|
break;
|
||||||
case GenericFont.Zwsp:
|
case GenericFont.Zwsp:
|
||||||
// don't add width for a zero-width space
|
// don't add width for a zero-width space
|
||||||
break;
|
break;
|
||||||
case ' ':
|
case ' ':
|
||||||
if (ignoreTrailingSpaces && GenericFont.IsTrailingSpace(text, index)) {
|
if (ignoreTrailingSpaces && GenericFont.IsTrailingSpace(text, i)) {
|
||||||
// if this is a trailing space, we can skip remaining spaces too
|
// if this is a trailing space, we can skip remaining spaces too
|
||||||
index = text.Length - 1;
|
i = text.Length - 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
xOffset += this.MeasureCharacter(' ');
|
xOffset += font.MeasureChar(' ');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
xOffset += this.MeasureCharacter(codePoint);
|
xOffset += font.MeasureChar(text[i]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// increase x size if this line is the longest
|
// increase x size if this line is the longest
|
||||||
if (xOffset > size.X)
|
if (xOffset > size.X)
|
||||||
size.X = xOffset;
|
size.X = xOffset;
|
||||||
index += length;
|
|
||||||
}
|
}
|
||||||
// include the last line's height too!
|
// include the last line's height too!
|
||||||
size.Y += this.LineHeight;
|
size.Y += this.LineHeight;
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawString(SpriteBatch batch, CodePointSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
internal StringBuilder TruncateString(CharSource text, float width, float scale, bool fromBack, string ellipsis, Func<int, GenericFont> fontFunction) {
|
||||||
|
var total = new StringBuilder();
|
||||||
|
for (var i = 0; i < text.Length; i++) {
|
||||||
|
if (fromBack) {
|
||||||
|
total.Insert(0, text[text.Length - 1 - i]);
|
||||||
|
} else {
|
||||||
|
total.Append(text[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var font = fontFunction?.Invoke(i) ?? this;
|
||||||
|
if (font.MeasureString(total + ellipsis).X * scale >= width) {
|
||||||
|
if (fromBack) {
|
||||||
|
return total.Remove(0, 1).Insert(0, ellipsis);
|
||||||
|
} else {
|
||||||
|
return total.Remove(total.Length - 1, 1).Append(ellipsis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal IEnumerable<string> SplitStringSeparate(CharSource text, float width, float scale, Func<int, GenericFont> fontFunction) {
|
||||||
|
var currWidth = 0F;
|
||||||
|
var lastSpaceIndex = -1;
|
||||||
|
var widthSinceLastSpace = 0F;
|
||||||
|
var curr = new StringBuilder();
|
||||||
|
for (var i = 0; i < text.Length; i++) {
|
||||||
|
var c = text[i];
|
||||||
|
if (c == '\n') {
|
||||||
|
// fake split at pre-defined new lines
|
||||||
|
curr.Append(c);
|
||||||
|
lastSpaceIndex = -1;
|
||||||
|
widthSinceLastSpace = 0;
|
||||||
|
currWidth = 0;
|
||||||
|
} else {
|
||||||
|
var font = fontFunction?.Invoke(i) ?? this;
|
||||||
|
var cWidth = font.MeasureString(c.ToCachedString()).X * scale;
|
||||||
|
if (c == ' ' || c == GenericFont.Emsp || c == GenericFont.Zwsp) {
|
||||||
|
// remember the location of this (breaking!) space
|
||||||
|
lastSpaceIndex = curr.Length;
|
||||||
|
widthSinceLastSpace = 0;
|
||||||
|
} else if (currWidth + cWidth >= width) {
|
||||||
|
// check if this line contains a space
|
||||||
|
if (lastSpaceIndex < 0) {
|
||||||
|
// if there is no last space, the word is longer than a line so we split here
|
||||||
|
yield return curr.ToString();
|
||||||
|
currWidth = 0;
|
||||||
|
curr.Clear();
|
||||||
|
} else {
|
||||||
|
// split after the last space
|
||||||
|
yield return curr.ToString().Substring(0, lastSpaceIndex + 1);
|
||||||
|
curr.Remove(0, lastSpaceIndex + 1);
|
||||||
|
// we need to restore the width accumulated since the last space for the new line
|
||||||
|
currWidth = widthSinceLastSpace;
|
||||||
|
}
|
||||||
|
widthSinceLastSpace = 0;
|
||||||
|
lastSpaceIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add current character
|
||||||
|
currWidth += cWidth;
|
||||||
|
widthSinceLastSpace += cWidth;
|
||||||
|
curr.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (curr.Length > 0)
|
||||||
|
yield return curr.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawString(SpriteBatch batch, CharSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
var (flipX, flipY) = (0F, 0F);
|
var (flipX, flipY) = (0F, 0F);
|
||||||
var flippedV = (effects & SpriteEffects.FlipVertically) != 0;
|
var flippedV = (effects & SpriteEffects.FlipVertically) != 0;
|
||||||
var flippedH = (effects & SpriteEffects.FlipHorizontally) != 0;
|
var flippedH = (effects & SpriteEffects.FlipHorizontally) != 0;
|
||||||
if (flippedV || flippedH) {
|
if (flippedV || flippedH) {
|
||||||
var size = this.MeasureString(text, false);
|
var size = this.MeasureString(text, false, null);
|
||||||
if (flippedH) {
|
if (flippedH) {
|
||||||
origin.X *= -1;
|
origin.X *= -1;
|
||||||
flipX = -size.X;
|
flipX = -size.X;
|
||||||
|
@ -252,170 +318,53 @@ namespace MLEM.Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
var offset = Vector2.Zero;
|
var offset = Vector2.Zero;
|
||||||
var index = 0;
|
for (var i = 0; i < text.Length; i++) {
|
||||||
while (index < text.Length) {
|
var c = text[i];
|
||||||
var (codePoint, length) = text.GetCodePoint(index);
|
if (c == '\n') {
|
||||||
if (codePoint == '\n') {
|
|
||||||
offset.X = 0;
|
offset.X = 0;
|
||||||
offset.Y += this.LineHeight;
|
offset.Y += this.LineHeight;
|
||||||
} else {
|
continue;
|
||||||
var character = CodePointSource.ToString(codePoint);
|
}
|
||||||
var charSize = this.MeasureString(character);
|
|
||||||
|
var cString = c.ToCachedString();
|
||||||
|
var cSize = this.MeasureString(cString);
|
||||||
|
|
||||||
var charPos = offset;
|
var charPos = offset;
|
||||||
if (flippedH)
|
if (flippedH)
|
||||||
charPos.X += charSize.X;
|
charPos.X += cSize.X;
|
||||||
if (flippedV)
|
if (flippedV)
|
||||||
charPos.Y += charSize.Y - this.LineHeight;
|
charPos.Y += cSize.Y - this.LineHeight;
|
||||||
Vector2.Transform(ref charPos, ref trans, out charPos);
|
Vector2.Transform(ref charPos, ref trans, out charPos);
|
||||||
|
|
||||||
this.DrawCharacter(batch, codePoint, character, charPos, color, rotation, scale, effects, layerDepth);
|
this.DrawChar(batch, cString, charPos, color, rotation, scale, effects, layerDepth);
|
||||||
offset.X += charSize.X;
|
offset.X += cSize.X;
|
||||||
}
|
|
||||||
index += length;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IEnumerable<IEnumerable<string>> SplitStringSeparate(IEnumerable<DecoratedCodePointSource> text, float maxWidth, float scale) {
|
private static bool IsTrailingSpace(CharSource s, int index) {
|
||||||
var currWidth = 0F;
|
for (var i = index + 1; i < s.Length; i++) {
|
||||||
var lastSpacePart = -1;
|
if (s[i] != ' ')
|
||||||
var lastSpaceIndex = -1;
|
|
||||||
var widthSinceLastSpace = 0F;
|
|
||||||
var curr = new StringBuilder();
|
|
||||||
var fullSplit = new List<List<string>>();
|
|
||||||
foreach (var part in text) {
|
|
||||||
var partSplit = new List<string>();
|
|
||||||
AddWidth(partSplit, part.ExtraWidth * scale, true);
|
|
||||||
|
|
||||||
var index = 0;
|
|
||||||
while (index < part.Source.Length) {
|
|
||||||
var (codePoint, length) = part.Source.GetCodePoint(index);
|
|
||||||
if (codePoint == '\n') {
|
|
||||||
// fake split at pre-defined new lines
|
|
||||||
curr.Append('\n');
|
|
||||||
lastSpacePart = -1;
|
|
||||||
lastSpaceIndex = -1;
|
|
||||||
widthSinceLastSpace = 0;
|
|
||||||
currWidth = 0;
|
|
||||||
} else {
|
|
||||||
var character = CodePointSource.ToString(codePoint);
|
|
||||||
var charWidth = part.Font.MeasureString(character).X * scale;
|
|
||||||
if (codePoint == ' ' || codePoint == GenericFont.Emsp || codePoint == GenericFont.Zwsp) {
|
|
||||||
// remember the location of this (breaking!) space
|
|
||||||
lastSpacePart = fullSplit.Count;
|
|
||||||
lastSpaceIndex = curr.Length;
|
|
||||||
widthSinceLastSpace = 0;
|
|
||||||
// we never want to insert a line break before a space!
|
|
||||||
AddWidth(partSplit, charWidth, false);
|
|
||||||
} else {
|
|
||||||
AddWidth(partSplit, charWidth, true);
|
|
||||||
}
|
|
||||||
curr.Append(character);
|
|
||||||
}
|
|
||||||
index += length;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (curr.Length > 0) {
|
|
||||||
partSplit.Add(curr.ToString());
|
|
||||||
curr.Clear();
|
|
||||||
}
|
|
||||||
fullSplit.Add(partSplit);
|
|
||||||
}
|
|
||||||
return fullSplit;
|
|
||||||
|
|
||||||
void AddWidth(ICollection<string> partSplit, float width, bool canBreakHere) {
|
|
||||||
if (canBreakHere && currWidth + width >= maxWidth) {
|
|
||||||
// check if this line contains a space
|
|
||||||
if (lastSpaceIndex < 0) {
|
|
||||||
// if there is no last space, the word is longer than a line so we split here
|
|
||||||
partSplit.Add(curr.ToString());
|
|
||||||
curr.Clear();
|
|
||||||
currWidth = 0;
|
|
||||||
} else {
|
|
||||||
if (lastSpacePart < fullSplit.Count) {
|
|
||||||
// the last space exists, but isn't a part of curr, so we have to backtrack and split the previous token
|
|
||||||
var prevPart = fullSplit[lastSpacePart];
|
|
||||||
var prevCurr = prevPart[prevPart.Count - 1];
|
|
||||||
prevPart[prevPart.Count - 1] = prevCurr.Substring(0, lastSpaceIndex + 1);
|
|
||||||
prevPart.Add(prevCurr.Substring(lastSpaceIndex + 1));
|
|
||||||
} else {
|
|
||||||
// split after the last space
|
|
||||||
partSplit.Add(curr.ToString().Substring(0, lastSpaceIndex + 1));
|
|
||||||
curr.Remove(0, lastSpaceIndex + 1);
|
|
||||||
}
|
|
||||||
// we need to restore the width accumulated since the last space for the new line
|
|
||||||
currWidth = widthSinceLastSpace;
|
|
||||||
}
|
|
||||||
widthSinceLastSpace = 0;
|
|
||||||
lastSpacePart = -1;
|
|
||||||
lastSpaceIndex = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
currWidth += width;
|
|
||||||
widthSinceLastSpace += width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static IEnumerable<StringBuilder> TruncateString(IEnumerable<DecoratedCodePointSource> text, float maxWidth, float scale, bool fromBack, string ellipsis) {
|
|
||||||
var total = new StringBuilder();
|
|
||||||
var extraWidth = 0F;
|
|
||||||
var endReached = false;
|
|
||||||
foreach (var part in fromBack ? text.Reverse() : text) {
|
|
||||||
var curr = new StringBuilder();
|
|
||||||
// if we reached the end previously, all the other parts should just be empty
|
|
||||||
if (!endReached) {
|
|
||||||
extraWidth += part.ExtraWidth * scale;
|
|
||||||
var index = 0;
|
|
||||||
while (index < part.Source.Length) {
|
|
||||||
var innerIndex = fromBack ? part.Source.Length - 1 - index : index;
|
|
||||||
var (codePoint, length) = part.Source.GetCodePoint(innerIndex, fromBack);
|
|
||||||
var character = CodePointSource.ToString(codePoint);
|
|
||||||
if (fromBack) {
|
|
||||||
curr.Insert(0, character);
|
|
||||||
total.Insert(0, character);
|
|
||||||
} else {
|
|
||||||
curr.Append(character);
|
|
||||||
total.Append(character);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (part.Font.MeasureString(new CodePointSource(total + ellipsis), false).X * scale + extraWidth >= maxWidth) {
|
|
||||||
if (fromBack) {
|
|
||||||
curr.Remove(0, length).Insert(0, ellipsis);
|
|
||||||
total.Remove(0, length).Insert(0, ellipsis);
|
|
||||||
} else {
|
|
||||||
curr.Remove(curr.Length - length, length).Append(ellipsis);
|
|
||||||
total.Remove(total.Length - length, length).Append(ellipsis);
|
|
||||||
}
|
|
||||||
endReached = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
index += length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yield return curr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsTrailingSpace(CodePointSource s, int index) {
|
|
||||||
while (index < s.Length) {
|
|
||||||
var (codePoint, length) = s.GetCodePoint(index);
|
|
||||||
if (codePoint != ' ')
|
|
||||||
return false;
|
return false;
|
||||||
index += length;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal readonly struct DecoratedCodePointSource {
|
internal readonly struct CharSource {
|
||||||
|
|
||||||
public readonly CodePointSource Source;
|
private readonly string strg;
|
||||||
public readonly GenericFont Font;
|
private readonly StringBuilder builder;
|
||||||
public readonly float ExtraWidth;
|
|
||||||
|
|
||||||
public DecoratedCodePointSource(CodePointSource source, GenericFont font, float extraWidth) {
|
public int Length => this.strg?.Length ?? this.builder.Length;
|
||||||
this.Source = source;
|
public char this[int index] => this.strg?[index] ?? this.builder[index];
|
||||||
this.Font = font;
|
|
||||||
this.ExtraWidth = extraWidth;
|
public CharSource(string strg) {
|
||||||
|
this.strg = strg;
|
||||||
|
this.builder = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSource(StringBuilder builder) {
|
||||||
|
this.strg = null;
|
||||||
|
this.builder = builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
#if !FNA
|
|
||||||
using System.Linq;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Font {
|
namespace MLEM.Font {
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -34,13 +32,13 @@ namespace MLEM.Font {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override float MeasureCharacter(int codePoint) {
|
protected override float MeasureChar(char c) {
|
||||||
return this.Font.MeasureString(CodePointSource.ToString(codePoint)).X;
|
return this.Font.MeasureString(c.ToCachedString()).X;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
protected override void DrawChar(SpriteBatch batch, string cString, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, character, position, color, rotation, Vector2.Zero, scale, effects, layerDepth);
|
batch.DrawString(this.Font, cString, position, color, rotation, Vector2.Zero, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SpriteFont SetDefaults(SpriteFont font) {
|
private static SpriteFont SetDefaults(SpriteFont font) {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
@ -69,11 +68,6 @@ namespace MLEM.Formatting.Codes {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="Token.GetSelfWidth"/>
|
|
||||||
public virtual float GetSelfWidth(GenericFont font) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update this formatting code's animations etc.
|
/// Update this formatting code's animations etc.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -86,13 +80,12 @@ namespace MLEM.Formatting.Codes {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="font">The font that is used</param>
|
/// <param name="font">The font that is used</param>
|
||||||
/// <returns>The replacement string for this formatting code</returns>
|
/// <returns>The replacement string for this formatting code</returns>
|
||||||
[Obsolete("This method is deprecated. Use GetSelfWidth to add additional width to this code and DrawSelf or DrawCharacter to draw additional items.")]
|
|
||||||
public virtual string GetReplacementString(GenericFont font) {
|
public virtual string GetReplacementString(GenericFont font) {
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="Formatting.Token.DrawCharacter"/>
|
/// <inheritdoc cref="Formatting.Token.DrawCharacter"/>
|
||||||
public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,8 +26,8 @@ namespace MLEM.Formatting.Codes {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override float GetSelfWidth(GenericFont font) {
|
public override string GetReplacementString(GenericFont font) {
|
||||||
return font.LineHeight;
|
return GenericFont.Emsp.ToCachedString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -41,6 +41,12 @@ namespace MLEM.Formatting.Codes {
|
||||||
batch.Draw(this.image.CurrentRegion, new RectangleF(pos, new Vector2(font.LineHeight * scale)), actualColor);
|
batch.Draw(this.image.CurrentRegion, new RectangleF(pos, new Vector2(font.LineHeight * scale)), actualColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
|
// we don't want to draw the first (space) character (in case it is set to a missing character in FNA)
|
||||||
|
return indexInToken == 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -9,19 +9,14 @@ namespace MLEM.Formatting.Codes {
|
||||||
public class LinkCode : UnderlineCode {
|
public class LinkCode : UnderlineCode {
|
||||||
|
|
||||||
private readonly Func<Token, bool> isSelected;
|
private readonly Func<Token, bool> isSelected;
|
||||||
private readonly Func<Color, Color?> color;
|
private readonly Color? color;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func<Token, bool> isSelected, Func<Color, Color?> color) :
|
public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func<Token, bool> isSelected, Color? color = null) : base(match, regex, thickness, yOffset) {
|
||||||
base(match, regex, thickness, yOffset) {
|
|
||||||
this.isSelected = isSelected;
|
this.isSelected = isSelected;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func<Token, bool> isSelected, Color? color = null) :
|
|
||||||
this(match, regex, thickness, yOffset, isSelected, d => color) {}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if this link formatting code is currently selected or hovered over, based on the selection function.
|
/// Returns true if this link formatting code is currently selected or hovered over, based on the selection function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -36,13 +31,13 @@ namespace MLEM.Formatting.Codes {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override Color? GetColor(Color defaultPick) {
|
public override Color? GetColor(Color defaultPick) {
|
||||||
return this.color.Invoke(defaultPick);
|
return this.color;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
// since we inherit from UnderlineCode, we can just call base if selected
|
// since we inherit from UnderlineCode, we can just call base if selected
|
||||||
return this.IsSelected() && base.DrawCharacter(time, batch, codePoint, character, token, indexInToken, ref pos, font, ref color, ref scale, depth);
|
return this.IsSelected() && base.DrawCharacter(time, batch, c, cString, token, indexInToken, ref pos, font, ref color, ref scale, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,8 @@ namespace MLEM.Formatting.Codes {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
font.DrawString(batch, character, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth);
|
font.DrawString(batch, cString, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth);
|
||||||
// we return false since we still want regular drawing to occur
|
// we return false since we still want regular drawing to occur
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Microsoft.Xna.Framework;
|
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
|
||||||
using MLEM.Font;
|
|
||||||
|
|
||||||
namespace MLEM.Formatting.Codes {
|
|
||||||
/// <inheritdoc />
|
|
||||||
public class SubSupCode : Code {
|
|
||||||
|
|
||||||
private readonly float offset;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public SubSupCode(Match match, Regex regex, float offset) : base(match, regex) {
|
|
||||||
this.offset = offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
|
||||||
pos.Y += this.offset * font.LineHeight * scale;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -19,11 +19,11 @@ namespace MLEM.Formatting.Codes {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
// don't underline spaces at the end of lines
|
// don't underline spaces at the end of lines
|
||||||
if (codePoint == ' ' && token.DisplayString.Length > indexInToken + 1 && token.DisplayString[indexInToken + 1] == '\n')
|
if (c == ' ' && token.DisplayString.Length > indexInToken + 1 && token.DisplayString[indexInToken + 1] == '\n')
|
||||||
return false;
|
return false;
|
||||||
var size = font.MeasureString(character) * scale;
|
var size = font.MeasureString(cString) * scale;
|
||||||
var t = size.Y * this.thickness;
|
var t = size.Y * this.thickness;
|
||||||
batch.Draw(batch.GetBlankTexture(), new RectangleF(pos.X, pos.Y + this.yOffset * size.Y - t, size.X, t), color);
|
batch.Draw(batch.GetBlankTexture(), new RectangleF(pos.X, pos.Y + this.yOffset * size.Y - t, size.X, t), color);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace MLEM.Formatting.Codes {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
var offset = new Vector2(0, (float) Math.Sin(token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale);
|
var offset = new Vector2(0, (float) Math.Sin(token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale);
|
||||||
pos += offset;
|
pos += offset;
|
||||||
// we return false since we still want regular drawing to occur, we just changed the position
|
// we return false since we still want regular drawing to occur, we just changed the position
|
||||||
|
|
|
@ -29,15 +29,10 @@ namespace MLEM.Formatting {
|
||||||
public readonly Dictionary<Regex, Macro> Macros = new Dictionary<Regex, Macro>();
|
public readonly Dictionary<Regex, Macro> Macros = new Dictionary<Regex, Macro>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new text formatter with an optional set of default formatting codes.
|
/// Creates a new text formatter with a set of default formatting codes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="hasFontModifiers">Whether default font modifier codes should be added, including bold, italic, strikethrough, shadow, subscript, and more.</param>
|
public TextFormatter() {
|
||||||
/// <param name="hasColors">Whether default color codes should be added, including all <see cref="Color"/> values and the ability to use custom colors.</param>
|
// font codes
|
||||||
/// <param name="hasAnimations">Whether default animation codes should be added, namely the wobbly animation.</param>
|
|
||||||
/// <param name="hasMacros">Whether default macros should be added, including TeX's ~ non-breaking space and more.</param>
|
|
||||||
public TextFormatter(bool hasFontModifiers = true, bool hasColors = true, bool hasAnimations = true, bool hasMacros = true) {
|
|
||||||
// general font modifier codes
|
|
||||||
if (hasFontModifiers) {
|
|
||||||
this.Codes.Add(new Regex("<b>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Bold));
|
this.Codes.Add(new Regex("<b>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Bold));
|
||||||
this.Codes.Add(new Regex("<i>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Italic));
|
this.Codes.Add(new Regex("<i>"), (f, m, r) => new FontCode(m, r, fnt => fnt.Italic));
|
||||||
this.Codes.Add(new Regex(@"<s(?: #([0-9\w]{6,8}) (([+-.0-9]*)))?>"), (f, m, r) => new ShadowCode(m, r,
|
this.Codes.Add(new Regex(@"<s(?: #([0-9\w]{6,8}) (([+-.0-9]*)))?>"), (f, m, r) => new ShadowCode(m, r,
|
||||||
|
@ -45,14 +40,8 @@ namespace MLEM.Formatting {
|
||||||
new Vector2(float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var offset) ? offset : 2)));
|
new Vector2(float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var offset) ? offset : 2)));
|
||||||
this.Codes.Add(new Regex("<u>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.85F));
|
this.Codes.Add(new Regex("<u>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.85F));
|
||||||
this.Codes.Add(new Regex("<st>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.55F));
|
this.Codes.Add(new Regex("<st>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.55F));
|
||||||
this.Codes.Add(new Regex(@"<sub(?: ([+-.0-9]+))?>"), (f, m, r) => new SubSupCode(m, r,
|
|
||||||
float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var off) ? off : 0.15F));
|
|
||||||
this.Codes.Add(new Regex(@"<sup(?: ([+-.0-9]+))?>"), (f, m, r) => new SubSupCode(m, r,
|
|
||||||
float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var off) ? -off : -0.25F));
|
|
||||||
}
|
|
||||||
|
|
||||||
// color codes
|
// color codes
|
||||||
if (hasColors) {
|
|
||||||
foreach (var c in typeof(Color).GetProperties()) {
|
foreach (var c in typeof(Color).GetProperties()) {
|
||||||
if (c.GetGetMethod().IsStatic) {
|
if (c.GetGetMethod().IsStatic) {
|
||||||
var value = (Color) c.GetValue(null);
|
var value = (Color) c.GetValue(null);
|
||||||
|
@ -60,23 +49,18 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m, r) => new ColorCode(m, r, ColorHelper.FromHexString(m.Groups[1].Value)));
|
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m, r) => new ColorCode(m, r, ColorHelper.FromHexString(m.Groups[1].Value)));
|
||||||
}
|
|
||||||
|
|
||||||
// animation codes
|
// animation codes
|
||||||
if (hasAnimations) {
|
|
||||||
this.Codes.Add(new Regex(@"<a wobbly(?: ([+-.0-9]*) ([+-.0-9]*))?>"), (f, m, r) => new WobblyCode(m, r,
|
this.Codes.Add(new Regex(@"<a wobbly(?: ([+-.0-9]*) ([+-.0-9]*))?>"), (f, m, r) => new WobblyCode(m, r,
|
||||||
float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var mod) ? mod : 5,
|
float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var mod) ? mod : 5,
|
||||||
float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var heightMod) ? heightMod : 1 / 8F));
|
float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var heightMod) ? heightMod : 1 / 8F));
|
||||||
}
|
|
||||||
|
|
||||||
// control codes
|
// control codes
|
||||||
this.Codes.Add(new Regex(@"</(\w+)>"), (f, m, r) => new SimpleEndCode(m, r, m.Groups[1].Value));
|
this.Codes.Add(new Regex(@"</(\w+)>"), (f, m, r) => new SimpleEndCode(m, r, m.Groups[1].Value));
|
||||||
|
|
||||||
// macros
|
// macros
|
||||||
if (hasMacros) {
|
this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToCachedString());
|
||||||
this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToString());
|
this.Macros.Add(new Regex("<n>"), (f, m, r) => '\n'.ToCachedString());
|
||||||
this.Macros.Add(new Regex("<n>"), (f, m, r) => '\n'.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -84,8 +68,8 @@ namespace MLEM.Formatting {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="font">The font to use for tokenization. Note that this font needs to be the same that will later be used for splitting, measuring and/or drawing.</param>
|
/// <param name="font">The font to use for tokenization. Note that this font needs to be the same that will later be used for splitting, measuring and/or drawing.</param>
|
||||||
/// <param name="s">The string to tokenize</param>
|
/// <param name="s">The string to tokenize</param>
|
||||||
/// <param name="alignment">The text alignment that should be used. This alignment can later be changed using <see cref="TokenizedString.Realign"/>.</param>
|
/// <param name="alignment">The text alignment that should be used. Note that this alignment needs to be the same that will later be used for splitting, measuring and/or drawing.</param>
|
||||||
/// <returns>The tokenized string.</returns>
|
/// <returns></returns>
|
||||||
public TokenizedString Tokenize(GenericFont font, string s, TextAlignment alignment = TextAlignment.Left) {
|
public TokenizedString Tokenize(GenericFont font, string s, TextAlignment alignment = TextAlignment.Left) {
|
||||||
// resolve macros
|
// resolve macros
|
||||||
s = this.ResolveMacros(s);
|
s = this.ResolveMacros(s);
|
||||||
|
@ -158,11 +142,8 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string StripFormatting(GenericFont font, string s, IEnumerable<Code> codes) {
|
private static string StripFormatting(GenericFont font, string s, IEnumerable<Code> codes) {
|
||||||
foreach (var code in codes) {
|
foreach (var code in codes)
|
||||||
#pragma warning disable CS0618
|
|
||||||
s = code.Regex.Replace(s, code.GetReplacementString(font));
|
s = code.Regex.Replace(s, code.GetReplacementString(font));
|
||||||
#pragma warning restore CS0618
|
|
||||||
}
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,19 +80,6 @@ namespace MLEM.Formatting {
|
||||||
return defaultPick;
|
return defaultPick;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the width of the token itself, including all of the <see cref="Code"/> instances that this token contains.
|
|
||||||
/// Note that this method does not return the width of this token's <see cref="DisplayString"/>, but only the width that the codes themselves take up.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="font">The font to use for calculating the width.</param>
|
|
||||||
/// <returns>The width of this token itself.</returns>
|
|
||||||
public float GetSelfWidth(GenericFont font) {
|
|
||||||
var ret = 0F;
|
|
||||||
foreach (var code in this.AppliedCodes)
|
|
||||||
ret += code.GetSelfWidth(font);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draws the token itself, including all of the <see cref="Code"/> instances that this token contains.
|
/// Draws the token itself, including all of the <see cref="Code"/> instances that this token contains.
|
||||||
/// Note that, to draw the token's actual string, <see cref="DrawCharacter"/> is used.
|
/// Note that, to draw the token's actual string, <see cref="DrawCharacter"/> is used.
|
||||||
|
@ -110,30 +97,31 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draws a given code point using this token's formatting options.
|
/// Draws a given character using this token's formatting options.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time</param>
|
/// <param name="time">The time</param>
|
||||||
/// <param name="batch">The sprite batch to use</param>
|
/// <param name="batch">The sprite batch to use</param>
|
||||||
/// <param name="codePoint">The code point of the character to draw</param>
|
/// <param name="c">The character to draw</param>
|
||||||
/// <param name="character">The string representation of the character to draw</param>
|
/// <param name="cString">A single-character string that contains the character to draw</param>
|
||||||
/// <param name="indexInToken">The index within this token that the character is at</param>
|
/// <param name="indexInToken">The index within this token that the character is at</param>
|
||||||
/// <param name="pos">The position to draw the token at</param>
|
/// <param name="pos">The position to draw the token at</param>
|
||||||
/// <param name="font">The font to use to draw</param>
|
/// <param name="font">The font to use to draw</param>
|
||||||
/// <param name="color">The color to draw with</param>
|
/// <param name="color">The color to draw with</param>
|
||||||
/// <param name="scale">The scale to draw at</param>
|
/// <param name="scale">The scale to draw at</param>
|
||||||
/// <param name="depth">The depth to draw at</param>
|
/// <param name="depth">The depth to draw at</param>
|
||||||
public void DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, string character, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
||||||
foreach (var code in this.AppliedCodes) {
|
foreach (var code in this.AppliedCodes) {
|
||||||
if (code.DrawCharacter(time, batch, codePoint, character, this, indexInToken, ref pos, font, ref color, ref scale, depth))
|
if (code.DrawCharacter(time, batch, c, cString, this, indexInToken, ref pos, font, ref color, ref scale, depth))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no code drew, we have to do it ourselves
|
// if no code drew, we have to do it ourselves
|
||||||
font.DrawString(batch, character, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, depth);
|
font.DrawString(batch, cString, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of rectangles that encompass this token's area.
|
/// Gets a list of rectangles that encompass this token's area.
|
||||||
|
/// Note that more than one rectangle is only returned if the string has been split.
|
||||||
/// This can be used to invoke events when the mouse is hovered over the token, for example.
|
/// This can be used to invoke events when the mouse is hovered over the token, for example.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stringPos">The position that the string is drawn at</param>
|
/// <param name="stringPos">The position that the string is drawn at</param>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -9,6 +8,7 @@ using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Formatting.Codes;
|
using MLEM.Formatting.Codes;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
|
using static MLEM.Font.GenericFont;
|
||||||
|
|
||||||
namespace MLEM.Formatting {
|
namespace MLEM.Formatting {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -40,7 +40,6 @@ namespace MLEM.Formatting {
|
||||||
public readonly Code[] AllCodes;
|
public readonly Code[] AllCodes;
|
||||||
private string modifiedString;
|
private string modifiedString;
|
||||||
private float initialInnerOffset;
|
private float initialInnerOffset;
|
||||||
private RectangleF area;
|
|
||||||
|
|
||||||
internal TokenizedString(GenericFont font, TextAlignment alignment, string rawString, string strg, Token[] tokens) {
|
internal TokenizedString(GenericFont font, TextAlignment alignment, string rawString, string strg, Token[] tokens) {
|
||||||
this.RawString = rawString;
|
this.RawString = rawString;
|
||||||
|
@ -53,7 +52,7 @@ namespace MLEM.Formatting {
|
||||||
foreach (var code in this.AllCodes)
|
foreach (var code in this.AllCodes)
|
||||||
code.Tokens = new ReadOnlyCollection<Token>(this.Tokens.Where(t => t.AppliedCodes.Contains(code)).ToList());
|
code.Tokens = new ReadOnlyCollection<Token>(this.Tokens.Where(t => t.AppliedCodes.Contains(code)).ToList());
|
||||||
|
|
||||||
this.Realign(font, alignment);
|
this.RecalculateTokenData(font, alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -65,16 +64,9 @@ namespace MLEM.Formatting {
|
||||||
/// <param name="scale">The scale to use for width measurements</param>
|
/// <param name="scale">The scale to use for width measurements</param>
|
||||||
/// <param name="alignment">The text alignment that should be used for width calculations</param>
|
/// <param name="alignment">The text alignment that should be used for width calculations</param>
|
||||||
public void Split(GenericFont font, float width, float scale, TextAlignment alignment = TextAlignment.Left) {
|
public void Split(GenericFont font, float width, float scale, TextAlignment alignment = TextAlignment.Left) {
|
||||||
var index = 0;
|
// a split string has the same character count as the input string but with newline characters added
|
||||||
var modified = new StringBuilder();
|
this.modifiedString = string.Join("\n", font.SplitStringSeparate(new CharSource(this.String), width, scale, i => this.GetFontForIndex(font, i)));
|
||||||
foreach (var part in GenericFont.SplitStringSeparate(this.AsDecoratedSources(font), width, scale)) {
|
this.StoreModifiedSubstrings(font, alignment);
|
||||||
var joined = string.Join("\n", part);
|
|
||||||
this.Tokens[index].ModifiedSubstring = joined;
|
|
||||||
modified.Append(joined);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
this.modifiedString = modified.ToString();
|
|
||||||
this.Realign(font, alignment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -88,77 +80,13 @@ namespace MLEM.Formatting {
|
||||||
/// <param name="ellipsis">The characters to add to the end of the string if it is too long</param>
|
/// <param name="ellipsis">The characters to add to the end of the string if it is too long</param>
|
||||||
/// <param name="alignment">The text alignment that should be used for width calculations</param>
|
/// <param name="alignment">The text alignment that should be used for width calculations</param>
|
||||||
public void Truncate(GenericFont font, float width, float scale, string ellipsis = "", TextAlignment alignment = TextAlignment.Left) {
|
public void Truncate(GenericFont font, float width, float scale, string ellipsis = "", TextAlignment alignment = TextAlignment.Left) {
|
||||||
var index = 0;
|
this.modifiedString = font.TruncateString(new CharSource(this.String), width, scale, false, ellipsis, i => this.GetFontForIndex(font, i)).ToString();
|
||||||
var modified = new StringBuilder();
|
this.StoreModifiedSubstrings(font, alignment);
|
||||||
foreach (var part in GenericFont.TruncateString(this.AsDecoratedSources(font), width, scale, false, ellipsis)) {
|
|
||||||
this.Tokens[index].ModifiedSubstring = part.ToString();
|
|
||||||
modified.Append(part);
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
this.modifiedString = modified.ToString();
|
|
||||||
this.Realign(font, alignment);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Realigns this tokenized string using the given <see cref="TextAlignment"/>.
|
|
||||||
/// If the <paramref name="alignment"/> is <see cref="TextAlignment.Right"/>, trailing space characters (but not <see cref="GenericFont.Nbsp"/>) will be removed.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="font">The font to use for width calculations.</param>
|
|
||||||
/// <param name="alignment">The text alignment that should be used for width calculations.</param>
|
|
||||||
public void Realign(GenericFont font, TextAlignment alignment) {
|
|
||||||
// split display strings
|
|
||||||
foreach (var token in this.Tokens)
|
|
||||||
token.SplitDisplayString = token.DisplayString.Split('\n');
|
|
||||||
|
|
||||||
// token areas and inner offsets
|
|
||||||
this.area = RectangleF.Empty;
|
|
||||||
this.initialInnerOffset = this.GetInnerOffsetX(font, 0, 0, alignment);
|
|
||||||
var innerOffset = new Vector2(this.initialInnerOffset, 0);
|
|
||||||
for (var t = 0; t < this.Tokens.Length; t++) {
|
|
||||||
var token = this.Tokens[t];
|
|
||||||
var tokenFont = token.GetFont(font);
|
|
||||||
token.InnerOffsets = new float[token.SplitDisplayString.Length - 1];
|
|
||||||
|
|
||||||
var tokenArea = new List<RectangleF>();
|
|
||||||
var selfRect = new RectangleF(innerOffset, new Vector2(token.GetSelfWidth(tokenFont), tokenFont.LineHeight));
|
|
||||||
if (!selfRect.IsEmpty) {
|
|
||||||
tokenArea.Add(selfRect);
|
|
||||||
this.area = RectangleF.Union(this.area, selfRect);
|
|
||||||
innerOffset.X += selfRect.Width;
|
|
||||||
}
|
|
||||||
for (var l = 0; l < token.SplitDisplayString.Length; l++) {
|
|
||||||
var size = tokenFont.MeasureString(token.SplitDisplayString[l], !this.EndsLater(t, l));
|
|
||||||
var rect = new RectangleF(innerOffset, size);
|
|
||||||
if (!rect.IsEmpty) {
|
|
||||||
tokenArea.Add(rect);
|
|
||||||
this.area = RectangleF.Union(this.area, rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (l < token.SplitDisplayString.Length - 1) {
|
|
||||||
innerOffset.X = token.InnerOffsets[l] = this.GetInnerOffsetX(font, t, l + 1, alignment);
|
|
||||||
innerOffset.Y += tokenFont.LineHeight;
|
|
||||||
} else {
|
|
||||||
innerOffset.X += size.X;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
token.Area = tokenArea.ToArray();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="GenericFont.MeasureString(string,bool)"/>
|
/// <inheritdoc cref="GenericFont.MeasureString(string,bool)"/>
|
||||||
[Obsolete("Measure is deprecated. Use GetArea, which returns the string's total size measurement, instead.")]
|
|
||||||
public Vector2 Measure(GenericFont font) {
|
public Vector2 Measure(GenericFont font) {
|
||||||
return this.GetArea(Vector2.Zero, 1).Size;
|
return font.MeasureString(new CharSource(this.DisplayString), false, i => this.GetFontForIndex(font, i));
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Measures the area that this entire tokenized string and all of its <see cref="Tokens"/> take up and returns it as a <see cref="RectangleF"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stringPos">The position that this string is being rendered at, which will offset the resulting <see cref="RectangleF"/>.</param>
|
|
||||||
/// <param name="scale">The scale that this string is being rendered with, which will scale the resulting <see cref="RectangleF"/>.</param>
|
|
||||||
/// <returns>The area that this tokenized string takes up.</returns>
|
|
||||||
public RectangleF GetArea(Vector2 stringPos, float scale) {
|
|
||||||
return new RectangleF(stringPos + this.area.Location * scale, this.area.Size * scale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -196,49 +124,115 @@ namespace MLEM.Formatting {
|
||||||
var drawFont = token.GetFont(font);
|
var drawFont = token.GetFont(font);
|
||||||
var drawColor = token.GetColor(color);
|
var drawColor = token.GetColor(color);
|
||||||
|
|
||||||
token.DrawSelf(time, batch, pos + innerOffset, drawFont, drawColor, scale, depth);
|
|
||||||
innerOffset.X += token.GetSelfWidth(drawFont) * scale;
|
|
||||||
|
|
||||||
var indexInToken = 0;
|
var indexInToken = 0;
|
||||||
for (var l = 0; l < token.SplitDisplayString.Length; l++) {
|
for (var l = 0; l < token.SplitDisplayString.Length; l++) {
|
||||||
var charIndex = 0;
|
foreach (var c in token.SplitDisplayString[l]) {
|
||||||
var line = new CodePointSource(token.SplitDisplayString[l]);
|
var cString = c.ToCachedString();
|
||||||
while (charIndex < line.Length) {
|
|
||||||
var (codePoint, length) = line.GetCodePoint(charIndex);
|
|
||||||
var character = CodePointSource.ToString(codePoint);
|
|
||||||
|
|
||||||
token.DrawCharacter(time, batch, codePoint, character, indexInToken, pos + innerOffset, drawFont, drawColor, scale, depth);
|
if (indexInToken == 0)
|
||||||
|
token.DrawSelf(time, batch, pos + innerOffset, drawFont, color, scale, depth);
|
||||||
|
token.DrawCharacter(time, batch, c, cString, indexInToken, pos + innerOffset, drawFont, drawColor, scale, depth);
|
||||||
|
|
||||||
innerOffset.X += drawFont.MeasureString(character).X * scale;
|
innerOffset.X += drawFont.MeasureString(cString).X * scale;
|
||||||
charIndex += length;
|
|
||||||
indexInToken++;
|
indexInToken++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only split at a new line, not between tokens!
|
// only split at a new line, not between tokens!
|
||||||
if (l < token.SplitDisplayString.Length - 1) {
|
if (l < token.SplitDisplayString.Length - 1) {
|
||||||
innerOffset.X = token.InnerOffsets[l] * scale;
|
innerOffset.X = token.InnerOffsets[l] * scale;
|
||||||
innerOffset.Y += drawFont.LineHeight * scale;
|
innerOffset.Y += font.LineHeight * scale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void StoreModifiedSubstrings(GenericFont font, TextAlignment alignment) {
|
||||||
|
if (this.Tokens.Length == 1) {
|
||||||
|
// skip substring logic for unformatted text
|
||||||
|
this.Tokens[0].ModifiedSubstring = this.modifiedString;
|
||||||
|
} else {
|
||||||
|
// this is basically a substring function that ignores added newlines for indexing
|
||||||
|
var index = 0;
|
||||||
|
var currToken = 0;
|
||||||
|
var splitIndex = 0;
|
||||||
|
var ret = new StringBuilder();
|
||||||
|
while (splitIndex < this.modifiedString.Length && currToken < this.Tokens.Length) {
|
||||||
|
var token = this.Tokens[currToken];
|
||||||
|
if (token.Substring.Length > 0) {
|
||||||
|
ret.Append(this.modifiedString[splitIndex]);
|
||||||
|
// if the current char is not an added newline, we simulate length increase
|
||||||
|
if (this.modifiedString[splitIndex] != '\n' || this.String[index] == '\n')
|
||||||
|
index++;
|
||||||
|
splitIndex++;
|
||||||
|
}
|
||||||
|
// move on to the next token if we reached its end
|
||||||
|
if (index >= token.Index + token.Substring.Length) {
|
||||||
|
token.ModifiedSubstring = ret.ToString();
|
||||||
|
ret.Clear();
|
||||||
|
currToken++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// set additional token contents beyond our string in case we truncated
|
||||||
|
if (ret.Length > 0)
|
||||||
|
this.Tokens[currToken++].ModifiedSubstring = ret.ToString();
|
||||||
|
while (currToken < this.Tokens.Length)
|
||||||
|
this.Tokens[currToken++].ModifiedSubstring = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.RecalculateTokenData(font, alignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RecalculateTokenData(GenericFont font, TextAlignment alignment) {
|
||||||
|
// split display strings
|
||||||
|
foreach (var token in this.Tokens)
|
||||||
|
token.SplitDisplayString = token.DisplayString.Split('\n');
|
||||||
|
|
||||||
|
// token areas and inner offsets
|
||||||
|
this.initialInnerOffset = this.GetInnerOffsetX(font, 0, 0, alignment);
|
||||||
|
var innerOffset = new Vector2(this.initialInnerOffset, 0);
|
||||||
|
for (var t = 0; t < this.Tokens.Length; t++) {
|
||||||
|
var token = this.Tokens[t];
|
||||||
|
var tokenFont = token.GetFont(font);
|
||||||
|
token.InnerOffsets = new float[token.SplitDisplayString.Length - 1];
|
||||||
|
var area = new List<RectangleF>();
|
||||||
|
for (var l = 0; l < token.SplitDisplayString.Length; l++) {
|
||||||
|
var size = tokenFont.MeasureString(token.SplitDisplayString[l]);
|
||||||
|
var rect = new RectangleF(innerOffset, size);
|
||||||
|
if (!rect.IsEmpty)
|
||||||
|
area.Add(rect);
|
||||||
|
|
||||||
|
if (l < token.SplitDisplayString.Length - 1) {
|
||||||
|
innerOffset.X = token.InnerOffsets[l] = this.GetInnerOffsetX(font, t, l + 1, alignment);
|
||||||
|
innerOffset.Y += font.LineHeight;
|
||||||
|
} else {
|
||||||
|
innerOffset.X += size.X;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token.Area = area.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private float GetInnerOffsetX(GenericFont defaultFont, int tokenIndex, int lineIndex, TextAlignment alignment) {
|
private float GetInnerOffsetX(GenericFont defaultFont, int tokenIndex, int lineIndex, TextAlignment alignment) {
|
||||||
if (alignment > TextAlignment.Left) {
|
if (alignment > TextAlignment.Left) {
|
||||||
var token = this.Tokens[tokenIndex];
|
var token = this.Tokens[tokenIndex];
|
||||||
var tokenFont = token.GetFont(defaultFont);
|
var tokenFont = token.GetFont(defaultFont);
|
||||||
var tokenWidth = lineIndex <= 0 ? token.GetSelfWidth(tokenFont) : 0;
|
// if we're the last line in our line array, then we don't contain a line split, so the line ends in a later token
|
||||||
var endsLater = this.EndsLater(tokenIndex, lineIndex);
|
var endsLater = lineIndex >= token.SplitDisplayString.Length - 1;
|
||||||
// if the line ends in our token, we should ignore trailing white space
|
// if the line ends in our token, we should ignore trailing white space
|
||||||
var restOfLine = tokenFont.MeasureString(token.SplitDisplayString[lineIndex], !endsLater).X + tokenWidth;
|
var restOfLine = tokenFont.MeasureString(token.SplitDisplayString[lineIndex], !endsLater).X;
|
||||||
if (endsLater) {
|
if (endsLater) {
|
||||||
for (var i = tokenIndex + 1; i < this.Tokens.Length; i++) {
|
for (var i = tokenIndex + 1; i < this.Tokens.Length; i++) {
|
||||||
var other = this.Tokens[i];
|
var other = this.Tokens[i];
|
||||||
var otherFont = other.GetFont(defaultFont);
|
var otherFont = other.GetFont(defaultFont);
|
||||||
restOfLine += otherFont.MeasureString(other.SplitDisplayString[0], !this.EndsLater(i, 0)).X + other.GetSelfWidth(otherFont);
|
if (other.SplitDisplayString.Length > 1) {
|
||||||
// if the token's split display string has multiple lines, then the line ends in it, which means we can stop
|
// the line ends in this token (so we also ignore trailing whitespaces)
|
||||||
if (other.SplitDisplayString.Length > 1)
|
restOfLine += otherFont.MeasureString(other.SplitDisplayString[0], true).X;
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
|
// the line doesn't end in this token (or it's the last token), so add it fully
|
||||||
|
var lastToken = i >= this.Tokens.Length - 1;
|
||||||
|
restOfLine += otherFont.MeasureString(other.DisplayString, lastToken).X;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (alignment == TextAlignment.Center)
|
if (alignment == TextAlignment.Center)
|
||||||
|
@ -248,16 +242,13 @@ namespace MLEM.Formatting {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool EndsLater(int tokenIndex, int lineIndex) {
|
private GenericFont GetFontForIndex(GenericFont font, int index) {
|
||||||
// if we're the last line in our line array, then we don't contain a line split, so the line ends in a later token
|
foreach (var token in this.Tokens) {
|
||||||
return lineIndex >= this.Tokens[tokenIndex].SplitDisplayString.Length - 1 && tokenIndex < this.Tokens.Length - 1;
|
index -= token.Substring.Length;
|
||||||
|
if (index <= 0)
|
||||||
|
return token.GetFont(font);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
private IEnumerable<GenericFont.DecoratedCodePointSource> AsDecoratedSources(GenericFont font) {
|
|
||||||
return this.Tokens.Select(t => {
|
|
||||||
var tokenFont = t.GetFont(font);
|
|
||||||
return new GenericFont.DecoratedCodePointSource(new CodePointSource(t.Substring), tokenFont, t.GetSelfWidth(tokenFont));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
@ -6,8 +5,8 @@ using MLEM.Textures;
|
||||||
|
|
||||||
namespace MLEM.Graphics {
|
namespace MLEM.Graphics {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class contains a <see cref="DrawAutoTile"/> method that allows users to easily draw a tile with automatic connections, as well as a more complex <see cref="DrawExtendedAutoTile(Microsoft.Xna.Framework.Graphics.SpriteBatch,Microsoft.Xna.Framework.Vector2,MLEM.Textures.TextureRegion,MLEM.Textures.TextureRegion,MLEM.Graphics.AutoTiling.ConnectsTo,Microsoft.Xna.Framework.Color,Microsoft.Xna.Framework.Color,System.Nullable{Microsoft.Xna.Framework.Vector2},System.Nullable{Microsoft.Xna.Framework.Vector2},float,float)"/> method.
|
/// This class contains a <see cref="DrawAutoTile"/> method that allows users to easily draw a tile with automatic connections, as well as a more complex <see cref="DrawExtendedAutoTile"/> method.
|
||||||
/// Note that <see cref="StaticSpriteBatch"/> can also be used for drawing by using the <see cref="AddAutoTile"/> and <see cref="AddExtendedAutoTile(MLEM.Graphics.StaticSpriteBatch,Microsoft.Xna.Framework.Vector2,MLEM.Textures.TextureRegion,MLEM.Textures.TextureRegion,MLEM.Graphics.AutoTiling.ConnectsTo,Microsoft.Xna.Framework.Color,Microsoft.Xna.Framework.Color,System.Nullable{Microsoft.Xna.Framework.Vector2},System.Nullable{Microsoft.Xna.Framework.Vector2},float,float,System.Collections.Generic.ICollection{MLEM.Graphics.StaticSpriteBatch.Item})"/> methods instead.
|
/// Note that <see cref="StaticSpriteBatch"/> can also be used for drawing by using the <see cref="AddAutoTile"/> and <see cref="AddExtendedAutoTile"/> methods instead.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class AutoTiling {
|
public static class AutoTiling {
|
||||||
|
|
||||||
|
@ -89,7 +88,7 @@ namespace MLEM.Graphics {
|
||||||
var orig = origin ?? Vector2.Zero;
|
var orig = origin ?? Vector2.Zero;
|
||||||
var sc = scale ?? Vector2.One;
|
var sc = scale ?? Vector2.One;
|
||||||
var od = layerDepth + overlayDepthOffset;
|
var od = layerDepth + overlayDepthOffset;
|
||||||
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(overlayTexture.Area, connectsTo);
|
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(pos, overlayTexture.Area, connectsTo, sc);
|
||||||
if (backgroundTexture != null)
|
if (backgroundTexture != null)
|
||||||
batch.Draw(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
batch.Draw(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
||||||
if (r1 != Rectangle.Empty)
|
if (r1 != Rectangle.Empty)
|
||||||
|
@ -102,30 +101,12 @@ namespace MLEM.Graphics {
|
||||||
batch.Draw(overlayTexture.Texture, pos, r4, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
batch.Draw(overlayTexture.Texture, pos, r4, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="DrawExtendedAutoTile(Microsoft.Xna.Framework.Graphics.SpriteBatch,Microsoft.Xna.Framework.Vector2,MLEM.Textures.TextureRegion,MLEM.Textures.TextureRegion,MLEM.Graphics.AutoTiling.ConnectsTo,Microsoft.Xna.Framework.Color,Microsoft.Xna.Framework.Color,System.Nullable{Microsoft.Xna.Framework.Vector2},System.Nullable{Microsoft.Xna.Framework.Vector2},float,float)"/>
|
/// <inheritdoc cref="DrawExtendedAutoTile"/>
|
||||||
public static void DrawExtendedAutoTile(SpriteBatch batch, Vector2 pos, TextureRegion backgroundTexture, Func<int, TextureRegion> overlayTextures, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0) {
|
|
||||||
var orig = origin ?? Vector2.Zero;
|
|
||||||
var sc = scale ?? Vector2.One;
|
|
||||||
var od = layerDepth + overlayDepthOffset;
|
|
||||||
var (xUl, xUr, xDl, xDr) = AutoTiling.CalculateExtendedAutoTileOffsets(connectsTo);
|
|
||||||
if (backgroundTexture != null)
|
|
||||||
batch.Draw(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
|
||||||
if (xUl >= 0)
|
|
||||||
batch.Draw(overlayTextures(xUl), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
if (xUr >= 0)
|
|
||||||
batch.Draw(overlayTextures(xUr), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
if (xDl >= 0)
|
|
||||||
batch.Draw(overlayTextures(xDl), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
if (xDr >= 0)
|
|
||||||
batch.Draw(overlayTextures(xDr), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="DrawExtendedAutoTile(Microsoft.Xna.Framework.Graphics.SpriteBatch,Microsoft.Xna.Framework.Vector2,MLEM.Textures.TextureRegion,MLEM.Textures.TextureRegion,MLEM.Graphics.AutoTiling.ConnectsTo,Microsoft.Xna.Framework.Color,Microsoft.Xna.Framework.Color,System.Nullable{Microsoft.Xna.Framework.Vector2},System.Nullable{Microsoft.Xna.Framework.Vector2},float,float)"/>
|
|
||||||
public static void AddExtendedAutoTile(StaticSpriteBatch batch, Vector2 pos, TextureRegion backgroundTexture, TextureRegion overlayTexture, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0, ICollection<StaticSpriteBatch.Item> items = null) {
|
public static void AddExtendedAutoTile(StaticSpriteBatch batch, Vector2 pos, TextureRegion backgroundTexture, TextureRegion overlayTexture, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0, ICollection<StaticSpriteBatch.Item> items = null) {
|
||||||
var orig = origin ?? Vector2.Zero;
|
var orig = origin ?? Vector2.Zero;
|
||||||
var sc = scale ?? Vector2.One;
|
var sc = scale ?? Vector2.One;
|
||||||
var od = layerDepth + overlayDepthOffset;
|
var od = layerDepth + overlayDepthOffset;
|
||||||
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(overlayTexture.Area, connectsTo);
|
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(pos, overlayTexture.Area, connectsTo, sc);
|
||||||
if (backgroundTexture != null) {
|
if (backgroundTexture != null) {
|
||||||
var background = batch.Add(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
var background = batch.Add(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
||||||
items?.Add(background);
|
items?.Add(background);
|
||||||
|
@ -148,34 +129,6 @@ namespace MLEM.Graphics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="DrawExtendedAutoTile(Microsoft.Xna.Framework.Graphics.SpriteBatch,Microsoft.Xna.Framework.Vector2,MLEM.Textures.TextureRegion,Func{int,MLEM.Textures.TextureRegion},MLEM.Graphics.AutoTiling.ConnectsTo,Microsoft.Xna.Framework.Color,Microsoft.Xna.Framework.Color,System.Nullable{Microsoft.Xna.Framework.Vector2},System.Nullable{Microsoft.Xna.Framework.Vector2},float,float)"/>
|
|
||||||
public static void AddExtendedAutoTile(StaticSpriteBatch batch, Vector2 pos, TextureRegion backgroundTexture, Func<int, TextureRegion> overlayTextures, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0, ICollection<StaticSpriteBatch.Item> items = null) {
|
|
||||||
var orig = origin ?? Vector2.Zero;
|
|
||||||
var sc = scale ?? Vector2.One;
|
|
||||||
var od = layerDepth + overlayDepthOffset;
|
|
||||||
var (xUl, xUr, xDl, xDr) = AutoTiling.CalculateExtendedAutoTileOffsets(connectsTo);
|
|
||||||
if (backgroundTexture != null) {
|
|
||||||
var background = batch.Add(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
|
|
||||||
items?.Add(background);
|
|
||||||
}
|
|
||||||
if (xUl >= 0) {
|
|
||||||
var o1 = batch.Add(overlayTextures(xUl), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
items?.Add(o1);
|
|
||||||
}
|
|
||||||
if (xUr >= 0) {
|
|
||||||
var o2 = batch.Add(overlayTextures(xUr), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
items?.Add(o2);
|
|
||||||
}
|
|
||||||
if (xDl >= 0) {
|
|
||||||
var o3 = batch.Add(overlayTextures(xDl), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
items?.Add(o3);
|
|
||||||
}
|
|
||||||
if (xDr >= 0) {
|
|
||||||
var o4 = batch.Add(overlayTextures(xDr), pos, overlayColor, 0, orig, sc, SpriteEffects.None, od);
|
|
||||||
items?.Add(o4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static (Vector2, Rectangle, Vector2, Rectangle, Vector2, Rectangle, Vector2, Rectangle) CalculateAutoTile(Vector2 pos, Rectangle textureRegion, ConnectsTo connectsTo, Vector2 scale) {
|
private static (Vector2, Rectangle, Vector2, Rectangle, Vector2, Rectangle, Vector2, Rectangle) CalculateAutoTile(Vector2 pos, Rectangle textureRegion, ConnectsTo connectsTo, Vector2 scale) {
|
||||||
var up = connectsTo(0, -1);
|
var up = connectsTo(0, -1);
|
||||||
var down = connectsTo(0, 1);
|
var down = connectsTo(0, 1);
|
||||||
|
@ -194,20 +147,15 @@ namespace MLEM.Graphics {
|
||||||
new Vector2(pos.X + w2 * scale.X, pos.Y + h2 * scale.Y), new Rectangle(textureRegion.X + w2 + xDr * w, textureRegion.Y + h2, w2, h2));
|
new Vector2(pos.X + w2 * scale.X, pos.Y + h2 * scale.Y), new Rectangle(textureRegion.X + w2 + xDr * w, textureRegion.Y + h2, w2, h2));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (int, int, int, int) CalculateExtendedAutoTileOffsets(ConnectsTo connectsTo) {
|
private static (Rectangle, Rectangle, Rectangle, Rectangle) CalculateExtendedAutoTile(Vector2 pos, Rectangle textureRegion, ConnectsTo connectsTo, Vector2 scale) {
|
||||||
var up = connectsTo(0, -1);
|
var up = connectsTo(0, -1);
|
||||||
var down = connectsTo(0, 1);
|
var down = connectsTo(0, 1);
|
||||||
var left = connectsTo(-1, 0);
|
var left = connectsTo(-1, 0);
|
||||||
var right = connectsTo(1, 0);
|
var right = connectsTo(1, 0);
|
||||||
return (
|
var xUl = up && left ? connectsTo(-1, -1) ? -1 : 12 : left ? 0 : up ? 8 : 4;
|
||||||
up && left ? connectsTo(-1, -1) ? -1 : 12 : left ? 0 : up ? 8 : 4,
|
var xUr = up && right ? connectsTo(1, -1) ? -1 : 13 : right ? 1 : up ? 9 : 5;
|
||||||
up && right ? connectsTo(1, -1) ? -1 : 13 : right ? 1 : up ? 9 : 5,
|
var xDl = down && left ? connectsTo(-1, 1) ? -1 : 14 : left ? 2 : down ? 10 : 6;
|
||||||
down && left ? connectsTo(-1, 1) ? -1 : 14 : left ? 2 : down ? 10 : 6,
|
var xDr = down && right ? connectsTo(1, 1) ? -1 : 15 : right ? 3 : down ? 11 : 7;
|
||||||
down && right ? connectsTo(1, 1) ? -1 : 15 : right ? 3 : down ? 11 : 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static (Rectangle, Rectangle, Rectangle, Rectangle) CalculateExtendedAutoTile(Rectangle textureRegion, ConnectsTo connectsTo) {
|
|
||||||
var (xUl, xUr, xDl, xDr) = AutoTiling.CalculateExtendedAutoTileOffsets(connectsTo);
|
|
||||||
var (w, h) = (textureRegion.Width, textureRegion.Height);
|
var (w, h) = (textureRegion.Width, textureRegion.Height);
|
||||||
return (
|
return (
|
||||||
xUl < 0 ? Rectangle.Empty : new Rectangle(textureRegion.X + xUl * w, textureRegion.Y, w, h),
|
xUl < 0 ? Rectangle.Empty : new Rectangle(textureRegion.X + xUl * w, textureRegion.Y, w, h),
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
#if FNA
|
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using System.IO;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Graphics {
|
namespace MLEM.Graphics {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A static sprite batch is a highly optimized variation of <see cref="SpriteBatch"/> that keeps all batched items in a <see cref="VertexBuffer"/>, allowing for them to be drawn multiple times.
|
/// A static sprite batch is a variation of <see cref="SpriteBatch"/> that keeps all batched items in a <see cref="VertexBuffer"/>, allowing for them to be drawn multiple times.
|
||||||
/// To add items to a static sprite batch, use <see cref="BeginBatch"/> to begin batching, <see cref="ClearBatch"/> to clear currently batched items, <c>Add</c> and its various overloads to add batch items, <see cref="Remove"/> to remove them again, and <see cref="EndBatch"/> to end batching.
|
/// To add items to a static sprite batch, use <see cref="BeginBatch"/> to begin batching, <see cref="ClearBatch"/> to clear currently batched items, <c>Add</c> and its various overloads to add batch items, <see cref="Remove"/> to remove them again, and <see cref="EndBatch"/> to end batching.
|
||||||
/// To draw the batched items, call <see cref="Draw"/>.
|
/// To draw the batched items, call <see cref="Draw"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -23,7 +21,7 @@ namespace MLEM.Graphics {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The amount of vertices that are currently batched.
|
/// The amount of vertices that are currently batched.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Vertices => this.itemAmount * 4;
|
public int Vertices => this.items.Count * 4;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The amount of vertex buffers that this static sprite batch has.
|
/// The amount of vertex buffers that this static sprite batch has.
|
||||||
/// To see the amount of buffers that are actually in use, see <see cref="FilledBuffers"/>.
|
/// To see the amount of buffers that are actually in use, see <see cref="FilledBuffers"/>.
|
||||||
|
@ -41,15 +39,13 @@ namespace MLEM.Graphics {
|
||||||
|
|
||||||
private readonly GraphicsDevice graphicsDevice;
|
private readonly GraphicsDevice graphicsDevice;
|
||||||
private readonly SpriteEffect spriteEffect;
|
private readonly SpriteEffect spriteEffect;
|
||||||
private readonly List<DynamicVertexBuffer> vertexBuffers = new List<DynamicVertexBuffer>();
|
|
||||||
private readonly List<Texture2D> textures = new List<Texture2D>();
|
|
||||||
private readonly SortedDictionary<float, ItemSet> items = new SortedDictionary<float, ItemSet>();
|
|
||||||
|
|
||||||
private SpriteSortMode sortMode = SpriteSortMode.Texture;
|
private readonly List<VertexBuffer> vertexBuffers = new List<VertexBuffer>();
|
||||||
|
private readonly List<Texture2D> textures = new List<Texture2D>();
|
||||||
|
private readonly ISet<Item> items = new HashSet<Item>();
|
||||||
private IndexBuffer indices;
|
private IndexBuffer indices;
|
||||||
private bool batching;
|
private bool batching;
|
||||||
private bool batchChanged;
|
private bool batchChanged;
|
||||||
private int itemAmount;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new static sprite batch with the given <see cref="GraphicsDevice"/>
|
/// Creates a new static sprite batch with the given <see cref="GraphicsDevice"/>
|
||||||
|
@ -64,27 +60,10 @@ namespace MLEM.Graphics {
|
||||||
/// Begins batching.
|
/// Begins batching.
|
||||||
/// Call this method before calling <c>Add</c> or any of its overloads.
|
/// Call this method before calling <c>Add</c> or any of its overloads.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sortMode">The drawing order for sprite drawing. When <see langword="null"/> is passed, the last used sort mode will be used again. The initial sort mode is <see cref="SpriteSortMode.Texture"/>. Note that <see cref="SpriteSortMode.Immediate"/> is not supported.</param>
|
|
||||||
/// <exception cref="InvalidOperationException">Thrown if this batch is currently batching already</exception>
|
/// <exception cref="InvalidOperationException">Thrown if this batch is currently batching already</exception>
|
||||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="sortMode"/> is <see cref="SpriteSortMode.Immediate"/>, which is not supported.</exception>
|
public void BeginBatch() {
|
||||||
public void BeginBatch(SpriteSortMode? sortMode = null) {
|
|
||||||
if (this.batching)
|
if (this.batching)
|
||||||
throw new InvalidOperationException("Already batching");
|
throw new InvalidOperationException("Already batching");
|
||||||
if (sortMode == SpriteSortMode.Immediate)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(sortMode), "Cannot use sprite sort mode Immediate for static batching");
|
|
||||||
|
|
||||||
// if the sort mode changed (which should be very rare in practice), we have to re-sort our list
|
|
||||||
if (sortMode != null && this.sortMode != sortMode) {
|
|
||||||
this.sortMode = sortMode.Value;
|
|
||||||
if (this.items.Count > 0) {
|
|
||||||
var tempItems = this.items.Values.SelectMany(s => s.Items).ToArray();
|
|
||||||
this.items.Clear();
|
|
||||||
foreach (var item in tempItems)
|
|
||||||
this.AddItemToSet(item);
|
|
||||||
this.batchChanged = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.batching = true;
|
this.batching = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,10 +71,14 @@ namespace MLEM.Graphics {
|
||||||
/// Ends batching.
|
/// Ends batching.
|
||||||
/// Call this method after calling <c>Add</c> or any of its overloads the desired number of times to add batched items.
|
/// Call this method after calling <c>Add</c> or any of its overloads the desired number of times to add batched items.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="sortMode">The drawing order for sprite drawing. <see cref="SpriteSortMode.Texture" /> by default, since it is the best in terms of rendering performance. Note that <see cref="SpriteSortMode.Immediate"/> is not supported.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown if this method is called before <see cref="BeginBatch"/> was called.</exception>
|
/// <exception cref="InvalidOperationException">Thrown if this method is called before <see cref="BeginBatch"/> was called.</exception>
|
||||||
public void EndBatch() {
|
/// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="sortMode"/> is <see cref="SpriteSortMode.Immediate"/>, which is not supported.</exception>
|
||||||
|
public void EndBatch(SpriteSortMode sortMode = SpriteSortMode.Texture) {
|
||||||
if (!this.batching)
|
if (!this.batching)
|
||||||
throw new InvalidOperationException("Not batching");
|
throw new InvalidOperationException("Not batching");
|
||||||
|
if (sortMode == SpriteSortMode.Immediate)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(sortMode), "Cannot use sprite sort mode Immediate for static batching");
|
||||||
this.batching = false;
|
this.batching = false;
|
||||||
|
|
||||||
// if we didn't add or remove any batch items, we don't have to recalculate anything
|
// if we didn't add or remove any batch items, we don't have to recalculate anything
|
||||||
|
@ -105,11 +88,25 @@ namespace MLEM.Graphics {
|
||||||
this.FilledBuffers = 0;
|
this.FilledBuffers = 0;
|
||||||
this.textures.Clear();
|
this.textures.Clear();
|
||||||
|
|
||||||
|
// order items according to the sort mode
|
||||||
|
IEnumerable<Item> ordered = this.items;
|
||||||
|
switch (sortMode) {
|
||||||
|
case SpriteSortMode.Texture:
|
||||||
|
// SortingKey is internal, but this will do for batching the same texture together
|
||||||
|
ordered = ordered.OrderBy(i => i.Texture.GetHashCode());
|
||||||
|
break;
|
||||||
|
case SpriteSortMode.BackToFront:
|
||||||
|
ordered = ordered.OrderBy(i => -i.Depth);
|
||||||
|
break;
|
||||||
|
case SpriteSortMode.FrontToBack:
|
||||||
|
ordered = ordered.OrderBy(i => i.Depth);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// fill vertex buffers
|
// fill vertex buffers
|
||||||
var dataIndex = 0;
|
var dataIndex = 0;
|
||||||
Texture2D texture = null;
|
Texture2D texture = null;
|
||||||
foreach (var itemSet in this.items.Values) {
|
foreach (var item in ordered) {
|
||||||
foreach (var item in itemSet.Items) {
|
|
||||||
// if the texture changes, we also have to start a new buffer!
|
// if the texture changes, we also have to start a new buffer!
|
||||||
if (dataIndex > 0 && (item.Texture != texture || dataIndex >= StaticSpriteBatch.Data.Length)) {
|
if (dataIndex > 0 && (item.Texture != texture || dataIndex >= StaticSpriteBatch.Data.Length)) {
|
||||||
this.FillBuffer(this.FilledBuffers++, texture, StaticSpriteBatch.Data);
|
this.FillBuffer(this.FilledBuffers++, texture, StaticSpriteBatch.Data);
|
||||||
|
@ -121,12 +118,11 @@ namespace MLEM.Graphics {
|
||||||
StaticSpriteBatch.Data[dataIndex++] = item.BottomRight;
|
StaticSpriteBatch.Data[dataIndex++] = item.BottomRight;
|
||||||
texture = item.Texture;
|
texture = item.Texture;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (dataIndex > 0)
|
if (dataIndex > 0)
|
||||||
this.FillBuffer(this.FilledBuffers++, texture, StaticSpriteBatch.Data);
|
this.FillBuffer(this.FilledBuffers++, texture, StaticSpriteBatch.Data);
|
||||||
|
|
||||||
// ensure we have enough indices
|
// ensure we have enough indices
|
||||||
var maxItems = Math.Min(this.itemAmount, StaticSpriteBatch.MaxBatchItems);
|
var maxItems = Math.Min(this.items.Count, StaticSpriteBatch.MaxBatchItems);
|
||||||
// each item has 2 triangles which each have 3 indices
|
// each item has 2 triangles which each have 3 indices
|
||||||
if (this.indices == null || this.indices.IndexCount < 6 * maxItems) {
|
if (this.indices == null || this.indices.IndexCount < 6 * maxItems) {
|
||||||
var newIndices = new short[6 * maxItems];
|
var newIndices = new short[6 * maxItems];
|
||||||
|
@ -180,7 +176,7 @@ namespace MLEM.Graphics {
|
||||||
for (var i = 0; i < this.FilledBuffers; i++) {
|
for (var i = 0; i < this.FilledBuffers; i++) {
|
||||||
var buffer = this.vertexBuffers[i];
|
var buffer = this.vertexBuffers[i];
|
||||||
var texture = this.textures[i];
|
var texture = this.textures[i];
|
||||||
var verts = Math.Min(this.itemAmount * 4 - totalIndex, buffer.VertexCount);
|
var verts = Math.Min(this.items.Count * 4 - totalIndex, buffer.VertexCount);
|
||||||
|
|
||||||
this.graphicsDevice.SetVertexBuffer(buffer);
|
this.graphicsDevice.SetVertexBuffer(buffer);
|
||||||
if (effect != null) {
|
if (effect != null) {
|
||||||
|
@ -354,21 +350,6 @@ namespace MLEM.Graphics {
|
||||||
return this.Add(texture, destinationRectangle, null, color);
|
return this.Add(texture, destinationRectangle, null, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds an item to this batch.
|
|
||||||
/// Note that this batch needs to currently be batching, meaning <see cref="BeginBatch"/> has to have been called previously.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item to add.</param>
|
|
||||||
/// <returns>The added <paramref name="item"/>, for chaining.</returns>
|
|
||||||
public Item Add(Item item) {
|
|
||||||
if (!this.batching)
|
|
||||||
throw new InvalidOperationException("Not batching");
|
|
||||||
this.AddItemToSet(item);
|
|
||||||
this.itemAmount++;
|
|
||||||
this.batchChanged = true;
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the given item from this batch.
|
/// Removes the given item from this batch.
|
||||||
/// Note that this batch needs to currently be batching, meaning <see cref="BeginBatch"/> has to have been called previously.
|
/// Note that this batch needs to currently be batching, meaning <see cref="BeginBatch"/> has to have been called previously.
|
||||||
|
@ -379,11 +360,7 @@ namespace MLEM.Graphics {
|
||||||
public bool Remove(Item item) {
|
public bool Remove(Item item) {
|
||||||
if (!this.batching)
|
if (!this.batching)
|
||||||
throw new InvalidOperationException("Not batching");
|
throw new InvalidOperationException("Not batching");
|
||||||
var key = item.GetSortKey(this.sortMode);
|
if (this.items.Remove(item)) {
|
||||||
if (this.items.TryGetValue(key, out var itemSet) && itemSet.Remove(item)) {
|
|
||||||
if (itemSet.IsEmpty)
|
|
||||||
this.items.Remove(key);
|
|
||||||
this.itemAmount--;
|
|
||||||
this.batchChanged = true;
|
this.batchChanged = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -401,7 +378,6 @@ namespace MLEM.Graphics {
|
||||||
this.items.Clear();
|
this.items.Clear();
|
||||||
this.textures.Clear();
|
this.textures.Clear();
|
||||||
this.FilledBuffers = 0;
|
this.FilledBuffers = 0;
|
||||||
this.itemAmount = 0;
|
|
||||||
this.batchChanged = true;
|
this.batchChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,6 +387,7 @@ namespace MLEM.Graphics {
|
||||||
this.indices?.Dispose();
|
this.indices?.Dispose();
|
||||||
foreach (var buffer in this.vertexBuffers)
|
foreach (var buffer in this.vertexBuffers)
|
||||||
buffer.Dispose();
|
buffer.Dispose();
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Item Add(Texture2D texture, Vector2 pos, Vector2 offset, Vector2 size, float sin, float cos, Color color, Vector2 texTl, Vector2 texBr, float depth) {
|
private Item Add(Texture2D texture, Vector2 pos, Vector2 offset, Vector2 size, float sin, float cos, Color color, Vector2 texTl, Vector2 texBr, float depth) {
|
||||||
|
@ -450,13 +427,18 @@ namespace MLEM.Graphics {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Item Add(Texture2D texture, float depth, VertexPositionColorTexture tl, VertexPositionColorTexture tr, VertexPositionColorTexture bl, VertexPositionColorTexture br) {
|
private Item Add(Texture2D texture, float depth, VertexPositionColorTexture tl, VertexPositionColorTexture tr, VertexPositionColorTexture bl, VertexPositionColorTexture br) {
|
||||||
return this.Add(new Item(texture, depth, tl, tr, bl, br));
|
if (!this.batching)
|
||||||
|
throw new InvalidOperationException("Not batching");
|
||||||
|
var item = new Item(texture, depth, tl, tr, bl, br);
|
||||||
|
this.items.Add(item);
|
||||||
|
this.batchChanged = true;
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FillBuffer(int index, Texture2D texture, VertexPositionColorTexture[] data) {
|
private void FillBuffer(int index, Texture2D texture, VertexPositionColorTexture[] data) {
|
||||||
if (this.vertexBuffers.Count <= index)
|
if (this.vertexBuffers.Count <= index)
|
||||||
this.vertexBuffers.Add(new DynamicVertexBuffer(this.graphicsDevice, VertexPositionColorTexture.VertexDeclaration, StaticSpriteBatch.MaxBatchItems * 4, BufferUsage.WriteOnly));
|
this.vertexBuffers.Add(new VertexBuffer(this.graphicsDevice, VertexPositionColorTexture.VertexDeclaration, StaticSpriteBatch.MaxBatchItems * 4, BufferUsage.WriteOnly));
|
||||||
this.vertexBuffers[index].SetData(data, 0, data.Length, SetDataOptions.Discard);
|
this.vertexBuffers[index].SetData(data);
|
||||||
this.textures.Insert(index, texture);
|
this.textures.Insert(index, texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,15 +450,6 @@ namespace MLEM.Graphics {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddItemToSet(Item item) {
|
|
||||||
var sortKey = item.GetSortKey(this.sortMode);
|
|
||||||
if (!this.items.TryGetValue(sortKey, out var itemSet)) {
|
|
||||||
itemSet = new ItemSet();
|
|
||||||
this.items.Add(sortKey, itemSet);
|
|
||||||
}
|
|
||||||
itemSet.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A struct that represents an item added to a <see cref="StaticSpriteBatch"/> using <c>Add</c> or any of its overloads.
|
/// A struct that represents an item added to a <see cref="StaticSpriteBatch"/> using <c>Add</c> or any of its overloads.
|
||||||
/// An item returned after adding can be removed using <see cref="Remove"/>.
|
/// An item returned after adding can be removed using <see cref="Remove"/>.
|
||||||
|
@ -499,65 +472,6 @@ namespace MLEM.Graphics {
|
||||||
this.BottomRight = bottomRight;
|
this.BottomRight = bottomRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal float GetSortKey(SpriteSortMode sortMode) {
|
|
||||||
switch (sortMode) {
|
|
||||||
case SpriteSortMode.Texture:
|
|
||||||
return this.Texture.GetHashCode();
|
|
||||||
case SpriteSortMode.BackToFront:
|
|
||||||
return -this.Depth;
|
|
||||||
case SpriteSortMode.FrontToBack:
|
|
||||||
return this.Depth;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ItemSet {
|
|
||||||
|
|
||||||
public IEnumerable<Item> Items {
|
|
||||||
get {
|
|
||||||
if (this.items != null)
|
|
||||||
return this.items;
|
|
||||||
if (this.single != null)
|
|
||||||
return Enumerable.Repeat(this.single, 1);
|
|
||||||
return Enumerable.Empty<Item>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public bool IsEmpty => this.items == null && this.single == null;
|
|
||||||
|
|
||||||
private HashSet<Item> items;
|
|
||||||
private Item single;
|
|
||||||
|
|
||||||
public void Add(Item item) {
|
|
||||||
if (this.items != null) {
|
|
||||||
this.items.Add(item);
|
|
||||||
} else if (this.single != null) {
|
|
||||||
this.items = new HashSet<Item>();
|
|
||||||
this.items.Add(this.single);
|
|
||||||
this.items.Add(item);
|
|
||||||
this.single = null;
|
|
||||||
} else {
|
|
||||||
this.single = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Remove(Item item) {
|
|
||||||
if (this.items != null && this.items.Remove(item)) {
|
|
||||||
if (this.items.Count <= 1) {
|
|
||||||
this.single = this.items.Single();
|
|
||||||
this.items = null;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} else if (this.single == item) {
|
|
||||||
this.single = null;
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if FNA
|
#if FNA
|
||||||
|
|
|
@ -6,41 +6,20 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using Microsoft.Xna.Framework.Input.Touch;
|
using Microsoft.Xna.Framework.Input.Touch;
|
||||||
using static MLEM.Input.GenericInput;
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An input handler is a more advanced wrapper around MonoGame's default input system.
|
/// An input handler is a more advanced wrapper around MonoGame's default input system.
|
||||||
/// It includes keyboard, mouse, gamepad and touch handling through the <see cref="GenericInput"/> wrapper, as well as a new "pressed" state for inputs, the ability for keyboard and gamepad repeat events, and the ability to track down, up and press times for inputs.
|
/// It includes keyboard, mouse, gamepad and touch states, as well as a new "pressed" state for keys and the ability for keyboard and gamepad repeat events.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class InputHandler : GameComponent {
|
public class InputHandler : GameComponent {
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// All values of the <see cref="Buttons"/> enum.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly Buttons[] AllButtons =
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
Enum.GetValues<Buttons>();
|
|
||||||
#else
|
|
||||||
(Buttons[]) Enum.GetValues(typeof(Buttons));
|
|
||||||
#endif
|
|
||||||
/// <summary>
|
|
||||||
/// All values of the <see cref="Keys"/> enum.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly Keys[] AllKeys =
|
|
||||||
#if NET6_0_OR_GREATER
|
|
||||||
Enum.GetValues<Keys>();
|
|
||||||
#else
|
|
||||||
(Keys[]) Enum.GetValues(typeof(Keys));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if FNA
|
#if FNA
|
||||||
private const int MaximumGamePadCount = 4;
|
private const int MaximumGamePadCount = 4;
|
||||||
#else
|
#else
|
||||||
private static readonly int MaximumGamePadCount = GamePad.MaximumGamePadCount;
|
private static readonly int MaximumGamePadCount = GamePad.MaximumGamePadCount;
|
||||||
#endif
|
#endif
|
||||||
private static readonly TouchLocation[] EmptyTouchLocations = new TouchLocation[0];
|
|
||||||
private static readonly GenericInput[] EmptyGenericInputs = new GenericInput[0];
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains all of the gestures that have finished during the last update call.
|
/// Contains all of the gestures that have finished during the last update call.
|
||||||
|
@ -93,31 +72,25 @@ namespace MLEM.Input {
|
||||||
/// Inverted behavior means that, instead of an input counting as pressed when it was up in the last frame and is now down, it will be counted as pressed when it was down in the last frame and is now up.
|
/// Inverted behavior means that, instead of an input counting as pressed when it was up in the last frame and is now down, it will be counted as pressed when it was down in the last frame and is now up.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool InvertPressBehavior;
|
public bool InvertPressBehavior;
|
||||||
/// <summary>
|
|
||||||
/// If your project already handles the processing of MonoGame's gestures elsewhere, you can set this field to true to ensure that this input handler's gesture handling does not override your own, since <see cref="GestureSample"/> objects can only be retrieved once and are then removed from the <see cref="TouchPanel"/>'s queue.
|
|
||||||
/// If this value is set to true, but you still want to be able to use <see cref="Gestures"/>, <see cref="GetGesture"/>, and <see cref="GetViewportGesture"/>, you can make this input handler aware of a gesture for the duration of the update frame that you added it on by using <see cref="AddExternalGesture"/>.
|
|
||||||
/// For more info, see https://mlem.ellpeck.de/articles/input.html#external-gesture-handling.
|
|
||||||
/// </summary>
|
|
||||||
public bool ExternalGestureHandling;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> values that are currently down.
|
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> values that are currently down.
|
||||||
/// Additionally, <see cref="TryGetDownTime"/> or <see cref="GetDownTime"/> can be used to determine the amount of time that a given input has been down for.
|
/// Additionally, <see cref="TryGetDownTime"/> or <see cref="GetDownTime"/> can be used to determine the amount of time that a given input has been down for.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public GenericInput[] InputsDown { get; private set; } = InputHandler.EmptyGenericInputs;
|
public GenericInput[] InputsDown { get; private set; } = Array.Empty<GenericInput>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> that are currently considered pressed.
|
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> that are currently considered pressed.
|
||||||
/// An input is considered pressed if it was up in the last update, and is up in the current one.
|
/// An input is considered pressed if it was up in the last update, and is up in the current one.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public GenericInput[] InputsPressed { get; private set; } = InputHandler.EmptyGenericInputs;
|
public GenericInput[] InputsPressed { get; private set; } = Array.Empty<GenericInput>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the touch state from the last update call
|
/// Contains the touch state from the last update call
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TouchCollection LastTouchState { get; private set; } = new TouchCollection(InputHandler.EmptyTouchLocations);
|
public TouchCollection LastTouchState { get; private set; } = new TouchCollection(Array.Empty<TouchLocation>());
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the current touch state
|
/// Contains the current touch state
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TouchCollection TouchState { get; private set; } = new TouchCollection(InputHandler.EmptyTouchLocations);
|
public TouchCollection TouchState { get; private set; } = new TouchCollection(Array.Empty<TouchLocation>());
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the <see cref="LastTouchState"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
|
/// Contains the <see cref="LastTouchState"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -127,8 +100,8 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IList<TouchLocation> ViewportTouchState { get; private set; }
|
public IList<TouchLocation> ViewportTouchState { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains the amount of gamepads that are currently connected. Note that this value will be set to 0 if <see cref="HandleGamepads"/> is false.
|
/// Contains the amount of gamepads that are currently connected.
|
||||||
/// This field is automatically updated in <see cref="Update()"/>.
|
/// This field is automatically updated in <see cref="Update()"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int ConnectedGamepads { get; private set; }
|
public int ConnectedGamepads { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -180,7 +153,6 @@ namespace MLEM.Input {
|
||||||
private readonly List<GestureSample> gestures = new List<GestureSample>();
|
private readonly List<GestureSample> gestures = new List<GestureSample>();
|
||||||
private readonly HashSet<(GenericInput, int)> consumedPresses = new HashSet<(GenericInput, int)>();
|
private readonly HashSet<(GenericInput, int)> consumedPresses = new HashSet<(GenericInput, int)>();
|
||||||
private readonly Dictionary<(GenericInput, int), DateTime> inputUpTimes = new Dictionary<(GenericInput, int), DateTime>();
|
private readonly Dictionary<(GenericInput, int), DateTime> inputUpTimes = new Dictionary<(GenericInput, int), DateTime>();
|
||||||
private readonly Dictionary<(GenericInput, int), DateTime> inputDownTimes = new Dictionary<(GenericInput, int), DateTime>();
|
|
||||||
private readonly Dictionary<(GenericInput, int), DateTime> inputPressedTimes = new Dictionary<(GenericInput, int), DateTime>();
|
private readonly Dictionary<(GenericInput, int), DateTime> inputPressedTimes = new Dictionary<(GenericInput, int), DateTime>();
|
||||||
|
|
||||||
private Point ViewportOffset => new Point(-this.Game.GraphicsDevice.Viewport.X, -this.Game.GraphicsDevice.Viewport.Y);
|
private Point ViewportOffset => new Point(-this.Game.GraphicsDevice.Viewport.X, -this.Game.GraphicsDevice.Viewport.Y);
|
||||||
|
@ -216,15 +188,15 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
this.consumedPresses.Clear();
|
this.consumedPresses.Clear();
|
||||||
|
|
||||||
this.LastKeyboardState = this.KeyboardState;
|
|
||||||
if (this.HandleKeyboard) {
|
if (this.HandleKeyboard) {
|
||||||
|
this.LastKeyboardState = this.KeyboardState;
|
||||||
this.KeyboardState = active ? Keyboard.GetState() : default;
|
this.KeyboardState = active ? Keyboard.GetState() : default;
|
||||||
var pressedKeys = this.KeyboardState.GetPressedKeys();
|
var pressedKeys = this.KeyboardState.GetPressedKeys();
|
||||||
foreach (var pressed in pressedKeys)
|
foreach (var pressed in pressedKeys)
|
||||||
this.AccumulateDown(pressed, -1);
|
this.AccumulateDown(pressed, -1);
|
||||||
|
|
||||||
this.triggerKeyRepeat = false;
|
|
||||||
if (this.HandleKeyboardRepeats) {
|
if (this.HandleKeyboardRepeats) {
|
||||||
|
this.triggerKeyRepeat = false;
|
||||||
// the key that started being held most recently should be the one being repeated
|
// the key that started being held most recently should be the one being repeated
|
||||||
this.heldKey = pressedKeys.OrderBy(k => this.GetDownTime(k)).FirstOrDefault();
|
this.heldKey = pressedKeys.OrderBy(k => this.GetDownTime(k)).FirstOrDefault();
|
||||||
if (this.TryGetDownTime(this.heldKey, out var heldTime)) {
|
if (this.TryGetDownTime(this.heldKey, out var heldTime)) {
|
||||||
|
@ -239,17 +211,11 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.heldKey = Keys.None;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.KeyboardState = default;
|
|
||||||
this.triggerKeyRepeat = false;
|
|
||||||
this.heldKey = Keys.None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.LastMouseState = this.MouseState;
|
|
||||||
if (this.HandleMouse) {
|
if (this.HandleMouse) {
|
||||||
|
this.LastMouseState = this.MouseState;
|
||||||
var state = Mouse.GetState();
|
var state = Mouse.GetState();
|
||||||
if (active && this.Game.GraphicsDevice.Viewport.Bounds.Contains(state.X, state.Y)) {
|
if (active && this.Game.GraphicsDevice.Viewport.Bounds.Contains(state.X, state.Y)) {
|
||||||
this.MouseState = state;
|
this.MouseState = state;
|
||||||
|
@ -265,8 +231,6 @@ namespace MLEM.Input {
|
||||||
this.MouseState = new MouseState(state.X, state.Y, state.ScrollWheelValue, 0, 0, 0, 0, 0, state.HorizontalScrollWheelValue);
|
this.MouseState = new MouseState(state.X, state.Y, state.ScrollWheelValue, 0, 0, 0, 0, 0, state.HorizontalScrollWheelValue);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.MouseState = default;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.HandleGamepads) {
|
if (this.HandleGamepads) {
|
||||||
|
@ -277,8 +241,8 @@ namespace MLEM.Input {
|
||||||
if (GamePad.GetCapabilities((PlayerIndex) i).IsConnected) {
|
if (GamePad.GetCapabilities((PlayerIndex) i).IsConnected) {
|
||||||
if (active) {
|
if (active) {
|
||||||
this.gamepads[i] = GamePad.GetState((PlayerIndex) i);
|
this.gamepads[i] = GamePad.GetState((PlayerIndex) i);
|
||||||
foreach (var button in InputHandler.AllButtons) {
|
foreach (var button in EnumHelper.Buttons) {
|
||||||
if (this.IsDown(button, i))
|
if (this.IsGamepadButtonDown(button, i))
|
||||||
this.AccumulateDown(button, i);
|
this.AccumulateDown(button, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -287,11 +251,11 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.HandleGamepadRepeats) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
this.triggerGamepadButtonRepeat[i] = false;
|
this.triggerGamepadButtonRepeat[i] = false;
|
||||||
if (this.HandleGamepadRepeats) {
|
this.heldGamepadButtons[i] = EnumHelper.Buttons
|
||||||
this.heldGamepadButtons[i] = InputHandler.AllButtons
|
.Where(b => this.IsGamepadButtonDown(b, i))
|
||||||
.Where(b => this.IsDown(b, i))
|
|
||||||
.OrderBy(b => this.GetDownTime(b, i))
|
.OrderBy(b => this.GetDownTime(b, i))
|
||||||
.Cast<Buttons?>().FirstOrDefault();
|
.Cast<Buttons?>().FirstOrDefault();
|
||||||
if (this.heldGamepadButtons[i].HasValue && this.TryGetDownTime(this.heldGamepadButtons[i].Value, out var heldTime, i)) {
|
if (this.heldGamepadButtons[i].HasValue && this.TryGetDownTime(this.heldGamepadButtons[i].Value, out var heldTime, i)) {
|
||||||
|
@ -303,24 +267,15 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.heldGamepadButtons[i] = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.ConnectedGamepads = 0;
|
|
||||||
for (var i = 0; i < InputHandler.MaximumGamePadCount; i++) {
|
|
||||||
this.lastGamepads[i] = this.gamepads[i];
|
|
||||||
this.gamepads[i] = default;
|
|
||||||
this.triggerGamepadButtonRepeat[i] = false;
|
|
||||||
this.heldGamepadButtons[i] = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.HandleTouch) {
|
||||||
this.LastTouchState = this.TouchState;
|
this.LastTouchState = this.TouchState;
|
||||||
this.LastViewportTouchState = this.ViewportTouchState;
|
this.LastViewportTouchState = this.ViewportTouchState;
|
||||||
if (this.HandleTouch) {
|
|
||||||
this.TouchState = active ? TouchPanel.GetState() : new TouchCollection(InputHandler.EmptyTouchLocations);
|
this.TouchState = active ? TouchPanel.GetState() : new TouchCollection(Array.Empty<TouchLocation>());
|
||||||
if (this.TouchState.Count > 0 && this.ViewportOffset != Point.Zero) {
|
if (this.TouchState.Count > 0 && this.ViewportOffset != Point.Zero) {
|
||||||
this.ViewportTouchState = new List<TouchLocation>();
|
this.ViewportTouchState = new List<TouchLocation>();
|
||||||
foreach (var touch in this.TouchState) {
|
foreach (var touch in this.TouchState) {
|
||||||
|
@ -332,20 +287,14 @@ namespace MLEM.Input {
|
||||||
this.ViewportTouchState = this.TouchState;
|
this.ViewportTouchState = this.TouchState;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we still want to clear gestures when handling externally to maintain the per-frame gesture system
|
|
||||||
this.gestures.Clear();
|
this.gestures.Clear();
|
||||||
if (active && !this.ExternalGestureHandling) {
|
while (active && TouchPanel.IsGestureAvailable)
|
||||||
while (TouchPanel.IsGestureAvailable)
|
|
||||||
this.gestures.Add(TouchPanel.ReadGesture());
|
this.gestures.Add(TouchPanel.ReadGesture());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.TouchState = new TouchCollection(InputHandler.EmptyTouchLocations);
|
|
||||||
this.gestures.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.inputsDownAccum.Count <= 0 && this.inputsDown.Count <= 0) {
|
if (this.inputsDownAccum.Count <= 0 && this.inputsDown.Count <= 0) {
|
||||||
this.InputsPressed = InputHandler.EmptyGenericInputs;
|
this.InputsPressed = Array.Empty<GenericInput>();
|
||||||
this.InputsDown = InputHandler.EmptyGenericInputs;
|
this.InputsDown = Array.Empty<GenericInput>();
|
||||||
} else {
|
} else {
|
||||||
// handle pressed inputs
|
// handle pressed inputs
|
||||||
var pressed = new List<GenericInput>();
|
var pressed = new List<GenericInput>();
|
||||||
|
@ -358,15 +307,13 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
this.InputsPressed = pressed.ToArray();
|
this.InputsPressed = pressed.ToArray();
|
||||||
|
|
||||||
// handle inputs that changed between down and up
|
// handle inputs that changed to up
|
||||||
|
foreach (var key in this.inputsDownAccum.Keys)
|
||||||
|
this.inputUpTimes.Remove(key);
|
||||||
foreach (var key in this.inputsDown.Keys) {
|
foreach (var key in this.inputsDown.Keys) {
|
||||||
if (!this.inputsDownAccum.ContainsKey(key))
|
if (!this.inputsDownAccum.ContainsKey(key))
|
||||||
this.inputUpTimes[key] = DateTime.UtcNow;
|
this.inputUpTimes[key] = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
foreach (var key in this.inputsDownAccum.Keys) {
|
|
||||||
if (!this.inputsDown.ContainsKey(key))
|
|
||||||
this.inputDownTimes[key] = DateTime.UtcNow;
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle inputs that are currently down
|
// handle inputs that are currently down
|
||||||
this.InputsDown = this.inputsDownAccum.Keys.Select(key => key.Item1).ToArray();
|
this.InputsDown = this.inputsDownAccum.Keys.Select(key => key.Item1).ToArray();
|
||||||
|
@ -399,39 +346,22 @@ namespace MLEM.Input {
|
||||||
return this.gamepads[index];
|
return this.gamepads[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given modifier key is down.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="modifier">The modifier key</param>
|
|
||||||
/// <returns>If the modifier key is down</returns>
|
|
||||||
public bool IsModifierKeyDown(ModifierKey modifier) {
|
|
||||||
foreach (var key in modifier.GetKeys()) {
|
|
||||||
if (this.IsDown(key))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
|
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
|
|
||||||
public bool IsKeyDown(Keys key) {
|
public bool IsKeyDown(Keys key) {
|
||||||
return this.KeyboardState.IsKeyDown(key);
|
return this.KeyboardState.IsKeyDown(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
|
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
|
|
||||||
public bool IsKeyUp(Keys key) {
|
public bool IsKeyUp(Keys key) {
|
||||||
return this.KeyboardState.IsKeyUp(key);
|
return this.KeyboardState.IsKeyUp(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
|
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
|
|
||||||
public bool WasKeyDown(Keys key) {
|
public bool WasKeyDown(Keys key) {
|
||||||
return this.LastKeyboardState.IsKeyDown(key);
|
return this.LastKeyboardState.IsKeyDown(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
|
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
|
|
||||||
public bool WasKeyUp(Keys key) {
|
public bool WasKeyUp(Keys key) {
|
||||||
return this.LastKeyboardState.IsKeyUp(key);
|
return this.LastKeyboardState.IsKeyUp(key);
|
||||||
}
|
}
|
||||||
|
@ -443,7 +373,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key to query</param>
|
/// <param name="key">The key to query</param>
|
||||||
/// <returns>If the key is pressed</returns>
|
/// <returns>If the key is pressed</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
|
|
||||||
public bool IsKeyPressed(Keys key) {
|
public bool IsKeyPressed(Keys key) {
|
||||||
// if the queried key is the held key and a repeat should be triggered, return true
|
// if the queried key is the held key and a repeat should be triggered, return true
|
||||||
if (this.HandleKeyboardRepeats && key == this.heldKey && this.triggerKeyRepeat)
|
if (this.HandleKeyboardRepeats && key == this.heldKey && this.triggerKeyRepeat)
|
||||||
|
@ -459,7 +388,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key to query</param>
|
/// <param name="key">The key to query</param>
|
||||||
/// <returns>If the key is pressed</returns>
|
/// <returns>If the key is pressed</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedIgnoreRepeats instead.")]
|
|
||||||
public bool IsKeyPressedIgnoreRepeats(Keys key) {
|
public bool IsKeyPressedIgnoreRepeats(Keys key) {
|
||||||
if (this.InvertPressBehavior)
|
if (this.InvertPressBehavior)
|
||||||
return this.WasKeyDown(key) && this.IsKeyUp(key);
|
return this.WasKeyDown(key) && this.IsKeyUp(key);
|
||||||
|
@ -471,7 +399,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key to query.</param>
|
/// <param name="key">The key to query.</param>
|
||||||
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
|
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
|
|
||||||
public bool IsKeyPressedAvailable(Keys key) {
|
public bool IsKeyPressedAvailable(Keys key) {
|
||||||
return this.IsKeyPressed(key) && !this.IsPressConsumed(key);
|
return this.IsKeyPressed(key) && !this.IsPressConsumed(key);
|
||||||
}
|
}
|
||||||
|
@ -484,7 +411,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key to query.</param>
|
/// <param name="key">The key to query.</param>
|
||||||
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
|
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
|
|
||||||
public bool TryConsumeKeyPressed(Keys key) {
|
public bool TryConsumeKeyPressed(Keys key) {
|
||||||
if (this.IsKeyPressedAvailable(key)) {
|
if (this.IsKeyPressedAvailable(key)) {
|
||||||
this.consumedPresses.Add((key, -1));
|
this.consumedPresses.Add((key, -1));
|
||||||
|
@ -493,12 +419,24 @@ namespace MLEM.Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns whether the given modifier key is down.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="modifier">The modifier key</param>
|
||||||
|
/// <returns>If the modifier key is down</returns>
|
||||||
|
public bool IsModifierKeyDown(ModifierKey modifier) {
|
||||||
|
foreach (var key in modifier.GetKeys()) {
|
||||||
|
if (this.IsKeyDown(key))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns whether the given mouse button is currently down.
|
/// Returns whether the given mouse button is currently down.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <returns>Whether or not the queried button is down</returns>
|
/// <returns>Whether or not the queried button is down</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
|
|
||||||
public bool IsMouseButtonDown(MouseButton button) {
|
public bool IsMouseButtonDown(MouseButton button) {
|
||||||
return this.MouseState.GetState(button) == ButtonState.Pressed;
|
return this.MouseState.GetState(button) == ButtonState.Pressed;
|
||||||
}
|
}
|
||||||
|
@ -508,7 +446,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <returns>Whether or not the queried button is up</returns>
|
/// <returns>Whether or not the queried button is up</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
|
|
||||||
public bool IsMouseButtonUp(MouseButton button) {
|
public bool IsMouseButtonUp(MouseButton button) {
|
||||||
return this.MouseState.GetState(button) == ButtonState.Released;
|
return this.MouseState.GetState(button) == ButtonState.Released;
|
||||||
}
|
}
|
||||||
|
@ -518,7 +455,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <returns>Whether or not the queried button was down</returns>
|
/// <returns>Whether or not the queried button was down</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
|
|
||||||
public bool WasMouseButtonDown(MouseButton button) {
|
public bool WasMouseButtonDown(MouseButton button) {
|
||||||
return this.LastMouseState.GetState(button) == ButtonState.Pressed;
|
return this.LastMouseState.GetState(button) == ButtonState.Pressed;
|
||||||
}
|
}
|
||||||
|
@ -528,7 +464,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <returns>Whether or not the queried button was up</returns>
|
/// <returns>Whether or not the queried button was up</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
|
|
||||||
public bool WasMouseButtonUp(MouseButton button) {
|
public bool WasMouseButtonUp(MouseButton button) {
|
||||||
return this.LastMouseState.GetState(button) == ButtonState.Released;
|
return this.LastMouseState.GetState(button) == ButtonState.Released;
|
||||||
}
|
}
|
||||||
|
@ -539,7 +474,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <returns>Whether the button is pressed</returns>
|
/// <returns>Whether the button is pressed</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
|
|
||||||
public bool IsMouseButtonPressed(MouseButton button) {
|
public bool IsMouseButtonPressed(MouseButton button) {
|
||||||
if (this.InvertPressBehavior)
|
if (this.InvertPressBehavior)
|
||||||
return this.WasMouseButtonDown(button) && this.IsMouseButtonUp(button);
|
return this.WasMouseButtonDown(button) && this.IsMouseButtonUp(button);
|
||||||
|
@ -551,7 +485,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query.</param>
|
/// <param name="button">The button to query.</param>
|
||||||
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
|
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
|
|
||||||
public bool IsMouseButtonPressedAvailable(MouseButton button) {
|
public bool IsMouseButtonPressedAvailable(MouseButton button) {
|
||||||
return this.IsMouseButtonPressed(button) && !this.IsPressConsumed(button);
|
return this.IsMouseButtonPressed(button) && !this.IsPressConsumed(button);
|
||||||
}
|
}
|
||||||
|
@ -563,7 +496,6 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="button">The button to query.</param>
|
/// <param name="button">The button to query.</param>
|
||||||
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
|
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
|
|
||||||
public bool TryConsumeMouseButtonPressed(MouseButton button) {
|
public bool TryConsumeMouseButtonPressed(MouseButton button) {
|
||||||
if (this.IsMouseButtonPressedAvailable(button)) {
|
if (this.IsMouseButtonPressedAvailable(button)) {
|
||||||
this.consumedPresses.Add((button, -1));
|
this.consumedPresses.Add((button, -1));
|
||||||
|
@ -573,7 +505,6 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="GamePadState.IsButtonDown"/>
|
/// <inheritdoc cref="GamePadState.IsButtonDown"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
|
|
||||||
public bool IsGamepadButtonDown(Buttons button, int index = -1) {
|
public bool IsGamepadButtonDown(Buttons button, int index = -1) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
|
@ -586,7 +517,6 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
|
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
|
|
||||||
public bool IsGamepadButtonUp(Buttons button, int index = -1) {
|
public bool IsGamepadButtonUp(Buttons button, int index = -1) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
|
@ -599,7 +529,6 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="GamePadState.IsButtonDown"/>
|
/// <inheritdoc cref="GamePadState.IsButtonDown"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
|
|
||||||
public bool WasGamepadButtonDown(Buttons button, int index = -1) {
|
public bool WasGamepadButtonDown(Buttons button, int index = -1) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
|
@ -612,7 +541,6 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
|
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
|
|
||||||
public bool WasGamepadButtonUp(Buttons button, int index = -1) {
|
public bool WasGamepadButtonUp(Buttons button, int index = -1) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
|
@ -632,7 +560,6 @@ namespace MLEM.Input {
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
|
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
|
||||||
/// <returns>Whether the given button is pressed</returns>
|
/// <returns>Whether the given button is pressed</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
|
|
||||||
public bool IsGamepadButtonPressed(Buttons button, int index = -1) {
|
public bool IsGamepadButtonPressed(Buttons button, int index = -1) {
|
||||||
if (this.HandleGamepadRepeats) {
|
if (this.HandleGamepadRepeats) {
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
@ -656,7 +583,6 @@ namespace MLEM.Input {
|
||||||
/// <param name="button">The button to query</param>
|
/// <param name="button">The button to query</param>
|
||||||
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
|
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
|
||||||
/// <returns>Whether the given button is pressed</returns>
|
/// <returns>Whether the given button is pressed</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedIgnoreRepeats instead.")]
|
|
||||||
public bool IsGamepadButtonPressedIgnoreRepeats(Buttons button, int index = -1) {
|
public bool IsGamepadButtonPressedIgnoreRepeats(Buttons button, int index = -1) {
|
||||||
if (this.InvertPressBehavior)
|
if (this.InvertPressBehavior)
|
||||||
return this.WasGamepadButtonDown(button, index) && this.IsGamepadButtonUp(button, index);
|
return this.WasGamepadButtonDown(button, index) && this.IsGamepadButtonUp(button, index);
|
||||||
|
@ -669,7 +595,6 @@ namespace MLEM.Input {
|
||||||
/// <param name="button">The button to query.</param>
|
/// <param name="button">The button to query.</param>
|
||||||
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
|
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
|
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
|
|
||||||
public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) {
|
public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) {
|
||||||
return this.IsGamepadButtonPressed(button) && !this.IsPressConsumed(button, index) && (index < 0 || !this.IsPressConsumed(button));
|
return this.IsGamepadButtonPressed(button) && !this.IsPressConsumed(button, index) && (index < 0 || !this.IsPressConsumed(button));
|
||||||
}
|
}
|
||||||
|
@ -683,7 +608,6 @@ namespace MLEM.Input {
|
||||||
/// <param name="button">The button to query.</param>
|
/// <param name="button">The button to query.</param>
|
||||||
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
|
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
|
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
|
||||||
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
|
|
||||||
public bool TryConsumeGamepadButtonPressed(Buttons button, int index = -1) {
|
public bool TryConsumeGamepadButtonPressed(Buttons button, int index = -1) {
|
||||||
if (this.IsGamepadButtonPressedAvailable(button, index)) {
|
if (this.IsGamepadButtonPressedAvailable(button, index)) {
|
||||||
this.consumedPresses.Add((button, index));
|
this.consumedPresses.Add((button, index));
|
||||||
|
@ -726,19 +650,6 @@ namespace MLEM.Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a gesture to the <see cref="Gestures"/> collection and allows it to be queried using <see cref="GetGesture"/> and <see cref="GetViewportGesture"/> for the duration of the update frame that it was added on.
|
|
||||||
/// This method should be used when <see cref="ExternalGestureHandling"/> is set to true, but <see cref="GetGesture"/> and <see cref="GetViewportGesture"/> should still be available.
|
|
||||||
/// For more info, see https://mlem.ellpeck.de/articles/input.html#external-gesture-handling.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sample">The gesture sample to add.</param>
|
|
||||||
/// <exception cref="InvalidOperationException">Thrown if <see cref="ExternalGestureHandling"/> is false.</exception>
|
|
||||||
public void AddExternalGesture(GestureSample sample) {
|
|
||||||
if (!this.ExternalGestureHandling)
|
|
||||||
throw new InvalidOperationException($"Cannot add external gestures if {nameof(this.ExternalGestureHandling)} is false");
|
|
||||||
this.gestures.Add(sample);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns if a given control of any kind is down.
|
/// Returns if a given control of any kind is down.
|
||||||
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
|
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
|
||||||
|
@ -746,21 +657,15 @@ namespace MLEM.Input {
|
||||||
/// <param name="control">The control whose down state to query</param>
|
/// <param name="control">The control whose down state to query</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
||||||
/// <returns>Whether the given control is down</returns>
|
/// <returns>Whether the given control is down</returns>
|
||||||
|
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
|
||||||
public bool IsDown(GenericInput control, int index = -1) {
|
public bool IsDown(GenericInput control, int index = -1) {
|
||||||
switch (control.Type) {
|
switch (control.Type) {
|
||||||
case InputType.Keyboard:
|
case GenericInput.InputType.Keyboard:
|
||||||
return this.KeyboardState.IsKeyDown(control);
|
return this.IsKeyDown(control);
|
||||||
case InputType.Gamepad:
|
case GenericInput.InputType.Gamepad:
|
||||||
if (index < 0) {
|
return this.IsGamepadButtonDown(control, index);
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
case GenericInput.InputType.Mouse:
|
||||||
if (this.GetGamepadState(i).GetAnalogValue(control) > this.GamepadButtonDeadzone)
|
return this.IsMouseButtonDown(control);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return this.GetGamepadState(index).GetAnalogValue(control) > this.GamepadButtonDeadzone;
|
|
||||||
case InputType.Mouse:
|
|
||||||
return this.MouseState.GetState(control) == ButtonState.Pressed;
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -773,75 +678,15 @@ namespace MLEM.Input {
|
||||||
/// <param name="control">The control whose up state to query</param>
|
/// <param name="control">The control whose up state to query</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
||||||
/// <returns>Whether the given control is up.</returns>
|
/// <returns>Whether the given control is up.</returns>
|
||||||
|
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
|
||||||
public bool IsUp(GenericInput control, int index = -1) {
|
public bool IsUp(GenericInput control, int index = -1) {
|
||||||
switch (control.Type) {
|
switch (control.Type) {
|
||||||
case InputType.Keyboard:
|
case GenericInput.InputType.Keyboard:
|
||||||
return this.KeyboardState.IsKeyUp(control);
|
return this.IsKeyUp(control);
|
||||||
case InputType.Gamepad:
|
case GenericInput.InputType.Gamepad:
|
||||||
if (index < 0) {
|
return this.IsGamepadButtonUp(control, index);
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
case GenericInput.InputType.Mouse:
|
||||||
if (this.GetGamepadState(i).GetAnalogValue(control) <= this.GamepadButtonDeadzone)
|
return this.IsMouseButtonUp(control);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return this.GetGamepadState(index).GetAnalogValue(control) <= this.GamepadButtonDeadzone;
|
|
||||||
case InputType.Mouse:
|
|
||||||
return this.MouseState.GetState(control) == ButtonState.Released;
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns if a given control of any kind was down in the last update call.
|
|
||||||
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose down state to query</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
|
||||||
/// <returns>Whether the given control was down</returns>
|
|
||||||
public bool WasDown(GenericInput control, int index = -1) {
|
|
||||||
switch (control.Type) {
|
|
||||||
case InputType.Keyboard:
|
|
||||||
return this.LastKeyboardState.IsKeyDown(control);
|
|
||||||
case InputType.Gamepad:
|
|
||||||
if (index < 0) {
|
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
|
||||||
if (this.GetLastGamepadState(i).GetAnalogValue(control) > this.GamepadButtonDeadzone)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return this.GetLastGamepadState(index).GetAnalogValue(control) > this.GamepadButtonDeadzone;
|
|
||||||
case InputType.Mouse:
|
|
||||||
return this.LastMouseState.GetState(control) == ButtonState.Pressed;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns if a given control of any kind was up in the last update call.
|
|
||||||
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose up state to query</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
|
||||||
/// <returns>Whether the given control was up.</returns>
|
|
||||||
public bool WasUp(GenericInput control, int index = -1) {
|
|
||||||
switch (control.Type) {
|
|
||||||
case InputType.Keyboard:
|
|
||||||
return this.LastKeyboardState.IsKeyUp(control);
|
|
||||||
case InputType.Gamepad:
|
|
||||||
if (index < 0) {
|
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
|
||||||
if (this.GetLastGamepadState(i).GetAnalogValue(control) <= this.GamepadButtonDeadzone)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return this.GetLastGamepadState(index).GetAnalogValue(control) <= this.GamepadButtonDeadzone;
|
|
||||||
case InputType.Mouse:
|
|
||||||
return this.LastMouseState.GetState(control) == ButtonState.Released;
|
|
||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -849,47 +694,23 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns if a given control of any kind is pressed.
|
/// Returns if a given control of any kind is pressed.
|
||||||
/// If <see cref="HandleKeyboardRepeats"/> or <see cref="HandleGamepadRepeats"/> are true, this method will also return true to signify a key or gamepad button repeat.
|
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
|
||||||
/// An input is considered pressed if it was not down the last update call, but is down the current update call. If <see cref="InvertPressBehavior"/> is true, this behavior is inverted.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="control">The control whose pressed state to query</param>
|
/// <param name="control">The control whose pressed state to query</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
||||||
/// <returns>Whether the given control is pressed.</returns>
|
/// <returns>Whether the given control is pressed.</returns>
|
||||||
|
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
|
||||||
public bool IsPressed(GenericInput control, int index = -1) {
|
public bool IsPressed(GenericInput control, int index = -1) {
|
||||||
// handle repeat events for specific inputs, and delegate to default "ignore repeats" behavior otherwise
|
|
||||||
switch (control.Type) {
|
switch (control.Type) {
|
||||||
case InputType.Keyboard:
|
case GenericInput.InputType.Keyboard:
|
||||||
if (this.HandleKeyboardRepeats && (Keys) control == this.heldKey && this.triggerKeyRepeat)
|
return this.IsKeyPressed(control);
|
||||||
return true;
|
case GenericInput.InputType.Gamepad:
|
||||||
break;
|
return this.IsGamepadButtonPressed(control, index);
|
||||||
case InputType.Gamepad:
|
case GenericInput.InputType.Mouse:
|
||||||
if (this.HandleGamepadRepeats) {
|
return this.IsMouseButtonPressed(control);
|
||||||
if (index < 0) {
|
default:
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
return false;
|
||||||
if (this.heldGamepadButtons[i] == (Buttons) control && this.triggerGamepadButtonRepeat[i])
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
} else if (this.heldGamepadButtons[index] == (Buttons) control && this.triggerGamepadButtonRepeat[index]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return this.IsPressedIgnoreRepeats(control, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// An input is considered pressed if it was not down the last update call, but is down the current update call. If <see cref="InvertPressBehavior"/> is true, this behavior is inverted.
|
|
||||||
/// This has the same behavior as <see cref="IsPressed"/>, but ignores keyboard and gamepad repeat events.
|
|
||||||
/// If <see cref="HandleKeyboardRepeats"/> and <see cref="HandleGamepadRepeats"/> are false, this method does the same as <see cref="IsPressed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose pressed state to query</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
|
|
||||||
/// <returns>Whether the given control is pressed, ignoring repeat events.</returns>
|
|
||||||
public bool IsPressedIgnoreRepeats(GenericInput control, int index = -1) {
|
|
||||||
if (this.InvertPressBehavior)
|
|
||||||
return this.WasDown(control, index) && this.IsUp(control, index);
|
|
||||||
return this.WasUp(control, index) && this.IsDown(control, index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -899,7 +720,16 @@ namespace MLEM.Input {
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
|
/// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
|
||||||
public bool IsPressedAvailable(GenericInput control, int index = -1) {
|
public bool IsPressedAvailable(GenericInput control, int index = -1) {
|
||||||
return this.IsPressed(control, index) && !this.IsPressConsumed(control, index);
|
switch (control.Type) {
|
||||||
|
case GenericInput.InputType.Keyboard:
|
||||||
|
return this.IsKeyPressedAvailable(control);
|
||||||
|
case GenericInput.InputType.Gamepad:
|
||||||
|
return this.IsGamepadButtonPressedAvailable(control, index);
|
||||||
|
case GenericInput.InputType.Mouse:
|
||||||
|
return this.IsMouseButtonPressedAvailable(control);
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -910,48 +740,16 @@ namespace MLEM.Input {
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
|
/// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
|
||||||
public bool TryConsumePressed(GenericInput control, int index = -1) {
|
public bool TryConsumePressed(GenericInput control, int index = -1) {
|
||||||
if (this.IsPressedAvailable(control, index)) {
|
switch (control.Type) {
|
||||||
this.consumedPresses.Add((control, index));
|
case GenericInput.InputType.Keyboard:
|
||||||
return true;
|
return this.TryConsumeKeyPressed(control);
|
||||||
}
|
case GenericInput.InputType.Gamepad:
|
||||||
|
return this.TryConsumeGamepadButtonPressed(control, index);
|
||||||
|
case GenericInput.InputType.Mouse:
|
||||||
|
return this.TryConsumeMouseButtonPressed(control);
|
||||||
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns if a given control of any kind was just let go this frame, and had been down for less than the given <paramref name="time"/> before that. Essentially, this action signifies a short press action.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose pressed state to query.</param>
|
|
||||||
/// <param name="time">The maximum time that the control should have been down for.</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
|
||||||
/// <returns>Whether the given control was pressed for less than the given time.</returns>
|
|
||||||
public bool WasPressedForLess(GenericInput control, TimeSpan time, int index = -1) {
|
|
||||||
return this.WasDown(control, index) && this.IsUp(control, index) && this.GetDownTime(control, index) < time;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns if a given control of any kind was just let go this frame, and had been down for less than the given <paramref name="time"/> before that, and if the press has not been consumed yet using <see cref="TryConsumePressedForLess"/>. Essentially, this action signifies a short press action.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose pressed state to query.</param>
|
|
||||||
/// <param name="time">The maximum time that the control should have been down for.</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
|
||||||
/// <returns>Whether the given control was pressed for less than the given time, and the press has not been consumed yet.</returns>
|
|
||||||
public bool WasPressedForLessAvailable(GenericInput control, TimeSpan time, int index = -1) {
|
|
||||||
return this.WasPressedForLess(control, time, index) && !this.IsPressConsumed(control, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns if a given control of any kind was just let go this frame, and had been down for less than the given <paramref name="time"/> before that, and if the press has not been consumed yet using <see cref="TryConsumePressedForLess"/>, and marks the press as consumed if it is. Essentially, this action signifies a short press action.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="control">The control whose pressed state to query.</param>
|
|
||||||
/// <param name="time">The maximum time that the control should have been down for.</param>
|
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
|
||||||
/// <returns>Whether the given control was pressed for less than the given time, and the press was successfully consumed.</returns>
|
|
||||||
public bool TryConsumePressedForLess(GenericInput control, TimeSpan time, int index = -1) {
|
|
||||||
if (this.WasPressedForLessAvailable(control, time, index)) {
|
|
||||||
this.consumedPresses.Add((control, index));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IsDown"/>
|
/// <inheritdoc cref="IsDown"/>
|
||||||
|
@ -992,26 +790,23 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to retrieve the amount of time that a given <see cref="GenericInput"/> has been held down for.
|
/// Tries to retrieve the amount of time that a given <see cref="GenericInput"/> has been held down for.
|
||||||
/// If the input is currently down or has been down previously, this method returns true and the amount of time that it has currently or last been down for is stored in <paramref name="downTime"/>.
|
/// If the input is currently down, this method returns true and the amount of time that it has been down for is stored in <paramref name="downTime"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">The input whose down time to query.</param>
|
/// <param name="input">The input whose down time to query.</param>
|
||||||
/// <param name="downTime">The resulting down time, or <see cref="TimeSpan.Zero"/> if the input is not being held.</param>
|
/// <param name="downTime">The resulting down time, or <see cref="TimeSpan.Zero"/> if the input is not being held.</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the input is currently being held.</returns>
|
/// <returns>Whether the input is currently being held.</returns>
|
||||||
public bool TryGetDownTime(GenericInput input, out TimeSpan downTime, int index = -1) {
|
public bool TryGetDownTime(GenericInput input, out TimeSpan downTime, int index = -1) {
|
||||||
if (this.inputDownTimes.TryGetValue((input, index), out var wentDown)) {
|
if (this.inputsDown.TryGetValue((input, index), out var start)) {
|
||||||
// if we're currently down, we return the amount of time we've been down for so far
|
downTime = DateTime.UtcNow - start;
|
||||||
// if we're not currently down, we return the last amount of time we were down for
|
|
||||||
downTime = (this.IsDown(input) || !this.inputUpTimes.TryGetValue((input, index), out var wentUp) ? DateTime.UtcNow : wentUp) - wentDown;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
downTime = default;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the current or last amount of time that a given <see cref="GenericInput"/> has been held down for.
|
/// Returns the amount of time that a given <see cref="GenericInput"/> has been held down for.
|
||||||
/// If this input isn't currently down and has not been down previously, this method returns <see cref="TimeSpan.Zero"/>.
|
/// If this input isn't currently down, this method returns <see cref="TimeSpan.Zero"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">The input whose down time to query.</param>
|
/// <param name="input">The input whose down time to query.</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
|
@ -1023,26 +818,23 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to retrieve the amount of time that a given <see cref="GenericInput"/> has been up for since the last time it was down.
|
/// Tries to retrieve the amount of time that a given <see cref="GenericInput"/> has been up for since the last time it was down.
|
||||||
/// If the input has previously been down, this method returns true and the amount of time that it has been up for is stored in <paramref name="upTime"/>.
|
/// If the input is currently up, this method returns true and the amount of time that it has been up for is stored in <paramref name="upTime"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">The input whose up time to query.</param>
|
/// <param name="input">The input whose up time to query.</param>
|
||||||
/// <param name="upTime">The resulting up time, or <see cref="TimeSpan.Zero"/> if the input is being held.</param>
|
/// <param name="upTime">The resulting up time, or <see cref="TimeSpan.Zero"/> if the input is being held.</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
/// <returns>Whether the input is currently up.</returns>
|
/// <returns>Whether the input is currently up.</returns>
|
||||||
public bool TryGetUpTime(GenericInput input, out TimeSpan upTime, int index = -1) {
|
public bool TryGetUpTime(GenericInput input, out TimeSpan upTime, int index = -1) {
|
||||||
if (this.inputUpTimes.TryGetValue((input, index), out var wentUp)) {
|
if (this.inputUpTimes.TryGetValue((input, index), out var start)) {
|
||||||
// if we're currently up, we return the amount of time we've been up for so far
|
upTime = DateTime.UtcNow - start;
|
||||||
// if we're not currently up, we return the last amount of time we were up for
|
|
||||||
upTime = (this.IsUp(input) || !this.inputDownTimes.TryGetValue((input, index), out var wentDown) ? DateTime.UtcNow : wentDown) - wentUp;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
upTime = default;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the amount of time that a given <see cref="GenericInput"/> has last been up for since the last time it was down.
|
/// Returns the amount of time that a given <see cref="GenericInput"/> has been up for since the last time it was down.
|
||||||
/// If this input hasn't been down previously, this method returns <see cref="TimeSpan.Zero"/>.
|
/// If this input isn't currently up, this method returns <see cref="TimeSpan.Zero"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">The input whose up time to query.</param>
|
/// <param name="input">The input whose up time to query.</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
|
@ -1065,13 +857,12 @@ namespace MLEM.Input {
|
||||||
lastPressTime = DateTime.UtcNow - start;
|
lastPressTime = DateTime.UtcNow - start;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
lastPressTime = default;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the amount of time that has passed since a given <see cref="GenericInput"/> last counted as pressed.
|
/// Returns the amount of time that has passed since a given <see cref="GenericInput"/> last counted as pressed.
|
||||||
/// If this input hasn't been pressed previously, this method returns <see cref="TimeSpan.Zero"/>.
|
/// If this input hasn't been pressed previously, or is currently pressed, this method returns <see cref="TimeSpan.Zero"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">The input whose up time to query.</param>
|
/// <param name="input">The input whose up time to query.</param>
|
||||||
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
|
||||||
|
|
|
@ -3,10 +3,6 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
#if NET452
|
|
||||||
using MLEM.Extensions;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A keybind represents a generic way to trigger input.
|
/// A keybind represents a generic way to trigger input.
|
||||||
|
@ -17,10 +13,8 @@ namespace MLEM.Input {
|
||||||
[DataContract]
|
[DataContract]
|
||||||
public class Keybind : IComparable<Keybind>, IComparable {
|
public class Keybind : IComparable<Keybind>, IComparable {
|
||||||
|
|
||||||
private static readonly Combination[] EmptyCombinations = new Combination[0];
|
|
||||||
|
|
||||||
[DataMember]
|
[DataMember]
|
||||||
private Combination[] combinations = Keybind.EmptyCombinations;
|
private Combination[] combinations = Array.Empty<Combination>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new keybind and adds the given key and modifiers using <see cref="Add(MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
|
/// Creates a new keybind and adds the given key and modifiers using <see cref="Add(MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
|
||||||
|
@ -83,7 +77,7 @@ namespace MLEM.Input {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>This keybind, for chaining</returns>
|
/// <returns>This keybind, for chaining</returns>
|
||||||
public Keybind Clear() {
|
public Keybind Clear() {
|
||||||
this.combinations = Keybind.EmptyCombinations;
|
this.combinations = Array.Empty<Combination>();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,21 +117,6 @@ namespace MLEM.Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether this keybind was considered to be down in the last update call.
|
|
||||||
/// See <see cref="InputHandler.WasDown"/> for more information.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>Whether this keybind was considered to be down</returns>
|
|
||||||
public bool WasDown(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
foreach (var combination in this.combinations) {
|
|
||||||
if (combination.WasDown(handler, gamepadIndex))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns whether this keybind is considered to be pressed.
|
/// Returns whether this keybind is considered to be pressed.
|
||||||
/// See <see cref="InputHandler.IsPressed"/> for more information.
|
/// See <see cref="InputHandler.IsPressed"/> for more information.
|
||||||
|
@ -198,54 +177,6 @@ namespace MLEM.Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether any of this keybind's modifier keys were down in the last update call.
|
|
||||||
/// See <see cref="InputHandler.WasDown"/> for more information.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>Whether any of this keyboard's modifier keys were down</returns>
|
|
||||||
public bool WasModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
foreach (var combination in this.combinations) {
|
|
||||||
if (combination.WasModifierDown(handler, gamepadIndex))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that this keybind has been held down for.
|
|
||||||
/// If this input isn't currently down, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting down time, or <see cref="TimeSpan.Zero"/> if the input is not being held.</returns>
|
|
||||||
public TimeSpan GetDownTime(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return this.combinations.Max(c => c.GetDownTime(handler, gamepadIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that this keybind has been up for since the last time it was down.
|
|
||||||
/// If this input isn't currently up, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting up time, or <see cref="TimeSpan.Zero"/> if the input is being held.</returns>
|
|
||||||
public TimeSpan GetUpTime(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return this.combinations.Min(c => c.GetUpTime(handler, gamepadIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that has passed since this keybind last counted as pressed.
|
|
||||||
/// If this input hasn't been pressed previously, or is currently pressed, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting up time, or <see cref="TimeSpan.Zero"/> if the input has never been pressed, or is currently pressed.</returns>
|
|
||||||
public TimeSpan GetTimeSincePress(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return this.combinations.Min(c => c.GetTimeSincePress(handler, gamepadIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns an enumerable of all of the combinations that this keybind currently contains
|
/// Returns an enumerable of all of the combinations that this keybind currently contains
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -350,7 +281,7 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns whether this combination is currently down.
|
/// Returns whether this combination is currently down
|
||||||
/// See <see cref="InputHandler.IsDown"/> for more information.
|
/// See <see cref="InputHandler.IsDown"/> for more information.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
/// <param name="handler">The input handler to query the keys with</param>
|
||||||
|
@ -360,17 +291,6 @@ namespace MLEM.Input {
|
||||||
return this.IsModifierDown(handler, gamepadIndex) && handler.IsDown(this.Key, gamepadIndex);
|
return this.IsModifierDown(handler, gamepadIndex) && handler.IsDown(this.Key, gamepadIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether this combination was down in the last upate call.
|
|
||||||
/// See <see cref="InputHandler.WasDown"/> for more information.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>Whether this combination was down</returns>
|
|
||||||
public bool WasDown(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return this.WasModifierDown(handler, gamepadIndex) && handler.WasDown(this.Key, gamepadIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns whether this combination is currently pressed.
|
/// Returns whether this combination is currently pressed.
|
||||||
/// See <see cref="InputHandler.IsPressed"/> for more information.
|
/// See <see cref="InputHandler.IsPressed"/> for more information.
|
||||||
|
@ -406,65 +326,20 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns whether all of this combination's modifier keys are currently down.
|
/// Returns whether this combination's modifier keys are currently down
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
/// <param name="handler">The input handler to query the keys with</param>
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
||||||
/// <returns>Whether this combination's modifiers are down</returns>
|
/// <returns>Whether this combination's modifiers are down</returns>
|
||||||
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
||||||
|
if (this.Modifiers.Length <= 0)
|
||||||
|
return true;
|
||||||
foreach (var modifier in this.Modifiers) {
|
foreach (var modifier in this.Modifiers) {
|
||||||
if (!handler.IsDown(modifier, gamepadIndex))
|
if (handler.IsDown(modifier, gamepadIndex))
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether all of this combination's modifier keys were down in the last update call.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>Whether this combination's modifiers were down</returns>
|
|
||||||
public bool WasModifierDown(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
foreach (var modifier in this.Modifiers) {
|
|
||||||
if (!handler.WasDown(modifier, gamepadIndex))
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that this combination has been held down for.
|
|
||||||
/// If this input isn't currently down, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting down time, or <see cref="TimeSpan.Zero"/> if the input is not being held.</returns>
|
|
||||||
public TimeSpan GetDownTime(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return handler.GetDownTime(this.Key, gamepadIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that this combination has been up for since the last time it was down.
|
|
||||||
/// If this input isn't currently up, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting up time, or <see cref="TimeSpan.Zero"/> if the input is being held.</returns>
|
|
||||||
public TimeSpan GetUpTime(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return handler.GetUpTime(this.Key, gamepadIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the amount of time that has passed since this combination last counted as pressed.
|
|
||||||
/// If this input hasn't been pressed previously, or is currently pressed, this method returns <see cref="TimeSpan.Zero"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler">The input handler to query the keys with</param>
|
|
||||||
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
|
|
||||||
/// <returns>The resulting up time, or <see cref="TimeSpan.Zero"/> if the input has never been pressed, or is currently pressed.</returns>
|
|
||||||
public TimeSpan GetTimeSincePress(InputHandler handler, int gamepadIndex = -1) {
|
|
||||||
return handler.GetTimeSincePress(this.Key, gamepadIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts this combination into an easily human-readable string.
|
/// Converts this combination into an easily human-readable string.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -12,7 +12,7 @@ namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All enum values of <see cref="ModifierKey"/>
|
/// All enum values of <see cref="ModifierKey"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly ModifierKey[] ModifierKeys = {ModifierKey.None, ModifierKey.Shift, ModifierKey.Control, ModifierKey.Alt};
|
public static readonly ModifierKey[] ModifierKeys = EnumHelper.GetValues<ModifierKey>().ToArray();
|
||||||
private static readonly Dictionary<ModifierKey, Keys[]> KeysLookup = new Dictionary<ModifierKey, Keys[]> {
|
private static readonly Dictionary<ModifierKey, Keys[]> KeysLookup = new Dictionary<ModifierKey, Keys[]> {
|
||||||
{ModifierKey.Shift, new[] {Keys.LeftShift, Keys.RightShift}},
|
{ModifierKey.Shift, new[] {Keys.LeftShift, Keys.RightShift}},
|
||||||
{ModifierKey.Control, new[] {Keys.LeftControl, Keys.RightControl}},
|
{ModifierKey.Control, new[] {Keys.LeftControl, Keys.RightControl}},
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -10,7 +12,7 @@ namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All enum values of <see cref="MouseButton"/>
|
/// All enum values of <see cref="MouseButton"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly MouseButton[] MouseButtons = {MouseButton.Left, MouseButton.Middle, MouseButton.Right, MouseButton.Extra1, MouseButton.Extra2};
|
public static readonly MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the <see cref="ButtonState"/> of the given mouse button.
|
/// Returns the <see cref="ButtonState"/> of the given mouse button.
|
||||||
|
|
|
@ -274,8 +274,8 @@ namespace MLEM.Input {
|
||||||
public void Update(GameTime time, InputHandler input) {
|
public void Update(GameTime time, InputHandler input) {
|
||||||
this.UpdateTextDataIfDirty();
|
this.UpdateTextDataIfDirty();
|
||||||
|
|
||||||
#if FNA
|
|
||||||
// FNA's text input event doesn't supply keys, so we handle this here
|
// FNA's text input event doesn't supply keys, so we handle this here
|
||||||
|
#if FNA
|
||||||
if (this.CaretPos > 0 && input.TryConsumePressed(Keys.Back)) {
|
if (this.CaretPos > 0 && input.TryConsumePressed(Keys.Back)) {
|
||||||
this.CaretPos--;
|
this.CaretPos--;
|
||||||
this.RemoveText(this.CaretPos, 1);
|
this.RemoveText(this.CaretPos, 1);
|
||||||
|
@ -289,16 +289,16 @@ namespace MLEM.Input {
|
||||||
this.CaretPos--;
|
this.CaretPos--;
|
||||||
} else if (this.CaretPos < this.text.Length && input.TryConsumePressed(Keys.Right)) {
|
} else if (this.CaretPos < this.text.Length && input.TryConsumePressed(Keys.Right)) {
|
||||||
this.CaretPos++;
|
this.CaretPos++;
|
||||||
} else if (this.Multiline && input.IsPressedAvailable(Keys.Up) && this.MoveCaretToLine(this.CaretLine - 1)) {
|
} else if (this.Multiline && input.IsKeyPressedAvailable(Keys.Up) && this.MoveCaretToLine(this.CaretLine - 1)) {
|
||||||
input.TryConsumePressed(Keys.Up);
|
input.TryConsumePressed(Keys.Up);
|
||||||
} else if (this.Multiline && input.IsPressedAvailable(Keys.Down) && this.MoveCaretToLine(this.CaretLine + 1)) {
|
} else if (this.Multiline && input.IsKeyPressedAvailable(Keys.Down) && this.MoveCaretToLine(this.CaretLine + 1)) {
|
||||||
input.TryConsumePressed(Keys.Down);
|
input.TryConsumePressed(Keys.Down);
|
||||||
} else if (this.CaretPos != 0 && input.TryConsumePressed(Keys.Home)) {
|
} else if (this.CaretPos != 0 && input.TryConsumePressed(Keys.Home)) {
|
||||||
this.CaretPos = 0;
|
this.CaretPos = 0;
|
||||||
} else if (this.CaretPos != this.text.Length && input.TryConsumePressed(Keys.End)) {
|
} else if (this.CaretPos != this.text.Length && input.TryConsumePressed(Keys.End)) {
|
||||||
this.CaretPos = this.text.Length;
|
this.CaretPos = this.text.Length;
|
||||||
} else if (input.IsModifierKeyDown(ModifierKey.Control)) {
|
} else if (input.IsModifierKeyDown(ModifierKey.Control)) {
|
||||||
if (input.IsPressedAvailable(Keys.V)) {
|
if (input.IsKeyPressedAvailable(Keys.V)) {
|
||||||
var clip = this.PasteFromClipboardFunction?.Invoke();
|
var clip = this.PasteFromClipboardFunction?.Invoke();
|
||||||
if (clip != null) {
|
if (clip != null) {
|
||||||
this.InsertText(clip, true);
|
this.InsertText(clip, true);
|
||||||
|
@ -417,10 +417,9 @@ namespace MLEM.Input {
|
||||||
private bool FilterText(ref string text, bool removeMismatching) {
|
private bool FilterText(ref string text, bool removeMismatching) {
|
||||||
if (removeMismatching) {
|
if (removeMismatching) {
|
||||||
var result = new StringBuilder();
|
var result = new StringBuilder();
|
||||||
foreach (var codePoint in new CodePointSource(text)) {
|
foreach (var c in text) {
|
||||||
var character = char.ConvertFromUtf32(codePoint);
|
if (this.InputRule(this, c.ToCachedString()))
|
||||||
if (this.InputRule(this, character))
|
result.Append(c);
|
||||||
result.Append(character);
|
|
||||||
}
|
}
|
||||||
text = result.ToString();
|
text = result.ToString();
|
||||||
} else if (!this.InputRule(this, text))
|
} else if (!this.InputRule(this, text))
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
<RootNamespace>MLEM</RootNamespace>
|
<RootNamespace>MLEM</RootNamespace>
|
||||||
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
<DefineConstants>$(DefineConstants);FNA</DefineConstants>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -21,11 +20,9 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FNA\FNA.csproj">
|
<ProjectReference Include="..\FNA\FNA.Core.csproj">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
|
|
||||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="'$(TargetFramework)'=='net452'" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net452;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||||
<IsTrimmable>true</IsTrimmable>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -22,8 +21,6 @@
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
|
||||||
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="'$(TargetFramework)'=='net452'" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -71,10 +71,7 @@ namespace MLEM.Misc {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All <see cref="Direction2"/> enum values
|
/// All <see cref="Direction2"/> enum values
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly Direction2[] All = {
|
public static readonly Direction2[] All = EnumHelper.GetValues<Direction2>().ToArray();
|
||||||
Direction2.None, Direction2.Up, Direction2.Right, Direction2.Down, Direction2.Left,
|
|
||||||
Direction2.UpRight, Direction2.DownRight, Direction2.UpLeft, Direction2.DownLeft
|
|
||||||
};
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The <see cref="Direction2.Up"/> through <see cref="Direction2.Left"/> directions
|
/// The <see cref="Direction2.Up"/> through <see cref="Direction2.Left"/> directions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -1,71 +1,30 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
namespace MLEM.Misc {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A helper class that allows easier usage of <see cref="Enum"/> values.
|
/// A helper class that allows easier usage of <see cref="Enum"/> values.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("EnumHelper has been moved into the DynamicEnums library: https://www.nuget.org/packages/DynamicEnums")]
|
|
||||||
public static class EnumHelper {
|
public static class EnumHelper {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All values of the <see cref="Buttons"/> enum.
|
/// All values of the <see cref="Buttons"/> enum.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("This field has been moved to InputHandler.AllButtons")]
|
public static readonly Buttons[] Buttons = EnumHelper.GetValues<Buttons>().ToArray();
|
||||||
public static readonly Buttons[] Buttons = EnumHelper.GetValues<Buttons>();
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All values of the <see cref="Keys"/> enum.
|
/// All values of the <see cref="Keys"/> enum.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Obsolete("This field has been moved to InputHandler.AllKeys")]
|
public static readonly Keys[] Keys = EnumHelper.GetValues<Keys>().ToArray();
|
||||||
public static readonly Keys[] Keys = EnumHelper.GetValues<Keys>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns an array containing all of the values of the given enum type.
|
/// Returns all of the values of the given enum type.
|
||||||
/// Note that this method is a version-independent equivalent of .NET 5's <c>Enum.GetValues<TEnum></c>.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type whose enum to get</typeparam>
|
/// <typeparam name="T">The type whose enum to get</typeparam>
|
||||||
/// <returns>An enumerable of the values of the enum, in declaration order.</returns>
|
/// <returns>An enumerable of the values of the enum, in declaration order.</returns>
|
||||||
public static T[] GetValues<T>() where T : struct, Enum {
|
public static IEnumerable<T> GetValues<T>() {
|
||||||
#if NET6_0_OR_GREATER
|
return Enum.GetValues(typeof(T)).Cast<T>();
|
||||||
return Enum.GetValues<T>();
|
|
||||||
#else
|
|
||||||
return (T[]) Enum.GetValues(typeof(T));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns all of the defined values from the given enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
|
|
||||||
/// Note that, if combined flags are defined in <typeparamref name="T"/>, and <paramref name="combinedFlag"/> contains them, they will also be returned.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
|
|
||||||
/// <param name="includeZero">Whether the enum value 0 should also be returned, if <typeparamref name="T"/> contains one.</param>
|
|
||||||
/// <typeparam name="T">The type of enum.</typeparam>
|
|
||||||
/// <returns>All of the flags that make up <paramref name="combinedFlag"/>.</returns>
|
|
||||||
public static IEnumerable<T> GetFlags<T>(T combinedFlag, bool includeZero = true) where T : struct, Enum {
|
|
||||||
foreach (var flag in EnumHelper.GetValues<T>()) {
|
|
||||||
if (combinedFlag.HasFlag(flag) && (includeZero || Convert.ToInt64(flag) != 0))
|
|
||||||
yield return flag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns all of the defined unique flags from the given enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
|
|
||||||
/// Any combined flags (flags that aren't powers of two) which are defined in <typeparamref name="T"/> will not be returned.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
|
|
||||||
/// <typeparam name="T">The type of enum.</typeparam>
|
|
||||||
/// <returns>All of the unique flags that make up <paramref name="combinedFlag"/>.</returns>
|
|
||||||
public static IEnumerable<T> GetUniqueFlags<T>(T combinedFlag) where T : struct, Enum {
|
|
||||||
var uniqueFlag = 1;
|
|
||||||
foreach (var flag in EnumHelper.GetValues<T>()) {
|
|
||||||
var flagValue = Convert.ToInt64(flag);
|
|
||||||
// GetValues is always ordered by binary value, so we can be sure that the next flag is bigger than the last
|
|
||||||
while (uniqueFlag < flagValue)
|
|
||||||
uniqueFlag <<= 1;
|
|
||||||
if (flagValue == uniqueFlag && combinedFlag.HasFlag(flag))
|
|
||||||
yield return flag;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
namespace MLEM.Misc {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -7,20 +8,15 @@ namespace MLEM.Misc {
|
||||||
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
|
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
|
||||||
/// This <see cref="IGenericDataHolder"/> implemention uses an underlying <see cref="Dictionary{String,Object}"/> that only keeps track of non-default values.
|
/// This <see cref="IGenericDataHolder"/> implemention uses an underlying <see cref="Dictionary{String,Object}"/> that only keeps track of non-default values.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[DataContract]
|
||||||
public class GenericDataHolder : IGenericDataHolder {
|
public class GenericDataHolder : IGenericDataHolder {
|
||||||
|
|
||||||
private static readonly string[] EmptyStrings = new string[0];
|
[DataMember(EmitDefaultValue = false)]
|
||||||
private Dictionary<string, object> data;
|
private Dictionary<string, object> data;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
|
|
||||||
public void SetData(string key, object data) {
|
public void SetData(string key, object data) {
|
||||||
this.SetData<object>(key, data);
|
if (data == default) {
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void SetData<T>(string key, T data) {
|
|
||||||
if (EqualityComparer<T>.Default.Equals(data, default)) {
|
|
||||||
if (this.data != null)
|
if (this.data != null)
|
||||||
this.data.Remove(key);
|
this.data.Remove(key);
|
||||||
} else {
|
} else {
|
||||||
|
@ -38,9 +34,9 @@ namespace MLEM.Misc {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IEnumerable<string> GetDataKeys() {
|
public IReadOnlyCollection<string> GetDataKeys() {
|
||||||
if (this.data == null)
|
if (this.data == null)
|
||||||
return GenericDataHolder.EmptyStrings;
|
return Array.Empty<string>();
|
||||||
return this.data.Keys;
|
return this.data.Keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,16 +53,8 @@ namespace MLEM.Misc {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key to store the data by</param>
|
/// <param name="key">The key to store the data by</param>
|
||||||
/// <param name="data">The data to store in the object</param>
|
/// <param name="data">The data to store in the object</param>
|
||||||
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
|
|
||||||
void SetData(string key, object data);
|
void SetData(string key, object data);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Store a piece of generic data on this object.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The key to store the data by</param>
|
|
||||||
/// <param name="data">The data to store in the object</param>
|
|
||||||
void SetData<T>(string key, T data);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a piece of generic data of the given type on this object.
|
/// Returns a piece of generic data of the given type on this object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -79,7 +67,7 @@ namespace MLEM.Misc {
|
||||||
/// Returns all of the generic data that this object stores.
|
/// Returns all of the generic data that this object stores.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The generic data on this object</returns>
|
/// <returns>The generic data on this object</returns>
|
||||||
IEnumerable<string> GetDataKeys();
|
IReadOnlyCollection<string> GetDataKeys();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,13 +95,6 @@ namespace MLEM.Misc {
|
||||||
return new Padding(p.Left * scale, p.Right * scale, p.Top * scale, p.Bottom * scale);
|
return new Padding(p.Left * scale, p.Right * scale, p.Top * scale, p.Bottom * scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Scales a padding by a <see cref="Vector2"/>.
|
|
||||||
/// </summary>
|
|
||||||
public static Padding operator *(Padding p, Vector2 scale) {
|
|
||||||
return new Padding(p.Left * scale.X, p.Right * scale.X, p.Top * scale.Y, p.Bottom * scale.Y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds two paddings together in a memberwise fashion.
|
/// Adds two paddings together in a memberwise fashion.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -1,110 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
|
||||||
/// <summary>
|
|
||||||
/// A seed source contains an <see cref="int"/> value which can be used as a seed for a <see cref="System.Random"/> or <see cref="SingleRandom"/>. Seed sources feature a convenient way to add multiple seeds using <see cref="Add(int)"/>, which will be sufficiently scrambled to be deterministically pseudorandom and combined into a single <see cref="int"/>.
|
|
||||||
/// This struct behaves similarly to <c>System.HashCode</c> in many ways, with an important distinction being that <see cref="SeedSource"/>'s scrambling procedure is not considered an implementation detail, and will stay consistent between process executions.
|
|
||||||
/// </summary>
|
|
||||||
/// <example>
|
|
||||||
/// For example, a seed source can be used to create a new <see cref="System.Random"/> based on an object's <c>x</c> and <c>y</c> coordinates by combining them into a <see cref="SeedSource"/> using <see cref="Add(int)"/>. The values generated by the <see cref="System.Random"/> created using <see cref="Random()"/> will then be determined by the specific pair of <c>x</c> and <c>y</c> values used.
|
|
||||||
/// </example>
|
|
||||||
public readonly struct SeedSource {
|
|
||||||
|
|
||||||
private readonly int? value;
|
|
||||||
|
|
||||||
private SeedSource(int? value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given seed to this seed source's value and returns the result as a new seed source.
|
|
||||||
/// The algorithm used for adding involves various scrambling operations that sufficiently pseudo-randomize the seed and final value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to add.</param>
|
|
||||||
/// <returns>A new seed source with the seed added.</returns>
|
|
||||||
public SeedSource Add(int seed) {
|
|
||||||
return new SeedSource(SeedSource.Scramble(this.Get()) + SeedSource.Scramble(seed));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given seed to this seed source's value and returns the result as a new seed source.
|
|
||||||
/// Floating point values are scrambled by invoking <see cref="Add(int)"/> using a typecast version, followed by invoking <see cref="Add(int)"/> using the decimal value multiplied by <see cref="int.MaxValue"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to add.</param>
|
|
||||||
/// <returns>A new seed source with the seed added.</returns>
|
|
||||||
public SeedSource Add(float seed) {
|
|
||||||
return this.Add((int) seed).Add((seed - (int) seed) * int.MaxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given seed to this seed source's value and returns the result as a new seed source.
|
|
||||||
/// Strings are scrambled by invoking <see cref="Add(int)"/> using every character contained in the string, in order.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to add.</param>
|
|
||||||
/// <returns>A new seed source with the seed added.</returns>
|
|
||||||
public SeedSource Add(string seed) {
|
|
||||||
var ret = this;
|
|
||||||
foreach (var c in seed)
|
|
||||||
ret = ret.Add(c);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given seed to this seed source's value and returns the result as a new seed source.
|
|
||||||
/// Guids are scrambled by invoking <see cref="Add(int)"/> using every byte in the <see cref="Guid"/>'s byte array.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to add.</param>
|
|
||||||
/// <returns>A new seed source with the seed added.</returns>
|
|
||||||
public SeedSource Add(Guid seed) {
|
|
||||||
var ret = this;
|
|
||||||
foreach (var b in seed.ToByteArray())
|
|
||||||
ret = ret.Add(b);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given seed to this seed source's value and returns the result as a new seed source.
|
|
||||||
/// Any objects that don't have a specially defined <see cref="Add(int)"/> overload get scrambled using <see cref="object.GetHashCode"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to add.</param>
|
|
||||||
/// <returns>A new seed source with the seed added.</returns>
|
|
||||||
public SeedSource Add(object seed) {
|
|
||||||
return this.Add(seed?.GetHashCode() ?? 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a new seed source whose value is this seed source's value, but scrambled further.
|
|
||||||
/// In essence, this creates a new seed source whose value is determined by the current seed source.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A new seed source with a rotated value.</returns>
|
|
||||||
public SeedSource Rotate() {
|
|
||||||
return new SeedSource(SeedSource.Scramble(this.Get()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns this seed source's seed value, which can then be used in <see cref="SingleRandom"/> or elsewhere.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>This seed source's value.</returns>
|
|
||||||
public int Get() {
|
|
||||||
return this.value ?? 1623487;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a new <see cref="Random"/> instance using this source seed's value, retrieved using <see cref="Get"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A new <see cref="Random"/> using this seed source's value.</returns>
|
|
||||||
public Random Random() {
|
|
||||||
return new Random(this.Get());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int Scramble(int x) {
|
|
||||||
x += 84317;
|
|
||||||
x ^= x << 7;
|
|
||||||
x *= 207398809;
|
|
||||||
x ^= x << 17;
|
|
||||||
x *= 928511849;
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,142 +0,0 @@
|
||||||
namespace MLEM.Misc {
|
|
||||||
/// <summary>
|
|
||||||
/// The SingleRandom class allows generating single, one-off pseudorandom numbers based on a seed or a <see cref="SeedSource"/>.
|
|
||||||
/// The types of numbers that can be generated are <see cref="int"/> and <see cref="float"/>, both of which can be generated with specific minimum and maximum values if desired.
|
|
||||||
/// Methods in this class are tested to be sufficiently "random", that is, to be sufficiently distributed throughout their range, as well as sufficiently different for neighboring seeds.
|
|
||||||
/// </summary>
|
|
||||||
public class SingleRandom {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between 0 and <see cref="int.MaxValue"/> based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(int seed) {
|
|
||||||
return (int) (SingleRandom.Single(seed) * int.MaxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between 0 and <see cref="int.MaxValue"/> based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(SeedSource source) {
|
|
||||||
return (int) (SingleRandom.Single(source) * int.MaxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between 0 and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(int maxValue, int seed) {
|
|
||||||
return (int) (maxValue * SingleRandom.Single(seed));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between 0 and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(int maxValue, SeedSource source) {
|
|
||||||
return (int) (maxValue * SingleRandom.Single(source));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="minValue">The (inclusive) minimum value.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(int minValue, int maxValue, int seed) {
|
|
||||||
return (int) ((maxValue - minValue) * SingleRandom.Single(seed)) + minValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom integer between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="minValue">The (inclusive) minimum value.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static int Int(int minValue, int maxValue, SeedSource source) {
|
|
||||||
return (int) ((maxValue - minValue) * SingleRandom.Single(source)) + minValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(int seed) {
|
|
||||||
return (new SeedSource().Add(seed).Get() / (float) int.MaxValue + 1) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(SeedSource source) {
|
|
||||||
return (source.Get() / (float) int.MaxValue + 1) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between 0 and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(float maxValue, int seed) {
|
|
||||||
return maxValue * SingleRandom.Single(seed);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between 0 and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(float maxValue, SeedSource source) {
|
|
||||||
return maxValue * SingleRandom.Single(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="minValue">The (inclusive) minimum value.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="seed">The seed to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(float minValue, float maxValue, int seed) {
|
|
||||||
return (maxValue - minValue) * SingleRandom.Single(seed) + minValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a single, one-off pseudorandom floating point number between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
|
|
||||||
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="minValue">The (inclusive) minimum value.</param>
|
|
||||||
/// <param name="maxValue">The (exclusive) maximum value.</param>
|
|
||||||
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
|
|
||||||
/// <returns>The generated number.</returns>
|
|
||||||
public static float Single(float minValue, float maxValue, SeedSource source) {
|
|
||||||
return (maxValue - minValue) * SingleRandom.Single(source) + minValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MLEM.Pathfinding {
|
namespace MLEM.Pathfinding {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -10,6 +11,21 @@ namespace MLEM.Pathfinding {
|
||||||
/// <typeparam name="T">The type of points used for this path</typeparam>
|
/// <typeparam name="T">The type of points used for this path</typeparam>
|
||||||
public abstract class AStar<T> {
|
public abstract class AStar<T> {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A value that represents an infinite path cost, or a cost for a location that cannot possibly be reached.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("This field is deprecated. Use float.PositiveInfinity or float.MaxValue instead.")]
|
||||||
|
public const float InfiniteCost = float.PositiveInfinity;
|
||||||
|
/// <summary>
|
||||||
|
/// The array of all directions that will be checked for path finding.
|
||||||
|
/// Note that this array is only used if <see cref="DefaultAllowDiagonals"/> is true.
|
||||||
|
/// </summary>
|
||||||
|
public readonly T[] AllDirections;
|
||||||
|
/// <summary>
|
||||||
|
/// The array of all adjacent directions that will be checked for path finding.
|
||||||
|
/// Note that this array is only used if <see cref="DefaultAllowDiagonals"/> is false.
|
||||||
|
/// </summary>
|
||||||
|
public readonly T[] AdjacentDirections;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default cost function that determines the cost for each path finding position.
|
/// The default cost function that determines the cost for each path finding position.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -23,10 +39,9 @@ namespace MLEM.Pathfinding {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int DefaultMaxTries;
|
public int DefaultMaxTries;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default <see cref="CollectAdditionalNeighbors"/> function.
|
/// Whether or not diagonal directions are considered while finding a path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CollectAdditionalNeighbors DefaultAdditionalNeighbors;
|
public bool DefaultAllowDiagonals;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The amount of tries required for finding the last queried path
|
/// The amount of tries required for finding the last queried path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -39,15 +54,24 @@ namespace MLEM.Pathfinding {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new A* pathfinder with the supplied default settings.
|
/// Creates a new A* pathfinder with the supplied default settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="allDirections">All directions that should be checked</param>
|
||||||
|
/// <param name="adjacentDirections">All adjacent directions that should be checked</param>
|
||||||
/// <param name="defaultCostFunction">The default function for cost determination of a path point</param>
|
/// <param name="defaultCostFunction">The default function for cost determination of a path point</param>
|
||||||
|
/// <param name="defaultAllowDiagonals">Whether or not diagonals should be allowed by default</param>
|
||||||
/// <param name="defaultCost">The default cost for a path point</param>
|
/// <param name="defaultCost">The default cost for a path point</param>
|
||||||
/// <param name="defaultMaxTries">The default amount of tries before path finding is aborted</param>
|
/// <param name="defaultMaxTries">The default amount of tries before path finding is aborted</param>
|
||||||
/// <param name="defaultAdditionalNeighbors">The default <see cref="CollectAdditionalNeighbors"/> function.</param>
|
protected AStar(T[] allDirections, T[] adjacentDirections, GetCost defaultCostFunction, bool defaultAllowDiagonals, float defaultCost = 1, int defaultMaxTries = 10000) {
|
||||||
protected AStar(GetCost defaultCostFunction, float defaultCost, int defaultMaxTries, CollectAdditionalNeighbors defaultAdditionalNeighbors) {
|
this.AllDirections = allDirections;
|
||||||
|
this.AdjacentDirections = adjacentDirections;
|
||||||
this.DefaultCostFunction = defaultCostFunction;
|
this.DefaultCostFunction = defaultCostFunction;
|
||||||
this.DefaultCost = defaultCost;
|
this.DefaultCost = defaultCost;
|
||||||
this.DefaultMaxTries = defaultMaxTries;
|
this.DefaultMaxTries = defaultMaxTries;
|
||||||
this.DefaultAdditionalNeighbors = defaultAdditionalNeighbors;
|
this.DefaultAllowDiagonals = defaultAllowDiagonals;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="FindPath"/>
|
||||||
|
public Task<Stack<T>> FindPathAsync(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, bool? allowDiagonals = null) {
|
||||||
|
return Task.Run(() => this.FindPath(start, goal, costFunction, defaultCost, maxTries, allowDiagonals));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -58,41 +82,22 @@ namespace MLEM.Pathfinding {
|
||||||
/// <param name="costFunction">The function that determines the cost for each path point</param>
|
/// <param name="costFunction">The function that determines the cost for each path point</param>
|
||||||
/// <param name="defaultCost">The default cost for each path point</param>
|
/// <param name="defaultCost">The default cost for each path point</param>
|
||||||
/// <param name="maxTries">The maximum amount of tries before path finding is aborted</param>
|
/// <param name="maxTries">The maximum amount of tries before path finding is aborted</param>
|
||||||
/// <param name="additionalNeighbors">A function that determines a set of additional neighbors to be considered for a given point.</param>
|
/// <param name="allowDiagonals">If diagonals should be looked at for path finding</param>
|
||||||
/// <returns>A stack of path points, where the top item is the first point to go to, or null if no path was found.</returns>
|
/// <returns>A stack of path points, where the top item is the first point to go to, or null if no path was found.</returns>
|
||||||
public Stack<T> FindPath(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, CollectAdditionalNeighbors additionalNeighbors = null) {
|
public Stack<T> FindPath(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, bool? allowDiagonals = null) {
|
||||||
this.TryFindPath(start, new[] {goal}, out var path, out _, costFunction, defaultCost, maxTries, additionalNeighbors);
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to find a path between two points using this pathfinder's default settings or, alternatively, the supplied override settings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="start">The point to start path finding at</param>
|
|
||||||
/// <param name="goals">The points to find a path to, one of which will be chosen as the closest or best destination</param>
|
|
||||||
/// <param name="path">The path that was found, or <see langword="null"/> if no path was found.</param>
|
|
||||||
/// <param name="totalCost">The total cost that was calculated for the path, or <see cref="float.PositiveInfinity"/> if no path was found.</param>
|
|
||||||
/// <param name="costFunction">The function that determines the cost for each path point</param>
|
|
||||||
/// <param name="defaultCost">The default cost for each path point</param>
|
|
||||||
/// <param name="maxTries">The maximum amount of tries before path finding is aborted</param>
|
|
||||||
/// <param name="additionalNeighbors">A function that determines a set of additional neighbors to be considered for a given point.</param>
|
|
||||||
/// <returns>Whether a path was found.</returns>
|
|
||||||
public bool TryFindPath(T start, ICollection<T> goals, out Stack<T> path, out float totalCost, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, CollectAdditionalNeighbors additionalNeighbors = null) {
|
|
||||||
path = null;
|
|
||||||
totalCost = float.PositiveInfinity;
|
|
||||||
|
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
var getCost = costFunction ?? this.DefaultCostFunction;
|
var getCost = costFunction ?? this.DefaultCostFunction;
|
||||||
|
var diags = allowDiagonals ?? this.DefaultAllowDiagonals;
|
||||||
var tries = maxTries ?? this.DefaultMaxTries;
|
var tries = maxTries ?? this.DefaultMaxTries;
|
||||||
var defCost = defaultCost ?? this.DefaultCost;
|
var defCost = defaultCost ?? this.DefaultCost;
|
||||||
var additional = additionalNeighbors ?? this.DefaultAdditionalNeighbors;
|
|
||||||
|
|
||||||
var neighbors = new HashSet<T>();
|
|
||||||
var open = new Dictionary<T, PathPoint<T>>();
|
var open = new Dictionary<T, PathPoint<T>>();
|
||||||
var closed = new Dictionary<T, PathPoint<T>>();
|
var closed = new Dictionary<T, PathPoint<T>>();
|
||||||
open.Add(start, new PathPoint<T>(start, this.GetMinHeuristicDistance(start, goals), null, 0, defCost));
|
open.Add(start, new PathPoint<T>(start, this.GetManhattanDistance(start, goal), null, 0, defCost));
|
||||||
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
Stack<T> ret = null;
|
||||||
while (open.Count > 0) {
|
while (open.Count > 0) {
|
||||||
PathPoint<T> current = null;
|
PathPoint<T> current = null;
|
||||||
foreach (var point in open.Values) {
|
foreach (var point in open.Values) {
|
||||||
|
@ -105,20 +110,17 @@ namespace MLEM.Pathfinding {
|
||||||
open.Remove(current.Pos);
|
open.Remove(current.Pos);
|
||||||
closed.Add(current.Pos, current);
|
closed.Add(current.Pos, current);
|
||||||
|
|
||||||
if (goals.Contains(current.Pos)) {
|
if (current.Pos.Equals(goal)) {
|
||||||
path = AStar<T>.CompilePath(current);
|
ret = AStar<T>.CompilePath(current);
|
||||||
totalCost = current.F;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
neighbors.Clear();
|
var dirsUsed = diags ? this.AllDirections : this.AdjacentDirections;
|
||||||
this.CollectNeighbors(current.Pos, neighbors);
|
foreach (var dir in dirsUsed) {
|
||||||
additional?.Invoke(current.Pos, neighbors);
|
var neighborPos = this.AddPositions(current.Pos, dir);
|
||||||
|
|
||||||
foreach (var neighborPos in neighbors) {
|
|
||||||
var cost = getCost(current.Pos, neighborPos);
|
var cost = getCost(current.Pos, neighborPos);
|
||||||
if (!float.IsPositiveInfinity(cost) && cost < float.MaxValue && !closed.ContainsKey(neighborPos)) {
|
if (!float.IsPositiveInfinity(cost) && cost < float.MaxValue && !closed.ContainsKey(neighborPos)) {
|
||||||
var neighbor = new PathPoint<T>(neighborPos, this.GetMinHeuristicDistance(neighborPos, goals), current, cost, defCost);
|
var neighbor = new PathPoint<T>(neighborPos, this.GetManhattanDistance(neighborPos, goal), current, cost, defCost);
|
||||||
// check if we already have a waypoint at this location with a worse path
|
// check if we already have a waypoint at this location with a worse path
|
||||||
if (open.TryGetValue(neighborPos, out var alreadyNeighbor)) {
|
if (open.TryGetValue(neighborPos, out var alreadyNeighbor)) {
|
||||||
if (neighbor.G < alreadyNeighbor.G) {
|
if (neighbor.G < alreadyNeighbor.G) {
|
||||||
|
@ -141,31 +143,18 @@ namespace MLEM.Pathfinding {
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
this.LastTriesNeeded = count;
|
this.LastTriesNeeded = count;
|
||||||
this.LastTimeNeeded = stopwatch.Elapsed;
|
this.LastTimeNeeded = stopwatch.Elapsed;
|
||||||
return path != null;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method should implement a heuristic that determines the total distance between the given <paramref name="start"/> position and the given second position <paramref name="position"/>.
|
/// A helper method to add two positions together.
|
||||||
/// Note that this is multiplied with the <see cref="DefaultCost"/> automatically, so no costs need to be considered in this method's return value.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="start">The start position.</param>
|
protected abstract T AddPositions(T first, T second);
|
||||||
/// <param name="position">The position to get the distance to.</param>
|
|
||||||
/// <returns>The total distance between the two positions.</returns>
|
|
||||||
protected abstract float GetHeuristicDistance(T start, T position);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method should populate a set of positions that are considered <paramref name="neighbors"/> to the given <paramref name="position"/>. For example, this method might return directly adjacent positions, diagonal positions, or faraway positions that can be teleported to.
|
/// A helper method to get the Manhattan Distance between two points.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="position">The position whose neighbors to return.</param>
|
protected abstract float GetManhattanDistance(T first, T second);
|
||||||
/// <param name="neighbors">The set to populate with neighbors.</param>
|
|
||||||
protected abstract void CollectNeighbors(T position, ISet<T> neighbors);
|
|
||||||
|
|
||||||
private float GetMinHeuristicDistance(T start, IEnumerable<T> positions) {
|
|
||||||
var min = float.MaxValue;
|
|
||||||
foreach (var position in positions)
|
|
||||||
min = Math.Min(min, this.GetHeuristicDistance(start, position));
|
|
||||||
return min;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Stack<T> CompilePath(PathPoint<T> current) {
|
private static Stack<T> CompilePath(PathPoint<T> current) {
|
||||||
var path = new Stack<T>();
|
var path = new Stack<T>();
|
||||||
|
@ -177,21 +166,14 @@ namespace MLEM.Pathfinding {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A cost function for a given pair of neighboring path finding positions.
|
/// A cost function for a given path finding position.
|
||||||
/// If a path point should have the default cost, <see cref="AStar{T}.DefaultCost"/> should be returned.
|
/// If a path point should have the default cost, <see cref="AStar{T}.DefaultCost"/> should be returned.
|
||||||
/// If a path point should be unreachable, <see cref="float.PositiveInfinity"/> or <see cref="float.MaxValue"/> should be returned.
|
/// If a path point should be unreachable, <see cref="float.PositiveInfinity"/> or <see cref="float.MaxValue"/> should be returned.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="currPos">The current position in the path.</param>
|
/// <param name="currPos">The current position in the path</param>
|
||||||
/// <param name="nextPos">The neighboring position whose cost to check.</param>
|
/// <param name="nextPos">The position we're trying to reach from the current position</param>
|
||||||
public delegate float GetCost(T currPos, T nextPos);
|
public delegate float GetCost(T currPos, T nextPos);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A delegate that determines a set of additional <paramref name="neighbors"/> to be considered for a given <paramref name="position"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="position">The position whose neighbors to return.</param>
|
|
||||||
/// <param name="neighbors">The set to populate with neighbors.</param>
|
|
||||||
public delegate void CollectAdditionalNeighbors(T position, ISet<T> neighbors);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -221,7 +203,7 @@ namespace MLEM.Pathfinding {
|
||||||
/// Creates a new path point with the supplied settings.
|
/// Creates a new path point with the supplied settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pos">The point's position</param>
|
/// <param name="pos">The point's position</param>
|
||||||
/// <param name="distance">The point's distance from the start point</param>
|
/// <param name="distance">The point's manhattan distance from the start point</param>
|
||||||
/// <param name="parent">The point's parent</param>
|
/// <param name="parent">The point's parent</param>
|
||||||
/// <param name="terrainCostForThisPos">The point's terrain cost, based on <see cref="AStar{T}.GetCost"/></param>
|
/// <param name="terrainCostForThisPos">The point's terrain cost, based on <see cref="AStar{T}.GetCost"/></param>
|
||||||
/// <param name="defaultCost">The default cost for a path point</param>
|
/// <param name="defaultCost">The default cost for a path point</param>
|
||||||
|
@ -250,7 +232,7 @@ namespace MLEM.Pathfinding {
|
||||||
/// <summary>Returns the hash code for this instance.</summary>
|
/// <summary>Returns the hash code for this instance.</summary>
|
||||||
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
|
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
|
||||||
public override int GetHashCode() {
|
public override int GetHashCode() {
|
||||||
return EqualityComparer<T>.Default.GetHashCode(this.Pos);
|
return this.Pos.GetHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue