puzzlize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +61 -0
  4. data/CHANGELOG +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +7 -0
  7. data/README.md +76 -0
  8. data/Rakefile +5 -0
  9. data/lib/puzzlize.rb +6 -0
  10. data/lib/puzzlize/action_view_helpers.rb +89 -0
  11. data/lib/puzzlize/cutter.rb +199 -0
  12. data/lib/puzzlize/model_additions.rb +18 -0
  13. data/lib/puzzlize/model_instance_methods.rb +125 -0
  14. data/lib/puzzlize/railtie.rb +17 -0
  15. data/lib/puzzlize/schema.rb +26 -0
  16. data/lib/puzzlize/version.rb +3 -0
  17. data/puzzlize.gemspec +26 -0
  18. data/spec/puzzlize/cutter_spec.rb +198 -0
  19. data/spec/puzzlize/model_additions_spec.rb +30 -0
  20. data/spec/puzzlize_spec.rb +50 -0
  21. data/spec/spec_helper.rb +3 -0
  22. data/spec/support/correct/regular_photo-0_0.gif +0 -0
  23. data/spec/support/correct/regular_photo-0_1.gif +0 -0
  24. data/spec/support/correct/regular_photo-1_0.gif +0 -0
  25. data/spec/support/correct/regular_photo-1_1.gif +0 -0
  26. data/spec/support/correct/regular_photo-2_0.gif +0 -0
  27. data/spec/support/correct/regular_photo-2_1.gif +0 -0
  28. data/spec/support/correct/regular_photo-3_0.gif +0 -0
  29. data/spec/support/correct/regular_photo-3_1.gif +0 -0
  30. data/spec/support/correct/regular_photo.jpg +0 -0
  31. data/spec/support/correct/simple_sample-0_0.gif +0 -0
  32. data/spec/support/correct/simple_sample-0_1.gif +0 -0
  33. data/spec/support/correct/simple_sample-0_2.gif +0 -0
  34. data/spec/support/correct/simple_sample-0_3.gif +0 -0
  35. data/spec/support/correct/simple_sample-1_0.gif +0 -0
  36. data/spec/support/correct/simple_sample-1_1.gif +0 -0
  37. data/spec/support/correct/simple_sample-1_2.gif +0 -0
  38. data/spec/support/correct/simple_sample-1_3.gif +0 -0
  39. data/spec/support/correct/simple_sample-2_0.gif +0 -0
  40. data/spec/support/correct/simple_sample-2_1.gif +0 -0
  41. data/spec/support/correct/simple_sample-2_2.gif +0 -0
  42. data/spec/support/correct/simple_sample-2_3.gif +0 -0
  43. data/spec/support/correct/simple_sample-3_0.gif +0 -0
  44. data/spec/support/correct/simple_sample-3_1.gif +0 -0
  45. data/spec/support/correct/simple_sample-3_2.gif +0 -0
  46. data/spec/support/correct/simple_sample-3_3.gif +0 -0
  47. data/spec/support/correct/simple_sample.jpg +0 -0
  48. data/spec/support/images/sample_image.png +0 -0
  49. data/spec/support/made/.DS_Store +0 -0
  50. data/spec/support/made/.gitignore +0 -0
  51. data/spec/support/made/regular_photo.jpg +0 -0
  52. data/spec/support/made/simple_sample.jpg +0 -0
  53. data/spec/support/regular_photo.jpg +0 -0
  54. data/spec/support/simple_sample.jpg +0 -0
  55. metadata +223 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.3-p0"
