Skip to content

Commit

Permalink
Changed Script interface for drawing variable rounded rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
GPrimola committed Aug 2, 2023
1 parent 84d8c6a commit 853baba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 122 deletions.
73 changes: 3 additions & 70 deletions lib/scenic/script.ex
Original file line number Diff line number Diff line change
Expand Up @@ -539,77 +539,10 @@ defmodule Scenic.Script do
[{:draw_rrect, {width, height, radius, flag}} | ops]
end

@doc """
Draw a rounded rectangle defined by height, width, radius1 and radius2. Can be filled or stroked.
Radius1 and radius2 values will be set as follow:
- Upper left corner: radius1
- Upper right corner: radius2
- Lower right corner: radius1
- Lower left corner: radius2
Creates a new path and draws it.
"""
@spec draw_rounded_rectangle(
ops :: t(),
width :: number,
height :: number,
r1 :: number,
r2 :: number,
fill_stroke_flags :: fill_stroke()
) :: ops :: t()
def draw_rounded_rectangle(ops, width, height, r1, r2, flag) do
upperLeftRadius = smallest([r1, width / 2, height / 2])
upperRightRadius = smallest([r2, width / 2, height / 2])
lowerRightRadius = smallest([r1, width / 2, height / 2])
lowerLeftRadius = smallest([r2, width / 2, height / 2])

[
{:draw_rrectv,
{width, height, upperLeftRadius, upperRightRadius, lowerRightRadius, lowerLeftRadius, flag}}
| ops
]
end

@doc """
Draw a rounded rectangle defined by height, width, radius1, radius2 and radius3. Can be filled or stroked.
Radius1 and radius2 values will be set as follow:
- Upper left corner: radius1
- Upper right corner: radius2
- Lower right corner: radius3
- Lower left corner: radius2
Creates a new path and draws it.
"""
@spec draw_rounded_rectangle(
ops :: t(),
width :: number,
height :: number,
r1 :: number,
r2 :: number,
r3 :: number,
fill_stroke_flags :: fill_stroke()
) :: ops :: t()
def draw_rounded_rectangle(ops, width, height, r1, r2, r3, flag) do
upperLeftRadius = smallest([r1, width / 2, height / 2])
upperRightRadius = smallest([r2, width / 2, height / 2])
lowerRightRadius = smallest([r3, width / 2, height / 2])
lowerLeftRadius = smallest([r2, width / 2, height / 2])

[
{:draw_rrectv,
{width, height, upperLeftRadius, upperRightRadius, lowerRightRadius, lowerLeftRadius, flag}}
| ops
]
end

@doc """
Draw a rounded rectangle defined by height, width, radius1, radius2, radius3 and radius4. Can be filled or stroked.
Radius1 and radius2 values will be set as follow:
Radii values will be set as follow:
- Upper left corner: radius1
- Upper right corner: radius2
Expand All @@ -618,7 +551,7 @@ defmodule Scenic.Script do
Creates a new path and draws it.
"""
@spec draw_rounded_rectangle(
@spec draw_variable_rounded_rectangle(
ops :: t(),
width :: number,
height :: number,
Expand All @@ -628,7 +561,7 @@ defmodule Scenic.Script do
r4 :: number,
fill_stroke_flags :: fill_stroke()
) :: ops :: t()
def draw_rounded_rectangle(ops, width, height, r1, r2, r3, r4, flag) do
def draw_variable_rounded_rectangle(ops, width, height, r1, r2, r3, r4, flag) do
upperLeftRadius = smallest([r1, width / 2, height / 2])

Check warning on line 565 in lib/scenic/script.ex

View workflow job for this annotation

GitHub Actions / build (1.11.4, 24.2)

Variable names should be written in snake_case.
upperRightRadius = smallest([r2, width / 2, height / 2])

Check warning on line 566 in lib/scenic/script.ex

View workflow job for this annotation

GitHub Actions / build (1.11.4, 24.2)

Variable names should be written in snake_case.
lowerRightRadius = smallest([r3, width / 2, height / 2])

Check warning on line 567 in lib/scenic/script.ex

View workflow job for this annotation

GitHub Actions / build (1.11.4, 24.2)

Variable names should be written in snake_case.
Expand Down
61 changes: 9 additions & 52 deletions test/scenic/script_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,75 +118,32 @@ defmodule Scenic.ScriptTest do
[{:draw_rrect, {13.0, 12.0, 6.0, :stroke}}]
end

test "draw_rounded_rectangle varying all corners works" do
test "draw_variable_rounded_rectangle varying all corners works" do
expected = [{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 5.0, :fill}}]
assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :fill) == expected
assert Script.draw_variable_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :fill) == expected
assert expected == Script.serialize(expected) |> Script.deserialize()

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :stroke) ==
assert Script.draw_variable_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 5.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :fill_stroke) ==
assert Script.draw_variable_rounded_rectangle([], 10, 11, 2, 3, 4, 5, :fill_stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 5.0, :fill_stroke}}]
end

