shorthands for xywh! initial implementation
This commit is contained in:
+6
-12
@@ -4,17 +4,16 @@ require_relative 'xywh_shorthands'
|
||||
module Squib::Args
|
||||
|
||||
module_function def extract_box(opts, deck, dsl_method_defaults = {})
|
||||
Box.new(deck, dsl_method_defaults, opts).extract!(opts, deck)
|
||||
Box.new(deck, dsl_method_defaults).extract!(opts, deck)
|
||||
end
|
||||
|
||||
class Box
|
||||
include ArgLoader
|
||||
include XYWHShorthands
|
||||
|
||||
def initialize(deck = nil, dsl_method_defaults = {}, opts = {})
|
||||
def initialize(deck = nil, dsl_method_defaults = {})
|
||||
@deck = deck
|
||||
@dsl_method_defaults = dsl_method_defaults
|
||||
@opts = opts # e.g. value of x can depend on the value of width
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
@@ -32,22 +31,17 @@ module Squib::Args
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def validate_x(arg, i)
|
||||
apply_x_shorthands(arg, @deck.width)
|
||||
end
|
||||
|
||||
def validate_y(arg,_i)
|
||||
apply_y_shorthands(arg, @deck.height)
|
||||
end
|
||||
def validate_x(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
|
||||
def validate_width(arg, _i)
|
||||
return arg if @deck.nil?
|
||||
apply_x_shorthands(arg, @deck.width)
|
||||
apply_shorthands(arg, @deck, axis: :x)
|
||||
end
|
||||
|
||||
def validate_height(arg, _i)
|
||||
return arg if @deck.nil?
|
||||
apply_y_shorthands(arg, @deck.height)
|
||||
apply_shorthands(arg, @deck, axis: :y)
|
||||
end
|
||||
|
||||
def validate_x_radius(arg, i)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require_relative 'arg_loader'
|
||||
require_relative 'xywh_shorthands'
|
||||
|
||||
module Squib::Args
|
||||
module_function def extract_coords(opts, deck)
|
||||
@@ -7,6 +8,7 @@ module Squib::Args
|
||||
|
||||
class Coords
|
||||
include ArgLoader
|
||||
include XYWHShorthands
|
||||
|
||||
def self.parameters
|
||||
{ x: 0, y: 0,
|
||||
@@ -30,6 +32,18 @@ module Squib::Args
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def validate_x(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
def validate_x1(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y1(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
def validate_x2(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y2(arg,_i) apply_shorthands(arg, @deck, axis: :y)end
|
||||
def validate_x3(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y3(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
def validate_cx1(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_cy1(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
def validate_cx2(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_cy2(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require_relative 'arg_loader'
|
||||
require_relative 'xywh_shorthands'
|
||||
|
||||
module Squib::Args
|
||||
module_function def extract_scale_box(opts, deck)
|
||||
@@ -7,6 +8,7 @@ module Squib::Args
|
||||
|
||||
class ScaleBox
|
||||
include ArgLoader
|
||||
include XYWHShorthands
|
||||
|
||||
def self.parameters
|
||||
{
|
||||
@@ -23,9 +25,13 @@ module Squib::Args
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def validate_x(arg, i) apply_shorthands(arg, @deck, axis: :x) end
|
||||
def validate_y(arg,_i) apply_shorthands(arg, @deck, axis: :y) end
|
||||
|
||||
def validate_width(arg, i)
|
||||
return @deck.width if arg.to_s == 'deck'
|
||||
return :native if arg.to_s == 'native'
|
||||
arg = apply_shorthands(arg, @deck, axis: :x)
|
||||
return arg if arg.respond_to? :to_f
|
||||
if arg.to_s == 'scale'
|
||||
raise 'if width is :scale, height must be a number' unless height[i].respond_to? :to_f
|
||||
@@ -37,6 +43,7 @@ module Squib::Args
|
||||
def validate_height(arg, i)
|
||||
return @deck.height if arg.to_s == 'deck'
|
||||
return :native if arg.to_s == 'native'
|
||||
arg = apply_shorthands(arg, @deck, axis: :y)
|
||||
return arg if arg.respond_to? :to_f
|
||||
if arg.to_s == 'scale'
|
||||
raise 'if height is \'scale\', width must be a number' unless width[i].respond_to? :to_f
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
require_relative 'unit_conversion'
|
||||
|
||||
module Squib
|
||||
module Args
|
||||
module XYWHShorthands
|
||||
|
||||
def apply_x_shorthands(arg, deck_width)
|
||||
WIDTH_MINUS_REGEX = /^width\s*\-\s*/
|
||||
HEIGHT_MINUS_REGEX = /^height\s*\-\s*/
|
||||
WIDTH_DIV_REGEX = /^width\s*\/\s*/
|
||||
HEIGHT_DIV_REGEX = /^height\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'
|
||||
deck_width / 2.0
|
||||
dimension / 2.0
|
||||
when 'center'
|
||||
deck_width / 2.0
|
||||
dimension / 2.0
|
||||
when 'deck'
|
||||
deck_width
|
||||
else
|
||||
arg
|
||||
end
|
||||
end
|
||||
|
||||
def apply_y_shorthands(arg, deck_height)
|
||||
arg_s = arg.to_s
|
||||
case arg_s
|
||||
when 'middle'
|
||||
deck_height / 2.0
|
||||
when 'center'
|
||||
deck_height / 2.0
|
||||
when 'deck'
|
||||
deck_height
|
||||
dimension
|
||||
when WIDTH_MINUS_REGEX # e.g. width - 1.5in
|
||||
n = arg_s.sub WIDTH_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n)
|
||||
deck.width - n
|
||||
when HEIGHT_MINUS_REGEX # e.g. height - 1.5in
|
||||
n = arg_s.sub HEIGHT_MINUS_REGEX, ''
|
||||
n = UnitConversion.parse(n)
|
||||
deck.height - n
|
||||
when WIDTH_DIV_REGEX # e.g. width / 3
|
||||
n = (arg_s.sub WIDTH_DIV_REGEX, '').to_f
|
||||
deck.width / n
|
||||
when HEIGHT_DIV_REGEX # e.g. height / 3
|
||||
n = (arg_s.sub HEIGHT_DIV_REGEX, '').to_f
|
||||
deck.height / n
|
||||
else
|
||||
arg
|
||||
end
|
||||
|
||||
@@ -69,7 +69,7 @@ module Squib
|
||||
end
|
||||
|
||||
def handle_relative_operators(parent_val, child_val)
|
||||
unless has_digits?(parent_val) && has_digits?(child_val)
|
||||
if uses_operator?(child_val) && !has_digits?(parent_val) && !has_digits?(child_val)
|
||||
raise "Layout parse error: can't combine #{parent_val} and #{child_val}"
|
||||
end
|
||||
if child_val.to_s.strip.start_with?('+=')
|
||||
@@ -112,8 +112,12 @@ module Squib
|
||||
# For relative operators, it's difficult for us to handle
|
||||
# some of the shorthands - so let's just freak out if you're trying to use
|
||||
# relative operators with words, e.g. "middle += 0.5in"
|
||||
def has_digits?(str)
|
||||
str.match? /.*\d.*/
|
||||
def has_digits?(arg)
|
||||
/.*\d.*/ === arg.to_s
|
||||
end
|
||||
|
||||
def uses_operator?(arg)
|
||||
/^[+=|\-=|\/=|*=].*/ === arg.to_s
|
||||
end
|
||||
|
||||
# Does this layout entry have an extends field?
|
||||
|
||||
Reference in New Issue
Block a user