Code Monkey home page Code Monkey logo

inputobservable's Introduction

Input Observable: Input Handling Utility with UniRx

Providing screen touch / mouse event Rx stream (IObservable<T>) and its Operators.

  • Input source object (IInputObservable) related with a finger or button id and its manager object (InputObservableContext)
  • Verious input gesture supported
    • long press (LongSequence())
    • double tap/clik (DoubleSequence())
    • swipe (Verocity(), TakeLastVerocities())
    • pinch in/out (RectangleObservable, PinchSequence())
  • Rotation conversion (Differnece(), ToEulerAngle())
  • Gyroscope, Mouse wheel operation also supported (GyroInputObservable, IMouseWheelObservable)

android

How To Use

    // Get IInputObservableContext
    IInputObservableContext context = this.DefaultInputContext();

    // Get IInputObservable with id=0, left button for Editor, fingerId=0 for Android/iOS
    IInputObservable touch0 = context.GetObservable(0);

    // Get IInputObservable with id=1, right button for Editor, fingerId=1 for Android/iOS
    IInputObservable touch1 = context.GetObservable(1);

    // Draw Point at the location of first touch (or left button clicked)
    touch0.OnBegin.Subscribe(e => // e is InputEvent
    {
        DrawPoint(e.position);
    });

    // Slide (or dragging) Detection
    touch0.OnMoved.Subscribe(e => ...);

    // Touch Up (or left button release) detection
    touch0.OnEnd.Subscribe(e => ...);

    // Any input event
    touch0.Any().Subscribe(e => ...);

    // Double Tap/click detection during 200 msec
    touch0.DoubleSequence(200).Subscribe(e => {})

    // Long Touch/press detection over 1000 msec
    touch0.LongSequence(1000).Subscribe(e => {})

    // Slide/Move speed detection between two consecutive points
    touch0.Verocity().Subscribe(v => // v is VerocityInfo
    {
        DrawPoint(v.@event.position);
        DrawArrow(v.vector); // v.vector is Vector2 per milliseconds
    });

    // Slide/Move speed detection between two consecutive points in the last 8 points
    touch0.TakeLastVerocities(8).Subscribe(verocities =>
    {
        foreach (var v in verocities) {
            DrawPoint(v.@event.position);
            DrawArrow(v.vector)
        }
    });

    // Rotation event conversion from slide/move operation,
    // at a rate of 90 degrees of slide at both ends of the screen (both up and down and left and right).
    touch0.Difference().Subscribe(diff =>  // diff is Vector2
    {
        var rot = diff.ToEulerAngle(-90.0f / Screen.width, -90.0f / Screen.height);
        target.Rotate(rot, Space.World);
    }

    // Differences of the width/height value of the rectangle over time (two consecutive rectangles)
    // for multi touch pinch operation
    RectangleObservable.From(touch0, touch1)
        .PinchSequence()
        .RepeatUntilDestroy(this)
        .Subscribe(diff =>  // diff is Vector2
    {
        if(diff.x > 0) { /* larger horizontally*/ }
        else if(diff.x <= 0) { /* smaller horizontally */ }
        if(diff.y > 0) { /* larger vertically */ }
        else if(diff.y <= 0) { /* smaller vertically */ }
    });

    // Mouse wheel operation, type check is usefull for shared code between Editor and Android/iOS
    (context as IMouseWheelObservable)?.Wheel.Subscribe(w => {
        // w is MouseWheelEvent
    });

    // Gyroscope to pose rotation
    var gyro = new GyroInputObservable(this);
    gyro.EulerAngles.Subscribe(e =>
    {
        // e is Vector3
        var rot = Quaternion.Euler(e);
    });

Requirements

  • 2019.3.4f1 or later

Install

install via openupm

    $ openupm add com.jtfrom9.input-observable

inputobservable's People

Contributors

jtfrom9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

longde123

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.