Handling Game Controller - sinusinu/Flora GitHub Wiki

Handling Game Controller is very similar to Handling Keyboard and Mouse.
IControllerHandler can be used to receive controller events.

Let's take a look!

Controller Events

Begin with Simple Program:

MyCore.cs

using Flora;
using Flora.Gfx;
using Flora.Input;
using Flora.Util;
using System;

namespace FloraTest {
    class MyCore : FloraCore, IControllerHandler {  // Note the addition of IControllerHandler
        public override void Prepare() {
            Controller.SetControllerHandler(this);
        }

        public override void Pause() {
            
        }

        public override void Resume() {
            
        }

        public override void Resize(int width, int height) {
            
        }

        public override void Render(float delta) {
            Gfx.Begin();
            Gfx.End();
        }

        public override void Cleanup() {

        }

        public void OnAxisMotion(int which, ControllerAxis axis, float value) {
            Console.WriteLine("Controller {0} Axis {1} Moved: {2}", which, Enum.GetName(axis), value);
        }

        public void OnButtonDown(int which, ControllerButton button) {
            Console.WriteLine("Controller {0} Button {1} Pressed", which, Enum.GetName(button));
        }

        public void OnButtonUp(int which, ControllerButton button) {
            Console.WriteLine("Controller {0} Button {1} Released", which, Enum.GetName(button));
        }

        public void OnControllerAdded(int which) {
            Console.WriteLine("Controller {0} Connected", which);
        }

        public void OnControllerRemoved(int which) {
            Console.WriteLine("Controller {0} Disconnected", which);
        }
    }
}

Can you see the similarity?

Make MyCore inherit IControllerHandler, and set it active with Controller.SetControllerHandler(this).

Then Flora will call these IControllerHandler functions:

  • OnAxisMotion(int which, ControllerAxis axis, float value)
    • Called when the axis is moved.
    • Axis can be one of LeftX, LeftY, RightX, RightY, TriggerLeft, TriggerRight.
    • value ranges from -1 to 1.
  • OnButtonDown(int which, ControllerButton button)
    • Called when the button is pressed.
  • OnButtonUp(int which, ControllerButton button)
    • Called when the button is released.
  • OnControllerAdded(int which)
    • Called when the new controller is connected.
  • OnControllerRemoved(int which)
    • Called when the controller is disconnected.

which refers to the ID of each controller.

Controller IDs

Each controller gets assigned with unique ID on their connection. Assigned ID number will always increment, making no duplicates.

To get currently available controller IDs, you can use Controller.GetControllerIds() function.

Begin with Simple Program:

MyCore.cs

using Flora;
using Flora.Gfx;
using Flora.Input;
using System;
using System.Text;

namespace FloraTest {
    class MyCore : FloraCore {
        StringBuilder sb = new StringBuilder();

        public override void Prepare() {

        }

        public override void Pause() {
            
        }

        public override void Resume() {
            
        }

        public override void Resize(int width, int height) {
            
        }

        public override void Render(float delta) {
            var ctrId = Controller.GetControllerIds();
            
            sb.Clear();
            foreach (var id in ctrId) sb.Append(id).Append(',');
            if (ctrId.Length > 0) sb.Length = sb.Length - 1;

            Console.WriteLine("Controllers: {0} connected [{1}]", ctrId.Length, sb.ToString());
            Gfx.Begin(0, 0, 0);
            Gfx.End();
        }

        public override void Cleanup() {

        }
    }
}

You will see an output like this:

Controllers: 0 connected []
...
Controllers: 1 connected [1]
...
Controllers: 2 connected [1,2]
...
Controllers: 1 connected [2]
...
Controllers: 2 connected [2,3]
...

You can briefly see how controller IDs work on here.

⚠️ **GitHub.com Fallback** ⚠️