Packages

Brings CSS-like grid as well as auto-layouts for components. This allows one to dynamically add components to a scene. The goal is to bring some familiar layout apis to Scenic.

Current section

Files

Jump to
scenic_layout_o_matic lib layouts primitives rounded_rectangle.ex
Raw

lib/layouts/primitives/rounded_rectangle.ex

defmodule LayoutOMatic.RoundedRectangle do
@default_stroke {1, :white}
# A rectangle is translated from the top left corner
@spec translate(%{
grid_xy: {number, number},
max_xy: {number, number},
primitive: %{data: {number, number, number}},
starting_xy: {number, number}
}) ::
{:error, <<_::160, _::_*32>>}
| {:ok, {number, number},
%{
grid_xy: {number, number},
max_xy: {number, number},
primitive: %{data: {number, number, number}},
starting_xy: {number, number}
}}
def translate(
%{
primitive: primitive,
starting_xy: starting_xy,
max_xy: max_xy,
grid_xy: grid_xy
} = layout
) do
%{data: {width, height, _}} = primitive
{grid_x, grid_y} = grid_xy
{starting_x, starting_y} = starting_xy
stroke_fill =
case Map.get(primitive, :styles) do
nil ->
elem(@default_stroke, 0)
styles when is_map(styles) ->
%{stroke: stroke} = styles
elem(stroke, 0)
end
case starting_xy == grid_xy do
true ->
layout =
Map.put(
layout,
:starting_xy,
{starting_x + width + stroke_fill, starting_y + stroke_fill}
)
{:ok, {starting_x + stroke_fill / 2, starting_y + stroke_fill / 2}, layout}
false ->
# already in a new group, use starting_xy
case fits_in_x?(starting_x + width + stroke_fill, max_xy) do
# fits in x
true ->
# fit in y?
case fits_in_y?(starting_y + height + stroke_fill, max_xy) do
true ->
# fits
layout =
Map.put(
layout,
:starting_xy,
{starting_x + width + stroke_fill, starting_y + stroke_fill}
)
{:ok, {starting_x, starting_y}, layout}
# Does not fit
false ->
{:error, "Does not fit in grid"}
end
# doesnt fit in x
false ->
# fit in new y?
new_y = grid_y + height + stroke_fill
case fits_in_y?(new_y, max_xy) do
true ->
new_layout =
layout
|> Map.put(:grid_xy, {grid_x, new_y})
|> Map.put(:starting_xy, {width + stroke_fill, new_y})
{:ok, {grid_x + stroke_fill / 2, new_y}, new_layout}
false ->
{:error, "Does not fit in the grid"}
end
end
end
end
def fits_in_x?(potential_x, {max_x, _}),
do: potential_x <= max_x
def fits_in_y?(potential_y, {_, max_y}),
do: potential_y <= max_y
end