public PageEditor(PageStorage newPage, bool multi, Int32Rect clip, int messageNum, string messageName, int pageNum, int totalPages)
        {
            Page = newPage;

            InitializeComponent();

            if (!multi) MessageTime.IsEnabled = false;
            Title = "MyFaPixel - Page Editor - " + messageName + " №" + messageNum + " - page №" + (pageNum + 1) + " (total " + totalPages + " pages)";

            SelectedMessagePreview = new PagePreview(Settings.Default.DisplayWidth, Settings.Default.DisplayHeight, clip);
            SelectedMessagePreview.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            PagePreviewScroll.Content = SelectedMessagePreview;

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFaPixel\Fonts";
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            DirectoryInfo tmpDI = new DirectoryInfo(path);
            FileInfo[] tmpFI = tmpDI.GetFiles("*.mfpf");

            foreach (FileInfo tmpFont in tmpFI)
            {
                ComboBoxItem tmpItem = new ComboBoxItem();
                tmpItem.Content = tmpFont.Name.Substring(0, tmpFont.Name.Length - 5);
                if (Page != null && Page.Strings[0].Font == (string)tmpItem.Content) MessageFont.SelectedItem = tmpItem.Content;
                MessageFont.Items.Add(tmpItem);
            }

            if (MessageFont.Items.Count == 0)
            {
                fontsFound = false;
                ComboBoxItem tmpItem = new ComboBoxItem();
                tmpItem.Content = "no fonts found";
                MessageFont.Items.Add(tmpItem);
            }
            else fontsFound = true;

            if (MessageFont.SelectedItem == null) MessageFont.SelectedIndex = 0;

            if (Page != null)
            {
                if (multi) MessageTime.Text = Page.Time.ToString();
                MessageText.Text = Page.Strings[0].Text;
                Indent = Page.Strings[0].Indent;
                Shift = Page.Strings[0].Shift;
                VShift = Page.Strings[0].VShift;
            }

            Initialized = true;

            MessageChanged();
        }
 private void SetStopPage(int message, int page, PageStorage storage)
 {
     CreateStopPage(message, page);
     Stops.Messages[message].Pages[page] = storage;
     UpdateStopPageList();
 }
 private void SetDestinationPage(int message, int page, PageStorage storage)
 {
     CreateDestinationPage(message, page);
     Destinations.Messages[message].Pages[page] = storage;
     UpdateDestinationPageList();
 }
 public PageStorage(PageStorage source)
 {
     Time = source.Time;
     Strings = new List<StringStorage>(source.Strings);
 }
 private void MessageChanged()
 {
     if (fontsFound && Initialized)
     {
         if (autoCenter) Shift = -1;
         SelectedMessagePreview.Clear(false);
         int tmp = 0;
         Page = new PageStorage(MessageTime.IsEnabled && int.TryParse(MessageTime.Text, out tmp) ? tmp : 0, 1);
         Page.Strings[0] = new StringStorage("Page", SelectedMessagePreview.Clip.X, SelectedMessagePreview.Clip.Y, SelectedMessagePreview.Clip.Width, SelectedMessagePreview.Clip.Height, MessageText.Text, (string)((ComboBoxItem)MessageFont.SelectedItem).Content, Shift, VShift, Indent, false);
         Page = SelectedMessagePreview.Render(Page);
         if (SelectedMessagePreview.Fits)
         {
             SaveButton.IsEnabled = true;
             SaveButton.Title = "save";
             SaveButton.Filter = Brushes.Green;
         }
         else
         {
             SaveButton.IsEnabled = false;
             SaveButton.Title = "TOO LONG TEXT";
             SaveButton.Filter = Brushes.Yellow;
         }
         Shift = Page.Strings[0].Shift;
     }
 }
        public PageStorage Render(PageStorage page)
        {
            if (page != null)
            {
                for (int i = 0; i < page.Strings.Count; ++i)
                {
                    StringStorage str = page.Strings[i];
                    String path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\MyFaPixel\Fonts\" + str.Font + @".mfpf";

                    if (File.Exists(path))
                    {
                        if (str.Font != FontName)
                        {
                            Stream stream = File.Open(path, FileMode.Open);
                            BinaryFormatter bformatter = new BinaryFormatter();
                            FontSymbols = (Collection<SymbolStorage>)bformatter.Deserialize(stream);
                            stream.Close();
                            FontName = str.Font;
                        }

                        byte[] symbols = System.Text.Encoding.GetEncoding(FontSymbols[0].Ascii).GetBytes(str.Text);
                        int margin = -1, tmpleft = (str.Shift > 0 ? str.Shift : 0) + str.X, left;

                        for (int j = 0; j < symbols.Length; ++j)
                        {
                            left = tmpleft;
                            margin = -1;
                            for (int x = 0; x < FontSymbols[symbols[j]].Width; ++x)
                            {
                                for (int y = 0; y < FontSymbols[symbols[j]].Height; ++y)
                                {
                                    if (FontSymbols[symbols[j]].Map[y][x])
                                    {
                                        if (margin < 0) margin = x;
                                        if (tmpleft < x - margin + left) tmpleft = left + x - margin;
                                        TurnOn(tmpleft, y + str.Y + str.VShift);
                                    }
                                }
                            }
                            tmpleft += (str.Indent + 1);
                        }

                        tmpleft -= (str.Indent + 1);
                        Fits = (tmpleft < Clip.X + Clip.Width);

                        if (str.Shift < 0)
                        {
                            if (tmpleft + 1 < str.X + str.Width)
                            {
                                page.Strings[i].Shift = (int)((double)(str.Width - (tmpleft - str.X)) / 2f);
                                for (int x = str.X + str.Width - 1; x >= str.X; --x)
                                {
                                    for (int y = str.Y; y < str.Y + str.Height; ++y)
                                    {
                                        if (States[x, y] == true)
                                        {
                                            TurnOff(x, y);
                                            TurnOn(x + page.Strings[i].Shift, y);
                                        }
                                    }
                                }
                            }
                            else page.Strings[i].Shift = 0;
                        }

                        Redraw();
                    }
                    else MessageBox.Show("FONT DOES NOT EXIST");
                }
                return page;
            }
            return null;
        }