cocoslicer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cocoslicer.gemspec
4
+ gem "plist", "~> 3.1.0"
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jtianling
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Cocoslicer
2
+
3
+ Slicer the packed cocos2d resources(with tool TexturePacker) into the original ones. Don't support zwoptex now."escription
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cocoslicer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cocoslicer
18
+
19
+ ## Usage
20
+
21
+ cocoslicer packed.plist
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/cocoslicer ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cocoslicer'
4
+
5
+ Cocoslicer.main()
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cocoslicer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "cocoslicer"
8
+ gem.version = Cocoslicer::VERSION
9
+ gem.authors = ["jtianling"]
10
+ gem.email = ["jtianling@gmail.com"]
11
+ gem.description = %q{Slicer the packed cocos2d resources(with tool TexturePacker) into the original ones. Don't support zwoptex now.}
12
+ gem.summary = %q{Slicer the packed cocos2d resources into the original ones.}
13
+ gem.homepage = "http://www.jtianling.com"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = ["cocoslicer"]
17
+ gem.require_paths = ["lib"]
18
+ gem.required_ruby_version = '>= 1.8.6'
19
+ gem.requirements << 'libmagick, v6.0'
20
+ gem.requirements << 'plist'
21
+ end
@@ -0,0 +1,3 @@
1
+ module Cocoslicer
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cocoslicer.rb ADDED
@@ -0,0 +1,192 @@
1
+ require "cocoslicer/version"
2
+ require 'plist'
3
+
4
+ module Cocoslicer
5
+ class Point
6
+ attr_accessor :x, :y
7
+
8
+ def initialize(x=0, y=0)
9
+ @x = x
10
+ @y = y
11
+ end
12
+
13
+ def import_from_str(str)
14
+ match = @@re.match(str)
15
+ @x = match[1].to_i
16
+ @y = match[2].to_i
17
+ return self
18
+ end
19
+
20
+ def to_cmd_str
21
+ return "+#{@x}+#{@y}"
22
+ end
23
+
24
+ def to_s
25
+ return "{x=#{@x},y=#{@y}}"
26
+ end
27
+
28
+ private
29
+ @@re = /\{(-?\d+),(-?\d+)\}/
30
+
31
+ end
32
+
33
+ class Size
34
+ attr_accessor :width, :height
35
+
36
+
37
+ def initialize(width=0, height=0)
38
+ @width = width
39
+ @height = height
40
+ end
41
+
42
+ def import_from_str(str)
43
+ match = @@re.match(str)
44
+ @width = match[1].to_i
45
+ @height = match[2].to_i
46
+ return self
47
+ end
48
+
49
+ def to_cmd_str(rotated)
50
+ if !rotated
51
+ return "#{@width}x#{@height}"
52
+ else
53
+ return "#{@height}x#{@width}"
54
+ end
55
+ end
56
+
57
+ def to_s
58
+ return "{width=#{@width},height=#{@height}}"
59
+ end
60
+
61
+ private
62
+ @@re = /\{(\d+),(\d+)\}/
63
+ end
64
+
65
+ class Rect
66
+ attr_accessor :orig, :size
67
+
68
+
69
+ def initialize(orig_point = Point.new, size = Size.new)
70
+ @orig = orig_point
71
+ @size = size
72
+ end
73
+
74
+ def import_from_str(str)
75
+ match = @@re.match(str)
76
+
77
+ @orig = Point.new( match[1].to_i, match[2].to_i )
78
+ @size = Size.new( match[3].to_i, match[4].to_i )
79
+ return self
80
+ end
81
+
82
+ def to_cmd_str(rotated)
83
+ return @size.to_cmd_str(rotated) + @orig.to_cmd_str
84
+ end
85
+
86
+ def to_s
87
+ return "{orig=#{@orig},size=#{@size}}"
88
+ end
89
+
90
+ private
91
+ @@re = /\{\{(\d+),(\d+)\},\{(\d+),(\d+)\}\}/
92
+ end
93
+
94
+ class ImageInfo
95
+ attr_accessor :name, :frame, :offset, :rotated, :source_color_rect, :source_size
96
+
97
+ def slice_img(tex_name)
98
+ cmd = "convert #{tex_name} -crop " + frame.to_cmd_str(@rotated)
99
+ if @rotated
100
+ cmd += " -rotate -90"
101
+ end
102
+
103
+ border_size = Size.new()
104
+ border_size.width = (@source_size.width - @frame.size.width) / 2 + @offset.x.abs;
105
+ border_size.height = (@source_size.height - @frame.size.height) / 2 + @offset.y.abs;
106
+ cmd += " -bordercolor none -border #{border_size.to_cmd_str(false)}"
107
+ cmd += " -chop " + get_chop_offset_str()
108
+
109
+ cmd += " #{@name}"
110
+ puts cmd
111
+ puts system cmd
112
+ end
113
+
114
+ def get_chop_offset_str
115
+ str = "#{@offset.x.abs*2}x#{@offset.y.abs*2}"
116
+ if @offset.x > 0
117
+ str += "+#{@source_size.width}"
118
+ else
119
+ str += "+0"
120
+ end
121
+
122
+ if @offset.y > 0
123
+ str += "+0"
124
+ else
125
+ str += "+#{@source_size.height}"
126
+ end
127
+
128
+ end
129
+
130
+ def to_s
131
+ return "name=#{@name},frame=#{@frame},offset=#{@offset},rotated=#{@rotated},source_color_rect=#{@source_color_rect},source_size=#{@source_size}"
132
+ end
133
+ end
134
+
135
+ def self.main
136
+ if ARGV[0] == nil
137
+ puts 'Need a argv as plist filename'
138
+ exit
139
+ end
140
+
141
+ doc = Plist::parse_xml(ARGV[0])
142
+ if doc == nil
143
+ puts ARGV[0] + ' is not a valid plist file.'
144
+ exit
145
+ end
146
+
147
+
148
+ path = ARGV[0].gsub(/(.*)\/(.*)/, '\1/')
149
+
150
+ metadata = doc['metadata']
151
+ tex_name = metadata['realTextureFileName']
152
+ if tex_name != nil then
153
+ tex_name = path + tex_name
154
+ if not FileTest::exist? tex_name then
155
+ puts tex_name + ' is not exist.'
156
+ exit
157
+ end
158
+ else
159
+ tex_name = ARGV[0].gsub(/(.*)\.plist/, '\1.png')
160
+
161
+ if not FileTest::exist? tex_name then
162
+ puts tex_name + ' is not exist.'
163
+ exit
164
+ end
165
+
166
+ end
167
+
168
+ frames = doc['frames']
169
+
170
+ if frames == nil
171
+ puts ARGV[0] + ' is not a valid cocos2d resource plist file.'
172
+ exit
173
+ end
174
+
175
+ infos = []
176
+ frames.each { |key, value|
177
+ puts "#{key} => #{value}"
178
+ info = ImageInfo.new
179
+ info.name = path + key
180
+
181
+ info.frame = Rect.new.import_from_str(value['frame'])
182
+ info.offset = Point.new.import_from_str(value['offset'])
183
+ info.rotated = value['rotated']
184
+ info.source_color_rect = Rect.new.import_from_str(value['sourceColorRect'])
185
+ info.source_size = Size.new.import_from_str(value['sourceSize'])
186
+
187
+ puts info
188
+
189
+ info.slice_img(tex_name)
190
+ }
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocoslicer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jtianling
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Slicer the packed cocos2d resources(with tool TexturePacker) into the
15
+ original ones. Don't support zwoptex now.
16
+ email:
17
+ - jtianling@gmail.com
18
+ executables:
19
+ - cocoslicer
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/cocoslicer
29
+ - cocoslicer.gemspec
30
+ - lib/cocoslicer.rb
31
+ - lib/cocoslicer/version.rb
32
+ homepage: http://www.jtianling.com
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.6
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements:
51
+ - libmagick, v6.0
52
+ - plist
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Slicer the packed cocos2d resources into the original ones.
58
+ test_files: []