用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裏來的,結果似乎不大好,代碼的縮進格式遠不如記事本里好看了。沒時間整理格式,又對不住大家了 :-)

發佈了76 篇原創文章 · 獲贊 2 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章