Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
NSCoder committed Jul 21, 2013
1 parent 6abbc2c commit 0bbc5db
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 0 deletions.
26 changes: 26 additions & 0 deletions command/command/image.rb
@@ -0,0 +1,26 @@
module Joybox
module Command
class Image

def initialize(file_path, suffix)
@file_path = file_path
@suffix = suffix
end

def scale(scale_factor)
retina_file_path = File.basename(@file_path, '.*') + @suffix + File.extname(@file_path)

if File.exists? retina_file_path
Motion::Project::App.log 'Warning', "Tileset image #{retina_file_path} already exists"
return
end

FileUtils.copy(@file_path, retina_file_path)
image_height = `sips -g pixelHeight #{retina_file_path}`.scan(/pixelHeight: (\d+)/).last.first
image_width = `sips -g pixelWidth #{retina_file_path}`.scan(/pixelWidth: (\d+)/).last.first
`sips -z #{image_height.to_i * scale_factor} #{image_width.to_i * scale_factor} #{retina_file_path}`
end

end
end
end
15 changes: 15 additions & 0 deletions command/command/string.rb
@@ -0,0 +1,15 @@
class String

def underscore
gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end

def titleize
gsub(/([A-Z])/, '_\1').split(/_/).map(&:capitalize).join
end

end
59 changes: 59 additions & 0 deletions command/command/template.rb
@@ -0,0 +1,59 @@
require File.expand_path('../string.rb', __FILE__)

module Joybox
module Command
class Template

attr_reader :name

def initialize(name, klass)
@name = name.titleize
@klass = klass.downcase

templates_directory = File.expand_path("../../templates/", __FILE__)
@template_class_file = File.expand_path("#{klass}.erb", templates_directory)
@template_spec_file = File.expand_path("#{klass}_spec.erb", templates_directory)
end

def save
begin
app_directory = find_directory('app')
spec_directory = find_directory('spec')
rescue
Motion::Project::App.log 'Error', 'The command needs to be run inside of a RubyMotion project'
return
end

class_file = File.join(app_directory, "#{@klass}s/#{name.underscore}_#{@klass}.rb")
test_file = File.join(spec_directory, "#{@klass}s/#{name.underscore}_#{@klass}_spec.rb")

create_file(class_file, @template_class_file)
create_file(test_file, @template_spec_file)
end

private

def find_directory(directory)
finded_directories = Dir.glob(directory)

if finded_directories.size != 1
find_app_directory(File.join('..', directory))
else
finded_directories[0]
end
end

def create_file(file_path, template_path)
return Motion::Project::App.log 'Warning', "#{file_path} exists" if File.exists?(file_path)

FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, "w") do |file|
file << ERB.new(File.read(template_path)).result(binding)
end

Motion::Project::App.log 'Create', "#{file_path}"
end

end
end
end
75 changes: 75 additions & 0 deletions command/command/tmx.rb
@@ -0,0 +1,75 @@
require "rexml/document"

module Joybox
module Command
class TMX

include REXML

def initialize(file_path, suffix)
@file_path = File.basename(file_path, '.*') + suffix + File.extname(file_path)
@suffix = suffix
@map_document = Document.new File.read(file_path)
end

def scale(scale_factor)
@scale_factor = scale_factor

scale_map(XPath.first(@map_document, "//map"))
XPath.each(@map_document, "//map/tileset") { |tileset| scale_tileset(tileset) }
XPath.each(@map_document, "//map/tileset/image") { |image| scale_image(image) }
XPath.each(@map_document, "//map/objectgroup/object") { |object| scale_object(object) }

save
end

private

def scale_map(map)
scale_attribute(map, 'tilewidth')
scale_attribute(map, 'tileheight')
end

def scale_tileset(tileset)
scale_attribute(tileset, 'tilewidth')
scale_attribute(tileset, 'tileheight')
scale_attribute(tileset, 'margin')
scale_attribute(tileset, 'spacing')
end

def scale_image(image)
scale_attribute(image, 'width')
scale_attribute(image, 'height')

image_file = image.attributes['source']

if File.exists? image_file
image = Image.new(image_file, @suffix)
image.scale(@scale_factor)
else
Motion::Project::App.log 'Warning', "Tileset image #{image_file} does not exist"
end
end

def scale_object(object)
scale_attribute(object, 'x')
scale_attribute(object, 'y')
scale_attribute(object, 'width')
scale_attribute(object, 'height')
end

def scale_attribute(element, attribute)
if element.attributes.has_key? attribute
element.attributes[attribute] = element.attributes[attribute].to_i * @scale_factor
end
end

def save
File.open(@file_path, "w") do |file|
file << @map_document
end
end

end
end
end
11 changes: 11 additions & 0 deletions command/templates/layer.erb
@@ -0,0 +1,11 @@
class <%= name %>Layer < Joybox::Core::Layer

def on_enter
# Set up
end

def on_exit
# Tear down
end

end
3 changes: 3 additions & 0 deletions command/templates/layer_spec.erb
@@ -0,0 +1,3 @@
describe "Layer '<%= name %>'" do

end
11 changes: 11 additions & 0 deletions command/templates/scene.erb
@@ -0,0 +1,11 @@
class <%= name %>Scene < Joybox::Core::Scene

def on_enter
# Set up
end

def on_exit
# Tear down
end

end
3 changes: 3 additions & 0 deletions command/templates/scene_spec.erb
@@ -0,0 +1,3 @@
describe "Scene '<%= name %>'" do

end
11 changes: 11 additions & 0 deletions command/templates/sprite.erb
@@ -0,0 +1,11 @@
class <%= name %>Sprite < Joybox::Core::Sprite

def on_enter
# Set up
end

def on_exit
# Tear down
end

end
3 changes: 3 additions & 0 deletions command/templates/sprite_spec.erb
@@ -0,0 +1,3 @@
describe "Sprite '<%= name %>'" do

end
34 changes: 34 additions & 0 deletions ext/extconf.rb
@@ -0,0 +1,34 @@
require 'fileutils'

ruby_motion_directory = File.expand_path('~/Library/RubyMotion')
command_directory = File.join(ruby_motion_directory, 'command')
template_directory = File.join(ruby_motion_directory, 'template')

joybox_command_directory = File.expand_path('../../command', __FILE__)
joybox_template_directory = File.expand_path('../../template/', __FILE__)

# Previous version clean up
FileUtils.rm_rf File.join(command_directory, 'joybox')
FileUtils.rm_f File.join(command_directory, 'joybox_generate_command.rb')
FileUtils.rm_rf File.join(template_directory, 'joybox-ios')
FileUtils.rm_rf File.join(template_directory, 'joybox-ios-example-repl')
FileUtils.rm_rf File.join(template_directory, 'joybox-osx')
FileUtils.rm_rf File.join(template_directory, 'joybox-osx-example-repl')

FileUtils.mkdir_p(command_directory) unless File.directory?(command_directory)
FileUtils.mkdir_p(template_directory) unless File.directory?(template_directory)

Dir.glob(File.join(joybox_command_directory, '*.rb')).each do |source_file|
destination_file = File.join(command_directory, File.basename(source_file))
FileUtils.rm_f destination_file
FileUtils.ln_s source_file, destination_file
end

Dir.glob(File.join(joybox_template_directory, '**')).each do |source_directory|
destination_directory = File.join(template_directory, File.basename(source_directory))
FileUtils.rm_f destination_directory
FileUtils.ln_s source_directory, destination_directory
end

require 'mkmf'
create_makefile('')

0 comments on commit 0bbc5db

Please sign in to comment.