geomotion 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Geomotion.gemspec +18 -0
- data/README.md +48 -0
- data/Rakefile +11 -0
- data/app/app_delegate.rb +5 -0
- data/lib/geomotion.rb +11 -0
- data/lib/geomotion/cg_rect.rb +118 -0
- data/lib/geomotion/version.rb +3 -0
- data/spec/main_spec.rb +36 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Geomotion.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/geomotion/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "geomotion"
|
6
|
+
s.version = Geomotion::VERSION
|
7
|
+
s.authors = ["Clay Allsopp"]
|
8
|
+
s.email = ["clay.allsopp@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/clayallsopp/geomotion"
|
10
|
+
s.summary = "A RubyMotion Geometry Wrapper"
|
11
|
+
s.description = "A RubyMotion Geometry Wrapper"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split($\)
|
14
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_development_dependency 'rake'
|
18
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Geomotion
|
2
|
+
|
3
|
+
iOS geometry is too verbose for Ruby. Geomotion tries to fix that.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# More idiomatic initializer
|
9
|
+
rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
10
|
+
|
11
|
+
# These do what you think
|
12
|
+
[rect.x, rect.y, rect.width, rect.height]
|
13
|
+
=> [10, 100, 50, 20]
|
14
|
+
|
15
|
+
# Chainable methods for adjusting frames
|
16
|
+
view = UIView.alloc.initWithFrame rect.below.width(100).height(10)
|
17
|
+
view.frame
|
18
|
+
=> #<CGRect origin=#<CGPoint x=10.0 y=120.0> size=#<CGSize width=100.0 height=10.0>>
|
19
|
+
|
20
|
+
view2 = UIView.alloc.initWithFrame rect.beside(10)
|
21
|
+
view2.frame
|
22
|
+
=> #<CGRect origin=#<CGPoint x=70.0 y=100.0> size=#<CGSize width=50.0 height=20.0>>
|
23
|
+
|
24
|
+
[rect.right(20).x, rect.left(20).x, rect.up(20).y, rect.down(20).y]
|
25
|
+
=> [30, -10, 80, 120]
|
26
|
+
|
27
|
+
# Layout a rect relative to others
|
28
|
+
rect2 = CGRect.new [50, 50], [100, 100]
|
29
|
+
rect3 = CGRect.new [100, 200], [20, 20]
|
30
|
+
|
31
|
+
CGRect.layout(rect, above: rect2, right_of: rect3)
|
32
|
+
=> #<CGRect origin=#<CGPoint x=120.0 y=30.0> size=#<CGSize width=50.0 height=20.0>>
|
33
|
+
|
34
|
+
# Also supports margins
|
35
|
+
CGRect.layout(rect, above: rect2, right_of: rect3, margins: [0, 0, 10, 15])
|
36
|
+
=> #<CGRect origin=#<CGPoint x=135.0 y=20.0> size=#<CGSize width=50.0 height=20.0>>
|
37
|
+
```
|
38
|
+
|
39
|
+
## Install
|
40
|
+
|
41
|
+
1. `gem install geomotion`
|
42
|
+
|
43
|
+
2. Add `require geomotion` in your `Rakefile`.
|
44
|
+
|
45
|
+
|
46
|
+
## Forking
|
47
|
+
|
48
|
+
If you have cool/better ideas, pull-request away!
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift("/Library/RubyMotion/lib")
|
2
|
+
require 'motion/project'
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
$:.unshift("./lib/")
|
6
|
+
require './lib/geomotion'
|
7
|
+
|
8
|
+
Motion::Project::App.setup do |app|
|
9
|
+
# Use `rake config' to see complete project settings.
|
10
|
+
app.name = 'Geomotion'
|
11
|
+
end
|
data/app/app_delegate.rb
ADDED
data/lib/geomotion.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "geomotion/version"
|
2
|
+
|
3
|
+
unless defined?(Motion::Project::Config)
|
4
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
5
|
+
end
|
6
|
+
|
7
|
+
Motion::Project::App.setup do |app|
|
8
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'geomotion/*.rb')).each do |file|
|
9
|
+
app.files.unshift(file)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
class CGRect
|
2
|
+
# CGRect.make(x: 10, y: 30)
|
3
|
+
def self.make(options = {})
|
4
|
+
CGRect.new([options[:x] || 0, options[:y] || 0], [options[:width] || 0, options[:height] || 0])
|
5
|
+
end
|
6
|
+
|
7
|
+
# OPTIONS: [:above, :below, :left_of, :right_of, :margins]
|
8
|
+
# :margins is array of [top, right, bottom, left]
|
9
|
+
# EX CGRect.layout(rect1, above: rect2, left_of: rect3, margins: [0, 10, 20, 0])
|
10
|
+
def self.layout(rect1, options)
|
11
|
+
if options.empty?
|
12
|
+
p "No options provided in CGRect.layout"
|
13
|
+
return rect1
|
14
|
+
end
|
15
|
+
|
16
|
+
rect = CGRect.new
|
17
|
+
rect.size = rect1.size
|
18
|
+
|
19
|
+
options[:margins] ||= []
|
20
|
+
margins = {}
|
21
|
+
[:top, :right, :bottom, :left].each_with_index do |margin, index|
|
22
|
+
margins[margin] = options[:margins][index] || 0
|
23
|
+
end
|
24
|
+
|
25
|
+
rect.y = options[:above].up(rect.height + margins[:bottom]).y if options[:above]
|
26
|
+
rect.y = options[:below].below(margins[:top]).y if options[:below]
|
27
|
+
|
28
|
+
rect.x = options[:left_of].left(rect.width + margins[:right]).x if options[:left_of]
|
29
|
+
rect.x = options[:right_of].beside(margins[:left]).x if options[:right_of]
|
30
|
+
|
31
|
+
rect
|
32
|
+
end
|
33
|
+
|
34
|
+
def x(setter = nil)
|
35
|
+
if setter
|
36
|
+
return CGRect.new([setter, self.y], self.size)
|
37
|
+
end
|
38
|
+
self.origin.x
|
39
|
+
end
|
40
|
+
|
41
|
+
def x=(_x)
|
42
|
+
self.origin.x = _x
|
43
|
+
end
|
44
|
+
|
45
|
+
def y(setter = nil)
|
46
|
+
if setter
|
47
|
+
return CGRect.new([self.x, setter], self.size)
|
48
|
+
end
|
49
|
+
self.origin.y
|
50
|
+
end
|
51
|
+
|
52
|
+
def y=(_y)
|
53
|
+
self.origin.y = _y
|
54
|
+
end
|
55
|
+
|
56
|
+
def width(setter = nil)
|
57
|
+
if setter
|
58
|
+
return CGRect.new(self.origin, [setter, self.height])
|
59
|
+
end
|
60
|
+
self.size.width
|
61
|
+
end
|
62
|
+
|
63
|
+
def width=(_width)
|
64
|
+
self.size.width = _width
|
65
|
+
end
|
66
|
+
|
67
|
+
def height(setter = nil)
|
68
|
+
if setter
|
69
|
+
return CGRect.new(self.origin, [self.width, setter])
|
70
|
+
end
|
71
|
+
self.size.height
|
72
|
+
end
|
73
|
+
|
74
|
+
def height=(_height)
|
75
|
+
self.size.height = _height
|
76
|
+
end
|
77
|
+
|
78
|
+
def left(dist = 0)
|
79
|
+
CGRect.new([self.x - dist, self.y], self.size)
|
80
|
+
end
|
81
|
+
|
82
|
+
def right(dist = 0)
|
83
|
+
CGRect.new([self.x + dist, self.y], self.size)
|
84
|
+
end
|
85
|
+
|
86
|
+
def up(dist = 0)
|
87
|
+
CGRect.new([self.x, self.y - dist], self.size)
|
88
|
+
end
|
89
|
+
|
90
|
+
def down(dist = 0)
|
91
|
+
CGRect.new([self.x, self.y + dist], self.size)
|
92
|
+
end
|
93
|
+
|
94
|
+
def below(margin = 0)
|
95
|
+
CGRect.new([self.x, self.y + self.height + margin], self.size)
|
96
|
+
end
|
97
|
+
|
98
|
+
def beside(margin = 0)
|
99
|
+
CGRect.new([self.x + self.width + margin, self.y], self.size)
|
100
|
+
end
|
101
|
+
|
102
|
+
def center(relative = false)
|
103
|
+
offset_x = relative ? self.x : 0
|
104
|
+
offset_y = relative ? self.y : 0
|
105
|
+
CGPoint.new(offset_x + self.width / 2, offset_y = self.height / 2)
|
106
|
+
end
|
107
|
+
|
108
|
+
def round
|
109
|
+
CGRect.new([self.x.round, self.y.round], [self.width.round, self.height.round])
|
110
|
+
end
|
111
|
+
|
112
|
+
def centered_in(rect, relative = false)
|
113
|
+
offset_x = relative ? rect.x : 0
|
114
|
+
offset_y = relative ? rect.y : 0
|
115
|
+
CGRect.new([offset_x + ((rect.width - self.width) / 2),
|
116
|
+
offset_y + ((rect.height - self.height) / 2)], self.size)
|
117
|
+
end
|
118
|
+
end
|
data/spec/main_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
describe "CGRect" do
|
2
|
+
before do
|
3
|
+
@rect = CGRect.make(x: 10, y: 100, width: 50, height: 20)
|
4
|
+
end
|
5
|
+
|
6
|
+
it ".x, etc work" do
|
7
|
+
[@rect.x, @rect.y, @rect.width, @rect.height].should == [10, 100, 50, 20]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "chaining works" do
|
11
|
+
rect = @rect.below(10).width(100).height(10)
|
12
|
+
[rect.x, rect.y, rect.width, rect.height].should == [10, 130, 100, 10]
|
13
|
+
|
14
|
+
[@rect.right(20).x, @rect.left(20).x, @rect.up(20).y, @rect.down(20).y].should == [30, -10, 80, 120]
|
15
|
+
end
|
16
|
+
|
17
|
+
it ".beside works" do
|
18
|
+
rect = @rect.beside(10)
|
19
|
+
[rect.x, rect.y, rect.width, rect.height].should == [70, 100, 50, 20]
|
20
|
+
end
|
21
|
+
|
22
|
+
it ".below works" do
|
23
|
+
@rect.below(10).y.should == 130
|
24
|
+
end
|
25
|
+
|
26
|
+
it "layout works" do
|
27
|
+
rect2 = CGRect.new [50, 50], [100, 100]
|
28
|
+
rect3 = CGRect.new [100, 200], [20, 20]
|
29
|
+
|
30
|
+
no_margins = CGRect.layout(@rect, above: rect2, right_of: rect3)
|
31
|
+
[no_margins.x, no_margins.y, no_margins.width, no_margins.height].should == [120, 30, 50, 20]
|
32
|
+
|
33
|
+
margins = CGRect.layout(@rect, above: rect2, right_of: rect3, margins: [0, 0, 10, 15])
|
34
|
+
[margins.x, margins.y, margins.width, margins.height].should == [135, 20, 50, 20]
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geomotion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Clay Allsopp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: A RubyMotion Geometry Wrapper
|
31
|
+
email:
|
32
|
+
- clay.allsopp@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- Geomotion.gemspec
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- app/app_delegate.rb
|
43
|
+
- lib/geomotion.rb
|
44
|
+
- lib/geomotion/cg_rect.rb
|
45
|
+
- lib/geomotion/version.rb
|
46
|
+
- spec/main_spec.rb
|
47
|
+
homepage: https://github.com/clayallsopp/geomotion
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A RubyMotion Geometry Wrapper
|
71
|
+
test_files:
|
72
|
+
- spec/main_spec.rb
|