Class: Reight::App::Chips
- Inherits:
-
Object
- Object
- Reight::App::Chips
- Includes:
- Hookable
- Defined in:
- lib/reight/app/chips.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #chip ⇒ Object
- #draw ⇒ Object
- #draw_frame ⇒ Object
- #draw_offset_grids ⇒ Object
- #index2offset(index) ⇒ Object
-
#initialize(app, chips, size = 8, page_width = app.project.chips_page_width, page_height = app.project.chips_page_height) ⇒ Chips
constructor
A new instance of Chips.
- #mouse_clicked(x, y) ⇒ Object
- #mouse_dragged(x, y) ⇒ Object
- #mouse_pressed(x, y) ⇒ Object
- #mouse_released(x, y) ⇒ Object
- #offset2index(offset = self.offset) ⇒ Object
- #set_frame(x, y, w = size, h = size) ⇒ Object
- #sprite ⇒ Object
Methods included from Hookable
Constructor Details
#initialize(app, chips, size = 8, page_width = app.project.chips_page_width, page_height = app.project.chips_page_height) ⇒ Chips
Returns a new instance of Chips.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/reight/app/chips.rb', line 8 def initialize( app, chips, size = 8, page_width = app.project.chips_page_width, page_height = app.project.chips_page_height) hook :frame_changed hook :offset_changed @app, @chips = app, chips @page_size = create_vector page_width, page_height @offset = create_vector 0, 0 set_frame 0, 0, size, size end |
Instance Attribute Details
#offset ⇒ Object
Returns the value of attribute offset.
23 24 25 |
# File 'lib/reight/app/chips.rb', line 23 def offset @offset end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
23 24 25 |
# File 'lib/reight/app/chips.rb', line 23 def size @size end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
23 24 25 |
# File 'lib/reight/app/chips.rb', line 23 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
23 24 25 |
# File 'lib/reight/app/chips.rb', line 23 def y @y end |
Instance Method Details
#chip ⇒ Object
25 |
# File 'lib/reight/app/chips.rb', line 25 def chip = @chips.at x, y, size, size |
#draw ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/reight/app/chips.rb', line 59 def draw() sp = sprite clip sp.x, sp.y, sp.w, sp.h fill 0 no_stroke rect 0, 0, sp.w, sp.h translate offset.x, offset.y draw_offset_grids image @chips.image, 0, 0 draw_frame end |
#draw_frame ⇒ Object
82 83 84 85 86 87 |
# File 'lib/reight/app/chips.rb', line 82 def draw_frame() no_fill stroke 255 stroke_weight 1 rect @x, @y, @size, @size end |
#draw_offset_grids ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/reight/app/chips.rb', line 73 def draw_offset_grids() no_fill stroke 50 iw, ih = @chips.image.width, @chips.image.height cw, ch = @page_size.x, @page_size.y (cw...iw).step(cw) {|x| line x, 0, x, ih} (ch...ih).step(ch) {|y| line 0, y, iw, y} end |
#index2offset(index) ⇒ Object
45 46 47 48 49 |
# File 'lib/reight/app/chips.rb', line 45 def index2offset(index) pw, ph = @page_size.x.to_i, @page_size.y.to_i size = @chips.image.width / pw create_vector -(index % size).to_i * pw, -(index / size).to_i * ph end |
#mouse_clicked(x, y) ⇒ Object
102 103 104 105 106 |
# File 'lib/reight/app/chips.rb', line 102 def mouse_clicked(x, y) set_frame( -offset.x + align_to_grid(x), -offset.y + align_to_grid(y)) end |
#mouse_dragged(x, y) ⇒ Object
96 97 98 99 100 |
# File 'lib/reight/app/chips.rb', line 96 def mouse_dragged(x, y) pos = create_vector x, y self.offset += pos - @prev_pos if @prev_pos @prev_pos = pos end |
#mouse_pressed(x, y) ⇒ Object
89 90 91 |
# File 'lib/reight/app/chips.rb', line 89 def mouse_pressed(x, y) @prev_pos = create_vector x, y end |
#mouse_released(x, y) ⇒ Object
93 94 |
# File 'lib/reight/app/chips.rb', line 93 def mouse_released(x, y) end |
#offset2index(offset = self.offset) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/reight/app/chips.rb', line 51 def offset2index(offset = self.offset) iw = @chips.image.width pw, ph = @page_size.x.to_i, @page_size.y.to_i x, y = (-offset.x / ph).to_i, (-offset.y / pw).to_i w = (iw / pw).to_i y * w + x end |
#set_frame(x, y, w = size, h = size) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/reight/app/chips.rb', line 27 def set_frame(x, y, w = size, h = size) raise 'Chips: width != height' if w != h @x = align_to_grid(x).clamp(0..@chips.image.width) @y = align_to_grid(y).clamp(0..@chips.image.height) @size = w frame_changed! @x, @y, @size, @size end |
#sprite ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/reight/app/chips.rb', line 108 def sprite() @sprite ||= RubySketch::Sprite.new.tap do |sp| sp.draw {draw} sp.mouse_pressed {mouse_pressed sp.mouse_x, sp.mouse_y} sp.mouse_released {mouse_released sp.mouse_x, sp.mouse_y} sp.mouse_dragged {mouse_dragged sp.mouse_x, sp.mouse_y} sp.mouse_clicked {mouse_clicked sp.mouse_x, sp.mouse_y} end end |