Difference between revisions of "Input Pointers"

From PowerUI
Jump to: navigation, search
(Making it click)
Line 14: Line 14:
  
 
PowerUI has a custom input pointer, a [http://powerui.kulestar.com/powerdocs-2_0/classPowerUI_1_1CameraPointer.html CameraPointer], for virtual reality. See [[Virtual Reality Cameras|the article]] relating to virtual reality camera's.
 
PowerUI has a custom input pointer, a [http://powerui.kulestar.com/powerdocs-2_0/classPowerUI_1_1CameraPointer.html CameraPointer], for virtual reality. See [[Virtual Reality Cameras|the article]] relating to virtual reality camera's.
 +
 +
== Define your own ==
 +
 +
If you're doing something more specialised then you can also define your own input pointer. Multiple Wii controllers, for example. This is very straight forward to do - at a minimum you must inherit from InputPointer and override Relocate:
 +
 +
<syntaxhighlight lang="csharp">
 +
 +
public class MyCustomPointer : InputPointer{
 +
   
 +
    public override bool Relocate(){
 +
   
 +
        // - Get your latest coordinates here -
 +
        Vector2 newPosition = Vector2.zero;
 +
       
 +
        if(newPosition.x == ScreenX && newPosition.y == ScreenY){
 +
            // It's not moved anywhere - quit:
 +
            return false;
 +
        }
 +
       
 +
        // It's moved! Update the x/y coords:
 +
        ScreenX = newPositionX;
 +
        ScreenY = newPositionY;
 +
       
 +
        return true;
 +
    }
 +
 +
}
 +
 +
</syntaxhighlight>
 +
 +
Then to use it, all you need to do is create and add it:
 +
 +
<syntaxhighlight lang="csharp">
 +
    MyCustomPointer pointer = new MyCustomPointer();
 +
    pointer.Add();
 +
</syntaxhighlight>
 +
 +
It can be clicked etc or have a pressure value like any other pointer.
  
 
== Making it click ==
 
== Making it click ==
Line 25: Line 63:
 
void OnGUI(){
 
void OnGUI(){
  
   // Returns true if your pointer actually did something.
+
   // Returns true if your pointer actually did something (it went down or up)
 
   // You can also directly pass a UnityEngine.Event too.
 
   // You can also directly pass a UnityEngine.Event too.
 
   bool didSomething = MyPointer.HandleEvent();
 
   bool didSomething = MyPointer.HandleEvent();

Revision as of 17:29, 23 April 2017

An input pointer is any kind of pointing device which presses, hovers, or both. A mouse, a finger and a stylus are the three main ones. They are related to the (draft) W3C Pointer Events specification. Here's some specific notes about the input pointers available in PowerUI.

Mouse Input

PowerUI assumes that only a desktop platform will have a mouse. A MousePointer is created when it's running on a desktop and PowerUI.Input.CreateSystemMouse is true (the default). If your project doesn't use a mouse pointer on a desktop platform (it's virtual reality for example), you'd remove it by setting CreateSystemMouse to false in an Awake method.

Touch and Stylus Input

PowerUI handles multi-touch input by default on any platform which supports it. It will automatically stack with a mouse input on desktops which also have touchscreens. Each time a new touch is detected, a FingerPointer or a StylusPointer is created. They fire the various touch events as well as mouse events.

All pointers are primary (which means they all fire those mouse events too).

Virtual Reality

PowerUI has a custom input pointer, a CameraPointer, for virtual reality. See the article relating to virtual reality camera's.

Define your own

If you're doing something more specialised then you can also define your own input pointer. Multiple Wii controllers, for example. This is very straight forward to do - at a minimum you must inherit from InputPointer and override Relocate:

public class MyCustomPointer : InputPointer{
    
    public override bool Relocate(){
    
        // - Get your latest coordinates here -
        Vector2 newPosition = Vector2.zero;
        
        if(newPosition.x == ScreenX && newPosition.y == ScreenY){
            // It's not moved anywhere - quit:
            return false;
        }
        
        // It's moved! Update the x/y coords:
        ScreenX = newPositionX;
        ScreenY = newPositionY;
        
        return true;
    }

}

Then to use it, all you need to do is create and add it:

    MyCustomPointer pointer = new MyCustomPointer();
    pointer.Add();

It can be clicked etc or have a pressure value like any other pointer.

Making it click

If you've got an input pointer and you'd like to click it (or vary its downward pressure), you can either:

  • Use the convenience method for mouse up/down events from OnGUI:
void OnGUI(){

  // Returns true if your pointer actually did something (it went down or up)
  // You can also directly pass a UnityEngine.Event too.
  bool didSomething = MyPointer.HandleEvent();

}
  • Call the MyPointer.Down and MyPointer.Up methods (Optionally specifying a mouse button ID):
// Mousedown event, left button:
MyPointer.Down(0);

// Which is also the same as:
MyPointer.LeftDown();

// Mouseup event, left button:
MyPointer.Up(0);

// Which is the same as:
MyPointer.LeftUp();
  • Use SetPressure directly - this is useful if you've got some kind of stylus (every other option is just an abstraction of SetPressure)
void Update(){
  
  // Update the pointer pressure:
  MyPointer.SetPressure(someVaryingField);

}

Accessing all pointers

PowerUI implements various standard API's for accessing pointers:

  • TouchEvent.touches
  • TouchEvent.targetTouches
  • TouchEvent.changedTouches
  • TouchEvent.identifier
  • More!

The non-standard internal ones:

  • UIEvent.trigger - MouseEvent, TouchEvent etc; The InputPointer instance this event came from
  • InputPointer.All - All active pointers (array created on use; Use AllRaw and PointerCount for better performance)
  • InputPointer.PointerCount - The number of actual pointers in the AllRaw pointer set
  • InputPointer.AllRaw - The raw set of pointers. Entries beyond PointerCount are random noise/ undefined.


Source locations

You'll find the implementations of MousePointer, FingerPointer etc here:

  • PowerUI/Source/Engine/Input/