Module: Vizcore::Shape

Defined in:
lib/vizcore/shape.rb

Overview

Mixin and runtime helpers for Ruby-defined custom shape generators.

Defined Under Namespace

Modules: ClassMethods Classes: AudioContext, Definition, DrawContext, PrimitiveBuilder

Constant Summary collapse

SUPPORTED_PRIMITIVES =
%i[circle line rect polygon polyline path star].freeze
STYLE_KEYS =
%i[fill stroke stroke_width stroke_color opacity blend line_cap line_join miter_limit dash].freeze

Class Method Summary collapse

Class Method Details

.expand_custom_shape(renderer, params:, shape_id: nil, layer_name: nil, palette: [], audio: {}, time: 0.0, frame: 0, resolution: [1280, 720], globals: {}, shape_name: nil, cache: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vizcore/shape.rb', line 27

def expand_custom_shape(renderer, params:, shape_id: nil, layer_name: nil, palette: [], audio: {}, time: 0.0, frame: 0, resolution: [1280, 720], globals: {}, shape_name: nil, cache: false)
  definition = shape_definition(renderer)
  renderer = definition.renderer if definition
  shape_name ||= definition&.name || renderer
  cache_key = custom_shape_cache_key(
    renderer: renderer,
    params: params,
    shape_id: shape_id,
    layer_name: layer_name,
    palette: palette,
    resolution: resolution,
    globals: globals,
    shape_name: shape_name
  ) if cache
  return deep_dup(custom_shape_cache[cache_key]) if cache_key && custom_shape_cache.key?(cache_key)

  context = DrawContext.new(
    params: params,
    param_schema: param_schema_for(renderer),
    shape_id: shape_id,
    layer_name: layer_name,
    palette: palette,
    audio: audio,
    time: time,
    frame: frame,
    resolution: resolution,
    globals: globals
  )
  result = call_renderer(renderer, context, params)
  result = context.shapes if result.nil?
  primitives = normalize_primitives(result, shape_name: shape_name)
  if shape_id && primitives.length != 1
    raise ArgumentError, "custom_shape id can only be assigned when one primitive is produced"
  end
  primitives.first[:id] ||= shape_id.to_sym if shape_id
  custom_shape_cache[cache_key] = deep_dup(primitives) if cache_key
  primitives
end

.included(base) ⇒ Object



11
12
13
# File 'lib/vizcore/shape.rb', line 11

def included(base)
  base.extend(ClassMethods)
end

.normalize_primitives(value, shape_name:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vizcore/shape.rb', line 15

def normalize_primitives(value, shape_name:)
  primitives = case value
               when nil
                 []
               when Hash
                 [value]
               else
                 Array(value)
               end
  primitives.map { |primitive| normalize_primitive(primitive, shape_name: shape_name) }
end

.param_schema_for(renderer) ⇒ Object



66
67
68
69
70
# File 'lib/vizcore/shape.rb', line 66

def param_schema_for(renderer)
  return renderer.shape_param_schema if renderer.respond_to?(:shape_param_schema)

  {}
end