CPJObjects.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonLang;
  5. using CommonUI.Display;
  6. namespace CommonUI.Cell.Game
  7. {
  8. public class CCD
  9. {
  10. public enum CDType : byte
  11. {
  12. CD_TYPE_RECT = 1,
  13. CD_TYPE_LINE = 2,
  14. CD_TYPE_POINT = 3,
  15. }
  16. public CDType Type = CDType.CD_TYPE_RECT;
  17. public int Mask;
  18. /// <summary>
  19. /// Left
  20. /// </summary>
  21. public float X1;
  22. /// <summary>
  23. /// Top
  24. /// </summary>
  25. public float Y1;
  26. /// <summary>
  27. /// Right
  28. /// </summary>
  29. public float X2;
  30. /// <summary>
  31. /// Bottom
  32. /// </summary>
  33. public float Y2;
  34. public CCD() { }
  35. public CCD(CCD theobj)
  36. {
  37. this.Type = theobj.Type;
  38. this.Mask = theobj.Mask;
  39. this.X1 = theobj.X1;
  40. this.Y1 = theobj.Y1;
  41. this.X2 = theobj.X2;
  42. this.Y2 = theobj.Y2;
  43. }
  44. public float Width
  45. {
  46. get
  47. {
  48. return X2 - X1;
  49. }
  50. }
  51. public float Height
  52. {
  53. get
  54. {
  55. return Y2 - Y1 + 1;
  56. }
  57. }
  58. /// <summary>
  59. /// 得到外包矩形中心点和渲染中心点的偏移
  60. /// </summary>
  61. public float MedianY
  62. {
  63. get
  64. {
  65. return -(Height / 2 - (Y1 + Height));
  66. }
  67. }
  68. /// <summary>
  69. /// 得到外包矩形中心点和渲染中心点的偏移
  70. /// </summary>
  71. public float MedianX
  72. {
  73. get
  74. {
  75. return -(Width / 2 - (X1 + Width));
  76. }
  77. }
  78. static public CCD CreateCDRect(int mask, float x, float y, float w, float h)
  79. {
  80. CCD ret = new CCD();
  81. ret.Type = CDType.CD_TYPE_RECT;
  82. ret.Mask = mask;
  83. ret.X1 = x;
  84. ret.Y1 = y;
  85. ret.X2 = (x + w);
  86. ret.Y2 = (y + h);
  87. return ret;
  88. }
  89. static public CCD CreateCDRect_2Point(int mask, float sx, float sy, float dx, float dy)
  90. {
  91. CCD ret = new CCD();
  92. ret.Type = CDType.CD_TYPE_RECT;
  93. ret.Mask = mask;
  94. ret.X1 = Math.Min(sx, dx);
  95. ret.Y1 = Math.Min(sy, dy);
  96. ret.X2 = Math.Max(sx, dx);
  97. ret.Y2 = Math.Max(sy, dy);
  98. return ret;
  99. }
  100. static public CCD CreateCDLine(int mask, float px, float py, float qx, float qy)
  101. {
  102. CCD ret = new CCD();
  103. ret.Type = CDType.CD_TYPE_LINE;
  104. ret.Mask = mask;
  105. ret.X1 = px;
  106. ret.Y1 = py;
  107. ret.X2 = qx;
  108. ret.Y2 = qy;
  109. return ret;
  110. }
  111. }
  112. public class CGroup
  113. {
  114. internal short[][] Frames;
  115. internal int SubIndex=0;
  116. internal int SubCount=0;
  117. internal float w_left = 0;
  118. internal float w_top = 0;
  119. internal float w_bottom = 1;
  120. internal float w_right = 1;
  121. internal float w_width = 0;
  122. internal float w_height = 0;
  123. protected void fixArea(float left, float top, float right, float botton)
  124. {
  125. if (left < w_left)
  126. w_left = (short)left;
  127. if (top < w_top)
  128. w_top = (short)top;
  129. if (right > w_right)
  130. w_right = (short)right;
  131. if (botton > w_bottom)
  132. w_bottom = (short)botton;
  133. w_width = (short)(w_right - w_left);
  134. w_height = (short)(w_bottom - w_top);
  135. }
  136. static public bool touchArea(
  137. CGroup c1, int x1, int y1,
  138. CGroup c2, int x2, int y2)
  139. {
  140. if (CMath.intersectRect(
  141. x1 + c1.w_left,
  142. y1 + c1.w_top,
  143. x1 + c1.w_right,
  144. y1 + c1.w_bottom,
  145. x2 + c2.w_left,
  146. y2 + c2.w_top,
  147. x2 + c2.w_right,
  148. y2 + c2.w_bottom))
  149. {
  150. return true;
  151. }
  152. return false;
  153. }
  154. public CCD getAllBounds()
  155. {
  156. CCD outcd = new CCD();
  157. outcd.X1 = w_left;
  158. outcd.X2 = w_right;
  159. outcd.Y1 = w_top;
  160. outcd.Y2 = w_bottom;
  161. return outcd;
  162. }
  163. public void setFrames(short[][] frames)
  164. {
  165. Frames = frames;
  166. }
  167. public short[][] getFrames()
  168. {
  169. return Frames;
  170. }
  171. public void setComboFrame(short[] frame, int index)
  172. {
  173. Frames[index] = frame;
  174. }
  175. public int getCount()
  176. {
  177. return Frames.Length;
  178. }
  179. public int getComboFrameCount(int index)
  180. {
  181. return Frames[index].Length;
  182. }
  183. }
  184. public class CCollides : CGroup
  185. {
  186. internal CCD[] cds;
  187. public CCollides(int cdCount)
  188. {
  189. SubCount = cdCount;
  190. cds = new CCD[cdCount];
  191. }
  192. private void setCD(int i, CCD cd)
  193. {
  194. if (i >= SubCount)
  195. {
  196. return;
  197. }
  198. cds[i] = cd;
  199. fixArea(cd.X1, cd.Y1, cd.X2, cd.Y2);
  200. }
  201. public void setCDRect(int i, int mask, float x, float y, float w, float h)
  202. {
  203. setCD(i, CCD.CreateCDRect(mask, x, y, w, h));
  204. }
  205. public void setCDLine(int i, int mask, float px, float py, float qx, float qy)
  206. {
  207. setCD(i, CCD.CreateCDLine(mask, px, py, qx, qy));
  208. }
  209. public CCD getCD(int index)
  210. {
  211. if (index < SubCount)
  212. {
  213. return cds[index];
  214. }
  215. return null;
  216. }
  217. public CCD getFrameCD(int frame, int sub)
  218. {
  219. return getCD(Frames[frame][sub]);
  220. }
  221. }
  222. public class CAnimates : CGroup
  223. {
  224. internal CPJAtlas tiles;
  225. internal int[] STileID;
  226. internal Trans[] SFlip;
  227. internal float[] SX;
  228. internal float[] SY;
  229. internal float[] SW;
  230. internal float[] SH;
  231. internal float[] SAlpha;
  232. internal float[] SRotate;
  233. internal float[] SScaleX;
  234. internal float[] SScaleY;
  235. internal bool ComplexMode;
  236. public CAnimates(int partCount, CPJAtlas tils)
  237. {
  238. tiles = tils;
  239. SubCount = partCount;
  240. STileID = new int[partCount];
  241. SFlip = new Trans[partCount];
  242. SW = new float[partCount];
  243. SH = new float[partCount];
  244. SX = new float[partCount];
  245. SY = new float[partCount];
  246. SAlpha = new float[partCount];
  247. SRotate = new float[partCount];
  248. SScaleX = new float[partCount];
  249. SScaleY = new float[partCount];
  250. }
  251. public void setPart(int SubIndex, float px, float py, int tileid, Trans trans, float alpha = 1f, float rotate = 0, float scaleX = 1f, float scaleY = 1f)
  252. {
  253. if (SubIndex >= SubCount)
  254. {
  255. return;
  256. }
  257. STileID[SubIndex] = tileid;
  258. SW[SubIndex] = tiles.getWidth(tileid);
  259. SH[SubIndex] = tiles.getHeight(tileid);
  260. SX[SubIndex] = px;
  261. SY[SubIndex] = py;
  262. SFlip[SubIndex] = trans;
  263. SAlpha[SubIndex] = alpha;
  264. SRotate[SubIndex] = rotate;
  265. SScaleX[SubIndex] = scaleX;
  266. SScaleY[SubIndex] = scaleY;
  267. switch (trans)
  268. {
  269. case Trans.TRANS_NONE:
  270. case Trans.TRANS_ROT180:
  271. case Trans.TRANS_MIRROR:
  272. case Trans.TRANS_MIRROR_ROT180:
  273. SW[SubIndex] = tiles.getWidth(tileid);
  274. SH[SubIndex] = tiles.getHeight(tileid);
  275. break;
  276. case Trans.TRANS_ROT90:
  277. case Trans.TRANS_ROT270:
  278. case Trans.TRANS_MIRROR_ROT90:
  279. case Trans.TRANS_MIRROR_ROT270:
  280. SW[SubIndex] = tiles.getHeight(tileid);
  281. SH[SubIndex] = tiles.getWidth(tileid);
  282. break;
  283. }
  284. fixArea(SX[SubIndex], SY[SubIndex],
  285. SX[SubIndex] + SW[SubIndex],
  286. SY[SubIndex] + SH[SubIndex]);
  287. if (rotate != 0 || scaleX != 1f || scaleY != 1f)
  288. {
  289. ComplexMode = true;
  290. }
  291. }
  292. public CPJAtlas getTiles()
  293. {
  294. return tiles;
  295. }
  296. public int getFrameTileID(int frame, int sub)
  297. {
  298. return STileID[Frames[frame][sub]];
  299. }
  300. public float getFrameX(int frame, int sub)
  301. {
  302. return SX[Frames[frame][sub]];
  303. }
  304. public float getFrameY(int frame, int sub)
  305. {
  306. return SY[Frames[frame][sub]];
  307. }
  308. public float getFrameW(int frame, int sub)
  309. {
  310. return SW[Frames[frame][sub]];
  311. }
  312. public float getFrameH(int frame, int sub)
  313. {
  314. return SH[Frames[frame][sub]];
  315. }
  316. public Trans getFrameTransform(int frame, int sub)
  317. {
  318. return SFlip[Frames[frame][sub]];
  319. }
  320. public float getFrameAlpha(int frame, int sub)
  321. {
  322. return SAlpha[Frames[frame][sub]];
  323. }
  324. public CCD getFrameBounds(int frame)
  325. {
  326. float left = float.MaxValue;
  327. float right = float.MinValue;
  328. float top = float.MaxValue;
  329. float bottom = float.MinValue;
  330. for (int i = SubCount; i >= 0; --i)
  331. {
  332. left = Math.Min(getFrameX(frame, i), left);
  333. right = Math.Max(getFrameX(frame, i) + getFrameW(frame, i), right);
  334. top = Math.Min(getFrameY(frame, i), top);
  335. bottom = Math.Max(getFrameY(frame, i) + getFrameH(frame, i), bottom);
  336. }
  337. CCD cd = CCD.CreateCDRect_2Point(0, left, top, right - left, bottom - top);
  338. return cd;
  339. }
  340. public void beginImage(Graphics g)
  341. {
  342. tiles.begin(g);
  343. }
  344. public void addVertex(VertexBuffer vertex, int index, float dx, float dy)
  345. {
  346. if (!ComplexMode)
  347. {
  348. for (int i = Frames[index].Length - 1; i >= 0; --i)
  349. {
  350. int idx = Frames[index][i];
  351. uint rgba = Color.COLOR_WHITE;
  352. if (SAlpha[idx] != 1)
  353. {
  354. rgba = Color.toRGBA(0xFFFFFF, Math.Min((int)(SAlpha[idx] * 255), 255));
  355. }
  356. tiles.addVertex(vertex,
  357. STileID[idx],
  358. SX[idx] + dx,
  359. SY[idx] + dy,
  360. SFlip[idx],
  361. rgba);
  362. }
  363. }
  364. else
  365. {
  366. vertex.translate(dx, dy);
  367. for (int i = Frames[index].Length - 1; i >= 0; --i)
  368. {
  369. int idx = Frames[index][i];
  370. float tx = SW[idx] / 2.0f;
  371. float ty = SH[idx] / 2.0f;
  372. uint rgba = Color.COLOR_WHITE;
  373. if (SAlpha[idx] != 1)
  374. {
  375. rgba = Color.toRGBA(0xFFFFFF, Math.Min((int)(SAlpha[idx] * 255), 255));
  376. }
  377. vertex.pushTransform();
  378. vertex.translate(SX[idx] + tx, SY[idx] + ty);
  379. vertex.rotate(SRotate[idx]);
  380. vertex.scale(SScaleX[idx], SScaleY[idx]);
  381. tiles.addVertex(vertex, STileID[idx], -tx, -ty, SFlip[idx], rgba);
  382. vertex.popTransform();
  383. }
  384. vertex.translate(-dx, -dy);
  385. }
  386. }
  387. public void render(Graphics g, int index, float dx = 0, float dy = 0)
  388. {
  389. if (!ComplexMode)
  390. {
  391. for (int i = Frames[index].Length - 1; i >= 0; --i)
  392. {
  393. int idx = Frames[index][i];
  394. g.addAlpha(SAlpha[idx]);
  395. tiles.render(g,
  396. STileID[idx],
  397. SX[idx] + dx,
  398. SY[idx] + dy,
  399. SFlip[idx]);
  400. }
  401. }
  402. else
  403. {
  404. g.translate(dx, dy);
  405. for (int i = Frames[index].Length - 1; i >= 0; --i)
  406. {
  407. int idx = Frames[index][i];
  408. float tx = SW[idx] / 2.0f;
  409. float ty = SH[idx] / 2.0f;
  410. float olda = g.getAlpha();
  411. g.addAlpha(SAlpha[idx]);
  412. g.pushTransform();
  413. g.translate(SX[idx] + tx, SY[idx] + ty);
  414. g.rotate(SRotate[idx]);
  415. g.scale(SScaleX[idx], SScaleY[idx]);
  416. tiles.render(g,
  417. STileID[idx],
  418. -tx, -ty,
  419. SFlip[idx]);
  420. g.popTransform();
  421. g.setAlpha(olda);
  422. }
  423. g.translate(-dx, -dy);
  424. }
  425. }
  426. }
  427. }