//--------------------------------------------------------------------------- public void Create(string name, int width, int height, int tileWidth, int tileHeight, Map map = null) { MapWidth = width; MapHeight = height; PxTileWidth = tileWidth; PxTileHeight = tileHeight; m_Map = (map ?? new Map(name, width, height)); m_Control?.Reset(MapWidth, MapHeight); TilesetManager.Get().UpdateTileDimension(tileWidth, tileHeight); if (map != null) { Mouse.OverrideCursor = Cursors.Wait; foreach (Cell cell in m_Map.Cells) { foreach (KeyValuePair <ELayerMode, Layer> kvp in cell.Layers) { if (kvp.Value.TargetX >= 0 && kvp.Value.TargetY >= 0) { m_Control?.SetTile(kvp.Key, cell.X, cell.Y, kvp.Value.TargetX, kvp.Value.TargetY); } } m_Control?.SetBlocker(cell.X, cell.Y, cell.IsBlocked); } Mouse.OverrideCursor = null; } }
//--------------------------------------------------------------------------- public void ExecuteAction(int sourceX, int sourceY, bool isLeftMousePressed) { SelectionRect selection = TilesetManager.Get().GetSelection(); switch (EditManager.Get().Mode) { case EEditMode.Tiles: if (isLeftMousePressed) { SetTile(LayerManager.Get().Mode, sourceX, sourceY, selection.X, selection.Y, selection.Width, selection.Height); } break; case EEditMode.Eraser: if (isLeftMousePressed) { ClearTile(LayerManager.Get().Mode, sourceX, sourceY); } break; case EEditMode.Fill: if (isLeftMousePressed) { FillTile(LayerManager.Get().Mode, sourceX, sourceY, selection.X, selection.Y); } break; case EEditMode.Blocker: if (isLeftMousePressed) { SetBlocker(sourceX, sourceY, !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))); } break; } }
//--------------------------------------------------------------------------- public TileControl(int x, int y) { X = x; Y = y; Image = TilesetManager.Get().Tileset?.Source; View = new Rect(); }
//--------------------------------------------------------------------------- private void OnDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); TilesetManager.Get().CreateTileset(files[0]); } }
//--------------------------------------------------------------------------- public void Update() { TilesTranslate.X = 0; TilesTranslate.Y = 0; Tileset.Source = TilesetManager.Get().Tileset.Source; Tileset.Width = TilesetManager.Get().Tileset.PxWidth; Tileset.Height = TilesetManager.Get().Tileset.PxHeight; Bounds = TilesetManager.Get().Tileset.Bounds; }
//--------------------------------------------------------------------------- private void SetHighlight(Point point) { Tileset tileset = TilesetManager.Get().Tileset; if (tileset != null && tileset.PxTileWidth > 0 && tileset.PxTileHeight > 0) { int beginX = (int)(point.X / tileset.PxTileWidth); int beginY = (int)(point.Y / tileset.PxTileHeight); SetHighlight(beginX, beginY); } }
//--------------------------------------------------------------------------- private void SetSelection(Point start, Point end) { Tileset tileset = TilesetManager.Get().Tileset; if (tileset != null && tileset.PxTileWidth > 0 && tileset.PxTileHeight > 0) { int beginX = (int)Math.Max(0, (start.X / tileset.PxTileWidth)); int beginY = (int)Math.Max(0, (start.Y / tileset.PxTileHeight)); int endX = (int)Math.Max(0, (end.X / tileset.PxTileWidth)); int endY = (int)Math.Max(0, (end.Y / tileset.PxTileHeight)); SetSelection(Math.Min(beginX, endX), Math.Min(beginY, endY), Math.Max(1, Math.Abs(endX - beginX) + 1), Math.Max(1, Math.Abs(endY - beginY) + 1)); } }
//--------------------------------------------------------------------------- public TilesetCanvas() { InitializeComponent(); DataContext = this; TilesetManager.Get().RegisterCanvas(this); string path = Properties.Settings.Default.LastTilesetPath; if (File.Exists(path)) { TilesetManager.Get().CreateTileset(path); } }
//--------------------------------------------------------------------------- private void LoadTileset(string path) { try { BitmapImage bitmap = new BitmapImage(new Uri(path)); if (bitmap != null) { TilesetManager.Get().CreateTileset(path); } } catch (Exception e) { MessageBox.Show(e.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } }
//--------------------------------------------------------------------------- public void SetTile(int sourceX, int sourceY, int targetX, int targetY) { BitmapSource source = TilesetManager.Get().Tileset?.Source; if (source != null) { Int32Rect targetRect = new Int32Rect(targetX * 32, targetY * 32, 32, 32); int bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8; int stride = bytesPerPixel * targetRect.Width; byte[] data = new byte[stride * targetRect.Height]; TilesetManager.Get().Tileset.Source.CopyPixels(targetRect, data, stride, 0); Int32Rect sourceRect = new Int32Rect(sourceX * 32, sourceY * 32, 32, 32); Image.WritePixels(sourceRect, data, stride, 0); } }
//--------------------------------------------------------------------------- public MapControl() { InitializeComponent(); DataContext = this; m_Layers = new Dictionary <ELayerMode, MapLayerControl>(); foreach (ELayerMode mode in Enum.GetValues(typeof(ELayerMode))) { MapLayerControl control = new MapLayerControl(); control.IsHitTestVisible = false; m_Layers.Add(mode, control); LayerContainer.Children.Add(control); } MapManager.Get().Register(this); TilesetManager.Get().TilesetSelectionChanged += OnTilesetSelectionChanged; }
//--------------------------------------------------------------------------- private void UpdateHighlight(int x, int y) { SelectionRect tilesetSelection = TilesetManager.Get().GetSelection(); SelectionRect rect = new SelectionRect(x, y, tilesetSelection.Width, tilesetSelection.Height).Within(new SelectionRect(0, 0, MapWidth, MapHeight)); switch (EditManager.Get().Mode) { case EEditMode.Tiles: UpdateHighlight(rect.X, rect.Y, rect.Width, rect.Height); break; case EEditMode.Eraser: UpdateHighlight(rect.X, rect.Y, 1, 1); break; case EEditMode.Fill: UpdateHighlight(rect.X, rect.Y, 1, 1); break; } }
//--------------------------------------------------------------------------- private void OnTilesetSelectionChanged(SelectionRect selection) { if (selection != null && selection.Width > 0 && selection.Height > 0) { HighlightImage = new WriteableBitmap(selection.Width * 32, selection.Height * 32, 96, 96, PixelFormats.Bgra32, null); BitmapSource source = TilesetManager.Get().Tileset?.Source; if (source != null) { Int32Rect targetRect = new Int32Rect(selection.X * 32, selection.Y * 32, selection.Width * 32, selection.Height * 32); int bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8; int stride = bytesPerPixel * targetRect.Width; byte[] data = new byte[stride * targetRect.Height]; TilesetManager.Get().Tileset.Source.CopyPixels(targetRect, data, stride, 0); Int32Rect sourceRect = new Int32Rect(0, 0, selection.Width * 32, selection.Height * 32); HighlightImage.WritePixels(sourceRect, data, stride, 0); } } OnPropertyChanged("HighlightImage"); }
//--------------------------------------------------------------------------- private void SetHighlight(int x, int y) { if (Highlight == null) { Highlight = new SelectionRect(x, y, 1, 1, Bounds); } else { Highlight.Update(x, y, 1, 1, Bounds); } Tileset tileset = TilesetManager.Get().Tileset; if (tileset != null && tileset.PxTileWidth > 0 && tileset.PxTileHeight > 0) { Canvas.SetLeft(HighlightRect, Highlight.X * tileset.PxTileWidth); Canvas.SetTop(HighlightRect, Highlight.Y * tileset.PxTileHeight); HighlightRect.Width = tileset.PxTileWidth; HighlightRect.Height = tileset.PxTileHeight; } OnPropertyChanged("Highlight"); }
//--------------------------------------------------------------------------- private void SetSelection(int x, int y, int width, int height) { if (Selection == null) { Selection = new SelectionRect(x, y, width, height, Bounds); } else { Selection.Update(x, y, width, height, Bounds); } Tileset tileset = TilesetManager.Get().Tileset; if (tileset != null && tileset.PxTileWidth > 0 && tileset.PxTileHeight > 0) { Canvas.SetLeft(SelectionRect, Selection.X * tileset.PxTileWidth); Canvas.SetTop(SelectionRect, Selection.Y * tileset.PxTileHeight); SelectionRect.Width = Selection.Width * tileset.PxTileWidth; SelectionRect.Height = Selection.Height * tileset.PxTileHeight; } OnPropertyChanged("Selection"); TilesetManager.Get().UpdateSelection(); }
//--------------------------------------------------------------------------- private void SetHighlight(Point point, bool isHovering) { if (isHovering) { int beginX = (int)(point.X / m_TileWidth); int beginY = (int)(point.Y / m_TileHeight); switch (EditManager.Get().Mode) { case EEditMode.Tiles: SelectionRect selection = TilesetManager.Get().GetSelection(); SetHighlight(beginX, beginY, selection != null ? selection.Width : 1, selection != null ? selection.Height : 1, true); break; case EEditMode.Blocker: SetHighlight(beginX, beginY, 1, 1, true); break; } } else { SetHighlight(0, 0, 0, 0, false); } }
//--------------------------------------------------------------------------- public void SetTile(int x, int y) { View = TilesetManager.Get().Tileset.Crop(x, y); OnPropertyChanged("View"); }