ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

MouseHelper.java

Go to the documentation of this file.
00001 package org.residualvm.residualvm;
00002 
00003 import android.view.InputDevice;
00004 import android.view.MotionEvent;
00005 import android.view.SurfaceView;
00006 import android.view.View;
00007 
00011 public class MouseHelper {
00012     private View.OnHoverListener _listener;
00013     private ResidualVM _residualvm;
00014     private long _rmbGuardTime;
00015     private boolean _rmbPressed;
00016     private boolean _lmbPressed;
00017     private boolean _mmbPressed;
00018 
00023     static {
00024         try {
00025             Class.forName("android.view.View$OnHoverListener");
00026         } catch (Exception ex) {
00027             throw new RuntimeException(ex);
00028         }
00029     }
00030 
00034     public static void checkHoverAvailable() {}
00035 
00036     public MouseHelper(ResidualVM residualvm) {
00037         _residualvm = residualvm;
00038         _listener = createListener();
00039     }
00040 
00041     private View.OnHoverListener createListener() {
00042         return new View.OnHoverListener() {
00043             @Override
00044             public boolean onHover(View view, MotionEvent e) {
00045                 return onMouseEvent(e, true);
00046             }
00047         };
00048     }
00049 
00050     public void attach(SurfaceView main_surface) {
00051         main_surface.setOnHoverListener(_listener);
00052     }
00053 
00054     public static boolean isMouse(MotionEvent e) {
00055         if (e == null) {
00056             return false;
00057         }
00058 
00059         InputDevice device = e.getDevice();
00060 
00061         if (device == null) {
00062             return false;
00063         }
00064 
00065         int sources = device.getSources();
00066 
00067         return ((sources & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) ||
00068                ((sources & InputDevice.SOURCE_STYLUS) == InputDevice.SOURCE_STYLUS) ||
00069                ((sources & InputDevice.SOURCE_TOUCHPAD) == InputDevice.SOURCE_TOUCHPAD);
00070     }
00071 
00072     public boolean onMouseEvent(MotionEvent e, boolean hover) {
00073         _residualvm.pushEvent(ResidualVMEvents.JE_MOUSE_MOVE, (int)e.getX(), (int)e.getY(), 0, 0, 0, 0);
00074 
00075         int buttonState = e.getButtonState();
00076 
00077         boolean lmbDown = (buttonState & MotionEvent.BUTTON_PRIMARY) == MotionEvent.BUTTON_PRIMARY;
00078 
00079         if (!hover && e.getAction() != MotionEvent.ACTION_UP && buttonState == 0) {
00080             // On some device types, ButtonState is 0 even when tapping on the touchpad or using the stylus on the screen etc.
00081             lmbDown = true;
00082         }
00083 
00084         if (lmbDown) {
00085             if (!_lmbPressed) {
00086                 // left mouse button was pressed just now
00087                 _residualvm.pushEvent(ResidualVMEvents.JE_LMB_DOWN, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00088             }
00089 
00090             _lmbPressed = true;
00091         } else {
00092             if (_lmbPressed) {
00093                 // left mouse button was released just now
00094                 _residualvm.pushEvent(ResidualVMEvents.JE_LMB_UP, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00095             }
00096 
00097             _lmbPressed = false;
00098         }
00099 
00100         boolean rmbDown = (buttonState & MotionEvent.BUTTON_SECONDARY) == MotionEvent.BUTTON_SECONDARY;
00101         if (rmbDown) {
00102             if (!_rmbPressed) {
00103                 // right mouse button was pressed just now
00104                 _residualvm.pushEvent(ResidualVMEvents.JE_RMB_DOWN, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00105             }
00106 
00107             _rmbPressed = true;
00108         } else {
00109             if (_rmbPressed) {
00110                 // right mouse button was released just now
00111                 _residualvm.pushEvent(ResidualVMEvents.JE_RMB_UP, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00112                 _rmbGuardTime = System.currentTimeMillis();
00113             }
00114 
00115             _rmbPressed = false;
00116         }
00117 
00118         boolean mmbDown = (buttonState & MotionEvent.BUTTON_TERTIARY) == MotionEvent.BUTTON_TERTIARY;
00119         if (mmbDown) {
00120             if (!_mmbPressed) {
00121                 // middle mouse button was pressed just now
00122                 _residualvm.pushEvent(ResidualVMEvents.JE_MMB_DOWN, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00123             }
00124 
00125             _mmbPressed = true;
00126         } else {
00127             if (_mmbPressed) {
00128                 // middle mouse button was released just now
00129                 _residualvm.pushEvent(ResidualVMEvents.JE_MMB_UP, (int)e.getX(), (int)e.getY(), e.getButtonState(), 0, 0, 0);
00130             }
00131 
00132             _mmbPressed = false;
00133         }
00134 
00135         return true;
00136     }
00137 
00144     public boolean getRmbGuard() {
00145         return _rmbPressed || _rmbGuardTime + 200 > System.currentTimeMillis();
00146     }
00147 }


Generated on Sat May 30 2020 05:00:34 for ResidualVM by doxygen 1.7.1
curved edge   curved edge