Advertisement
thebys

DSDCG storyboard editing form

Jan 3rd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using DSDCG.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.IO;
  17. using System.Reflection;
  18. using System.Threading;
  19.  
  20.  
  21. namespace DSDCG
  22. {
  23.     /// <summary>
  24.     /// Interaction logic for MainWindow.xaml
  25.     /// </summary>
  26.     public partial class MainWindow : Window
  27.     {
  28.         public MediaItem _activeItem;
  29.         public Container _activeContainer;
  30.         public Container Storyboard;
  31.         public int currentCID = 1; //storyboard má 1, další kontejnery maj 2 a furt nahoru...
  32.         public int currentMID = 1; //není žádný rootitem takže první přidělujeme jedničku...
  33.         public MainWindow()
  34.         {
  35.             InitializeComponent();
  36.             Storyboard = new Container(currentCID, "Storyboard", new TimeSpan(0, 0, 0));
  37.             currentCID++;
  38.             _activeContainer = new Container(currentCID, "Kontejner 1", new TimeSpan(0, 0, 30));
  39.             currentCID++;
  40.             LoadActiveContainer(_activeContainer);
  41.         }
  42.  
  43.         #region obsluha formu
  44.         private void Button_Click_1(object sender, RoutedEventArgs e) //opensinglefiledialog
  45.         {
  46.             Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  47.             dlg.Filter = "Obrázky |*.jpg;*.jpeg;*.png;*.bmp;*.gif"; // Filter files by extension
  48.             Nullable<bool> result = dlg.ShowDialog();
  49.  
  50.             // Process open file dialog box results
  51.             if (result == true)
  52.             {
  53.                 // Open document
  54.                 _activeItem = new MediaItem(currentMID, dlg.SafeFileName, System.IO.Path.GetDirectoryName(dlg.FileName), MediaItemType.image, string.Empty, string.Empty, new TimeSpan(0, 0, 10));
  55.                 currentMID++;
  56.                 LoadActiveItem(_activeItem);
  57.             }
  58.         }
  59.         private void saveitembutton_Click(object sender, RoutedEventArgs e)
  60.         {
  61.             SaveActiveItem();
  62.         } //uloží změny z formu do activeitemu
  63.         private void discarditemchangesbutton_Click(object sender, RoutedEventArgs e)
  64.         {
  65.             LoadActiveItem(_activeItem);
  66.         } //odmaže změny - vrátí co je v _activeitemu
  67.         private void discardcontainerchanges_Click(object sender, RoutedEventArgs e)
  68.         {
  69.             LoadActiveContainer(_activeContainer);
  70.         } //odmaže změny - vrátí co je v activecontaineru
  71.         private void savecontainer_Click(object sender, RoutedEventArgs e)
  72.         {
  73.             SaveActiveContainer();
  74.         }
  75.         private void addtocontainer_Click(object sender, RoutedEventArgs e)
  76.         {
  77.             //changedvalues keep/discard dialog here...
  78.             _activeContainer.mediaItems.Add(new MediaItem(_activeItem.MID, _activeItem.name, _activeItem.path, _activeItem.type.Value, _activeItem.title, _activeItem.text, _activeItem.duration));
  79.         } //přidá do aktivního kontejneru aktivní media item...
  80.         private void CreateInActiveButton_Click(object sender, RoutedEventArgs e)
  81.         {
  82.             Container newcont = new Container();
  83.             newcont.name = this.containername.Text;
  84.             newcont.cid = currentCID;
  85.             currentCID++;
  86.             if (newcont.behaviour == null || newcont.behaviour.Count < 1)
  87.             {
  88.                 if (this.containerbehaviour.SelectedItem != null)
  89.                 {
  90.                     newcont.behaviour.Add((Behaviour)this.containerbehaviour.SelectedItem);
  91.                 }
  92.                 else
  93.                 {
  94.                     //default behaviour setting?
  95.                 }
  96.             }
  97.             else
  98.             {
  99.                 newcont.behaviour[0] = (Behaviour)this.containerbehaviour.SelectedItem;
  100.             }
  101.  
  102.             try
  103.             {
  104.                 newcont.duration = TimeSpan.Parse(this.containerduration.Text);
  105.                 _activeContainer.containers.Add(newcont);
  106.             }
  107.             catch
  108.             {
  109.                 MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
  110.                 LoadActiveContainer(newcont);
  111.             }
  112.  
  113.         } //vytvoří z dat ve formu kontejner do aktivního kontejneru
  114.         private void DeleteActiveContainer_Click(object sender, RoutedEventArgs e)
  115.         {
  116.             DeleteContainer(_activeContainer.cid);
  117.         } //odmaže aktivní kontejner a všechny subkontejnery
  118.         private void newcontainerbutton_Click(object sender, RoutedEventArgs e)
  119.         {
  120.             _activeContainer = new Container(currentCID, "Nový kontejner", new TimeSpan(0, 0, 30));
  121.             currentCID++;
  122.             LoadActiveContainer(_activeContainer);
  123.         } //přidání nového kontejneru... prakticky asi useless...
  124.         private void AddContToSTB_Click(object sender, RoutedEventArgs e)
  125.         {
  126.             MessageBoxResult result = MessageBox.Show("Uložit změny?", "Přejete si uložit změny nebo ponechat kontejner tak jak je?", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
  127.  
  128.             // Process message box results
  129.             switch (result)
  130.             {
  131.                 case MessageBoxResult.Yes:
  132.                     SaveActiveContainer();
  133.                     Storyboard.containers.Add(_activeContainer);
  134.                     break;
  135.                 case MessageBoxResult.No:
  136.                     Storyboard.containers.Add(_activeContainer); //ano i ne dělá to stejný, akorát "ano" ještě uloží poslední úpravy z formuláře, škoda, že nemám databinding...
  137.                     break;
  138.                 case MessageBoxResult.Cancel:
  139.                     // User pressed Cancel button
  140.                     // ...
  141.                     break;
  142.             }
  143.             //dialog asking about changes...
  144.  
  145.         } //tlačítko na přídání aktivního kontejneru do storyboardu? To jako vážně?
  146.  
  147.         private void redraw_Click(object sender, RoutedEventArgs e)
  148.         {
  149.             Storygrid.Children.Clear();
  150.             //(re)draw storyboard
  151.             this.Storygrid.Children.Add(DrawSomething(Storyboard));
  152.         } //znovuvykreslovací tlačítko boardu.
  153.         void itembtn_Click(object sender, RoutedEventArgs e)
  154.         {
  155.             ItemButton clickeditem = sender as ItemButton;
  156.             _activeItem = clickeditem.RelatedMediaItem;
  157.             LoadActiveItem(_activeItem);
  158.         } //co se stane když někdo klikne na mediaitembutton
  159.         void congrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  160.         {
  161.             e.Handled = true;
  162.             BoardGrid contgrid = new BoardGrid();
  163.             contgrid = sender as BoardGrid;
  164.             contgrid.RelatedContainer.contbrush = contgrid.Background;
  165.             _activeContainer = contgrid.RelatedContainer;
  166.             LoadActiveContainer(contgrid.RelatedContainer);
  167.         } //selektor kontejnerů na kliknutí...
  168.  
  169.         private void exit_Click(object sender, RoutedEventArgs e)
  170.         {
  171.             Application.Current.Shutdown();
  172.         } //exitovací tlačítko :)
  173.         #endregion
  174.  
  175.         #region methods
  176.         private void LoadActiveItem(MediaItem ItemToBeLoaded) //načte _activeitem do formu...
  177.         {
  178.             this.filename.Text = ItemToBeLoaded.name;
  179.             this.filepath.Text = ItemToBeLoaded.path;
  180.             this.filetype.SelectedItem = ItemToBeLoaded.type;
  181.             try
  182.             {
  183.                 this.title.Text = _activeItem.title;
  184.             }
  185.             catch (Exception)
  186.             {
  187.                 this.title.Text = string.Empty;
  188.             }
  189.             try
  190.             {
  191.                 this.text.Text = _activeItem.text;
  192.             }
  193.             catch (Exception)
  194.             {
  195.                 this.text.Text = string.Empty;
  196.             }
  197.             try
  198.             {
  199.                 this.duration.Text = _activeItem.duration.ToString();
  200.             }
  201.             catch (Exception)
  202.             {
  203.                 this.duration.Text = new TimeSpan(0, 13, 37).ToString();
  204.             }
  205.         }
  206.         private void SaveActiveItem() //savne data z formu do _activeitem
  207.         {
  208.             _activeItem.name = this.filename.Text;
  209.             _activeItem.path = this.filepath.Text;
  210.             _activeItem.type = (MediaItemType)this.filetype.SelectedItem;
  211.             _activeItem.title = this.title.Text;
  212.             _activeItem.text = this.text.Text;
  213.             try
  214.             {
  215.                 _activeItem.duration = TimeSpan.Parse(this.duration.Text);
  216.             }
  217.             catch
  218.             {
  219.                 MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
  220.                 LoadActiveItem(_activeItem);
  221.             }
  222.         }
  223.         private void LoadActiveContainer(Container ContainerToBeLoaded) //načte _activecontainer do formu
  224.         {
  225.             this.containername.Text = ContainerToBeLoaded.name;
  226.             this.ActiveContainerNameLabel.Content = ContainerToBeLoaded.name;
  227.             try
  228.             {
  229.                 this.containerbehaviour.SelectedItem = ContainerToBeLoaded.behaviour[0];
  230.             }
  231.             catch
  232.             {
  233.             }
  234.             if (ContainerToBeLoaded.contbrush != null)
  235.             {
  236.                 ActiveContainerBorder.BorderBrush = ContainerToBeLoaded.contbrush;
  237.             }
  238.             else
  239.             {
  240.                 ActiveContainerBorder.BorderBrush = Brushes.Transparent;
  241.             }
  242.             if (_activeContainer.duration != null)
  243.             {
  244.                 this.containerduration.Text = _activeContainer.duration.ToString();
  245.             }
  246.             if (_activeContainer.duration == null)
  247.             {
  248.                 this.containerduration.Text = new TimeSpan(0, 0, 20).ToString();
  249.             }
  250.         }
  251.         private void SaveActiveContainer() //načte data z formu do _activecontainer
  252.         {
  253.             _activeContainer.name = this.containername.Text;
  254.             if (_activeContainer.behaviour == null || _activeContainer.behaviour.Count < 1)
  255.             {
  256.                 if (this.containerbehaviour.SelectedItem != null)
  257.                 {
  258.                     _activeContainer.behaviour.Add((Behaviour)this.containerbehaviour.SelectedItem);
  259.                 }
  260.                 else
  261.                 {
  262.                     //default behaviour setting?
  263.                 }
  264.             }
  265.             else
  266.             {
  267.                 _activeContainer.behaviour[0] = (Behaviour)this.containerbehaviour.SelectedItem;
  268.             }
  269.  
  270.             try
  271.             {
  272.                 _activeContainer.duration = TimeSpan.Parse(this.containerduration.Text);
  273.             }
  274.             catch
  275.             {
  276.                 MessageBox.Show("Čas se nepodařilo rozparsovat, uveďte jej prosím ve správném formátu");
  277.                 LoadActiveContainer(_activeContainer);
  278.             }
  279.         }
  280.         private void UpdateContainer(int cid, Container newcont) //první část rekurzivního procházení kontejnerů...
  281.         {
  282.             if (cid == 1)
  283.             {
  284.                 Storyboard = newcont;
  285.             }
  286.             else
  287.             {
  288.                 Container ContainerToUpdate = new Container();
  289.                 ContainerToUpdate = searchContainerByID(cid, Storyboard, false);
  290.                 if (ContainerToUpdate != null || ContainerToUpdate.cid != 0)
  291.                 {
  292.                     ContainerToUpdate = newcont;
  293.                 }
  294.             }
  295.  
  296.  
  297.         }
  298.         private Container searchContainerByID(int cid, Container startcont, bool returnParent) //rekurzivní procházecí funkce kontejnerů, používám ji abych mohl editovat storyboard
  299.         {
  300.             //Storyboard.containers.Find(delegate(Container c) { return c.cid == cid; }); alternativní vyhledávací metoda
  301.             if (!returnParent)
  302.             {
  303.                 if (startcont.cid == cid)
  304.                 {
  305.                     return startcont;
  306.                 }
  307.                 else
  308.                 {
  309.                     foreach (Container cont in startcont.containers)
  310.                     {
  311.                         return (searchContainerByID(cid, cont, returnParent));
  312.                     }
  313.                     return null;
  314.                 }
  315.             }
  316.             else
  317.             {
  318.                 foreach (Container cont in startcont.containers)
  319.                 {
  320.  
  321.                     if (cont.cid == cid)
  322.                     {
  323.                         return startcont;
  324.                     }
  325.                     else
  326.                     {
  327.                         return (searchContainerByID(cid, cont, true));
  328.                     }
  329.                 }
  330.                 return null;
  331.             }
  332.  
  333.  
  334.         }
  335.         public TextBlock GenerateGridTextBlock(string text, int fontsize, Thickness paddingThickness, int row, int col)
  336.         {
  337.             TextBlock newblock = new TextBlock();
  338.             newblock.Text = text;
  339.             newblock.FontSize = fontsize;
  340.             newblock.Padding = paddingThickness;
  341.             Grid.SetRow(newblock, row);
  342.             Grid.SetColumn(newblock, col);
  343.             return newblock;
  344.         } //šikovná věcička, ušetřila mi spoustu řádků textblocků
  345.         private Brush PickRandomBrush()
  346.         {
  347.             Brush result = Brushes.Transparent;
  348.             Thread.Sleep(3);
  349.             Random rnd = new Random();
  350.  
  351.             Type brushesType = typeof(Brushes);
  352.  
  353.             PropertyInfo[] properties = brushesType.GetProperties();
  354.  
  355.             int random = rnd.Next(properties.Length);
  356.             result = (Brush)properties[random].GetValue(null, null);
  357.  
  358.             return result;
  359.         }//funkce nějakýho toníka ze stackoverflow, mrknout na credits "pickbrush()"
  360.         public Border DrawSomething(Container CTD)
  361.         {
  362.             CTD.cid = currentCID;
  363.             currentCID++;
  364.  
  365.             Border br1 = new Border();
  366.             br1.BorderBrush = Brushes.Black;
  367.             br1.BorderThickness = new Thickness(1);
  368.             br1.Padding = new Thickness(1);
  369.  
  370.             Border br2 = new Border();
  371.             br2.BorderBrush = PickRandomBrush();
  372.             br2.BorderThickness = new Thickness(1);
  373.             br2.Padding = new Thickness(1);
  374.  
  375.             BoardGrid congrid = new BoardGrid();
  376.             congrid.RelatedContainer = CTD;
  377.             congrid.Background = PickRandomBrush();
  378.             congrid.HorizontalAlignment = HorizontalAlignment.Left;
  379.             congrid.VerticalAlignment = VerticalAlignment.Center;
  380.             congrid.HorizontalAlignment = HorizontalAlignment.Right;
  381.             congrid.ShowGridLines = false;
  382.             congrid.MouseLeftButtonDown += congrid_MouseLeftButtonDown;
  383.  
  384.             ColumnDefinition gridCol1 = new ColumnDefinition();
  385.             gridCol1.Width = new GridLength();
  386.             ColumnDefinition gridCol2 = new ColumnDefinition();
  387.             gridCol2.Width = new GridLength();
  388.             RowDefinition gridRow1 = new RowDefinition();
  389.             gridRow1.Height = new GridLength();
  390.             RowDefinition gridRow2 = new RowDefinition();
  391.             gridRow2.Height = new GridLength();
  392.             RowDefinition gridRow3 = new RowDefinition();
  393.             gridRow3.Height = new GridLength();
  394.  
  395.             RowDefinition gridRow4 = new RowDefinition();
  396.             gridRow4.Height = new GridLength();
  397.  
  398.             for (int z = 0; z < CTD.containers.Count; z++)
  399.             {
  400.                 RowDefinition rowDef1 = new RowDefinition();
  401.                 rowDef1.Height = new GridLength();
  402.                 congrid.RowDefinitions.Add(rowDef1);
  403.             }
  404.  
  405.  
  406.             StackPanel mediastackpanel = new StackPanel();
  407.             mediastackpanel.Orientation = Orientation.Horizontal;
  408.             Grid.SetRow(mediastackpanel, 3);
  409.             Grid.SetColumn(mediastackpanel, 1);
  410.             foreach (MediaItem item in CTD.mediaItems)
  411.             {
  412.                 ItemButton itembtn = new ItemButton();
  413.                 itembtn.RelatedMediaItem = item;
  414.                 itembtn.Content = item.name;
  415.                 itembtn.ToolTip = "Cesta: " + item.path + "\nTyp: " + item.type.ToString() + "\nTrvání: " + item.duration.ToString();
  416.                 itembtn.Click += itembtn_Click;
  417.                 mediastackpanel.Children.Add(itembtn);
  418.             }
  419.  
  420.  
  421.             int i = 0;
  422.             foreach (Container container in CTD.containers)
  423.             {
  424.                 Border brx = DrawSomething(container);
  425.                 int col = Convert.ToInt32(i % 2);
  426.                 Grid.SetColumn(brx, col);
  427.                 int row = Convert.ToInt32(4 + (Math.Truncate((double)i / 2)));
  428.                 Grid.SetRow(brx, row);
  429.                 congrid.Children.Add(brx);
  430.                 i++;
  431.             }
  432.  
  433.  
  434.             congrid.Children.Add(GenerateGridTextBlock("Jméno kontejneru", 14, new Thickness(2), 0, 0));
  435.             congrid.Children.Add(GenerateGridTextBlock("Chování kontejneru", 14, new Thickness(2), 1, 0));
  436.             congrid.Children.Add(GenerateGridTextBlock("Trvání kontejneru", 14, new Thickness(2), 2, 0));
  437.             congrid.Children.Add(GenerateGridTextBlock("Mediální položky", 14, new Thickness(2), 3, 0));
  438.             congrid.Children.Add(GenerateGridTextBlock(CTD.name, 14, new Thickness(2), 0, 1));
  439.             string behaviour = string.Empty;
  440.             try
  441.             {
  442.                 behaviour = CTD.behaviour[0].ToString();
  443.             }
  444.             catch (Exception)
  445.             {
  446.                 behaviour = Behaviour.sequence.ToString();
  447.             }
  448.             finally
  449.             {
  450.                 congrid.Children.Add(GenerateGridTextBlock(behaviour, 14, new Thickness(2), 1, 1));
  451.             }
  452.             congrid.Children.Add(GenerateGridTextBlock(CTD.duration.ToString(), 14, new Thickness(2), 2, 1));
  453.             congrid.Children.Add(mediastackpanel);
  454.             congrid.ColumnDefinitions.Add(gridCol1);
  455.             congrid.ColumnDefinitions.Add(gridCol2);
  456.             congrid.RowDefinitions.Add(gridRow1);
  457.             congrid.RowDefinitions.Add(gridRow2);
  458.             congrid.RowDefinitions.Add(gridRow3);
  459.             congrid.RowDefinitions.Add(gridRow4);
  460.             br1.Child = congrid;
  461.             br2.Child = br1;
  462.             br2.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
  463.             br2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
  464.  
  465.             return br2;
  466.         } //vykreslovací srdce všeho... rekurzivně vykresluje kontejnery od storyboardu dolu...
  467.         private void DeleteContainer(int cid)
  468.         {
  469.             Container contToDelete = searchContainerByID(cid, Storyboard, false);
  470.             Container parentcont = searchContainerByID(cid, Storyboard, true);
  471.             parentcont.containers.Remove(contToDelete);
  472.         } //odmaže kontejner s daným CID
  473.         #endregion
  474.     }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement