Skip to content

Commit

Permalink
fixed layer dimensions when not given explicitly (#385)
Browse files Browse the repository at this point in the history
* fixed layer dimensions when not given explicitly
  • Loading branch information
Wikunia authored Aug 8, 2021
1 parent afa0b71 commit 3f2cad0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 45 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Javis.jl - Changelog

## Unreleased
- bugfix in `@JLayer` when dimensions are not defined explicitly

## v0.6.1 (7th of August 2021)
- Add shorthands for basic shapes
- New functions `JBox, JCircle, JEllipse, JLine, JPoly, JRect, JStar, @JShape`
- added support for `rescale_factor` keyword in `render` function
- Docstring improvements to `translate`


## v0.6.0 (3rd of August 2021)
- Added layers see `@JLayer`

Expand Down
15 changes: 9 additions & 6 deletions src/Javis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ function centered_point(pos::Point, width::Int, height::Int)
Point(pos.x - width / 2, pos.y - height / 2)
end

"""
preprocess_frames!(video::Video)
"""
function preprocess_frames!(video::Video)
return preprocess_frames!([video.objects..., flatten(video.layers)...])
end

"""
preprocess_frames!(objects::Vector{<:AbstractObject})
Expand Down Expand Up @@ -241,13 +249,8 @@ function render(
rescale_factor = 1.0,
)
layers = video.layers
layer_flat = flatten(layers)
objects = video.objects
if isempty(layers)
frames = preprocess_frames!(objects)
else
frames = preprocess_frames!([objects..., layer_flat...])
end
frames = preprocess_frames!(video)

if liveview
if isdefined(Main, :IJulia) && Main.IJulia.inited
Expand Down
4 changes: 2 additions & 2 deletions src/layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ Returns an expression that creates a layer and pushes the objects defined within
function to_layer_m(
frames,
body;
width = CURRENT_VIDEO[1].width,
height = CURRENT_VIDEO[1].height,
width = nothing, # will be set to CURRENT_VIDEO[1].width inside Javis.Layer
height = nothing,
position = Point(0, 0),
transparent = QuoteNode(:transparent),
)
Expand Down
11 changes: 9 additions & 2 deletions src/structs/Layer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const CURRENT_LAYER = Array{Layer,1}()
# for width, height and position defaults are defined in the to_layer_m function
function Layer(
frames,
width::Int,
height::Int,
width,
height,
position::Point;
layer_objects::Vector{AbstractObject} = AbstractObject[],
actions::Vector{AbstractAction} = AbstractAction[],
Expand All @@ -48,6 +48,13 @@ function Layer(
mat = nothing,
layer_cache::LayerCache = LayerCache(),
)
if width === nothing
width = CURRENT_VIDEO[1].width
end
if height === nothing
height = CURRENT_VIDEO[1].height
end

layer = Layer(
frames,
width,
Expand Down
64 changes: 30 additions & 34 deletions test/layers.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
vid = Video(600, 600)
video = Video(600, 600)

@testset "Layers Feature" begin
function ground(args...)
background("white")
sethue("black")
end

Background(1:80, ground)

function object_layer(p = O, color = "black")
@JShape begin
sethue(color)
Expand All @@ -28,6 +23,9 @@ video = Video(600, 600)
line(p1, p2, :stroke)
end

video = Video(600, 600)
Background(1:80, ground)

path_of_red = Point[]
path_of_blue = Point[]

Expand Down Expand Up @@ -102,33 +100,31 @@ video = Video(600, 600)
pob = Object(5:41, (args...) -> path!(path_of_blue, pos(ball2), "blue"))
layer_objects = [ball1, ball2, conn, por, pob]

@testset "Layer macro" begin
@test l1.frames.frames == l2.frames.frames == l3.frames.frames
@test l1.width == l2.width == l3.width == 600
@test l1.height == l2.height == l3.height == 600
@test l1.position == l2.position == l3.position == Point(0, 0)
@test length(l1.layer_objects) ==
length(l2.layer_objects) ==
length(l3.layer_objects) ==
length(layer_objects) + 2
@test length(l1.actions) ==
length(l2.actions) ==
length(l3.actions) ==
length(layer_actions)
@test l1.current_setting.opacity ==
l2.current_setting.opacity ==
l3.current_setting.opacity ==
1.0
@test l1.current_setting.scale ==
l2.current_setting.scale ==
l3.current_setting.scale ==
Javis.Scale(1.0, 1.0)
@test l1.current_setting.rotation_angle ==
l2.current_setting.rotation_angle ==
l3.current_setting.rotation_angle ==
0.0
@test l1.image_matrix == l2.image_matrix == l3.image_matrix == nothing
end
@test l1.frames.frames == l2.frames.frames == l3.frames.frames
@test l1.width == l2.width == l3.width == 600
@test l1.height == l2.height == l3.height == 600
@test l1.position == l2.position == l3.position == Point(0, 0)
@test length(l1.layer_objects) ==
length(l2.layer_objects) ==
length(l3.layer_objects) ==
length(layer_objects) + 2
@test length(l1.actions) ==
length(l2.actions) ==
length(l3.actions) ==
length(layer_actions)
@test l1.current_setting.opacity ==
l2.current_setting.opacity ==
l3.current_setting.opacity ==
1.0
@test l1.current_setting.scale ==
l2.current_setting.scale ==
l3.current_setting.scale ==
Javis.Scale(1.0, 1.0)
@test l1.current_setting.rotation_angle ==
l2.current_setting.rotation_angle ==
l3.current_setting.rotation_angle ==
0.0
@test l1.image_matrix == l2.image_matrix == l3.image_matrix == nothing

# remove duplicate layers after above testing
video.layers = [l1]
Expand Down Expand Up @@ -177,7 +173,7 @@ video = Video(600, 600)
rm("layer_test.gif")


Javis.CURRENT_VIDEO[1] = vid
vid = Video(600, 600)
Background(1:20, ground)

# test @JLayer multiple dispatch
Expand Down Expand Up @@ -216,6 +212,6 @@ video = Video(600, 600)

act!(l5, Action(opacity_anim, setopacity()))

Javis.preprocess_frames!([vid.objects..., Javis.flatten(vid.layers)...])
@test_throws ErrorException Javis.get_layer_frame(vid, l5, 6)

end

0 comments on commit 3f2cad0

Please sign in to comment.