ShapeObject.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 ShapeObject : SceneObjectDecorator
  11. {
  12. private float _alpha, _cornerRadius, _fillAlpha, _fillAlpha1, _fillAlpha2, _gradientAngle, _rotation, _rotationX, _rotationY, _rotationZ, _scaleZ;
  13. private int _lineAlpha, _lineSize, _x, _y, _z, _points, _radius;
  14. private Boolean _cacheAsBitmap, _fillEnable, _lineEnabled, _visible;
  15. private string _fillType, _gradientType, _fillColor, _lineColor, _fillColor1, _fillColor2;
  16. private float[] _gradientAlphas;
  17. private string[] _gradientFills;
  18. private string[] _attributes = new string[24]{ "alpha", "cacheAsBitmap", "cornerRadius", "gradientAngle", "gradientType", "fillAlpha", "fillColor",
  19. "fillEnable", "fillType", "lineAlpha", "lineColor", "lineEnabled", "lineSize", "points", "radius",
  20. "rotation", "rotationX", "rotationY", "rotationZ", "scaleZ", "visible", "x",
  21. "y", "z"};
  22. private List<String> _shapeObjectAccessorChild = new List<string>();
  23. private List<XmlElement> _accessorChildList;
  24. private Properties _properties;
  25. public enum shape_type{
  26. Rectangle,
  27. Circle,
  28. Polygon
  29. }
  30. public ShapeObject(SceneObject sceneobject, shape_type shapeType) : base(sceneobject)
  31. {
  32. _shapeObjectAccessorChild.AddRange(_attributes);
  33. string objectType = "";
  34. switch (shapeType)
  35. {
  36. case shape_type.Rectangle:
  37. objectType = "com.yooba.shapes.RoundedRectangleShape";
  38. _shapeObjectAccessorChild.Remove("radius");
  39. break;
  40. case shape_type.Circle:
  41. objectType = "com.yooba.shapes.CircleShape";
  42. _shapeObjectAccessorChild.Remove("cornerRadius");
  43. break;
  44. case shape_type.Polygon:
  45. objectType = "com.yooba.shapes.PolygonShape";
  46. _shapeObjectAccessorChild.Remove("radius");
  47. _shapeObjectAccessorChild.Remove("cornerRadius");
  48. break;
  49. }
  50. sceneobject.setObjectType(objectType);
  51. _accessorChildList = new List<XmlElement>();
  52. _alpha = 1;
  53. _cacheAsBitmap = false;
  54. _cornerRadius = 0;
  55. _gradientAngle = 90;
  56. _gradientType = "linear";
  57. _fillAlpha = 100000;
  58. _fillAlpha1 = 100000;
  59. _fillAlpha2 = 100000;
  60. _fillColor = "16743690";
  61. _fillEnable = true;
  62. _fillType = "solid";
  63. _lineAlpha = 1;
  64. _lineColor = "6426397";
  65. _lineEnabled = false;
  66. _lineSize = 0;
  67. _points = 3;
  68. _radius = 0;
  69. _rotation = 0;
  70. _rotationX = 0;
  71. _rotationY = 0;
  72. _rotationZ = 0;
  73. _scaleZ = 1;
  74. _visible = true;
  75. _x = 0;
  76. _y = 0;
  77. _z = 0;
  78. _gradientAlphas = new float[2];
  79. _gradientAlphas[0] = 100000;
  80. _gradientAlphas[1] = 100000;
  81. _gradientFills = new string[2];
  82. _fillColor1 = _fillColor;
  83. _fillColor2 = "10834182";
  84. _properties = new Properties(true,false,true,true,true,true,true,true,false,true,true,false,true,true,true);
  85. }
  86. public override XmlElement getXMLTree()
  87. {
  88. XmlElement parent = base.getXMLTree();
  89. parent.AppendChild(Properties.getNode(getXMLDocumentRoot()));
  90. XmlElement acce = getXMLDocumentRoot().CreateElement("accessors");
  91. foreach (string s in _shapeObjectAccessorChild)
  92. {
  93. XmlElement xmlChild = getXMLDocumentRoot().CreateElement(s);
  94. FieldInfo fieldInfo = GetType().GetField("_" + s, BindingFlags.NonPublic | BindingFlags.Instance);
  95. if (fieldInfo != null)
  96. xmlChild.InnerText = fieldInfo.GetValue(this).ToString().Replace(",",".").ToLower();
  97. acce.AppendChild(xmlChild);
  98. }
  99. XmlElement gradientColor = getXMLDocumentRoot().CreateElement("gradientFills");
  100. gradientColor.InnerText = _fillColor1.ToString() + ", " + _fillColor2.ToString();
  101. XmlElement gradientAlpha = getXMLDocumentRoot().CreateElement("gradientAlphas");
  102. gradientAlpha.InnerText = _fillAlpha1.ToString().Replace(",", ".") + ", " + _fillAlpha2.ToString().Replace(",", ".");
  103. acce.AppendChild(gradientColor);
  104. acce.AppendChild(gradientAlpha);
  105. parent.AppendChild(acce);
  106. return parent;
  107. }
  108. public override void ConvertToYoobaUnits(int width, int height)
  109. {
  110. base.ConvertToYoobaUnits(width, height);
  111. if (_fillType.Equals("solid"))
  112. {
  113. _fillColor = getColorAsInteger(_fillColor).ToString();
  114. _fillAlpha /= 100000;
  115. }
  116. if (_fillType.Equals("gradient"))
  117. {
  118. _fillColor = getColorAsInteger(_fillColor1).ToString();
  119. _fillAlpha1 /= 100000;
  120. _fillAlpha2 /= 100000;
  121. _fillColor1 = _fillColor;
  122. _fillColor2 = getColorAsInteger(_fillColor2).ToString();
  123. _gradientAngle /= 60000;
  124. }
  125. _lineSize = (int) Math.Round((double)_lineSize / 12700);
  126. if (_lineSize <= 0)
  127. _lineEnabled = false;
  128. else
  129. _lineEnabled = true;
  130. _lineColor = getColorAsInteger(_lineColor).ToString();
  131. _cornerRadius = (float) Math.Round((_cornerRadius / 100000) * 128 * 4);
  132. _rotation /= 60000;
  133. }
  134. public override XmlDocument getXMLDocumentRoot()
  135. {
  136. return base.getXMLDocumentRoot();
  137. }
  138. public override void setXMLDocumentRoot(ref XmlDocument xmldocument)
  139. {
  140. base.setXMLDocumentRoot(ref xmldocument);
  141. }
  142. public override Properties getProperties()
  143. {
  144. return base.getProperties();
  145. }
  146. public override void setProperties(Properties properties)
  147. {
  148. base.setProperties(properties);
  149. }
  150. public int getColorAsInteger(string color)
  151. {
  152. if (color != "")
  153. return int.Parse(color, System.Globalization.NumberStyles.HexNumber);
  154. else
  155. return 0;
  156. }
  157. public float CornerRadius
  158. {
  159. get { return _cornerRadius; }
  160. set { _cornerRadius = value; }
  161. }
  162. public float ScaleZ
  163. {
  164. get { return _scaleZ; }
  165. set { _scaleZ = value; }
  166. }
  167. public string FillColor2
  168. {
  169. get { return _fillColor2; }
  170. set { _fillColor2 = value; }
  171. }
  172. public string FillColor1
  173. {
  174. get { return _fillColor1; }
  175. set { _fillColor1 = value; }
  176. }
  177. public float RotationZ
  178. {
  179. get { return _rotationZ; }
  180. set { _rotationZ = value; }
  181. }
  182. public float RotationY
  183. {
  184. get { return _rotationY; }
  185. set { _rotationY = value; }
  186. }
  187. public float RotationX
  188. {
  189. get { return _rotationX; }
  190. set { _rotationX = value; }
  191. }
  192. public float Rotation
  193. {
  194. get { return _rotation; }
  195. set { _rotation = value; }
  196. }
  197. public float GradientAngle
  198. {
  199. get { return _gradientAngle; }
  200. set { _gradientAngle = value; }
  201. }
  202. public float FillAlpha
  203. {
  204. get { return _fillAlpha; }
  205. set { _fillAlpha = value; }
  206. }
  207. public float Alpha
  208. {
  209. get { return _alpha; }
  210. set { _alpha = value; }
  211. }
  212. public float[] GradientAlphas
  213. {
  214. get { return _gradientAlphas; }
  215. set { _gradientAlphas = value; }
  216. }
  217. public int Z
  218. {
  219. get { return _z; }
  220. set { _z = value; }
  221. }
  222. public int Y
  223. {
  224. get { return _y; }
  225. set { _y = value; }
  226. }
  227. public int X
  228. {
  229. get { return _x; }
  230. set { _x = value; }
  231. }
  232. public int LineSize
  233. {
  234. get { return _lineSize; }
  235. set { _lineSize = value; }
  236. }
  237. public string LineColor
  238. {
  239. get { return _lineColor; }
  240. set { _lineColor = value; }
  241. }
  242. public int LineAlpha
  243. {
  244. get { return _lineAlpha; }
  245. set { _lineAlpha = value; }
  246. }
  247. public string FillColor
  248. {
  249. get { return _fillColor; }
  250. set { _fillColor = value; }
  251. }
  252. public Boolean Visible
  253. {
  254. get { return _visible; }
  255. set { _visible = value; }
  256. }
  257. public Boolean LineEnabled
  258. {
  259. get { return _lineEnabled; }
  260. set { _lineEnabled = value; }
  261. }
  262. public Boolean FillEnable
  263. {
  264. get { return _fillEnable; }
  265. set { _fillEnable = value; }
  266. }
  267. public Boolean CacheAsBitmap
  268. {
  269. get { return _cacheAsBitmap; }
  270. set { _cacheAsBitmap = value; }
  271. }
  272. public string GradientType
  273. {
  274. get { return _gradientType; }
  275. set { _gradientType = value; }
  276. }
  277. public string FillType
  278. {
  279. get { return _fillType; }
  280. set { _fillType = value; }
  281. }
  282. public string[] GradientFills
  283. {
  284. get { return _gradientFills; }
  285. set { _gradientFills = value; }
  286. }
  287. public int Radius
  288. {
  289. get { return _radius; }
  290. set { _radius = value; }
  291. }
  292. public int Points
  293. {
  294. get { return _points; }
  295. set { _points = value; }
  296. }
  297. public float FillAlpha2
  298. {
  299. get { return _fillAlpha2; }
  300. set { _fillAlpha2 = value; }
  301. }
  302. public float FillAlpha1
  303. {
  304. get { return _fillAlpha1; }
  305. set { _fillAlpha1 = value; }
  306. }
  307. public Properties Properties
  308. {
  309. get { return _properties; }
  310. set { _properties = value; }
  311. }
  312. internal void setAttributes(TableStyle tableStyle)
  313. {
  314. if(tableStyle==null)
  315. return;
  316. FillAlpha = (tableStyle.FillAlpha != 0) ? tableStyle.FillAlpha : FillAlpha;
  317. FillColor = (tableStyle.FillColor != "") ? tableStyle.FillColor : FillColor;
  318. LineSize = (tableStyle.LineSize != 0) ? tableStyle.LineSize : LineSize;
  319. LineColor = (tableStyle.LineColor != "") ? tableStyle.LineColor : LineColor;
  320. }
  321. }
  322. }