Advertisement
thebys

DSDCG 04012013

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