internal void LoadDefaults()
        {
            var spr = FindResource("PathPoint") as BitmapImage;

            WindowAPI.RegisterTexture(renderer, "roaam_path", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out int texture);
            LoadedImages["roaam_path"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr), new Point(12, 12));

            spr = FindResource("EmptyImage") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_empty", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_empty"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr), new Point(9, 9));

            spr = FindResource("WhitePixel") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_zone", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_zone"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr));

            spr = FindResource("TargetSprite") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_target", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_target"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr));

            spr = FindResource("Arrows") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_arrows", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_arrows"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr), new Point(56, 56));

            spr = FindResource("Square") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_square", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_square"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr), new Point(18, 18));

            spr = FindResource("Respawn") as BitmapImage;
            WindowAPI.RegisterTexture(renderer, "roaam_respawn", System.AppDomain.CurrentDomain.BaseDirectory + spr.UriSource.LocalPath, 1, out texture);
            LoadedImages["roaam_respawn"] = new TexData(true, texture, WindowAPI.BitmapImageToBitmap(spr), new Point(16, 32));
        }
Exemple #2
0
        public void Update()
        {
            if (Tileset?.Tileset != SelectedTileset)
            {
                if (SelectedTileset == null)
                {
                    Tileset = null;
                }
                else
                {
                    Tileset = new TilesetViewModel(SelectedTileset);
                    Tileset.PropertyChangedValue += Tileset_PropertyChangedValue;
                }
            }
            else if (SelectedTileset != null)
            {
                Tileset.TileWidth  = SelectedTileset.TileWidth;
                Tileset.TileHeight = SelectedTileset.TileHeight;
                Tileset.SpritePath = SelectedTileset.SpritePath;
            }
            if (!string.IsNullOrEmpty(SelectedTileset?.SpritePath))
            {
                TexData spr = viewer.LoadImage(Tileset.SpritePath);
                widthBox.Maximum  = spr.image.Width;
                heightBox.Maximum = spr.image.Height;
                int tileCount = (spr.image.Width / Tileset.TileWidth) * (spr.image.Height / Tileset.TileHeight);
                SelectedTile = SelectedTile >= tileCount ? tileCount - 1 : SelectedTile;
            }

            tilesetPicker.Items.Clear();
            foreach (var tileset in ApplicationSettings.Instance.ActiveProject.Tilesets)
            {
                tilesetPicker.Items.Add(tileset.Value);
            }
            tilesetPicker.Items.Add(null);
            tilesetPicker.Items.Add(new TilesetDummy {
                Name = "New Tileset..."
            });

            if (SelectedTileset != null)
            {
                tilesetPicker.SelectedValuePath = "Name";
                tilesetPicker.SelectedValue     = SelectedTileset.Name;
            }
            else
            {
                tilesetPicker.SelectedIndex = 0;
            }

            spriteButton.Content = Tileset?.SpritePath ?? "Select Sprite";
        }
