Class: AuthorEngine::SpritePicker

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/author_engine/sprite_picker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#code_editor, #mouse_over?, #sprite_editor, #window

Constructor Details

#initialize(x: nil, y:, width: nil, height: nil) ⇒ SpritePicker

Returns a new instance of SpritePicker.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#active_spriteObject (readonly)

Returns the value of attribute active_sprite.



5
6
7
# File 'lib/author_engine/sprite_picker.rb', line 5

def active_sprite
  @active_sprite
end

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'lib/author_engine/sprite_picker.rb', line 7

def columns
  @columns
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/author_engine/sprite_picker.rb', line 6

def height
  @height
end

#offsetObject (readonly)

Returns the value of attribute offset.



7
8
9
# File 'lib/author_engine/sprite_picker.rb', line 7

def offset
  @offset
end

#rowsObject (readonly)

Returns the value of attribute rows.



7
8
9
# File 'lib/author_engine/sprite_picker.rb', line 7

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/author_engine/sprite_picker.rb', line 6

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/author_engine/sprite_picker.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/author_engine/sprite_picker.rb', line 6

def y
  @y
end

Instance Method Details

#button_up(id) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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

#drawObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_tooltipObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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_gridObject



47
48
49
50
51
52
53
54
55
56
57
# 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_spritesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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_spriteObject



82
83
84
85
86
# 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) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
# 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_spriteObject



116
117
118
119
120
121
# 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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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