class AuthorEngine::SpritePicker

Attributes

active_sprite[R]
columns[R]
height[R]
offset[R]
rows[R]
width[R]
x[R]
y[R]

Public Class Methods

new(x: nil, y:, width: nil, height: nil) click to toggle source
# File lib/author_engine/sprite_picker.rb, line 8
def initialize(x: nil, y:, width: nil, height: nil)
  @x, @y, @width, @height = x, y, width, height
  @sprite_size = window.sprite_size
  @scaled_sprite_size = @sprite_size * window.square_scale
  @active_sprite = 0 # array index

  @width = width ? width : window.width - (@scaled_sprite_size)
  @height= height ? height : @scaled_sprite_size*2

  @x = x ? x : window.width/2 - @width/2

  @columns = (@width  / @scaled_sprite_size).floor
  @rows    = (@height / @scaled_sprite_size).floor

  @offset = 1 * window.square_scale
  @tooltip = AuthorEngine::Text.new(message: "", z: 100)
  @current_page = AuthorEngine::Text.new(message: "Page 0", size: 20, x: window.width/2, z: 100)

  @page = 0
end

Public Instance Methods

button_up(id) click to toggle source
# File lib/author_engine/sprite_picker.rb, line 146
def button_up(id)
  case id
  when Gosu::MsLeft
    if mouse_over?(self)
      select_sprite
    end
  when Gosu::KbLeft
    @page-=1
    @page = (255/(@rows * @columns)) if @page < 0
  when Gosu::KbRight
    @page+=1
    @page = 0 if @page > (255/(@rows * @columns))
  end
end
draw() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 33
def draw
  @current_page.y = @y - 24

  Gosu.draw_rect(@x-@offset, @y-@offset, @width+(@offset*2), @height+(@offset*2), Gosu::Color.rgba(255,255,255,220), 15)
  Gosu.draw_rect(@x, @y, @width, @height, Gosu::Color.rgba(10, 10, 10, 200), 15)
  draw_grid
  draw_sprites

  draw_and_update_tooltip
  @current_page.message = "Page #{@page}"
  @current_page.x = @width/2 - @current_page.width/2
  @current_page.draw
end
draw_and_update_tooltip() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 95
def draw_and_update_tooltip
  found = false

  sprite_block do |x,y,index|
    if @tooltip
      @tooltip.message = "#{index}"
      @tooltip.x = window.mouse_x - @tooltip.width/2
      @tooltip.y = window.mouse_y - @tooltip.height
    end

    found = true
  end

  if found
    Gosu.draw_rect(@tooltip.x - @offset, @tooltip.y - @offset, @tooltip.width+(@offset*2), @tooltip.height+(@offset), Gosu::Color.rgba(0,0,0,100), 100)
    @tooltip.draw
  else
    @tooltip.message = ""
  end
end
draw_grid() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 47
def draw_grid
  (@columns-1).times do |i|
    i += 1
    # Vertical line
    Gosu.draw_rect((@x + (i * @width /  @columns)) - 1, @y, 1, @height, Gosu::Color::WHITE, 17)
  end
  #Horizontal line
  (@rows-1).times do |i|
    Gosu.draw_rect(@x, (@y + (i * @height / @rows)) + @scaled_sprite_size, @width, 1, Gosu::Color::WHITE, 17)
  end
end
draw_sprites() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 59
def draw_sprites
  y = @y
  x = @x
  n = 0
  size  = (@columns * @rows)
  index = size * @page

  SpriteEditor.instance.sprites[index..(index + size)].each_with_index do |sprite, i|
    sprite.draw(x, y, 16, 1.0 * window.square_scale, 1.0 * window.square_scale) if sprite
    x+=@scaled_sprite_size

    if n >= @columns-1
      y+=@scaled_sprite_size
      x = @x
      n = 0
    else
      n += 1
    end
  end

  highlight_sprite
end
highlight_sprite() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 82
def highlight_sprite
  sprite_block do |x, y|
    Gosu.draw_rect(x + @x, y + @y, @sprite_size * window.square_scale, @sprite_size * window.square_scale, Gosu::Color.rgba(0,0,0, 50), 17)
  end
end
mouse_over_sprite?(x, y, width, height) click to toggle source
# File lib/author_engine/sprite_picker.rb, line 88
def mouse_over_sprite?(x, y, width, height)
  if window.mouse_x.between?(x, x + width) &&
     window.mouse_y.between?(y, y + height)
     return true
  end
end
select_sprite() click to toggle source
# File lib/author_engine/sprite_picker.rb, line 116
def select_sprite
  sprite_block do |x, y, index|
    @active_sprite = index
    SpriteEditor.instance.set_sprite
  end
end
sprite_block(&block) click to toggle source
# File lib/author_engine/sprite_picker.rb, line 123
def sprite_block(&block)
  found = false
  index = @page * (@columns * @rows)

  @rows.times do |y|
    _y = y * @scaled_sprite_size
    break if found

    @columns.times do |x|
      break if found
      _x = x * @scaled_sprite_size

      if mouse_over_sprite?(_x + @x, _y + @y, @sprite_size * window.square_scale, @sprite_size * window.square_scale)
        found = true
        block.call(_x, _y, index, found) if block
      end

      break if index >= 255
      index+=1
    end
  end
end
y=(n) click to toggle source
# File lib/author_engine/sprite_picker.rb, line 29
def y=(n)
  @y = n
end