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;
- namespace DSDCG
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MediaItem _activeItem;
- public Container _activeContainer;
- public Container Storyboard;
- 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);
- }
- #region obsluha formu
- private void Button_Click_1(object sender, RoutedEventArgs e) //opensinglefiledialog
- {
- Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
- dlg.Filter = "Obrázky |*.jpg;*.jpeg;*.png;*.bmp;*.gif"; // Filter files by extension
- Nullable<bool> result = dlg.ShowDialog();
- // Process open file dialog box results
- if (result == true)
- {
- // Open document
- _activeItem = new MediaItem(currentMID, dlg.SafeFileName, System.IO.Path.GetDirectoryName(dlg.FileName), MediaItemType.image, string.Empty, string.Empty, new TimeSpan(0, 0, 10));
- currentMID++;
- LoadActiveItem(_activeItem);
- }
- }
- private void saveitembutton_Click(object sender, RoutedEventArgs e)
- {
- SaveActiveItem();
- } //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 savecontainer_Click(object sender, RoutedEventArgs e)
- {
- SaveActiveContainer();
- }
- 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));
- } //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++;
- if (newcont.behaviour == null || newcont.behaviour.Count < 1)
- {
- if (this.containerbehaviour.SelectedItem != null)
- {
- newcont.behaviour.Add((Behaviour)this.containerbehaviour.SelectedItem);
- }
- else
- {
- //default behaviour setting?
- }
- }
- else
- {
- newcont.behaviour[0] = (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);
- }
- } //vytvoří z dat ve formu kontejner do aktivního kontejneru
- private void DeleteActiveContainer_Click(object sender, RoutedEventArgs e)
- {
- DeleteContainer(_activeContainer.cid);
- } //odmaže aktivní kontejner a všechny subkontejnery
- private void newcontainerbutton_Click(object sender, RoutedEventArgs e)
- {
- _activeContainer = new Container(currentCID, "Nový kontejner", new TimeSpan(0, 0, 30));
- currentCID++;
- LoadActiveContainer(_activeContainer);
- } //přidání nového kontejneru... prakticky asi useless...
- private void AddContToSTB_Click(object sender, RoutedEventArgs e)
- {
- 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);
- // Process message box results
- switch (result)
- {
- case MessageBoxResult.Yes:
- SaveActiveContainer();
- Storyboard.containers.Add(_activeContainer);
- break;
- case MessageBoxResult.No:
- 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...
- break;
- case MessageBoxResult.Cancel:
- // User pressed Cancel button
- // ...
- break;
- }
- //dialog asking about changes...
- } //tlačítko na přídání aktivního kontejneru do storyboardu? To jako vážně?
- private void redraw_Click(object sender, RoutedEventArgs e)
- {
- Storygrid.Children.Clear();
- //(re)draw storyboard
- this.Storygrid.Children.Add(DrawSomething(Storyboard));
- } //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
- private void LoadActiveItem(MediaItem ItemToBeLoaded) //načte _activeitem do formu...
- {
- this.filename.Text = ItemToBeLoaded.name;
- this.filepath.Text = 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[0];
- }
- 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;
- if (_activeContainer.behaviour == null || _activeContainer.behaviour.Count < 1)
- {
- if (this.containerbehaviour.SelectedItem != null)
- {
- _activeContainer.behaviour.Add((Behaviour)this.containerbehaviour.SelectedItem);
- }
- else
- {
- //default behaviour setting?
- }
- }
- else
- {
- _activeContainer.behaviour[0] = (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;
- }
- }
- 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 result = Brushes.Transparent;
- Thread.Sleep(3);
- Random rnd = new Random();
- Type brushesType = typeof(Brushes);
- PropertyInfo[] properties = brushesType.GetProperties();
- int random = rnd.Next(properties.Length);
- result = (Brush)properties[random].GetValue(null, null);
- 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[0].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 DeleteContainer(int cid)
- {
- Container contToDelete = searchContainerByID(cid, Storyboard, false);
- Container parentcont = searchContainerByID(cid, Storyboard, true);
- parentcont.containers.Remove(contToDelete);
- } //odmaže kontejner s daným CID
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement