Permalink
Browse files

any locomotive can by assigned to a Gamepad/Joystick (i.e. Playstatio…

…n2 format), one controller can control two different locomotives
  • Loading branch information...
cbries committed Sep 17, 2017
1 parent 9785a6b commit 29ab4d63649b762ad57b9aa43776c20e6a249e97
View
@@ -3,6 +3,7 @@
Release Candidate 5 (RC5)
- information about any Locomotive's function
- query speed step and provide stepping in locomotive control
- any locomotive can by assigned to a Gamepad/Joystick (i.e. Playstation2 format), one controller can control two different locomotives
2017 September 10th
-------------------
View
@@ -23,6 +23,8 @@
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Ecos2Core;
using RailwayEssentialCore;
@@ -138,6 +140,67 @@ public bool InitializeWeaving(TrackPlanParser.Track track, string weaveFilepath)
return true;
}
public void ForwardGamepadInput(IReadOnlyList<IGamepadInput> commands)
{
var allLocs = GetDataProvider().Objects.OfType<Locomotive>();
foreach (var cmd in commands)
{
if (cmd == null)
continue;
Locomotive locObj = null;
if (cmd.IsRight)
{
var locObjs = allLocs.Where(x => x.HasRightController).ToList();
if(locObjs.Count <= 0)
continue;
locObj = locObjs[0];
}
else if (cmd.IsLeft)
{
var locObjs = allLocs.Where(x => x.HasLeftController).ToList();
if (locObjs.Count <= 0)
continue;
locObj = locObjs[0];
}
if (locObj != null)
{
if (cmd.F0)
locObj.ToggleFunction(0);
if (cmd.F1)
locObj.ToggleFunction(1);
if (cmd.F2)
locObj.ToggleFunction(2);
if (cmd.F3)
locObj.ToggleFunction(3);
if (cmd.F4)
locObj.ToggleFunction(4);
if (cmd.F5)
locObj.ToggleFunction(5);
if (cmd.IncSpeed)
locObj.IncreaseSpeed();
if (cmd.DecSpeed)
locObj.DescreaseSpeed();
if (cmd.MaxSpeed)
{
var maxSpeedstep = locObj.GetNumberOfSpeedsteps();
locObj.ChangeSpeedstep(maxSpeedstep);
}
if(cmd.StopSpeed)
locObj.ChangeSpeedstep(0);
}
}
}
public async Task ForwardCommands(IReadOnlyList<ICommand> commands)
{
if (!_communication.IsConnected)
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using SharpDX.DirectInput;
namespace RailwayEssentialCore
{
public enum StickDirection
{
Left,
Right,
Top,
Bottom,
Unknown
}
public class GamepadEventArgs : EventArgs
{
public JoystickUpdate Data { get; set; }
public bool IsRightStick { get; set; }
public bool IsLeftStick { get; set; }
public StickDirection Direction { get; set; }
public GamepadEventArgs()
{
Direction = StickDirection.Unknown;
}
}
public interface IGamepad
{
event EventHandler ButtonHandler;
List<Guid> GetAvailableDevices();
List<Guid> GetGamepadGuids();
List<Guid> GetJoystickGuids();
bool IsAvailable(Guid guid);
bool ActivateGamepad(Guid guid);
bool Close();
}
}
@@ -0,0 +1,20 @@
namespace RailwayEssentialCore
{
public interface IGamepadInput
{
bool IsLeft { get; set; }
bool IsRight { get; set; }
bool IncSpeed { get; set; }
bool DecSpeed { get; set; }
bool StopSpeed { get; set; }
bool MaxSpeed { get; set; }
bool F0 { get; set; }
bool F1 { get; set; }
bool F2 { get; set; }
bool F3 { get; set; }
bool F4 { get; set; }
bool F5 { get; set; }
}
}
@@ -25,6 +25,7 @@ namespace RailwayEssentialCore
{
public interface IRailwayEssentialModel
{
IGamepad Gamepad { get; set; }
bool IsVisualLabelActivated { get; set; }
void TriggerPropertyChanged(string name);
@@ -70,6 +70,12 @@
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="SharpDX, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.DirectInput, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.DirectInput.4.0.1\lib\net45\SharpDX.DirectInput.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
@@ -86,6 +92,8 @@
<Compile Include="Base64.cs" />
<Compile Include="Globals.cs" />
<Compile Include="IConfiguration.cs" />
<Compile Include="IGamepad.cs" />
<Compile Include="IGamepadInput.cs" />
<Compile Include="IPersist.cs" />
<Compile Include="IRailwayEssentialModel.cs" />
<Compile Include="ITrackEdit.cs" />
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net46" />
<package id="SharpDX" version="4.0.1" targetFramework="net46" />
<package id="SharpDX.DirectInput" version="4.0.1" targetFramework="net46" />
</packages>
@@ -21,13 +21,31 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System.Collections.Generic;
using RailwayEssentialMdi.DataObjects;
using RailwayEssentialMdi.Input;
namespace RailwayEssentialMdi
{
using System;
using RailwayEssentialCore;
public class Configuration : Bases.ViewModelBase, IConfiguration
{
private ProjectGamepad _gamepad;
public ProjectGamepad Gamepad
{
get => _gamepad;
set
{
_gamepad = value;
RaisePropertyChanged("Gamepad");
RaisePropertyChanged("ControllerGuid");
}
}
private string _ipAddress;
private UInt16 _port;
private int _designerColumns;
@@ -53,6 +71,33 @@ public UInt16 Port
}
}
public string ControllerGuid
{
get => Gamepad.Guid;
set
{
Gamepad.Guid = value;
RaisePropertyChanged("ControllerGuid");
}
}
public List<string> ControllerGuids
{
get
{
var mgr = new Gamepad();
var devices = mgr.GetAvailableDevices();
List<string> guids = new List<string>();
foreach (var dev in devices)
{
if (!guids.Contains(dev.ToString("D").ToUpper()))
guids.Add(dev.ToString("D").ToUpper());
}
guids.Insert(0, "--");
return guids;
}
}
public int DesignerColumns
{
get => _designerColumns;
@@ -77,6 +122,7 @@ public int DesignerRows
public Configuration()
{
Gamepad = new ProjectGamepad();
#if DEBUG
IpAddress = "192.168.178.61";
#else
@@ -44,6 +44,7 @@ public class ProjectFile : IPersist
public UInt16 TargetPort { get; set; }
public int DesignerColumns { get; set; }
public int DesignerRows { get; set; }
public ProjectGamepad Gamepad { get; set; }
public List<string> Objects { get; set; }
public ProjectTrack Track { get; set; }
public List<ProjectTrackView> TrackViews { get; set; }
@@ -57,6 +58,7 @@ public ProjectFile()
TrackViews = new List<ProjectTrackView>();
BlockRoutes = new List<Route>();
BlockRouteGroups= new List<RouteGroup>();
Gamepad= new ProjectGamepad();
}
public bool Load(string path)
@@ -97,6 +99,15 @@ public bool Load(string path)
TargetPort = 15471;
}
if (o["gamepad"] != null)
{
var go = new ProjectGamepad();
if (go.Parse(o["gamepad"]))
Gamepad = go;
else
Gamepad = new ProjectGamepad();
}
if (o["designerColumns"] != null)
DesignerColumns = (int) o["designerColumns"];
@@ -213,6 +224,7 @@ private JObject ToJson()
["version"] = Version,
["targetHost"] = TargetHost,
["targetPort"] = TargetPort,
["gamepad"] = Gamepad.ToJson(),
["designerColumns"] = DesignerColumns,
["designerRows"] = DesignerRows,
["track"] = Track.ToJson(),
@@ -0,0 +1,35 @@
using Newtonsoft.Json.Linq;
namespace RailwayEssentialMdi.DataObjects
{
public class ProjectGamepad
{
public string Guid { get; set; }
public ProjectGamepad()
{
Guid = "--";
}
public bool Parse(JToken tkn)
{
var o = tkn as JObject;
if (o == null)
return false;
if (o["guid"] != null)
Guid = o["guid"].ToString();
return true;
}
public JObject ToJson()
{
var o = new JObject
{
["guid"] = Guid
};
return o;
}
}
}
Oops, something went wrong.

0 comments on commit 29ab4d6

Please sign in to comment.