public void Initialize(ScreenLayer initialLayer = null) { ResourceContainer = new AssemblyResourceContainer(Assembly.GetEntryAssembly(), $"{nameof(StorybrewEditor)}.Resources", "resources"); DrawState.Initialize(ResourceContainer, Window.Width, Window.Height); drawContext = new DrawContext(); drawContext.Register(this, false); drawContext.Register <TextureContainer>(new TextureContainerAtlas(ResourceContainer), true); drawContext.Register <QuadRenderer>(new QuadRendererBuffered(), true); drawContext.Register <LineRenderer>(new LineRendererBuffered(), true); try { var brewLibAssembly = Assembly.GetAssembly(typeof(Drawable)); Skin = new Skin(drawContext.Get <TextureContainer>()) { ResolveDrawableType = (drawableTypeName) => brewLibAssembly.GetType($"{nameof(BrewLib)}.{nameof(BrewLib.Graphics)}.{nameof(BrewLib.Graphics.Drawables)}.{drawableTypeName}", true, true), ResolveWidgetType = (widgetTypeName) => Type.GetType($"{nameof(StorybrewEditor)}.{nameof(UserInterface)}.{widgetTypeName}", false, true) ?? brewLibAssembly.GetType($"{nameof(BrewLib)}.{nameof(UserInterface)}.{widgetTypeName}", true, true), ResolveStyleType = (styleTypeName) => Type.GetType($"{nameof(StorybrewEditor)}.{nameof(UserInterface)}.{nameof(UserInterface.Skinning)}.{nameof(UserInterface.Skinning.Styles)}.{styleTypeName}", false, true) ?? brewLibAssembly.GetType($"{nameof(BrewLib)}.{nameof(UserInterface)}.{nameof(UserInterface.Skinning)}.{nameof(UserInterface.Skinning.Styles)}.{styleTypeName}", true, true), }; Skin.Load("skin.json", ResourceContainer); } catch (Exception e) { Trace.WriteLine($"Failed to load skin: {e}"); Skin = new Skin(drawContext.Get <TextureContainer>()); } var inputDispatcher = new InputDispatcher(); InputManager = new InputManager(Window, inputDispatcher); ScreenLayerManager = new ScreenLayerManager(Window, clock, this); inputDispatcher.Add(createOverlay(ScreenLayerManager)); inputDispatcher.Add(ScreenLayerManager.InputHandler); Restart(initialLayer); Window.Resize += window_Resize; Window.Closing += window_Closing; resizeToWindow(); }
public string GetStats() { var spriteRenderer = drawContext.Get <SpriteRenderer>(); return(string.Format("Sprite - t:{0}k f:{1:0.0}k b:{2} w:{3} lb:{4}", spriteRenderer.RenderedSpriteCount / 1000, spriteRenderer.FlushedBufferCount / 1000f, spriteRenderer.DiscardedBufferCount, spriteRenderer.BufferWaitCount, spriteRenderer.LargestBatch)); }
public void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity) { validate(); var inverseScaling = 1 / scaling; var color = Color.WithOpacity(opacity); var renderer = DrawState.Prepare(drawContext.Get <SpriteRenderer>(), camera, RenderStates); var clipRegion = DrawState.GetClipRegion(camera) ?? new Box2(camera.ExtendedViewport.Left, camera.ExtendedViewport.Top, camera.ExtendedViewport.Right, camera.ExtendedViewport.Bottom); foreach (var layoutGlyph in textLayout.VisibleGlyphs) { var glyph = layoutGlyph.Glyph; var position = layoutGlyph.Position; var y = bounds.Top + position.Y * inverseScaling; var height = glyph.Height * inverseScaling; if (y > clipRegion.Bottom)// || y - height > bounds.Bottom) { break; } if (y + height < clipRegion.Top)// || y < bounds.Top) { continue; } var x = bounds.Left + position.X * inverseScaling; var width = glyph.Width * inverseScaling; renderer.Draw(glyph.Texture, x, y, 0, 0, inverseScaling, inverseScaling, 0, color); } }
public void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity = 1) { storyboard.DisplayTime = Time; if (Clip) { using (DrawState.Clip(bounds, camera)) storyboard.Draw(drawContext, camera, bounds, opacity); } else { storyboard.Draw(drawContext, camera, bounds, opacity); DrawState.Prepare(drawContext.Get <LineRenderer>(), camera, linesRenderStates) .DrawSquare(new Vector3(bounds.Left, bounds.Top, 0), new Vector3(bounds.Right, bounds.Bottom, 0), Color4.Black); } }
public void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity) { if (!Visible) { return; } if (Highlight || effect.Highlight) { opacity *= (float)((Math.Sin(drawContext.Get <Editor>().TimeSource.Current * 4) + 1) * 0.5); } foreach (var displayableObject in displayableObjects) { displayableObject.Draw(drawContext, camera, bounds, opacity, effect.Project); } }
public void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity) { if (Texture == null) { return; } var renderer = DrawState.Prepare(drawContext.Get <SpriteRenderer>(), camera, RenderStates); var color = Color.WithOpacity(opacity); float textureX0 = 0; float textureY0 = 0; float textureX1 = Texture.Width; float textureY1 = Texture.Height; var scaleH = bounds.Width / Texture.Width; var scaleV = bounds.Height / Texture.Height; float scale; switch (ScaleMode) { case ScaleMode.Fill: if (scaleH > scaleV) { scale = scaleH; textureY0 = (Texture.Height - bounds.Height / scale) * 0.5f; textureY1 = Texture.Height - textureY0; } else { scale = scaleV; textureX0 = (Texture.Width - bounds.Width / scale) * 0.5f; textureX1 = Texture.Width - textureX0; } break; case ScaleMode.Fit: case ScaleMode.RepeatFit: scale = Math.Min(scaleH, scaleV); break; default: scale = 1f; break; } switch (ScaleMode) { case ScaleMode.Repeat: case ScaleMode.RepeatFit: for (var y = bounds.Top; y < bounds.Bottom; y += Texture.Height * scale) { for (var x = bounds.Left; x < bounds.Right; x += Texture.Width * scale) { var textureX = Math.Min((bounds.Right - x) / scale, Texture.Width); var textureY = Math.Min((bounds.Bottom - y) / scale, Texture.Height); renderer.Draw(Texture, x, y, 0, 0, scale, scale, 0, color, 0, 0, textureX, textureY); } } break; default: renderer.Draw(Texture, (bounds.Left + bounds.Right) * 0.5f, (bounds.Top + bounds.Bottom) * 0.5f, (textureX1 - textureX0) * 0.5f, (textureY1 - textureY0) * 0.5f, scale, scale, Rotation, color, textureX0, textureY0, textureX1, textureY1); break; } }
public static void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity, Project project, FrameStats frameStats, OsbSprite sprite) { var time = project.DisplayTime * 1000; if (sprite.TexturePath == null || !sprite.IsActive(time)) { return; } if (frameStats != null) { frameStats.SpriteCount++; frameStats.CommandCount += sprite.CommandCost; frameStats.IncompatibleCommands |= sprite.HasIncompatibleCommands; frameStats.OverlappedCommands |= sprite.HasOverlappedCommands; } var fade = sprite.OpacityAt(time); if (fade < 0.00001f) { return; } var scale = (Vector2)sprite.ScaleAt(time); if (scale.X == 0 || scale.Y == 0) { return; } if (sprite.FlipHAt(time)) { scale.X = -scale.X; } if (sprite.FlipVAt(time)) { scale.Y = -scale.Y; } Texture2dRegion texture = null; var fullPath = Path.Combine(project.MapsetPath, sprite.GetTexturePathAt(time)); try { texture = project.TextureContainer.Get(fullPath); if (texture == null) { fullPath = Path.Combine(project.ProjectAssetFolderPath, sprite.GetTexturePathAt(time)); texture = project.TextureContainer.Get(fullPath); } } catch (IOException) { // Happens when another process is writing to the file, will try again later. return; } if (texture == null) { return; } var position = sprite.PositionAt(time); var rotation = sprite.RotationAt(time); var color = sprite.ColorAt(time); var finalColor = ((Color4)color) .LerpColor(Color4.Black, project.DimFactor) .WithOpacity(opacity * fade); var additive = sprite.AdditiveAt(time); var origin = GetOriginVector(sprite.Origin, texture.Width, texture.Height); if (frameStats != null) { var size = texture.Size * scale; var spriteObb = new OrientedBoundingBox(position, origin * scale, size.X, size.Y, rotation); if (spriteObb.Intersects(OsuHitObject.WidescreenStoryboardBounds)) { frameStats.EffectiveCommandCount += sprite.CommandCost; // Approximate how much of the sprite is on screen var spriteAabb = spriteObb.GetAABB(); var intersection = spriteAabb.IntersectWith(OsuHitObject.WidescreenStoryboardBounds); var aabbIntersectionFactor = (intersection.Width * intersection.Height) / (spriteAabb.Width * spriteAabb.Height); var intersectionArea = size.X * size.Y * aabbIntersectionFactor; frameStats.ScreenFill += Math.Min(OsuHitObject.WidescreenStoryboardArea, intersectionArea) / OsuHitObject.WidescreenStoryboardArea; } } var boundsScaling = bounds.Height / 480; DrawState.Prepare(drawContext.Get <QuadRenderer>(), camera, additive ? AdditiveStates : AlphaBlendStates) .Draw(texture, bounds.Left + bounds.Width * 0.5f + (position.X - 320) * boundsScaling, bounds.Top + position.Y * boundsScaling, origin.X, origin.Y, scale.X * boundsScaling, scale.Y * boundsScaling, rotation, finalColor); }
public static void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity, Project project, OsbSprite sprite) { var time = project.DisplayTime * 1000; if (sprite.TexturePath == null || !sprite.IsActive(time)) { return; } var fade = sprite.OpacityAt(time); if (fade < 0.00001f) { return; } var scale = (Vector2)sprite.ScaleAt(time); if (scale.X == 0 || scale.Y == 0) { return; } if (sprite.FlipHAt(time)) { scale.X = -scale.X; } if (sprite.FlipVAt(time)) { scale.Y = -scale.Y; } var fullPath = Path.Combine(project.MapsetPath, sprite.GetTexturePathAt(time)); Texture2d texture = null; try { texture = project.TextureContainer.Get(fullPath); } catch (IOException) { // Happens when another process is writing to the file, will try again later. return; } if (texture == null) { return; } var position = sprite.PositionAt(time); var rotation = sprite.RotationAt(time); var color = sprite.ColorAt(time); var finalColor = ((Color4)color).WithOpacity(opacity * fade); var additive = sprite.AdditiveAt(time); Vector2 origin; switch (sprite.Origin) { default: case OsbOrigin.TopLeft: origin = new Vector2(0, 0); break; case OsbOrigin.TopCentre: origin = new Vector2(texture.Width * 0.5f, 0); break; case OsbOrigin.TopRight: origin = new Vector2(texture.Width, 0); break; case OsbOrigin.CentreLeft: origin = new Vector2(0, texture.Height * 0.5f); break; case OsbOrigin.Centre: origin = new Vector2(texture.Width * 0.5f, texture.Height * 0.5f); break; case OsbOrigin.CentreRight: origin = new Vector2(texture.Width, texture.Height * 0.5f); break; case OsbOrigin.BottomLeft: origin = new Vector2(0, texture.Height); break; case OsbOrigin.BottomCentre: origin = new Vector2(texture.Width * 0.5f, texture.Height); break; case OsbOrigin.BottomRight: origin = new Vector2(texture.Width, texture.Height); break; } var boundsScaling = bounds.Height / 480; DrawState.Prepare(drawContext.Get <SpriteRenderer>(), camera, additive ? AdditiveStates : AlphaBlendStates) .Draw(texture, bounds.Left + bounds.Width * 0.5f + (position.X - 320) * boundsScaling, bounds.Top + position.Y * boundsScaling, origin.X, origin.Y, scale.X * boundsScaling, scale.Y * boundsScaling, rotation, finalColor); }
public void Draw(DrawContext drawContext, Camera camera, Box2 bounds, float opacity) { if (Texture == null) { return; } var width = bounds.Width + Outset.Horizontal; var height = bounds.Height + Outset.Vertical; var x0 = bounds.Left - Outset.Left; var y0 = bounds.Top - Outset.Top; var x1 = x0 + Borders.Left; var y1 = y0 + Borders.Top; var x2 = bounds.Right + Outset.Right - (Texture.Width - Borders.Right); var y2 = bounds.Bottom + Outset.Bottom - (Texture.Height - Borders.Bottom); var horizontalScale = (x2 - x1) / (Borders.Right - Borders.Left); var verticalScale = (y2 - y1) / (Borders.Bottom - Borders.Top); var color = Color.WithOpacity(opacity); var renderer = DrawState.Prepare(drawContext.Get <SpriteRenderer>(), camera, RenderStates); // Center if (!BordersOnly && horizontalScale > 0 && verticalScale > 0) { renderer.Draw(Texture, x1, y1, 0, 0, horizontalScale, verticalScale, 0, color, Borders.Left, Borders.Top, Borders.Right, Borders.Bottom); } // Sides if (verticalScale > 0) { renderer.Draw(Texture, x0, y1, 0, 0, 1, verticalScale, 0, color, 0, Borders.Top, Borders.Left, Borders.Bottom); renderer.Draw(Texture, x2, y1, 0, 0, 1, verticalScale, 0, color, Borders.Right, Borders.Top, Texture.Width, Borders.Bottom); } if (horizontalScale > 0) { renderer.Draw(Texture, x1, y0, 0, 0, horizontalScale, 1, 0, color, Borders.Left, 0, Borders.Right, Borders.Top); renderer.Draw(Texture, x1, y2, 0, 0, horizontalScale, 1, 0, color, Borders.Left, Borders.Bottom, Borders.Right, Texture.Height); } // Corners renderer.Draw(Texture, x0, y0, 0, 0, 1, 1, 0, color, 0, 0, Borders.Left, Borders.Top); renderer.Draw(Texture, x2, y0, 0, 0, 1, 1, 0, color, Borders.Right, 0, Texture.Width, Borders.Top); renderer.Draw(Texture, x0, y2, 0, 0, 1, 1, 0, color, 0, Borders.Bottom, Borders.Left, Texture.Height); renderer.Draw(Texture, x2, y2, 0, 0, 1, 1, 0, color, Borders.Right, Borders.Bottom, Texture.Width, Texture.Height); }