Example #1
0
        public MapcacheDialog(string text)
            : base("Mapcache", "cache.png", SizeToContent.Manual, ResizeMode.CanResize)
        {
            InitializeComponent();

            ShowInTaskbar         = true;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Owner = WpfUtilities.TopWindow;

            _cache = new Mapcache();
            _cache.Commands.ModifiedStateChanged += _commands_ModifiedStateChanged;
            _asyncOperation = new AsyncOperation(_progressBar);

            ListViewDataTemplateHelper.GenerateListViewTemplateNew(_listView, new ListViewDataTemplateHelper.GeneralColumnInfo[] {
                new ListViewDataTemplateHelper.GeneralColumnInfo {
                    Header = "Name", DisplayExpression = "MapName", SearchGetAccessor = "MapName", ToolTipBinding = "MapName", TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center, FixedWidth = 140
                },
                new ListViewDataTemplateHelper.GeneralColumnInfo {
                    Header = "Width", DisplayExpression = "Xs", SearchGetAccessor = "Xs", ToolTipBinding = "Xs", TextAlignment = TextAlignment.Center, FixedWidth = 80
                },
                new ListViewDataTemplateHelper.GeneralColumnInfo {
                    Header = "Height", DisplayExpression = "Ys", SearchGetAccessor = "Ys", ToolTipBinding = "Ys", TextAlignment = TextAlignment.Center, FixedWidth = 80
                },
                new ListViewDataTemplateHelper.GeneralColumnInfo {
                    Header = "Size", DisplayExpression = "DisplaySize", SearchGetAccessor = "Len", ToolTipBinding = "DisplaySize", TextAlignment = TextAlignment.Center, FixedWidth = 150
                },
            }, new DefaultListViewComparer <MapInfo>(), new string[] { "Added", "{DynamicResource CellBrushAdded}" });

            _tmbUndo.SetUndo(_cache.Commands);
            _tmbRedo.SetRedo(_cache.Commands);
            _commands_ModifiedStateChanged(null, null);
            _listView.ItemsSource = _cache.ViewMaps;

            ApplicationShortcut.Link(ApplicationShortcut.Save, () => _menuItemSave_Click(null, null), this);
            ApplicationShortcut.Link(ApplicationShortcut.Undo, () => _cache.Commands.Undo(), this);
            ApplicationShortcut.Link(ApplicationShortcut.Redo, () => _cache.Commands.Redo(), this);
            ApplicationShortcut.Link(ApplicationShortcut.New, () => _menuItemNew_Click(null, null), this);
            ApplicationShortcut.Link(ApplicationShortcut.Open, () => _menuItemOpenFrom_Click(null, null), this);
            ApplicationShortcut.Link(ApplicationShortcut.Delete, () => _miDelete_Click(null, null), this);

            _recentFilesManager              = new WpfRecentFiles(Configuration.ConfigAsker, 6, _menuItemRecentFiles, "Mapcache");
            _recentFilesManager.FileClicked += _recentFilesManager_FileClicked;

            if (text != null)
            {
                _recentFilesManager.AddRecentFile(text);
                _load(text);
            }
            else
            {
                // Open the latest file
                if (_recentFilesManager.Files.Count > 0 && File.Exists(_recentFilesManager.Files[0]))
                {
                    _load(_recentFilesManager.Files[0]);
                }
            }
        }
Example #2
0
        private void _menuItemMerge_Click(object sender, RoutedEventArgs e)
        {
            try {
                if (!_validateState())
                {
                    return;
                }

                string file = PathRequest.OpenFileMapcache("filter", "Grf and Dat Files (*.grf, *.dat)|*.grf;*.dat");

                if (file != null)
                {
                    if (file.IsExtension(".grf"))
                    {
                        _asyncOperation.SetAndRunOperation(new GrfThread(delegate {
                            AProgress.Init(_cache);

                            try {
                                _cache.Commands.Begin();

                                List <FileEntry> files = new List <FileEntry>();
                                int count = 0;

                                using (GrfHolder grf = new GrfHolder(file, GrfLoadOptions.Normal)) {
                                    files.AddRange(grf.FileTable.EntriesInDirectory("data\\", SearchOption.TopDirectoryOnly).Where(entry => entry.RelativePath.IsExtension(".gat")));

                                    foreach (var entry in files)
                                    {
                                        count++;
                                        AProgress.IsCancelling(_cache);
                                        FileEntry rswEntry = grf.FileTable.TryGet(entry.RelativePath.ReplaceExtension(".rsw"));

                                        if (rswEntry == null)
                                        {
                                            continue;
                                        }

                                        _cache.Progress = (float)count / files.Count * 100f;
                                        _cache.Commands.AddMap(Path.GetFileNameWithoutExtension(entry.DisplayRelativePath), entry.GetDecompressedData(), rswEntry.GetDecompressedData());
                                    }
                                }
                            }
                            catch (OperationCanceledException) {
                                _cache.IsCancelled = true;
                                _cache.Commands.CancelEdit();
                            }
                            catch (Exception err) {
                                _cache.Commands.CancelEdit();
                                ErrorHandler.HandleException(err);
                            }
                            finally {
                                _cache.Commands.End();
                                AProgress.Finalize(_cache);
                            }
                        }, _cache, 200));
                    }
                    else if (file.IsExtension(".dat"))
                    {
                        Mapcache cache = new Mapcache(file);

                        try {
                            _cache.Commands.Begin();

                            foreach (var map in cache.Maps)
                            {
                                _cache.Commands.AddMapRaw(map.MapName, map);
                            }
                        }
                        catch {
                            _cache.Commands.CancelEdit();
                            throw;
                        }
                        finally {
                            _cache.Commands.End();
                        }
                    }
                    else
                    {
                        throw new Exception("Unreognized file format.");
                    }
                }
            }
            catch (Exception err) {
                ErrorHandler.HandleException(err);
            }
        }