Class IO
In: io.rb
Parent: Object

The video class controls all output to the screen. In the future things might be different, but for now it’s how it works :-)

TODO: The name of this class must be changed, it’s confilcting with the IO class from Ruby.

Methods
destroy    draw_map    event    move_map    new    option_window    refresh_status_bar   
Public Class methods
new()

Starts ncurses engine, creating all windows and stuff…

# File io.rb, line 43
   def initialize
      Ncurses.initscr
      Ncurses.cbreak
      Ncurses.noecho
      Ncurses.curs_set(0)
      Ncurses.start_color
      
      size_x = Array.new
      size_y = Array.new
      Ncurses.getmaxyx(Ncurses::stdscr,size_y,size_x)
      @size_x = size_x[0] - 1
      @size_y = size_y[0] - 6

      map_border = Ncurses.newwin(@size_y+1, @size_x+1, 1, 0)
      map_border.box(0, 0)
      @map_window = Ncurses.newwin(@size_y-1, @size_x-1, 2, 1)
      @map_window.keypad(true)

      @status_window = Ncurses.newwin(4, @size_x+1, @size_y+2, 0)
      @status_window.box(0, 0)

      @top_x = @top_y = 0

      # Colors
      Ncurses.init_pair(C_ENGLAND + C_SEA,Ncurses::COLOR_RED,Ncurses::COLOR_BLUE)
      Ncurses.init_pair(C_ENGLAND + C_GRASS,Ncurses::COLOR_RED,Ncurses::COLOR_GREEN)

      map_border.refresh
      @map_window.refresh
      @status_window.refresh

   end
Public Instance methods
destroy()

Ends ncurses engine, giving a clean exit to the user.

# File io.rb, line 77
   def destroy
      Ncurses.endwin
   end
draw_map()

Redraws the whole (visible) map.

# File io.rb, line 82
   def draw_map
      -1.upto($g.map.size_x) do |x|
         -1.upto($g.map.size_y) do |y|
            draw_tile(x,y)
         end
      end
      refresh
   end
move_map(direction)

Move the map one tile (according to the direction)

# File io.rb, line 92
   def move_map(direction)
      case direction
      when 8
         @top_y += 1
      when 2
         @top_y -= 1
      when 4
         @top_x += 1
      when 6
         @top_x -= 1
      end
      draw_map
   end
event()

Recieves an event, and returns the corresponding action as a constant

# File io.rb, line 107
   def event
      while true
         case @map_window.getch
         when Ncurses::KEY_UP
            return MAP_UP
         when Ncurses::KEY_DOWN
            return MAP_DOWN
         when Ncurses::KEY_LEFT
            return MAP_LEFT
         when Ncurses::KEY_RIGHT
            return MAP_RIGHT
         when 56, 450 # 8
            return UNIT_N
         when 57, 451 # 9
            return UNIT_NE
         when 54, 454 # 6
            return UNIT_E
         when 51, 457 # 3
            return UNIT_SE
         when 50, 456 # 2
            return UNIT_S
         when 49, 455 # 1
            return UNIT_SW
         when 52, 452 # 4
            return UNIT_W
         when 55, 449 # 7
            return UNIT_NW
         when 119, 87 # W
            return NEXT_UNIT
         when 32 # space
            return NEW_ROUND
         end
      end
   end
option_window(text, *options)

Creates a multi-option window and returns the chosen option. Text must be passed as a array (each line = one array level).

# File io.rb, line 144
   def option_window(text, *options)
      w = 0
      text.length.times do |i|
         w = text[i].length if text[i].length > w
      end
      h = text.length + options.length + 1
      w += 2
      h += 2

      t = @size_y/2 - (h/2)
      l = @size_x/2 - (w/2)

      window = Ncurses.newwin(h, w, t , l);
      window.box(0, 0)
      window.keypad(true)

      text.length.times { |i| window.mvaddstr(i+1, 1, text[i]) }

      option = 0

      ch = 0
      while ch != 10
         options.length.times do |i| 
            window.attron(Ncurses::A_REVERSE) if i == option
            window.mvaddstr(i+text.length+2, 1, options[i]) 
            window.attroff(Ncurses::A_REVERSE) if i == option
         end
         window.refresh
         ch = window.getch
         option -= 1 if ch == Ncurses::KEY_UP
         option += 1 if ch == Ncurses::KEY_DOWN
         option = options.length-1 if option == -1
         option = 0 if option == options.length
      end

      window.delwin
      
      redraw_screen

      option
   
   end
refresh_status_bar()

Refreshes the information about the game and the currently focused unit to the status bar.

# File io.rb, line 189
   def refresh_status_bar
      
      # The unit icon (TODO: do it)
      @status_window.mvaddstr(1,1,"X")

      # Unit name
      @status_window.mvaddstr(1,3,\
            $g.nation[$g.current_nation].unit_focused.military.name \
            + "(" + $g.nation[$g.current_nation].unit_focused.x.to_s \
            + "," + $g.nation[$g.current_nation].unit_focused.y.to_s + ") " \
            + "                              ")
      @status_window.mvaddstr(1,30,\
            $g.nation[$g.current_nation].unit_focused.moves_left.to_s + 
            " moves left")

      # Year
      @status_window.mvaddstr(2,3,"Year: " + $g.year.to_s)

      @status_window.refresh
      
   end