用Flash MX 2004自制调色版和配色组件(五)

王咏刚,2005年4月

颜色按钮组件对应的是ColorCombo类,它负责下拉或收回调色版,负责向应用程序发送change事件。它的代码如下:


import mx.core.UIComponent;
import mx.controls.Button;
import wix.*;

[IconFile("ColorCombo.png")]
[InspectableList("color", "visible", "enabled")]
[Event("change")]
class wix.ColorCombo extends UIComponent
{
 static var version:String = "1.0.0";
 static var symbolName:String = "ColorCombo";
 static var symbolOwner:Object = Object(wix.ColorCombo);
 var className:String = "ColorCombo";
 
 private var curDepth:Number = 950;
 
 private var picker:ColorPicker;
 private var box:MovieClip;
 private var up:MovieClip;
 private var over:MovieClip;
 private var down:MovieClip;
 private var state:Number = 0;
 private var drop:Boolean = false;
 private var boundingBox_mc:MovieClip;
 private var buttonClose:mx.controls.Button;
 
 private var _color:Number = 0x000000;
 [Inspectable(defaultValue=0x000000, type="Color")]
 public function get color():Number {
  return _color;
 }
 public function set color(newColor:Number) {
  if (enabled) {
   _color = ColorMan.toInteger(newColor, 0, 0xFFFFFF);
   picker.color = _color;
   update();
  }
 }
 
 function init(Void):Void {
  super.init();
 }
 
 function size(Void):Void {
  super.size();
  box._width = this.width - 2;
  box._height = this.height - 2;
  up._width = this.width;
  over._width = this.width;
  down._width = this.width;
  up._height = this.height;
  over._height = this.height;
  down._height = this.height;
  boundingBox_mc._width = this.width;
  boundingBox_mc._height = this.height;
  picker._y = box._height + 3; 
  buttonClose._y = picker._y + 7;
 }
 
 function draw(Void):Void {
  super.draw();
  size();
 }
 
 public function createChildren(Void):Void
 {  
  super.createChildren();
  createObject("ColorBox", "box", curDepth++);
  box._x = box._y = 1;
  box.useHandCursor = true;
  box.onPress = onPressButton;
  box.onRollOver = onRollOverButton;
  box.onRollOut = onRollOutButton;
  box.onDragOut = onRollOutButton;
  box.onRelease = onReleaseButton;
  
  createObject("ButtonUp", "up", curDepth++);
  createObject("ButtonOver", "over", curDepth++);
  createObject("ButtonDown", "down", curDepth++);
  up._x = up._y = over._x = over._y = down._x = down._y = 0;

  createObject("ColorPicker", "picker", curDepth++);
  picker._x = 0; picker._y = 51;
  picker.color = _color;
  var o = new Object();
  o.change = onPickerChange;
  picker.addEventListener("change", o);
  
  createClassObject(mx.controls.Button, "buttonClose", curDepth++);
  buttonClose._x = 7; buttonClose._y = 58; buttonClose.setSize(22, 22);
  buttonClose.label = "X";
  o = new Object();
  o.click = onPressButtonClose;
  buttonClose.addEventListener("click", o);
  
  viewButton();
  viewPicker();
  
  update();
 }
 
 public function onPressButton() {
  if (_parent.enabled) {
   _parent.drop = !_parent.drop;
   _parent.viewPicker();
   _parent.state = 2;
   _parent.viewButton();
  }
 }
 
 public function onReleaseButton() {
  if (_parent.enabled) {
   if (_parent.box.hitTest(_root._xmouse, _root._ymouse))
    _parent.state = 1;
   else
    _parent.state = 0;
   _parent.viewButton();
  }
 }
 
 public function onRollOverButton() {
  if (_parent.enabled) {
   _parent.state = 1;
   _parent.viewButton();
  }
 }
 
 public function onRollOutButton() {
  if (_parent.enabled) {
   _parent.state = 0;
   _parent.viewButton();
  }
 }
 
 public function onPressButtonClose(eventObj) {
  var p = eventObj.target._parent;
  if (p.enabled) {
   p.drop = false;
   p.viewPicker();
  }
 }
 
 public function viewPicker() {
  if (enabled) {
   picker.visible = drop;
   picker.enable(drop);
   buttonClose.visible = drop;
   buttonClose.enabled = drop; 
  }
 }
 
 public function viewButton() {
  switch (state)
  {
  case 0:
   up._visible = true;
   over._visible = false;
   down._visible = false;
   break;
  case 1:
   up._visible = false;
   over._visible = true;
   down._visible = false;
   break;
  case 2:
   up._visible = false;
   over._visible = false;
   down._visible = true;
   break;
  }
 }
 
 public function onPickerChange(eventObj) {
  if (eventObj.target._parent.enabled) {
   eventObj.target._parent._color = eventObj.target.color;
   eventObj.target._parent.update();
  }
 }
 
 public function update(Void):Void {
  if (enabled) {
   var c = new Color(box);
   c.setRGB(_color);
   dispatchEvent({type:"change", target:this});
  }
 } 
}

好了,重要的代码都在这里了。代码写得不好,仅供喜欢用Flash MX 2004开发应用又不熟悉组件编程的程序员参考。

——就到这里吧。还有,我是直接从记事本复制粘贴代码到这个blog里来的,结果似乎不大好,代码的缩进格式远不如记事本里好看了。没时间整理格式,又对不住大家了 :-)

原文地址:https://www.cnblogs.com/xiaomaohai/p/6157222.html