GObject.cs 61 KB

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