Module: DXRuby::Tiled::TMXLoader
- Defined in:
- lib/dxruby_tiled/tmx_loader.rb
Class Method Summary collapse
- .animation_to_array(animation_element) ⇒ Object
- .element_to_hash(elm, attrs_i = [], attrs_f = [], attrs_b = []) ⇒ Object
- .image_to_hash(image_element) ⇒ Object
- .layers_to_array(layers_parent_element) ⇒ Object
- .load_tmx(tmxfile, encoding = Encoding::UTF_8, dir = nil) ⇒ Object
- .object_to_hash(object_element) ⇒ Object
- .objectgroup_to_hash(objectgroup_element) ⇒ Object
- .properties_parent_to_hash(element) ⇒ Object
- .read_xmlfile(xmlfile, encoding = Encoding::UTF_8) ⇒ Object
- .tileset_to_hash(tileset_element) ⇒ Object
- .tmx_to_hash(tmx) ⇒ Object
- .tsx_to_hash(tsx) ⇒ Object
- .tx_to_hash(tx) ⇒ Object
Class Method Details
.animation_to_array(animation_element) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 159 def animation_to_array(animation_element) animations = [] animation_element.each_element("frame") do |frame| animations.push({ tileid: frame.attribute("tileid" ).to_s.to_i, duration: frame.attribute("duration").to_s.to_i }) end animations end |
.element_to_hash(elm, attrs_i = [], attrs_f = [], attrs_b = []) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 20 def element_to_hash(elm, attrs_i = [], attrs_f = [], attrs_b = []) hash = {} elm.attributes.each_pair do |key, value| str = value.to_s if attrs_i.include?(key) hash[key.to_sym] = str.to_i elsif attrs_f.include?(key) hash[key.to_sym] = str.to_f elsif attrs_b.include?(key) hash[key.to_sym] = !!str && str != "0" && str != "false" else hash[key.to_sym] = str end end hash end |
.image_to_hash(image_element) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 97 def image_to_hash(image_element) hash = {} image_hash = element_to_hash(image_element, %w[width height]) hash[:image] = image_hash[:source] if image_hash.has_key?(:source) hash[:imagewidth] = image_hash[:width ] if image_hash.has_key?(:width ) hash[:imageheight] = image_hash[:height] if image_hash.has_key?(:height) hash[:transparentcolor] = image_hash[:trans ] if image_hash.has_key?(:trans ) hash end |
.layers_to_array(layers_parent_element) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 172 def layers_to_array(layers_parent_element) layers_array = [] layers_parent_element.each_child do |layer_element| next unless layer_element.is_a?(REXML::Element) layer_hash = element_to_hash(layer_element, %w[x y width height offsetx offsety], %w[opacity], %w[visible] ) layer_hash.merge!(properties_parent_to_hash(layer_element)) case layer_element.name.to_s when "layer" layer_hash[:type] = "tilelayer" data = layer_element.get_elements("data").first data_hash = element_to_hash(data) layer_hash[:compression] = data_hash[:compression] if data_hash.has_key?(:compression) layer_hash[:encoding ] = data_hash[:encoding ] if data_hash.has_key?(:encoding ) chunks = data.get_elements("chunk") if chunks.empty? if layer_hash[:encoding] == "csv" layer_hash[:data] = data.text.strip.split(",").map(&:to_i) else layer_hash[:data] = data.text end else layer_hash[:chunks] = [] chunks.each do |chunk| chunk_hash = element_to_hash(chunk, %w[x y width height]) if layer_hash[:encoding] == "csv" chunk_hash[:data] = chunk.text.strip.split(",").map(&:to_i) else chunk_hash[:data] = chunk.text end layer_hash[:chunks].push(chunk_hash) end end when "objectgroup" layer_hash[:type] = "objectgroup" layer_hash.merge!(objectgroup_to_hash(layer_element)) when "imagelayer" layer_hash[:type] = "imagelayer" layer_element.each_element("image") do |image_element| layer_hash.merge!(image_to_hash(image_element)) end when "group" layer_hash[:type] = "group" layer_hash[:layers] = layers_to_array(layer_element) else next end layers_array.push(layer_hash) end layers_array end |
.load_tmx(tmxfile, encoding = Encoding::UTF_8, dir = nil) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 9 def load_tmx(tmxfile, encoding = Encoding::UTF_8, dir = nil) Map.new( TMXLoader.tmx_to_hash(TMXLoader.read_xmlfile(tmxfile, encoding)), dir || File.dirname(tmxfile) ) end |
.object_to_hash(object_element) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 124 def object_to_hash(object_element) object_hash = { rotation: 0, visible: true } object_hash.merge!(element_to_hash(object_element, %w[id gid], %w[x y width height rotation opacity], %w[visible] )) object_hash.merge!(properties_parent_to_hash(object_element)) object_hash[:ellipse] = true unless object_element.get_elements("ellipse").empty? object_hash[:point] = true unless object_element.get_elements("point").empty? object_element.each_element("polygon") do |polygon_element| object_hash[:polygon] = polygon_element.attribute("points").to_s.split(" ").map do |point| x, y = point.split(",").map(&:to_f) { x: x, y: y } end end object_element.each_element("polyline") do |polyline_element| object_hash[:polyline] = polyline_element.attribute("points").to_s.split(" ").map do |point| x, y = point.split(",").map(&:to_f) { x: x, y: y } end end object_element.each_element("text") do |text_element| object_hash[:text] = element_to_hash(text_element, %w[pixelsize], %w[], %w[wrap bold italic underline strikeout kerning] ) object_hash[:text][:text] = text_element.text end object_hash end |
.objectgroup_to_hash(objectgroup_element) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 109 def objectgroup_to_hash(objectgroup_element) objectgroup_hash = element_to_hash(objectgroup_element, %w[x y width height offsetx offsety], %w[opacity], %w[visible] ) objectgroup_hash.merge!(properties_parent_to_hash(objectgroup_element)) objectgroup_hash[:objects] = [] objectgroup_element.each_element("object") do |object_element| objectgroup_hash[:objects].push(object_to_hash(object_element)) end objectgroup_hash end |
.properties_parent_to_hash(element) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 39 def properties_parent_to_hash(element) properties_hash = { properties: {}, propertytypes: {} } element.each_element("properties") do |properties| properties.each_element_with_attribute("type") do |property| name = property.attribute("name" ).to_s.to_sym type = property.attribute("type" ).to_s value = property.attribute("value").to_s case type when "string", "color", "file" properties_hash[:properties][name] = value when "int" properties_hash[:properties][name] = value.to_i when "float" properties_hash[:properties][name] = value.to_f when "bool" properties_hash[:properties][name] = !!value && value != "0" && value != "false" end properties_hash[:propertytypes][name] = type end end properties_hash[:properties].empty? ? {} : properties_hash end |
.read_xmlfile(xmlfile, encoding = Encoding::UTF_8) ⇒ Object
16 17 18 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 16 def read_xmlfile(xmlfile, encoding = Encoding::UTF_8) REXML::Document.new(DXRuby::Tiled.read_file(xmlfile, encoding)) end |
.tileset_to_hash(tileset_element) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 64 def tileset_to_hash(tileset_element) tileset_hash = element_to_hash(tileset_element, %w[firstgid tilewidth tileheight spacing margin tilecount columns] ) tileset_hash.merge!(properties_parent_to_hash(tileset_element)) tileset_element.each_element("tileoffset") do |tileoffset| tileset_hash[:tileoffset] = { x: tileoffset.attribute("x").to_s.to_i, y: tileoffset.attribute("y").to_s.to_i } end tileset_element.each_element("image") do |image_element| tileset_hash.merge!(image_to_hash(image_element)) end tileset_hash[:tiles] = {} tileset_element.each_element("tile") do |tile_element| hash = element_to_hash(tile_element) tile_hash = { type: hash[:type] } tile_element.each_element("image") do |image_element| tile_hash.merge!(image_to_hash(image_element)) end tile_element.each_element("objectgroup") do |objectgroup_element| tile_hash[:objectgroup] = objectgroup_to_hash(objectgroup_element) end tile_element.each_element("animation") do |animation_element| tile_hash[:animation] = animation_to_array(animation_element) end tileset_hash[:tiles][hash[:id]] = tile_hash end tileset_hash end |
.tmx_to_hash(tmx) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 230 def tmx_to_hash(tmx) map_element = tmx.root hash = element_to_hash(map_element, %w[width height tilewidth tileheight hexsidelength nextobjectid], %w[version], %w[] ) hash.merge!(properties_parent_to_hash(map_element)) hash[:tilesets] = [] map_element.each_element("tileset") do |tileset_element| hash[:tilesets].push(tileset_to_hash(tileset_element)) end hash[:layers] = layers_to_array(map_element) hash end |
.tsx_to_hash(tsx) ⇒ Object
247 248 249 250 251 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 247 def tsx_to_hash(tsx) hash = tileset_to_hash(tsx.root) hash end |
.tx_to_hash(tx) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/dxruby_tiled/tmx_loader.rb', line 253 def tx_to_hash(tx) hash = {} tx.root.each_element("tileset") do |tileset_element| hash[:tileset] = element_to_hash(tileset_element, %w[firstgid], %w[], %w[] ) end tx.root.each_element("object") do |object_element| hash[:object] = object_to_hash(object_element) end hash end |