Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly enhancing base functionality + implementing press targeting. #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions MonoSprites/Content/Content.mgcb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#----------------------------- Global Properties ----------------------------#

/outputDir:bin/$(Platform)
/intermediateDir:obj/$(Platform)
/outputDir:bin
/intermediateDir:obj
/platform:DesktopGL
/config:
/profile:Reach
Expand Down Expand Up @@ -136,6 +136,7 @@
#begin font.spritefont
/importer:FontDescriptionImporter
/processor:FontDescriptionProcessor
/processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:font.spritefont

2 changes: 1 addition & 1 deletion MonoSprites/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Game1 : Game
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Content.RootDirectory = "Content/bin";
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions MonoSprites/MonoGame.Framework.dll.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<dllmap dll="SDL2.dll" os="osx" target="libSDL2-2.0.0.dylib"/>
<dllmap dll="soft_oal.dll" os="osx" target="libopenal.1.dylib" />
<dllmap dll="SDL2.dll" os="linux" cpu="x86" target="./x86/libSDL2-2.0.so.0"/>
<dllmap dll="soft_oal.dll" os="linux" cpu="x86" target="./x86/libopenal.so.1" />
<dllmap dll="SDL2.dll" os="linux" cpu="x86-64" target="./x64/libSDL2-2.0.so.0"/>
<dllmap dll="soft_oal.dll" os="linux" cpu="x86-64" target="./x64/libopenal.so.1" />
</configuration>
75 changes: 59 additions & 16 deletions MonoSprites/MonoSprites.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{97C17E57-5739-4438-98C4-B39C05EDA1ED}</ProjectGuid>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MonoSprites</RootNamespace>
<AssemblyName>MonoSprites</AssemblyName>
Expand Down Expand Up @@ -47,13 +47,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;LINUX</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile>bin\DesktopGL\x86\Debug\MonoSprites.XML</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
Expand All @@ -71,24 +72,16 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Container.cs" />
<Compile Include="Source\Pressable\Container.cs" />
<Compile Include="Source\Pressable\Sprite.cs" />
<Compile Include="Source\Renderable.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="OpenTK">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\OpenTK.dll</HintPath>
</Reference>
<Reference Include="NVorbis">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\NVorbis.dll</HintPath>
</Reference>
<Reference Include="MonoGame.Framework">
<HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\DesktopGL\MonoGame.Framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Source\Sprite.cs" />
<Compile Include="Source\Transformations.cs" />
<EmbeddedResource Include="SDL2.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Icon.ico" />
<Content Include="OpenTK.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand All @@ -97,8 +90,58 @@
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGame.Extended, Version=1.0.617.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Extended.1.0.617\lib\portable-net45+win8+wpa81\MonoGame.Extended.dll</HintPath>
</Reference>
<Reference Include="MonoGame.Extended.Input, Version=1.0.617.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Extended.Input.1.0.617\lib\portable-net45+win8+wpa81\MonoGame.Extended.Input.dll</HintPath>
</Reference>
<Reference Include="MonoGame.Framework, Version=3.6.0.1625, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Framework.DesktopGL.3.6.0.1625\lib\net40\MonoGame.Framework.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Content\bin\font.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\back_foot.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\back_hand.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\back_leg.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\back_palm.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\body.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\front_foot.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\front_hand.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\front_leg.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\front_palm.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\bin\sprite\head.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="MonoGame.Framework.dll.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Binary file added MonoSprites/SDL2.dll
Binary file not shown.
209 changes: 209 additions & 0 deletions MonoSprites/Source/Pressable/Container.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using MonoGame.Extended.Input.InputListeners;
using Microsoft.Xna.Framework;

namespace MonoSprites.Pressable
{
/// <summary>
/// Extension of container to handle click targetting.
/// </summary>
public class PressableContainer : Container
{
/// <summary>
/// The container's mouse event listener.
/// </summary>
protected MouseListener listener;

/// <summary>
/// The subject of the current drag event.
/// </summary>
protected PressableSprite dragSubject;

/// <summary>
/// Pressable container's constructor.
/// </summary>
public PressableContainer(MouseListener listener) : base()
{
this.listener = listener;

listener.MouseClicked += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (target == null)
{
return;
}

var actions = new Dictionary<MouseButton, Action<MouseEventArgs>>()
{
{ MouseButton.Left, target.LeftClick },
{ MouseButton.Right, target.RightClick },
{ MouseButton.Middle, target.MiddleClick }
};

if (actions[args.Button] != null)
{
actions[args.Button](args);
}
};

listener.MouseDown += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (target == null)
{
return;
}

var actions = new Dictionary<MouseButton, Action<MouseEventArgs>>()
{
{ MouseButton.Left, target.LeftDown },
{ MouseButton.Right, target.RightDown },
{ MouseButton.Middle, target.MiddleDown }
};

if (actions[args.Button] != null)
{
actions[args.Button](args);
}
};

listener.MouseUp += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (target == null)
{
return;
}

var actions = new Dictionary<MouseButton, Action<MouseEventArgs>>()
{
{ MouseButton.Left, target.LeftUp },
{ MouseButton.Right, target.RightUp },
{ MouseButton.Middle, target.MiddleUp }
};

if (actions[args.Button] != null)
{
actions[args.Button](args);
}
};

listener.MouseDragStart += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (target == null)
{
return;
}

if (target.DragStart != null)
{
target.DragStart(args);

dragSubject = target;
}
};

listener.MouseDragEnd += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (dragSubject != null && dragSubject.DragEnd != null)
{
dragSubject.DragEnd(args, target);
}

dragSubject = null;

if (target == null)
{
return;
}

if (target.DragStart != null)
{
target.DragStart(args);
}
};

listener.MouseMoved += (object sender, MouseEventArgs args) => {
var target = FindTarget(args.Position.ToVector2());

if (target != null && target.Under != null)
{
target.Under(args);
}
};
}

/// <summary>
/// Finds the top level sprite under the specified world position.
/// </summary>
protected PressableSprite FindTarget(Vector2 position)
{
var under = Under(position);

// Should give us a front-to-back list.
under.Reverse();

// Find the first pressable or child of a cascading pressable under the mouse.
foreach (var current in under)
{
var target = BubblePress(current);

if (target != null)
{
return target;
}
}

return null;
}

/// <summary>
/// Handler for mouse under event.
/// </summary>
public List<PressableSprite> Under(Vector2 position)
{
var ret = new List<PressableSprite>();

Traverse(
(Renderable node) => {
if (node.Visible && ((PressableSprite)node).Contains(position))
{
ret.Add((PressableSprite)node);
}
},
typeof(PressableSprite)
);

return ret;
}

/// <summary>
/// Handles bubbling up the target event throughout the descendant tree.
/// </summary>
public static PressableSprite BubblePress(Renderable target, bool bubbling = false)
{
if (target.GetType() == typeof(PressableSprite))
{
var pressTarget = (PressableSprite)target;

if ((bubbling && !pressTarget.TargetChildren) || !pressTarget.Active)
{
return null;
}

return (PressableSprite)target;
}

if (target.Parent != null)
{
return BubblePress(target.Parent, true);
}

return null;
}
}
}
Loading