Class: Core::GUI::Window

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

Overview

make hash?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, w, h, titlestr, close = true, bg = nil, move = false, z = -1)) ⇒ Window

Returns a new instance of Window.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#closeObject (readonly)

Returns the value of attribute close.



7
8
9
# File 'lib/gui/base.rb', line 7

def close
  @close
end

#hObject (readonly)

Returns the value of attribute h.



7
8
9
# File 'lib/gui/base.rb', line 7

def h
  @h
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/gui/base.rb', line 10

def parent
  @parent
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/gui/base.rb', line 7

def state
  @state
end

#wObject (readonly)

Returns the value of attribute w.



7
8
9
# File 'lib/gui/base.rb', line 7

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/gui/base.rb', line 7

def x
  @x
end

#xoffObject

height includes titlebar



9
10
11
# File 'lib/gui/base.rb', line 9

def xoff
  @xoff
end

#yObject (readonly)

Returns the value of attribute y.



7
8
9
# File 'lib/gui/base.rb', line 7

def y
  @y
end

#yoffObject

height includes titlebar



9
10
11
# File 'lib/gui/base.rb', line 9

def yoff
  @yoff
end

#zoffObject

height includes titlebar



9
10
11
# File 'lib/gui/base.rb', line 9

def zoff
  @zoff
end

Instance Method Details

#[](sym) ⇒ Object



50
51
52
# File 'lib/gui/base.rb', line 50

def [](sym)
  return get(sym)
end

#add(name, el) ⇒ Object

Add an element



68
69
70
# File 'lib/gui/base.rb', line 68

def add(name, el)
  @elements.store(name, el)
end

#close!Object



56
57
58
# File 'lib/gui/base.rb', line 56

def close!
  @remove = true
end

#drawObject



124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

#emptyObject



44
45
46
# File 'lib/gui/base.rb', line 44

def empty
  @elements = {}
end

#get(sym) ⇒ Object



53
54
55
# File 'lib/gui/base.rb', line 53

def get(sym)
  return @elements[sym]
end

#include?(sym) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gui/base.rb', line 47

def include?(sym)
  return get(sym) != nil
end

#remove?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gui/base.rb', line 59

def remove?
  return @remove
end

#save_pos(xsym, ysym) ⇒ Object

Enable position saving



73
74
75
76
# File 'lib/gui/base.rb', line 73

def save_pos(xsym, ysym)
  @xsym, @ysym = xsym, ysym
  @save_pos = true
end

#title=(str) ⇒ Object

Set the title



63
64
65
66
# File 'lib/gui/base.rb', line 63

def title=(str)
  @title = str
  @tfw = @titlefont.text_width(@title)
end

#updateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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