GObject.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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)
  985. return displayObject.gameObject.name;
  986. else
  987. return null;
  988. }
  989. set
  990. {
  991. if (displayObject != null)
  992. displayObject.gameObject.name = value;
  993. }
  994. }
  995. /// <summary>
  996. ///
  997. /// </summary>
  998. /// <param name="obj"></param>
  999. public void SetHome(GObject obj)
  1000. {
  1001. if (obj != null && displayObject != null && obj.displayObject != null)
  1002. displayObject.home = obj.displayObject.cachedTransform;
  1003. }
  1004. /// <summary>
  1005. /// If the object has lowlevel displayobject and the displayobject has a container parent?
  1006. /// </summary>
  1007. public bool inContainer
  1008. {
  1009. get
  1010. {
  1011. return displayObject != null && displayObject.parent != null;
  1012. }
  1013. }
  1014. /// <summary>
  1015. /// If the object is on stage.
  1016. /// </summary>
  1017. public bool onStage
  1018. {
  1019. get
  1020. {
  1021. return displayObject != null && displayObject.stage != null;
  1022. }
  1023. }
  1024. /// <summary>
  1025. /// Resource url of this object.
  1026. /// </summary>
  1027. public string resourceURL
  1028. {
  1029. get
  1030. {
  1031. if (packageItem != null)
  1032. return UIPackage.URL_PREFIX + packageItem.owner.id + packageItem.id;
  1033. else
  1034. return null;
  1035. }
  1036. }
  1037. /// <summary>
  1038. /// Gear to xy controller.
  1039. /// </summary>
  1040. public GearXY gearXY
  1041. {
  1042. get
  1043. {
  1044. return (GearXY)GetGear(1);
  1045. }
  1046. }
  1047. /// <summary>
  1048. /// Gear to size controller.
  1049. /// </summary>
  1050. public GearSize gearSize
  1051. {
  1052. get
  1053. {
  1054. return (GearSize)GetGear(2);
  1055. }
  1056. }
  1057. /// <summary>
  1058. /// Gear to look controller.
  1059. /// </summary>
  1060. public GearLook gearLook
  1061. {
  1062. get
  1063. {
  1064. return (GearLook)GetGear(3);
  1065. }
  1066. }
  1067. public GearBase GetGear(int index)
  1068. {
  1069. GearBase gear = _gears[index];
  1070. if (gear == null)
  1071. {
  1072. switch (index)
  1073. {
  1074. case 0:
  1075. gear = new GearDisplay(this);
  1076. break;
  1077. case 1:
  1078. gear = new GearXY(this);
  1079. break;
  1080. case 2:
  1081. gear = new GearSize(this);
  1082. break;
  1083. case 3:
  1084. gear = new GearLook(this);
  1085. break;
  1086. case 4:
  1087. gear = new GearColor(this);
  1088. break;
  1089. case 5:
  1090. gear = new GearAnimation(this);
  1091. break;
  1092. case 6:
  1093. gear = new GearText(this);
  1094. break;
  1095. case 7:
  1096. gear = new GearIcon(this);
  1097. break;
  1098. case 8:
  1099. gear = new GearDisplay2(this);
  1100. break;
  1101. case 9:
  1102. gear = new GearFontSize(this);
  1103. break;
  1104. default:
  1105. throw new System.Exception("FairyGUI: invalid gear index!");
  1106. }
  1107. _gears[index] = gear;
  1108. }
  1109. return gear;
  1110. }
  1111. protected void UpdateGear(int index)
  1112. {
  1113. if (underConstruct || _gearLocked)
  1114. return;
  1115. GearBase gear = _gears[index];
  1116. if (gear != null && gear.controller != null)
  1117. gear.UpdateState();
  1118. }
  1119. internal bool CheckGearController(int index, Controller c)
  1120. {
  1121. return _gears[index] != null && _gears[index].controller == c;
  1122. }
  1123. internal void UpdateGearFromRelations(int index, float dx, float dy)
  1124. {
  1125. if (_gears[index] != null)
  1126. _gears[index].UpdateFromRelations(dx, dy);
  1127. }
  1128. internal uint AddDisplayLock()
  1129. {
  1130. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1131. if (gearDisplay != null && gearDisplay.controller != null)
  1132. {
  1133. uint ret = gearDisplay.AddLock();
  1134. CheckGearDisplay();
  1135. return ret;
  1136. }
  1137. else
  1138. return 0;
  1139. }
  1140. internal void ReleaseDisplayLock(uint token)
  1141. {
  1142. GearDisplay gearDisplay = (GearDisplay)_gears[0];
  1143. if (gearDisplay != null && gearDisplay.controller != null)
  1144. {
  1145. gearDisplay.ReleaseLock(token);
  1146. CheckGearDisplay();
  1147. }
  1148. }
  1149. void CheckGearDisplay()
  1150. {
  1151. if (_handlingController)
  1152. return;
  1153. bool connected = _gears[0] == null || ((GearDisplay)_gears[0]).connected;
  1154. if (_gears[8] != null)
  1155. connected = ((GearDisplay2)_gears[8]).Evaluate(connected);
  1156. if (connected != _internalVisible)
  1157. {
  1158. _internalVisible = connected;
  1159. if (parent != null)
  1160. parent.ChildStateChanged(this);
  1161. if (_group != null && _group.excludeInvisibles)
  1162. _group.SetBoundsChangedFlag();
  1163. }
  1164. }
  1165. /// <summary>
  1166. /// Mark the fairy batching state is invalid.
  1167. /// </summary>
  1168. public void InvalidateBatchingState()
  1169. {
  1170. if (displayObject != null)
  1171. displayObject.InvalidateBatchingState();
  1172. else if ((this is GGroup) && parent != null)
  1173. parent.container.InvalidateBatchingState(true);
  1174. }
  1175. virtual public void HandleControllerChanged(Controller c)
  1176. {
  1177. _handlingController = true;
  1178. for (int i = 0; i < 10; i++)
  1179. {
  1180. GearBase gear = _gears[i];
  1181. if (gear != null && gear.controller == c)
  1182. gear.Apply();
  1183. }
  1184. _handlingController = false;
  1185. CheckGearDisplay();
  1186. }
  1187. /// <summary>
  1188. ///
  1189. /// </summary>
  1190. /// <param name="target"></param>
  1191. /// <param name="relationType"></param>
  1192. public void AddRelation(GObject target, RelationType relationType)
  1193. {
  1194. AddRelation(target, relationType, false);
  1195. }
  1196. /// <summary>
  1197. ///
  1198. /// </summary>
  1199. /// <param name="target"></param>
  1200. /// <param name="relationType"></param>
  1201. /// <param name="usePercent"></param>
  1202. public void AddRelation(GObject target, RelationType relationType, bool usePercent)
  1203. {
  1204. relations.Add(target, relationType, usePercent);
  1205. }
  1206. /// <summary>
  1207. ///
  1208. /// </summary>
  1209. /// <param name="target"></param>
  1210. /// <param name="relationType"></param>
  1211. public void RemoveRelation(GObject target, RelationType relationType)
  1212. {
  1213. relations.Remove(target, relationType);
  1214. }
  1215. /// <summary>
  1216. ///
  1217. /// </summary>
  1218. public void RemoveFromParent()
  1219. {
  1220. if (parent != null)
  1221. parent.RemoveChild(this);
  1222. }
  1223. /// <summary>
  1224. /// Group belonging to.
  1225. /// </summary>
  1226. public GGroup group
  1227. {
  1228. get { return _group; }
  1229. set
  1230. {
  1231. if (_group != value)
  1232. {
  1233. if (_group != null)
  1234. _group.SetBoundsChangedFlag();
  1235. _group = value;
  1236. if (_group != null)
  1237. _group.SetBoundsChangedFlag();
  1238. HandleVisibleChanged();
  1239. if (parent != null)
  1240. parent.ChildStateChanged(this);
  1241. }
  1242. }
  1243. }
  1244. /// <summary>
  1245. ///
  1246. /// </summary>
  1247. public GRoot root
  1248. {
  1249. get
  1250. {
  1251. GObject p = this;
  1252. while (p.parent != null)
  1253. p = p.parent;
  1254. if (p is GRoot)
  1255. return (GRoot)p;
  1256. if (p.displayObject != null && p.displayObject.parent != null)
  1257. {
  1258. DisplayObject d = p.displayObject.parent.GetChild("GRoot");
  1259. if (d != null && (d.gOwner is GRoot))
  1260. return (GRoot)d.gOwner;
  1261. }
  1262. return GRoot.inst;
  1263. }
  1264. }
  1265. /// <summary>
  1266. ///
  1267. /// </summary>
  1268. virtual public string text
  1269. {
  1270. get { return null; }
  1271. set { /*override in child*/}
  1272. }
  1273. /// <summary>
  1274. ///
  1275. /// </summary>
  1276. virtual public string icon
  1277. {
  1278. get { return null; }
  1279. set { /*override in child*/}
  1280. }
  1281. /// <summary>
  1282. ///
  1283. /// </summary>
  1284. public bool draggable
  1285. {
  1286. get { return _draggable; }
  1287. set
  1288. {
  1289. if (_draggable != value)
  1290. {
  1291. _draggable = value;
  1292. InitDrag();
  1293. }
  1294. }
  1295. }
  1296. /// <summary>
  1297. ///
  1298. /// </summary>
  1299. public void StartDrag()
  1300. {
  1301. StartDrag(-1);
  1302. }
  1303. /// <summary>
  1304. ///
  1305. /// </summary>
  1306. /// <param name="touchId"></param>
  1307. public void StartDrag(int touchId)
  1308. {
  1309. if (displayObject.stage == null)
  1310. return;
  1311. DragBegin(touchId);
  1312. }
  1313. /// <summary>
  1314. ///
  1315. /// </summary>
  1316. public void StopDrag()
  1317. {
  1318. DragEnd();
  1319. }
  1320. /// <summary>
  1321. ///
  1322. /// </summary>
  1323. public bool dragging
  1324. {
  1325. get { return draggingObject == this; }
  1326. }
  1327. /// <summary>
  1328. /// Transforms a point from the local coordinate system to global (Stage) coordinates.
  1329. /// </summary>
  1330. /// <param name="pt"></param>
  1331. /// <returns></returns>
  1332. public Vector2 LocalToGlobal(Vector2 pt)
  1333. {
  1334. if (_pivotAsAnchor)
  1335. {
  1336. pt.x += _width * _pivotX;
  1337. pt.y += _height * _pivotY;
  1338. }
  1339. return displayObject.LocalToGlobal(pt);
  1340. }
  1341. /// <summary>
  1342. /// Transforms a point from global (Stage) coordinates to the local coordinate system.
  1343. /// </summary>
  1344. /// <param name="pt"></param>
  1345. /// <returns></returns>
  1346. public Vector2 GlobalToLocal(Vector2 pt)
  1347. {
  1348. pt = displayObject.GlobalToLocal(pt);
  1349. if (_pivotAsAnchor)
  1350. {
  1351. pt.x -= _width * _pivotX;
  1352. pt.y -= _height * _pivotY;
  1353. }
  1354. return pt;
  1355. }
  1356. /// <summary>
  1357. ///
  1358. /// </summary>
  1359. /// <param name="rect"></param>
  1360. /// <returns></returns>
  1361. public Rect LocalToGlobal(Rect rect)
  1362. {
  1363. Rect ret = new Rect();
  1364. Vector2 v = this.LocalToGlobal(new Vector2(rect.xMin, rect.yMin));
  1365. ret.xMin = v.x;
  1366. ret.yMin = v.y;
  1367. v = this.LocalToGlobal(new Vector2(rect.xMax, rect.yMax));
  1368. ret.xMax = v.x;
  1369. ret.yMax = v.y;
  1370. return ret;
  1371. }
  1372. /// <summary>
  1373. ///
  1374. /// </summary>
  1375. /// <param name="rect"></param>
  1376. /// <returns></returns>
  1377. public Rect GlobalToLocal(Rect rect)
  1378. {
  1379. Rect ret = new Rect();
  1380. Vector2 v = this.GlobalToLocal(new Vector2(rect.xMin, rect.yMin));
  1381. ret.xMin = v.x;
  1382. ret.yMin = v.y;
  1383. v = this.GlobalToLocal(new Vector2(rect.xMax, rect.yMax));
  1384. ret.xMax = v.x;
  1385. ret.yMax = v.y;
  1386. return ret;
  1387. }
  1388. /// <summary>
  1389. /// Transforms a point from the local coordinate system to GRoot coordinates.
  1390. /// </summary>
  1391. /// <param name="pt"></param>
  1392. /// <param name="r"></param>
  1393. /// <returns></returns>
  1394. public Vector2 LocalToRoot(Vector2 pt, GRoot r)
  1395. {
  1396. pt = LocalToGlobal(pt);
  1397. if (r == null || r == GRoot.inst)
  1398. {
  1399. //fast
  1400. pt.x /= UIContentScaler.scaleFactor;
  1401. pt.y /= UIContentScaler.scaleFactor;
  1402. return pt;
  1403. }
  1404. else
  1405. return r.GlobalToLocal(pt);
  1406. }
  1407. /// <summary>
  1408. /// Transforms a point from the GRoot coordinate to local coordinates system.
  1409. /// </summary>
  1410. /// <param name="pt"></param>
  1411. /// <param name="r"></param>
  1412. /// <returns></returns>
  1413. public Vector2 RootToLocal(Vector2 pt, GRoot r)
  1414. {
  1415. if (r == null || r == GRoot.inst)
  1416. {
  1417. //fast
  1418. pt.x *= UIContentScaler.scaleFactor;
  1419. pt.y *= UIContentScaler.scaleFactor;
  1420. }
  1421. else
  1422. pt = r.LocalToGlobal(pt);
  1423. return GlobalToLocal(pt);
  1424. }
  1425. /// <summary>
  1426. ///
  1427. /// </summary>
  1428. /// <param name="pt"></param>
  1429. /// <returns></returns>
  1430. public Vector2 WorldToLocal(Vector3 pt)
  1431. {
  1432. return WorldToLocal(pt, HitTestContext.cachedMainCamera);
  1433. }
  1434. /// <summary>
  1435. ///
  1436. /// </summary>
  1437. /// <param name="pt"></param>
  1438. /// <param name="camera"></param>
  1439. /// <returns></returns>
  1440. public Vector2 WorldToLocal(Vector3 pt, Camera camera)
  1441. {
  1442. Vector3 v = camera.WorldToScreenPoint(pt);
  1443. v.y = Screen.height - v.y;
  1444. v.z = 0;
  1445. return GlobalToLocal(v);
  1446. }
  1447. /// <summary>
  1448. ///
  1449. /// </summary>
  1450. /// <param name="point"></param>
  1451. /// <param name="targetSpace"></param>
  1452. /// <returns></returns>
  1453. public Vector2 TransformPoint(Vector2 pt, GObject targetSpace)
  1454. {
  1455. if (_pivotAsAnchor)
  1456. {
  1457. pt.x += _width * _pivotX;
  1458. pt.y += _height * _pivotY;
  1459. }
  1460. return this.displayObject.TransformPoint(pt, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1461. }
  1462. /// <summary>
  1463. ///
  1464. /// </summary>
  1465. /// <param name="rect"></param>
  1466. /// <param name="targetSpace"></param>
  1467. /// <returns></returns>
  1468. public Rect TransformRect(Rect rect, GObject targetSpace)
  1469. {
  1470. if (_pivotAsAnchor)
  1471. {
  1472. rect.x += _width * _pivotX;
  1473. rect.y += _height * _pivotY;
  1474. }
  1475. return this.displayObject.TransformRect(rect, targetSpace != null ? targetSpace.displayObject : Stage.inst);
  1476. }
  1477. /// <summary>
  1478. ///
  1479. /// </summary>
  1480. public bool isDisposed
  1481. {
  1482. get { return _disposed; }
  1483. }
  1484. /// <summary>
  1485. ///
  1486. /// </summary>
  1487. virtual public void Dispose()
  1488. {
  1489. if (_disposed)
  1490. return;
  1491. _disposed = true;
  1492. RemoveFromParent();
  1493. RemoveEventListeners();
  1494. relations.Dispose();
  1495. relations = null;
  1496. for (int i = 0; i < 10; i++)
  1497. {
  1498. GearBase gear = _gears[i];
  1499. if (gear != null)
  1500. gear.Dispose();
  1501. }
  1502. if (displayObject != null)
  1503. {
  1504. displayObject.gOwner = null;
  1505. displayObject.Dispose();
  1506. }
  1507. data = null;
  1508. }
  1509. /// <summary>
  1510. ///
  1511. /// </summary>
  1512. public GImage asImage
  1513. {
  1514. get { return this as GImage; }
  1515. }
  1516. /// <summary>
  1517. ///
  1518. /// </summary>
  1519. public GComponent asCom
  1520. {
  1521. get { return this as GComponent; }
  1522. }
  1523. /// <summary>
  1524. ///
  1525. /// </summary>
  1526. public GButton asButton
  1527. {
  1528. get { return this as GButton; }
  1529. }
  1530. /// <summary>
  1531. ///
  1532. /// </summary>
  1533. public GLabel asLabel
  1534. {
  1535. get { return this as GLabel; }
  1536. }
  1537. /// <summary>
  1538. ///
  1539. /// </summary>
  1540. public GProgressBar asProgress
  1541. {
  1542. get { return this as GProgressBar; }
  1543. }
  1544. /// <summary>
  1545. ///
  1546. /// </summary>
  1547. public GSlider asSlider
  1548. {
  1549. get { return this as GSlider; }
  1550. }
  1551. /// <summary>
  1552. ///
  1553. /// </summary>
  1554. public GComboBox asComboBox
  1555. {
  1556. get { return this as GComboBox; }
  1557. }
  1558. /// <summary>
  1559. ///
  1560. /// </summary>
  1561. public GTextField asTextField
  1562. {
  1563. get { return this as GTextField; }
  1564. }
  1565. /// <summary>
  1566. ///
  1567. /// </summary>
  1568. public GRichTextField asRichTextField
  1569. {
  1570. get { return this as GRichTextField; }
  1571. }
  1572. /// <summary>
  1573. ///
  1574. /// </summary>
  1575. public GTextInput asTextInput
  1576. {
  1577. get { return this as GTextInput; }
  1578. }
  1579. /// <summary>
  1580. ///
  1581. /// </summary>
  1582. public GLoader asLoader
  1583. {
  1584. get { return this as GLoader; }
  1585. }
  1586. /// <summary>
  1587. ///
  1588. /// </summary>
  1589. public GLoader3D asLoader3D
  1590. {
  1591. get { return this as GLoader3D; }
  1592. }
  1593. /// <summary>
  1594. ///
  1595. /// </summary>
  1596. public GList asList
  1597. {
  1598. get { return this as GList; }
  1599. }
  1600. /// <summary>
  1601. ///
  1602. /// </summary>
  1603. public GGraph asGraph
  1604. {
  1605. get { return this as GGraph; }
  1606. }
  1607. /// <summary>
  1608. ///
  1609. /// </summary>
  1610. public GGroup asGroup
  1611. {
  1612. get { return this as GGroup; }
  1613. }
  1614. /// <summary>
  1615. ///
  1616. /// </summary>
  1617. public GMovieClip asMovieClip
  1618. {
  1619. get { return this as GMovieClip; }
  1620. }
  1621. /// <summary>
  1622. ///
  1623. /// </summary>
  1624. public GTree asTree
  1625. {
  1626. get { return this as GTree; }
  1627. }
  1628. /// <summary>
  1629. ///
  1630. /// </summary>
  1631. public GTreeNode treeNode
  1632. {
  1633. get { return _treeNode; }
  1634. }
  1635. virtual protected void CreateDisplayObject()
  1636. {
  1637. }
  1638. internal void InternalSetParent(GComponent value)
  1639. {
  1640. parent = value;
  1641. }
  1642. virtual protected void HandlePositionChanged()
  1643. {
  1644. if (displayObject != null)
  1645. {
  1646. float xv = _x;
  1647. float yv = _y;
  1648. if (!_pivotAsAnchor)
  1649. {
  1650. xv += _width * _pivotX;
  1651. yv += _height * _pivotY;
  1652. }
  1653. displayObject.location = new Vector3(xv, yv, _z);
  1654. }
  1655. }
  1656. virtual protected void HandleSizeChanged()
  1657. {
  1658. if (displayObject != null)
  1659. displayObject.SetSize(_width, _height);
  1660. }
  1661. virtual protected void HandleScaleChanged()
  1662. {
  1663. if (displayObject != null)
  1664. displayObject.SetScale(_scaleX, _scaleY);
  1665. }
  1666. virtual protected void HandleGrayedChanged()
  1667. {
  1668. if (displayObject != null)
  1669. displayObject.grayed = _grayed;
  1670. }
  1671. virtual protected void HandleAlphaChanged()
  1672. {
  1673. if (displayObject != null)
  1674. displayObject.alpha = _alpha;
  1675. }
  1676. virtual internal protected void HandleVisibleChanged()
  1677. {
  1678. if (displayObject != null)
  1679. displayObject.visible = internalVisible2;
  1680. }
  1681. virtual public void ConstructFromResource()
  1682. {
  1683. }
  1684. virtual public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  1685. {
  1686. buffer.Seek(beginPos, 0);
  1687. buffer.Skip(5);
  1688. id = buffer.ReadS();
  1689. name = buffer.ReadS();
  1690. float f1 = buffer.ReadInt();
  1691. float f2 = buffer.ReadInt();
  1692. SetXY(f1, f2);
  1693. if (buffer.ReadBool())
  1694. {
  1695. initWidth = buffer.ReadInt();
  1696. initHeight = buffer.ReadInt();
  1697. SetSize(initWidth, initHeight, true);
  1698. }
  1699. if (buffer.ReadBool())
  1700. {
  1701. minWidth = buffer.ReadInt();
  1702. maxWidth = buffer.ReadInt();
  1703. minHeight = buffer.ReadInt();
  1704. maxHeight = buffer.ReadInt();
  1705. }
  1706. if (buffer.ReadBool())
  1707. {
  1708. f1 = buffer.ReadFloat();
  1709. f2 = buffer.ReadFloat();
  1710. SetScale(f1, f2);
  1711. }
  1712. if (buffer.ReadBool())
  1713. {
  1714. f1 = buffer.ReadFloat();
  1715. f2 = buffer.ReadFloat();
  1716. this.skew = new Vector2(f1, f2);
  1717. }
  1718. if (buffer.ReadBool())
  1719. {
  1720. f1 = buffer.ReadFloat();
  1721. f2 = buffer.ReadFloat();
  1722. SetPivot(f1, f2, buffer.ReadBool());
  1723. }
  1724. f1 = buffer.ReadFloat();
  1725. if (f1 != 1)
  1726. this.alpha = f1;
  1727. f1 = buffer.ReadFloat();
  1728. if (f1 != 0)
  1729. this.rotation = f1;
  1730. if (!buffer.ReadBool())
  1731. this.visible = false;
  1732. if (!buffer.ReadBool())
  1733. this.touchable = false;
  1734. if (buffer.ReadBool())
  1735. this.grayed = true;
  1736. this.blendMode = (BlendMode)buffer.ReadByte();
  1737. int filter = buffer.ReadByte();
  1738. if (filter == 1)
  1739. {
  1740. ColorFilter cf = new ColorFilter();
  1741. this.filter = cf;
  1742. cf.AdjustBrightness(buffer.ReadFloat());
  1743. cf.AdjustContrast(buffer.ReadFloat());
  1744. cf.AdjustSaturation(buffer.ReadFloat());
  1745. cf.AdjustHue(buffer.ReadFloat());
  1746. }
  1747. string str = buffer.ReadS();
  1748. if (str != null)
  1749. this.data = str;
  1750. }
  1751. virtual public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)
  1752. {
  1753. buffer.Seek(beginPos, 1);
  1754. string str = buffer.ReadS();
  1755. if (str != null)
  1756. this.tooltips = str;
  1757. int groupId = buffer.ReadShort();
  1758. if (groupId >= 0)
  1759. group = parent.GetChildAt(groupId) as GGroup;
  1760. buffer.Seek(beginPos, 2);
  1761. int cnt = buffer.ReadShort();
  1762. for (int i = 0; i < cnt; i++)
  1763. {
  1764. int nextPos = buffer.ReadUshort();
  1765. nextPos += buffer.position;
  1766. GearBase gear = GetGear(buffer.ReadByte());
  1767. gear.Setup(buffer);
  1768. buffer.position = nextPos;
  1769. }
  1770. }
  1771. #region Drag support
  1772. Vector2 _dragTouchStartPos;
  1773. bool _dragTesting;
  1774. static Vector2 sGlobalDragStart = new Vector2();
  1775. static Rect sGlobalRect = new Rect();
  1776. static bool sUpdateInDragging = false;
  1777. private void InitDrag()
  1778. {
  1779. if (_draggable)
  1780. {
  1781. onTouchBegin.Add(__touchBegin);
  1782. onTouchMove.Add(__touchMove);
  1783. onTouchEnd.Add(__touchEnd);
  1784. }
  1785. else
  1786. {
  1787. onTouchBegin.Remove(__touchBegin);
  1788. onTouchMove.Remove(__touchMove);
  1789. onTouchEnd.Remove(__touchEnd);
  1790. }
  1791. }
  1792. private void DragBegin(int touchId)
  1793. {
  1794. if (DispatchEvent("onDragStart", touchId))
  1795. return;
  1796. if (draggingObject != null)
  1797. {
  1798. GObject tmp = draggingObject;
  1799. draggingObject.StopDrag();
  1800. draggingObject = null;
  1801. tmp.DispatchEvent("onDragEnd", null);
  1802. }
  1803. onTouchMove.Add(__touchMove);
  1804. onTouchEnd.Add(__touchEnd);
  1805. sGlobalDragStart = Stage.inst.GetTouchPosition(touchId);
  1806. sGlobalRect = this.LocalToGlobal(new Rect(0, 0, this.width, this.height));
  1807. _dragTesting = false;
  1808. draggingObject = this;
  1809. Stage.inst.AddTouchMonitor(touchId, this);
  1810. }
  1811. private void DragEnd()
  1812. {
  1813. if (draggingObject == this)
  1814. {
  1815. _dragTesting = false;
  1816. draggingObject = null;
  1817. }
  1818. }
  1819. private void __touchBegin(EventContext context)
  1820. {
  1821. if ((Stage.inst.focus is InputTextField) && ((InputTextField)Stage.inst.focus).editable)
  1822. {
  1823. _dragTesting = false;
  1824. return;
  1825. }
  1826. InputEvent evt = context.inputEvent;
  1827. _dragTouchStartPos = evt.position;
  1828. _dragTesting = true;
  1829. context.CaptureTouch();
  1830. }
  1831. private void __touchMove(EventContext context)
  1832. {
  1833. InputEvent evt = context.inputEvent;
  1834. if (_dragTesting && draggingObject != this)
  1835. {
  1836. int sensitivity;
  1837. if (Stage.touchScreen)
  1838. sensitivity = UIConfig.touchDragSensitivity;
  1839. else
  1840. sensitivity = UIConfig.clickDragSensitivity;
  1841. if (Mathf.Abs(_dragTouchStartPos.x - evt.x) < sensitivity
  1842. && Mathf.Abs(_dragTouchStartPos.y - evt.y) < sensitivity)
  1843. return;
  1844. _dragTesting = false;
  1845. DragBegin(evt.touchId);
  1846. }
  1847. if (draggingObject == this)
  1848. {
  1849. float xx = evt.x - sGlobalDragStart.x + sGlobalRect.x;
  1850. float yy = evt.y - sGlobalDragStart.y + sGlobalRect.y;
  1851. if (dragBounds != null)
  1852. {
  1853. Rect rect = GRoot.inst.LocalToGlobal((Rect)dragBounds);
  1854. if (xx < rect.x)
  1855. xx = rect.x;
  1856. else if (xx + sGlobalRect.width > rect.xMax)
  1857. {
  1858. xx = rect.xMax - sGlobalRect.width;
  1859. if (xx < rect.x)
  1860. xx = rect.x;
  1861. }
  1862. if (yy < rect.y)
  1863. yy = rect.y;
  1864. else if (yy + sGlobalRect.height > rect.yMax)
  1865. {
  1866. yy = rect.yMax - sGlobalRect.height;
  1867. if (yy < rect.y)
  1868. yy = rect.y;
  1869. }
  1870. }
  1871. Vector2 pt = this.parent.GlobalToLocal(new Vector2(xx, yy));
  1872. if (float.IsNaN(pt.x))
  1873. return;
  1874. sUpdateInDragging = true;
  1875. this.SetXY(Mathf.RoundToInt(pt.x), Mathf.RoundToInt(pt.y));
  1876. sUpdateInDragging = false;
  1877. DispatchEvent("onDragMove", null);
  1878. }
  1879. }
  1880. private void __touchEnd(EventContext context)
  1881. {
  1882. if (draggingObject == this)
  1883. {
  1884. draggingObject = null;
  1885. DispatchEvent("onDragEnd", null);
  1886. }
  1887. }
  1888. #endregion
  1889. #region Tween Helpers
  1890. public GTweener TweenMove(Vector2 endValue, float duration)
  1891. {
  1892. return GTween.To(this.xy, endValue, duration).SetTarget(this, TweenPropType.XY);
  1893. }
  1894. public GTweener TweenMoveX(float endValue, float duration)
  1895. {
  1896. return GTween.To(_x, endValue, duration).SetTarget(this, TweenPropType.X);
  1897. }
  1898. public GTweener TweenMoveY(float endValue, float duration)
  1899. {
  1900. return GTween.To(_y, endValue, duration).SetTarget(this, TweenPropType.Y);
  1901. }
  1902. public GTweener TweenScale(Vector2 endValue, float duration)
  1903. {
  1904. return GTween.To(this.scale, endValue, duration).SetTarget(this, TweenPropType.Scale);
  1905. }
  1906. public GTweener TweenScaleX(float endValue, float duration)
  1907. {
  1908. return GTween.To(_scaleX, endValue, duration).SetTarget(this, TweenPropType.ScaleX);
  1909. }
  1910. public GTweener TweenScaleY(float endValue, float duration)
  1911. {
  1912. return GTween.To(_scaleY, endValue, duration).SetTarget(this, TweenPropType.ScaleY);
  1913. }
  1914. public GTweener TweenResize(Vector2 endValue, float duration)
  1915. {
  1916. return GTween.To(this.size, endValue, duration).SetTarget(this, TweenPropType.Size);
  1917. }
  1918. public GTweener TweenFade(float endValue, float duration)
  1919. {
  1920. return GTween.To(_alpha, endValue, duration).SetTarget(this, TweenPropType.Alpha);
  1921. }
  1922. public GTweener TweenRotate(float endValue, float duration)
  1923. {
  1924. return GTween.To(_rotation, endValue, duration).SetTarget(this, TweenPropType.Rotation);
  1925. }
  1926. #endregion
  1927. }
  1928. }