Class: Pc::Window

Inherits:
Gosu::Window
  • Object
show all
Includes:
Singleton
Defined in:
lib/pc/interfaces/desktop/window.rb

Instance Method Summary collapse

Constructor Details

#initializeWindow

Returns a new instance of Window.



8
9
10
11
12
# File 'lib/pc/interfaces/desktop/window.rb', line 8

def initialize
  super 640, 480, is_fullscreen
  self.caption = 'Game Hall'
  @font = Gosu::Font.new self, Gosu::default_font_name, 20
end

Instance Method Details

#button_down(id) ⇒ Object

This method is called whenever a key is pressed down



60
61
62
63
64
# File 'lib/pc/interfaces/desktop/window.rb', line 60

def button_down(id)
  case id
    when Gosu::KbEscape then close
  end
end

#button_up(id) ⇒ Object

This method is called whenever a key is pressed up



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pc/interfaces/desktop/window.rb', line 67

def button_up(id)
  case id
  when Gosu::MsLeft
    @buttons.each do |button, index|
      if button[:y1] < mouse_y && mouse_y < button[:y2] && button[:x1] < mouse_x && mouse_x < button[:x2]
        @buttons = nil
        button[:action].call
        break
      end
    end if @buttons
  end
end

#drawObject

This method is called after every update and whenever the OS wants the window to repaint itself.



54
55
56
57
# File 'lib/pc/interfaces/desktop/window.rb', line 54

def draw
  draw_text @alert if @alert
  draw_buttons @buttons if @buttons
end

#draw_buttons(buttons) ⇒ Object



46
47
48
49
50
51
# File 'lib/pc/interfaces/desktop/window.rb', line 46

def draw_buttons(buttons)
  buttons.reject{|button| button[:label] == :next}.each do |button|
    draw_rectangle button
    @font.draw button[:label], button[:x1], button[:y1], 2
  end
end

#draw_rectangle(button) ⇒ Object



39
40
41
42
43
44
# File 'lib/pc/interfaces/desktop/window.rb', line 39

def draw_rectangle(button)
  c1, c2 = [Gosu::Color.new(0xFF1EB1FA), Gosu::Color.new(0xFF1D4DB5)]
  x1, x2, y1, y2 = [button[:x1], button[:x2], button[:y1], button[:y2]]
  z_index = 0
  draw_quad x1, y1, c1, x2, y1, c1, x1, y2, c2, x2, y2, c2, z_index
end

#draw_text(text) ⇒ Object



35
36
37
# File 'lib/pc/interfaces/desktop/window.rb', line 35

def draw_text(text)
  @font.draw @alert, 200, 450, 2 # TO DO: center the text
end

#set_alert(message) ⇒ Object



14
15
16
# File 'lib/pc/interfaces/desktop/window.rb', line 14

def set_alert(message)
  @alert = message
end

#set_buttons(choices = {}) ⇒ Object



18
19
20
21
22
# File 'lib/pc/interfaces/desktop/window.rb', line 18

def set_buttons(choices = {})
  @buttons = choices.map.with_index do |(key, value), index|
    {timeout: Time.now + 1.2, label: key, action: value, x1: 100, x2: 200, y1: 30+index*30, y2: 50+index*30}
  end
end

#updateObject

This method is called once every #update_interval milliseconds while the window is being shown.



25
26
27
28
29
30
31
32
33
# File 'lib/pc/interfaces/desktop/window.rb', line 25

def update
  @buttons.each do |button, index|
    if button[:timeout] < Time.now && button[:label] == :next
      @buttons = nil
      button[:action].call
      break
    end
  end if @buttons
end