make shorthands more intuitive
parent
02d02ac125
commit
11d550f3df
|
|
@ -5,17 +5,20 @@ For the arguments ``x``, ``y``, ``width``, and ``height``, a few convenient shor
|
|||
|
||||
* ``middle`` for ``x`` and ``width`` refer to the deck's width / 2
|
||||
* ``middle`` for ``y`` and ``height`` refer to the deck's height / 2
|
||||
* The word ``center`` behaves the same way
|
||||
* ``deck`` refers to the deck's width for ``x`` and ``width``
|
||||
* ``deck`` refers to the deck's height for ``y`` and ``height``
|
||||
* You can offset from the middle by using +, -, and /, e.g. ``middle + 1in``
|
||||
* You can offset from the width or height using, e.g. ``width - 1in`` or ``height - 2mm``
|
||||
* Works with the ``cell`` unit as well, e.g. `middle + 1 cell`. See :doc:`units`.
|
||||
* You can offset from the middle by using + or - operators, e.g. ``middle + 1in``
|
||||
* You can offset from the deck width or height using the + or - operators, e.g. ``deck - 1in`` or ``deck - 2mm``
|
||||
* You can offset from the deck width or height using, e.g. ``deck / 3``
|
||||
* Works with all unit conversion too, e.g. `middle + 1 cell`. See :doc:`units`.
|
||||
|
||||
These are all passed as strings. So you will need to quote them in Ruby, or just plain in your layout YAML.
|
||||
|
||||
Note that the following are NOT supported:
|
||||
|
||||
* The `+=` operator when using `extends` in a layout file
|
||||
* Complicated formulas. We're not evaluating this as code, we're looking for these specific patterns and applying them. Anything more complicated you'll have to handle with Ruby code.
|
||||
|
||||
Samples
|
||||
-------
|
||||
|
|
|
|||
|
|
@ -3,49 +3,44 @@ require_relative 'unit_conversion'
|
|||
module Squib
|
||||
module Args
|
||||
module XYWHShorthands
|
||||
WIDTH_MINUS_REGEX = /^width\s*\-\s*/
|
||||
HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
|
||||
WIDTH_DIV_REGEX = /^width\s*\/\s*/
|
||||
HEIGHT_DIV_REGEX = /^height\s*\/\s*/
|
||||
MIDDLE_PLUS_REGEX = /^middle\s*\+\s*/
|
||||
MIDDLE_MINUS_REGEX = /^middle\s*\-\s*/
|
||||
|
||||
MIDDLE_ONLY = /^(middle|center)\s*$/
|
||||
DECK_ONLY = /^deck\s*$/
|
||||
MIDDLE_MINUS_REGEX = /^(middle|center)\s*\-\s*/
|
||||
MIDDLE_PLUS_REGEX = /^(middle|center)\s*\+\s*/
|
||||
DECK_MINUS_REGEX = /^deck\s*\-\s*/
|
||||
DECK_PLUS_REGEX = /^deck\s*\+\s*/
|
||||
DECK_DIV_REGEX = /^deck\s*\/\s*/
|
||||
|
||||
# dimension is usually either deck_width or deck_height
|
||||
def apply_shorthands(arg, deck, axis: :x)
|
||||
dimension = (axis == :x) ? deck.width : deck.height
|
||||
arg_s = arg.to_s
|
||||
case arg_s
|
||||
when 'middle'
|
||||
when MIDDLE_ONLY
|
||||
dimension / 2.0
|
||||
when 'center'
|
||||
dimension / 2.0
|
||||
when 'deck'
|
||||
when DECK_ONLY
|
||||
dimension
|
||||
when WIDTH_MINUS_REGEX # e.g. width - 1.5in
|
||||
n = arg_s.sub WIDTH_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
|
||||
deck.width - n
|
||||
when HEIGHT_MINUS_REGEX # e.g. height - 1.5in
|
||||
n = arg_s.sub HEIGHT_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
|
||||
deck.height - n
|
||||
when WIDTH_DIV_REGEX # e.g. width / 3
|
||||
n = arg_s.sub WIDTH_DIV_REGEX, ''
|
||||
when MIDDLE_MINUS_REGEX # e.g. width: middle - 3
|
||||
n = arg_s.sub MIDDLE_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
deck.width / n
|
||||
when HEIGHT_DIV_REGEX # e.g. height / 3
|
||||
n = arg_s.sub HEIGHT_DIV_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
deck.height / n
|
||||
dimension / 2.0 - n
|
||||
when MIDDLE_PLUS_REGEX # e.g. middle + 1.5in
|
||||
n = arg_s.sub MIDDLE_PLUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
dimension / 2.0 + n
|
||||
when MIDDLE_MINUS_REGEX # e.g. middle - 1.5in
|
||||
n = arg_s.sub MIDDLE_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px)
|
||||
dimension / 2.0 - n
|
||||
when DECK_MINUS_REGEX # e.g. width: deck - 1.5in
|
||||
n = arg_s.sub DECK_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
dimension - n
|
||||
when DECK_PLUS_REGEX # e.g. deck + 1.5in (which is weird but ok)
|
||||
n = arg_s.sub DECK_PLUS_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
dimension + n
|
||||
when DECK_DIV_REGEX # e.g. width: deck/3
|
||||
n = arg_s.sub DECK_DIV_REGEX, ''
|
||||
n = UnitConversion.parse(n, deck.dpi, deck.cell_px).to_f
|
||||
dimension / n
|
||||
else
|
||||
arg
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ module Squib
|
|||
# Most of the time this is in the alpha of the next release.
|
||||
# e.g. v0.0.5a is on its way to becoming v0.0.5
|
||||
#
|
||||
VERSION = '0.16.0-preview1'
|
||||
VERSION = '0.16.0-preview2a'
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -20,14 +20,13 @@ Squib::Deck.new(width: '0.5in', height: '0.25in') do
|
|||
|
||||
# We can also do width-, height-, width/, height/
|
||||
rect x: 20, y: 5, stroke_color: :green,
|
||||
width: 'width - 0.1in', height: 10
|
||||
width: 'deck - 0.1in', height: 10
|
||||
|
||||
rect x: 10, y: 50, width: 10, height: 'height / 3',
|
||||
rect x: 10, y: 50, width: 10, height: 'deck / 3',
|
||||
stroke_color: :purple
|
||||
|
||||
# And middle+/-
|
||||
|
||||
rect x: 'middle + 0.1in', y: 'middle - 0.1in',
|
||||
rect x: 'middle + 0.1in', y: 'center - 0.1in',
|
||||
width: '0.1in', height: '0.1in', fill_color: :blue
|
||||
|
||||
# Layouts apply this too.
|
||||
|
|
|
|||
|
|
@ -152,14 +152,14 @@ describe Squib::Args::Box do
|
|||
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
||||
end
|
||||
|
||||
it 'listens to height/2' do
|
||||
args = { width: 'height / 2', height: :deck }
|
||||
it 'listens to deck/2' do
|
||||
args = { width: 'deck / 2', height: :deck }
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(width: [228.0], height: [456])
|
||||
expect(box).to have_attributes(width: [61.5], height: [456])
|
||||
end
|
||||
|
||||
it 'listens to width - 0.5in' do
|
||||
args = { x: 'width - 0.5in'}
|
||||
it 'listens to deck - 0.5in' do
|
||||
args = { x: 'deck - 0.5in'}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(x: [ 123 - 150 ])
|
||||
end
|
||||
|
|
@ -172,8 +172,8 @@ describe Squib::Args::Box do
|
|||
args = {
|
||||
x: 'middle + 1c',
|
||||
y: 'middle',
|
||||
width: 'width - 2c',
|
||||
height: 'height / 3'
|
||||
width: 'deck - 2c',
|
||||
height: 'deck / 3'
|
||||
}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ describe Squib::Args::XYWHShorthands do
|
|||
args = {
|
||||
x: 'middle',
|
||||
y: 'middle + 1in',
|
||||
width: 'width / 2',
|
||||
height: 'height - 1in',
|
||||
width: 'deck / 2',
|
||||
height: 'deck - 1in',
|
||||
}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes({
|
||||
|
|
|
|||
Loading…
Reference in New Issue