class Core::GUI::Window

Attributes

close[R]
h[R]
parent[RW]
state[R]
w[R]
x[R]
xoff[RW]

height includes titlebar

y[R]
yoff[RW]

height includes titlebar

zoff[RW]

height includes titlebar

Public Class Methods

new(x, y, w, h, titlestr, close=true, bg=nil, move=false, z=-1) click to toggle source
# File lib/gui/base.rb, line 11
def initialize(x, y, w, h, titlestr, close=true, bg=nil, move=false, z=-1)
  @state = Core.window.state
  @xoff = @yoff = @zoff = 0
  if bg
    @bg = Core.sprite(bg)
  else
    @bg = nil
  end
  @x, @y, @w, @h, @z = x, y, w, h, z
  if @z < 0
    @z = Core::GUI_Z
  end
  @elements = {}
  @bar = [
    Core.sprite("gui/bar_left", true),
    Core.sprite("gui/bar_center", true),
    Core.sprite("gui/bar_right", true)
  ]
  @titlefont = Core.font("arial", 24)
  @title = titlestr
  @tfw = @titlefont.text_width(@title)
  @remove = false
  if close
    @close = ImageButton.new(@w-24, @y, "gui/button_close", lambda { @remove = true })
  else
    @close = nil
  end
  @move = move
  @changed = false
  @dragging = false
  @parent = nil
  @save_pos = false
end

Public Instance Methods

[](sym) click to toggle source
# File lib/gui/base.rb, line 50
def [](sym)
  return get(sym)
end
add(name, el) click to toggle source

Add an element

# File lib/gui/base.rb, line 68
def add(name, el)
  @elements.store(name, el)
end
close!() click to toggle source
# File lib/gui/base.rb, line 56
def close!
  @remove = true
end
draw() click to toggle source
# File lib/gui/base.rb, line 124
def draw
  @bg.draw(@x, @y+24, @z, @w/@bg.width.to_f, @h/@bg.height.to_f) if @bg
  @elements.each_value { |el|
    el.xoff = @x
    el.yoff = @y + 24 # titlebar
    el.draw
  }
  @bar[0].draw(@x, @y, @z)
  @bar[1].draw(@x+16, @y, @z, (@w-32)/@bar[1].width)
  @bar[2].draw(@x+@w-16, @y, @z)
  @titlefont.draw(@title, @x+(@w/2)-(@tfw/2), @y+1, @z+1, 1, 1, Gosu::Color::BLACK)
  @close.draw if @close
end
empty() click to toggle source
# File lib/gui/base.rb, line 44
def empty
  @elements = {}
end
get(sym) click to toggle source
# File lib/gui/base.rb, line 53
def get(sym)
  return @elements[sym]
end
include?(sym) click to toggle source
# File lib/gui/base.rb, line 47
def include?(sym)
  return get(sym) != nil
end
remove?() click to toggle source
# File lib/gui/base.rb, line 59
def remove?
  return @remove
end
save_pos(xsym, ysym) click to toggle source

Enable position saving

# File lib/gui/base.rb, line 73
def save_pos(xsym, ysym)
  @xsym, @ysym = xsym, ysym
  @save_pos = true
end
title=(str) click to toggle source

Set the title

# File lib/gui/base.rb, line 63
def title=(str)
  @title = str
  @tfw = @titlefont.text_width(@title)
end
update() click to toggle source
# File lib/gui/base.rb, line 78
def update
  @elements.each_value { |el|
    el.update
    if el.remove?
      @elements.delete(@elements.key(el))
    end
  }
  if @close
    @close.x, @close.y, @close.zoff = @x+@w-24, @y, @zoff
    @close.update
  end
  if @move
    m = [Core.window.mouse_x, Core.window.mouse_y]
    if Core.inside?(m.x, m.y, @x, @y, @x+@w-24, @y+24) or @dragging
      if Core.window.button_down?(Gosu::MsLeft)
        if !@dragging
          @dragging = true
          @dragx = m.x - @x
          @dragy = m.y - @y
        else
          @x = m.x - @dragx
          @y = m.y - @dragy
        end
      else
        @dragging = false
      end
    end
  end
  if @dragging
    if @x > 1024 - @w
      @x = 1024 - @w
    elsif @x < 0
      @x = 0
    end
    if @y > 744
      @y = 744
    elsif @y < 0
      @y = 0
    end
  end
  if @save_pos
    Core.config[@xsym] = @x
    Core.config[@ysym] = @y
  end
end