Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.IO;
  16. using System.Threading;
  17. using System.Diagnostics;
  18. using Microsoft.Win32;
  19.  
  20. namespace SpriteViewerViktoriaKirschbaum
  21. {
  22. public partial class MainWindow : Window
  23. {
  24. public List<Image> m_Images;
  25.  
  26. private BitmapImage m_ImageAtlas;
  27. private CroppedBitmap m_CroppedBitmap;
  28. private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
  29. private Stopwatch sw = new Stopwatch();
  30.  
  31. private string m_ImagePath;
  32.  
  33. private int m_Speed = 500;
  34. private int m_AmountColumn = 4;
  35. private int m_AmountRow = 2;
  36. private int m_Height = 1024;
  37. private int m_Width = 1024;
  38.  
  39. private int m_newHeight;
  40. private int m_newWidth;
  41. private int m_WidthFromTile;
  42. private int m_HeightFromTile;
  43. private int i = 0; //row
  44. private int j = 0; //column
  45. private int m_Counter;
  46.  
  47. private bool m_DoAnimation = false;
  48.  
  49. public MainWindow()
  50. {
  51. InitializeComponent();
  52.  
  53. UpdateTextBoxes();
  54.  
  55. m_ImageAtlas = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Tiles\ExampleImageOne.png", UriKind.Absolute));
  56. Image_Preview.Source = m_ImageAtlas;
  57. }
  58.  
  59. //TextBoxes
  60. #region
  61. private void UpdateTextBoxes()
  62. {
  63. if (m_Speed == 0)
  64. {
  65. Label_StatusBar.Content = "Enter a speed in ms.";
  66. }
  67. else if (m_AmountColumn == 0)
  68. {
  69. Label_StatusBar.Content = "You need to enter the amount of columns.";
  70. }
  71. else if (m_AmountRow == 0)
  72. {
  73. Label_StatusBar.Content = "You need to enter the amount of rows.";
  74. }
  75. else if (m_Height == 0)
  76. {
  77. Label_StatusBar.Content = "Please enter the height of the imported image.";
  78. }
  79. else if (m_Width == 0)
  80. {
  81. Label_StatusBar.Content = "Please enter the width of the imported image.";
  82. }
  83. else
  84. {
  85. Label_StatusBar.Content = "Sprite is ready.";
  86. }
  87.  
  88. TextBox_Speed_Title.Content = "Animation Speed in ms:";
  89. TextBox_AmountColumn_Title.Content = "Amount Columns:";
  90. TextBox_AmountRow_Title.Content = "Amount Rows:";
  91. TextBox_Height_Title.Content = "Height:";
  92. TextBox_Width_Title.Content = "Width:";
  93. }
  94.  
  95. private void TextBox_Speed_KeyDown(object sender, TextChangedEventArgs e)
  96. {
  97. try
  98. {
  99. m_Speed = Convert.ToInt32(TextBox_Speed.Text);
  100. }
  101. catch
  102. {
  103. MessageBox.Show("Enter a number.");
  104. }
  105. }
  106.  
  107. private void TextBox_AmountColumn_KeyDown(object sender, TextChangedEventArgs e)
  108. {
  109. try
  110. {
  111. m_AmountColumn = Convert.ToInt32(TextBox_AmountColumn.Text);
  112. }
  113. catch
  114. {
  115. MessageBox.Show("Enter a number.");
  116. }
  117. }
  118.  
  119. private void TextBox_AmountRow_KeyDown(object sender, TextChangedEventArgs e)
  120. {
  121. try
  122. {
  123. m_AmountRow = Convert.ToInt32(TextBox_AmountRow.Text);
  124. }
  125. catch
  126. {
  127. MessageBox.Show("Enter a number.");
  128. }
  129. }
  130.  
  131. private void TextBox_Height_KeyDown(object sender, TextChangedEventArgs e)
  132. {
  133. try
  134. {
  135. m_Height = Convert.ToInt32(TextBox_Height.Text);
  136. }
  137. catch
  138. {
  139. MessageBox.Show("Enter a number.");
  140. }
  141. }
  142.  
  143. private void TextBox_Width_KeyDown(object sender, TextChangedEventArgs e)
  144. {
  145. try
  146. {
  147. m_Width = Convert.ToInt32(TextBox_Width.Text);
  148. }
  149. catch
  150. {
  151. MessageBox.Show("Enter a number.");
  152. }
  153. }
  154. #endregion
  155.  
  156. //Buttons
  157. #region
  158. private void Button_Click_Start(object sender, RoutedEventArgs e)
  159. {
  160. UpdateTextBoxes();
  161.  
  162. if (m_Speed == 0 || m_Height == 0 || m_Width == 0)
  163. {
  164. MessageBox.Show("You need to enter a number for all of the configurations. See the status bar for more information.");
  165. }
  166. else if (m_Speed < 0 || m_AmountColumn < 0 || m_AmountRow < 0 || m_Height < 0 || m_Width < 0)
  167. {
  168. MessageBox.Show("You need to enter a positive number for all of the configurations.");
  169. }
  170. else
  171. {
  172. m_newHeight = m_Height / m_AmountColumn; //301 = 1024 / 4
  173. m_newWidth = m_Width / m_AmountRow;
  174.  
  175. m_DoAnimation = true;
  176. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  177. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, m_Speed);
  178. dispatcherTimer.Start();
  179. }
  180. }
  181.  
  182. private void Button_Click_ImportSprite(object sender, RoutedEventArgs e)
  183. {
  184. OpenFileDialog Dialog = new OpenFileDialog();
  185. Dialog.Filter = "PNG Files (*.png)|*.png|JPEG Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp";
  186. Dialog.Multiselect = false;
  187.  
  188. bool? ImageSelected = Dialog.ShowDialog();
  189.  
  190. if (ImageSelected == true)
  191. {
  192. m_ImagePath = Dialog.FileName;
  193.  
  194. MessageBox.Show("Importing image...");
  195. m_ImageAtlas = new BitmapImage(new Uri(m_ImagePath, UriKind.Absolute));
  196.  
  197. Image_Preview.Source = m_ImageAtlas;
  198. }
  199. }
  200. #endregion
  201.  
  202. private void dispatcherTimer_Tick(object sender, EventArgs e)
  203. {
  204. if (Image_Sprite == null)
  205. {
  206. return;
  207. }
  208.  
  209. if (j > m_AmountColumn - 1)
  210. {
  211. i++;
  212. j = 0;
  213. }
  214.  
  215. if(i > m_AmountRow - 1 && j > m_AmountColumn - 1)
  216. {
  217. i = 0;
  218. j = 0;
  219. }
  220.  
  221. m_WidthFromTile = j * m_newWidth;
  222. m_HeightFromTile = i * m_newHeight;
  223.  
  224. m_CroppedBitmap = new CroppedBitmap(m_ImageAtlas, new Int32Rect(m_WidthFromTile, m_HeightFromTile, m_newWidth, m_newHeight));
  225. Image_Sprite.Source = m_CroppedBitmap;
  226.  
  227. i++;
  228.  
  229. i = i;
  230. j = j;
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement