Class: Dialog

Inherits:
Scene show all
Defined in:
lib/rubysketch/solitaire/common/dialog.rb

Defined Under Namespace

Classes: Label

Instance Attribute Summary

Attributes inherited from Scene

#name, #parent

Instance Method Summary collapse

Methods inherited from Scene

#active?, #add, #emitParticle, #focusChanged, #mouseDragged, #mouseMoved, #mousePressed, #mouseReleased, #particle, #pause, #remove, #resume, #transition, #update

Constructor Details

#initialize(background: 0, alpha: 100, z: 1000, &block) ⇒ Dialog

Returns a new instance of Dialog.



10
11
12
13
14
15
16
17
18
19
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 10

def initialize(background: 0, alpha: 100, z: 1000, &block)
  @background, @alpha = background, 0
  @elements = []
  super
  overlay.z = z
  group :vertical, &block if block
  animate 0.2 do |t|
    @alpha = alpha * t
  end
end

Instance Method Details

#activatedObject



94
95
96
97
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 94

def activated()
  super
  parent.pause
end

#addButton(*args, **kwargs, &block) ⇒ Object



59
60
61
62
63
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 59

def addButton(*args, **kwargs, &block)
  addElement Button.new(*args, **kwargs).tap {|b|
    b.clicked &block
  }
end

#addElement(sprite) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 34

def addElement(sprite)
  (@group || @elements).push sprite
  sprite.z = overlay.z
  addSprite sprite if active?
  updateLayout
  sprite
end

#addLabel(label, rgb: [255], alpha: nil, fontSize: 24, align: CENTER, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 42

def addLabel(label, rgb: [255], alpha: nil, fontSize: 24, align: CENTER, &block)
  bounds = textFont.textBounds label, 0, 0, fontSize
  addElement Label.new(0, 0, width - MARGIN * 2, bounds.h).tap {|sp|
    sp.label = label
    sp.draw do
      r, g, b, a = skin.translucentBackgroundColor
      fill r, g, b, alpha || (a * 3)
      rect 0, -MARGIN / 2, sp.w, sp.h + MARGIN
      textAlign align, CENTER
      textSize fontSize
      fill *rgb
      text sp.label, 0, 0, sp.w, sp.h
    end
    sp.mouseClicked &block
  }
end

#addSpace(height = 0) ⇒ Object



65
66
67
68
69
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 65

def addSpace(height = 0)
  addElement Sprite.new(0, 0, 1, height).tap {|sp|
    sp.draw {}
  }
end

#closeObject



71
72
73
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 71

def close()
  delay {parent.remove self}
end

#deactivatedObject



99
100
101
102
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 99

def deactivated()
  parent.resume
  super
end

#drawObject



84
85
86
87
88
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 84

def draw()
  sprite overlay
  super
  sprite *elements
end

#elementsObject



79
80
81
82
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 79

def elements()
  f = -> es {es.map {|e| ((e in {elements:})) ? f[elements] : e}}
  f.call(@elements).flatten
end

#group(flow = :horizontal, space: nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 21

def group(flow = :horizontal, space: nil, &block)
  old, @group = @group, []
  block.call self
ensure
  (old || @elements).push({
    elements: @group,
    flow:     flow,
    space:    space || MARGIN
  })
  @group = old
  updateLayout
end

#resized(w, h) ⇒ Object



90
91
92
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 90

def resized(w, h)
  updateLayout
end

#spritesObject



75
76
77
# File 'lib/rubysketch/solitaire/common/dialog.rb', line 75

def sprites()
  super + [overlay, *elements]
end