SimpleSceneObject.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Threading.Tasks;
  8. namespace ConsoleApplication
  9. {
  10. public class SimpleSceneObject : SceneObject
  11. {
  12. private float _alpha, _height, _width, _rotation;
  13. //BoundsX and BoundsY corresponds to positions from left top corner
  14. //ClipHeight and ClipWidth are the height and width of the scene object
  15. private int _boundsX, _boundsY, _clipHeight, _clipWidth, _flip, _z;
  16. private string _clipID, _type, _name;
  17. private Boolean _hidden, _isLine;
  18. private XmlDocument _doc;
  19. private Properties _properties;
  20. private OptimizedClip _optimizedClip;
  21. private string[] _attributes = new string[14]{ "type", "clipID","z", "boundsX", "boundsY", "clipWidth", "clipHeight",
  22. "width","height", "rotation", "alpha","name", "hidden", "flip"};
  23. private SimpleSceneObject simpleSceneObjectShape;
  24. public SimpleSceneObject()
  25. {
  26. _clipID = Guid.NewGuid().ToString();
  27. _name = Guid.NewGuid().ToString();
  28. _doc = new XmlDocument();
  29. _alpha = 1;
  30. _hidden = false;
  31. _width = 0;
  32. _height = 0;
  33. _z = 0;
  34. _flip = 0;
  35. _properties = new Properties();
  36. _optimizedClip = new OptimizedClip();
  37. _isLine = false;
  38. _rotation = 0;
  39. _z = 0;
  40. _type = "";
  41. _boundsX = 0;
  42. _boundsY = 0;
  43. _clipHeight = 0;
  44. _clipWidth = 0;
  45. }
  46. public SimpleSceneObject(SimpleSceneObject s)
  47. {
  48. _clipID = s.ClipID;
  49. _name = s.Name;
  50. _doc = new XmlDocument();
  51. _alpha = s.Alpha;
  52. _hidden = s.Hidden;
  53. _width = s.Width;
  54. _height = s.Height;
  55. _z = s.Z;
  56. _flip = 0;
  57. _properties = new Properties();
  58. _optimizedClip = new OptimizedClip();
  59. _rotation = s.Rotation;
  60. _z = s.Z;
  61. _type = s.Type;
  62. _isLine = s.IsLine;
  63. _boundsX = s.BoundsX;
  64. _boundsY = s.BoundsY;
  65. _clipHeight = s.ClipHeight;
  66. _clipWidth = s.ClipWidth;
  67. }
  68. public object Clone()
  69. {
  70. return this;
  71. }
  72. public void ConvertToYoobaUnits(int width, int height)
  73. {
  74. //_boundsX, _boundsY, _clipHeight, _clipWidth,
  75. int pptWidth = width, pptHeight = height, yoobaWidth = 1024, yoobaHeight = 768;
  76. yoobaWidth = (int)Math.Round(pptWidth * ((double)yoobaHeight / (double)pptHeight));
  77. Console.WriteLine(yoobaWidth + ":" + yoobaHeight);
  78. float scaleWidth = (float)yoobaWidth / (float)pptWidth, scaleHeight = (float)yoobaHeight / (float)pptHeight;
  79. _boundsX = (int) Math.Round(_boundsX * scaleWidth);
  80. _boundsY = (int) Math.Round(_boundsY * scaleHeight);
  81. _clipWidth = (int) Math.Round(_clipWidth * scaleWidth);
  82. _clipHeight = (int) Math.Round(_clipHeight * scaleHeight);
  83. //rotation
  84. if(!IsLine)
  85. handleTranslationWhenRotate();
  86. _rotation = _rotation / 60000;
  87. }
  88. public void setZindex(int z)
  89. {
  90. _z = z;
  91. }
  92. public void handleTranslationWhenRotate()
  93. {
  94. double rotationInDegrees = (double)_rotation / 60000;
  95. //Handle negative and too large angles
  96. while (rotationInDegrees < 0)
  97. rotationInDegrees += 360;
  98. while (rotationInDegrees > 360)
  99. rotationInDegrees -= 360;
  100. double tempRot = 0;
  101. //Handle the 4 different cases
  102. if (rotationInDegrees >= 0 && rotationInDegrees <= 90)
  103. tempRot = rotationInDegrees;
  104. else if (rotationInDegrees > 90 && rotationInDegrees <= 180)
  105. tempRot = 180 - rotationInDegrees;
  106. else if (rotationInDegrees > 180 && rotationInDegrees <= 270)
  107. tempRot = rotationInDegrees - 180;
  108. else if (rotationInDegrees > 270 && rotationInDegrees < 360)
  109. tempRot = 360 - rotationInDegrees;
  110. //Store the Center of mass for the object before the rotation
  111. double COM_X = _boundsX + _clipWidth / 2,
  112. COM_Y = _boundsY + _clipHeight / 2;
  113. //Calculate the top left position for the rotated object, stored in newX and newY
  114. double newAngle = 90 - tempRot;
  115. double newAngleInRadians = newAngle * Math.PI / 180;
  116. double stepLeft = Math.Cos(newAngleInRadians) * ClipHeight;
  117. double newX = stepLeft + BoundsX;
  118. double newY = BoundsY;
  119. //Calculate the center of mass position for the rotated object
  120. newX += Math.Sin(newAngleInRadians) * ClipWidth / 2;
  121. newY += Math.Cos(newAngleInRadians) * ClipWidth / 2;
  122. newX += Math.Sin(newAngleInRadians - Math.PI / 2) * ClipHeight / 2;
  123. newY += Math.Cos(newAngleInRadians - Math.PI / 2) * ClipHeight / 2;
  124. //Calculate the difference in COM
  125. double diffX = newX - COM_X;
  126. double diffY = newY - COM_Y;
  127. //Subtract the diffrence from the original bounds
  128. _boundsX -= (int)Math.Round(diffX);
  129. _boundsY -= (int)Math.Round(diffY);
  130. }
  131. public Properties getProperties()
  132. {
  133. return _properties;
  134. }
  135. public void setProperties(Properties properties)
  136. {
  137. _properties = properties;
  138. }
  139. public void setXMLDocumentRoot(ref XmlDocument xmldocument)
  140. {
  141. _doc = xmldocument;
  142. }
  143. public void setObjectType(string objectType)
  144. {
  145. _type = objectType;
  146. }
  147. public XmlDocument getXMLDocumentRoot()
  148. {
  149. return _doc;
  150. }
  151. public XmlElement getXMLTree()
  152. {
  153. //generateAttributes();
  154. XmlElement xmlElement = _doc.CreateElement("sceneObject");
  155. xmlElement.AppendChild(_optimizedClip.getOptimizedClipNode(_doc));
  156. xmlElement.AppendChild(getDsColNode());
  157. foreach (string s in _attributes)
  158. {
  159. XmlAttribute xmlAttr = _doc.CreateAttribute(s);
  160. FieldInfo fieldInfo = GetType().GetField("_" + s, BindingFlags.NonPublic | BindingFlags.Instance);
  161. if (fieldInfo != null)
  162. xmlAttr.Value = fieldInfo.GetValue(this).ToString();
  163. xmlElement.Attributes.Append(xmlAttr);
  164. }
  165. _doc.DocumentElement.AppendChild(xmlElement);
  166. return xmlElement;
  167. }
  168. public XmlElement getDsColNode()
  169. {
  170. XmlElement dsCol = getXMLDocumentRoot().CreateElement("dsCol");
  171. XmlCDataSection cData = getXMLDocumentRoot().CreateCDataSection("");
  172. dsCol.AppendChild(cData);
  173. return dsCol;
  174. }
  175. public Boolean IsLine
  176. {
  177. get { return _isLine; }
  178. set { _isLine = value; }
  179. }
  180. public int Z
  181. {
  182. get { return _z; }
  183. set { _z = value; }
  184. }
  185. public float Rotation
  186. {
  187. get { return _rotation; }
  188. set { _rotation = value; }
  189. }
  190. public float Width
  191. {
  192. get { return _width; }
  193. set { _width = value; }
  194. }
  195. public float Height
  196. {
  197. get { return _height; }
  198. set { _height = value; }
  199. }
  200. public float Alpha
  201. {
  202. get { return _alpha; }
  203. set { _alpha = value; }
  204. }
  205. public int ClipWidth
  206. {
  207. get { return _clipWidth; }
  208. set { _clipWidth = value; }
  209. }
  210. public int ClipHeight
  211. {
  212. get { return _clipHeight; }
  213. set { _clipHeight = value; }
  214. }
  215. public int BoundsY
  216. {
  217. get { return _boundsY; }
  218. set { _boundsY = value; }
  219. }
  220. public int BoundsX
  221. {
  222. get { return _boundsX; }
  223. set { _boundsX = value; }
  224. }
  225. public string Type
  226. {
  227. get { return _type; }
  228. set { _type = value; }
  229. }
  230. public string Name
  231. {
  232. get { return _name; }
  233. set { _name = value; }
  234. }
  235. public string ClipID
  236. {
  237. get { return _clipID; }
  238. set { _clipID = value; }
  239. }
  240. public Boolean Hidden
  241. {
  242. get { return _hidden; }
  243. set { _hidden = value; }
  244. }
  245. }
  246. }