Exemple #3
0
 private void OnSpriteNameChanged(object sender, RoutedEventArgs e)
 {
     if (SelectedTileset != null)
     {
         var tileset = SelectedTileset;
         var window  = new SpriteSelectionWindow();
         window.Sprite  = SelectedTileset.SpritePath;
         window.Closed += (s, args) =>
         {
             if (tileset != null && tileset.SpritePath != window.Sprite)
             {
                 var op = new ModifyTilesetOperation(ApplicationSettings.Instance.ActiveProject, tileset, nameof(Tileset.SpritePath), SelectedTileset.SpritePath, window.Sprite);
                 ApplicationSettings.Instance.ActiveProject.ExecuteOp(op);
                 TexData spr = viewer.LoadImage(Tileset.SpritePath);
                 SelectedTileset.TileWidth  = Clamp(SelectedTileset.TileWidth, 1, spr.image.Width);
                 SelectedTileset.TileHeight = Clamp(SelectedTileset.TileHeight, 1, spr.image.Height);
                 Update();
             }
         };
         window.Owner = App.Current.MainWindow;
         window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
         window.Show();
     }
 }
        unsafe void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            RenderingEventArgs args = (RenderingEventArgs)e;

            // It's possible for Rendering to call back twice in the same frame
            // so only render when we haven't already rendered in this frame.
            if (_lastRender != args.RenderingTime)
            {
                if (!LoadedImages.ContainsKey("roaam_path"))
                {
                    LoadDefaults();
                }

                foreach (Article art in articles)
                {
                    string spriteName = art.Sprite;
                    // Check if the sprite is loaded
                    if (!LoadedImages.ContainsKey(spriteName))
                    {
                        // If spritename is set, attempt to load sprite
                        bool hasSprite = false;
                        if (!string.IsNullOrEmpty(spriteName))
                        {
                            hasSprite = WindowAPI.LoadImage(spriteName, renderer, out TexData data);
                            LoadedImages.Add(spriteName, data);
                        }

                        // If no sprite is found, use EmptyImage for this article
                        if (!hasSprite)
                        {
                            BitmapImage overrideSpr = FindResource("EmptyImage") as BitmapImage;
                            WindowAPI.RegisterTexture(renderer, spriteName, AppDomain.CurrentDomain.BaseDirectory + overrideSpr.UriSource.LocalPath, 1, out int texture);
                            LoadedImages[spriteName] = new TexData(false, texture, null);
                        }
                    }

                    // Get texture data for this article
                    TexData sprite             = LoadedImages[spriteName];
                    Point   offset             = art.RealPoint - new Vector(sprite.offset.X, sprite.offset.Y);
                    Point   scale              = new Point(1, 1);
                    System.Drawing.Color color = System.Drawing.Color.White;

                    switch (art.ArticleNum)
                    {
                    case ArticleType.Terrain:
                        scale = new Point(2, 2);
                        break;

                    case ArticleType.Zone:
                        if (art is Zone zone)
                        {
                            scale = new Point(zone.TriggerWidth, zone.TriggerHeight);
                            // Set the color based on event type
                            switch (zone.EventID)
                            {
                            //blastzone
                            case 4:
                                color = System.Drawing.Color.Red;
                                break;
                            }
                            // Make semi-transparent
                            color = System.Drawing.Color.FromArgb(64, color.R, color.G, color.B);

                            // Show transform handles
                            if (SelectedObj == art)
                            {
                                // Push a box at each corner of the zone for scale handles
                                TexData spr = LoadedImages["roaam_square"];
                                foreach (Point point in ROAAM_CONST.ZONE_POINTS)
                                {
                                    PushArticle(new DX_Article(
                                                    spr.texture,
                                                    zone.RealPoint + new Vector(point.X * zone.TriggerWidth, point.Y * zone.TriggerHeight) - (Vector)spr.offset / zoomLevel,
                                                    new Point(1 / zoomLevel, 1 / zoomLevel),
                                                    -15));
                                }
                            }
                        }
                        break;

                    case ArticleType.Target:
                        if (SelectedObj == art && (art as Target).Path.Any())
                        {
                            Vector prev = (Vector)(art as Target).Path.Last();
                            for (int i = 0; i < (art as Target).Path.Count; i++)
                            {
                                Vector  point   = (Vector)(art as Target).Path[i];
                                TexData targSpr = LoadedImages["roaam_path"];

                                // Push path point sprite to renderer
                                PushArticle(new DX_Article(
                                                targSpr.texture,
                                                (Point)((point * ROAAM_CONST.GRID_SIZE) - (Vector)targSpr.offset),
                                                new Point(1, 1),
                                                -10));
                                // Push semi-transparent target sprite to renderer
                                PushArticle(new DX_Article(
                                                sprite.texture,
                                                (Point)((point * ROAAM_CONST.GRID_SIZE) - (Vector)sprite.offset),
                                                scale,
                                                art.Depth,
                                                unchecked ((int)0x40FFFFFF)));

                                // Push transform handles if this path point is selected
                                if (SelectedPath == i)
                                {
                                    TexData arrowSpr = LoadedImages["roaam_arrows"];
                                    PushArticle(new DX_Article(
                                                    arrowSpr.texture,
                                                    (Point)((point * ROAAM_CONST.GRID_SIZE) - ((Vector)arrowSpr.offset / zoomLevel)),
                                                    new Point(1 / zoomLevel, 1 / zoomLevel),
                                                    -15));
                                }

                                // Push path line to renderer, and highlight orange if selected in editor panel
                                IntPtr pathColor = WhiteBrush;
                                float  width     = 2;
                                if (i == ObjViewer.Instance.HighlightedPath)
                                {
                                    pathColor = OrangeBrush;
                                    width     = 4;
                                }
                                PushLine(new DX_Line((Point)(prev * ROAAM_CONST.GRID_SIZE),
                                                     (Point)(point * ROAAM_CONST.GRID_SIZE),
                                                     width,
                                                     art.Depth - 0.1f,
                                                     pathColor));
                                // Store this point for line drawing
                                prev = point;
                            }
                        }
                        break;

                    case ArticleType.Tilemap:
                        if (art is Tilemap tilemap)
                        {
                            Tileset tileset = tilemap.Tileset;
                            if (tileset == null || string.IsNullOrEmpty(tileset.SpritePath))
                            {
                                continue;
                            }
                            // Check if the sprite is loaded
                            if (!LoadedImages.ContainsKey(tileset.SpritePath))
                            {
                                // If spritename is set, attempt to load sprite
                                bool hasSprite = false;
                                if (!string.IsNullOrEmpty(tileset.SpritePath))
                                {
                                    hasSprite = WindowAPI.LoadImage(tileset.SpritePath, renderer, out TexData data);
                                    LoadedImages.Add(tileset.SpritePath, data);
                                }

                                // If no sprite is found, use EmptyImage for this article
                                if (!hasSprite)
                                {
                                    BitmapImage overrideSpr = FindResource("EmptyImage") as BitmapImage;
                                    WindowAPI.RegisterTexture(renderer, tileset.SpritePath, AppDomain.CurrentDomain.BaseDirectory + overrideSpr.UriSource.LocalPath, 1, out int texture);
                                    LoadedImages[tileset.SpritePath] = new TexData(false, texture, null);
                                }
                            }

                            TexData tex = LoadedImages[tileset.SpritePath];
                            foreach (var tilegrid in tilemap.Tilegrid)
                            {
                                Point pos = new Point(tileset.TileWidth * 2 * TilegridArray.ChunkSizeX * tilegrid.Key.Item1 + tilemap.RealPoint.X,
                                                      tileset.TileHeight * 2 * TilegridArray.ChunkSizeY * tilegrid.Key.Item2 + tilemap.RealPoint.Y);
                                PushTilemap(new DX_Tilemap(tex.texture, pos, tileset.TileWidth, tileset.TileHeight, tilegrid.Value, tilemap.Depth, new Point(2, 2)));
                            }
                        }
                        break;
                    }

                    if (art.ArticleNum != ArticleType.Tilemap)
                    {
                        var c = color.ToArgb();
                        // Push the article sprite to the renderer
                        PushArticle(new DX_Article(
                                        sprite.texture,
                                        new Point(offset.X, offset.Y),
                                        scale,
                                        art.Depth,
                                        SelectedObj == art ? System.Drawing.Color.Orange.ToArgb() : color.ToArgb()));
                    }

                    if (SelectedObj == art && SelectedPath == -1 && Overlay.Visibility != Visibility.Visible)
                    {
                        TexData arrowSpr = LoadedImages["roaam_arrows"];
                        if (art is Zone zone)
                        {
                            offset = new Point(offset.X + zone.TriggerWidth / 2, offset.Y + zone.TriggerHeight / 2);
                        }

                        // Push the transform handles to the renderer if selected
                        PushArticle(new DX_Article(
                                        arrowSpr.texture,
                                        offset - ((Vector)arrowSpr.offset / zoomLevel),
                                        new Point(1 / zoomLevel, 1 / zoomLevel),
                                        -15));
                    }
                }

                if (ApplicationSettings.Instance.ActiveProject != null)
                {
                    TexData respawnSpr = LoadedImages["roaam_respawn"];
                    PushArticle(new DX_Article(
                                    respawnSpr.texture,
                                    ApplicationSettings.Instance.ActiveProject.RespawnPoint - (Vector)respawnSpr.offset,
                                    new Point(1, 1),
                                    5));
                }

                WindowAPI.PrepareForRender(renderer);
                WindowAPI.SetCameraTransform(renderer, new Point(RenderOffset.X, RenderOffset.Y), zoomLevel);
                WindowAPI.Render(renderer, articles_internal, articles_count_internal, lines_internal, lines_count_internal, tilemap_internal, tilemap_count_internal);

                ClearArticles();
                ClearLines();
                ClearTilemaps();

                _lastRender = args.RenderingTime;
            }
        }
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (!IsVisible)
            {
                return;
            }
            if (RoomEditor.Instance.SelectedObj is Tilemap tilemap)
            {
                double x, y, xStart, yStart;
                x = xStart = -RoomEditor.Instance.RenderOffset.X;
                y = yStart = -RoomEditor.Instance.RenderOffset.Y;
                double actualWidth    = RoomEditor.Instance.ActualWidth / RoomEditor.Instance.zoomLevel;
                double actualHeight   = RoomEditor.Instance.ActualHeight / RoomEditor.Instance.zoomLevel;
                double xIncrement     = tilemap.Tileset.TileWidth * 2;
                double yIncrement     = tilemap.Tileset.TileHeight * 2;
                double tilemapXOffset = Repeat(tilemap.RealPoint.X, xIncrement);
                double tilemapYOffset = Repeat(tilemap.RealPoint.Y, yIncrement);
                double xOffset        = tilemapXOffset - Repeat(xStart, xIncrement);
                double yOffset        = tilemapYOffset - Repeat(yStart, xIncrement);

                // Don't draw grid if we're too zoomed out
                if (tilemap.Tileset.TileWidth * RoomEditor.Instance.zoomLevel > 2)
                {
                    while ((x - xStart) + xOffset <= actualWidth)
                    {
                        RoomEditor.Instance.PushLine(new DX_Line(
                                                         new Point(x + xOffset, yStart),
                                                         new Point(x + xOffset, yStart + actualHeight),
                                                         1.5f / RoomEditor.Instance.zoomLevel,
                                                         tilemap.Depth,
                                                         RoomEditor.Instance.GrayBrush));
                        x += xIncrement;
                    }
                    while ((y - yStart) + yOffset <= actualHeight)
                    {
                        RoomEditor.Instance.PushLine(new DX_Line(
                                                         new Point(xStart, y + yOffset),
                                                         new Point(xStart + actualWidth, y + yOffset),
                                                         1.5f / RoomEditor.Instance.zoomLevel,
                                                         tilemap.Depth,
                                                         RoomEditor.Instance.GrayBrush));
                        y += yIncrement;
                    }
                }

                if (string.IsNullOrEmpty(tilemap.Tileset.SpritePath) || !RoomEditor.Instance.LoadedImages.ContainsKey(tilemap.Tileset.SpritePath))
                {
                    return;
                }
                TexData spr        = RoomEditor.Instance.LoadedImages[tilemap.Tileset.SpritePath];
                int     tile       = TilesetEditor.Instance.SelectedTile;
                int     tileSpan   = spr.image.Width / tilemap.Tileset.TileWidth;
                int     tileHeight = spr.image.Height / tilemap.Tileset.TileHeight;
                int     tileX      = tile % tileSpan;
                int     tileY      = tile / tileSpan;

                var   transform    = RoomEditor.Instance.GetTransform();
                Point hoveredPoint = transform.Transform(Mouse.GetPosition(RoomEditor.Instance));
                hoveredPoint  = new Point(Math.Floor((hoveredPoint.X - tilemapXOffset) / xIncrement) * xIncrement + tilemapXOffset, Math.Floor((hoveredPoint.Y - tilemapYOffset) / yIncrement) * yIncrement + tilemapYOffset);
                hoveredPoint -= new Vector(tileX * tilemap.Tileset.TileWidth * 2, tileY * tilemap.Tileset.TileHeight * 2);
                RoomEditor.Instance.PushArticle(new DX_Article(
                                                    spr.texture,
                                                    hoveredPoint,
                                                    new Point(2, 2),
                                                    tilemap.Depth - 0.1f,
                                                    unchecked ((int)0xAAFFFFFF))
                {
                    CropStart = new Point(tileX / (float)tileSpan, tileY / (float)tileHeight),
                    CropEnd   = new Point((tileX + 1) / (float)tileSpan, (tileY + 1) / (float)tileHeight)
                });
            }
            else
            {
                Visibility = Visibility.Collapsed;
            }
        }
        public static bool LoadImage(string name, IntPtr renderer, out TexData texData)
        {
            Point           offset   = new Point();
            string          loadFile = Path.Combine(Path.GetDirectoryName(ApplicationSettings.Instance.ActiveProject.ProjectPath), "scripts", "load.gml");
            MatchCollection matches  = null;

            if (File.Exists(loadFile))
            {
                string lines = File.ReadAllText(loadFile);
                matches = Regex.Matches(lines, "sprite_change_offset\\s*\\(\\s*\"([\\w\\d]+)\",\\s*(\\d+),\\s*(\\d+)\\s*\\)");
            }
            string directory;

            if (ApplicationSettings.Instance.ActiveProject.Type == ProjectType.AdventureMode)
            {
                directory = Path.Combine(Path.GetDirectoryName(ApplicationSettings.Instance.ActiveProject.ProjectPath), "sprites", "articles");
            }
            else
            {
                directory = Path.Combine(Path.GetDirectoryName(ApplicationSettings.Instance.ActiveProject.ProjectPath), "sprites");
            }
            if (File.Exists(Path.Combine(directory, name + ".png")))
            {
                string path = Path.Combine(directory, name + ".png");
                System.Drawing.Bitmap img = null;
                using (FileStream file = new FileStream(path, FileMode.Open))
                {
                    MemoryStream stream = new MemoryStream();
                    file.CopyTo(stream);
                    img = new System.Drawing.Bitmap(stream);
                }
                WindowAPI.RegisterTexture(renderer, name, path, 1, out int texture);
                if (matches != null)
                {
                    Match match = matches.OfType <Match>().FirstOrDefault(m => m.Groups[1].Value == name);
                    if (match != null)
                    {
                        offset.X = Double.Parse(match.Groups[2].Value);
                        offset.Y = Double.Parse(match.Groups[3].Value);
                    }
                }
                texData = new TexData(true, texture, img, offset);
                return(true);
            }

            var files = Directory.EnumerateFiles(directory, name + "*.png");

            if (files.Any())
            {
                string file = files.FirstOrDefault(f => Regex.Match(f, name + "_strip(\\d+)").Success);
                if (!string.IsNullOrEmpty(file))
                {
                    Match match = Regex.Match(file, "strip(\\d+)");
                    int   count = int.Parse(match.Groups[1].Value);
                    System.Drawing.Bitmap img = null;
                    using (FileStream fstream = new FileStream(file, FileMode.Open))
                    {
                        MemoryStream stream = new MemoryStream();
                        fstream.CopyTo(stream);
                        img = new System.Drawing.Bitmap(stream);
                    }
                    WindowAPI.RegisterTexture(renderer, name, file, count, out int texture);
                    int index = file.IndexOf("_strip");
                    if (matches != null)
                    {
                        Match offsetMatch = matches.OfType <Match>().FirstOrDefault(m => m.Groups[1].Value == file.Substring(0, index));
                        if (offsetMatch != null)
                        {
                            offset.X = Double.Parse(offsetMatch.Groups[2].Value);
                            offset.Y = Double.Parse(offsetMatch.Groups[3].Value);
                        }
                    }
                    texData = new TexData(true, texture, img, offset);
                    return(true);
                }
            }
            texData = new TexData(false, 0);
            return(false);
        }