Class: Core::GUI::Grid
Overview
Allows EH::GUI::Elements to be placed into a 32x32 px grid. Elements can be selected and dragged
Instance Attribute Summary collapse
-
#selected ⇒ Object
readonly
Returns the value of attribute selected.
Attributes inherited from Element
#h, #w, #x, #xoff, #y, #yoff, #zoff
Instance Method Summary collapse
-
#add(obj) ⇒ Object
Adds obj to the grid at the next free position Returns the position where obj was inserted.
-
#add_at(obj, tx, ty) ⇒ Object
Adds obj at the given position, overwriting any other value Calls add() if the given position is invalid.
- #clear ⇒ Object
- #draw ⇒ Object
- #empty? ⇒ Boolean
- #get_at(tx, ty) ⇒ Object
-
#initialize(x, y, tx, ty, in_classes = []) ⇒ Grid
constructor
A new instance of Grid.
- #mouse_to_tile(mx, my) ⇒ Object
- #update ⇒ Object
Methods inherited from Element
Constructor Details
#initialize(x, y, tx, ty, in_classes = []) ⇒ Grid
Returns a new instance of Grid.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gui/grid.rb', line 10 def initialize(x, y, tx, ty, in_classes=[]) super(x, y, tx*32, ty*32) @classes = in_classes @grid = Array.new(tx) { Array.new(ty) } @bg = Core.sprite("gui/container_background") @tile = Core.sprite("gui/tile_32") @hl = Core.sprite("gui/tile_32_highlight") @selected = nil @highlight = nil @hovered = nil end |
Instance Attribute Details
#selected ⇒ Object (readonly)
Returns the value of attribute selected.
8 9 10 |
# File 'lib/gui/grid.rb', line 8 def selected @selected end |
Instance Method Details
#add(obj) ⇒ Object
Adds obj to the grid at the next free position Returns the position where obj was inserted
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gui/grid.rb', line 62 def add(obj) if @classes.include?(obj.class) x = 0 @grid.each { |col| y = 0 col.each { |tile| if tile == nil @grid[x][y] = obj return [x, y] end y += 1 } x += 1 } end return [0, 0] end |
#add_at(obj, tx, ty) ⇒ Object
Adds obj at the given position, overwriting any other value Calls add() if the given position is invalid
82 83 84 85 86 87 88 89 |
# File 'lib/gui/grid.rb', line 82 def add_at(obj, tx, ty) if @classes.include?(obj.class) @grid[tx][ty] = obj end rescue NoMethodError warn("WARNING: Tried to insert element at invalid index (#{obj}, #{tx}|#{ty})") add(obj) end |
#clear ⇒ Object
112 113 114 |
# File 'lib/gui/grid.rb', line 112 def clear @grid = Array.new(@w/32) { Array.new(@h/32) } end |
#draw ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gui/grid.rb', line 39 def draw x = @x + @xoff y = @y + @yoff @bg.draw(x, y, Core::GUI_Z + @zoff, @w/@bg.width.to_f, @h/@bg.height.to_f) @grid.each { |col| col.each { |tile| tile.icon.draw(x, y, Core::GUI_Z + @zoff + 1) if tile @tile.draw(x, y, Core::GUI_Z + @zoff + 1) y += 32 } x += 32 y = @y + @yoff } if @selected and @highlight @hl.draw(@x+@xoff+@highlight.x*32, @y+@yoff+@highlight.y*32, Core::GUI_Z + @zoff + 2) end if @hovered and (!@highlight or !(@hovered.x == @highlight.x and @hovered.y == @highlight.y)) @hl.draw(@x+@xoff+@hovered.x*32, @y+@yoff+@hovered.y*32, Core::GUI_Z + @zoff + 2) end end |
#empty? ⇒ Boolean
103 104 105 106 107 108 109 110 |
# File 'lib/gui/grid.rb', line 103 def empty? @grid.each { |col| col.each { |tile| return false if tile } } return true end |
#get_at(tx, ty) ⇒ Object
91 92 93 94 95 |
# File 'lib/gui/grid.rb', line 91 def get_at(tx, ty) return @grid[tx][ty] rescue NoMethodError return nil end |
#mouse_to_tile(mx, my) ⇒ Object
97 98 99 100 101 |
# File 'lib/gui/grid.rb', line 97 def mouse_to_tile(mx, my) x = (mx - @x - @xoff).to_i / 32 y = (my - @y - @yoff).to_i / 32 return [x, y] end |
#update ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gui/grid.rb', line 22 def update if Core.mouse_inside?(@x+@xoff, @y+@yoff, @x+@xoff+@w, @y+@yoff+@h) if Core.window.pressed?(Gosu::MsLeft) @highlight = mouse_to_tile(Core.window.mouse_x, Core.window.mouse_y) @selected = get_at(@highlight.x, @highlight.y) else @hovered = mouse_to_tile(Core.window.mouse_x, Core.window.mouse_y) end else @hovered = nil if Core.window.(Gosu::MsLeft) @selected = nil @highlight = nil end end end |