Repository: flex-examples
Updated Branches:
  refs/heads/develop c652cfba6 -> 0cd6ec753


PseudoThread utility is now truly open source


Project: http://git-wip-us.apache.org/repos/asf/flex-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-examples/commit/0cd6ec75
Tree: http://git-wip-us.apache.org/repos/asf/flex-examples/tree/0cd6ec75
Diff: http://git-wip-us.apache.org/repos/asf/flex-examples/diff/0cd6ec75

Branch: refs/heads/develop
Commit: 0cd6ec7536976dab62c73dac5bc370ef5628d1de
Parents: c652cfb
Author: Alex Harui <aha...@apache.org>
Authored: Wed Sep 3 13:23:27 2014 -0700
Committer: Alex Harui <aha...@apache.org>
Committed: Wed Sep 3 13:23:27 2014 -0700

----------------------------------------------------------------------
 PseudoThread/NOTICE          |   9 ++++
 PseudoThread/PseudoThread.as | 107 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-examples/blob/0cd6ec75/PseudoThread/NOTICE
----------------------------------------------------------------------
diff --git a/PseudoThread/NOTICE b/PseudoThread/NOTICE
new file mode 100644
index 0000000..d91483e
--- /dev/null
+++ b/PseudoThread/NOTICE
@@ -0,0 +1,9 @@
+Apache Flex PseudoThread Utility
+Copyright 2014 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
+The Initial Developer of the Original Code 
+is Adobe Systems Incorporated (http://www.adobe.com/).
+    Copyright 2003 - 2012 Adobe Systems Incorporated. All Rights Reserved.

http://git-wip-us.apache.org/repos/asf/flex-examples/blob/0cd6ec75/PseudoThread/PseudoThread.as
----------------------------------------------------------------------
diff --git a/PseudoThread/PseudoThread.as b/PseudoThread/PseudoThread.as
new file mode 100644
index 0000000..f90f20b
--- /dev/null
+++ b/PseudoThread/PseudoThread.as
@@ -0,0 +1,107 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package
+{
+ import flash.display.DisplayObjectContainer;
+ import flash.events.Event;
+ import flash.events.EventDispatcher;
+ import flash.events.KeyboardEvent;
+ import flash.events.MouseEvent;
+ import flash.utils.getTimer;
+ import mx.core.UIComponent;
+ import mx.managers.ISystemManager;
+ 
+ public class PseudoThread extends EventDispatcher
+ {
+        public function PseudoThread(sm:ISystemManager, 
threadFunction:Function, threadObject:Object)
+        {
+                fn = threadFunction;
+                obj = threadObject;
+
+                // add high priority listener for ENTER_FRAME
+                sm.stage.addEventListener(Event.ENTER_FRAME, 
enterFrameHandler, false, 100);
+                sm.stage.addEventListener(MouseEvent.MOUSE_MOVE, 
mouseMoveHandler);
+                sm.stage.addEventListener(KeyboardEvent.KEY_DOWN, 
keyDownHandler);
+                
+                thread = new UIComponent();
+                sm.addChild(thread);
+                thread.addEventListener(Event.RENDER, renderHandler);
+        }
+
+        // number of milliseconds we think it takes to render the screen
+        public var RENDER_DEDUCTION:int = 10;
+
+        private var fn:Function;
+        private var obj:Object;
+        private var thread:UIComponent;
+        private var start:Number;
+        private var due:Number;
+
+        private var mouseEvent:Boolean;
+        private var keyEvent:Boolean;
+
+        private function enterFrameHandler(event:Event):void
+        {
+               start = getTimer();
+               var fr:Number = Math.floor(1000 / 
thread.systemManager.stage.frameRate);
+               due = start + fr;
+
+               thread.systemManager.stage.invalidate();
+               thread.graphics.clear();
+               thread.graphics.moveTo(0, 0);
+               thread.graphics.lineTo(0, 0);   
+        }
+
+        private function renderHandler(event:Event):void
+        {
+                if (mouseEvent || keyEvent)
+                        due -= RENDER_DEDUCTION;
+
+                while (getTimer() < due)
+                {
+                       if (!fn(obj))
+                       {
+                               if (!thread.parent)
+                                       return;
+
+                               var sm:ISystemManager = thread.systemManager;
+                               sm.stage.removeEventListener(Event.ENTER_FRAME, 
enterFrameHandler);
+                               
sm.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
+                               
sm.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
+                               sm.removeChild(thread);
+                               thread.removeEventListener(Event.RENDER, 
renderHandler);
+                               dispatchEvent(new Event("threadComplete"));
+                       }
+                }
+
+                mouseEvent = false;
+                keyEvent = false;
+        }
+
+        private function mouseMoveHandler(event:Event):void
+        {
+               mouseEvent = true;
+        }
+
+        private function keyDownHandler(event:Event):void
+        {
+               keyEvent = true;
+        }
+ } 
+}

Reply via email to