123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187 |
- using System;
- using UnityEngine;
- using FairyGUI.Utils;
- namespace FairyGUI
- {
- public class GObject : EventDispatcher
- {
- /// <summary>
- /// GObject的id,仅作为内部使用。与name不同,id值是不会相同的。
- /// id is for internal use only.
- /// </summary>
- public string id { get; private set; }
- /// <summary>
- /// Name of the object.
- /// </summary>
- public string name;
- /// <summary>
- /// User defined data.
- /// </summary>
- public object data;
- /// <summary>
- /// The source width of the object.
- /// </summary>
- public int sourceWidth;
- /// <summary>
- /// The source height of the object.
- /// </summary>
- public int sourceHeight;
- /// <summary>
- /// The initial width of the object.
- /// </summary>
- public int initWidth;
- /// <summary>
- /// The initial height of the object.
- /// </summary>
- public int initHeight;
- /// <summary>
- ///
- /// </summary>
- public int minWidth;
- /// <summary>
- ///
- /// </summary>
- public int maxWidth;
- /// <summary>
- ///
- /// </summary>
- public int minHeight;
- /// <summary>
- ///
- /// </summary>
- public int maxHeight;
- /// <summary>
- /// Relations Object.
- /// </summary>
- public Relations relations { get; private set; }
- /// <summary>
- /// Restricted range of dragging.
- /// </summary>
- public Rect? dragBounds;
- /// <summary>
- /// Parent object.
- /// </summary>
- public GComponent parent { get; private set; }
- /// <summary>
- /// Lowlevel display object.
- /// </summary>
- public DisplayObject displayObject { get; protected set; }
- /// <summary>
- /// 当前全局正在被拖动的对象
- /// </summary>
- public static GObject draggingObject { get; private set; }
- /// <summary>
- ///
- /// </summary>
- public PackageItem packageItem;
- float _x;
- float _y;
- float _z;
- float _pivotX;
- float _pivotY;
- bool _pivotAsAnchor;
- float _alpha;
- float _rotation;
- float _rotationX;
- float _rotationY;
- bool _visible;
- bool _internalVisible;
- bool _handlingController;
- bool _touchable;
- bool _grayed;
- bool _draggable;
- float _scaleX;
- float _scaleY;
- int _sortingOrder;
- string _tooltips;
- GGroup _group;
- GearBase[] _gears;
- EventListener _onClick;
- EventListener _onRightClick;
- EventListener _onTouchBegin;
- EventListener _onTouchMove;
- EventListener _onTouchEnd;
- EventListener _onRollOver;
- EventListener _onRollOut;
- EventListener _onAddedToStage;
- EventListener _onRemovedFromStage;
- EventListener _onKeyDown;
- EventListener _onClickLink;
- EventListener _onPositionChanged;
- EventListener _onSizeChanged;
- EventListener _onDragStart;
- EventListener _onDragMove;
- EventListener _onDragEnd;
- EventListener _onGearStop;
- EventListener _onFocusIn;
- EventListener _onFocusOut;
- internal protected bool underConstruct;
- internal float _width;
- internal float _height;
- internal float _rawWidth;
- internal float _rawHeight;
- internal bool _gearLocked;
- internal float _sizePercentInGroup;
- internal bool _disposed;
- internal GTreeNode _treeNode;
- internal static uint _gInstanceCounter;
- public GObject()
- {
- _width = 0;
- _height = 0;
- _alpha = 1;
- _visible = true;
- _touchable = true;
- _scaleX = 1;
- _scaleY = 1;
- _internalVisible = true;
- id = "_n" + _gInstanceCounter++;
- name = string.Empty;
- CreateDisplayObject();
- relations = new Relations(this);
- _gears = new GearBase[10];
- }
- /// <summary>
- /// Dispatched when the object or its child was clicked.
- /// </summary>
- public EventListener onClick
- {
- get { return _onClick ?? (_onClick = new EventListener(this, "onClick")); }
- }
- /// <summary>
- /// Dispatched when the object or its child was clicked by right mouse button. Web only.
- /// </summary>
- public EventListener onRightClick
- {
- get { return _onRightClick ?? (_onRightClick = new EventListener(this, "onRightClick")); }
- }
- /// <summary>
- /// Dispatched when the finger touched the object or its child just now.
- /// </summary>
- public EventListener onTouchBegin
- {
- get { return _onTouchBegin ?? (_onTouchBegin = new EventListener(this, "onTouchBegin")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onTouchMove
- {
- get { return _onTouchMove ?? (_onTouchMove = new EventListener(this, "onTouchMove")); }
- }
- /// <summary>
- /// Dispatched when the finger was lifted from the screen or from the mouse button.
- /// </summary>
- public EventListener onTouchEnd
- {
- get { return _onTouchEnd ?? (_onTouchEnd = new EventListener(this, "onTouchEnd")); }
- }
- /// <summary>
- /// The cursor or finger hovers over an object.
- /// </summary>
- public EventListener onRollOver
- {
- get { return _onRollOver ?? (_onRollOver = new EventListener(this, "onRollOver")); }
- }
- /// <summary>
- /// The cursor or finger leave an object.
- /// </summary>
- public EventListener onRollOut
- {
- get { return _onRollOut ?? (_onRollOut = new EventListener(this, "onRollOut")); }
- }
- /// <summary>
- /// Dispatched when the object was added to the stage.
- /// </summary>
- public EventListener onAddedToStage
- {
- get { return _onAddedToStage ?? (_onAddedToStage = new EventListener(this, "onAddedToStage")); }
- }
- /// <summary>
- /// Dispatched when the object was removed from the stage.
- /// </summary>
- public EventListener onRemovedFromStage
- {
- get { return _onRemovedFromStage ?? (_onRemovedFromStage = new EventListener(this, "onRemovedFromStage")); }
- }
- /// <summary>
- /// Dispatched on key pressed when the object is in focus.
- /// </summary>
- public EventListener onKeyDown
- {
- get { return _onKeyDown ?? (_onKeyDown = new EventListener(this, "onKeyDown")); }
- }
- /// <summary>
- /// Dispatched when links in the object or its child was clicked.
- /// </summary>
- public EventListener onClickLink
- {
- get { return _onClickLink ?? (_onClickLink = new EventListener(this, "onClickLink")); }
- }
- /// <summary>
- /// Dispatched when the object was moved.
- /// </summary>
- public EventListener onPositionChanged
- {
- get { return _onPositionChanged ?? (_onPositionChanged = new EventListener(this, "onPositionChanged")); }
- }
- /// <summary>
- /// Dispatched when the object was resized.
- /// </summary>
- public EventListener onSizeChanged
- {
- get { return _onSizeChanged ?? (_onSizeChanged = new EventListener(this, "onSizeChanged")); }
- }
- /// <summary>
- /// Dispatched when drag start.
- /// </summary>
- public EventListener onDragStart
- {
- get { return _onDragStart ?? (_onDragStart = new EventListener(this, "onDragStart")); }
- }
- /// <summary>
- /// Dispatched when dragging.
- /// </summary>
- public EventListener onDragMove
- {
- get { return _onDragMove ?? (_onDragMove = new EventListener(this, "onDragMove")); }
- }
- /// <summary>
- /// Dispatched when drag end.
- /// </summary>
- public EventListener onDragEnd
- {
- get { return _onDragEnd ?? (_onDragEnd = new EventListener(this, "onDragEnd")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onGearStop
- {
- get { return _onGearStop ?? (_onGearStop = new EventListener(this, "onGearStop")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onFocusIn
- {
- get { return _onFocusIn ?? (_onFocusIn = new EventListener(this, "onFocusIn")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onFocusOut
- {
- get { return _onFocusOut ?? (_onFocusOut = new EventListener(this, "onFocusOut")); }
- }
- /// <summary>
- /// The x coordinate of the object relative to the local coordinates of the parent.
- /// </summary>
- public float x
- {
- get { return _x; }
- set
- {
- SetPosition(value, _y, _z);
- }
- }
- /// <summary>
- /// The y coordinate of the object relative to the local coordinates of the parent.
- /// </summary>
- public float y
- {
- get { return _y; }
- set
- {
- SetPosition(_x, value, _z);
- }
- }
- /// <summary>
- /// The z coordinate of the object relative to the local coordinates of the parent.
- /// </summary>
- public float z
- {
- get { return _z; }
- set
- {
- SetPosition(_x, _y, value);
- }
- }
- /// <summary>
- /// The x and y coordinates of the object relative to the local coordinates of the parent.
- /// </summary>
- public Vector2 xy
- {
- get { return new Vector2(_x, _y); }
- set { SetPosition(value.x, value.y, _z); }
- }
- /// <summary>
- /// The x,y,z coordinates of the object relative to the local coordinates of the parent.
- /// </summary>
- public Vector3 position
- {
- get { return new Vector3(_x, _y, _z); }
- set { SetPosition(value.x, value.y, value.z); }
- }
- /// <summary>
- /// change the x and y coordinates of the object relative to the local coordinates of the parent.
- /// </summary>
- /// <param name="xv">x value.</param>
- /// <param name="yv">y value.</param>
- public void SetXY(float xv, float yv)
- {
- SetPosition(xv, yv, _z);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="xv"></param>
- /// <param name="yv"></param>
- /// <param name="topLeft"></param>
- public void SetXY(float xv, float yv, bool topLeftValue)
- {
- if (_pivotAsAnchor)
- SetPosition(xv + _pivotX * _width, yv + _pivotY * _height, _z);
- else
- SetPosition(xv, yv, _z);
- }
- /// <summary>
- /// change the x,y,z coordinates of the object relative to the local coordinates of the parent.
- /// </summary>
- /// <param name="xv">x value.</param>
- /// <param name="yv">y value.</param>
- /// <param name="zv">z value.</param>
- public void SetPosition(float xv, float yv, float zv)
- {
- if (_x != xv || _y != yv || _z != zv)
- {
- float dx = xv - _x;
- float dy = yv - _y;
- _x = xv;
- _y = yv;
- _z = zv;
- HandlePositionChanged();
- if (this is GGroup)
- ((GGroup)this).MoveChildren(dx, dy);
- UpdateGear(1);
- if (parent != null && !(parent is GList))
- {
- parent.SetBoundsChangedFlag();
- if (_group != null)
- _group.SetBoundsChangedFlag(true);
- DispatchEvent("onPositionChanged", null);
- }
- if (draggingObject == this && !sUpdateInDragging)
- sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
- }
- }
- [Obsolete("Use UIConfig.makePixelPerfect or DisplayObject.pixelPerfect")]
- public bool pixelSnapping
- {
- get { return false; }
- set { }
- }
- /// <summary>
- /// Set the object in middle of the parent or GRoot if the parent is not set.
- /// </summary>
- public void Center()
- {
- Center(false);
- }
- /// <summary>
- /// Set the object in middle of the parent or GRoot if the parent is not set.
- /// </summary>
- /// <param name="restraint">Add relations to maintain the center state.</param>
- public void Center(bool restraint)
- {
- GComponent r;
- if (parent != null)
- r = parent;
- else
- r = this.root;
- this.SetXY((int)((r.width - this.width) / 2), (int)((r.height - this.height) / 2), true);
- if (restraint)
- {
- this.AddRelation(r, RelationType.Center_Center);
- this.AddRelation(r, RelationType.Middle_Middle);
- }
- }
- /// <summary>
- /// 设置对象为全屏大小(逻辑屏幕)。
- /// </summary>
- public void MakeFullScreen()
- {
- this.SetSize(GRoot.inst.width, GRoot.inst.height);
- }
- /// <summary>
- /// The width of the object in pixels.
- /// </summary>
- public float width
- {
- get
- {
- return _width;
- }
- set
- {
- SetSize(value, _rawHeight);
- }
- }
- /// <summary>
- /// The height of the object in pixels.
- /// </summary>
- public float height
- {
- get
- {
- return _height;
- }
- set
- {
- SetSize(_rawWidth, value);
- }
- }
- /// <summary>
- /// The size of the object in pixels.
- /// </summary>
- public Vector2 size
- {
- get { return new Vector2(width, height); }
- set { SetSize(value.x, value.y); }
- }
- /// <summary>
- /// actualWidth = width * scalex
- /// </summary>
- public float actualWidth
- {
- get { return this.width * _scaleX; }
- }
- /// <summary>
- /// actualHeight = height * scaleY
- /// </summary>
- public float actualHeight
- {
- get { return this.height * _scaleY; }
- }
- /// <summary>
- /// Change size.
- /// </summary>
- /// <param name="wv">Width value.</param>
- /// <param name="hv">Height value.</param>
- public void SetSize(float wv, float hv)
- {
- SetSize(wv, hv, false);
- }
- /// <summary>
- /// Change size.
- /// </summary>
- /// <param name="wv">Width value.</param>
- /// <param name="hv">Height value.</param>
- /// <param name="ignorePivot">If pivot is set, the object's positon will change when its size change. Set ignorePivot=true to keep the position.</param>
- public void SetSize(float wv, float hv, bool ignorePivot)
- {
- if (_rawWidth != wv || _rawHeight != hv)
- {
- _rawWidth = wv;
- _rawHeight = hv;
- if (wv < minWidth)
- wv = minWidth;
- else if (maxWidth > 0 && wv > maxWidth)
- wv = maxWidth;
- if (hv < minHeight)
- hv = minHeight;
- else if (maxHeight > 0 && hv > maxHeight)
- hv = maxHeight;
- float dWidth = wv - _width;
- float dHeight = hv - _height;
- _width = wv;
- _height = hv;
- HandleSizeChanged();
- if (_pivotX != 0 || _pivotY != 0)
- {
- if (!_pivotAsAnchor)
- {
- if (!ignorePivot)
- SetXY(_x - _pivotX * dWidth, _y - _pivotY * dHeight);
- else
- HandlePositionChanged();
- }
- else
- HandlePositionChanged();
- }
- if (this is GGroup)
- ((GGroup)this).ResizeChildren(dWidth, dHeight);
- UpdateGear(2);
- if (parent != null)
- {
- relations.OnOwnerSizeChanged(dWidth, dHeight, _pivotAsAnchor || !ignorePivot);
- parent.SetBoundsChangedFlag();
- if (_group != null)
- _group.SetBoundsChangedFlag();
- }
- DispatchEvent("onSizeChanged", null);
- }
- }
- protected void SetSizeDirectly(float wv, float hv)
- {
- _rawWidth = wv;
- _rawHeight = hv;
- if (wv < 0)
- wv = 0;
- if (hv < 0)
- hv = 0;
- _width = wv;
- _height = hv;
- }
- /// <summary>
- ///
- /// </summary>
- public float xMin
- {
- get
- {
- return _pivotAsAnchor ? (_x - _width * _pivotX) : _x;
- }
- set
- {
- if (_pivotAsAnchor)
- SetPosition(value + _width * _pivotX, _y, _z);
- else
- SetPosition(value, _y, _z);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float yMin
- {
- get
- {
- return _pivotAsAnchor ? (_y - _height * _pivotY) : _y;
- }
- set
- {
- if (_pivotAsAnchor)
- SetPosition(_x, value + _height * _pivotY, _z);
- else
- SetPosition(_x, value, _z);
- }
- }
- /// <summary>
- /// The horizontal scale factor. '1' means no scale, cannt be negative.
- /// </summary>
- public float scaleX
- {
- get { return _scaleX; }
- set
- {
- SetScale(value, _scaleY);
- }
- }
- /// <summary>
- /// The vertical scale factor. '1' means no scale, cannt be negative.
- /// </summary>
- public float scaleY
- {
- get { return _scaleY; }
- set
- {
- SetScale(_scaleX, value);
- }
- }
- /// <summary>
- /// The scale factor.
- /// </summary>
- public Vector2 scale
- {
- get { return new Vector2(_scaleX, _scaleY); }
- set { SetScale(value.x, value.y); }
- }
- /// <summary>
- /// Change the scale factor.
- /// </summary>
- /// <param name="wv">The horizontal scale factor.</param>
- /// <param name="hv">The vertical scale factor</param>
- public void SetScale(float wv, float hv)
- {
- if (_scaleX != wv || _scaleY != hv)
- {
- _scaleX = wv;
- _scaleY = hv;
- HandleScaleChanged();
- UpdateGear(2);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 skew
- {
- get
- {
- if (displayObject != null)
- return displayObject.skew;
- else
- return Vector2.zero;
- }
- set
- {
- if (displayObject != null)
- displayObject.skew = value;
- }
- }
- /// <summary>
- /// The x coordinate of the object's origin in its own coordinate space.
- /// </summary>
- public float pivotX
- {
- get { return _pivotX; }
- set
- {
- SetPivot(value, _pivotY, _pivotAsAnchor);
- }
- }
- /// <summary>
- /// The y coordinate of the object's origin in its own coordinate space.
- /// </summary>
- public float pivotY
- {
- get { return _pivotY; }
- set
- {
- SetPivot(_pivotX, value, _pivotAsAnchor);
- }
- }
- /// <summary>
- /// The x and y coordinates of the object's origin in its own coordinate space.
- /// </summary>
- public Vector2 pivot
- {
- get { return new Vector2(_pivotX, _pivotY); }
- set { SetPivot(value.x, value.y, _pivotAsAnchor); }
- }
- public bool pivotAsAnchor
- {
- get { return _pivotAsAnchor; }
- set
- {
- SetPivot(_pivotX, _pivotY, value);
- }
- }
- /// <summary>
- /// Change the x and y coordinates of the object's origin in its own coordinate space.
- /// </summary>
- /// <param name="xv">x value in ratio</param>
- /// <param name="yv">y value in ratio</param>
- public void SetPivot(float xv, float yv)
- {
- SetPivot(xv, yv, false);
- }
- /// <summary>
- /// Change the x and y coordinates of the object's origin in its own coordinate space.
- /// </summary>
- /// <param name="xv">x value in ratio</param>
- /// <param name="yv">y value in ratio</param>
- /// <param name="asAnchor">If use the pivot as the anchor position</param>
- public void SetPivot(float xv, float yv, bool asAnchor)
- {
- if (_pivotX != xv || _pivotY != yv || _pivotAsAnchor != asAnchor)
- {
- _pivotX = xv;
- _pivotY = yv;
- _pivotAsAnchor = asAnchor;
- if (displayObject != null)
- displayObject.pivot = new Vector2(_pivotX, _pivotY);
- HandlePositionChanged();
- }
- }
- /// <summary>
- /// If the object can touch or click. GImage/GTextField is not touchable even it is true.
- /// </summary>
- public bool touchable
- {
- get
- {
- return _touchable;
- }
- set
- {
- if (_touchable != value)
- {
- _touchable = value;
- UpdateGear(3);
- if (displayObject != null)
- displayObject.touchable = _touchable;
- }
- }
- }
- /// <summary>
- /// If true, apply a grayed effect on this object.
- /// </summary>
- public bool grayed
- {
- get
- {
- return _grayed;
- }
- set
- {
- if (_grayed != value)
- {
- _grayed = value;
- HandleGrayedChanged();
- UpdateGear(3);
- }
- }
- }
- /// <summary>
- /// Enabled is shortcut for grayed and !touchable combination.
- /// </summary>
- public bool enabled
- {
- get
- {
- return !_grayed && _touchable;
- }
- set
- {
- this.grayed = !value;
- this.touchable = value;
- }
- }
- /// <summary>
- /// The rotation around the z axis of the object in degrees.
- /// </summary>
- public float rotation
- {
- get
- {
- return _rotation;
- }
- set
- {
- _rotation = value;
- if (displayObject != null)
- displayObject.rotation = _rotation;
- UpdateGear(3);
- }
- }
- /// <summary>
- /// The rotation around the x axis of the object in degrees.
- /// </summary>
- public float rotationX
- {
- get
- {
- return _rotationX;
- }
- set
- {
- _rotationX = value;
- if (displayObject != null)
- displayObject.rotationX = _rotationX;
- }
- }
- /// <summary>
- /// The rotation around the y axis of the object in degrees.
- /// </summary>
- public float rotationY
- {
- get
- {
- return _rotationY;
- }
- set
- {
- _rotationY = value;
- if (displayObject != null)
- displayObject.rotationY = _rotationY;
- }
- }
- /// <summary>
- /// The opacity of the object. 0 = transparent, 1 = opaque.
- /// </summary>
- public float alpha
- {
- get
- {
- return _alpha;
- }
- set
- {
- _alpha = value;
- HandleAlphaChanged();
- UpdateGear(3);
- }
- }
- /// <summary>
- /// The visibility of the object. An invisible object will be untouchable.
- /// </summary>
- public bool visible
- {
- get
- {
- return _visible;
- }
- set
- {
- if (_visible != value)
- {
- _visible = value;
- HandleVisibleChanged();
- if (parent != null)
- parent.SetBoundsChangedFlag();
- if (_group != null && _group.excludeInvisibles)
- _group.SetBoundsChangedFlag();
- }
- }
- }
- internal bool internalVisible
- {
- get
- {
- return _internalVisible && (group == null || group.internalVisible);
- }
- }
- internal bool internalVisible2
- {
- get
- {
- return _visible && (group == null || group.internalVisible2);
- }
- }
- internal bool internalVisible3
- {
- get
- {
- return _visible && _internalVisible;
- }
- }
- /// <summary>
- /// By default(when sortingOrder==0), object added to component is arrange by the added roder.
- /// The bigger is the sorting order, the object is more in front.
- /// </summary>
- public int sortingOrder
- {
- get { return _sortingOrder; }
- set
- {
- if (value < 0)
- value = 0;
- if (_sortingOrder != value)
- {
- int old = _sortingOrder;
- _sortingOrder = value;
- if (parent != null)
- parent.ChildSortingOrderChanged(this, old, _sortingOrder);
- }
- }
- }
- /// <summary>
- /// If the object can be focused?
- /// </summary>
- public bool focusable
- {
- get { return displayObject != null && displayObject.focusable; }
- set { if (displayObject != null) displayObject.focusable = value; }
- }
- /// <summary>
- /// If the object can be navigated by TAB?
- /// </summary>
- public bool tabStop
- {
- get { return displayObject != null && displayObject.tabStop; }
- set { if (displayObject != null) displayObject.tabStop = value; }
- }
- /// <summary>
- /// If the object is focused.
- /// </summary>
- public bool focused
- {
- get { return displayObject != null && displayObject.focused; }
- }
- /// <summary>
- /// Request focus on this object.
- /// </summary>
- public void RequestFocus()
- {
- if (displayObject != null)
- Stage.inst.SetFocus(displayObject, false);
- }
- /// <summary>
- /// Request focus on this object.
- /// </summary>
- public void RequestFocus(bool byKey)
- {
- if (displayObject != null)
- Stage.inst.SetFocus(displayObject, byKey);
- }
- /// <summary>
- /// Tooltips of this object. UIConfig.tooltipsWin must be set first.
- /// </summary>
- public string tooltips
- {
- get { return _tooltips; }
- set
- {
- if (!string.IsNullOrEmpty(_tooltips))
- {
- this.onRollOver.Remove(__rollOver);
- this.onRollOut.Remove(__rollOut);
- }
- _tooltips = value;
- if (!string.IsNullOrEmpty(_tooltips))
- {
- this.onRollOver.Add(__rollOver);
- this.onRollOut.Add(__rollOut);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <value></value>
- public string cursor
- {
- get { return displayObject != null ? displayObject.cursor : null; }
- set
- {
- if (displayObject != null)
- displayObject.cursor = value;
- }
- }
- private void __rollOver()
- {
- this.root.ShowTooltips(tooltips);
- }
- private void __rollOut()
- {
- this.root.HideTooltips();
- }
- /// <summary>
- ///
- /// </summary>
- virtual public IFilter filter
- {
- get { return displayObject != null ? displayObject.filter : null; }
- set { if (displayObject != null) displayObject.filter = value; }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public BlendMode blendMode
- {
- get { return displayObject != null ? displayObject.blendMode : BlendMode.None; }
- set { if (displayObject != null) displayObject.blendMode = value; }
- }
- /// <summary>
- /// 设定GameObject的名称
- /// </summary>
- public string gameObjectName
- {
- get
- {
- if (displayObject != null)
- return displayObject.gameObject.name;
- else
- return null;
- }
- set
- {
- Debug.Log("zoya00:" + displayObject);
- Debug.Log("zoya11:" + displayObject.gameObject);
- Debug.Log("zoya22:" + value);
- if (displayObject != null && displayObject.gameObject != null)
- {
- Debug.Log("zoya33:" + value);
- displayObject.gameObject.name = value;
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="obj"></param>
- public void SetHome(GObject obj)
- {
- if (obj != null && displayObject != null && obj.displayObject != null)
- displayObject.home = obj.displayObject.cachedTransform;
- }
- /// <summary>
- /// If the object has lowlevel displayobject and the displayobject has a container parent?
- /// </summary>
- public bool inContainer
- {
- get
- {
- return displayObject != null && displayObject.parent != null;
- }
- }
- /// <summary>
- /// If the object is on stage.
- /// </summary>
- public bool onStage
- {
- get
- {
- return displayObject != null && displayObject.stage != null;
- }
- }
- /// <summary>
- /// Resource url of this object.
- /// </summary>
- public string resourceURL
- {
- get
- {
- if (packageItem != null)
- return UIPackage.URL_PREFIX + packageItem.owner.id + packageItem.id;
- else
- return null;
- }
- }
- /// <summary>
- /// Gear to xy controller.
- /// </summary>
- public GearXY gearXY
- {
- get
- {
- return (GearXY)GetGear(1);
- }
- }
- /// <summary>
- /// Gear to size controller.
- /// </summary>
- public GearSize gearSize
- {
- get
- {
- return (GearSize)GetGear(2);
- }
- }
- /// <summary>
- /// Gear to look controller.
- /// </summary>
- public GearLook gearLook
- {
- get
- {
- return (GearLook)GetGear(3);
- }
- }
- public GearBase GetGear(int index)
- {
- GearBase gear = _gears[index];
- if (gear == null)
- {
- switch (index)
- {
- case 0:
- gear = new GearDisplay(this);
- break;
- case 1:
- gear = new GearXY(this);
- break;
- case 2:
- gear = new GearSize(this);
- break;
- case 3:
- gear = new GearLook(this);
- break;
- case 4:
- gear = new GearColor(this);
- break;
- case 5:
- gear = new GearAnimation(this);
- break;
- case 6:
- gear = new GearText(this);
- break;
- case 7:
- gear = new GearIcon(this);
- break;
- case 8:
- gear = new GearDisplay2(this);
- break;
- case 9:
- gear = new GearFontSize(this);
- break;
- default:
- throw new System.Exception("FairyGUI: invalid gear index!");
- }
- _gears[index] = gear;
- }
- return gear;
- }
- protected void UpdateGear(int index)
- {
- if (underConstruct || _gearLocked)
- return;
- GearBase gear = _gears[index];
- if (gear != null && gear.controller != null)
- gear.UpdateState();
- }
- internal bool CheckGearController(int index, Controller c)
- {
- return _gears[index] != null && _gears[index].controller == c;
- }
- internal void UpdateGearFromRelations(int index, float dx, float dy)
- {
- if (_gears[index] != null)
- _gears[index].UpdateFromRelations(dx, dy);
- }
- internal uint AddDisplayLock()
- {
- GearDisplay gearDisplay = (GearDisplay)_gears[0];
- if (gearDisplay != null && gearDisplay.controller != null)
- {
- uint ret = gearDisplay.AddLock();
- CheckGearDisplay();
- return ret;
- }
- else
- return 0;
- }
- internal void ReleaseDisplayLock(uint token)
- {
- GearDisplay gearDisplay = (GearDisplay)_gears[0];
- if (gearDisplay != null && gearDisplay.controller != null)
- {
- gearDisplay.ReleaseLock(token);
- CheckGearDisplay();
- }
- }
- void CheckGearDisplay()
- {
- if (_handlingController)
- return;
- bool connected = _gears[0] == null || ((GearDisplay)_gears[0]).connected;
- if (_gears[8] != null)
- connected = ((GearDisplay2)_gears[8]).Evaluate(connected);
- if (connected != _internalVisible)
- {
- _internalVisible = connected;
- if (parent != null)
- parent.ChildStateChanged(this);
- if (_group != null && _group.excludeInvisibles)
- _group.SetBoundsChangedFlag();
- }
- }
- /// <summary>
- /// Mark the fairy batching state is invalid.
- /// </summary>
- public void InvalidateBatchingState()
- {
- if (displayObject != null)
- displayObject.InvalidateBatchingState();
- else if ((this is GGroup) && parent != null)
- parent.container.InvalidateBatchingState(true);
- }
- virtual public void HandleControllerChanged(Controller c)
- {
- _handlingController = true;
- for (int i = 0; i < 10; i++)
- {
- GearBase gear = _gears[i];
- if (gear != null && gear.controller == c)
- gear.Apply();
- }
- _handlingController = false;
- CheckGearDisplay();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- /// <param name="relationType"></param>
- public void AddRelation(GObject target, RelationType relationType)
- {
- AddRelation(target, relationType, false);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- /// <param name="relationType"></param>
- /// <param name="usePercent"></param>
- public void AddRelation(GObject target, RelationType relationType, bool usePercent)
- {
- relations.Add(target, relationType, usePercent);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="target"></param>
- /// <param name="relationType"></param>
- public void RemoveRelation(GObject target, RelationType relationType)
- {
- relations.Remove(target, relationType);
- }
- /// <summary>
- ///
- /// </summary>
- public void RemoveFromParent()
- {
- if (parent != null)
- parent.RemoveChild(this);
- }
- /// <summary>
- /// Group belonging to.
- /// </summary>
- public GGroup group
- {
- get { return _group; }
- set
- {
- if (_group != value)
- {
- if (_group != null)
- _group.SetBoundsChangedFlag();
- _group = value;
- if (_group != null)
- _group.SetBoundsChangedFlag();
- HandleVisibleChanged();
- if (parent != null)
- parent.ChildStateChanged(this);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public GRoot root
- {
- get
- {
- GObject p = this;
- while (p.parent != null)
- p = p.parent;
- if (p is GRoot)
- return (GRoot)p;
- if (p.displayObject != null && p.displayObject.parent != null)
- {
- DisplayObject d = p.displayObject.parent.GetChild("GRoot");
- if (d != null && (d.gOwner is GRoot))
- return (GRoot)d.gOwner;
- }
- return GRoot.inst;
- }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public string text
- {
- get { return null; }
- set { /*override in child*/}
- }
- /// <summary>
- ///
- /// </summary>
- virtual public string icon
- {
- get { return null; }
- set { /*override in child*/}
- }
- /// <summary>
- ///
- /// </summary>
- public bool draggable
- {
- get { return _draggable; }
- set
- {
- if (_draggable != value)
- {
- _draggable = value;
- InitDrag();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public void StartDrag()
- {
- StartDrag(-1);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="touchId"></param>
- public void StartDrag(int touchId)
- {
- if (displayObject.stage == null)
- return;
- DragBegin(touchId);
- }
- /// <summary>
- ///
- /// </summary>
- public void StopDrag()
- {
- DragEnd();
- }
- /// <summary>
- ///
- /// </summary>
- public bool dragging
- {
- get { return draggingObject == this; }
- }
- /// <summary>
- /// Transforms a point from the local coordinate system to global (Stage) coordinates.
- /// </summary>
- /// <param name="pt"></param>
- /// <returns></returns>
- public Vector2 LocalToGlobal(Vector2 pt)
- {
- if (_pivotAsAnchor)
- {
- pt.x += _width * _pivotX;
- pt.y += _height * _pivotY;
- }
- return displayObject.LocalToGlobal(pt);
- }
- /// <summary>
- /// Transforms a point from global (Stage) coordinates to the local coordinate system.
- /// </summary>
- /// <param name="pt"></param>
- /// <returns></returns>
- public Vector2 GlobalToLocal(Vector2 pt)
- {
- pt = displayObject.GlobalToLocal(pt);
- if (_pivotAsAnchor)
- {
- pt.x -= _width * _pivotX;
- pt.y -= _height * _pivotY;
- }
- return pt;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="rect"></param>
- /// <returns></returns>
- public Rect LocalToGlobal(Rect rect)
- {
- Rect ret = new Rect();
- Vector2 v = this.LocalToGlobal(new Vector2(rect.xMin, rect.yMin));
- ret.xMin = v.x;
- ret.yMin = v.y;
- v = this.LocalToGlobal(new Vector2(rect.xMax, rect.yMax));
- ret.xMax = v.x;
- ret.yMax = v.y;
- return ret;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="rect"></param>
- /// <returns></returns>
- public Rect GlobalToLocal(Rect rect)
- {
- Rect ret = new Rect();
- Vector2 v = this.GlobalToLocal(new Vector2(rect.xMin, rect.yMin));
- ret.xMin = v.x;
- ret.yMin = v.y;
- v = this.GlobalToLocal(new Vector2(rect.xMax, rect.yMax));
- ret.xMax = v.x;
- ret.yMax = v.y;
- return ret;
- }
- /// <summary>
- /// Transforms a point from the local coordinate system to GRoot coordinates.
- /// </summary>
- /// <param name="pt"></param>
- /// <param name="r"></param>
- /// <returns></returns>
- public Vector2 LocalToRoot(Vector2 pt, GRoot r)
- {
- pt = LocalToGlobal(pt);
- if (r == null || r == GRoot.inst)
- {
- //fast
- pt.x /= UIContentScaler.scaleFactor;
- pt.y /= UIContentScaler.scaleFactor;
- return pt;
- }
- else
- return r.GlobalToLocal(pt);
- }
- /// <summary>
- /// Transforms a point from the GRoot coordinate to local coordinates system.
- /// </summary>
- /// <param name="pt"></param>
- /// <param name="r"></param>
- /// <returns></returns>
- public Vector2 RootToLocal(Vector2 pt, GRoot r)
- {
- if (r == null || r == GRoot.inst)
- {
- //fast
- pt.x *= UIContentScaler.scaleFactor;
- pt.y *= UIContentScaler.scaleFactor;
- }
- else
- pt = r.LocalToGlobal(pt);
- return GlobalToLocal(pt);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="pt"></param>
- /// <returns></returns>
- public Vector2 WorldToLocal(Vector3 pt)
- {
- return WorldToLocal(pt, HitTestContext.cachedMainCamera);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="pt"></param>
- /// <param name="camera"></param>
- /// <returns></returns>
- public Vector2 WorldToLocal(Vector3 pt, Camera camera)
- {
- Vector3 v = camera.WorldToScreenPoint(pt);
- v.y = Screen.height - v.y;
- v.z = 0;
- return GlobalToLocal(v);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="point"></param>
- /// <param name="targetSpace"></param>
- /// <returns></returns>
- public Vector2 TransformPoint(Vector2 pt, GObject targetSpace)
- {
- if (_pivotAsAnchor)
- {
- pt.x += _width * _pivotX;
- pt.y += _height * _pivotY;
- }
- return this.displayObject.TransformPoint(pt, targetSpace != null ? targetSpace.displayObject : Stage.inst);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="rect"></param>
- /// <param name="targetSpace"></param>
- /// <returns></returns>
- public Rect TransformRect(Rect rect, GObject targetSpace)
- {
- if (_pivotAsAnchor)
- {
- rect.x += _width * _pivotX;
- rect.y += _height * _pivotY;
- }
- return this.displayObject.TransformRect(rect, targetSpace != null ? targetSpace.displayObject : Stage.inst);
- }
- /// <summary>
- ///
- /// </summary>
- public bool isDisposed
- {
- get { return _disposed; }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public void Dispose()
- {
- if (_disposed)
- return;
- _disposed = true;
- RemoveFromParent();
- RemoveEventListeners();
- relations.Dispose();
- relations = null;
- for (int i = 0; i < 10; i++)
- {
- GearBase gear = _gears[i];
- if (gear != null)
- gear.Dispose();
- }
- if (displayObject != null)
- {
- displayObject.gOwner = null;
- displayObject.Dispose();
- }
- data = null;
- }
- /// <summary>
- ///
- /// </summary>
- public GImage asImage
- {
- get { return this as GImage; }
- }
- /// <summary>
- ///
- /// </summary>
- public GComponent asCom
- {
- get { return this as GComponent; }
- }
- /// <summary>
- ///
- /// </summary>
- public GButton asButton
- {
- get { return this as GButton; }
- }
- /// <summary>
- ///
- /// </summary>
- public GLabel asLabel
- {
- get { return this as GLabel; }
- }
- /// <summary>
- ///
- /// </summary>
- public GProgressBar asProgress
- {
- get { return this as GProgressBar; }
- }
- /// <summary>
- ///
- /// </summary>
- public GSlider asSlider
- {
- get { return this as GSlider; }
- }
- /// <summary>
- ///
- /// </summary>
- public GComboBox asComboBox
- {
- get { return this as GComboBox; }
- }
- /// <summary>
- ///
- /// </summary>
- public GTextField asTextField
- {
- get { return this as GTextField; }
- }
- /// <summary>
- ///
- /// </summary>
- public GRichTextField asRichTextField
- {
- get { return this as GRichTextField; }
- }
- /// <summary>
- ///
- /// </summary>
- public GTextInput asTextInput
- {
- get { return this as GTextInput; }
- }
- /// <summary>
- ///
- /// </summary>
- public GLoader asLoader
- {
- get { return this as GLoader; }
- }
- /// <summary>
- ///
- /// </summary>
- public GLoader3D asLoader3D
- {
- get { return this as GLoader3D; }
- }
- /// <summary>
- ///
- /// </summary>
- public GList asList
- {
- get { return this as GList; }
- }
- /// <summary>
- ///
- /// </summary>
- public GGraph asGraph
- {
- get { return this as GGraph; }
- }
- /// <summary>
- ///
- /// </summary>
- public GGroup asGroup
- {
- get { return this as GGroup; }
- }
- /// <summary>
- ///
- /// </summary>
- public GMovieClip asMovieClip
- {
- get { return this as GMovieClip; }
- }
- /// <summary>
- ///
- /// </summary>
- public GTree asTree
- {
- get { return this as GTree; }
- }
- /// <summary>
- ///
- /// </summary>
- public GTreeNode treeNode
- {
- get { return _treeNode; }
- }
- virtual protected void CreateDisplayObject()
- {
- }
- internal void InternalSetParent(GComponent value)
- {
- parent = value;
- }
- virtual protected void HandlePositionChanged()
- {
- if (displayObject != null)
- {
- float xv = _x;
- float yv = _y;
- if (!_pivotAsAnchor)
- {
- xv += _width * _pivotX;
- yv += _height * _pivotY;
- }
- displayObject.location = new Vector3(xv, yv, _z);
- }
- }
- virtual protected void HandleSizeChanged()
- {
- if (displayObject != null)
- displayObject.SetSize(_width, _height);
- }
- virtual protected void HandleScaleChanged()
- {
- if (displayObject != null)
- displayObject.SetScale(_scaleX, _scaleY);
- }
- virtual protected void HandleGrayedChanged()
- {
- if (displayObject != null)
- displayObject.grayed = _grayed;
- }
- virtual protected void HandleAlphaChanged()
- {
- if (displayObject != null)
- displayObject.alpha = _alpha;
- }
- virtual internal protected void HandleVisibleChanged()
- {
- if (displayObject != null)
- displayObject.visible = internalVisible2;
- }
- virtual public void ConstructFromResource()
- {
- }
- virtual public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
- {
- buffer.Seek(beginPos, 0);
- buffer.Skip(5);
- id = buffer.ReadS();
- name = buffer.ReadS();
- float f1 = buffer.ReadInt();
- float f2 = buffer.ReadInt();
- SetXY(f1, f2);
- if (buffer.ReadBool())
- {
- initWidth = buffer.ReadInt();
- initHeight = buffer.ReadInt();
- SetSize(initWidth, initHeight, true);
- }
- if (buffer.ReadBool())
- {
- minWidth = buffer.ReadInt();
- maxWidth = buffer.ReadInt();
- minHeight = buffer.ReadInt();
- maxHeight = buffer.ReadInt();
- }
- if (buffer.ReadBool())
- {
- f1 = buffer.ReadFloat();
- f2 = buffer.ReadFloat();
- SetScale(f1, f2);
- }
- if (buffer.ReadBool())
- {
- f1 = buffer.ReadFloat();
- f2 = buffer.ReadFloat();
- this.skew = new Vector2(f1, f2);
- }
- if (buffer.ReadBool())
- {
- f1 = buffer.ReadFloat();
- f2 = buffer.ReadFloat();
- SetPivot(f1, f2, buffer.ReadBool());
- }
- f1 = buffer.ReadFloat();
- if (f1 != 1)
- this.alpha = f1;
- f1 = buffer.ReadFloat();
- if (f1 != 0)
- this.rotation = f1;
- if (!buffer.ReadBool())
- this.visible = false;
- if (!buffer.ReadBool())
- this.touchable = false;
- if (buffer.ReadBool())
- this.grayed = true;
- this.blendMode = (BlendMode)buffer.ReadByte();
- int filter = buffer.ReadByte();
- if (filter == 1)
- {
- ColorFilter cf = new ColorFilter();
- this.filter = cf;
- cf.AdjustBrightness(buffer.ReadFloat());
- cf.AdjustContrast(buffer.ReadFloat());
- cf.AdjustSaturation(buffer.ReadFloat());
- cf.AdjustHue(buffer.ReadFloat());
- }
- string str = buffer.ReadS();
- if (str != null)
- this.data = str;
- }
- virtual public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
- {
- buffer.Seek(beginPos, 1);
- string str = buffer.ReadS();
- if (str != null)
- this.tooltips = str;
- int groupId = buffer.ReadShort();
- if (groupId >= 0)
- group = parent.GetChildAt(groupId) as GGroup;
- buffer.Seek(beginPos, 2);
- int cnt = buffer.ReadShort();
- for (int i = 0; i < cnt; i++)
- {
- int nextPos = buffer.ReadShort();
- nextPos += buffer.position;
- GearBase gear = GetGear(buffer.ReadByte());
- gear.Setup(buffer);
- buffer.position = nextPos;
- }
- }
- #region Drag support
- Vector2 _dragTouchStartPos;
- bool _dragTesting;
- static Vector2 sGlobalDragStart = new Vector2();
- static Rect sGlobalRect = new Rect();
- static bool sUpdateInDragging = false;
- private void InitDrag()
- {
- if (_draggable)
- {
- onTouchBegin.Add(__touchBegin);
- onTouchMove.Add(__touchMove);
- onTouchEnd.Add(__touchEnd);
- }
- else
- {
- onTouchBegin.Remove(__touchBegin);
- onTouchMove.Remove(__touchMove);
- onTouchEnd.Remove(__touchEnd);
- }
- }
- private void DragBegin(int touchId)
- {
- if (DispatchEvent("onDragStart", touchId))
- return;
- if (draggingObject != null)
- {
- GObject tmp = draggingObject;
- draggingObject.StopDrag();
- draggingObject = null;
- tmp.DispatchEvent("onDragEnd", null);
- }
- onTouchMove.Add(__touchMove);
- onTouchEnd.Add(__touchEnd);
- sGlobalDragStart = Stage.inst.GetTouchPosition(touchId);
- sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
- _dragTesting = false;
- draggingObject = this;
- Stage.inst.AddTouchMonitor(touchId, this);
- }
- private void DragEnd()
- {
- if (draggingObject == this)
- {
- _dragTesting = false;
- draggingObject = null;
- }
- }
- private void __touchBegin(EventContext context)
- {
- if ((Stage.inst.focus is InputTextField) && ((InputTextField)Stage.inst.focus).editable)
- {
- _dragTesting = false;
- return;
- }
- InputEvent evt = context.inputEvent;
- _dragTouchStartPos = evt.position;
- _dragTesting = true;
- context.CaptureTouch();
- }
- private void __touchMove(EventContext context)
- {
- InputEvent evt = context.inputEvent;
- if (_dragTesting && draggingObject != this)
- {
- int sensitivity;
- if (Stage.touchScreen)
- sensitivity = UIConfig.touchDragSensitivity;
- else
- sensitivity = UIConfig.clickDragSensitivity;
- if (Mathf.Abs(_dragTouchStartPos.x - evt.x) < sensitivity
- && Mathf.Abs(_dragTouchStartPos.y - evt.y) < sensitivity)
- return;
- _dragTesting = false;
- DragBegin(evt.touchId);
- }
- if (draggingObject == this)
- {
- float xx = evt.x - sGlobalDragStart.x + sGlobalRect.x;
- float yy = evt.y - sGlobalDragStart.y + sGlobalRect.y;
- if (dragBounds != null)
- {
- Rect rect = GRoot.inst.LocalToGlobal((Rect)dragBounds);
- if (xx < rect.x)
- xx = rect.x;
- else if (xx + sGlobalRect.width > rect.xMax)
- {
- xx = rect.xMax - sGlobalRect.width;
- if (xx < rect.x)
- xx = rect.x;
- }
- if (yy < rect.y)
- yy = rect.y;
- else if (yy + sGlobalRect.height > rect.yMax)
- {
- yy = rect.yMax - sGlobalRect.height;
- if (yy < rect.y)
- yy = rect.y;
- }
- }
- Vector2 pt = this.parent.GlobalToLocal(new Vector2(xx, yy));
- if (float.IsNaN(pt.x))
- return;
- sUpdateInDragging = true;
- this.SetXY(Mathf.RoundToInt(pt.x), Mathf.RoundToInt(pt.y));
- sUpdateInDragging = false;
- DispatchEvent("onDragMove", null);
- }
- }
- private void __touchEnd(EventContext context)
- {
- if (draggingObject == this)
- {
- draggingObject = null;
- DispatchEvent("onDragEnd", null);
- }
- }
- #endregion
- #region Tween Helpers
- public GTweener TweenMove(Vector2 endValue, float duration)
- {
- return GTween.To(this.xy, endValue, duration).SetTarget(this, TweenPropType.XY);
- }
- public GTweener TweenMoveX(float endValue, float duration)
- {
- return GTween.To(_x, endValue, duration).SetTarget(this, TweenPropType.X);
- }
- public GTweener TweenMoveY(float endValue, float duration)
- {
- return GTween.To(_y, endValue, duration).SetTarget(this, TweenPropType.Y);
- }
- public GTweener TweenScale(Vector2 endValue, float duration)
- {
- return GTween.To(this.scale, endValue, duration).SetTarget(this, TweenPropType.Scale);
- }
- public GTweener TweenScaleX(float endValue, float duration)
- {
- return GTween.To(_scaleX, endValue, duration).SetTarget(this, TweenPropType.ScaleX);
- }
- public GTweener TweenScaleY(float endValue, float duration)
- {
- return GTween.To(_scaleY, endValue, duration).SetTarget(this, TweenPropType.ScaleY);
- }
- public GTweener TweenResize(Vector2 endValue, float duration)
- {
- return GTween.To(this.size, endValue, duration).SetTarget(this, TweenPropType.Size);
- }
- public GTweener TweenFade(float endValue, float duration)
- {
- return GTween.To(_alpha, endValue, duration).SetTarget(this, TweenPropType.Alpha);
- }
- public GTweener TweenRotate(float endValue, float duration)
- {
- return GTween.To(_rotation, endValue, duration).SetTarget(this, TweenPropType.Rotation);
- }
- #endregion
- }
- }
|