package {
import flash.events.*;
import flash.display.*;
public class RectContainer extends Sprite {
private var currentRect:Rect;
public var isDragging:Boolean;
public function RectContainer() {
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addChild(this);
}
private function onMouseDown(evt:MouseEvent):void {
if (!isDragging) {
currentRect = new Rect();
currentRect.container = this;
currentRect.x = evt.stageX;
currentRect.y = evt.stageY;
addChild(currentRect);
}
}
private function onMouseUp(evt:MouseEvent):void {
if (null != currentRect) {
currentRect = null;
}
}
private function onMouseMove(evt:MouseEvent):void {
if (evt.buttonDown && !isDragging) {
var dX:Number = evt.stageX - currentRect.x;
var dY:Number = evt.stageY - currentRect.y;
if (dX>0 && dY>0)
currentRect.draw(dX,dY);
}
}
}
}
package {
import flash.events.*;
import flash.display.*;
public class Rect extends Sprite {
public var color:uint = 0xff00ff;
public var container:RectContainer;
public function Rect() {
addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
}
public function draw(width:int,height:int):void {
graphics.clear();
graphics.lineStyle(1);
graphics.beginFill(color);
graphics.drawRect(0,0,width,height);
graphics.endFill();
}
private function onMouseMove(evt:MouseEvent):void {
}
private function onMouseUp(evt:MouseEvent):void {
this.stopDrag();
container.isDragging = false;
}
private function onMouseDown(evt:MouseEvent):void {
container.isDragging = true;
this.startDrag();
}
}
}
import mx.controls.ColorPicker;
...
var colorPicker:ColorPicker = new ColorPicker();
addChild(colorPicker);
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" applicationComplete="FlexMain.main()">
</mx:Application>
package {
import mx.controls.*;
import mx.core.*;
public class FlexMain {
public static function main ():void {
Application.application.addChild(new RectContainer());
}
}
}
package {
import flash.events.*;
import flash.display.*;
import mx.core.Application;
import mx.core.UIComponent;
public class RectContainer extends UIComponent {
private var currentRect:Rect;
public var isDragging:Boolean;
public function RectContainer() {
var stage:Stage = Application.application.stage;
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addChild(this);
}
// RectContainer.as
package {
import flash.events.*;
import flash.display.*;
import mx.core.UIComponent;
import mx.controls.ColorPicker;
import mx.events.ColorPickerEvent;
public class Rect extends UIComponent {
public var color:uint = 0xff00ff;
public var container:RectContainer;
public var colorPicker:ColorPicker = new ColorPicker();
public var _width:Number;
public var _height:Number;
public function Rect() {
addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
colorPicker.visible = false;
colorPicker.x = 0;
colorPicker.y = 0;
colorPicker.selectedColor = color;
addChild(colorPicker);
colorPicker.addEventListener(ColorPickerEvent.CHANGE,onColorChanged);
}
public function draw(width:int,height:int):void {
_width = width;
_height = height;
graphics.clear();
graphics.lineStyle(1);
graphics.beginFill(color);
graphics.drawRect(0,0,_width,_height);
graphics.endFill();
const minSize:Number = 15;
if (width>minSize && height > minSize) {
colorPicker.visible = true;
colorPicker.setActualSize(minSize,minSize);
}
}
private function onMouseMove(evt:MouseEvent):void {
}
private function onMouseUp(evt:MouseEvent):void {
this.stopDrag();
container.isDragging = false;
}
private function onMouseDown(evt:MouseEvent):void {
container.isDragging = true;
this.startDrag();
}
private function onColorChanged(evt:ColorPickerEvent):void {
color = evt.color;
draw(_width,_height);
}
}
}
Source: https://habr.com/ru/post/91468/