Search in sources :

Example 1 with NSObject

use of cli.MonoTouch.Foundation.NSObject in project playn by threerings.

the class IOSTouch method toTouchEvents.

private Event.Impl[] toTouchEvents(NSSet touches, UIEvent event) {
    final Event.Impl[] events = new Event.Impl[Convert.ToInt32(touches.get_Count())];
    touches.Enumerate(new NSSetEnumerator(new NSSetEnumerator.Method() {

        public void Invoke(NSObject obj, boolean[] stop) {
            UITouch touch = (UITouch) obj;
            PointF loc = touch.LocationInView(touch.get_View());
            // transform the point based on our current scale
            IPoint xloc = graphics.transformTouch(loc.get_X(), loc.get_Y());
            // on iOS the memory address of the UITouch object is the unique id
            int id = touch.get_Handle().ToInt32();
            events[_idx++] = new Event.Impl(new Events.Flags.Impl(), touch.get_Timestamp() * 1000, xloc.x(), xloc.y(), id);
            stop[0] = false;
        }

        private int _idx = 0;
    }));
    return events;
}
Also used : TouchImpl(playn.core.TouchImpl) NSObject(cli.MonoTouch.Foundation.NSObject) Events(playn.core.Events) NSSetEnumerator(cli.MonoTouch.Foundation.NSSetEnumerator) PointF(cli.System.Drawing.PointF) UIEvent(cli.MonoTouch.UIKit.UIEvent) IPoint(pythagoras.f.IPoint) UITouch(cli.MonoTouch.UIKit.UITouch) IPoint(pythagoras.f.IPoint)

Example 2 with NSObject

use of cli.MonoTouch.Foundation.NSObject in project playn by threerings.

the class IOSNet method sendRequest.

protected void sendRequest(NSUrlRequest req, final Callback<Response> callback) {
    new NSUrlConnection(req, new NSUrlConnectionDelegate() {

        private NSMutableData data;

        private int rspCode = -1;

        private NSDictionary headers;

        @Override
        public void ReceivedResponse(NSUrlConnection conn, NSUrlResponse rsp) {
            // if we are redirected, we may accumulate data as we bounce through requests, so we reset
            // our data accumulator each time we receive the response headers
            data = new NSMutableData();
            // note our most recent response code
            if (rsp instanceof NSHttpUrlResponse) {
                NSHttpUrlResponse hrsp = (NSHttpUrlResponse) rsp;
                rspCode = hrsp.get_StatusCode();
                headers = hrsp.get_AllHeaderFields();
            }
        }

        @Override
        public void ReceivedData(NSUrlConnection conn, NSData data) {
            this.data.AppendData(data);
        }

        @Override
        public void FailedWithError(NSUrlConnection conn, NSError error) {
            String errmsg = error.get_LocalizedDescription();
            Exception exn = rspCode > 0 ? new HttpException(rspCode, errmsg) : new Exception(errmsg);
            platform.notifyFailure(callback, exn);
        }

        @Override
        public void FinishedLoading(NSUrlConnection conn) {
            platform.notifySuccess(callback, new ResponseImpl(rspCode) {

                @Override
                protected Map<String, List<String>> extractHeaders() {
                    Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
                    for (NSObject key : headers.get_Keys()) {
                        // iOS concatenates all repeated headers into a single header separated by commas,
                        // which is known to be a fucking stupid thing to do, but hey, they're doing it!
                        headerMap.put(key.ToString(), Collections.singletonList(headers.get_Item(key).ToString()));
                    }
                    return headerMap;
                }

                @Override
                public String payloadString() {
                    return NSString.op_Implicit(NSString.FromData(data, NSStringEncoding.wrap(NSStringEncoding.UTF8)));
                }

                @Override
                public byte[] payload() {
                    int length = Convert.ToInt32(data.get_Length());
                    byte[] bytes = new byte[length];
                    Marshal.Copy(data.get_Bytes(), bytes, 0, length);
                    return bytes;
                }
            });
        }
    }, true);
}
Also used : NSObject(cli.MonoTouch.Foundation.NSObject) NSData(cli.MonoTouch.Foundation.NSData) HashMap(java.util.HashMap) NSDictionary(cli.MonoTouch.Foundation.NSDictionary) NSError(cli.MonoTouch.Foundation.NSError) NSUrlConnectionDelegate(cli.MonoTouch.Foundation.NSUrlConnectionDelegate) NSString(cli.MonoTouch.Foundation.NSString) NSHttpUrlResponse(cli.MonoTouch.Foundation.NSHttpUrlResponse) NSUrlResponse(cli.MonoTouch.Foundation.NSUrlResponse) NSUrlConnection(cli.MonoTouch.Foundation.NSUrlConnection) NSMutableData(cli.MonoTouch.Foundation.NSMutableData) List(java.util.List)

