代码重构---ToolTip类

      设计模式 2005-7-15 9:7
//抽象类
class fc.tip.Tip {
    private var _target:MovieClip;
    public function set target(t) {
        _target = t;
    }
    public function get target() {
        return _target;
    }
}
//tip显示的文本
import fc.graphics.Color;
import fc.tip.Tip;
class fc.tip.TipString extends Tip {
    //字体的颜色
    private var _color:Number = Color.black;
    private var _w:Number;
    private var _h:Number;
    public function TipString(_c) {
        _color = _c;
    }
    public function get width() {
        return _w;
    }
    public function get height() {
        return _h;
    }
    public function create(str) {
        var t = _target;
        t.createTextField("tip_str", 1, t._xmouse, t._ymouse, 0, 0);
        t.tip_str.autoSize = true;
        t.tip_str.text = str;
        t.tip_str.textColor = _color;
        t.tip_str.selectable = false;
        _w = t.tip_str._width;
        _h = t.tip_str._height;
    }
    public function clear() {
        _target.tip_str.removeTextField();
    }
}
//tip的背景框
import fc.tip.TipString;
import fc.graphics.*;
import fc.graphics.draw2d.*;
import fc.tip.Tip;
class fc.tip.TipShape extends Tip {
    private var _g:GDI;
    private var pen:Pen;
    private var brush:Brush;
    private var _shape:TipShape;
    public function TipShape(s:TipShape) {
        _g = GDI.getInstance();
        _shape = s;
    }
    public function create(w, h) {
        _g.target = _target;
        //默认为矩形提示框
        if (_shape == undefined) {
            draw(w, h);
        //其它样式的提示框
        } else {
            _shape.draw(w, h);
        }
    }
    //矩形提示框
    public function draw(w, h) {
        _g.fill(new SolidBrush(0xfafafa, 100), new Rect(0, 0, w, h));
        _g.line(new Pen(1, Color.black, 100), new Rect(0, 0, w, h));
    }
    public function clear() {
        _g.clear();
    }
}
//提示类
import fc.tip.TipString;
import fc.tip.TipShape;
import fc.utils.Delegate;
class fc.tip.ToolTip {
    //添加tip的mc
    private var _target:MovieClip;
    //放置tip的mc
    private var _tip:MovieClip;
    private var str:TipString;
    private var shape:TipShape;
    private var _moveID:Number;
    private var _time:Number = 0;
    public function ToolTip(stri:TipString, shap:TipShape) {
        str = stri;
        shape = shap;
        //默认tip的样式
        if (stri == undefined || shap == undefined) {
            str = new TipString();
            shape = new TipShape();
        }
    }
    public function set target(t) {
        _target = t;
    }
    public function get target() {
        return _target;
    }
    //target为要设置tip的mc的实例名,msg为要显示的tip
    public function setTip(t:MovieClip, s:String) {
        _target = t;
        var ins = this;
        target.onRollOver = function() {
            ins.create(s);
        };
        target.onRollOut = target.onReleaseOutside=function () {
            ins.clear();
        };
    }
    //创建tip
    private function create(s) {
        //创建放置空影片的mc
        var t = _target._parent;
        _tip = t.createEmptyMovieClip("theTip", t.getNextHighestDepth());
        _tip._visible = false;
        _tip._x = t._xmouse;
        _tip._y = t._ymouse;
        //创建文本
        str.target = _tip;
        str.create(s);
        //根据文本的宽和高创建背景图
        shape.target = _tip;
        shape.create(str.width, str.height);
        _moveID = setInterval(Delegate.create(this, moveTip), 80);
        _time = 0;
    }
    //移动tip
    private function moveTip() {
        //延迟出现tip
        if (_time>5 && !_tip._visible) {
            _tip._visible = true;
            clearInterval(_moveID);
        } else {
            _time++;
        }
        //鼠标的尺寸为12*18
        var t = _target._parent;
        var w = _tip._x+_tip._width;
        var h = _tip._y+_tip._height;
        if (w>Stage.width) {
            _tip._x = t._xmouse-_tip._width;
            _tip._y = t._ymouse+18;
        } else if (h>Stage.height) {
            _tip._y = t._ymouse-_tip._height;
            _tip._x = t._xmouse;
        } else {
            _tip._x = t._xmouse+12;
            _tip._y = t._ymouse+18;
        }
        //右下角还有bug
        updateAfterEvent();
    }
    //清除tip
    private function clear() {
        str.clear();
        shape.clear();
        _target._parent.theTip.removeMovieClip();
        clearInterval(_moveID);
    }
}
//使用方法
import fc.tip.ToolTip;
var t:ToolTip = new ToolTip();
t.setTip(mc, "flash入门书\n问:\"这本书");
t.setTip(mc1, "中国是一个大国");
t.setTip(mc3, "flash入门书\n问:\"这本");
t.setTip(mc4, "flash入门书\n问:\"这本")
//扩展形状的方法
import fc.graphics.*;
import fc.graphics.draw2d.*;
import fc.tip.TipShape;
//椭圆形形提示框
class fc.tip.CircleTip extends fc.tip.TipShape {
    //绘制椭圆形
    public function draw(w, h) {
        _g.fill(new SolidBrush(0xfafafa, 100), new Ellipse(w/2, h/2, w, h));
        _g.line(new Pen(1, Color.black, 100), new Ellipse(w/2, h/2, w, h));
    }
}
//用扩展的提示
import fc.tip.ToolTip;
import fc.tip.TipString;
import fc.tip.TipShape;
import fc.tip.CircleTip;
var t:ToolTip = new ToolTip(new TipString(0xff9900), new TipShape(new CircleTip()));
t.setTip(mc1, "中国是一个大国");
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}

注册5D通行证,立刻拥有自己的博客发表留言

用户:  密码: 匿名
内容:
加粗 下划线 文本颜色 居中 插入链接 建立Google搜索链接 插入图片 引用文本
 
汗 喜欢 骄傲的笑 哭 酷! 愤怒 黑线 什么? 猥琐 偷笑 无语 晕 吃惊 诱惑
smile frown redface biggrin wink rolleyes mad eek 禁止笑脸转换
zoom inzoom out
验证: checkcode 换一个