Class: GameOverseer::Console

Inherits:
Gosu::Window show all
Defined in:
lib/gameoverseer/console/console.rb

Constant Summary collapse

PENDING_LOG =

TODO: Use Gosu::Window.record to lower number of objects that need to be drawn

[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gameoverseer/console/console.rb', line 6

def initialize
  GameOverseer::Console.instance = self
  super(720, 480, false)
  $window = self
  $window.caption = "GameOverseer Console"
  $window.text_input = Gosu::TextInput.new

  @default_text_instance = Gosu::Font.new($window, 'Consolas', 18)
  @messages = []
  setup_ui
end

Class Method Details

.defer_log(string) ⇒ Object



152
153
154
# File 'lib/gameoverseer/console/console.rb', line 152

def self.defer_log(string)
  PENDING_LOG << string
end

.instanceObject



130
131
132
# File 'lib/gameoverseer/console/console.rb', line 130

def self.instance
  @instance
end

.instance=(_instance) ⇒ Object



134
135
136
# File 'lib/gameoverseer/console/console.rb', line 134

def self.instance=_instance
  @instance = _instance
end

.log(string) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/gameoverseer/console/console.rb', line 138

def self.log(string)
  self.log_it(string) if string.strip.length > 0
  begin
    GameOverseer::Console.instance.submit_text(string, false)
  rescue NoMethodError
    self.defer_log(string)
  end
end

.log_it(string) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gameoverseer/console/console.rb', line 156

def self.log_it(string)
  puts string
  retry_limit = 0
  begin
    @log_file = File.open("#{Dir.pwd}/logs/log-#{Time.now.strftime('%B-%d-%Y')}.txt", 'a+') unless defined? @log_file
    @log_file.write "[#{Time.now.strftime('%c')}] #{string}\n"
  rescue Errno::ENOENT
    Dir.mkdir("#{Dir.pwd}/logs") unless File.exist?("#{Dir.pwd}/logs") && File.directory?("#{Dir.pwd}/logs")
    retry_limit+=1
    retry unless retry_limit >= 2
  end
end

.log_with_color(string, color = Gosu::Color::WHITE) ⇒ Object



147
148
149
150
# File 'lib/gameoverseer/console/console.rb', line 147

def self.log_with_color(string, color = Gosu::Color::WHITE)
  self.log_it(string) if string.strip.length > 0
  GameOverseer::Console.instance.submit_text(string, false, color)
end

Instance Method Details

#button_up(id) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gameoverseer/console/console.rb', line 115

def button_up(id)
  case id
  when 41 # Escape
    # Quit?
  when 40 # Enter
    submit_text($window.text_input.text)
  when 88 # Numpad Enter
    submit_text($window.text_input.text)
  when 259 # Mouse wheel
    scroll(:up)
  when 260  # Mouse wheel
    scroll(:down)
  end
end

#clean_messages(count) ⇒ Object



109
110
111
112
113
# File 'lib/gameoverseer/console/console.rb', line 109

def clean_messages(count)
  if @messages.count >= count
    @messages.delete(@messages.first)
  end
end

#drawObject



22
23
24
# File 'lib/gameoverseer/console/console.rb', line 22

def draw
  @ui_image.draw(0,0,0) if defined?(@ui_image)
end

#draw_rect(x1, y1, x2, y2, color = Gosu::Color::GRAY, z = 2) ⇒ Object



88
89
90
# File 'lib/gameoverseer/console/console.rb', line 88

def draw_rect(x1,y1, x2,y2, color = Gosu::Color::GRAY, z = 2)
  $window.draw_quad(x1, y1, color, x2, y1, color, x1, y2, color, x2, y2, color, z)
end

#draw_uiObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gameoverseer/console/console.rb', line 48

def draw_ui
  draw_rect(0,0, 720, 26, Gosu::Color.rgb(200, 75, 25))
  draw_rect(0,454, 720, 480, Gosu::Color::WHITE)
  text_instance.draw("GameOverSeer Console. GameOverseer version #{GameOverseer::VERSION} #{GameOverseer::RELEASE_NAME} #{@messages.count}", 4, 4, 3)
  @current_text.draw("#{$window.text_input.text}", @current_text_x, 458, 3, 1, 1, Gosu::Color::BLACK)
  draw_rect(@caret+@current_text_x, 456, 2.0+@caret+@current_text_x, 474, Gosu::Color::BLUE, 4) if defined?(@caret) && @render_caret

  @messages.each do |message|
    message[:instance].draw(message[:text],message[:x],message[:y],message[:z], 1, 1, message[:color])
    p message[:color] unless message[:color] == Gosu::Color::WHITE
  end
end

#needs_cursor?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/gameoverseer/console/console.rb', line 18

def needs_cursor?
  true
end

#scroll(direction) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gameoverseer/console/console.rb', line 92

def scroll(direction)
  case direction
  when :down
    if @messages.last[:y] >= 480 - 26 - 18
      @messages.each do |message|
        message[:y]-=18
      end
    end
  when :up
    if @messages.first[:y] <= 26#<= 480 - 26 - 32
      @messages.each do |message|
        message[:y]+=18
      end
    end
  end
end

#setup_uiObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gameoverseer/console/console.rb', line 31

def setup_ui
  @current_text = text_instance
  @current_text_x = 4

  # Required first message
  @messages << {
    text: '',
    instance: text_instance,
    color: Gosu::Color::WHITE,
    x: 4,
    y: 480-26-18,
    z: 1
  }

  submit_text("#{Time.now.strftime('%c')}", false)
end

#text_instanceObject



84
85
86
# File 'lib/gameoverseer/console/console.rb', line 84

def text_instance
  @default_text_instance
end

#updateObject



26
27
28
29
# File 'lib/gameoverseer/console/console.rb', line 26

def update
  update_ui
  @ui_image = $window.record(720, 480) {draw_ui}
end

#update_uiObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gameoverseer/console/console.rb', line 61

def update_ui
  PENDING_LOG.each do |log_message|
    submit_text(log_message, false) if log_message.strip.length > 0
    PENDING_LOG.delete(log_message)
  end

  @caret = @current_text.text_width($window.text_input.text[0...$window.text_input.caret_pos])

  @caret_tick = 0 unless defined?(@caret_tick)
  @render_caret = true if @caret_tick < 15
  @render_caret = false if @caret_tick > 30

  @caret_tick = 0 unless @caret_tick < 45
  @caret_tick+=1

  value = @current_text.text_width($window.text_input.text)+@current_text_x
  if value >= 720
    @current_text_x-=4
  elsif value <= 715
    @current_text_x+=4 unless @current_text_x >= 4
  end
end