Flash StageManager v1.0.0

[ 2007-04-23 10:56:16 | Author: liuhuan ]
Font Size: Large | Medium | Small
StageManager is a tool for user to easily align objects in full size flash.
usage:
StageManager.init(scaleMode, alignMode, maxWidth, minWidth, maxHeight, minHeight);
StageManager.addAlignItem(McName, alignMode, X, Y,fitheight,fitwidth,heightgap,widthgap,customWidth,customHeight);

example:
StageManager.init("noScale", "TL", 4000, 400, 3000, 300);
StageManager.addAlignItem(Mctest1, "lt", 0, 0);

If want to make movieclip 100% width whenever Stage resizes:
StageManager.addAlignItem(Mctest1, "lt", 0, 0,false,true,0,0);

If want to make movieclip 100% height whenever Stage resizes:
StageManager.addAlignItem(Mctest1, "lt", 0, 0,true,false,0,0);

If the movieclip is very big while you want to show it in stage in a certain width and height, like 400*300:
StageManager.addAlignItem(Mctest1, "lt", 0, 0,false,false,0,0,400,300);

Download:
Click Here To Download

Example:
http://www.liuhuan.com/blog/uploads/200704/23_105731_example.swf

- www.liuhuan.com
- e-mail:duijiu@hotmail.com

