Class: Core::GUI::Container

Inherits:
Element show all
Defined in:
lib/gui/base.rb

Overview

scrollable

Direct Known Subclasses

Inventory

Instance Attribute Summary collapse

Attributes inherited from Element

#h, #w, #x, #xoff, #y, #yoff, #zoff

Instance Method Summary collapse

Methods inherited from Element

#remove?

Constructor Details

#initialize(x, y, w, h, ch) ⇒ Container

ch = content element height



160
161
162
163
164
165
166
167
168
# File 'lib/gui/base.rb', line 160

def initialize(x, y, w, h, ch)
  super(x, y, w, h)
  @bg = Core.sprite("gui/container_background")
  @scrollbar = Scrollbar.new(x+w-24, y, 24, h)
  @ch = ch
  @items = []
  @item = nil
  @changed = false
end

Instance Attribute Details

#chObject (readonly)

Returns the value of attribute ch.



158
159
160
# File 'lib/gui/base.rb', line 158

def ch
  @ch
end

Instance Method Details

#add(element) ⇒ Object



170
171
172
173
174
# File 'lib/gui/base.rb', line 170

def add(element)
  element.x = @x
  element.y = @y + @yoff + ((@items.size+1)*@ch)
  @items.push(element)
end

#changed?Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
# File 'lib/gui/base.rb', line 217

def changed?
  if @changed
    @changed = false
    return true
  else
    return false
  end
end

#drawObject



202
203
204
205
206
207
208
209
210
211
# File 'lib/gui/base.rb', line 202

def draw
  @scrollbar.draw
  @bg.draw(@x+@xoff, @y+@yoff, Core::GUI_Z, (@w-24)/@bg.width.to_f, @h/@bg.height.to_f)
  @items.each { |item|
    if item.y + item.yoff + item.h < @scrollbar.y + @scrollbar.yoff
      next
    end
    item.draw
  }
end

#hoveredObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/gui/base.rb', line 176

def hovered
  i = 0
  @items.each { |it|
    if it.hovered?
      return i
    end
    i += 1
  }
  return -1
end

#selectedObject



213
214
215
# File 'lib/gui/base.rb', line 213

def selected
  return @item
end

#updateObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gui/base.rb', line 187

def update
  # TODO scrollbar probably doesnt work
  @scrollbar.update
  @scrollbar.xoff, @scrollbar.yoff, @scrollbar.zoff = @xoff, @yoff, @zoff
  offset = @scrollbar.offset * -@ch
  @items.each { |item|
    item.xoff, item.zoff = @xoff, @zoff
    item.yoff = @yoff# + (offset/@ch) - @ch
    if item.y + item.yoff + item.h < @scrollbar.y + @scrollbar.yoff
      next
    end
    item.update
  }
end