Report abuse

Copyright (C) 2006 by Han Dao


			
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#This program is a map editor.
#You can contract the author at wikipediankiba@gmail.com
class Edit
  def initialize name
    @name = name
    @screen = Rubygame::Screen.set_mode([800,600],0, [Rubygame::HWSURFACE, Rubygame::DOUBLEBUF])
    @screen.show_cursor = false
    @background = Rubygame::Surface.new(@screen.size)
    @image = []
    @map
    @mouse = Mouse_input.new
  end
  def new_file file
    save(file , @name)
    @map = read(@name)
    image
    edit_mode
  end
  def load
    @map = read(@name)
    image
    edit_mode
  end
  def read filename
    yaml_string = File.read filename; YAML:: load yaml_string
  end
  def save object , filename
    File.open filename , 'w' do |f|
      f.write(object.to_yaml)
    end
  end
  def image
    #load image files according to the strings it read in @map['file']
    @map['file'].each do |file|
      @image <<  Rubygame::Image.load("data/maps/#{file}")
    end
    map
  end
  def map
    x = y = time = 0
    @map['points'].each do |render|  
      @image[render].blit(@screen,[x,y])
      @image[render].blit(@background,[x,y])
      x += 50
      time += 1
      if time == 16
        y += 50
        x = 0
        time = 0
      end
    end
  end
  def change event
    #Compare x and y  until the value are greater and then map the last points.
    #Purpose is to determine the relative locations of the mouse in which squares
    #Start at negative 1 because @map['points'] contains array element and array start with 0
    max = 0
    square = -1
    while (1)
      #Deal with x position; square increase by 1
      if event.pos[0] >= max
        max +=50
        square += 1
      else
        #So the square will appear where the mouse is..relatively
        square -=16
        max = 0
        break
      end
    end
    while (1)
      #Deal with y position; square increase by 16
      if event.pos[1] >= max
        max +=50
        square += 16
      else
        break
      end
    end
    if @map['points'][square] == nil
      @map['points'][square] = 0
    else
      @map['points'][square] += 1
      if @map['points'][square] == 7
        @map['points'][square] = 0
      end
    end
    map
  end
  def edit_mode
    q = Rubygame::Queue.instance()
    while 1
      q.get.each do |event|
        case event
          when Rubygame::MouseMotionEvent
            @mouse.pos(event)
          when Rubygame::MouseDownEvent
            case event.button
              when Rubygame::MOUSE_LEFT
                change(event)
            end
          when Rubygame::KeyDownEvent
            case event.key
              when Rubygame::K_ESCAPE
                save(@map , @name)
                exit
            end  
          when Rubygame::QuitEvent
            exit
        end
      end
      @mouse.undraw(@screen,@background)
      @mouse.update
      @mouse.draw(@screen)
      @screen.flip
    end
  end
end