四角を描いて動かす(コンテキストメニュー改造)

by ashitaka
------------------------------------------------
クリック&ドラッグで四角図形を描けます
図形はドラッグで動かすことができます
図形の上で右クリックすると削除を選ぶことができます
------------------------------------------------
♥1 | Line 78 | Modified 2010-02-23 12:57:58 | MIT License
play

ActionScript3 source code

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

// forked from ashitaka's 四角を描いて動かす
//------------------------------------------------
// クリック&ドラッグで四角図形を描けます
// 図形はドラッグで動かすことができます
// 図形の上で右クリックすると削除を選ぶことができます
//------------------------------------------------
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    [SWF(width=465, height=465, backgroundColor=0xffffff)]
    import flash.geom.Matrix;
    public class FlashTest extends Sprite {

        private var _x:Number;
        private var _y:Number;
        private var _rect:Rect;

        public function FlashTest() {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
        
        public function onMouseDown(e:MouseEvent):void {
            _x = e.stageX;
            _y = e.stageY;
            
            _rect = new Rect()
            addChild(_rect);
        }

        public function onMouseMove(e:MouseEvent):void {
            if(!_rect) return;
            
            _rect.graphics.clear();
            _rect.graphics.lineStyle(2, 0x000000, 0.5);
            _rect.graphics.moveTo(_x, _y);
            _rect.graphics.lineTo(e.stageX, _y);
            _rect.graphics.lineTo(e.stageX, e.stageY);
            _rect.graphics.lineTo(_x, e.stageY);
            _rect.graphics.lineTo(_x, _y);
        }

        public function onMouseUp(e:MouseEvent):void {
            if(!_rect) return;
            _rect.graphics.clear();
            _rect.graphics.lineStyle(0);
            _rect.graphics.beginFill(0xffffff * Math.random(), 0.8);
            _rect.graphics.drawRect(_x, _y, e.stageX - _x, e.stageY - _y);
            _rect.graphics.endFill();
            _rect = null;
        }
    }
}

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.ContextMenuEvent;
import flash.filters.DropShadowFilter;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
class Rect extends MovieClip{
    
    public function Rect() {
        buttonMode = true;

        // イベントの設定
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
        addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
        addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        
        // コンテキストメニューの設定
        contextMenu = new ContextMenu();
        contextMenu.hideBuiltInItems()
        var item:ContextMenuItem = new ContextMenuItem("削除");
        item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,onMenuItemDelete);
        contextMenu.customItems.push(item);
    }

    private function onMouseDown(e:MouseEvent):void {
        startDrag();
        e.stopImmediatePropagation();
    }
    private function onMouseOver(e:MouseEvent):void {
        filters = [new DropShadowFilter()];
    }
    private function onMouseOut(e:MouseEvent):void {
        filters = null;
    }
    private function onMouseUp(e:MouseEvent):void {
        stopDrag();
    }
    
    private function onMenuItemDelete(e:ContextMenuEvent): void {
        parent.removeChild(this); 
    }
}