Scrollable Container

by hemingway
♥0 | Line 270 | Modified 2013-03-13 03:36:06 | MIT License
play

ActionScript3 source code

/**
 * Copyright hemingway ( http://wonderfl.net/user/hemingway )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oYRR
 */

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    
    [SWF(frameRate = 60, width = 465, height = 465)]
    public class Main extends Sprite
    {
        public static var displayContext :Sprite;
        public static var displayOutput :Output;
        
        public function Main()
        {
            displayContext = new Sprite();
            displayOutput = new Output("iOS Scrollable Container");
            
            addEventListener(Event.ADDED_TO_STAGE, addedToStage);
        }
        
        public function addedToStage($e:*) :void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
            
            addChild(new Container(1, 62, 463, 1000));
            
            addChild(displayContext);
            addChild(displayOutput);
            init();   
        }
        
        public function init() :void
        {
            displayContext.graphics.clear();
            displayContext.graphics.lineStyle(1, 0, 1);
            displayContext.graphics.drawRect(0, 0, 464, 464);
            displayContext.graphics.lineStyle();
            displayContext.graphics.beginFill(0xA5A5A5);
            displayContext.graphics.drawRect(1, 1, 463, 31);
            displayContext.graphics.endFill();
            displayContext.graphics.beginFill(0xAFAFAF);
            displayContext.graphics.drawRect(1, 31, 463, 31);
            displayContext.graphics.endFill();
        }
    }
}

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;

class Container extends Sprite
{
    protected var _x :Number;
    protected var _y :Number;
    protected var _width :Number;
    protected var _height :Number;
    protected var _alpha :Number;
    
    public static var boxMargin:Number = 0;
    
    private var questionBox1:QuestionBox;
    private var questionBox2:QuestionBox;
    private var questionBox3:QuestionBox;
    private var questionBox4:QuestionBox;
    private var questionBox5:QuestionBox;
    
    private var prevMouse :Point = new Point();
    private var currMouse :Point = new Point();
    private var calcMouse :Point = new Point();
    
    public function Container($x:Number = 0, $y:Number = 0, $width:Number = 465, $height:Number = 465, $alpha:Number = 1)
    {
        _x = $x;
        _y = $y;
        _width = $width;
        _height = $height;
        _alpha = $alpha;
        
        questionBox1 = new QuestionBox(0, _x + (_width * 0.1), _y + 50, 375, 36, 0.5);
        questionBox2 = new QuestionBox(72, _x + (_width * 0.1), _y + 100, 375, 36, 0.5);
        questionBox3 = new QuestionBox(144, _x + (_width * 0.1), _y + 150, 375, 36, 0.5);
        questionBox4 = new QuestionBox(216, _x + (_width * 0.1), _y + 200, 375, 36, 0.5);
        questionBox5 = new QuestionBox(288, _x + (_width * 0.1), _y + 250, 375, 36, 0.5);
        
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }
    
    public function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

        init();
        
        addChild(questionBox1);
        addChild(questionBox2); 
        addChild(questionBox3);
        addChild(questionBox4);
        addChild(questionBox5);
        
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    
    public function init() :void
    {
        graphics.clear();
        graphics.beginFill(0xCCCCCC, _alpha);
        graphics.drawRect(_x, _y, _width, _height);
        graphics.endFill();
    }
    
    public function onMouseDown($e:MouseEvent) :void
    {
        removeEventListener(Event.ENTER_FRAME, normalizeOnFrame);
        
        prevMouse = new Point($e.stageX, $e.stageY);
        
        switch($e.target)
        {
            case this:
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            break;
        }

        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    }

    public function onMouseMove($e:MouseEvent) :void
    {
        currMouse = new Point($e.stageX, $e.stageY);
        calcMouse = currMouse.subtract(prevMouse);
        
        y = (y + calcMouse.y);
        
        prevMouse = currMouse;
    }
    
    public function onMouseUp($e:MouseEvent) :void
    {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        
        addEventListener(Event.ENTER_FRAME, normalizeOnFrame);
    }
    
    public function normalizeOnFrame($e:*) :void
    {
        if (y > 62)
        {
            y -= int((y - 55) / 5);        
        }
        else if ((height + y) < 465)
        {
            y += int((Math.abs(y) - (height - 472)) / 5);
        }else{
            removeEventListener(Event.ENTER_FRAME, normalizeOnFrame);
        }

        //Main.displayOutput.content = "'Container' Y: " + y + "\n'Container' Height: " + height;
    }

