PatternObjectData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PatternObjectData.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Graphics;
  17. #endregion
  18. namespace SceneDataLibrary
  19. {
  20. /// <summary>
  21. /// Pattern data
  22. /// This data class is for a rectangle that has conversion parameters and textures.
  23. /// In Layout, pattern objects correspond to this pattern data.
  24. /// The data managed in this class contains:
  25. /// textures to be used, cutting range, display position (within a pattern group),
  26. /// scale, angle, center position, color, flip information, etc.
  27. ///
  28. /// パターンデータ
  29. /// テクスチャを伴い、変換用のパラメータをもった矩形のデータクラスです。
  30. /// Layoutではパターンオブジェクトが相当します。
  31. /// 保持するデータとして、使用するテクスチャ、切り取り範囲、
  32. /// (パターングループ内での)表示位置、スケール、角度、中心位置、色、反転情報
  33. /// などがあります。
  34. /// </summary>
  35. public class PatternObjectData
  36. {
  37. #region Fields
  38. private String textureName = String.Empty;//Texture name //テクスチャ名
  39. private Texture2D texture = null;//Texture substance //テクスチャの実体
  40. private Rectangle patternRect = new Rectangle();//Pattern rectangle
  41. private bool flipH = false;//Horizontal flip flag //水平反転フラグ
  42. private bool flipV = false;//Vertical flip flag //垂直反転フラグ
  43. private DrawData drawData = new DrawData();//Conversion information //変換情報
  44. //Temporary conversion information to play a sequence
  45. //
  46. //シーケンス再生用の一時的な変換情報
  47. private DrawData interpolationDrawData = new DrawData();
  48. #endregion
  49. #region Properties
  50. /// <summary>
  51. /// Obtains and sets the texture name.
  52. /// The setting is called from SceneDataReader on initialization of scenes.
  53. ///
  54. /// テクスチャ名の設定取得を行います。
  55. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  56. /// </summary>
  57. public String TextureName
  58. {
  59. get
  60. {
  61. return textureName;
  62. }
  63. set
  64. {
  65. textureName = value;
  66. }
  67. }
  68. /// <summary>
  69. /// Obtains and sets the pattern rectangle.
  70. /// The setting is called from SceneDataReader on initialization of scenes.
  71. ///
  72. /// パターン矩形の設定取得を行います。
  73. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  74. /// </summary>
  75. public Rectangle Rect
  76. {
  77. get
  78. {
  79. return patternRect;
  80. }
  81. set
  82. {
  83. patternRect = value;
  84. }
  85. }
  86. /// <summary>
  87. /// Obtains and sets the horizontal flip flag for patterns.
  88. /// The setting is called from SceneDataReader on initialization of scenes.
  89. ///
  90. /// パターンの水平反転フラグの設定取得を行います。
  91. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  92. /// </summary>
  93. public bool FlipH
  94. {
  95. get
  96. {
  97. return flipH;
  98. }
  99. set
  100. {
  101. flipH = value;
  102. }
  103. }
  104. /// <summary>
  105. /// Obtains and sets the vertical flip flag for patterns.
  106. /// The setting is called from SceneDataReader on initialization of scenes.
  107. ///
  108. /// パターンの垂直反転フラグの設定取得を行います。
  109. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  110. /// </summary>
  111. public bool FlipV
  112. {
  113. get
  114. {
  115. return flipV;
  116. }
  117. set
  118. {
  119. flipV = value;
  120. }
  121. }
  122. /// <summary>
  123. /// Obtains and sets the conversion parameters for drawing.
  124. /// The setting is called from SceneDataReader on initialization of scenes.
  125. ///
  126. /// 描画用変換パラメータの設定取得を行います。
  127. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  128. /// </summary>
  129. public DrawData Data
  130. {
  131. get
  132. {
  133. return drawData;
  134. }
  135. set
  136. {
  137. drawData = value;
  138. }
  139. }
  140. /// <summary>
  141. /// Obtains the drawing position.
  142. ///
  143. /// 描画位置を取得します。
  144. /// </summary>
  145. public Point Position
  146. {
  147. get
  148. {
  149. return drawData.Position;
  150. }
  151. }
  152. /// <summary>
  153. /// Obtains the drawing color.
  154. ///
  155. /// 描画色を取得します。
  156. /// </summary>
  157. public Color Color
  158. {
  159. get
  160. {
  161. return drawData.Color;
  162. }
  163. }
  164. /// <summary>
  165. /// Obtains the drawing conversion scale.
  166. ///
  167. /// 描画変換スケールを取得します。
  168. /// </summary>
  169. public Vector2 Scale
  170. {
  171. get
  172. {
  173. return drawData.Scale;
  174. }
  175. }
  176. /// <summary>
  177. /// Obtains the center position for drawing conversion.
  178. ///
  179. /// 描画変換中心を取得します。
  180. /// </summary>
  181. public Point Center
  182. {
  183. get
  184. {
  185. return drawData.Center;
  186. }
  187. }
  188. /// <summary>
  189. /// Obtains the rotation value for drawing conversion.
  190. ///
  191. /// 描画変換回転量を取得します。
  192. /// </summary>
  193. public float RotateZ
  194. {
  195. get
  196. {
  197. return drawData.RotateZ;
  198. }
  199. }
  200. /// <summary>
  201. /// Obtains and sets the texture.
  202. /// The setting is called from SceneDataReader on initialization of scenes.
  203. ///
  204. /// テクスチャの設定取得を行います。
  205. /// 設定はシーンの初期化時にSceneDataReaderから呼び出されます。
  206. /// </summary>
  207. private Texture2D Texture
  208. {
  209. get{
  210. return texture;
  211. }
  212. set
  213. {
  214. texture = value;
  215. }
  216. }
  217. /// <summary>
  218. /// The temporary drawing conversion information
  219. /// specified during sequence play. Based on this information,
  220. /// display items synchronized with the sequence can be positioned.
  221. /// </summary>
  222. [ContentSerializerIgnore]
  223. public DrawData InterpolationDrawData
  224. {
  225. get { return interpolationDrawData; }
  226. set { interpolationDrawData = value; }
  227. }
  228. #endregion
  229. /// <summary>
  230. /// Performs initialization.
  231. /// Loads the XNA graphic textures through ContentManager.
  232. ///
  233. /// 初期化を行います。
  234. /// ContentManagerを通して、
  235. /// XNAグラフィックのテクスチャを読み込みます。
  236. /// </summary>
  237. /// <param name="content">
  238. /// ContentManager
  239. ///
  240. /// コンテントマネージャー
  241. /// </param>
  242. public void Init(ContentManager content)
  243. {
  244. if (!String.IsNullOrEmpty(TextureName))
  245. {
  246. Texture = content.Load<Texture2D>(TextureName);
  247. }
  248. }
  249. /// <summary>
  250. /// Performs drawing.
  251. /// baseDrawData contains information for entire sequence conversion,
  252. /// and sequenceDrawData contains conversion information interpolated for
  253. /// sequence display (including conversion information for pattern objects
  254. /// themselves).
  255. ///
  256. /// 描画を行います。
  257. /// baseDrawDataは、シーケンス全体の変換を、
  258. /// sequenceDrawDataは、シーケンス表示のために動きを補完された
  259. /// 変換情報が入っています(これはパターンオブジェクト自体の変換情報を
  260. /// 含んでいます)。
  261. /// </summary>
  262. /// <param name="sb">
  263. /// SpriteBatch
  264. ///
  265. /// スプライトバッチ
  266. /// </param>
  267. /// <param name="sequenceDrawData">
  268. /// Conversion information for sequence
  269. ///
  270. /// シーケンス用変換情報
  271. /// </param>
  272. /// <param name="baseDrawData">
  273. /// Basic conversion information for drawing
  274. ///
  275. /// 描画用基本変換情報
  276. /// </param>
  277. public void Draw(SpriteBatch sb, DrawData sequenceDrawData,
  278. DrawData baseDrawData)
  279. {
  280. // If no texture is specified, returns.
  281. //
  282. // テクスチャが指定されていなければ抜ける
  283. if ((Texture == null) || Texture.IsDisposed)
  284. {
  285. return;
  286. }
  287. Vector2 position = new Vector2();
  288. //Creates a matrix and colors for drawing
  289. //from the interpolated conversion information.
  290. //This matrix is temporarily used to determine the display position.
  291. //
  292. //補完された変換情報から
  293. //描画のためのマトリクスと色を作成します
  294. //マトリクスは、表示位置を求めるための一時的なものです。
  295. float rotateZ = sequenceDrawData.RotateZ;
  296. Vector2 vectorScale = sequenceDrawData.Scale;
  297. Color color = sequenceDrawData.Color;
  298. Matrix matrix = Matrix.CreateTranslation(
  299. sequenceDrawData.Position.X + sequenceDrawData.Center.X,
  300. sequenceDrawData.Position.Y + sequenceDrawData.Center.Y,
  301. 0.0f
  302. );
  303. //If the basic conversion information is valid,
  304. //creates the matrix and colors.
  305. //
  306. //基本変換情報が有効なら、
  307. //マトリクスと色を作成します。
  308. if (null != baseDrawData)
  309. {
  310. rotateZ += baseDrawData.RotateZ;
  311. vectorScale *= baseDrawData.Scale;
  312. color = new Color( (byte)(color.R * baseDrawData.Color.R / 0xFF),
  313. (byte)(color.G * baseDrawData.Color.G / 0xFF),
  314. (byte)(color.B * baseDrawData.Color.B / 0xFF),
  315. (byte)(color.A * baseDrawData.Color.A / 0xFF));
  316. position = new Vector2(baseDrawData.Position.X,
  317. baseDrawData.Position.Y);
  318. matrix *=
  319. Matrix.CreateScale(new Vector3(baseDrawData.Scale.X,
  320. baseDrawData.Scale.Y, 1.0f)) *
  321. Matrix.CreateRotationZ(baseDrawData.RotateZ);
  322. }
  323. //Determines the final display position.
  324. //
  325. //最終の表示位置を求めます。
  326. position += new Vector2(matrix.Translation.X, matrix.Translation.Y);
  327. SpriteEffects effects = SpriteEffects.None;
  328. //If the sprite needs to be flipped, apply the flip information to it.
  329. //
  330. //反転がある場合、適用します。
  331. if (flipH)
  332. effects |= SpriteEffects.FlipHorizontally;
  333. if (flipV)
  334. effects |= SpriteEffects.FlipVertically;
  335. //Drawing
  336. //
  337. //描画
  338. sb.Draw(Texture, position, Rect, color,
  339. MathHelper.ToRadians(rotateZ),
  340. new Vector2(sequenceDrawData.Center.X, sequenceDrawData.Center.Y),
  341. vectorScale,
  342. effects,
  343. 1.0f);
  344. }
  345. }
  346. }