S0L0
Topic Author
Posts: 18
Joined: 07 Aug 2014, 14:49

Gamepad Support?

07 Aug 2014, 14:54

I'm considering buying NoesisGUI from the asset store for my game which currently uses inControl for gamepad input, how does NoesisGUI handle gamepad input in Unity? Can I have a C# script in Unity hooked up to the NoesisGUI that handles input?
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Gamepad Support?

07 Aug 2014, 16:06

We want to provide better input integration in NoesisGUI in a following release, by addding the possibility to inject input events directly into the NoesisGUIPanel component.

While this is publicly released, you can patch our scripts like this:

Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs
using UnityEngine;
using Noesis;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

[AddComponentMenu("NoesisGUI/NoesisGUI Panel")]
public class NoesisGUIPanel : MonoBehaviour
{
    // ...

    ////////////////////////////////////////////////////////////////////////////////////////////////
    private Noesis.UIRenderer _uiRenderer;

    ////////////////////////////////////////////////////////////////////////////////////////////////
    public T GetRoot<T>() where T : BaseComponent
    {
        if (_uiRenderer != null)
        {
            return _uiRenderer.GetRoot<T>();
        }
        else
        {
            return null;
        }
    }

    // ADD THE FOLLOWING FUNCTIONS FOR INPUT HANDLING
    //@{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public void KeyDown(Noesis.Key key)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.KeyDown(key);
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    public void KeyUp(Noesis.Key key)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.KeyUp(key);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////
    public void MouseDown(float x, float y, Noesis.MouseButton button)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.MouseDown(x, y, button);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////
    public void MouseUp(float x, float y, Noesis.MouseButton button)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.MouseUp(x, y, button);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////
    public void MouseDoubleClick(float x, float y, Noesis.MouseButton button)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.MouseDoubleClick(x, y, button);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////
    public void MouseWheel(float x, float y, int wheelRotation)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.MouseWheel(x, y, wheelRotation);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////
    public void MouseMove(float x, float y)
    {
        if (_uiRenderer != null)
        {
            _uiRenderer.MouseMove(x, y);
        }
    }
    //@}

    // ...
}
Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRenderer.cs
using UnityEngine;
using System;
using System.Runtime.InteropServices;

namespace Noesis
{
    // ...

    /////////////////////////////////////////////////////////////////////////////////////
    /// Manages updates, render and input events of a Noesis UI panel
    /////////////////////////////////////////////////////////////////////////////////////
    internal partial class UIRenderer
    {
        // ...

        /////////////////////////////////////////////////////////////////////////////////
        public T GetRoot<T>() where T : BaseComponent
        {
            return _root.As<T>();
        }

