Beispiel #1
0
 public bool GetForegroundAt(int X, int Y, out BlockName Foreground)
 {
     Foreground = BlockName.NONE;
     if (HasForegroundAt(X, Y))
     {
         Foreground = SelectableTile.GetBlockNameByString(TileDB.Tiles[X, Y].blName);
         return(true);
     }
     return(false);
 }
Beispiel #2
0
 public bool GetBackgroundAt(int X, int Y, out BackgroundName Background)
 {
     Background = BackgroundName.NONE;
     if (HasBackgroundAt(X, Y))
     {
         Background = SelectableTile.GetBackgroundNameByString(TileDB.Tiles[X, Y].bgName);
         return(true);
     }
     return(false);
 }
Beispiel #3
0
        private void TileSelect_OnClick(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(TileCanvas);
            int   X = (int)Math.Floor(p.X / 32);
            int   Y = (int)Math.Floor(p.Y / 32);

            index = GetIndexFromPosition(X, Y);

            try
            {
                Image image = new Image()
                {
                    Height = 32,
                    Width  = 32,

                    Source = selectableTiles[index].source
                };

                TileType       tt = TileType.None;
                BlockName      blockName;
                BackgroundName backgroundName;
                switch (ComboTypes.SelectedIndex)
                {
                case 0:
                    backgroundName    = SelectableTile.GetBackgroundNameByString(selectableTiles[index].bgName);
                    tt                = TileType.Background;
                    TileHover.Content = selectableTiles[index].bgName;
                    break;

                case 1:
                    blockName         = SelectableTile.GetBlockNameByString(selectableTiles[index].blName);
                    tt                = TileType.Foreground;
                    TileHover.Content = selectableTiles[index].blName;
                    break;
                }

                TileCanvas.Children.Remove(selectBorder);

                selectBorder = new Border()
                {
                    BorderBrush     = Brushes.SkyBlue,
                    BorderThickness = new Thickness(2),
                    Width           = 32,
                    Height          = 32
                };
                Canvas.SetTop(selectBorder, Y * 32);
                Canvas.SetLeft(selectBorder, X * 32);

                TileCanvas.Children.Add(selectBorder);
                FirstSelected   = true;
                LabelImg.Source = image.Source;
                _selectedTile   = new Tile(tt, image);
            } catch (IndexOutOfRangeException index) { }
        }
Beispiel #4
0
        public void LoadMap(TileType tt)
        {
            var executingAssembly = Assembly.GetExecutingAssembly();

            string folderName;

            if (tt == TileType.Background && backgroundMap.Count <= 0)
            {
                folderName = string.Format("{0}.Resources.Tiles.Backgrounds", executingAssembly.GetName().Name);
            }
            else if (tt == TileType.Foreground && blockMap.Count <= 0)
            {
                folderName = string.Format("{0}.Resources.Tiles.Blocks", executingAssembly.GetName().Name);
            }
            else
            {
                return;
            }

            string[] files = executingAssembly.GetManifestResourceNames().Where(r => r.StartsWith(folderName) && r.EndsWith(".png")).ToArray();

            for (int count = 0; count < files.Length; count++)
            {
                using (Stream file = executingAssembly.GetManifestResourceStream(files[count].ToString()))
                {
                    string fileName = files[count].ToString().Substring(folderName.Length + 1);
                    var    src      = new BitmapImage();
                    src.BeginInit();
                    src.CacheOption  = BitmapCacheOption.OnLoad;
                    src.StreamSource = file;
                    src.EndInit();

                    if (tt == TileType.Background)
                    {
                        backgroundMap.Add(SelectableTile.GetBackgroundNameByString(fileName), src);
                    }
                    else if (tt == TileType.Foreground)
                    {
                        blockMap.Add(SelectableTile.GetBlockNameByString(fileName), src);
                    }
                }
            }
        }
Beispiel #5
0
        public void MakeBlockSpriteSheet()
        {
            int x     = 0;
            int y     = 0;
            int count = 0;

            foreach (KeyValuePair <BlockName, BitmapImage> entry in blockMap)
            {
                selectableTiles[count] = new SelectableTile(TileType.Foreground, entry.Key.ToString(), entry.Value);
                Image image = new Image
                {
                    Width  = 32,
                    Height = 32
                };
                image.Source = selectableTiles[count].source;

                if (count == 0)
                {
                }
                else if (count % columns == 0)
                {
                    y += 32;
                    x  = 0;
                }
                else
                {
                    x += 32;
                }

                Canvas.SetTop(image, y);
                Canvas.SetLeft(image, x);
                TileCanvas.Children.Add(image);
                TileCanvas.MinWidth  = columns * 32;
                TileCanvas.MinHeight = count / columns * 32;

                count++;
            }
        }
