refactoring to get middle/center, possibly more
This commit is contained in:
+16
-7
@@ -1,17 +1,20 @@
|
||||
require_relative 'arg_loader'
|
||||
require_relative 'xywh_shorthands'
|
||||
|
||||
module Squib::Args
|
||||
|
||||
module_function def extract_box(opts, deck, dsl_method_defaults = {})
|
||||
Box.new(deck, dsl_method_defaults).extract!(opts, deck)
|
||||
Box.new(deck, dsl_method_defaults, opts).extract!(opts, deck)
|
||||
end
|
||||
|
||||
class Box
|
||||
include ArgLoader
|
||||
include XYWHShorthands
|
||||
|
||||
def initialize(deck = nil, dsl_method_defaults = {})
|
||||
def initialize(deck = nil, dsl_method_defaults = {}, opts = {})
|
||||
@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
|
||||
@@ -29,16 +32,22 @@ 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_width(arg, _i)
|
||||
return arg if @deck.nil?
|
||||
return @deck.width if arg == :deck
|
||||
arg
|
||||
apply_x_shorthands(arg, @deck.width)
|
||||
end
|
||||
|
||||
def validate_height(arg, _i)
|
||||
return arg if @deck.nil?
|
||||
return @deck.height if arg == :deck
|
||||
arg
|
||||
apply_y_shorthands(arg, @deck.height)
|
||||
end
|
||||
|
||||
def validate_x_radius(arg, i)
|
||||
@@ -49,7 +58,7 @@ module Squib::Args
|
||||
def validate_y_radius(arg, i)
|
||||
return radius[i] unless radius[i].nil?
|
||||
arg
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ module Squib
|
||||
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM
|
||||
when /mm$/ # ends with "mm"
|
||||
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM / 10.0
|
||||
when /deg$/ # ends with "deg"
|
||||
arg.rstrip[0..-3].to_f * (Math::PI / 180.0)
|
||||
else
|
||||
arg
|
||||
end
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
module Squib
|
||||
module Args
|
||||
module XYWHShorthands
|
||||
|
||||
def apply_x_shorthands(arg, deck_width)
|
||||
arg_s = arg.to_s
|
||||
case arg_s
|
||||
when 'middle'
|
||||
deck_width / 2.0
|
||||
when 'center'
|
||||
deck_width / 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
|
||||
else
|
||||
arg
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -23,5 +23,10 @@ module Squib
|
||||
@dpi * Squib::INCHES_IN_CM * n.to_f / 10.0
|
||||
end
|
||||
|
||||
# DSL method. See http://squib.readthedocs.io
|
||||
def deg(n)
|
||||
n.to_f * (Math::PI / 180.0)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
require 'yaml'
|
||||
require_relative 'args/xywh_shorthands'
|
||||
|
||||
module Squib
|
||||
# Internal class for handling layouts
|
||||
# @api private
|
||||
class LayoutParser
|
||||
include Args::XYWHShorthands
|
||||
|
||||
def initialize(dpi = 300)
|
||||
@dpi = dpi
|
||||
@@ -67,6 +69,9 @@ module Squib
|
||||
end
|
||||
|
||||
def handle_relative_operators(parent_val, child_val)
|
||||
unless 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?('+=')
|
||||
add_parent_child(parent_val, child_val)
|
||||
elsif child_val.to_s.strip.start_with?('-=')
|
||||
@@ -104,6 +109,13 @@ module Squib
|
||||
parent_pixels / child_float
|
||||
end
|
||||
|
||||
# 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.*/
|
||||
end
|
||||
|
||||
# Does this layout entry have an extends field?
|
||||
# i.e. is it a base-case or will it need recursion?
|
||||
# :nodoc:
|
||||
|
||||
Reference in New Issue
Block a user