test "draw_rounded_rectangle varying 3 corners works" do
expected = [{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 3.0, :fill}}]
assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, :fill) == expected
assert expected == Script.serialize(expected) |> Script.deserialize()

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, :stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 3.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, 4, :fill_stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 4.0, 3.0, :fill_stroke}}]
end

test "draw_rounded_rectangle varying 2 corners works" do
expected = [{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 2.0, 3.0, :fill}}]
assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, :fill) == expected
assert expected == Script.serialize(expected) |> Script.deserialize()

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, :stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 2.0, 3.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 10, 11, 2, 3, :fill_stroke) ==
[{:draw_rrectv, {10.0, 11.0, 2.0, 3.0, 2.0, 3.0, :fill_stroke}}]
end

test "draw_rounded_rectangle varying all corners shrinks radius if too big" do
assert Script.draw_rounded_rectangle([], 10, 12, 30, 40, 50, 4, :fill) ==
test "draw_variable_rounded_rectangle varying all corners shrinks radius if too big" do
assert Script.draw_variable_rounded_rectangle([], 10, 12, 30, 40, 50, 4, :fill) ==
[{:draw_rrectv, {10.0, 12.0, 5.0, 5.0, 5.0, 4.0, :fill}}]

assert Script.draw_rounded_rectangle([], 13, 12, 30, 40, 5, 60, :stroke) ==
assert Script.draw_variable_rounded_rectangle([], 13, 12, 30, 40, 5, 60, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 6.0, 6.0, 5.0, 6.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 13, 12, 30, 4, 50, 60, :stroke) ==
assert Script.draw_variable_rounded_rectangle([], 13, 12, 30, 4, 50, 60, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 6.0, 4.0, 6.0, 6.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 13, 12, 3, 40, 50, 60, :stroke) ==
assert Script.draw_variable_rounded_rectangle([], 13, 12, 3, 40, 50, 60, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 3.0, 6.0, 6.0, 6.0, :stroke}}]
end

test "draw_rounded_rectangle varying 3 corners shrinks radius if too big" do
assert Script.draw_rounded_rectangle([], 10, 12, 30, 40, 5, :fill) ==
[{:draw_rrectv, {10.0, 12.0, 5.0, 5.0, 5.0, 5.0, :fill}}]

assert Script.draw_rounded_rectangle([], 13, 12, 30, 4, 50, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 6.0, 4.0, 6.0, 4.0, :stroke}}]

assert Script.draw_rounded_rectangle([], 13, 12, 3, 40, 50, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 3.0, 6.0, 6.0, 6.0, :stroke}}]
end

test "draw_rounded_rectangle varying 2 corners shrinks radius if too big" do
assert Script.draw_rounded_rectangle([], 10, 12, 30, 4, :fill) ==
[{:draw_rrectv, {10.0, 12.0, 5.0, 4.0, 5.0, 4.0, :fill}}]

assert Script.draw_rounded_rectangle([], 13, 12, 3, 40, :stroke) ==
[{:draw_rrectv, {13.0, 12.0, 3.0, 6.0, 3.0, 6.0, :stroke}}]
end

test "draw_sector works" do
expected = [{:draw_sector, {10.0, 3.0, :fill}}]
assert Script.draw_sector([], 10, 3, :fill) == expected
Expand Down

0 comments on commit 853baba

Please sign in to comment.