        // ADD THE FOLLOWING FUNCTIONS FOR INPUT HANDLING
        //@{
        /////////////////////////////////////////////////////////////////////////////////
        public void KeyDown(Noesis.Key key)
        {
            Noesis_KeyDown(_rendererId, (int)key);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void KeyUp(Noesis.Key key)
        {
            Noesis_KeyUp(_rendererId, (int)key);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void MouseDown(float x, float y, Noesis.MouseButton button)
        {
            Noesis_MouseButtonDown(_rendererId, x, y, (int)button);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void MouseUp(float x, float y, Noesis.MouseButton button)
        {
            Noesis_MouseButtonUp(_rendererId, x, y, (int)button);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void MouseDoubleClick(float x, float y, Noesis.MouseButton button)
        {
            Noesis_MouseDoubleClick(_rendererId, x, y, (int)button);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void MouseWheel(float x, float y, int wheelRotation)
        {
            Noesis_MouseWheel(_rendererId, x, y, wheelRotation);
        }

        /////////////////////////////////////////////////////////////////////////////////
        public void MouseMove(float x, float y)
        {
            Noesis_MouseMove(_rendererId, x, y);
        }
        //@}

        // ...
    }
}
You can simulate a mouse pointer with the axis info of your gamepad. Or maybe you will prefer to simulate Tab, Shift+Tab, and Arrow keys to manage Keyboard focus.
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Gamepad Support?

08 Aug 2014, 20:21

Let me clarify how to use this. NoesisGUI only understands the events that are listed in the file Assets\Plugins\NoesisGUI\Scripts\Core\InputEnums.cs. With the patch given by Sergio we have exposed a way to manually inject key codes.

For example, to translate from InControl you would have to do something like this (pseudocode):
if (InputManager.ActiveDevice.Direction.Left.IsPressed)
{
    noesisGUIPanel.KeyDown(Noesis.Key.Left);   
}
else if (InputManager.ActiveDevice.Direction.Left.WasReleased)
{
    noesisGUIPanel.KeyUp(Noesis.Key.Left);
}
This way, you are translating the event Direction.Left from InControl to Noesis. The same for the rest of events.
 
S0L0
Topic Author
Posts: 18
Joined: 07 Aug 2014, 14:49

Re: Gamepad Support?

08 Aug 2014, 22:30

I'm still not sure exactly where the code should be added? I followed the tutorial where a script was created in the camera to attach a button.click event, is this where
if (InputManager.ActiveDevice.Direction.Left.IsPressed)
{
    noesisGUIPanel.KeyDown(Noesis.Key.Left);   
}
else if (InputManager.ActiveDevice.Direction.Left.WasReleased)
{
    noesisGUIPanel.KeyUp(Noesis.Key.Left);
}
should be added? I am trying to setup a horizontal stack panel that can be navigated left and right.
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Gamepad Support?

08 Aug 2014, 23:22

Yes, you can do it from a script:
 using UnityEngine;
 using Noesis;

 public class Test: MonoBehaviour
 {
     void Start()
     {
         // Access to the NoesisGUI component
         NoesisGUIPanel noesisGUI = GetComponent<NoesisGUIPanel>();

         // This simulates a press of the left key
         noesisGUI.KeyDown(Noesis.Key.Left);
         noesisGUI.KeyUp(Noesis.Key.Left);
     }

 }
 
S0L0
Topic Author
Posts: 18
Joined: 07 Aug 2014, 14:49

Re: Gamepad Support?

08 Aug 2014, 23:34

It says noesisGuiPanel does not exist? Not sure what to do since I added the Noesis namespace.

http://media-cache-ec0.pinimg.com/origi ... 8b3171.jpg
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Gamepad Support?

08 Aug 2014, 23:41

Sorry, my code was not correct. I have fixed it.
 
S0L0
Topic Author
Posts: 18
Joined: 07 Aug 2014, 14:49

Re: Gamepad Support?

09 Aug 2014, 00:03

That worked, but it seems the patch to inject key codes manually does not seem to be working as Noesis.UIRenderer is not recognizing KeyDown?

http://media-cache-ec0.pinimg.com/origi ... 5322dd.jpg

http://media-cache-ak0.pinimg.com/origi ... bf4ff2.jpg

It is also unable to add the noesisguipanel script to the camera:

http://media-cache-ec0.pinimg.com/origi ... da47ae.jpg


P.S. I sent you a message on Unity forum that may be more help with getting this setup :)
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Gamepad Support?

10 Aug 2014, 17:12

Ok, I did the following. First, you need to apply the patches commented above. They will be included in the next version of noesisGUI. You can download the patches from here:
Then, I have created a grid of buttons for testing:
<Grid
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d">

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" KeyboardNavigation.DirectionalNavigation="Cycle">
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
      </Grid.RowDefinitions>

      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="0" Grid.Column="0" />
      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="0" Grid.Column="1" />
      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="0" Grid.Column="2" />

      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="1" Grid.Column="0" />
      <Button Content="Button" x:Name="start" Padding="20" FontSize="20" Grid.Row="1" Grid.Column="1" />
      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="1" Grid.Column="2" />

      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="2" Grid.Column="0" />
      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="2" Grid.Column="1" />
      <Button Content="Button" Padding="20" FontSize="20" Grid.Row="2" Grid.Column="2" />
    </Grid>
  
</Grid>
Then I created a simple navigation script:
using System;
using UnityEngine;
using InControl;
using Noesis;


namespace BasicExample
{
    public class Controller : MonoBehaviour
    {
        NoesisGUIPanel noesisGUI_;

        void Start()
        {
            noesisGUI_ = GetComponent<NoesisGUIPanel>();
            var root = noesisGUI_.GetRoot<Grid>();
            var start = root.FindName<Button>("start");
            start.Focus();
        }

        private void Handle(OneAxisInputControl input, Noesis.Key key)
        {
            if (input.WasPressed)
            {
                noesisGUI_.KeyDown(key);
            }

            if (input.WasReleased)
            {
                noesisGUI_.KeyUp(key);
            }
        }

        private void Handle(InputControl input, Noesis.Key key)
        {
            if (input.WasPressed)
            {
                noesisGUI_.KeyDown(key);
            }

            if (input.WasReleased)
            {
                noesisGUI_.KeyUp(key);
            }
        }

        void Update()
        {
            // Use last device which provided input.
            var inputDevice = InputManager.ActiveDevice;

            Handle(inputDevice.Direction.Left, Key.Left);
            Handle(inputDevice.Direction.Right, Key.Right);
            Handle(inputDevice.Direction.Up, Key.Up);
            Handle(inputDevice.Direction.Down, Key.Down);

            Handle(inputDevice.Action1, Key.Space);
        }
    }
}
I have tested it with a XBOX360 controller and it is working fine. I can select the desired button with the left stick or DPad and click each element with the A button.

You can download this sample scene here: https://drive.google.com/file/d/0Bwxw0R ... sp=sharing
 
S0L0
Topic Author
Posts: 18
Joined: 07 Aug 2014, 14:49

Re: Gamepad Support?

10 Aug 2014, 23:21

Works Great Now! Thank You! :D

Though I have a question about the xaml grid, how is it that only one of the buttons has X:Name="start"?

Who is online

Users browsing this forum: No registered users and 0 guests