Code:
Quote
dynamic class com.StageManager {
  var __items, __height, __width, __centerWidth, __centerHeight, __QWidth, __stageWidth, __stageHeight, __maxWidth, __minWidth, __maxHeight, __minHeight, __QHeight, __mode, __get__mode, __align, __get__align;
  function StageManager() {
    __second = 1;
    __easeType = "easeInOutExpo";
    __items = new Array();
    Stage.addListener(this);
    this.onResize = function() {
      this.setProps();
      this.alignClips();
    };
  }
  public function init(m, a, maxW, minW, maxH, minH) {
    this.setMode(m || "noScale");
    this.setAlign(a || "TL");
    this.setHeight(maxH || 960, minH || 480);
    this.setWidth(maxW || 1440, minW || 960);
  }
  public function alignClips() {
    var i = 0;
    while (i<__items.length) {
      var j = __items[i];
      this.alignItem(j);
      i++;
    }
  }
  public function alignItem(o) {
    this.setXYPos(o.item, o.pos, o.X, o.Y, o.easeType, o.easeSec, o.W, o.H, o.firsttime, o.delay, o.fs, o.oW, o.oH, o.autoX, o.autoY);
    if (o.firsttime) {
      o.firsttime = false;
    }
    if (o.HFit) {
      this.fitHPos(o.item, o.pos, o.HFitParams);
    }
    if (o.VFit) {
      this.fitVPos(o.item, o.pos, o.VFitParams);
    }
  }
  public function fitHPos(m, pos, H) {
    m._height = __height-H;
  }
  public function fitVPos(m, pos, W) {
    m._width = __width-W;
  }
  public function setXYPos(m, pos, X, Y, easeType, easeSec, W, H, firstime, delay, fs, oW, oH, autoX, autoY) {
    var k = W || (m._width);
    var l = H || (m._height);
    if ((((pos == "tl") || (pos == "TL")) || (pos == "lt")) || (pos == "LT")) {
      newX = X;
      newY = Y;
    } else if ((((pos == "tc") || (pos == "TC")) || (pos == "ct")) || (pos == "CT")) {
      newX = (__centerWidth+X)-(k/2);
      newY = Y;
    } else if ((((pos == "tr") || (pos == "TR")) || (pos == "rt")) || (pos == "RT")) {
      newX = (__width-X)-k;
      newY = Y;
    } else if ((((pos == "cr") || (pos == "CR")) || (pos == "rc")) || (pos == "RC")) {
      newX = (__width-X)-k;
      newY = (__centerHeight-Y)-(l/2);
    } else if ((((pos == "br") || (pos == "BR")) || (pos == "rb")) || (pos == "RB")) {
      newX = (__width-X)-k;
      newY = (__height-Y)-l;
    } else if ((((pos == "cb") || (pos == "CB")) || (pos == "bc")) || (pos == "BC")) {
      newX = (__centerWidth+X)-(k/2);
      newY = (__height-Y)-l;
    } else if ((((pos == "bl") || (pos == "BL")) || (pos == "lb")) || (pos == "LB")) {
      newX = X;
      newY = (__height-Y)-l;
    } else if ((((pos == "cl") || (pos == "CL")) || (pos == "lc")) || (pos == "LC")) {
      newX = X;
      newY = (__centerHeight-Y)-(l/2);
    } else if (((pos == "center") || (pos == "c")) || (pos == "C")) {
      newX = (__centerWidth+X)-(k/2);
      newY = (__centerHeight-Y)-(l/2);
    } else if ((((pos == "qlc") || (pos == "QLC")) || (pos == "clq")) || (pos == "CLQ")) {
      newX = (__QWidth+X)-(k/2);
      newY = (__centerHeight-Y)-(l/2);
    } else if ((((pos == "qlb") || (pos == "QLB")) || (pos == "blq")) || (pos == "BLQ")) {
      newX = (__QWidth+X)-(k/2);
      newY = (__height-Y)-l;
    } else if ((((pos == "QLT") || (pos == "qlt")) || (pos == "TLQ")) || (pos == "tlq")) {
      newX = (__QWidth+X)-(k/2);
      newY = Y;
    } else if ((((pos == "qrc") || (pos == "QRC")) || (pos == "CRQ")) || (pos == "crq")) {
      newX = ((__QWidth*3)+X)-(k/2);
      newY = (__centerHeight-Y)-(l/2);
    } else if ((((pos == "trq") || (pos == "TRQ")) || (pos == "qrt")) || (pos == "QRT")) {
      newX = ((__QWidth*3)+X)-(k/2);
      newY = Y;
    } else if ((((pos == "qrb") || (pos == "QRB")) || (pos == "brq")) || (pos == "BRQ")) {
      newX = ((__QWidth*3)+X)-(k/2);
      newY = (__height-Y)-l;
    }
    if (autoX != null) {
      if (autoX == 999999) {
        newX = __stageWidth-k;
      } else {
        newX = ((__stageWidth-k)*autoX)/(1+autoX);
      }
    }
    if (autoY != null) {
      if (autoY == 999999) {
        newY = __stageHeight-l;
      } else {
        newY = ((__stageHeight-l)*autoY)/(1+autoY);
      }
    }
    newX = Math.round(newX);
    newY = Math.round(newY);
    m.newX = newX;
    m.newY = newY;
    if (easeType) {
      myet = easeType;
    } else {
      myet = __easeType;
    }
    if (easeSec != null) {
      myes = easeSec;
    } else {
      myes = __second;
    }
    if (firstime) {
      mydelay = delay;
    } else {
      mydelay = 0;
    }
    if (fs) {
      if (__stageWidth/__stageHeight>=oW/oH) {
        m._width = __stageWidth;
        m._height = m._width*oH/oW;
      } else {
        m._height = __stageHeight;
        m._width = m._height*oW/oH;
      }
      m._x = 0;
      m._y = 0;
    } else {
      m._x = newX;
      m._y = newY;
    }
  }
  public function removeAlignItem(item) {
    var i = 0;
    while (i<__items.length) {
      var j = __items[i];
      if (j.item == item) {
        __items.splice(i, 1);
        break;
      }
      i++;
    }
  }
  public function remove(item) {
    this.removeAlignItem(item);
  }
  public function addAlignItem(item, pos, X, Y, cw, ch, HF, VF, HFP, VFP, fullscreen) {
    var i = new Object();
    i.item = item;
    i.pos = pos;
    if (cw != null) {
      i.W = cw;
    }
    if (ch != null) {
      i.H = ch;
    }
    if (X != null) {
      i.X = X;
    } else {
      propX1 = item._x;
      if (cw != null) {
        propX2 = Stage.width-item._x-cw;
      } else {
        propX2 = Stage.width-item._x-item._width;
      }
      if (propX2<=0.01) {
        i.autoX = 999999;
      } else {
        i.autoX = propX1/propX2;
      }
    }
    if (Y != null) {
      i.Y = Y;
    } else {
      propY1 = item._y;
      if (ch != null) {
        propY2 = Stage.height-item._y-ch;
      } else {
        propY2 = Stage.height-item._y-item._height;
      }
      if (propY2<=0.01) {
        i.autoY = 999999;
      } else {
        i.autoY = propY1/propY2;
      }
    }
    i.Y = Y;
    i.oW = item._width;
    i.oH = item._height;
    i.firsttime = true;
    if (HF) {
      i.HFit = HF;
      i.HFitParams = HFP;
    }
    if (VF) {
      i.VFit = VF;
      i.VFitParams = VFP;
    }
    if (fullscreen) {
      i.fs = fullscreen;
    }
    __items.push(i);
    this.setProps();
    var m = 0;
    while (m<__items.length) {
      var j = __items[m];
      if (__items[m].item == item) {
        this.alignItem(j);
        break;
      }
      m++;
    }
  }
  public function setProps() {
    __stageWidth = Stage.width;
    __stageHeight = Stage.height;
    __maxWidth = Math.max(__stageWidth, __minWidth);
    __maxHeight = Math.max(__stageHeight, __minHeight);
    __width = Math.min(__maxWidth, Math.max(__stageWidth, __minWidth));
    __height = Math.min(__maxHeight, Math.max(__stageHeight, __minHeight));
    __centerWidth = Math.round(__width/2);
    __centerHeight = Math.round(__height/2);
    __QWidth = Math.round(__centerWidth/2);
    __QHeight = Math.round(__centerHeight/2);
  }
  public function setHeight(max, min) {
    __maxHeight = Stage.height;
    __minHeight = min;
  }
  public function setWidth(max, min) {
    __maxWidth = Stage.width;
    __minWidth = min;
  }
  public function setMode(m) {
    __mode = m;
    Stage.scaleMode = m;
  }
  public function setAlign(a) {
    __align = a;
    Stage.align = a;
  }
  public function set mode(m) {
    setMode(m);
  }
  public function set align(a) {
    setAlign(a);
  }
  public function get W() {
    return (__width);
  }
  public function get H() {
    return (__height);
  }
  public function get QW() {
    return (__QWidth);
  }
  public function get QH() {
    return (__QHeight);
  }
}
[Last Modified By liuhuan, at 2007-05-27 08:49:22]
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=754

