GObject.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. using System;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. public class GObject : EventDispatcher
  7. {
  8. /// <summary>
  9. /// GObject的id,仅作为内部使用。与name不同,id值是不会相同的。
  10. /// id is for internal use only.
  11. /// </summary>
  12. public string id { get; private set; }
  13. /// <summary>
  14. /// Name of the object.
  15. /// </summary>
  16. public string name;
  17. /// <summary>
  18. /// User defined data.
  19. /// </summary>
  20. public object data;
  21. /// <summary>
  22. /// The source width of the object.
  23. /// </summary>
  24. public int sourceWidth;
  25. /// <summary>
  26. /// The source height of the object.
  27. /// </summary>
  28. public int sourceHeight;
  29. /// <summary>
  30. /// The initial width of the object.
  31. /// </summary>
  32. public int initWidth;
  33. /// <summary>
  34. /// The initial height of the object.
  35. /// </summary>
  36. public int initHeight;
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public int minWidth;
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public int maxWidth;
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. public int minHeight;
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public int maxHeight;
  53. /// <summary>
  54. /// Relations Object.
  55. /// </summary>
  56. public Relations relations { get; private set; }
  57. /// <summary>
  58. /// Restricted range of dragging.
  59. /// </summary>
  60. public Rect? dragBounds;
  61. /// <summary>
  62. /// Parent object.
  63. /// </summary>
  64. public GComponent parent { get; private set; }
  65. /// <summary>
  66. /// Lowlevel display object.
  67. /// </summary>
  68. public DisplayObject displayObject { get; protected set; }
  69. /// <summary>
  70. /// 当前全局正在被拖动的对象
  71. /// </summary>
  72. public static GObject draggingObject { get; private set; }
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. public PackageItem packageItem;
  77. float _x;
  78. float _y;
  79. float _z;
  80. float _pivotX;
  81. float _pivotY;
  82. bool _pivotAsAnchor;
  83. float _alpha;
  84. float _rotation;
  85. float _rotationX;
  86. float _rotationY;
  87. bool _visible;
  88. bool _internalVisible;
  89. bool _handlingController;
  90. bool _touchable;
  91. bool _grayed;
  92. bool _draggable;
  93. float _scaleX;
  94. float _scaleY;
  95. int _sortingOrder;
  96. string _tooltips;
  97. GGroup _group;
  98. GearBase[] _gears;
  99. EventListener _onClick;
  100. EventListener _onRightClick;
  101. EventListener _onTouchBegin;
  102. EventListener _onTouchMove;
  103. EventListener _onTouchEnd;
  104. EventListener _onRollOver;
  105. EventListener _onRollOut;
  106. EventListener _onAddedToStage;
  107. EventListener _onRemovedFromStage;
  108. EventListener _onKeyDown;
  109. EventListener _onClickLink;
  110. EventListener _onPositionChanged;
  111. EventListener _onSizeChanged;
  112. EventListener _onDragStart;
  113. EventListener _onDragMove;
  114. EventListener _onDragEnd;
  115. EventListener _onGearStop;
  116. EventListener _onFocusIn;
  117. EventListener _onFocusOut;
  118. internal protected bool underConstruct;
  119. internal float _width;
  120. internal float _height;
  121. internal float _rawWidth;
  122. internal float _rawHeight;
  123. internal bool _gearLocked;
  124. internal float _sizePercentInGroup;
  125. internal bool _disposed;
  126. internal GTreeNode _treeNode;
  127. internal static uint _gInstanceCounter;
  128. public GObject()
  129. {
  130. _width = 0;
  131. _height = 0;
  132. _alpha = 1;
  133. _visible = true;
  134. _touchable = true;
  135. _scaleX = 1;
  136. _scaleY = 1;
  137. _internalVisible = true;
  138. id = "_n" + _gInstanceCounter++;
  139. name = string.Empty;
  140. CreateDisplayObject();
  141. relations = new Relations(this);
  142. _gears = new GearBase[10];
  143. }
  144. /// <summary>
  145. /// Dispatched when the object or its child was clicked.
  146. /// </summary>
  147. public EventListener onClick
  148. {
  149. get { return _onClick ?? (_onClick = new EventListener(this, "onClick")); }
  150. }
  151. /// <summary>
  152. /// Dispatched when the object or its child was clicked by right mouse button. Web only.
  153. /// </summary>
  154. public EventListener onRightClick
  155. {
  156. get { return _onRightClick ?? (_onRightClick = new EventListener(this, "onRightClick")); }
  157. }
  158. /// <summary>
  159. /// Dispatched when the finger touched the object or its child just now.
  160. /// </summary>
  161. public EventListener onTouchBegin
  162. {
  163. get { return _onTouchBegin ?? (_onTouchBegin = new EventListener(this, "onTouchBegin")); }
  164. }
  165. /// <summary>
  166. ///
  167. /// </summary>
  168. public EventListener onTouchMove
  169. {
  170. get { return _onTouchMove ?? (_onTouchMove = new EventListener(this, "onTouchMove")); }
  171. }
  172. /// <summary>
  173. /// Dispatched when the finger was lifted from the screen or from the mouse button.
  174. /// </summary>
  175. public EventListener onTouchEnd
  176. {
  177. get { return _onTouchEnd ?? (_onTouchEnd = new EventListener(this, "onTouchEnd")); }
  178. }
  179. /// <summary>
  180. /// The cursor or finger hovers over an object.
  181. /// </summary>
  182. public EventListener onRollOver
  183. {
  184. get { return _onRollOver ?? (_onRollOver = new EventListener(this, "onRollOver")); }
  185. }
  186. /// <summary>
  187. /// The cursor or finger leave an object.
  188. /// </summary>
  189. public EventListener onRollOut
  190. {
  191. get { return _onRollOut ?? (_onRollOut = new EventListener(this, "onRollOut")); }
  192. }
  193. /// <summary>
  194. /// Dispatched when the object was added to the stage.
  195. /// </summary>
  196. public EventListener onAddedToStage
  197. {
  198. get { return _onAddedToStage ?? (_onAddedToStage = new EventListener(this, "onAddedToStage")); }
  199. }
  200. /// <summary>
  201. /// Dispatched when the object was removed from the stage.
  202. /// </summary>
  203. public EventListener onRemovedFromStage
  204. {
  205. get { return _onRemovedFromStage ?? (_onRemovedFromStage = new EventListener(this, "onRemovedFromStage")); }
  206. }
  207. /// <summary>
  208. /// Dispatched on key pressed when the object is in focus.
  209. /// </summary>
  210. public EventListener onKeyDown
  211. {
  212. get { return _onKeyDown ?? (_onKeyDown = new EventListener(this, "onKeyDown")); }
  213. }
  214. /// <summary>
  215. /// Dispatched when links in the object or its child was clicked.
  216. /// </summary>
  217. public EventListener onClickLink
  218. {
  219. get { return _onClickLink ?? (_onClickLink = new EventListener(this, "onClickLink")); }
  220. }
  221. /// <summary>
  222. /// Dispatched when the object was moved.
  223. /// </summary>
  224. public EventListener onPositionChanged
  225. {
  226. get { return _onPositionChanged ?? (_onPositionChanged = new EventListener(this, "onPositionChanged")); }
  227. }
  228. /// <summary>
  229. /// Dispatched when the object was resized.
  230. /// </summary>
  231. public EventListener onSizeChanged
  232. {
  233. get { return _onSizeChanged ?? (_onSizeChanged = new EventListener(this, "onSizeChanged")); }
  234. }
  235. /// <summary>
  236. /// Dispatched when drag start.
  237. /// </summary>
  238. public EventListener onDragStart
  239. {
  240. get { return _onDragStart ?? (_onDragStart = new EventListener(this, "onDragStart")); }
  241. }
  242. /// <summary>
  243. /// Dispatched when dragging.
  244. /// </summary>
  245. public EventListener onDragMove
  246. {
  247. get { return _onDragMove ?? (_onDragMove = new EventListener(this, "onDragMove")); }
  248. }
  249. /// <summary>
  250. /// Dispatched when drag end.
  251. /// </summary>
  252. public EventListener onDragEnd
  253. {
  254. get { return _onDragEnd ?? (_onDragEnd = new EventListener(this, "onDragEnd")); }
  255. }
  256. /// <summary>
  257. ///
  258. /// </summary>
  259. public EventListener onGearStop
  260. {
  261. get { return _onGearStop ?? (_onGearStop = new EventListener(this, "onGearStop")); }
  262. }
  263. /// <summary>
  264. ///
  265. /// </summary>
  266. public EventListener onFocusIn
  267. {
  268. get { return _onFocusIn ?? (_onFocusIn = new EventListener(this, "onFocusIn")); }
  269. }
  270. /// <summary>
  271. ///
  272. /// </summary>
  273. public EventListener onFocusOut
  274. {
  275. get { return _onFocusOut ?? (_onFocusOut = new EventListener(this, "onFocusOut")); }
  276. }
  277. /// <summary>
  278. /// The x coordinate of the object relative to the local coordinates of the parent.
  279. /// </summary>
  280. public float x
  281. {
  282. get { return _x; }
  283. set
  284. {
  285. SetPosition(value, _y, _z);
  286. }
  287. }
  288. /// <summary>
  289. /// The y coordinate of the object relative to the local coordinates of the parent.
  290. /// </summary>
  291. public float y
  292. {
  293. get { return _y; }
  294. set
  295. {
  296. SetPosition(_x, value, _z);
  297. }
  298. }
  299. /// <summary>
  300. /// The z coordinate of the object relative to the local coordinates of the parent.
  301. /// </summary>
  302. public float z
  303. {
  304. get { return _z; }
  305. set
  306. {
  307. SetPosition(_x, _y, value);
  308. }
  309. }
  310. /// <summary>
  311. /// The x and y coordinates of the object relative to the local coordinates of the parent.
  312. /// </summary>
  313. public Vector2 xy
  314. {
  315. get { return new Vector2(_x, _y); }
  316. set { SetPosition(value.x, value.y, _z); }
  317. }
  318. /// <summary>
  319. /// The x,y,z coordinates of the object relative to the local coordinates of the parent.
  320. /// </summary>
  321. public Vector3 position
  322. {
  323. get { return new Vector3(_x, _y, _z); }
  324. set { SetPosition(value.x, value.y, value.z); }
  325. }
  326. /// <summary>
  327. /// change the x and y coordinates of the object relative to the local coordinates of the parent.
  328. /// </summary>
  329. /// <param name="xv">x value.</param>
  330. /// <param name="yv">y value.</param>
  331. public void SetXY(float xv, float yv)
  332. {
  333. SetPosition(xv, yv, _z);
  334. }
  335. /// <summary>
  336. ///
  337. /// </summary>
  338. /// <param name="xv"></param>
  339. /// <param name="yv"></param>
  340. /// <param name="topLeft"></param>
  341. public void SetXY(float xv, float yv, bool topLeftValue)
  342. {
  343. if (_pivotAsAnchor)
  344. SetPosition(xv + _pivotX * _width, yv + _pivotY * _height, _z);
  345. else
  346. SetPosition(xv, yv, _z);
  347. }
  348. /// <summary>
  349. /// change the x,y,z coordinates of the object relative to the local coordinates of the parent.
  350. /// </summary>
  351. /// <param name="xv">x value.</param>
  352. /// <param name="yv">y value.</param>
  353. /// <param name="zv">z value.</param>
  354. public void SetPosition(float xv, float yv, float zv)
  355. {
  356. if (_x != xv || _y != yv || _z != zv)
  357. {
  358. float dx = xv - _x;
  359. float dy = yv - _y;
  360. _x = xv;
  361. _y = yv;
  362. _z = zv;
  363. HandlePositionChanged();
  364. if (this is GGroup)
  365. ((GGroup)this).MoveChildren(dx, dy);
  366. UpdateGear(1);
  367. if (parent != null && !(parent is GList))
  368. {
  369. parent.SetBoundsChangedFlag();
  370. if (_group != null)
  371. _group.SetBoundsChangedFlag(true);
  372. DispatchEvent("onPositionChanged", null);
  373. }
  374. if (draggingObject == this && !sUpdateInDragging)
  375. sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
  376. }
  377. }
  378. [Obsolete("Use UIConfig.makePixelPerfect or DisplayObject.pixelPerfect")]
  379. public bool pixelSnapping
  380. {
  381. get { return false; }
  382. set { }
  383. }
  384. /// <summary>
  385. /// Set the object in middle of the parent or GRoot if the parent is not set.
  386. /// </summary>
  387. public void Center()
  388. {
  389. Center(false);
  390. }
  391. /// <summary>
  392. /// Set the object in middle of the parent or GRoot if the parent is not set.
  393. /// </summary>
  394. /// <param name="restraint">Add relations to maintain the center state.</param>
  395. public void Center(bool restraint)
  396. {
  397. GComponent r;
  398. if (parent != null)
  399. r = parent;
  400. else
  401. r = this.root;
  402. this.SetXY((int)((r.width - this.width) / 2), (int)((r.height - this.height) / 2), true);
  403. if (restraint)
  404. {
  405. this.AddRelation(r, RelationType.Center_Center);
  406. this.AddRelation(r, RelationType.Middle_Middle);
  407. }
  408. }
  409. /// <summary>
  410. /// 设置对象为全屏大小(逻辑屏幕)。
  411. /// </summary>
  412. public void MakeFullScreen()
  413. {
  414. this.SetSize(GRoot.inst.width, GRoot.inst.height);
  415. }
  416. /// <summary>
  417. /// The width of the object in pixels.
  418. /// </summary>
  419. public float width
  420. {
  421. get
  422. {
  423. return _width;
  424. }
  425. set
  426. {
  427. SetSize(value, _rawHeight);
  428. }
  429. }
  430. /// <summary>
  431. /// The height of the object in pixels.
  432. /// </summary>
  433. public float height
  434. {
  435. get
  436. {
  437. return _height;
  438. }
  439. set
  440. {
  441. SetSize(_rawWidth, value);
  442. }
  443. }
  444. /// <summary>
  445. /// The size of the object in pixels.
  446. /// </summary>
  447. public Vector2 size
  448. {
  449. get { return new Vector2(width, height); }
  450. set { SetSize(value.x, value.y); }
  451. }
  452. /// <summary>
  453. /// actualWidth = width * scalex
  454. /// </summary>
  455. public float actualWidth
  456. {
  457. get { return this.width * _scaleX; }
  458. }
  459. /// <summary>
  460. /// actualHeight = height * scaleY
  461. /// </summary>
  462. public float actualHeight
  463. {
  464. get { return this.height * _scaleY; }
  465. }
  466. /// <summary>
  467. /// Change size.
  468. /// </summary>
  469. /// <param name="wv">Width value.</param>
  470. /// <param name="hv">Height value.</param>
  471. public void SetSize(float wv, float hv)
  472. {
  473. SetSize(wv, hv, false);
  474. }
  475. /// <summary>
  476. /// Change size.
  477. /// </summary>
  478. /// <param name="wv">Width value.</param>
  479. /// <param name="hv">Height value.</param>
  480. /// <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>
  481. public void SetSize(float wv, float hv, bool ignorePivot)
  482. {
  483. if (_rawWidth != wv || _rawHeight != hv)
  484. {
  485. _rawWidth = wv;
  486. _rawHeight = hv;
  487. if (wv < minWidth)
  488. wv = minWidth;
  489. else if (maxWidth > 0 && wv > maxWidth)
  490. wv = maxWidth;
  491. if (hv < minHeight)
  492. hv = minHeight;
  493. else if (maxHeight > 0 && hv > maxHeight)
  494. hv = maxHeight;
  495. float dWidth = wv - _width;
  496. float dHeight = hv - _height;
  497. _width = wv;
  498. _height = hv;
  499. HandleSizeChanged();
  500. if (_pivotX != 0 || _pivotY != 0)
  501. {
  502. if (!_pivotAsAnchor)
  503. {
  504. if (!ignorePivot)
  505. SetXY(_x - _pivotX * dWidth, _y - _pivotY * dHeight);
  506. else
  507. HandlePositionChanged();
  508. }
  509. else
  510. HandlePositionChanged();
  511. }
  512. if (this is GGroup)
  513. ((GGroup)this).ResizeChildren(dWidth, dHeight);
  514. UpdateGear(2);
  515. if (parent != null)
  516. {
  517. relations.OnOwnerSizeChanged(dWidth, dHeight, _pivotAsAnchor || !ignorePivot);
  518. parent.SetBoundsChangedFlag();
  519. if (_group != null)
  520. _group.SetBoundsChangedFlag();
  521. }
  522. DispatchEvent("onSizeChanged", null);
  523. }
  524. }
  525. protected void SetSizeDirectly(float wv, float hv)
  526. {
  527. _rawWidth = wv;
  528. _rawHeight = hv;
  529. if (wv < 0)
  530. wv = 0;
  531. if (hv < 0)
  532. hv = 0;
  533. _width = wv;
  534. _height = hv;
  535. }
  536. /// <summary>
  537. ///
  538. /// </summary>
  539. public float xMin
  540. {
  541. get
  542. {
  543. return _pivotAsAnchor ? (_x - _width * _pivotX) : _x;
  544. }
  545. set
  546. {
  547. if (_pivotAsAnchor)
  548. SetPosition(value + _width * _pivotX, _y, _z);
  549. else
  550. SetPosition(value, _y, _z);
  551. }
  552. }
  553. /// <summary>
  554. ///
  555. /// </summary>
  556. public float yMin
  557. {
  558. get
  559. {
  560. return _pivotAsAnchor ? (_y - _height * _pivotY) : _y;
  561. }
  562. set
  563. {
  564. if (_pivotAsAnchor)
  565. SetPosition(_x, value + _height * _pivotY, _z);
  566. else
  567. SetPosition(_x, value, _z);
  568. }
  569. }
  570. /// <summary>
  571. /// The horizontal scale factor. '1' means no scale, cannt be negative.
  572. /// </summary>
  573. public float scaleX
  574. {
  575. get { return _scaleX; }
  576. set
  577. {
  578. SetScale(value, _scaleY);
  579. }
  580. }
  581. /// <summary>
  582. /// The vertical scale factor. '1' means no scale, cannt be negative.
  583. /// </summary>
  584. public float scaleY
  585. {
  586. get { return _scaleY; }
  587. set
  588. {
  589. SetScale(_scaleX, value);
  590. }
  591. }
  592. /// <summary>
  593. /// The scale factor.
  594. /// </summary>
  595. public Vector2 scale
  596. {
  597. get { return new Vector2(_scaleX, _scaleY); }
  598. set { SetScale(value.x, value.y); }
  599. }
  600. /// <summary>
  601. /// Change the scale factor.
  602. /// </summary>
  603. /// <param name="wv">The horizontal scale factor.</param>
  604. /// <param name="hv">The vertical scale factor</param>
  605. public void SetScale(float wv, float hv)
  606. {
  607. if (_scaleX != wv || _scaleY != hv)
  608. {
  609. _scaleX = wv;
  610. _scaleY = hv;
  611. HandleScaleChanged();
  612. UpdateGear(2);
  613. }
  614. }
  615. /// <summary>
  616. ///
  617. /// </summary>
  618. public Vector2 skew
  619. {
  620. get
  621. {
  622. if (displayObject != null)
  623. return displayObject.skew;
  624. else
  625. return Vector2.zero;
  626. }
  627. set
  628. {
  629. if (displayObject != null)
  630. displayObject.skew = value;
  631. }
  632. }
  633. /// <summary>
  634. /// The x coordinate of the object's origin in its own coordinate space.
  635. /// </summary>
  636. public float pivotX
  637. {
  638. get { return _pivotX; }
  639. set
  640. {
  641. SetPivot(value, _pivotY, _pivotAsAnchor);
  642. }
  643. }
  644. /// <summary>
  645. /// The y coordinate of the object's origin in its own coordinate space.
  646. /// </summary>
  647. public float pivotY
  648. {
  649. get { return _pivotY; }
  650. set
  651. {
  652. SetPivot(_pivotX, value, _pivotAsAnchor);
  653. }
  654. }
  655. /// <summary>
  656. /// The x and y coordinates of the object's origin in its own coordinate space.
  657. /// </summary>
  658. public Vector2 pivot
  659. {
  660. get { return new Vector2(_pivotX, _pivotY); }
  661. set { SetPivot(value.x, value.y, _pivotAsAnchor); }
  662. }
  663. public bool pivotAsAnchor
  664. {
  665. get { return _pivotAsAnchor; }
  666. set
  667. {
  668. SetPivot(_pivotX, _pivotY, value);
  669. }
  670. }
  671. /// <summary>
  672. /// Change the x and y coordinates of the object's origin in its own coordinate space.
  673. /// </summary>
  674. /// <param name="xv">x value in ratio</param>
  675. /// <param name="yv">y value in ratio</param>
  676. public void SetPivot(float xv, float yv)
  677. {
  678. SetPivot(xv, yv, false);
  679. }
  680. /// <summary>
  681. /// Change the x and y coordinates of the object's origin in its own coordinate space.
  682. /// </summary>
  683. /// <param name="xv">x value in ratio</param>
  684. /// <param name="yv">y value in ratio</param>
  685. /// <param name="asAnchor">If use the pivot as the anchor position</param>
  686. public void SetPivot(float xv, float yv, bool asAnchor)
  687. {
  688. if (_pivotX != xv || _pivotY != yv || _pivotAsAnchor != asAnchor)
  689. {
  690. _pivotX = xv;
  691. _pivotY = yv;
  692. _pivotAsAnchor = asAnchor;
  693. if (displayObject != null)
  694. displayObject.pivot = new Vector2(_pivotX, _pivotY);
  695. HandlePositionChanged();
  696. }
  697. }
  698. /// <summary>
  699. /// If the object can touch or click. GImage/GTextField is not touchable even it is true.
  700. /// </summary>
  701. public bool touchable
  702. {
  703. get
  704. {
  705. return _touchable;
  706. }
  707. set
  708. {
  709. if (_touchable != value)
  710. {
  711. _touchable = value;
  712. UpdateGear(3);
  713. if (displayObject != null)
  714. displayObject.touchable = _touchable;
  715. }
  716. }
  717. }
  718. /// <summary>
  719. /// If true, apply a grayed effect on this object.
  720. /// </summary>
  721. public bool grayed
  722. {
  723. get
  724. {
  725. return _grayed;
  726. }
  727. set
  728. {
  729. if (_grayed != value)
  730. {
  731. _grayed = value;
  732. HandleGrayedChanged();
  733. UpdateGear(3);
  734. }
  735. }
  736. }
  737. /// <summary>
  738. /// Enabled is shortcut for grayed and !touchable combination.
  739. /// </summary>
  740. public bool enabled
  741. {
  742. get
  743. {
  744. return !_grayed && _touchable;
  745. }
  746. set
  747. {
  748. this.grayed = !value;
  749. this.touchable = value;
  750. }
  751. }
  752. /// <summary>
  753. /// The rotation around the z axis of the object in degrees.
  754. /// </summary>
  755. public float rotation
  756. {
  757. get
  758. {
  759. return _rotation;
  760. }
  761. set
  762. {
  763. _rotation = value;
  764. if (displayObject != null)
  765. displayObject.rotation = _rotation;
  766. UpdateGear(3);
  767. }
  768. }
  769. /// <summary>
  770. /// The rotation around the x axis of the object in degrees.
  771. /// </summary>
  772. public float rotationX
  773. {
  774. get
  775. {
  776. return _rotationX;
  777. }
  778. set
  779. {
  780. _rotationX = value;
  781. if (displayObject != null)
  782. displayObject.rotationX = _rotationX;
  783. }
  784. }
  785. /// <summary>
  786. /// The rotation around the y axis of the object in degrees.
  787. /// </summary>
  788. public float rotationY
  789. {
  790. get
  791. {
  792. return _rotationY;
  793. }
  794. set
  795. {
  796. _rotationY = value;
  797. if (displayObject != null)
  798. displayObject.rotationY = _rotationY;
  799. }
  800. }
  801. /// <summary>
  802. /// The opacity of the object. 0 = transparent, 1 = opaque.
  803. /// </summary>
  804. public float alpha
  805. {
  806. get
  807. {
  808. return _alpha;
  809. }
  810. set
  811. {
  812. _alpha = value;
  813. HandleAlphaChanged();
  814. UpdateGear(3);
  815. }
  816. }
  817. /// <summary>
  818. /// The visibility of the object. An invisible object will be untouchable.
  819. /// </summary>
  820. public bool visible
  821. {
  822. get
  823. {
  824. return _visible;
  825. }
  826. set
  827. {
  828. if (_visible != value)
  829. {
  830. _visible = value;
  831. HandleVisibleChanged();
  832. if (parent != null)
  833. parent.SetBoundsChangedFlag();
  834. if (_group != null && _group.excludeInvisibles)
  835. _group.SetBoundsChangedFlag();
  836. }
  837. }
  838. }
  839. internal bool internalVisible
  840. {
  841. get
  842. {
  843. return _internalVisible && (group == null || group.internalVisible);
  844. }
  845. }
  846. internal bool internalVisible2
  847. {
  848. get
  849. {
  850. return _visible && (group == null || group.internalVisible2);
  851. }
  852. }
  853. internal bool internalVisible3
  854. {
  855. get
  856. {
  857. return _visible && _internalVisible;
  858. }
  859. }
  860. /// <summary>
  861. /// By default(when sortingOrder==0), object added to component is arrange by the added roder.
  862. /// The bigger is the sorting order, the object is more in front.
  863. /// </summary>
  864. public int sortingOrder
  865. {
  866. get { return _sortingOrder; }
  867. set
  868. {
  869. if (value < 0)
  870. value = 0;
  871. if (_sortingOrder != value)
  872. {
  873. int old = _sortingOrder;
  874. _sortingOrder = value;
  875. if (parent != null)
  876. parent.ChildSortingOrderChanged(this, old, _sortingOrder);
  877. }
  878. }
  879. }
  880. /// <summary>
  881. /// If the object can be focused?
  882. /// </summary>
  883. public bool focusable
  884. {
  885. get { return displayObject != null && displayObject.focusable; }
  886. set { if (displayObject != null) displayObject.focusable = value; }
  887. }
  888. /// <summary>
  889. /// If the object can be navigated by TAB?
  890. /// </summary>
  891. public bool tabStop
  892. {
  893. get { return displayObject != null && displayObject.tabStop; }
  894. set { if (displayObject != null) displayObject.tabStop = value; }
  895. }
  896. /// <summary>
  897. /// If the object is focused.
  898. /// </summary>
  899. public bool focused
  900. {
  901. get { return displayObject != null && displayObject.focused; }
  902. }
  903. /// <summary>
  904. /// Request focus on this object.
  905. /// </summary>
  906. public void RequestFocus()
  907. {
  908. if (displayObject != null)
  909. Stage.inst.SetFocus(displayObject, false);
  910. }
  911. /// <summary>
  912. /// Request focus on this object.
  913. /// </summary>
  914. public void RequestFocus(bool byKey)
  915. {
  916. if (displayObject != null)
  917. Stage.inst.SetFocus(displayObject, byKey);
  918. }
  919. /// <summary>
  920. /// Tooltips of this object. UIConfig.tooltipsWin must be set first.
  921. /// </summary>
  922. public string tooltips
  923. {
  924. get { return _tooltips; }
  925. set
  926. {
  927. if (!string.IsNullOrEmpty(_tooltips))
  928. {
  929. this.onRollOver.Remove(__rollOver);
  930. this.onRollOut.Remove(__rollOut);
  931. }
  932. _tooltips = value;
  933. if (!string.IsNullOrEmpty(_tooltips))
  934. {
  935. this.onRollOver.Add(__rollOver);
  936. this.onRollOut.Add(__rollOut);
  937. }
  938. }
  939. }
  940. /// <summary>
  941. ///
  942. /// </summary>
  943. /// <value></value>
  944. public string cursor
  945. {
  946. get { return displayObject != null ? displayObject.cursor : null; }
  947. set
  948. {
  949. if (displayObject != null)
  950. displayObject.cursor = value;
  951. }
  952. }
  953. private void __rollOver()
  954. {
  955. this.root.ShowTooltips(tooltips);
  956. }
  957. private void __rollOut()
  958. {
  959. this.root.HideTooltips();
  960. }
  961. /// <summary>
  962. ///
  963. /// </summary>
  964. virtual public IFilter filter
  965. {
  966. get { return displayObject != null ? displayObject.filter : null; }
  967. set { if (displayObject != null) displayObject.filter = value; }
  968. }
  969. /// <summary>
  970. ///
  971. /// </summary>
  972. virtual public BlendMode blendMode
  973. {
  974. get { return displayObject != null ? displayObject.blendMode : BlendMode.None; }
  975. set { if (displayObject != null) displayObject.blendMode = value; }
  976. }
  977. /// <summary>
  978. /// 设定GameObject的名称
  979. /// </summary>
  980. public string gameObjectName
  981. {
  982. get
  983. {
  984. if (displayObject != null && displayObject.gameObject != null)
  985. return displayObject.gameObject.name;
  986. else
  987. return null;
  988. }
  989. set
  990. {
  991. if (displayObject != null && displayObject.gameObject != null)
  992. {
  993. displayObject.gameObject.name = value;
  994. }
  995. }
  996. }
  997. /// <summary>
  998. ///
  999. /// </summary>
  1000. /// <param name="obj"></param>
  1001. public void SetHome(GObject obj)
  1002. {
  1003. if (obj != null && displayObject != null && obj.displayObject != null)
  1004. displayObject.home = obj.displayObject.cachedTransform;
  1005. }
  1006. /// <summary>
  1007. /// If the object has lowlevel displayobject and the displayobject has a container parent?
  1008. /// </summary>
  1009. public bool inContainer
  1010. {
  1011. get
  1012. {
  1013. return displayObject != null && displayObject.parent != null;
  1014. }
  1015. }
  1016. /// <summary>
  1017. /// If the object is on stage.
  1018. /// </summary>
  1019. public bool onStage
  1020. {
  1021. get
  1022. {
  1023. return displayObject != null && displayObject.stage != null;
  1024. }
  1025. }
  1026. /// <summary>
  1027. /// Resource url of this object.
  1028. /// </summary>
  1029. public string resourceURL
  1030. {
  1031. get
  1032. {
  1033. if (packageItem != null)
  1034. return UIPackage.URL_PREFIX + packageItem.owner.id + packageItem.id;
  1035. else
  1036. return null;
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// Gear to xy controller.
  1041. /// </summary>
  1042. public GearXY gearXY
  1043. {
  1044. get
  1045. {
  1046. return (GearXY)GetGear(1);
  1047. }
  1048. }
  1049. /// <summary>
  1050. /// Gear to size controller.
  1051. /// </summary>
  1052. public GearSize gearSize
  1053. {
  1054. get
  1055. {
  1056. return (GearSize)GetGear(2);
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// Gear to look controller.
  1061. /// </summary>
  1062. public GearLook gearLook
  1063. {
  1064. get
  1065. {
  1066. return (GearLook)GetGear(3);
  1067. }
  1068. }
  1069. public GearBase GetGear(int index)
  1070. {
  1071. GearBase gear = _gears[index];
  1072. if (gear == null)
  1073. {
  1074. switch (index)
  1075. {
  1076. case 0:
  1077. gear = new GearDisplay(this);
  1078. break;
  1079. case 1:
  1080. gear = new GearXY(this);
  1081. break;
  1082. case 2:
  1083. gear = new GearSize(this);
  1084. break;
  1085. case 3:
  1086. gear = new GearLook(this);
  1087. break;
  1088. case 4:
  1089. gear = new GearColor(this);
  1090. break;
  1091. case 5:
  1092. gear = new GearAnimation(this);
  1093. break;
  1094. case 6:
  1095. gear = new GearText(this);
  1096. break;
  1097. case 7:
  1098. gear = new GearIcon(this);
  1099. break;
  1100. case 8:
  1101. gear = new GearDisplay2(this);
  1102. break;
  1103. case 9:
  1104. gear = new GearFontSize(this);
  1105. break;
  1106. default:
  1107. throw new System.Exception("FairyGUI: invalid gear index!");
  1108. }
  1109. _gears[index] = gear;
  1110. }
  1111. return gear;
  1112. }
  1113. protected void UpdateGear(int index)
  1114. {
  1115. if (underConstruct || _gearLocked)
  1116. return;
  1117. GearBase gear = _gears[index];
  1118. if (gear != null && gear.controller != null)
  1119. gear.UpdateState();
  1120. }
  1121. internal bool CheckGearController(int index, Controller c)
  1122. {
  1123. return _gears[index] != null && _gears[index].controller == c;
  1124. }
  1125. internal void UpdateGearFromRelations(int index, float dx, float dy)
  1126. {
  1127. if (_gears[index] != null)
  1128. _gears[index].UpdateFromRelations(dx, dy);
  1129. }
  1130. internal uint AddDisplayLock()
  1131. {
  1132. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1133. if (gearDisplay != null && gearDisplay.controller != null)
  1134. {
  1135. uint ret = gearDisplay.AddLock();
  1136. CheckGearDisplay();
  1137. return ret;
  1138. }
  1139. else
  1140. return 0;
  1141. }
  1142. internal void ReleaseDisplayLock(uint token)
  1143. {
  1144. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1145. if (gearDisplay != null && gearDisplay.controller != null)
  1146. {
  1147. gearDisplay.ReleaseLock(token);
  1148. CheckGearDisplay();
  1149. }
  1150. }
  1151. void CheckGearDisplay()
  1152. {
  1153. if (_handlingController)
  1154. return;
  1155. bool connected = _gears[0] == null || ((GearDisplay)_gears[0]).connected;
  1156. if (_gears[8] != null)
  1157. connected = ((GearDisplay2)_gears[8]).Evaluate(connected);
  1158. if (connected != _internalVisible)
  1159. {
  1160. _internalVisible = connected;
  1161. if (parent != null)
  1162. parent.ChildStateChanged(this);
  1163. if (_group != null && _group.excludeInvisibles)
  1164. _group.SetBoundsChangedFlag();
  1165. }
  1166. }
  1167. /// <summary>
  1168. /// Mark the fairy batching state is invalid.
  1169. /// </summary>
  1170. public void InvalidateBatchingState()
  1171. {
  1172. if (displayObject != null)
  1173. displayObject.InvalidateBatchingState();
  1174. else if ((this is GGroup) && parent != null)
  1175. parent.container.InvalidateBatchingState(true);
  1176. }
  1177. virtual public void HandleControllerChanged(Controller c)
  1178. {
  1179. _handlingController = true;
  1180. for (int i = 0; i < 10; i++)
  1181. {
  1182. GearBase gear = _gears[i];
  1183. if (gear != null && gear.controller == c)
  1184. gear.Apply();
  1185. }
  1186. _handlingController = false;
  1187. CheckGearDisplay();
  1188. }
  1189. /// <summary>
  1190. ///
  1191. /// </summary>
  1192. /// <param name="target"></param>
  1193. /// <param name="relationType"></param>
  1194. public void AddRelation(GObject target, RelationType relationType)
  1195. {
  1196. AddRelation(target, relationType, false);
  1197. }
  1198. /// <summary>
  1199. ///
  1200. /// </summary>
  1201. /// <param name="target"></param>
  1202. /// <param name="relationType"></param>
  1203. /// <param name="usePercent"></param>
  1204. public void AddRelation(GObject target, RelationType relationType, bool usePercent)
  1205. {
  1206. relations.Add(target, relationType, usePercent);
  1207. }
  1208. /// <summary>
  1209. ///
  1210. /// </summary>
  1211. /// <param name="target"></param>
  1212. /// <param name="relationType"></param>
  1213. public void RemoveRelation(GObject target, RelationType relationType)
  1214. {
  1215. relations.Remove(target, relationType);
  1216. }
  1217. /// <summary>
  1218. ///
  1219. /// </summary>
  1220. public void RemoveFromParent()
  1221. {
  1222. if (parent != null)
  1223. parent.RemoveChild(this);
  1224. }
  1225. /// <summary>
  1226. /// Group belonging to.
  1227. /// </summary>
  1228. public GGroup group
  1229. {
  1230. get { return _group; }
  1231. set
  1232. {
  1233. if (_group != value)
  1234. {
  1235. if (_group != null)
  1236. _group.SetBoundsChangedFlag();
  1237. _group = value;
  1238. if (_group != null)
  1239. _group.SetBoundsChangedFlag();
  1240. HandleVisibleChanged();
  1241. if (parent != null)
  1242. parent.ChildStateChanged(this);
  1243. }
  1244. }
  1245. }
  1246. /// <summary>
  1247. ///
  1248. /// </summary>
  1249. public GRoot root
  1250. {
  1251. get
  1252. {
  1253. GObject p = this;
  1254. while (p.parent != null)
  1255. p = p.parent;
  1256. if (p is GRoot)
  1257. return (GRoot)p;
  1258. if (p.displayObject != null && p.displayObject.parent != null)
  1259. {
  1260. DisplayObject d = p.displayObject.parent.GetChild("GRoot");
  1261. if (d != null && (d.gOwner is GRoot))
  1262. return (GRoot)d.gOwner;
  1263. }
  1264. return GRoot.inst;
  1265. }
  1266. }
  1267. /// <summary>
  1268. ///
  1269. /// </summary>
  1270. virtual public string text
  1271. {
  1272. get { return null; }
  1273. set { /*override in child*/}
  1274. }
  1275. /// <summary>
  1276. ///
  1277. /// </summary>
  1278. virtual public string icon
  1279. {
  1280. get { return null; }
  1281. set { /*override in child*/}
  1282. }
  1283. /// <summary>
  1284. ///
  1285. /// </summary>
  1286. public bool draggable
  1287. {
  1288. get { return _draggable; }
  1289. set
  1290. {
  1291. if (_draggable != value)
  1292. {
  1293. _draggable = value;
  1294. InitDrag();
  1295. }
  1296. }
  1297. }
  1298. /// <summary>
  1299. ///
  1300. /// </summary>
  1301. public void StartDrag()
  1302. {
  1303. StartDrag(-1);
  1304. }
  1305. /// <summary>
  1306. ///
  1307. /// </summary>
  1308. /// <param name="touchId"></param>
  1309. public void StartDrag(int touchId)
  1310. {
  1311. if (displayObject.stage == null)
  1312. return;
  1313. DragBegin(touchId);
  1314. }
  1315. /// <summary>
  1316. ///
  1317. /// </summary>
  1318. public void StopDrag()
  1319. {
  1320. DragEnd();
  1321. }
  1322. /// <summary>
  1323. ///
  1324. /// </summary>
  1325. public bool dragging
  1326. {
  1327. get { return draggingObject == this; }
  1328. }
  1329. /// <summary>
  1330. /// Transforms a point from the local coordinate system to global (Stage) coordinates.
  1331. /// </summary>
  1332. /// <param name="pt"></param>
  1333. /// <returns></returns>
  1334. public Vector2 LocalToGlobal(Vector2 pt)
  1335. {
  1336. if (_pivotAsAnchor)
  1337. {
  1338. pt.x += _width * _pivotX;
  1339. pt.y += _height * _pivotY;
  1340. }
  1341. return displayObject.LocalToGlobal(pt);
  1342. }
  1343. /// <summary>
  1344. /// Transforms a point from global (Stage) coordinates to the local coordinate system.
  1345. /// </summary>
  1346. /// <param name="pt"></param>
  1347. /// <returns></returns>
  1348. public Vector2 GlobalToLocal(Vector2 pt)
  1349. {
  1350. pt = displayObject.GlobalToLocal(pt);
  1351. if (_pivotAsAnchor)
  1352. {
  1353. pt.x -= _width * _pivotX;
  1354. pt.y -= _height * _pivotY;
  1355. }
  1356. return pt;
  1357. }
  1358. /// <summary>
  1359. ///
  1360. /// </summary>
  1361. /// <param name="rect"></param>
  1362. /// <returns></returns>
  1363. public Rect LocalToGlobal(Rect rect)
  1364. {
  1365. Rect ret = new Rect();
  1366. Vector2 v = this.LocalToGlobal(new Vector2(rect.xMin, rect.yMin));
  1367. ret.xMin = v.x;
  1368. ret.yMin = v.y;
  1369. v = this.LocalToGlobal(new Vector2(rect.xMax, rect.yMax));
  1370. ret.xMax = v.x;
  1371. ret.yMax = v.y;
  1372. return ret;
  1373. }
  1374. /// <summary>
  1375. ///
  1376. /// </summary>
  1377. /// <param name="rect"></param>
  1378. /// <returns></returns>
  1379. public Rect GlobalToLocal(Rect rect)
  1380. {
  1381. Rect ret = new Rect();
  1382. Vector2 v = this.GlobalToLocal(new Vector2(rect.xMin, rect.yMin));
  1383. ret.xMin = v.x;
  1384. ret.yMin = v.y;
  1385. v = this.GlobalToLocal(new Vector2(rect.xMax, rect.yMax));
  1386. ret.xMax = v.x;
  1387. ret.yMax = v.y;
  1388. return ret;
  1389. }
  1390. /// <summary>
  1391. /// Transforms a point from the local coordinate system to GRoot coordinates.
  1392. /// </summary>
  1393. /// <param name="pt"></param>
  1394. /// <param name="r"></param>
  1395. /// <returns></returns>
  1396. public Vector2 LocalToRoot(Vector2 pt, GRoot r)
  1397. {
  1398. pt = LocalToGlobal(pt);
  1399. if (r == null || r == GRoot.inst)
  1400. {
  1401. //fast
  1402. pt.x /= UIContentScaler.scaleFactor;
  1403. pt.y /= UIContentScaler.scaleFactor;
  1404. return pt;
  1405. }
  1406. else
  1407. return r.GlobalToLocal(pt);
  1408. }
  1409. /// <summary>
  1410. /// Transforms a point from the GRoot coordinate to local coordinates system.
  1411. /// </summary>
  1412. /// <param name="pt"></param>
  1413. /// <param name="r"></param>
  1414. /// <returns></returns>
  1415. public Vector2 RootToLocal(Vector2 pt, GRoot r)
  1416. {
  1417. if (r == null || r == GRoot.inst)
  1418. {
  1419. //fast
  1420. pt.x *= UIContentScaler.scaleFactor;
  1421. pt.y *= UIContentScaler.scaleFactor;
  1422. }
  1423. else
  1424. pt = r.LocalToGlobal(pt);
  1425. return GlobalToLocal(pt);
  1426. }
  1427. /// <summary>
  1428. ///
  1429. /// </summary>
  1430. /// <param name="pt"></param>
  1431. /// <returns></returns>
  1432. public Vector2 WorldToLocal(Vector3 pt)
  1433. {
  1434. return WorldToLocal(pt, HitTestContext.cachedMainCamera);
  1435. }
  1436. /// <summary>
  1437. ///
  1438. /// </summary>
  1439. /// <param name="pt"></param>
  1440. /// <param name="camera"></param>
  1441. /// <returns></returns>
  1442. public Vector2 WorldToLocal(Vector3 pt, Camera camera)
  1443. {
  1444. Vector3 v = camera.WorldToScreenPoint(pt);
  1445. v.y = Screen.height - v.y;
  1446. v.z = 0;
  1447. return GlobalToLocal(v);
  1448. }
  1449. /// <summary>
  1450. ///
  1451. /// </summary>
  1452. /// <param name="point"></param>
  1453. /// <param name="targetSpace"></param>
  1454. /// <returns></returns>
  1455. public Vector2 TransformPoint(Vector2 pt, GObject targetSpace)
  1456. {
  1457. if (_pivotAsAnchor)
  1458. {
  1459. pt.x += _width * _pivotX;
  1460. pt.y += _height * _pivotY;
  1461. }
  1462. return this.displayObject.TransformPoint(pt, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1463. }
  1464. /// <summary>
  1465. ///
  1466. /// </summary>
  1467. /// <param name="rect"></param>
  1468. /// <param name="targetSpace"></param>
  1469. /// <returns></returns>
  1470. public Rect TransformRect(Rect rect, GObject targetSpace)
  1471. {
  1472. if (_pivotAsAnchor)
  1473. {
  1474. rect.x += _width * _pivotX;
  1475. rect.y += _height * _pivotY;
  1476. }
  1477. return this.displayObject.TransformRect(rect, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1478. }
  1479. /// <summary>
  1480. ///
  1481. /// </summary>
  1482. public bool isDisposed
  1483. {
  1484. get { return _disposed; }
  1485. }
  1486. /// <summary>
  1487. ///
  1488. /// </summary>
  1489. virtual public void Dispose()
  1490. {
  1491. if (_disposed)
  1492. return;
  1493. _disposed = true;
  1494. RemoveFromParent();
  1495. RemoveEventListeners();
  1496. relations.Dispose();
  1497. relations = null;
  1498. for (int i = 0; i < 10; i++)
  1499. {
  1500. GearBase gear = _gears[i];
  1501. if (gear != null)
  1502. gear.Dispose();
  1503. }
  1504. if (displayObject != null)
  1505. {
  1506. displayObject.gOwner = null;
  1507. displayObject.Dispose();
  1508. }
  1509. data = null;
  1510. }
  1511. /// <summary>
  1512. ///
  1513. /// </summary>
  1514. public GImage asImage
  1515. {
  1516. get { return this as GImage; }
  1517. }
  1518. /// <summary>
  1519. ///
  1520. /// </summary>
  1521. public GComponent asCom
  1522. {
  1523. get { return this as GComponent; }
  1524. }
  1525. /// <summary>
  1526. ///
  1527. /// </summary>
  1528. public GButton asButton
  1529. {
  1530. get { return this as GButton; }
  1531. }
  1532. /// <summary>
  1533. ///
  1534. /// </summary>
  1535. public GLabel asLabel
  1536. {
  1537. get { return this as GLabel; }
  1538. }
  1539. /// <summary>
  1540. ///
  1541. /// </summary>
  1542. public GProgressBar asProgress
  1543. {
  1544. get { return this as GProgressBar; }
  1545. }
  1546. /// <summary>
  1547. ///
  1548. /// </summary>
  1549. public GSlider asSlider
  1550. {
  1551. get { return this as GSlider; }
  1552. }
  1553. /// <summary>
  1554. ///
  1555. /// </summary>
  1556. public GComboBox asComboBox
  1557. {
  1558. get { return this as GComboBox; }
  1559. }
  1560. /// <summary>
  1561. ///
  1562. /// </summary>
  1563. public GTextField asTextField
  1564. {
  1565. get { return this as GTextField; }
  1566. }
  1567. /// <summary>
  1568. ///
  1569. /// </summary>
  1570. public GRichTextField asRichTextField
  1571. {
  1572. get { return this as GRichTextField; }
  1573. }
  1574. /// <summary>
  1575. ///
  1576. /// </summary>
  1577. public GTextInput asTextInput
  1578. {
  1579. get { return this as GTextInput; }
  1580. }
  1581. /// <summary>
  1582. ///
  1583. /// </summary>
  1584. public GLoader asLoader
  1585. {
  1586. get { return this as GLoader; }
  1587. }
  1588. /// <summary>
  1589. ///
  1590. /// </summary>
  1591. public GLoader3D asLoader3D
  1592. {
  1593. get { return this as GLoader3D; }
  1594. }
  1595. /// <summary>
  1596. ///
  1597. /// </summary>
  1598. public GList asList
  1599. {
  1600. get { return this as GList; }
  1601. }
  1602. /// <summary>
  1603. ///
  1604. /// </summary>
  1605. public GGraph asGraph
  1606. {
  1607. get { return this as GGraph; }
  1608. }
  1609. /// <summary>
  1610. ///
  1611. /// </summary>
  1612. public GGroup asGroup
  1613. {
  1614. get { return this as GGroup; }
  1615. }
  1616. /// <summary>
  1617. ///
  1618. /// </summary>
  1619. public GMovieClip asMovieClip
  1620. {
  1621. get { return this as GMovieClip; }
  1622. }
  1623. /// <summary>
  1624. ///
  1625. /// </summary>
  1626. public GTree asTree
  1627. {
  1628. get { return this as GTree; }
  1629. }
  1630. /// <summary>
  1631. ///
  1632. /// </summary>
  1633. public GTreeNode treeNode
  1634. {
  1635. get { return _treeNode; }
  1636. }
  1637. virtual protected void CreateDisplayObject()
  1638. {
  1639. }
  1640. internal void InternalSetParent(GComponent value)
  1641. {
  1642. parent = value;
  1643. }
  1644. virtual protected void HandlePositionChanged()
  1645. {
  1646. if (displayObject != null)
  1647. {
  1648. float xv = _x;
  1649. float yv = _y;
  1650. if (!_pivotAsAnchor)
  1651. {
  1652. xv += _width * _pivotX;
  1653. yv += _height * _pivotY;
  1654. }
  1655. displayObject.location = new Vector3(xv, yv, _z);
  1656. }
  1657. }
  1658. virtual protected void HandleSizeChanged()
  1659. {
  1660. if (displayObject != null)
  1661. displayObject.SetSize(_width, _height);
  1662. }
  1663. virtual protected void HandleScaleChanged()
  1664. {
  1665. if (displayObject != null)
  1666. displayObject.SetScale(_scaleX, _scaleY);
  1667. }
  1668. virtual protected void HandleGrayedChanged()
  1669. {
  1670. if (displayObject != null)
  1671. displayObject.grayed = _grayed;
  1672. }
  1673. virtual protected void HandleAlphaChanged()
  1674. {
  1675. if (displayObject != null)
  1676. displayObject.alpha = _alpha;
  1677. }
  1678. virtual internal protected void HandleVisibleChanged()
  1679. {
  1680. if (displayObject != null)
  1681. displayObject.visible = internalVisible2;
  1682. }
  1683. virtual public void ConstructFromResource()
  1684. {
  1685. }
  1686. virtual public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  1687. {
  1688. buffer.Seek(beginPos, 0);
  1689. buffer.Skip(5);
  1690. id = buffer.ReadS();
  1691. name = buffer.ReadS();
  1692. float f1 = buffer.ReadInt();
  1693. float f2 = buffer.ReadInt();
  1694. SetXY(f1, f2);
  1695. if (buffer.ReadBool())
  1696. {
  1697. initWidth = buffer.ReadInt();
  1698. initHeight = buffer.ReadInt();
  1699. SetSize(initWidth, initHeight, true);
  1700. }
  1701. if (buffer.ReadBool())
  1702. {
  1703. minWidth = buffer.ReadInt();
  1704. maxWidth = buffer.ReadInt();
  1705. minHeight = buffer.ReadInt();
  1706. maxHeight = buffer.ReadInt();
  1707. }
  1708. if (buffer.ReadBool())
  1709. {
  1710. f1 = buffer.ReadFloat();
  1711. f2 = buffer.ReadFloat();
  1712. SetScale(f1, f2);
  1713. }
  1714. if (buffer.ReadBool())
  1715. {
  1716. f1 = buffer.ReadFloat();
  1717. f2 = buffer.ReadFloat();
  1718. this.skew = new Vector2(f1, f2);
  1719. }
  1720. if (buffer.ReadBool())
  1721. {
  1722. f1 = buffer.ReadFloat();
  1723. f2 = buffer.ReadFloat();
  1724. SetPivot(f1, f2, buffer.ReadBool());
  1725. }
  1726. f1 = buffer.ReadFloat();
  1727. if (f1 != 1)
  1728. this.alpha = f1;
  1729. f1 = buffer.ReadFloat();
  1730. if (f1 != 0)
  1731. this.rotation = f1;
  1732. if (!buffer.ReadBool())
  1733. this.visible = false;
  1734. if (!buffer.ReadBool())
  1735. this.touchable = false;
  1736. if (buffer.ReadBool())
  1737. this.grayed = true;
  1738. this.blendMode = (BlendMode)buffer.ReadByte();
  1739. int filter = buffer.ReadByte();
  1740. if (filter == 1)
  1741. {
  1742. ColorFilter cf = new ColorFilter();
  1743. this.filter = cf;
  1744. cf.AdjustBrightness(buffer.ReadFloat());
  1745. cf.AdjustContrast(buffer.ReadFloat());
  1746. cf.AdjustSaturation(buffer.ReadFloat());
  1747. cf.AdjustHue(buffer.ReadFloat());
  1748. }
  1749. string str = buffer.ReadS();
  1750. if (str != null)
  1751. this.data = str;
  1752. }
  1753. virtual public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  1754. {
  1755. buffer.Seek(beginPos, 1);
  1756. string str = buffer.ReadS();
  1757. if (str != null)
  1758. this.tooltips = str;
  1759. int groupId = buffer.ReadShort();
  1760. if (groupId >= 0)
  1761. group = parent.GetChildAt(groupId) as GGroup;
  1762. buffer.Seek(beginPos, 2);
  1763. int cnt = buffer.ReadShort();
  1764. for (int i = 0; i < cnt; i++)
  1765. {
  1766. int nextPos = buffer.ReadShort();
  1767. nextPos += buffer.position;
  1768. GearBase gear = GetGear(buffer.ReadByte());
  1769. gear.Setup(buffer);
  1770. buffer.position = nextPos;
  1771. }
  1772. }
  1773. #region Drag support
  1774. Vector2 _dragTouchStartPos;
  1775. bool _dragTesting;
  1776. static Vector2 sGlobalDragStart = new Vector2();
  1777. static Rect sGlobalRect = new Rect();
  1778. static bool sUpdateInDragging = false;
  1779. private void InitDrag()
  1780. {
  1781. if (_draggable)
  1782. {
  1783. onTouchBegin.Add(__touchBegin);
  1784. onTouchMove.Add(__touchMove);
  1785. onTouchEnd.Add(__touchEnd);
  1786. }
  1787. else
  1788. {
  1789. onTouchBegin.Remove(__touchBegin);
  1790. onTouchMove.Remove(__touchMove);
  1791. onTouchEnd.Remove(__touchEnd);
  1792. }
  1793. }
  1794. private void DragBegin(int touchId)
  1795. {
  1796. if (DispatchEvent("onDragStart", touchId))
  1797. return;
  1798. if (draggingObject != null)
  1799. {
  1800. GObject tmp = draggingObject;
  1801. draggingObject.StopDrag();
  1802. draggingObject = null;
  1803. tmp.DispatchEvent("onDragEnd", null);
  1804. }
  1805. onTouchMove.Add(__touchMove);
  1806. onTouchEnd.Add(__touchEnd);
  1807. sGlobalDragStart = Stage.inst.GetTouchPosition(touchId);
  1808. sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
  1809. _dragTesting = false;
  1810. draggingObject = this;
  1811. Stage.inst.AddTouchMonitor(touchId, this);
  1812. }
  1813. private void DragEnd()
  1814. {
  1815. if (draggingObject == this)
  1816. {
  1817. _dragTesting = false;
  1818. draggingObject = null;
  1819. }
  1820. }
  1821. private void __touchBegin(EventContext context)
  1822. {
  1823. if ((Stage.inst.focus is InputTextField) && ((InputTextField)Stage.inst.focus).editable)
  1824. {
  1825. _dragTesting = false;
  1826. return;
  1827. }
  1828. InputEvent evt = context.inputEvent;
  1829. _dragTouchStartPos = evt.position;
  1830. _dragTesting = true;
  1831. context.CaptureTouch();
  1832. }
  1833. private void __touchMove(EventContext context)
  1834. {
  1835. InputEvent evt = context.inputEvent;
  1836. if (_dragTesting && draggingObject != this)
  1837. {
  1838. int sensitivity;
  1839. if (Stage.touchScreen)
  1840. sensitivity = UIConfig.touchDragSensitivity;
  1841. else
  1842. sensitivity = UIConfig.clickDragSensitivity;
  1843. if (Mathf.Abs(_dragTouchStartPos.x - evt.x) < sensitivity
  1844. && Mathf.Abs(_dragTouchStartPos.y - evt.y) < sensitivity)
  1845. return;
  1846. _dragTesting = false;
  1847. DragBegin(evt.touchId);
  1848. }
  1849. if (draggingObject == this)
  1850. {
  1851. float xx = evt.x - sGlobalDragStart.x + sGlobalRect.x;
  1852. float yy = evt.y - sGlobalDragStart.y + sGlobalRect.y;
  1853. if (dragBounds != null)
  1854. {
  1855. Rect rect = GRoot.inst.LocalToGlobal((Rect)dragBounds);
  1856. if (xx < rect.x)
  1857. xx = rect.x;
  1858. else if (xx + sGlobalRect.width > rect.xMax)
  1859. {
  1860. xx = rect.xMax - sGlobalRect.width;
  1861. if (xx < rect.x)
  1862. xx = rect.x;
  1863. }
  1864. if (yy < rect.y)
  1865. yy = rect.y;
  1866. else if (yy + sGlobalRect.height > rect.yMax)
  1867. {
  1868. yy = rect.yMax - sGlobalRect.height;
  1869. if (yy < rect.y)
  1870. yy = rect.y;
  1871. }
  1872. }
  1873. Vector2 pt = this.parent.GlobalToLocal(new Vector2(xx, yy));
  1874. if (float.IsNaN(pt.x))
  1875. return;
  1876. sUpdateInDragging = true;
  1877. this.SetXY(Mathf.RoundToInt(pt.x), Mathf.RoundToInt(pt.y));
  1878. sUpdateInDragging = false;
  1879. DispatchEvent("onDragMove", null);
  1880. }
  1881. }
  1882. private void __touchEnd(EventContext context)
  1883. {
  1884. if (draggingObject == this)
  1885. {
  1886. draggingObject = null;
  1887. DispatchEvent("onDragEnd", null);
  1888. }
  1889. }
  1890. #endregion
  1891. #region Tween Helpers
  1892. public GTweener TweenMove(Vector2 endValue, float duration)
  1893. {
  1894. return GTween.To(this.xy, endValue, duration).SetTarget(this, TweenPropType.XY);
  1895. }
  1896. public GTweener TweenMoveX(float endValue, float duration)
  1897. {
  1898. return GTween.To(_x, endValue, duration).SetTarget(this, TweenPropType.X);
  1899. }
  1900. public GTweener TweenMoveY(float endValue, float duration)
  1901. {
  1902. return GTween.To(_y, endValue, duration).SetTarget(this, TweenPropType.Y);
  1903. }
  1904. public GTweener TweenScale(Vector2 endValue, float duration)
  1905. {
  1906. return GTween.To(this.scale, endValue, duration).SetTarget(this, TweenPropType.Scale);
  1907. }
  1908. public GTweener TweenScaleX(float endValue, float duration)
  1909. {
  1910. return GTween.To(_scaleX, endValue, duration).SetTarget(this, TweenPropType.ScaleX);
  1911. }
  1912. public GTweener TweenScaleY(float endValue, float duration)
  1913. {
  1914. return GTween.To(_scaleY, endValue, duration).SetTarget(this, TweenPropType.ScaleY);
  1915. }
  1916. public GTweener TweenResize(Vector2 endValue, float duration)
  1917. {
  1918. return GTween.To(this.size, endValue, duration).SetTarget(this, TweenPropType.Size);
  1919. }
  1920. public GTweener TweenFade(float endValue, float duration)
  1921. {
  1922. return GTween.To(_alpha, endValue, duration).SetTarget(this, TweenPropType.Alpha);
  1923. }
  1924. public GTweener TweenRotate(float endValue, float duration)
  1925. {
  1926. return GTween.To(_rotation, endValue, duration).SetTarget(this, TweenPropType.Rotation);
  1927. }
  1928. #endregion
  1929. }
  1930. }