class Core::Tools::WorldmapEditor

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/tools/worldmap_editor.rb, line 25
def initialize
  super(1024, 768, false)
  self.caption = "Essytas Worldmap Editor"
  Core.window = self
  @font = Core.font(Core::DEFAULT_FONT, 20)
  @xoff = @yoff = 0
  @unpress = []
  @gui = {
    :list => Core::GUI::Container.new(0, 0, 192, 768, 24)
  }
  @maps = []
  start = Time.now.sec
  Dir.new("maps/").each { |file|
    next if File.directory?(file) or File.extname(file) != ".tmx"
    map = Core::Parse.map(file.sub(".tmx", ""), true)
    if !map.properties[:name] or map.properties[:name].empty?
      map.properties[:name] = file.sub(".tmx", "")
    end
    map = AbstractMap.new(map.properties[:name], file.sub(".tmx", ""))
    @maps.push(map)
    @gui[:list].add(Core::GUI::Button.new(0, 0, 168, 24, map.name, lambda { clicked(map) }, true, :left))
  }
  puts("INFO: Loaded #{@maps.size} maps in #{Time.now.sec - start} seconds")
  @world = load_world
end

Public Instance Methods

check_attribute(doc, attribute) click to toggle source
# File lib/tools/worldmap_editor.rb, line 117
def check_attribute(doc, attribute)
  if !doc.root.elements['/map/properties/property[@name="'+attribute+'"]']
    props = doc.root.elements['/map/properties[1]']
    if !props
      props = Element.new('properties')
      doc.root.elements['/map'] << props
      props = doc.root.elements['/map/properties[1]']
    end
    prop = Element.new("property")
    prop.add_attribute("name", "#{attribute}")
    prop.add_attribute("value", "")
    props << prop
    doc.root.elements.add(props)
  else
    puts("found attribute #{attribute}")
  end
end
clicked(map) click to toggle source
# File lib/tools/worldmap_editor.rb, line 183
def clicked(map)
  @current = map
end
draw() click to toggle source
# File lib/tools/worldmap_editor.rb, line 153
def draw
  @gui.each_value { |el|
    el.draw
  }
  y = 0
  @world.size.times {
    x = 0
    @world.size.times {
      if @world[y][x]
        px = 192 + @xoff + (x * 32)
        py = @yoff + (y * 24)
        c = Gosu::Color::GRAY
        draw_quad(px, py, c, px+32, py, c, px+32, py+24, c, px, py+24, c) 
      end
      x += 1
    }
    y += 1
  }
  x = (mouse_x.to_i - 192) / 32
  y = mouse_y.to_i / 24
  if @world[y] and @world[y][x]
    c = Gosu::Color::WHITE
    @font.draw(@world[y][x].name, mouse_x + 12, mouse_y + 12, 200, 1, 1, Gosu::Color::BLACK)
    w = @font.text_width(@world[y][x].name) + 8
    x = mouse_x + 8
    y = mouse_y + 8
    draw_quad(x, y, c, x+w, y, c, x+w, y+24, c, x, y+24, c, 100)
  end
end
load_world() click to toggle source
# File lib/tools/worldmap_editor.rb, line 64
def load_world
  begin
    file = File.open("tools/world.map", "r")
    world = Marshal.load(file)
    file.close
    return world
  rescue
    return Array.new(@maps.size) { Array.new(@maps.size) }
  end
end
needs_cursor?() click to toggle source
# File lib/tools/worldmap_editor.rb, line 207
def needs_cursor?
  return true
end
place_map(mx, my) click to toggle source
# File lib/tools/worldmap_editor.rb, line 135
def place_map(mx, my)
  x = mx.to_i/32
  y = my.to_i/24
  if y >= @world.size or x >= @world.first.size
    return
  end
  @world[y][x] = @current
end
pressed?(key) click to toggle source
# File lib/tools/worldmap_editor.rb, line 187
def pressed?(key)
  p = button_down?(key)
  if p
    if @unpress.include?(key)
      p = false
    else
      @unpress.push(key)
    end
  end
  return p
end
property_element(doc, attribute) click to toggle source
# File lib/tools/worldmap_editor.rb, line 113
def property_element(doc, attribute)
  return doc.root.elements['/map/properties/property[@name="'+attribute+'"]']
end
remove_map(mx, my) click to toggle source
# File lib/tools/worldmap_editor.rb, line 144
def remove_map(mx, my)
  x = mx.to_i/32
  y = my.to_i/24
  if y >= @world.size or x >= @world.first.size
    return
  end
  @world[y][x] = nil
end
save_world() click to toggle source
# File lib/tools/worldmap_editor.rb, line 75
def save_world
  Marshal.dump(@world, File.open("tools/world.map", "w"))
  y = 0
  @world.size.times {
    x = 0
    @world[y].size.times {
      if @world[y][x]
        @world[y][x].left = @world[y][x - 1] if x > 0
        @world[y][x].right = @world[y][x + 1] if x < @world[y].size
        @world[y][x].upper = @world[y - 1][x] if y > 0
        @world[y][x].lower = @world[y + 1][x] if y < @world.size
        doc = Document.new(File.open("maps/#{@world[y][x].file}.tmx"))
        doc.context[:attribute_quote] = :quote
        
        if @world[y][x].upper
          check_attribute(doc, "upper")
          property_element(doc, "upper").add_attribute("value", @world[y][x].upper.file)
        end
        if @world[y][x].lower
          check_attribute(doc, "lower")
          property_element(doc, "lower").add_attribute("value", @world[y][x].lower.file)
        end
        if @world[y][x].left
          check_attribute(doc, "left")
          property_element(doc, "left").add_attribute("value", @world[y][x].left.file)
        end
        if @world[y][x].right
          check_attribute(doc, "right")
          property_element(doc, "right").add_attribute("value", @world[y][x].right.file)
        end
        doc.write(File.open("maps/#{@world[y][x].file}.tmx", "w"))
      end
      x += 1
    }
    y += 1
  }
end
unpress() click to toggle source
# File lib/tools/worldmap_editor.rb, line 199
def unpress
  @unpress.each { |key|
    if !button_down?(key)
      @unpress.delete(key)
    end
  }
end
update() click to toggle source
# File lib/tools/worldmap_editor.rb, line 51
def update
  @gui.each_value { |el|
    el.update
  }
  if @current and pressed?(Gosu::MsLeft) and mouse_x > 192
    place_map(mouse_x - 192, mouse_y)
  end
  if pressed?(Gosu::MsRight) and mouse_x > 192
    remove_map(mouse_x - 192, mouse_y)
  end
  unpress
end