Example 3 with NSObject

use of cli.MonoTouch.Foundation.NSObject in project playn by threerings.

the class IOSPlatform method willTerminate.

private void willTerminate() {
    // let the app know that we're terminating
    onExit();
    // terminate our lifecycle observers
    for (NSObject obs : lifecycleObservers) {
        NSNotificationCenter.get_DefaultCenter().RemoveObserver(obs);
    }
    lifecycleObservers.clear();
    // wait for the desired interval and then terminate the GL and AL systems
    NSTimer.CreateScheduledTimer(config.timeForTermination, new NSAction(new NSAction.Method() {

        @Override
        public void Invoke() {
            // stop the GL view
            gameView.Stop();
            // stop and release the AL resources (if audio was ever initialized)
            if (audio != null)
                audio.terminate();
            // clear out the platform in order to make sure the game creation flow can be repeated when
            // it is used as a part of a larger application
            PlayN.setPlatform(null);
        }
    }));
}
Also used : NSObject(cli.MonoTouch.Foundation.NSObject) NSAction(cli.MonoTouch.Foundation.NSAction)

Example 4 with NSObject

use of cli.MonoTouch.Foundation.NSObject in project playn by threerings.

the class IOSPointer method toPointerEvent.

private Event.Impl toPointerEvent(NSSet touches, UIEvent event) {
    final Event.Impl[] eventw = new Event.Impl[1];
    touches.Enumerate(new NSSetEnumerator(new NSSetEnumerator.Method() {

        public void Invoke(NSObject obj, boolean[] stop) {
            UITouch touch = (UITouch) obj;
            int handle = touch.get_Handle().ToInt32();
            // if we have an active touch, we only care about that touch
            if (_active != 0 && handle != _active) {
                stop[0] = false;
            } else {
                _active = handle;
                PointF loc = touch.LocationInView(touch.get_View());
                // transform the point based on our current scale
                IPoint xloc = graphics.transformTouch(loc.get_X(), loc.get_Y());
                eventw[0] = new Event.Impl(new Events.Flags.Impl(), touch.get_Timestamp() * 1000, xloc.x(), xloc.y(), true);
                stop[0] = true;
            }
        }
    }));
    return eventw[0];
}
Also used : PointerImpl(playn.core.PointerImpl) NSObject(cli.MonoTouch.Foundation.NSObject) Events(playn.core.Events) NSSetEnumerator(cli.MonoTouch.Foundation.NSSetEnumerator) PointF(cli.System.Drawing.PointF) UIEvent(cli.MonoTouch.UIKit.UIEvent) IPoint(pythagoras.f.IPoint) UITouch(cli.MonoTouch.UIKit.UITouch) IPoint(pythagoras.f.IPoint)

Aggregations

NSObject (cli.MonoTouch.Foundation.NSObject)4 NSSetEnumerator (cli.MonoTouch.Foundation.NSSetEnumerator)2 UIEvent (cli.MonoTouch.UIKit.UIEvent)2 UITouch (cli.MonoTouch.UIKit.UITouch)2 PointF (cli.System.Drawing.PointF)2 Events (playn.core.Events)2 IPoint (pythagoras.f.IPoint)2 NSAction (cli.MonoTouch.Foundation.NSAction)1 NSData (cli.MonoTouch.Foundation.NSData)1 NSDictionary (cli.MonoTouch.Foundation.NSDictionary)1 NSError (cli.MonoTouch.Foundation.NSError)1 NSHttpUrlResponse (cli.MonoTouch.Foundation.NSHttpUrlResponse)1 NSMutableData (cli.MonoTouch.Foundation.NSMutableData)1 NSString (cli.MonoTouch.Foundation.NSString)1 NSUrlConnection (cli.MonoTouch.Foundation.NSUrlConnection)1 NSUrlConnectionDelegate (cli.MonoTouch.Foundation.NSUrlConnectionDelegate)1 NSUrlResponse (cli.MonoTouch.Foundation.NSUrlResponse)1 HashMap (java.util.HashMap)1 List (java.util.List)1 PointerImpl (playn.core.PointerImpl)1