Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO;
- using System.Threading;
- using System.Diagnostics;
- using Microsoft.Win32;
- namespace SpriteViewerViktoriaKirschbaum
- {
- public partial class MainWindow : Window
- {
- public List<Image> m_Images;
- private BitmapImage m_ImageAtlas;
- private CroppedBitmap m_CroppedBitmap;
- private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
- private Stopwatch sw = new Stopwatch();
- private string m_ImagePath;
- private int m_Speed = 500;
- private int m_AmountColumn = 4;
- private int m_AmountRow = 2;
- private int m_Height = 1024;
- private int m_Width = 1024;
- private int m_newHeight;
- private int m_newWidth;
- private int m_WidthFromTile;
- private int m_HeightFromTile;
- private int i = 0; //row
- private int j = 0; //column
- private int m_Counter;
- private bool m_DoAnimation = false;
- public MainWindow()
- {
- InitializeComponent();
- UpdateTextBoxes();
- m_ImageAtlas = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Tiles\ExampleImageOne.png", UriKind.Absolute));
- Image_Preview.Source = m_ImageAtlas;
- }
- //TextBoxes
- #region
- private void UpdateTextBoxes()
- {
- if (m_Speed == 0)
- {
- Label_StatusBar.Content = "Enter a speed in ms.";
- }
- else if (m_AmountColumn == 0)
- {
- Label_StatusBar.Content = "You need to enter the amount of columns.";
- }
- else if (m_AmountRow == 0)
- {
- Label_StatusBar.Content = "You need to enter the amount of rows.";
- }
- else if (m_Height == 0)
- {
- Label_StatusBar.Content = "Please enter the height of the imported image.";
- }
- else if (m_Width == 0)
- {
- Label_StatusBar.Content = "Please enter the width of the imported image.";
- }
- else
- {
- Label_StatusBar.Content = "Sprite is ready.";
- }
- TextBox_Speed_Title.Content = "Animation Speed in ms:";
- TextBox_AmountColumn_Title.Content = "Amount Columns:";
- TextBox_AmountRow_Title.Content = "Amount Rows:";
- TextBox_Height_Title.Content = "Height:";
- TextBox_Width_Title.Content = "Width:";
- }
- private void TextBox_Speed_KeyDown(object sender, TextChangedEventArgs e)
- {
- try
- {
- m_Speed = Convert.ToInt32(TextBox_Speed.Text);
- }
- catch
- {
- MessageBox.Show("Enter a number.");
- }
- }
- private void TextBox_AmountColumn_KeyDown(object sender, TextChangedEventArgs e)
- {
- try
- {
- m_AmountColumn = Convert.ToInt32(TextBox_AmountColumn.Text);
- }
- catch
- {
- MessageBox.Show("Enter a number.");
- }
- }
- private void TextBox_AmountRow_KeyDown(object sender, TextChangedEventArgs e)
- {
- try
- {
- m_AmountRow = Convert.ToInt32(TextBox_AmountRow.Text);
- }
- catch
- {
- MessageBox.Show("Enter a number.");
- }
- }
- private void TextBox_Height_KeyDown(object sender, TextChangedEventArgs e)
- {
- try
- {
- m_Height = Convert.ToInt32(TextBox_Height.Text);
- }
- catch
- {
- MessageBox.Show("Enter a number.");
- }
- }
- private void TextBox_Width_KeyDown(object sender, TextChangedEventArgs e)
- {
- try
- {
- m_Width = Convert.ToInt32(TextBox_Width.Text);
- }
- catch
- {
- MessageBox.Show("Enter a number.");
- }
- }
- #endregion
- //Buttons
- #region
- private void Button_Click_Start(object sender, RoutedEventArgs e)
- {
- UpdateTextBoxes();
- if (m_Speed == 0 || m_Height == 0 || m_Width == 0)
- {
- MessageBox.Show("You need to enter a number for all of the configurations. See the status bar for more information.");
- }
- else if (m_Speed < 0 || m_AmountColumn < 0 || m_AmountRow < 0 || m_Height < 0 || m_Width < 0)
- {
- MessageBox.Show("You need to enter a positive number for all of the configurations.");
- }
- else
- {
- m_newHeight = m_Height / m_AmountColumn; //301 = 1024 / 4
- m_newWidth = m_Width / m_AmountRow;
- m_DoAnimation = true;
- dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
- dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, m_Speed);
- dispatcherTimer.Start();
- }
- }
- private void Button_Click_ImportSprite(object sender, RoutedEventArgs e)
- {
- OpenFileDialog Dialog = new OpenFileDialog();
- Dialog.Filter = "PNG Files (*.png)|*.png|JPEG Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp";
- Dialog.Multiselect = false;
- bool? ImageSelected = Dialog.ShowDialog();
- if (ImageSelected == true)
- {
- m_ImagePath = Dialog.FileName;
- MessageBox.Show("Importing image...");
- m_ImageAtlas = new BitmapImage(new Uri(m_ImagePath, UriKind.Absolute));
- Image_Preview.Source = m_ImageAtlas;
- }
- }
- #endregion
- private void dispatcherTimer_Tick(object sender, EventArgs e)
- {
- if (Image_Sprite == null)
- {
- return;
- }
- if (j > m_AmountColumn - 1)
- {
- i++;
- j = 0;
- }
- if(i > m_AmountRow - 1 && j > m_AmountColumn - 1)
- {
- i = 0;
- j = 0;
- }
- m_WidthFromTile = j * m_newWidth;
- m_HeightFromTile = i * m_newHeight;
- m_CroppedBitmap = new CroppedBitmap(m_ImageAtlas, new Int32Rect(m_WidthFromTile, m_HeightFromTile, m_newWidth, m_newHeight));
- Image_Sprite.Source = m_CroppedBitmap;
- i++;
- i = i;
- j = j;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement