GObject.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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. //这里做了最大宽度适配
  415. float targetWidth;
  416. float maxAspectRatio = 1080 * 1.0f / 1920;
  417. if (Screen.width * 1.0f / Screen.height > maxAspectRatio)
  418. {
  419. targetWidth = GRoot.inst.height * maxAspectRatio;
  420. this.Center();
  421. }
  422. else
  423. {
  424. targetWidth = GRoot.inst.width;
  425. }
  426. this.SetSize(targetWidth, GRoot.inst.height);
  427. }
  428. /// <summary>
  429. /// The width of the object in pixels.
  430. /// </summary>
  431. public float width
  432. {
  433. get
  434. {
  435. return _width;
  436. }
  437. set
  438. {
  439. SetSize(value, _rawHeight);
  440. }
  441. }
  442. /// <summary>
  443. /// The height of the object in pixels.
  444. /// </summary>
  445. public float height
  446. {
  447. get
  448. {
  449. return _height;
  450. }
  451. set
  452. {
  453. SetSize(_rawWidth, value);
  454. }
  455. }
  456. /// <summary>
  457. /// The size of the object in pixels.
  458. /// </summary>
  459. public Vector2 size
  460. {
  461. get { return new Vector2(width, height); }
  462. set { SetSize(value.x, value.y); }
  463. }
  464. /// <summary>
  465. /// actualWidth = width * scalex
  466. /// </summary>
  467. public float actualWidth
  468. {
  469. get { return this.width * _scaleX; }
  470. }
  471. /// <summary>
  472. /// actualHeight = height * scaleY
  473. /// </summary>
  474. public float actualHeight
  475. {
  476. get { return this.height * _scaleY; }
  477. }
  478. /// <summary>
  479. /// Change size.
  480. /// </summary>
  481. /// <param name="wv">Width value.</param>
  482. /// <param name="hv">Height value.</param>
  483. public void SetSize(float wv, float hv)
  484. {
  485. SetSize(wv, hv, false);
  486. }
  487. /// <summary>
  488. /// Change size.
  489. /// </summary>
  490. /// <param name="wv">Width value.</param>
  491. /// <param name="hv">Height value.</param>
  492. /// <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>
  493. public void SetSize(float wv, float hv, bool ignorePivot)
  494. {
  495. if (_rawWidth != wv || _rawHeight != hv)
  496. {
  497. _rawWidth = wv;
  498. _rawHeight = hv;
  499. if (wv < minWidth)
  500. wv = minWidth;
  501. else if (maxWidth > 0 && wv > maxWidth)
  502. wv = maxWidth;
  503. if (hv < minHeight)
  504. hv = minHeight;
  505. else if (maxHeight > 0 && hv > maxHeight)
  506. hv = maxHeight;
  507. float dWidth = wv - _width;
  508. float dHeight = hv - _height;
  509. _width = wv;
  510. _height = hv;
  511. HandleSizeChanged();
  512. if (_pivotX != 0 || _pivotY != 0)
  513. {
  514. if (!_pivotAsAnchor)
  515. {
  516. if (!ignorePivot)
  517. SetXY(_x - _pivotX * dWidth, _y - _pivotY * dHeight);
  518. else
  519. HandlePositionChanged();
  520. }
  521. else
  522. HandlePositionChanged();
  523. }
  524. if (this is GGroup)
  525. ((GGroup)this).ResizeChildren(dWidth, dHeight);
  526. UpdateGear(2);
  527. if (parent != null)
  528. {
  529. relations.OnOwnerSizeChanged(dWidth, dHeight, _pivotAsAnchor || !ignorePivot);
  530. parent.SetBoundsChangedFlag();
  531. if (_group != null)
  532. _group.SetBoundsChangedFlag();
  533. }
  534. DispatchEvent("onSizeChanged", null);
  535. }
  536. }
  537. protected void SetSizeDirectly(float wv, float hv)
  538. {
  539. _rawWidth = wv;
  540. _rawHeight = hv;
  541. if (wv < 0)
  542. wv = 0;
  543. if (hv < 0)
  544. hv = 0;
  545. _width = wv;
  546. _height = hv;
  547. }
  548. /// <summary>
  549. ///
  550. /// </summary>
  551. public float xMin
  552. {
  553. get
  554. {
  555. return _pivotAsAnchor ? (_x - _width * _pivotX) : _x;
  556. }
  557. set
  558. {
  559. if (_pivotAsAnchor)
  560. SetPosition(value + _width * _pivotX, _y, _z);
  561. else
  562. SetPosition(value, _y, _z);
  563. }
  564. }
  565. /// <summary>
  566. ///
  567. /// </summary>
  568. public float yMin
  569. {
  570. get
  571. {
  572. return _pivotAsAnchor ? (_y - _height * _pivotY) : _y;
  573. }
  574. set
  575. {
  576. if (_pivotAsAnchor)
  577. SetPosition(_x, value + _height * _pivotY, _z);
  578. else
  579. SetPosition(_x, value, _z);
  580. }
  581. }
  582. /// <summary>
  583. /// The horizontal scale factor. '1' means no scale, cannt be negative.
  584. /// </summary>
  585. public float scaleX
  586. {
  587. get { return _scaleX; }
  588. set
  589. {
  590. SetScale(value, _scaleY);
  591. }
  592. }
  593. /// <summary>
  594. /// The vertical scale factor. '1' means no scale, cannt be negative.
  595. /// </summary>
  596. public float scaleY
  597. {
  598. get { return _scaleY; }
  599. set
  600. {
  601. SetScale(_scaleX, value);
  602. }
  603. }
  604. /// <summary>
  605. /// The scale factor.
  606. /// </summary>
  607. public Vector2 scale
  608. {
  609. get { return new Vector2(_scaleX, _scaleY); }
  610. set { SetScale(value.x, value.y); }
  611. }
  612. /// <summary>
  613. /// Change the scale factor.
  614. /// </summary>
  615. /// <param name="wv">The horizontal scale factor.</param>
  616. /// <param name="hv">The vertical scale factor</param>
  617. public void SetScale(float wv, float hv)
  618. {
  619. if (_scaleX != wv || _scaleY != hv)
  620. {
  621. _scaleX = wv;
  622. _scaleY = hv;
  623. HandleScaleChanged();
  624. UpdateGear(2);
  625. }
  626. }
  627. /// <summary>
  628. ///
  629. /// </summary>
  630. public Vector2 skew
  631. {
  632. get
  633. {
  634. if (displayObject != null)
  635. return displayObject.skew;
  636. else
  637. return Vector2.zero;
  638. }
  639. set
  640. {
  641. if (displayObject != null)
  642. displayObject.skew = value;
  643. }
  644. }
  645. /// <summary>
  646. /// The x coordinate of the object's origin in its own coordinate space.
  647. /// </summary>
  648. public float pivotX
  649. {
  650. get { return _pivotX; }
  651. set
  652. {
  653. SetPivot(value, _pivotY, _pivotAsAnchor);
  654. }
  655. }
  656. /// <summary>
  657. /// The y coordinate of the object's origin in its own coordinate space.
  658. /// </summary>
  659. public float pivotY
  660. {
  661. get { return _pivotY; }
  662. set
  663. {
  664. SetPivot(_pivotX, value, _pivotAsAnchor);
  665. }
  666. }
  667. /// <summary>
  668. /// The x and y coordinates of the object's origin in its own coordinate space.
  669. /// </summary>
  670. public Vector2 pivot
  671. {
  672. get { return new Vector2(_pivotX, _pivotY); }
  673. set { SetPivot(value.x, value.y, _pivotAsAnchor); }
  674. }
  675. public bool pivotAsAnchor
  676. {
  677. get { return _pivotAsAnchor; }
  678. set
  679. {
  680. SetPivot(_pivotX, _pivotY, value);
  681. }
  682. }
  683. /// <summary>
  684. /// Change the x and y coordinates of the object's origin in its own coordinate space.
  685. /// </summary>
  686. /// <param name="xv">x value in ratio</param>
  687. /// <param name="yv">y value in ratio</param>
  688. public void SetPivot(float xv, float yv)
  689. {
  690. SetPivot(xv, yv, false);
  691. }
  692. /// <summary>
  693. /// Change the x and y coordinates of the object's origin in its own coordinate space.
  694. /// </summary>
  695. /// <param name="xv">x value in ratio</param>
  696. /// <param name="yv">y value in ratio</param>
  697. /// <param name="asAnchor">If use the pivot as the anchor position</param>
  698. public void SetPivot(float xv, float yv, bool asAnchor)
  699. {
  700. if (_pivotX != xv || _pivotY != yv || _pivotAsAnchor != asAnchor)
  701. {
  702. _pivotX = xv;
  703. _pivotY = yv;
  704. _pivotAsAnchor = asAnchor;
  705. if (displayObject != null)
  706. displayObject.pivot = new Vector2(_pivotX, _pivotY);
  707. HandlePositionChanged();
  708. }
  709. }
  710. /// <summary>
  711. /// If the object can touch or click. GImage/GTextField is not touchable even it is true.
  712. /// </summary>
  713. public bool touchable
  714. {
  715. get
  716. {
  717. return _touchable;
  718. }
  719. set
  720. {
  721. if (_touchable != value)
  722. {
  723. _touchable = value;
  724. UpdateGear(3);
  725. if (displayObject != null)
  726. displayObject.touchable = _touchable;
  727. }
  728. }
  729. }
  730. /// <summary>
  731. /// If true, apply a grayed effect on this object.
  732. /// </summary>
  733. public bool grayed
  734. {
  735. get
  736. {
  737. return _grayed;
  738. }
  739. set
  740. {
  741. if (_grayed != value)
  742. {
  743. _grayed = value;
  744. HandleGrayedChanged();
  745. UpdateGear(3);
  746. }
  747. }
  748. }
  749. /// <summary>
  750. /// Enabled is shortcut for grayed and !touchable combination.
  751. /// </summary>
  752. public bool enabled
  753. {
  754. get
  755. {
  756. return !_grayed && _touchable;
  757. }
  758. set
  759. {
  760. this.grayed = !value;
  761. this.touchable = value;
  762. }
  763. }
  764. /// <summary>
  765. /// The rotation around the z axis of the object in degrees.
  766. /// </summary>
  767. public float rotation
  768. {
  769. get
  770. {
  771. return _rotation;
  772. }
  773. set
  774. {
  775. _rotation = value;
  776. if (displayObject != null)
  777. displayObject.rotation = _rotation;
  778. UpdateGear(3);
  779. }
  780. }
  781. /// <summary>
  782. /// The rotation around the x axis of the object in degrees.
  783. /// </summary>
  784. public float rotationX
  785. {
  786. get
  787. {
  788. return _rotationX;
  789. }
  790. set
  791. {
  792. _rotationX = value;
  793. if (displayObject != null)
  794. displayObject.rotationX = _rotationX;
  795. }
  796. }
  797. /// <summary>
  798. /// The rotation around the y axis of the object in degrees.
  799. /// </summary>
  800. public float rotationY
  801. {
  802. get
  803. {
  804. return _rotationY;
  805. }
  806. set
  807. {
  808. _rotationY = value;
  809. if (displayObject != null)
  810. displayObject.rotationY = _rotationY;
  811. }
  812. }
  813. /// <summary>
  814. /// The opacity of the object. 0 = transparent, 1 = opaque.
  815. /// </summary>
  816. public float alpha
  817. {
  818. get
  819. {
  820. return _alpha;
  821. }
  822. set
  823. {
  824. _alpha = value;
  825. HandleAlphaChanged();
  826. UpdateGear(3);
  827. }
  828. }
  829. /// <summary>
  830. /// The visibility of the object. An invisible object will be untouchable.
  831. /// </summary>
  832. public bool visible
  833. {
  834. get
  835. {
  836. return _visible;
  837. }
  838. set
  839. {
  840. if (_visible != value)
  841. {
  842. _visible = value;
  843. HandleVisibleChanged();
  844. if (parent != null)
  845. parent.SetBoundsChangedFlag();
  846. if (_group != null && _group.excludeInvisibles)
  847. _group.SetBoundsChangedFlag();
  848. }
  849. }
  850. }
  851. internal bool internalVisible
  852. {
  853. get
  854. {
  855. return _internalVisible && (group == null || group.internalVisible);
  856. }
  857. }
  858. internal bool internalVisible2
  859. {
  860. get
  861. {
  862. return _visible && (group == null || group.internalVisible2);
  863. }
  864. }
  865. internal bool internalVisible3
  866. {
  867. get
  868. {
  869. return _visible && _internalVisible;
  870. }
  871. }
  872. /// <summary>
  873. /// By default(when sortingOrder==0), object added to component is arrange by the added roder.
  874. /// The bigger is the sorting order, the object is more in front.
  875. /// </summary>
  876. public int sortingOrder
  877. {
  878. get { return _sortingOrder; }
  879. set
  880. {
  881. if (value < 0)
  882. value = 0;
  883. if (_sortingOrder != value)
  884. {
  885. int old = _sortingOrder;
  886. _sortingOrder = value;
  887. if (parent != null)
  888. parent.ChildSortingOrderChanged(this, old, _sortingOrder);
  889. }
  890. }
  891. }
  892. /// <summary>
  893. /// If the object can be focused?
  894. /// </summary>
  895. public bool focusable
  896. {
  897. get { return displayObject != null && displayObject.focusable; }
  898. set { if (displayObject != null) displayObject.focusable = value; }
  899. }
  900. /// <summary>
  901. /// If the object can be navigated by TAB?
  902. /// </summary>
  903. public bool tabStop
  904. {
  905. get { return displayObject != null && displayObject.tabStop; }
  906. set { if (displayObject != null) displayObject.tabStop = value; }
  907. }
  908. /// <summary>
  909. /// If the object is focused.
  910. /// </summary>
  911. public bool focused
  912. {
  913. get { return displayObject != null && displayObject.focused; }
  914. }
  915. /// <summary>
  916. /// Request focus on this object.
  917. /// </summary>
  918. public void RequestFocus()
  919. {
  920. if (displayObject != null)
  921. Stage.inst.SetFocus(displayObject, false);
  922. }
  923. /// <summary>
  924. /// Request focus on this object.
  925. /// </summary>
  926. public void RequestFocus(bool byKey)
  927. {
  928. if (displayObject != null)
  929. Stage.inst.SetFocus(displayObject, byKey);
  930. }
  931. /// <summary>
  932. /// Tooltips of this object. UIConfig.tooltipsWin must be set first.
  933. /// </summary>
  934. public string tooltips
  935. {
  936. get { return _tooltips; }
  937. set
  938. {
  939. if (!string.IsNullOrEmpty(_tooltips))
  940. {
  941. this.onRollOver.Remove(__rollOver);
  942. this.onRollOut.Remove(__rollOut);
  943. }
  944. _tooltips = value;
  945. if (!string.IsNullOrEmpty(_tooltips))
  946. {
  947. this.onRollOver.Add(__rollOver);
  948. this.onRollOut.Add(__rollOut);
  949. }
  950. }
  951. }
  952. /// <summary>
  953. ///
  954. /// </summary>
  955. /// <value></value>
  956. public string cursor
  957. {
  958. get { return displayObject != null ? displayObject.cursor : null; }
  959. set
  960. {
  961. if (displayObject != null)
  962. displayObject.cursor = value;
  963. }
  964. }
  965. private void __rollOver()
  966. {
  967. this.root.ShowTooltips(tooltips);
  968. }
  969. private void __rollOut()
  970. {
  971. this.root.HideTooltips();
  972. }
  973. /// <summary>
  974. ///
  975. /// </summary>
  976. virtual public IFilter filter
  977. {
  978. get { return displayObject != null ? displayObject.filter : null; }
  979. set { if (displayObject != null) displayObject.filter = value; }
  980. }
  981. /// <summary>
  982. ///
  983. /// </summary>
  984. virtual public BlendMode blendMode
  985. {
  986. get { return displayObject != null ? displayObject.blendMode : BlendMode.None; }
  987. set { if (displayObject != null) displayObject.blendMode = value; }
  988. }
  989. /// <summary>
  990. /// 设定GameObject的名称
  991. /// </summary>
  992. public string gameObjectName
  993. {
  994. get
  995. {
  996. if (displayObject != null && displayObject.gameObject != null)
  997. return displayObject.gameObject.name;
  998. else
  999. return null;
  1000. }
  1001. set
  1002. {
  1003. if (displayObject != null && displayObject.gameObject != null)
  1004. {
  1005. displayObject.gameObject.name = value;
  1006. }
  1007. }
  1008. }
  1009. /// <summary>
  1010. ///
  1011. /// </summary>
  1012. /// <param name="obj"></param>
  1013. public void SetHome(GObject obj)
  1014. {
  1015. if (obj != null && displayObject != null && obj.displayObject != null)
  1016. displayObject.home = obj.displayObject.cachedTransform;
  1017. }
  1018. /// <summary>
  1019. /// If the object has lowlevel displayobject and the displayobject has a container parent?
  1020. /// </summary>
  1021. public bool inContainer
  1022. {
  1023. get
  1024. {
  1025. return displayObject != null && displayObject.parent != null;
  1026. }
  1027. }
  1028. /// <summary>
  1029. /// If the object is on stage.
  1030. /// </summary>
  1031. public bool onStage
  1032. {
  1033. get
  1034. {
  1035. return displayObject != null && displayObject.stage != null;
  1036. }
  1037. }
  1038. /// <summary>
  1039. /// Resource url of this object.
  1040. /// </summary>
  1041. public string resourceURL
  1042. {
  1043. get
  1044. {
  1045. if (packageItem != null)
  1046. return UIPackage.URL_PREFIX + packageItem.owner.id + packageItem.id;
  1047. else
  1048. return null;
  1049. }
  1050. }
  1051. /// <summary>
  1052. /// Gear to xy controller.
  1053. /// </summary>
  1054. public GearXY gearXY
  1055. {
  1056. get
  1057. {
  1058. return (GearXY)GetGear(1);
  1059. }
  1060. }
  1061. /// <summary>
  1062. /// Gear to size controller.
  1063. /// </summary>
  1064. public GearSize gearSize
  1065. {
  1066. get
  1067. {
  1068. return (GearSize)GetGear(2);
  1069. }
  1070. }
  1071. /// <summary>
  1072. /// Gear to look controller.
  1073. /// </summary>
  1074. public GearLook gearLook
  1075. {
  1076. get
  1077. {
  1078. return (GearLook)GetGear(3);
  1079. }
  1080. }
  1081. public GearBase GetGear(int index)
  1082. {
  1083. GearBase gear = _gears[index];
  1084. if (gear == null)
  1085. {
  1086. switch (index)
  1087. {
  1088. case 0:
  1089. gear = new GearDisplay(this);
  1090. break;
  1091. case 1:
  1092. gear = new GearXY(this);
  1093. break;
  1094. case 2:
  1095. gear = new GearSize(this);
  1096. break;
  1097. case 3:
  1098. gear = new GearLook(this);
  1099. break;
  1100. case 4:
  1101. gear = new GearColor(this);
  1102. break;
  1103. case 5:
  1104. gear = new GearAnimation(this);
  1105. break;
  1106. case 6:
  1107. gear = new GearText(this);
  1108. break;
  1109. case 7:
  1110. gear = new GearIcon(this);
  1111. break;
  1112. case 8:
  1113. gear = new GearDisplay2(this);
  1114. break;
  1115. case 9:
  1116. gear = new GearFontSize(this);
  1117. break;
  1118. default:
  1119. throw new System.Exception("FairyGUI: invalid gear index!");
  1120. }
  1121. _gears[index] = gear;
  1122. }
  1123. return gear;
  1124. }
  1125. protected void UpdateGear(int index)
  1126. {
  1127. if (underConstruct || _gearLocked)
  1128. return;
  1129. GearBase gear = _gears[index];
  1130. if (gear != null && gear.controller != null)
  1131. gear.UpdateState();
  1132. }
  1133. internal bool CheckGearController(int index, Controller c)
  1134. {
  1135. return _gears[index] != null && _gears[index].controller == c;
  1136. }
  1137. internal void UpdateGearFromRelations(int index, float dx, float dy)
  1138. {
  1139. if (_gears[index] != null)
  1140. _gears[index].UpdateFromRelations(dx, dy);
  1141. }
  1142. internal uint AddDisplayLock()
  1143. {
  1144. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1145. if (gearDisplay != null && gearDisplay.controller != null)
  1146. {
  1147. uint ret = gearDisplay.AddLock();
  1148. CheckGearDisplay();
  1149. return ret;
  1150. }
  1151. else
  1152. return 0;
  1153. }
  1154. internal void ReleaseDisplayLock(uint token)
  1155. {
  1156. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1157. if (gearDisplay != null && gearDisplay.controller != null)
  1158. {
  1159. gearDisplay.ReleaseLock(token);
  1160. CheckGearDisplay();
  1161. }
  1162. }
  1163. void CheckGearDisplay()
  1164. {
  1165. if (_handlingController)
  1166. return;
  1167. bool connected = _gears[0] == null || ((GearDisplay)_gears[0]).connected;
  1168. if (_gears[8] != null)
  1169. connected = ((GearDisplay2)_gears[8]).Evaluate(connected);
  1170. if (connected != _internalVisible)
  1171. {
  1172. _internalVisible = connected;
  1173. if (parent != null)
  1174. parent.ChildStateChanged(this);
  1175. if (_group != null && _group.excludeInvisibles)
  1176. _group.SetBoundsChangedFlag();
  1177. }
  1178. }
  1179. /// <summary>
  1180. /// Mark the fairy batching state is invalid.
  1181. /// </summary>
  1182. public void InvalidateBatchingState()
  1183. {
  1184. if (displayObject != null)
  1185. displayObject.InvalidateBatchingState();
  1186. else if ((this is GGroup) && parent != null)
  1187. parent.container.InvalidateBatchingState(true);
  1188. }
  1189. virtual public void HandleControllerChanged(Controller c)
  1190. {
  1191. _handlingController = true;
  1192. for (int i = 0; i < 10; i++)
  1193. {
  1194. GearBase gear = _gears[i];
  1195. if (gear != null && gear.controller == c)
  1196. gear.Apply();
  1197. }
  1198. _handlingController = false;
  1199. CheckGearDisplay();
  1200. }
  1201. /// <summary>
  1202. ///
  1203. /// </summary>
  1204. /// <param name="target"></param>
  1205. /// <param name="relationType"></param>
  1206. public void AddRelation(GObject target, RelationType relationType)
  1207. {
  1208. AddRelation(target, relationType, false);
  1209. }
  1210. /// <summary>
  1211. ///
  1212. /// </summary>
  1213. /// <param name="target"></param>
  1214. /// <param name="relationType"></param>
  1215. /// <param name="usePercent"></param>
  1216. public void AddRelation(GObject target, RelationType relationType, bool usePercent)
  1217. {
  1218. relations.Add(target, relationType, usePercent);
  1219. }
  1220. /// <summary>
  1221. ///
  1222. /// </summary>
  1223. /// <param name="target"></param>
  1224. /// <param name="relationType"></param>
  1225. public void RemoveRelation(GObject target, RelationType relationType)
  1226. {
  1227. relations.Remove(target, relationType);
  1228. }
  1229. /// <summary>
  1230. ///
  1231. /// </summary>
  1232. public void RemoveFromParent()
  1233. {
  1234. if (parent != null)
  1235. parent.RemoveChild(this);
  1236. }
  1237. /// <summary>
  1238. /// Group belonging to.
  1239. /// </summary>
  1240. public GGroup group
  1241. {
  1242. get { return _group; }
  1243. set
  1244. {
  1245. if (_group != value)
  1246. {
  1247. if (_group != null)
  1248. _group.SetBoundsChangedFlag();
  1249. _group = value;
  1250. if (_group != null)
  1251. _group.SetBoundsChangedFlag();
  1252. HandleVisibleChanged();
  1253. if (parent != null)
  1254. parent.ChildStateChanged(this);
  1255. }
  1256. }
  1257. }
  1258. /// <summary>
  1259. ///
  1260. /// </summary>
  1261. public GRoot root
  1262. {
  1263. get
  1264. {
  1265. GObject p = this;
  1266. while (p.parent != null)
  1267. p = p.parent;
  1268. if (p is GRoot)
  1269. return (GRoot)p;
  1270. if (p.displayObject != null && p.displayObject.parent != null)
  1271. {
  1272. DisplayObject d = p.displayObject.parent.GetChild("GRoot");
  1273. if (d != null && (d.gOwner is GRoot))
  1274. return (GRoot)d.gOwner;
  1275. }
  1276. return GRoot.inst;
  1277. }
  1278. }
  1279. /// <summary>
  1280. ///
  1281. /// </summary>
  1282. virtual public string text
  1283. {
  1284. get { return null; }
  1285. set { /*override in child*/}
  1286. }
  1287. /// <summary>
  1288. ///
  1289. /// </summary>
  1290. virtual public string icon
  1291. {
  1292. get { return null; }
  1293. set { /*override in child*/}
  1294. }
  1295. /// <summary>
  1296. ///
  1297. /// </summary>
  1298. public bool draggable
  1299. {
  1300. get { return _draggable; }
  1301. set
  1302. {
  1303. if (_draggable != value)
  1304. {
  1305. _draggable = value;
  1306. InitDrag();
  1307. }
  1308. }
  1309. }
  1310. /// <summary>
  1311. ///
  1312. /// </summary>
  1313. public void StartDrag()
  1314. {
  1315. StartDrag(-1);
  1316. }
  1317. /// <summary>
  1318. ///
  1319. /// </summary>
  1320. /// <param name="touchId"></param>
  1321. public void StartDrag(int touchId)
  1322. {
  1323. if (displayObject.stage == null)
  1324. return;
  1325. DragBegin(touchId);
  1326. }
  1327. /// <summary>
  1328. ///
  1329. /// </summary>
  1330. public void StopDrag()
  1331. {
  1332. DragEnd();
  1333. }
  1334. /// <summary>
  1335. ///
  1336. /// </summary>
  1337. public bool dragging
  1338. {
  1339. get { return draggingObject == this; }
  1340. }
  1341. /// <summary>
  1342. /// Transforms a point from the local coordinate system to global (Stage) coordinates.
  1343. /// </summary>
  1344. /// <param name="pt"></param>
  1345. /// <returns></returns>
  1346. public Vector2 LocalToGlobal(Vector2 pt)
  1347. {
  1348. if (_pivotAsAnchor)
  1349. {
  1350. pt.x += _width * _pivotX;
  1351. pt.y += _height * _pivotY;
  1352. }
  1353. return displayObject.LocalToGlobal(pt);
  1354. }
  1355. /// <summary>
  1356. /// Transforms a point from global (Stage) coordinates to the local coordinate system.
  1357. /// </summary>
  1358. /// <param name="pt"></param>
  1359. /// <returns></returns>
  1360. public Vector2 GlobalToLocal(Vector2 pt)
  1361. {
  1362. pt = displayObject.GlobalToLocal(pt);
  1363. if (_pivotAsAnchor)
  1364. {
  1365. pt.x -= _width * _pivotX;
  1366. pt.y -= _height * _pivotY;
  1367. }
  1368. return pt;
  1369. }
  1370. /// <summary>
  1371. ///
  1372. /// </summary>
  1373. /// <param name="rect"></param>
  1374. /// <returns></returns>
  1375. public Rect LocalToGlobal(Rect rect)
  1376. {
  1377. Rect ret = new Rect();
  1378. Vector2 v = this.LocalToGlobal(new Vector2(rect.xMin, rect.yMin));
  1379. ret.xMin = v.x;
  1380. ret.yMin = v.y;
  1381. v = this.LocalToGlobal(new Vector2(rect.xMax, rect.yMax));
  1382. ret.xMax = v.x;
  1383. ret.yMax = v.y;
  1384. return ret;
  1385. }
  1386. /// <summary>
  1387. ///
  1388. /// </summary>
  1389. /// <param name="rect"></param>
  1390. /// <returns></returns>
  1391. public Rect GlobalToLocal(Rect rect)
  1392. {
  1393. Rect ret = new Rect();
  1394. Vector2 v = this.GlobalToLocal(new Vector2(rect.xMin, rect.yMin));
  1395. ret.xMin = v.x;
  1396. ret.yMin = v.y;
  1397. v = this.GlobalToLocal(new Vector2(rect.xMax, rect.yMax));
  1398. ret.xMax = v.x;
  1399. ret.yMax = v.y;
  1400. return ret;
  1401. }
  1402. /// <summary>
  1403. /// Transforms a point from the local coordinate system to GRoot coordinates.
  1404. /// </summary>
  1405. /// <param name="pt"></param>
  1406. /// <param name="r"></param>
  1407. /// <returns></returns>
  1408. public Vector2 LocalToRoot(Vector2 pt, GRoot r)
  1409. {
  1410. pt = LocalToGlobal(pt);
  1411. if (r == null || r == GRoot.inst)
  1412. {
  1413. //fast
  1414. pt.x /= UIContentScaler.scaleFactor;
  1415. pt.y /= UIContentScaler.scaleFactor;
  1416. return pt;
  1417. }
  1418. else
  1419. return r.GlobalToLocal(pt);
  1420. }
  1421. /// <summary>
  1422. /// Transforms a point from the GRoot coordinate to local coordinates system.
  1423. /// </summary>
  1424. /// <param name="pt"></param>
  1425. /// <param name="r"></param>
  1426. /// <returns></returns>
  1427. public Vector2 RootToLocal(Vector2 pt, GRoot r)
  1428. {
  1429. if (r == null || r == GRoot.inst)
  1430. {
  1431. //fast
  1432. pt.x *= UIContentScaler.scaleFactor;
  1433. pt.y *= UIContentScaler.scaleFactor;
  1434. }
  1435. else
  1436. pt = r.LocalToGlobal(pt);
  1437. return GlobalToLocal(pt);
  1438. }
  1439. /// <summary>
  1440. ///
  1441. /// </summary>
  1442. /// <param name="pt"></param>
  1443. /// <returns></returns>
  1444. public Vector2 WorldToLocal(Vector3 pt)
  1445. {
  1446. return WorldToLocal(pt, HitTestContext.cachedMainCamera);
  1447. }
  1448. /// <summary>
  1449. ///
  1450. /// </summary>
  1451. /// <param name="pt"></param>
  1452. /// <param name="camera"></param>
  1453. /// <returns></returns>
  1454. public Vector2 WorldToLocal(Vector3 pt, Camera camera)
  1455. {
  1456. Vector3 v = camera.WorldToScreenPoint(pt);
  1457. v.y = Screen.height - v.y;
  1458. v.z = 0;
  1459. return GlobalToLocal(v);
  1460. }
  1461. /// <summary>
  1462. ///
  1463. /// </summary>
  1464. /// <param name="point"></param>
  1465. /// <param name="targetSpace"></param>
  1466. /// <returns></returns>
  1467. public Vector2 TransformPoint(Vector2 pt, GObject targetSpace)
  1468. {
  1469. if (_pivotAsAnchor)
  1470. {
  1471. pt.x += _width * _pivotX;
  1472. pt.y += _height * _pivotY;
  1473. }
  1474. return this.displayObject.TransformPoint(pt, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1475. }
  1476. /// <summary>
  1477. ///
  1478. /// </summary>
  1479. /// <param name="rect"></param>
  1480. /// <param name="targetSpace"></param>
  1481. /// <returns></returns>
  1482. public Rect TransformRect(Rect rect, GObject targetSpace)
  1483. {
  1484. if (_pivotAsAnchor)
  1485. {
  1486. rect.x += _width * _pivotX;
  1487. rect.y += _height * _pivotY;
  1488. }
  1489. return this.displayObject.TransformRect(rect, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1490. }
  1491. /// <summary>
  1492. ///
  1493. /// </summary>
  1494. public bool isDisposed
  1495. {
  1496. get { return _disposed; }
  1497. }
  1498. /// <summary>
  1499. ///
  1500. /// </summary>
  1501. virtual public void Dispose()
  1502. {
  1503. if (_disposed)
  1504. return;
  1505. _disposed = true;
  1506. RemoveFromParent();
  1507. RemoveEventListeners();
  1508. relations.Dispose();
  1509. relations = null;
  1510. for (int i = 0; i < 10; i++)
  1511. {
  1512. GearBase gear = _gears[i];
  1513. if (gear != null)
  1514. gear.Dispose();
  1515. }
  1516. if (displayObject != null)
  1517. {
  1518. displayObject.gOwner = null;
  1519. displayObject.Dispose();
  1520. }
  1521. data = null;
  1522. }
  1523. /// <summary>
  1524. ///
  1525. /// </summary>
  1526. public GImage asImage
  1527. {
  1528. get { return this as GImage; }
  1529. }
  1530. /// <summary>
  1531. ///
  1532. /// </summary>
  1533. public GComponent asCom
  1534. {
  1535. get { return this as GComponent; }
  1536. }
  1537. /// <summary>
  1538. ///
  1539. /// </summary>
  1540. public GButton asButton
  1541. {
  1542. get { return this as GButton; }
  1543. }
  1544. /// <summary>
  1545. ///
  1546. /// </summary>
  1547. public GLabel asLabel
  1548. {
  1549. get { return this as GLabel; }
  1550. }
  1551. /// <summary>
  1552. ///
  1553. /// </summary>
  1554. public GProgressBar asProgress
  1555. {
  1556. get { return this as GProgressBar; }
  1557. }
  1558. /// <summary>
  1559. ///
  1560. /// </summary>
  1561. public GSlider asSlider
  1562. {
  1563. get { return this as GSlider; }
  1564. }
  1565. /// <summary>
  1566. ///
  1567. /// </summary>
  1568. public GComboBox asComboBox
  1569. {
  1570. get { return this as GComboBox; }
  1571. }
  1572. /// <summary>
  1573. ///
  1574. /// </summary>
  1575. public GTextField asTextField
  1576. {
  1577. get { return this as GTextField; }
  1578. }
  1579. /// <summary>
  1580. ///
  1581. /// </summary>
  1582. public GRichTextField asRichTextField
  1583. {
  1584. get { return this as GRichTextField; }
  1585. }
  1586. /// <summary>
  1587. ///
  1588. /// </summary>
  1589. public GTextInput asTextInput
  1590. {
  1591. get { return this as GTextInput; }
  1592. }
  1593. /// <summary>
  1594. ///
  1595. /// </summary>
  1596. public GLoader asLoader
  1597. {
  1598. get { return this as GLoader; }
  1599. }
  1600. /// <summary>
  1601. ///
  1602. /// </summary>
  1603. public GLoader3D asLoader3D
  1604. {
  1605. get { return this as GLoader3D; }
  1606. }
  1607. /// <summary>
  1608. ///
  1609. /// </summary>
  1610. public GList asList
  1611. {
  1612. get { return this as GList; }
  1613. }
  1614. /// <summary>
  1615. ///
  1616. /// </summary>
  1617. public GGraph asGraph
  1618. {
  1619. get { return this as GGraph; }
  1620. }
  1621. /// <summary>
  1622. ///
  1623. /// </summary>
  1624. public GGroup asGroup
  1625. {
  1626. get { return this as GGroup; }
  1627. }
  1628. /// <summary>
  1629. ///
  1630. /// </summary>
  1631. public GMovieClip asMovieClip
  1632. {
  1633. get { return this as GMovieClip; }
  1634. }
  1635. /// <summary>
  1636. ///
  1637. /// </summary>
  1638. public GTree asTree
  1639. {
  1640. get { return this as GTree; }
  1641. }
  1642. /// <summary>
  1643. ///
  1644. /// </summary>
  1645. public GTreeNode treeNode
  1646. {
  1647. get { return _treeNode; }
  1648. }
  1649. virtual protected void CreateDisplayObject()
  1650. {
  1651. }
  1652. internal void InternalSetParent(GComponent value)
  1653. {
  1654. parent = value;
  1655. }
  1656. virtual protected void HandlePositionChanged()
  1657. {
  1658. if (displayObject != null)
  1659. {
  1660. float xv = _x;
  1661. float yv = _y;
  1662. if (!_pivotAsAnchor)
  1663. {
  1664. xv += _width * _pivotX;
  1665. yv += _height * _pivotY;
  1666. }
  1667. displayObject.location = new Vector3(xv, yv, _z);
  1668. }
  1669. }
  1670. virtual protected void HandleSizeChanged()
  1671. {
  1672. if (displayObject != null)
  1673. displayObject.SetSize(_width, _height);
  1674. }
  1675. virtual protected void HandleScaleChanged()
  1676. {
  1677. if (displayObject != null)
  1678. displayObject.SetScale(_scaleX, _scaleY);
  1679. }
  1680. virtual protected void HandleGrayedChanged()
  1681. {
  1682. if (displayObject != null)
  1683. displayObject.grayed = _grayed;
  1684. }
  1685. virtual protected void HandleAlphaChanged()
  1686. {
  1687. if (displayObject != null)
  1688. displayObject.alpha = _alpha;
  1689. }
  1690. virtual internal protected void HandleVisibleChanged()
  1691. {
  1692. if (displayObject != null)
  1693. displayObject.visible = internalVisible2;
  1694. }
  1695. virtual public void ConstructFromResource()
  1696. {
  1697. }
  1698. virtual public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  1699. {
  1700. buffer.Seek(beginPos, 0);
  1701. buffer.Skip(5);
  1702. id = buffer.ReadS();
  1703. name = buffer.ReadS();
  1704. float f1 = buffer.ReadInt();
  1705. float f2 = buffer.ReadInt();
  1706. SetXY(f1, f2);
  1707. if (buffer.ReadBool())
  1708. {
  1709. initWidth = buffer.ReadInt();
  1710. initHeight = buffer.ReadInt();
  1711. SetSize(initWidth, initHeight, true);
  1712. }
  1713. if (buffer.ReadBool())
  1714. {
  1715. minWidth = buffer.ReadInt();
  1716. maxWidth = buffer.ReadInt();
  1717. minHeight = buffer.ReadInt();
  1718. maxHeight = buffer.ReadInt();
  1719. }
  1720. if (buffer.ReadBool())
  1721. {
  1722. f1 = buffer.ReadFloat();
  1723. f2 = buffer.ReadFloat();
  1724. SetScale(f1, f2);
  1725. }
  1726. if (buffer.ReadBool())
  1727. {
  1728. f1 = buffer.ReadFloat();
  1729. f2 = buffer.ReadFloat();
  1730. this.skew = new Vector2(f1, f2);
  1731. }
  1732. if (buffer.ReadBool())
  1733. {
  1734. f1 = buffer.ReadFloat();
  1735. f2 = buffer.ReadFloat();
  1736. SetPivot(f1, f2, buffer.ReadBool());
  1737. }
  1738. f1 = buffer.ReadFloat();
  1739. if (f1 != 1)
  1740. this.alpha = f1;
  1741. f1 = buffer.ReadFloat();
  1742. if (f1 != 0)
  1743. this.rotation = f1;
  1744. if (!buffer.ReadBool())
  1745. this.visible = false;
  1746. if (!buffer.ReadBool())
  1747. this.touchable = false;
  1748. if (buffer.ReadBool())
  1749. this.grayed = true;
  1750. this.blendMode = (BlendMode)buffer.ReadByte();
  1751. int filter = buffer.ReadByte();
  1752. if (filter == 1)
  1753. {
  1754. ColorFilter cf = new ColorFilter();
  1755. this.filter = cf;
  1756. cf.AdjustBrightness(buffer.ReadFloat());
  1757. cf.AdjustContrast(buffer.ReadFloat());
  1758. cf.AdjustSaturation(buffer.ReadFloat());
  1759. cf.AdjustHue(buffer.ReadFloat());
  1760. }
  1761. string str = buffer.ReadS();
  1762. if (str != null)
  1763. this.data = str;
  1764. }
  1765. virtual public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  1766. {
  1767. buffer.Seek(beginPos, 1);
  1768. string str = buffer.ReadS();
  1769. if (str != null)
  1770. this.tooltips = str;
  1771. int groupId = buffer.ReadShort();
  1772. if (groupId >= 0)
  1773. group = parent.GetChildAt(groupId) as GGroup;
  1774. buffer.Seek(beginPos, 2);
  1775. int cnt = buffer.ReadShort();
  1776. for (int i = 0; i < cnt; i++)
  1777. {
  1778. int nextPos = buffer.ReadShort();
  1779. nextPos += buffer.position;
  1780. GearBase gear = GetGear(buffer.ReadByte());
  1781. gear.Setup(buffer);
  1782. buffer.position = nextPos;
  1783. }
  1784. }
  1785. #region Drag support
  1786. Vector2 _dragTouchStartPos;
  1787. bool _dragTesting;
  1788. static Vector2 sGlobalDragStart = new Vector2();
  1789. static Rect sGlobalRect = new Rect();
  1790. static bool sUpdateInDragging = false;
  1791. private void InitDrag()
  1792. {
  1793. if (_draggable)
  1794. {
  1795. onTouchBegin.Add(__touchBegin);
  1796. onTouchMove.Add(__touchMove);
  1797. onTouchEnd.Add(__touchEnd);
  1798. }
  1799. else
  1800. {
  1801. onTouchBegin.Remove(__touchBegin);
  1802. onTouchMove.Remove(__touchMove);
  1803. onTouchEnd.Remove(__touchEnd);
  1804. }
  1805. }
  1806. private void DragBegin(int touchId)
  1807. {
  1808. if (DispatchEvent("onDragStart", touchId))
  1809. return;
  1810. if (draggingObject != null)
  1811. {
  1812. GObject tmp = draggingObject;
  1813. draggingObject.StopDrag();
  1814. draggingObject = null;
  1815. tmp.DispatchEvent("onDragEnd", null);
  1816. }
  1817. onTouchMove.Add(__touchMove);
  1818. onTouchEnd.Add(__touchEnd);
  1819. sGlobalDragStart = Stage.inst.GetTouchPosition(touchId);
  1820. sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
  1821. _dragTesting = false;
  1822. draggingObject = this;
  1823. Stage.inst.AddTouchMonitor(touchId, this);
  1824. }
  1825. private void DragEnd()
  1826. {
  1827. if (draggingObject == this)
  1828. {
  1829. _dragTesting = false;
  1830. draggingObject = null;
  1831. }
  1832. }
  1833. private void __touchBegin(EventContext context)
  1834. {
  1835. if ((Stage.inst.focus is InputTextField) && ((InputTextField)Stage.inst.focus).editable)
  1836. {
  1837. _dragTesting = false;
  1838. return;
  1839. }
  1840. InputEvent evt = context.inputEvent;
  1841. _dragTouchStartPos = evt.position;
  1842. _dragTesting = true;
  1843. context.CaptureTouch();
  1844. }
  1845. private void __touchMove(EventContext context)
  1846. {
  1847. InputEvent evt = context.inputEvent;
  1848. if (_dragTesting && draggingObject != this)
  1849. {
  1850. int sensitivity;
  1851. if (Stage.touchScreen)
  1852. sensitivity = UIConfig.touchDragSensitivity;
  1853. else
  1854. sensitivity = UIConfig.clickDragSensitivity;
  1855. if (Mathf.Abs(_dragTouchStartPos.x - evt.x) < sensitivity
  1856. && Mathf.Abs(_dragTouchStartPos.y - evt.y) < sensitivity)
  1857. return;
  1858. _dragTesting = false;
  1859. DragBegin(evt.touchId);
  1860. }
  1861. if (draggingObject == this)
  1862. {
  1863. float xx = evt.x - sGlobalDragStart.x + sGlobalRect.x;
  1864. float yy = evt.y - sGlobalDragStart.y + sGlobalRect.y;
  1865. if (dragBounds != null)
  1866. {
  1867. Rect rect = GRoot.inst.LocalToGlobal((Rect)dragBounds);
  1868. if (xx < rect.x)
  1869. xx = rect.x;
  1870. else if (xx + sGlobalRect.width > rect.xMax)
  1871. {
  1872. xx = rect.xMax - sGlobalRect.width;
  1873. if (xx < rect.x)
  1874. xx = rect.x;
  1875. }
  1876. if (yy < rect.y)
  1877. yy = rect.y;
  1878. else if (yy + sGlobalRect.height > rect.yMax)
  1879. {
  1880. yy = rect.yMax - sGlobalRect.height;
  1881. if (yy < rect.y)
  1882. yy = rect.y;
  1883. }
  1884. }
  1885. Vector2 pt = this.parent.GlobalToLocal(new Vector2(xx, yy));
  1886. if (float.IsNaN(pt.x))
  1887. return;
  1888. sUpdateInDragging = true;
  1889. this.SetXY(Mathf.RoundToInt(pt.x), Mathf.RoundToInt(pt.y));
  1890. sUpdateInDragging = false;
  1891. DispatchEvent("onDragMove", null);
  1892. }
  1893. }
  1894. private void __touchEnd(EventContext context)
  1895. {
  1896. if (draggingObject == this)
  1897. {
  1898. draggingObject = null;
  1899. DispatchEvent("onDragEnd", null);
  1900. }
  1901. }
  1902. #endregion
  1903. #region Tween Helpers
  1904. public GTweener TweenMove(Vector2 endValue, float duration)
  1905. {
  1906. return GTween.To(this.xy, endValue, duration).SetTarget(this, TweenPropType.XY);
  1907. }
  1908. public GTweener TweenMoveX(float endValue, float duration)
  1909. {
  1910. return GTween.To(_x, endValue, duration).SetTarget(this, TweenPropType.X);
  1911. }
  1912. public GTweener TweenMoveY(float endValue, float duration)
  1913. {
  1914. return GTween.To(_y, endValue, duration).SetTarget(this, TweenPropType.Y);
  1915. }
  1916. public GTweener TweenScale(Vector2 endValue, float duration)
  1917. {
  1918. return GTween.To(this.scale, endValue, duration).SetTarget(this, TweenPropType.Scale);
  1919. }
  1920. public GTweener TweenScaleX(float endValue, float duration)
  1921. {
  1922. return GTween.To(_scaleX, endValue, duration).SetTarget(this, TweenPropType.ScaleX);
  1923. }
  1924. public GTweener TweenScaleY(float endValue, float duration)
  1925. {
  1926. return GTween.To(_scaleY, endValue, duration).SetTarget(this, TweenPropType.ScaleY);
  1927. }
  1928. public GTweener TweenResize(Vector2 endValue, float duration)
  1929. {
  1930. return GTween.To(this.size, endValue, duration).SetTarget(this, TweenPropType.Size);
  1931. }
  1932. public GTweener TweenFade(float endValue, float duration)
  1933. {
  1934. return GTween.To(_alpha, endValue, duration).SetTarget(this, TweenPropType.Alpha);
  1935. }
  1936. public GTweener TweenRotate(float endValue, float duration)
  1937. {
  1938. return GTween.To(_rotation, endValue, duration).SetTarget(this, TweenPropType.Rotation);
  1939. }
  1940. #endregion
  1941. }
  1942. }