Beispiel #6
0
        //World Load
        private void LoadWorld_Click(object sender, RoutedEventArgs e)
        {
            if (firstPlaced && MessageBox.Show("Are you sure to load a new world? You may lose all your unsaved progress!", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
            {
                return;
            }

            isLoading = true;

            OpenFileDialog dialog = new OpenFileDialog()
            {
                Filter           = "Pixel Worlds World (*.pww)|*.pww",
                DefaultExt       = "pww",
                RestoreDirectory = true
            };
            Nullable <bool> Selected = dialog.ShowDialog();
            string          path     = dialog.FileName;

            if (Selected == true)
            {
                MainCanvas.Children.Clear();
                TileDB = (TileData)DataHandler.LoadWorld(path);
                DrawGrid(TileDB.Height, TileDB.Width);

                if (!TileDB.hasMainBackground)
                {
                    MainCanvas.Background = new SolidColorBrush(Utils.IntToARGBColor(TileDB.ARGBBackgroundColor));
                }
                else
                {
                    MainCanvas.Background = BackgroundData.GetBackground(TileDB.MainBackground);
                }

                SortedList <string, int> invalids = new SortedList <string, int>();
                for (int i = 0; i < TileDB.Tiles.GetLength(0); i++)
                {
                    for (int j = 0; j < TileDB.Tiles.GetLength(1); j++)
                    {
                        if (TileDB.Tiles[i, j] != null)
                        {
                            if (TileDB.Tiles[i, j].Type == TileType.Background || TileDB.Tiles[i, j].Type == TileType.Both)
                            {
                                Image image = new Image()
                                {
                                    Height = 32,
                                    Width  = 32
                                };
                                string         dataName = TileDB.Tiles[i, j].bgName;
                                BackgroundName name     = SelectableTile.GetBackgroundNameByString(TileDB.Tiles[i, j].bgName);

                                //If the sprite does not exist.
                                bool exists = backgroundMap.TryGetValue(name, out BitmapImage src);
                                if (!exists && dataName != null)
                                {
                                    if (invalids.ContainsKey(dataName))
                                    {
                                        invalids[dataName]++;
                                    }
                                    else
                                    {
                                        invalids.Add(dataName, 0);
                                    }
                                    TileDB.Tiles[i, j] = null;
                                }
                                else
                                {
                                    image.Source = src;

                                    Canvas.SetTop(image, TileDB.Tiles[i, j].Positions.BackgroundY.Value * 32);
                                    Canvas.SetLeft(image, TileDB.Tiles[i, j].Positions.BackgroundX.Value * 32);
                                    image.SetValue(Canvas.ZIndexProperty, 10);
                                    MainCanvas.Children.Add(image);
                                    TileDB.Tiles[i, j].Background = image;
                                }
                            }

                            if (TileDB.Tiles[i, j].Type == TileType.Foreground || TileDB.Tiles[i, j].Type == TileType.Both)
                            {
                                Image image = new Image()
                                {
                                    Height = 32,
                                    Width  = 32
                                };
                                string    dataName = TileDB.Tiles[i, j].blName;
                                BlockName name     = SelectableTile.GetBlockNameByString(TileDB.Tiles[i, j].blName);

                                bool exists = blockMap.TryGetValue(name, out BitmapImage src);
                                if (!exists && dataName != null)
                                {
                                    if (invalids.ContainsKey(dataName))
                                    {
                                        invalids[dataName]++;
                                    }
                                    else
                                    {
                                        invalids.Add(dataName, 0);
                                    }
                                    TileDB.Tiles[i, j] = null;
                                }
                                else
                                {
                                    image.Source = src;

                                    Canvas.SetTop(image, TileDB.Tiles[i, j].Positions.ForegroundY.Value * 32);
                                    Canvas.SetLeft(image, TileDB.Tiles[i, j].Positions.ForegroundX.Value * 32);
                                    image.SetValue(Canvas.ZIndexProperty, 20);
                                    MainCanvas.Children.Add(image);
                                    TileDB.Tiles[i, j].Foreground = image;
                                }
                            }
                        }
                    }
                    ColorSelector.SelectedColor = Utils.IntToARGBColor(TileDB.ARGBBackgroundColor);
                    firstPlaced          = false;
                    SaveButton.IsEnabled = false;
                    SavedPath            = String.Empty;
                }
                if (invalids.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"Could not load {invalids.Count} tiles (Using an older version?)");
                    foreach (KeyValuePair <string, int> entry in invalids)
                    {
                        sb.AppendLine($"-{entry.Key} [x{entry.Value}]");
                    }
                    MessageBox.Show(sb.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            isLoading = false;
        }