Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DSDCG.Models;
- 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.Reflection;
- using System.Threading;
- using DSDCG.Workers;
- namespace DSDCG
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MediaItem _activeItem;
- public Container _activeContainer;
- public Container Storyboard;
- //něco nám užírá CIDy, asi při manipulaci s kontejnerama se někde něco pořád tvoří... tick, vyřešeno
- //něco způsobuje, že se neukládá / nepřenáší / nenačítá čas...
- public int currentCID = 1; //storyboard má 1, další kontejnery maj 2 a furt nahoru...
- public int currentMID = 1; //není žádný rootitem takže první přidělujeme jedničku...
- public MainWindow()
- {
- InitializeComponent();
- Storyboard = new Container(currentCID, "Storyboard", new TimeSpan(0, 0, 0));
- currentCID++;
- _activeContainer = new Container(currentCID, "Kontejner 1", new TimeSpan(0, 0, 30));
- currentCID++;
- LoadActiveContainer(_activeContainer);
- this.Storygrid.Children.Add(DrawSomething(Storyboard)); //vykreslíme /root kontejner
- }
- #region obsluha formu
- private void Button_Click_1(object sender, RoutedEventArgs e) //opensinglefiledialog
- {
- Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
- dlg.Filter = "soubory obrázků |*.jpg;*.jpeg;*.png;*.bmp;*.gif|soubory videa |*.avi;*.mp4;*.mkv;*.wmv"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- // Process open file dialog box results
- if (result == true)
- {
- // Open document
- MediaItemType newitemtype = MediaItemType.text;
- FileInfo fi = new FileInfo(dlg.FileName);
- if (fi.Extension == ".jpg" || fi.Extension == ".jpeg" || fi.Extension == ".png" || fi.Extension == ".bmp" || fi.Extension == ".gif")
- {
- newitemtype = MediaItemType.image;
- }
- else if (fi.Extension == ".avi" || fi.Extension == ".mp4" || fi.Extension == ".mkv" || fi.Extension == ".wmv")
- {
- newitemtype = MediaItemType.video;
- }
- _activeItem = new MediaItem(currentMID, dlg.SafeFileName, System.IO.Path.GetDirectoryName(dlg.FileName), newitemtype, string.Empty, string.Empty, new TimeSpan(0, 0, 10));
- currentMID++;
- LoadActiveItem(_activeItem);
- }
- }
- private void SaveStoryboardButton_Click(object sender, RoutedEventArgs e)//savefiledialog - pouze ukládá storyboard.xml nekonsoliduje...
- {
- Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
- dlg.Filter = "Nekonsolidovaný storyboard |*.sb"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- if (result == true)
- {
- //budeme muset konsolidovat zdroje... řešit složky...
- //a všechno to správně seserializovat a uložit a zabalit a udělat reverzní operace...
- Serializer serializer1 = new Serializer();
- //ask about saving changes maybe?
- serializer1.SerializeStoryboard(Storyboard, dlg.FileName, false, dlg.SafeFileName);
- }
- }
- private void LoadStoryboardButton_Click(object sender, RoutedEventArgs e)//openfiledialog - načítá pouze nekonsolidované kampaně
- {
- Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
- dlg.Filter = "Nekonsolidovaný storyboard |*.sb"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- if (result == true)
- {
- Serializer serializer1 = new Serializer();
- //ask about saving changes maybe?
- Storyboard = serializer1.DeserializeStoryboard(dlg.FileName);
- //a překreslíme:
- redraw();
- }
- }
- private void ExportButton_Click_1(object sender, RoutedEventArgs e)//konsoliduje soubory a tím finalizuje storyboard k přehrávání.
- {
- string boardroot;
- Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
- dlg.Filter = "Konsolidovaný storyboard |*.ksb"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- if (result == true)
- {
- boardroot = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dlg.FileName));
- //budeme muset konsolidovat zdroje... řešit složky...
- //a všechno to správně seserializovat a uložit a zabalit a udělat reverzní operace...
- Konsolidator konsolidator1 = new Konsolidator();
- Serializer serializer1 = new Serializer();
- //ask about saving changes maybe?
- konsolidator1.ConsolidateStoryboard(Storyboard, boardroot);
- serializer1.SerializeStoryboard(Storyboard, boardroot, true, dlg.SafeFileName);
- }
- }
- private void saveitembutton_Click(object sender, RoutedEventArgs e)
- {
- SaveActiveItem();
- redraw();
- } //uloží změny z formu do activeitemu
- private void discarditemchangesbutton_Click(object sender, RoutedEventArgs e)
- {
- LoadActiveItem(_activeItem);
- } //odmaže změny - vrátí co je v _activeitemu
- private void discardcontainerchanges_Click(object sender, RoutedEventArgs e)
- {
- LoadActiveContainer(_activeContainer);
- } //odmaže změny - vrátí co je v activecontaineru
- private void DeleteActiveItemButton_Click(object sender, RoutedEventArgs e)//smaže aktivní item
- {
- DeleteItem(_activeItem);
- redraw();
- }
- private void savecontainer_Click(object sender, RoutedEventArgs e)//uloží změny z formuláře do aktivního kontejneru
- {
- SaveActiveContainer();
- redraw();
- }
- private void addtocontainer_Click(object sender, RoutedEventArgs e)
- {
- //changedvalues keep/discard dialog here...
- _activeContainer.mediaItems.Add(new MediaItem(_activeItem.MID, _activeItem.name, _activeItem.path, _activeItem.type.Value, _activeItem.title, _activeItem.text, _activeItem.duration));
- //a překreslíme:
- redraw();
- } //přidá do aktivního kontejneru aktivní media item...
- private void CreateInActiveButton_Click(object sender, RoutedEventArgs e)
- {
- Container newcont = new Container();
- newcont.name = this.containername.Text;
- newcont.cid = currentCID;
- currentCID++;
- newcont.behaviour = (Behaviour)this.containerbehaviour.SelectedItem;
- try
- {
- newcont.duration = TimeSpan.Parse(this.containerduration.Text);
- _activeContainer.containers.Add(newcont);
- }
- catch
- {
- MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
- LoadActiveContainer(newcont);
- }
- finally { redraw(); }
- } //vytvoří z dat ve formu kontejner do aktivního kontejneru
- private void DeleteActiveContainer_Click(object sender, RoutedEventArgs e)
- {
- DeleteContainer(_activeContainer.cid);
- redraw();
- } //odmaže aktivní kontejner a všechny subkontejnery
- private void redrawButton_Click(object sender, RoutedEventArgs e)
- {
- redraw();
- } //znovuvykreslovací tlačítko boardu.
- void itembtn_Click(object sender, RoutedEventArgs e)
- {
- ItemButton clickeditem = sender as ItemButton;
- _activeItem = clickeditem.RelatedMediaItem;
- LoadActiveItem(_activeItem);
- } //co se stane když někdo klikne na mediaitembutton
- void congrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- e.Handled = true;
- BoardGrid contgrid = new BoardGrid();
- contgrid = sender as BoardGrid;
- contgrid.RelatedContainer.contbrush = contgrid.Background;
- _activeContainer = contgrid.RelatedContainer;
- LoadActiveContainer(contgrid.RelatedContainer);
- } //selektor kontejnerů na kliknutí...
- private void exit_Click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- } //exitovací tlačítko :)
- #endregion
- #region methods
- public void redraw() //znovuvykreslovací metoda storyboardu...
- {
- Storygrid.Children.Clear();
- this.Storygrid.Children.Add(DrawSomething(Storyboard));
- }
- private void LoadActiveItem(MediaItem ItemToBeLoaded) //načte _activeitem do formu...
- {
- this.filename.Text = ItemToBeLoaded.name;
- this.filepath.Text = ItemToBeLoaded.path;
- this.filepath.ToolTip = ItemToBeLoaded.path;
- this.filetype.SelectedItem = ItemToBeLoaded.type;
- try
- {
- this.title.Text = _activeItem.title;
- }
- catch (Exception)
- {
- this.title.Text = string.Empty;
- }
- try
- {
- this.text.Text = _activeItem.text;
- }
- catch (Exception)
- {
- this.text.Text = string.Empty;
- }
- try
- {
- this.duration.Text = _activeItem.duration.ToString();
- }
- catch (Exception)
- {
- this.duration.Text = new TimeSpan(0, 13, 37).ToString();
- }
- }
- private void SaveActiveItem() //savne data z formu do _activeitem
- {
- _activeItem.name = this.filename.Text;
- _activeItem.path = this.filepath.Text;
- _activeItem.type = (MediaItemType)this.filetype.SelectedItem;
- _activeItem.title = this.title.Text;
- _activeItem.text = this.text.Text;
- try
- {
- _activeItem.duration = TimeSpan.Parse(this.duration.Text);
- }
- catch
- {
- MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
- LoadActiveItem(_activeItem);
- }
- }
- private void LoadActiveContainer(Container ContainerToBeLoaded) //načte _activecontainer do formu
- {
- this.containername.Text = ContainerToBeLoaded.name;
- this.ActiveContainerNameLabel.Content = ContainerToBeLoaded.name;
- try
- {
- this.containerbehaviour.SelectedItem = ContainerToBeLoaded.behaviour;
- }
- catch
- {
- }
- if (ContainerToBeLoaded.contbrush != null)
- {
- ActiveContainerBorder.BorderBrush = ContainerToBeLoaded.contbrush;
- }
- else
- {
- ActiveContainerBorder.BorderBrush = Brushes.Transparent;
- }
- if (_activeContainer.duration != null)
- {
- this.containerduration.Text = _activeContainer.duration.ToString();
- }
- if (_activeContainer.duration == null)
- {
- this.containerduration.Text = new TimeSpan(0, 0, 20).ToString();
- }
- }
- private void SaveActiveContainer() //načte data z formu do _activecontainer
- {
- _activeContainer.name = this.containername.Text;
- _activeContainer.behaviour = (Behaviour)this.containerbehaviour.SelectedItem;
- try
- {
- _activeContainer.duration = TimeSpan.Parse(this.containerduration.Text);
- }
- catch
- {
- MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
- LoadActiveContainer(_activeContainer);
- }
- }
- private void UpdateContainer(int cid, Container newcont) //první část rekurzivního procházení kontejnerů...
- {
- if (cid == 1)
- {
- Storyboard = newcont;
- }
- else
- {
- Container ContainerToUpdate = new Container();
- ContainerToUpdate = searchContainerByID(cid, Storyboard, false);
- if (ContainerToUpdate != null || ContainerToUpdate.cid != 0)
- {
- ContainerToUpdate = newcont;
- }
- }
- }
- private Container searchContainerByID(int cid, Container startcont, bool returnParent) //rekurzivní procházecí funkce kontejnerů, používám ji abych mohl editovat storyboard
- {
- //Storyboard.containers.Find(delegate(Container c) { return c.cid == cid; }); alternativní vyhledávací metoda
- if (!returnParent)
- {
- if (startcont.cid == cid)
- {
- return startcont;
- }
- else
- {
- foreach (Container cont in startcont.containers)
- {
- return (searchContainerByID(cid, cont, returnParent));
- }
- return null;
- }
- }
- else
- {
- foreach (Container cont in startcont.containers)
- {
- if (cont.cid == cid)
- {
- return startcont;
- }
- else
- {
- return (searchContainerByID(cid, cont, true));
- }
- }
- return null;
- }
- }
- private Container searchContainerByMID(int MID, Container startcont) //najde kontejner obsahující MID
- {
- if (startcont.mediaItems.Find(delegate(MediaItem item)
- {
- return item.MID == MID;
- }) != null)
- {
- return startcont;
- }
- else
- {
- foreach (Container cont in startcont.containers)
- {
- return searchContainerByMID(MID, cont);
- }
- }
- return null;
- }
- public TextBlock GenerateGridTextBlock(string text, int fontsize, Thickness paddingThickness, int row, int col)
- {
- TextBlock newblock = new TextBlock();
- newblock.Text = text;
- newblock.FontSize = fontsize;
- newblock.Padding = paddingThickness;
- Grid.SetRow(newblock, row);
- Grid.SetColumn(newblock, col);
- return newblock;
- } //šikovná věcička, ušetřila mi spoustu řádků textblocků
- private Brush PickRandomBrush()
- {
- Brush[] AcceptableColors = new Brush[4] { Brushes.Wheat, Brushes.LimeGreen, Brushes.LightGray, Brushes.Ivory };
- Brush result = Brushes.Transparent;
- Thread.Sleep(3);
- Random rnd = new Random();
- int random = rnd.Next(AcceptableColors.Length);
- result = AcceptableColors[random];
- return result;
- }//funkce nějakýho toníka ze stackoverflow, mrknout na credits "pickbrush()"
- public Border DrawSomething(Container CTD)
- {
- //CTD.cid = currentCID;
- //currentCID++;
- Border br1 = new Border();
- br1.BorderBrush = Brushes.Black;
- br1.BorderThickness = new Thickness(1);
- br1.Padding = new Thickness(1);
- Border br2 = new Border();
- br2.BorderBrush = PickRandomBrush();
- br2.BorderThickness = new Thickness(1);
- br2.Padding = new Thickness(1);
- BoardGrid congrid = new BoardGrid();
- congrid.RelatedContainer = CTD;
- congrid.Background = PickRandomBrush();
- congrid.HorizontalAlignment = HorizontalAlignment.Left;
- congrid.VerticalAlignment = VerticalAlignment.Center;
- congrid.HorizontalAlignment = HorizontalAlignment.Right;
- congrid.ShowGridLines = false;
- congrid.MouseLeftButtonDown += congrid_MouseLeftButtonDown;
- ColumnDefinition gridCol1 = new ColumnDefinition();
- gridCol1.Width = new GridLength();
- ColumnDefinition gridCol2 = new ColumnDefinition();
- gridCol2.Width = new GridLength();
- RowDefinition gridRow1 = new RowDefinition();
- gridRow1.Height = new GridLength();
- RowDefinition gridRow2 = new RowDefinition();
- gridRow2.Height = new GridLength();
- RowDefinition gridRow3 = new RowDefinition();
- gridRow3.Height = new GridLength();
- RowDefinition gridRow4 = new RowDefinition();
- gridRow4.Height = new GridLength();
- for (int z = 0; z < CTD.containers.Count; z++)
- {
- RowDefinition rowDef1 = new RowDefinition();
- rowDef1.Height = new GridLength();
- congrid.RowDefinitions.Add(rowDef1);
- }
- StackPanel mediastackpanel = new StackPanel();
- mediastackpanel.Orientation = Orientation.Horizontal;
- Grid.SetRow(mediastackpanel, 3);
- Grid.SetColumn(mediastackpanel, 1);
- foreach (MediaItem item in CTD.mediaItems)
- {
- ItemButton itembtn = new ItemButton();
- itembtn.RelatedMediaItem = item;
- itembtn.Content = item.name;
- itembtn.ToolTip = "Cesta: " + item.path + "\nTyp: " + item.type.ToString() + "\nTrvání: " + item.duration.ToString();
- itembtn.Click += itembtn_Click;
- mediastackpanel.Children.Add(itembtn);
- }
- int i = 0;
- foreach (Container container in CTD.containers)
- {
- Border brx = DrawSomething(container);
- int col = Convert.ToInt32(i % 2);
- Grid.SetColumn(brx, col);
- int row = Convert.ToInt32(4 + (Math.Truncate((double)i / 2)));
- Grid.SetRow(brx, row);
- congrid.Children.Add(brx);
- i++;
- }
- congrid.Children.Add(GenerateGridTextBlock("Jméno kontejneru", 14, new Thickness(2), 0, 0));
- congrid.Children.Add(GenerateGridTextBlock("Chování kontejneru", 14, new Thickness(2), 1, 0));
- congrid.Children.Add(GenerateGridTextBlock("Trvání kontejneru", 14, new Thickness(2), 2, 0));
- congrid.Children.Add(GenerateGridTextBlock("Mediální položky", 14, new Thickness(2), 3, 0));
- congrid.Children.Add(GenerateGridTextBlock(CTD.name, 14, new Thickness(2), 0, 1));
- string behaviour = string.Empty;
- try
- {
- behaviour = CTD.behaviour.ToString();
- }
- catch (Exception)
- {
- behaviour = Behaviour.sequence.ToString();
- }
- finally
- {
- congrid.Children.Add(GenerateGridTextBlock(behaviour, 14, new Thickness(2), 1, 1));
- }
- congrid.Children.Add(GenerateGridTextBlock(CTD.duration.ToString(), 14, new Thickness(2), 2, 1));
- congrid.Children.Add(mediastackpanel);
- congrid.ColumnDefinitions.Add(gridCol1);
- congrid.ColumnDefinitions.Add(gridCol2);
- congrid.RowDefinitions.Add(gridRow1);
- congrid.RowDefinitions.Add(gridRow2);
- congrid.RowDefinitions.Add(gridRow3);
- congrid.RowDefinitions.Add(gridRow4);
- br1.Child = congrid;
- br2.Child = br1;
- br2.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
- br2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
- return br2;
- } //vykreslovací srdce všeho... rekurzivně vykresluje kontejnery od storyboardu dolu...
- private void DeleteItem(MediaItem ItemToDelete) //smaže item s daným MID
- {
- try
- {
- Container Parentcont = searchContainerByMID(ItemToDelete.MID, Storyboard);
- try
- {
- Parentcont.mediaItems.Remove(ItemToDelete);
- Storygrid.Children.Clear();
- //(re)draw storyboard
- this.Storygrid.Children.Add(DrawSomething(Storyboard));
- }
- catch (NullReferenceException)
- {
- MessageBox.Show("Položku se nepodařilo smazat, možná už je smazaná...");
- }
- }
- catch (NullReferenceException)
- {
- }
- }
- private void DeleteContainer(int cid)//odmaže kontejner s daným CID
- {
- if (cid != 1)
- {
- Container contToDelete = searchContainerByID(cid, Storyboard, false);
- Container parentcont = searchContainerByID(cid, Storyboard, true);
- try
- {
- parentcont.containers.Remove(contToDelete);
- Storygrid.Children.Clear();
- //(re)draw storyboard
- this.Storygrid.Children.Add(DrawSomething(Storyboard));
- }
- catch (NullReferenceException)
- {
- MessageBox.Show("kontejner ke samazání se nepodařilo nalézt...");
- }
- }
- else
- {
- MessageBox.Show("Storyboard nelze smazat, vytvořte si nový projekt");
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement