Class: Shoes::Slot

Inherits:
Drawable show all
Defined in:
lacci/lib/shoes.rb,
lacci/lib/shoes/drawables/slot.rb

Direct Known Subclasses

Flow, Shape, Stack, Widget

Constant Summary

Constants inherited from Drawable

Drawable::DRAW_CONTEXT_STYLES

Constants included from Log

Log::DEFAULT_COMPONENT, Log::DEFAULT_DEBUG_LOG_CONFIG, Log::DEFAULT_LOG_CONFIG

Instance Attribute Summary collapse

Attributes inherited from Drawable

#debug_id, #destroyed, #parent

Attributes inherited from Linkable

#linkable_id

Instance Method Summary collapse

Methods inherited from Drawable

allocate_drawable_id, #app, #banner, #caption, convert_to_float, convert_to_integer, #destroy, #download, drawable_by_id, drawable_class_by_name, dsl_name, #event, expects_parent?, feature_for_shoes_style, get_shoes_events, #hide, #hover, init_args, #inscription, #inspect, is_widget_class?, #leave, #motion, opt_init_args, optional_init_args, register_drawable_id, registered_shoes_events?, required_init_args, #set_parent, shoes_events, shoes_style, shoes_style_hashes, shoes_style_name?, shoes_style_names, #shoes_style_values, shoes_styles, #show, #style, #subtitle, #tagline, #title, #toggle, unregister_drawable_id, use_current_app, validate_as, with_current_app

Methods included from MarginHelper

#margin_parse

Methods included from Colors

#gray, #rgb, #to_rgb

Methods included from Log

configure_logger, #log_init, logger

Methods inherited from Linkable

#bind_shoes_event, #send_self_event, #send_shoes_event, #unsub_all_shoes_events, #unsub_shoes_event

Constructor Details

#initializeSlot



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lacci/lib/shoes/drawables/slot.rb', line 14

def initialize(...)
  # The draw context tracks current settings like fill and stroke,
  # plus potentially other current state that changes from drawable
  # to drawable and slot to slot.
  @draw_context = {
    "fill" => nil,
    "stroke" => nil,
    "strokewidth" => nil,
    "rotate" => nil,
    # "transform" => nil, # "corner",
    # "translate" => nil, # [0, 0],
  }

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object

We use method_missing for drawable-creating methods like "button". The parent's method_missing will auto-create Shoes style getters and setters. This is similar to the method_missing in Shoes::App, but differs in where the new drawable will appear.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lacci/lib/shoes/drawables/slot.rb', line 55

def method_missing(name, *args, **kwargs, &block)
  klass = ::Shoes::Drawable.drawable_class_by_name(name)
  return super unless klass

  ::Shoes::Slot.define_method(name) do |*args, **kwargs, &block|
    instance = nil

    # Look up the Shoes drawable and create it. But first set
    # this slot as the current one so that draw context
    # is handled properly.
    @app.with_slot(self) do
      Shoes::Drawable.with_current_app(self.app) do
        instance = klass.new(*args, **kwargs, &block)
      end
    end

    instance
  end

  send(name, *args, **kwargs, &block)
end

Instance Attribute Details

#childrenObject (readonly)

Incompatibilities with Shoes:

  • Shoes uses #content, not #children, for this. Scarpe does both.



5
6
7
# File 'lacci/lib/shoes/drawables/slot.rb', line 5

def children
  @children
end

#draw_contextObject (readonly)

This only shows this specific slot's settings, not its parent's. Use current_draw_context to allow inheritance.



11
12
13
# File 'lacci/lib/shoes/drawables/slot.rb', line 11

def draw_context
  @draw_context
end

Instance Method Details

#add_child(child) ⇒ Object

Do not call directly, use set_parent



40
41
42
43
# File 'lacci/lib/shoes/drawables/slot.rb', line 40

def add_child(child)
  @children ||= []
  @children << child
end

#append { ... } ⇒ void

This method returns an undefined value.

Call the block to append new children to a Slot.

Should only be called on a Slot, since only Slots can have children.

Yields:

  • the block to call to replace children; will be called on the Shoes::App, appending to the called Slot as the current slot

