Class: Background

Inherits:
Scene
  • Object
show all
Defined in:
lib/rubysketch/solitaire/background.rb

Constant Summary collapse

TYPES =
{
  checker: {
    name: 'Checker'
  },
  checker2: {
    name: 'Checker (No Scroll)'
  },
  cosmic2: {
    name:   'Cosmic 2',
    author: 'huwb',
    url:    'https://www.shadertoy.com/view/XllGzN'
  },
  classicPSPWave: {
    name:   'Classic PSP Wave',
    author: 'ParkingLotGames',
    url:    'https://www.shadertoy.com/view/ddV3DK'
  },
  reflectiveHexes: {
    name:   'Reflective hexes',
    author: 'mrange',
    url:    'https://www.shadertoy.com/view/ds2XRt'
  },
  colorfulUnderwaterBubbles2: {
    name:   'Colorful underwater bubbles II',
    author: 'mrange',
    url:    'https://www.shadertoy.com/view/mlBSWc'
  },
}

Instance Attribute Summary collapse

Attributes inherited from Scene

#parent

Instance Method Summary collapse

Methods inherited from Scene

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

Constructor Details

#initialize(type = nil) ⇒ Background

Returns a new instance of Background.



36
37
38
39
40
41
# File 'lib/rubysketch/solitaire/background.rb', line 36

def initialize(type = nil)
  super
  @start  = now
  @canvas = createGraphics width, height
  set type || settings['background']&.intern|| types.first
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/rubysketch/solitaire/background.rb', line 43

def type
  @type
end

Instance Method Details

#authorObject



53
54
55
# File 'lib/rubysketch/solitaire/background.rb', line 53

def author()
  TYPES[type][:author]
end

#drawObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rubysketch/solitaire/background.rb', line 83

def draw()
  @canvas.beginDraw do |g|
    sh = @shader
    case type
    when :checker, :checker2
      colors = skin.backgroundCheckerColors
      sh.set :iTime, (type == :checker2 ? 0.0 : now - @start)
      sh.set :color1, *colors[0].map {|n| n / 255.0}
      sh.set :color2, *colors[1].map {|n| n / 255.0}
    else
      sh.set :iTime, now - @start
      sh.set :iResolution, width, height
    end
    g.shader sh
    g.translate 0, g.height
    g.scale 1, -1
    g.rect 0, 0, g.width, g.height
  end
  copy @canvas, 0, 0, @canvas.width, @canvas.height, 0, 0, width, height
end

#nameObject



49
50
51
# File 'lib/rubysketch/solitaire/background.rb', line 49

def name()
  TYPES[type][:name]
end

#nextTypeObject



61
62
63
64
# File 'lib/rubysketch/solitaire/background.rb', line 61

def nextType()
  index = types.index(@type) || 0
  types[(index + 1) % types.size]
end

#set(type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubysketch/solitaire/background.rb', line 66

def set(type)
  type = types.first unless types.include?(type)
  case type
  when :checker, :checker2
    @shader = createShader nil, checker
  when :cosmic2
    @shader = createShader nil, cosmic2
  when :classicPSPWave
    @shader = createShader nil, classicPSPWave
  when :reflectiveHexes
    @shader = createShader nil, reflectiveHexes
  when :colorfulUnderwaterBubbles2
    @shader = createShader nil, colorfulUnderwaterBubbles2
  end
  settings['background'] = @type = type
end

#typesObject



45
46
47
# File 'lib/rubysketch/solitaire/background.rb', line 45

def types()
  TYPES.keys
end

#urlObject



57
58
59
# File 'lib/rubysketch/solitaire/background.rb', line 57

def url()
  TYPES[type][:url]
end