Advertisement
thebys

DSDCG basics

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