Raises:

Incompatibilities with Shoes:

  • Shoes Classic calls the append block with current self, while Scarpe uses the Shoes::App as self



174
175
176
177
178
179
# File 'lacci/lib/shoes/drawables/slot.rb', line 174

def append(&block)
  raise(Shoes::Errors::InvalidAttributeValueError, "append requires a block!") unless block_given?
  raise(Shoes::Errors::InvalidAttributeValueError, "Don't append to something that isn't a slot!") unless self.is_a?(Shoes::Slot)

  @app.with_slot(self, &block)
end

#clear { ... } ⇒ void

This method returns an undefined value.

Remove all children from this drawable. If a block is given, call the block to replace the children with new contents from that block.

Should only be called on Slots, which can have children.

Yields:

  • The block to call to replace the contents of the drawable (optional)

Incompatibilities with Shoes:

  • Shoes Classic calls the clear block with current self, while Scarpe uses the Shoes::App as self



159
160
161
162
163
164
# File 'lacci/lib/shoes/drawables/slot.rb', line 159

def clear(&block)
  @children ||= []
  @children.dup.each(&:destroy)
  append(&block) if block_given?
  nil
end

#contentsObject

Get a list of child drawables



46
47
48
49
# File 'lacci/lib/shoes/drawables/slot.rb', line 46

def contents
  @children ||= []
  @children.dup
end

#current_draw_contextHash

Get the current draw context styles, based on this slot and its parent slots.



139
140
141
142
143
144
# File 'lacci/lib/shoes/drawables/slot.rb', line 139

def current_draw_context
  s = @parent ? @parent.current_draw_context : {}
  @draw_context.each { |k, v| s[k] = v unless v.nil? }

  s
end

#fill(color) ⇒ void

This method returns an undefined value.

Set the default fill color in this slot and child slots. Pass nil for "no setting", so that it can inherit defaults.



90
91
92
# File 'lacci/lib/shoes/drawables/slot.rb', line 90

def fill(color)
  @draw_context["fill"] = color
end

#nofillvoid

This method returns an undefined value.

Set the default fill in this slot and child slots to transparent.



97
98
99
# File 'lacci/lib/shoes/drawables/slot.rb', line 97

def nofill
  @draw_context["fill"] = rgb(0, 0, 0, 0)
end

#nostrokevoid

This method returns an undefined value.

Set the default stroke in this slot and child slots to transparent.



123
124
125
# File 'lacci/lib/shoes/drawables/slot.rb', line 123

def nostroke
  @draw_context["stroke"] = rgb(0, 0, 0, 0)
end

#remove_child(child) ⇒ Object

Do not call directly, use set_parent



31
32
33
34
35
36
37
# File 'lacci/lib/shoes/drawables/slot.rb', line 31

def remove_child(child)
  @children ||= []
  unless @children.include?(child)
    @log.warn("remove_child: no such child(#{child.inspect}) for parent(#{parent.inspect})!")
  end
  @children.delete(child)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean



77
78
79
80
81
# File 'lacci/lib/shoes/drawables/slot.rb', line 77

def respond_to_missing?(name, include_private = false)
  return true if ::Shoes::Drawable.drawable_class_by_name(name.to_s)

  false
end

#rotate(angle) ⇒ void

This method returns an undefined value.

Set the current rotation in this slot and any child slots. Pass nil to reset the angle to default.



132
133
134
# File 'lacci/lib/shoes/drawables/slot.rb', line 132

def rotate(angle)
  @draw_context["rotate"] = angle
end

#stroke(color) ⇒ void

This method returns an undefined value.

Set the default stroke color in this slot and child slots. Pass nil for "no setting" so it can inherit defaults.



106
107
108
# File 'lacci/lib/shoes/drawables/slot.rb', line 106

def stroke(color)
  @draw_context["stroke"] = color
end

#strokewidth(width) ⇒ void

This method returns an undefined value.

Set the default strokewidth in this slot and child slots. Pass nil for "no setting".



115
116
117
# File 'lacci/lib/shoes/drawables/slot.rb', line 115

def strokewidth(width)
  @draw_context["strokewidth"] = width
end