8
+
9
+ #
10
+ # Uncomment following line if you want options to be set only for given project.
11
+ #
12
+ # PROJECT_JRUBY_OPTS=( --1.9 )
13
+
14
+ #
15
+ # First we attempt to load the desired environment directly from the environment
16
+ # file. This is very fast and efficient compared to running through the entire
17
+ # CLI and selector. If you want feedback on which environment was used then
18
+ # insert the word 'use' after --create as this triggers verbose mode.
19
+ #
20
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
21
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
22
+ then
23
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
24
+
25
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
26
+ then
27
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
28
+ fi
29
+ else
30
+ # If the environment file has not yet been created, use the RVM CLI to select.
31
+ if ! rvm --create "$environment_id"
32
+ then
33
+ echo "Failed to create RVM environment '${environment_id}'."
34
+ return 1
35
+ fi
36
+ fi
37
+
38
+ #
39
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
40
+ # it be automatically loaded. Uncomment the following and adjust the filename if
41
+ # necessary.
42
+ #
43
+ # filename=".gems"
44
+ # if [[ -s "$filename" ]]
45
+ # then
46
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
47
+ # fi
48
+
49
+ # If you use bundler, this might be useful to you:
50
+ # if command -v bundle && [[ -s Gemfile ]]
51
+ # then
52
+ # bundle install
53
+ # fi
54
+
55
+ if [[ $- == *i* ]] # check for interactive shells
56
+ then
57
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
58
+ else
59
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
60
+ fi
61
+
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in puzzlize.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Ziemek Wolski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Puzzlizer
2
+
3
+ ## Description
4
+
5
+ Puzzlizer allows you to create a puzzle from an upload image. It uses RMagic to cut up the image and provides helper function to allows the user to move the pieces around to solve the puzzle.
6
+
7
+ ## Installation
8
+
9
+ Edit you gem file add puzzlize to it.
10
+
11
+ ```ruby
12
+ gem "puzzlize"
13
+ ```
14
+
15
+ Alternatively you could install the gem yourself:
16
+
17
+ ```ruby
18
+ console:
19
+ gem install puzzlize
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Before you start you will need to add a migration on the model that you want to use:
25
+
26
+ ```ruby
27
+ console:
28
+ rails g migration add_puzzle_columns
29
+ ```
30
+
31
+ Inside the migration you will need (assuming that your model is Painting):
32
+
33
+ ```ruby
34
+ def self.up
35
+ change_table :paintings do |t|
36
+ t.add_puzzlize_columns
37
+ end
38
+ end
39
+
40
+ def self.down
41
+ remove_puzzlize_columns :paintings
42
+ end
43
+ ```
44
+
45
+ Inside your model you will need:
46
+
47
+ ```ruby
48
+ class Painting < ActiveRecord::Base
49
+ has_attached_file :image, {:styles => { :medium => "800x800>" }}.merge(PAPERCLIP_STORAGE_OPTIONS) <-- after paperclip
50
+ puzzlize :image
51
+ end
52
+ ```
53
+
54
+ If you are not using paperclip and your image object does not have access to `image.path(:medium)` and `image.url(:medium)` you must overwritte these two methods in your model.
55
+
56
+ ```ruby
57
+ class Painting < ActiveRecord::Base
58
+
59
+ def default_puzzle_image_url
60
+ // Replacement code for --- image.url(:medium) ---
61
+ end
62
+
63
+ def default_puzzle_image_path
64
+ // Replacement code for --- image.path(:medium) ---
65
+ end
66
+ end
67
+ ```
68
+
69
+ in the show/view:
70
+
71
+ ```ruby
72
+ puzzlize_javascript_and_css(@painting)
73
+ puzzlize_show_puzzle(@painting)
74
+ ```
75
+
76
+ where @painting is model object.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
data/lib/puzzlize.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "puzzlize/model_additions"
2
+ require "puzzlize/version"
3
+ require "puzzlize/cutter"
4
+ require "puzzlize/action_view_helpers"
5
+ require "puzzlize/schema"
6
+ require "puzzlize/railtie" if defined? Rails
@@ -0,0 +1,89 @@
1
+ module Puzzlize
2
+ module ViewHelpers
3
+ def puzzlize_javascript_and_css(puzzle)
4
+ droppable_function = puzzle.puzzle_pieces_names.collect do |name|
5
+
6
+ "$('#i-#{name}').draggable({helper:'original', handle:'.drag'});
7
+ $('#s-#{name} > .sensible').droppable({
8
+ accept: '#i-#{name}',
9
+ tolerance: 'intersect',
10
+ activeClass: 'droppable-active',
11
+ hoverClass: 'droppable-hover',
12
+ drop: function() {
13
+ $('#s-#{name}').addClass('s-#{name}');
14
+ $('#s-#{name}').addClass('encastrada');
15
+ $('#i-#{name}').remove();
16
+ }
17
+ });"
18
+ end.join(" ")
19
+
20
+ droppable_css = ""
21
+ image_urls = puzzle.puzzle_images_urls
22
+ puzzle.puzzle_pieces_names.each_with_index do |name, index|
23
+ droppable_css += ".s-#{name} {
24
+ background-image:url(#{image_urls[index]});
25
+ background-repeat:no-repeat;
26
+ background-position:center center;
27
+ }"
28
+ end
29
+
30
+ "<script type='text/javascript'>
31
+ // Javascript library made by:
32
+ // Fernando.com.ar
33
+ // http://www.fernando.com.ar/jquery-puzzle/index.php
34
+
35
+ $(document).ready(function(){
36
+ #{droppable_function}
37
+ });
38
+
39
+ </script>
40
+ <style type='text/css'>
41
+ #{droppable_css}
42
+ .drag{
43
+ width:#{puzzle.horizontal_piece_size}px;
44
+ height:#{puzzle.vertical_piece_size}px;
45
+ }
46
+ .sensible{
47
+ width:#{puzzle.horizontal_piece_size}px;
48
+ height:#{puzzle.vertical_piece_size}px;
49
+ border:0px solid red;
50
+ }
51
+ #canvas{
52
+ width:#{puzzle.image_width}px;
53
+ height:#{puzzle.image_height}px;
54
+ background-color:#A5A5A5;
55
+ float:left;
56
+ }
57
+ #canvasFinal{
58
+ width:#{puzzle.image_width}px;
59
+ height:#{puzzle.image_height}px;
60
+ background-image:url(#{ puzzle.default_puzzle_image_url});
61
+ background-repeat:no-repeat;
62
+ background-position:center center;
63
+ float:left;
64
+ position:absolute;
65
+ display:none;
66
+ z-index:9;
67
+ }
68
+ </style>".html_safe
69
+ end
70
+
71
+ def puzzlize_show_puzzle(puzzle)
72
+ puzzle_pieces_html = ""
73
+ puzzle.puzzle_pieces_names.each do |name|
74
+ puzzle_pieces_html += "
75
+ <div class='s-#{name}' id='i-#{name}' style='width:#{puzzle.horizontal_piece_size}px; height:#{puzzle.vertical_piece_size}px; position:absolute; top:#{134 + rand(320)}px; left:#{rand(180)}px;' >
76
+ <div class='drag'></div>
77
+ </div>"
78
+ end
79
+
80
+ "<div>
81
+ <div class='puzzle' style='width:660px;'>
82
+ <div style='width:390px; height:450px; background-color:#FFF; float:left;'>
83
+ #{puzzle_pieces_html}
84
+ </div>
85
+ </div>
86
+ </div>".html_safe
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,199 @@
1
+ module Puzzlize
2
+ class Cutter
3
+ CONNECTOR_TOP_PATTERN = [-1, -1 ,1]
4
+ CONNECTOR_RIGHT_PATTERN = [1, 1, -1]
5
+ attr_accessor :piece_size, :image_width, :image_height
6
+
7
+ #piece size including all connectors and white space
8
+ def real_piece_size
9
+ piece_size + connector_size
10
+ end
11
+
12
+ def horizontal_pieces
13
+ (image_width / piece_size).to_i
14
+ end
15
+
16
+ def vertical_pieces
17
+ (image_height / piece_size).to_i
18
+ end
19
+
20
+ def connector_size
21
+ @connector_size ||= (piece_size * 0.2).to_i
22
+ end
23
+
24
+ def connector_radius
25
+ return connector_size / 2
26
+ end
27
+
28
+ def get_piece_matrix
29
+ matrix = []
30
+ vertical_pieces.times do |y_axis|
31
+ horizontal_pieces.times do |x_axis|
32
+ matrix << piece_points(x_axis, y_axis)
33
+ end
34
+ end
35
+ matrix
36
+ end
37
+
38
+ def piece_points(row_x , row_y, include_connector_size = true)
39
+ # the pieces should overlap so we have a way to make the connector
40
+ start_point_x = row_x * piece_size
41
+ start_point_y = row_y * piece_size
42
+ # if by adding the connector buffer we have gone passed the edge of the image
43
+ # go back to the edge of the image.
44
+ piece_width = piece_size
45
+ piece_height = piece_size
46
+ end_point_x = start_point_x + piece_width
47
+ end_point_y = start_point_y + piece_height
48
+
49
+ #translating number of pieces to positions
50
+ horizontal_position = horizontal_pieces - 1
51
+ vertical_position = vertical_pieces - 1
52
+
53
+ if include_connector_size
54
+ start_point_x -= connector_radius if row_x > 0
55
+ start_point_y -= connector_radius if row_y > 0
56
+
57
+ # if the piece are not on the edges, they are bigger by the radius of the connector.
58
+ piece_width += connector_radius if row_x < horizontal_position
59
+ piece_height += connector_radius if row_y < vertical_position
60
+
61
+ piece_width += connector_radius if row_x > 0
62
+ piece_height += connector_radius if row_y > 0
63
+ end
64
+
65
+ [start_point_x, start_point_y, piece_width, piece_height]
66
+ end
67
+
68
+ def get_connector_locations_matrix
69
+ matrix = []
70
+ vertical_pieces.times do |y_axis|
71
+ horizontal_pieces.times do |x_axis|
72
+ matrix << connector_locations(x_axis, y_axis)
73
+ end
74
+ end
75
+ matrix
76
+ end
77
+
78
+ def connector_locations(row_x, row_y)
79
+ points = []
80
+ mid = piece_size / 2
81
+ width = piece_size
82
+ height = piece_size
83
+ cr = connector_radius
84
+ vertical_position = vertical_pieces - 1
85
+ horizontal_position = horizontal_pieces - 1
86
+
87
+ points << (row_y == 0 ? nil : [mid, 0])
88
+ points << (row_y == vertical_position ? nil : [mid, height])
89
+ points << (row_x == 0 ? nil : [0, mid])
90
+ points << (row_x == horizontal_position ? nil : [width, mid])
91
+
92
+ if row_y > 0
93
+ points[0][1] += cr unless points[0].nil?
94
+ points[1][1] += cr unless points[1].nil?
95
+ points[2][1] += cr unless points[2].nil?
96
+ points[3][1] += cr unless points[3].nil?
97
+ end
98
+
99
+ if row_x > 0
100
+ #use loop
101
+ points[0][0] += cr unless points[0].nil?
102
+ points[1][0] += cr unless points[1].nil?
103
+ points[2][0] += cr unless points[2].nil?
104
+ points[3][0] += cr unless points[3].nil?
105
+ end
106
+ points
107
+ end
108
+
109
+ def get_rectangle_matrix
110
+ matrix = []
111
+ vertical_pieces.times do |y_axis|
112
+ horizontal_pieces.times do |x_axis|
113
+ matrix << rectangle_locations(x_axis, y_axis)
114
+ end
115
+ end
116
+ matrix
117
+ end
118
+
119
+ def rectangle_locations(row_x, row_y)
120
+ points = []
121
+ width = piece_size
122
+ height = piece_size
123
+ cr = connector_radius
124
+ vertical_position = vertical_pieces - 1
125
+ horizontal_position = horizontal_pieces - 1
126
+
127
+
128
+ points << (row_y == 0 ? nil : [0,0, width, cr])
129
+ points << (row_y == vertical_position ? nil : [0,height, width, height + cr])
130
+ points << (row_x == 0 ? nil : [0,0, cr, height])
131
+ points << (row_x == horizontal_position ? nil : [width, 0, width + cr, height])
132
+
133
+ if row_x > 0
134
+ points[0][2] += cr unless points[0].nil?
135
+ points[1][2] += cr unless points[1].nil?
136
+
137
+ points[3][0] += cr unless points[3].nil?
138
+ points[3][2] += cr unless points[3].nil?
139
+ end
140
+
141
+ if row_x < horizontal_position
142
+ points[0][2] += cr unless points[0].nil?
143
+ points[1][2] += cr unless points[1].nil?
144
+ end
145
+
146
+ if row_y > 0
147
+ points[1][1] += cr unless points[1].nil?
148
+ points[1][3] += cr unless points[1].nil?
149
+
150
+ points[2][3] += cr unless points[2].nil?
151
+
152
+ points[3][3] += cr unless points[3].nil?
153
+ end
154
+
155
+ if row_y < vertical_position
156
+ points[2][3] += cr unless points[2].nil?
157
+ points[3][3] += cr unless points[3].nil?
158
+ end
159
+
160
+ points
161
+ end
162
+
163
+ def connector_types(row_x, row_y)
164
+ # Pattern based connectors
165
+ # Top and right connector can be anything. We determine this based on a pattern.
166
+ top = connector_top(row_x, row_y)
167
+ right = connector_right(row_x, row_y)
168
+
169
+ # Fitter connectors
170
+ # left and bottom connectors need are fitters. Meaning they might fit to was on top and to the right.
171
+ # I determine this by shifting to that piece and looking at either the top or right piece and getting the
172
+ # opposite of that piece. If that piece is at the border we return nil.
173
+ left = row_x - 1 >= 0 ? (connector_right(row_x -1, row_y) * -1) : nil
174
+ bottom = row_y - 1 >= 0 ? (connector_top(row_x, row_y - 1) * -1) : nil
175
+
176
+ [bottom, top , left, right]
177
+ end
178
+
179
+ def connector_top(row_x, row_y)
180
+ # -1 because it's a count vs location starting at 0.
181
+ return nil if vertical_pieces - 1 == row_y
182
+ CONNECTOR_TOP_PATTERN[row_x % CONNECTOR_TOP_PATTERN.size]
183
+ end
184
+
185
+ def connector_right(row_x, row_y)
186
+ # -1 because it's a count vs location starting at 0.
187
+ return nil if horizontal_pieces - 1 == row_x
188
+ CONNECTOR_RIGHT_PATTERN[row_x % CONNECTOR_RIGHT_PATTERN.size]
189
+ end
190
+
191
+ def piece_fitting_diamentions
192
+ horizontal = horizontal_pieces * piece_size
193
+ vertical = vertical_pieces * piece_size
194
+ self.image_width = horizontal
195
+ self.image_height = vertical
196
+ [0, 0,horizontal ,vertical ]
197
+ end
198
+ end
199
+ end