    public override function get x() :Number
    { return _x }
    public override function get y() :Number
    { return _y }
    public override function get width() :Number
    { return _width }
    public override function get height() :Number
    { return _height }
    public override function get alpha() :Number
    { return _alpha }
    
    public override function set x($:Number) :void
    { _x = $; init() }
    public override function set y($:Number) :void
    { _y = $; init() }
    public override function set width($:Number) :void
    { _width = $; init() }
    public override function set height($:Number) :void
    { _height = $; init() }
    public override function set alpha($:Number) :void
    { _alpha = $; init() }
}

class QuestionBox extends Sprite
{
    protected var _x :Number;
    protected var _y :Number;
    protected var _width :Number;
    protected var _height :Number;
    protected var _alpha :Number;
    protected var _limit :Number;
    
    private var answerBox:Sprite = new Sprite();
    
    private var answerBoxYMargin:Number = 0;
    private var answerBoxAlpha:Number = 0;
    
    private var questionOut:Output = new Output("questionBox");
    private var answersOut:Output = new Output("answerBox");
    
    public function QuestionBox($limit:Number, $x:Number = 0, $y:Number = 0, $width:Number = 465, $height:Number = 465, $alpha:Number = 1)
    {
        _x = $x;
        _y = $y;
        _width = $width;
        _height = $height;
        _alpha = $alpha;
        _limit = $limit;
        
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }
    
    public function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
        
        addChild(questionOut);
        addChild(answerBox);
        
        init();
        
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }    
    
    public function init() :void
    {
        graphics.clear();
        graphics.beginFill(0, _alpha);
        graphics.drawRect(_x, _y, _width, _height);
        graphics.endFill();
        graphics.lineStyle(1, 0xFFFFFF, 0.25);
        graphics.moveTo(_x + 90, _y + 1);
        graphics.lineTo(_x + 90, _y + 35);
        
        answerBox.graphics.clear();
        answerBox.graphics.beginFill(0, _alpha * 0.75);
        answerBox.graphics.drawRect(_x, (_y + 36), _width, 72); 
        answerBox.graphics.endFill();
        
        questionOut.x = (_x + 5);
        questionOut.y = (_y + 8);
    }
    
    public function onMouseDown($e:MouseEvent) :void
    {
        alpha = alpha * 0.5;
        
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    }
    
    public function onMouseUp($e:MouseEvent) :void
    {
        //answerBoxYMargin = 172;
        //answerBox.height = 172;
        alpha = alpha * 2;
        Container(parent).init();
        
        stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    }
    
    public override function get x() :Number
    { return _x }
    public override function get y() :Number
    { return _y }
    public override function get width() :Number
    { return _width }
    public override function get height() :Number
    { return _height }
    public override function get alpha() :Number
    { return _alpha }
    
    public override function set x($:Number) :void
    { _x = $; init() }
    public override function set y($:Number) :void
    { _y = $; init() }
    public override function set width($:Number) :void
    { _width = $; init() }
    public override function set height($:Number) :void
    { _height = $; init() }
    public override function set alpha($:Number) :void
    { _alpha = $; init() }
}
    
class Output extends TextField
{
    private var textFormat :TextFormat;
        
    protected var _x :Number;
    protected var _y :Number;
    protected var _font :String;
    protected var _content :String;
        
    public function Output($content:String, $x:Number = 1, $y:Number = 0, $font:String = "Helvetica")
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
            
        _x = $x;
        _y = $y;
        _font = $font;
        _content = $content;
            
        multiline = true;
        autoSize = "left";
        selectable = mouseEnabled = false;
        antiAliasType = AntiAliasType.ADVANCED;
    }
        
    public function _init() :void
    {
        x = _x;
        y = _y;
        text = _content;
            
        textFormat = new TextFormat(_font);
        setTextFormat(textFormat);
    }
        
    public function addedToStage($e:Event) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
            
        _init(); 
    }
        
    public function get font() :String
    { return _font }
    public function get nWidth() :Number
    { return width }
        
    public function set font($:String) :void
    { _font = $; _init() }
    public function set content($:String) :void
    { _content = $; _init() }
}