View Mode: Show All | Comments: 4 | Trackbacks: 0 | Toggle Order | Views: 22382
Quote pocong
[ 2007-04-23 20:00:59 ]
Hmm, i think that StageManager by Denis Kolyako is more useful and it has better functionality, than yours. StageManager by Denis Kolyako is a AS2 class, yours is AS1 and it creates some variables in root, that's no good.
Quote liuhuan
[ 2007-04-23 21:52:23 ]
Thanks a lot for your suggestion pocong. Yes, Denis Kolyako's is better than mine. I just wanna to create a simple enough component for most simple usage. [razz]
Quote pocong
[ 2007-04-23 23:54:21 ]
BTW, good work! I have a question: the X and Y arguments in addAlignItem syntax is offset coords? Your example generates some traces, which i can't remove, coz clip already compiled. In other align modes (i.e. BR) StageManager works incorrect, i think.
Quote liuhuan
[ 2007-04-24 01:16:29 ]
Hi pocong, i'm sorry that there's manything wrong in this stagemanager. I have corrected them and reuploaded the component.
For the X and Y arguments in addAlignItem syntax, it's the realative offset for the mc.
For example: StageManager.addAlignItem(Mctest1, "lt", 30, 30);
Mctest1 will be aligned to left top, but 30*30 pixels from the very left and top point.(Mctest1._x = X;)
However, if we align to right side, such as rt, rc and rb, then the X will be the distance between the Stage border and the mc.(Mctest1._x = Stage.width-X;)

There should be many more mistakes:(

You can't post comment on this article.