Class: Scratchpad::SheetModel

Inherits:
GLib::Object
  • Object
show all
Includes:
Cairo, Math
Defined in:
lib/scratchpad.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ SheetModel

Returns a new instance of SheetModel.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/scratchpad.rb', line 137

def initialize(width, height)
  super()
  @surface         = ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
  @debug_surface   = ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
  @outline_surface = ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)

  cr = Context.new(@surface)
  cr.set_operator(Cairo::OPERATOR_OVER)
  cr.line_cap = cr.line_join = :round
  @context = cr

  @pen = Pen.new

  @portion = :all
end

Instance Attribute Details

#debug_surfaceObject (readonly)

Returns the value of attribute debug_surface.



135
136
137
# File 'lib/scratchpad.rb', line 135

def debug_surface
  @debug_surface
end

#outline_surfaceObject (readonly)

Returns the value of attribute outline_surface.



135
136
137
# File 'lib/scratchpad.rb', line 135

def outline_surface
  @outline_surface
end

#penObject (readonly)

Returns the value of attribute pen.



135
136
137
# File 'lib/scratchpad.rb', line 135

def pen
  @pen
end

#surfaceObject (readonly)

Returns the value of attribute surface.



135
136
137
# File 'lib/scratchpad.rb', line 135

def surface
  @surface
end

Instance Method Details

#clearObject



157
158
159
160
161
162
# File 'lib/scratchpad.rb', line 157

def clear
  clear_surface(@surface)
  clear_surface(@outline_surface)
  clear_surface(@debug_surface)
  signal_emit('changed')
end

#clear_surface(surface) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/scratchpad.rb', line 164

def clear_surface(surface)
  c = Context.new(surface)
  c.save do
    c.set_operator(Cairo::OPERATOR_SOURCE)
    c.set_source_rgba(0, 0, 0, 0)
    c.paint
  end
  c.destroy
end

#crObject



153
154
155
# File 'lib/scratchpad.rb', line 153

def cr
  @context
end

#normalize(ary) ⇒ Object



184
185
186
187
# File 'lib/scratchpad.rb', line 184

def normalize(ary)
  sum = ary.max
  ary.map { |x| x / sum }
end

#pen_down(ev) ⇒ Object



174
175
176
177
# File 'lib/scratchpad.rb', line 174

def pen_down(ev)
  @pen.down(ev)
  @portion = :latter_half
end

#pen_move(ev) ⇒ Object



246
247
248
249
# File 'lib/scratchpad.rb', line 246

def pen_move(ev)
  @pen.move(ev)
  update
end

#pen_up(ev) ⇒ Object



179
180
181
182
# File 'lib/scratchpad.rb', line 179

def pen_up(ev)
  @pen.up(ev)
  @portion = :first_half
end

#stroke_outline(path) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/scratchpad.rb', line 227

def stroke_outline(path)
  cr = Cairo::Context.new(@outline_surface)
  cr.line_cap = cr.line_join = :round
  cr.set_source_rgba([1, 1, 1, 1.0])
  cr.line_width = @pen.radius * 3
  stroke_path(cr, path)
end

#stroke_path(cr, path) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/scratchpad.rb', line 235

def stroke_path(cr, path)
  path.each.with_index do |(x, y, radius), i|
    if i == 0
	cr.move_to(x, y)
    else
      cr.line_to(x, y)
    end
  end
  cr.stroke
end

#updateObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/scratchpad.rb', line 189

def update
  if @pen.down?
    case @portion 
    when :all
      path = @pen.path

      stroke_outline(path)

      cr.set_source_rgba(@pen.color)
      cr.line_width = @pen.radius
      stroke_path(cr, path)
    when :latter_half
      path = @pen.path[5..10] || []

      stroke_outline(path)

      cr.set_source_rgba(@pen.color)
      cr.line_width = @pen.radius
      stroke_path(cr, path)
    end
    @portion = :all
    signal_emit('changed')
  else
    if @portion == :first_half
      path = @pen.path[0..4] || []

      stroke_outline(path)

      cr.set_source_rgba(@pen.color)
      cr.line_width = @pen.radius
      stroke_path(cr, path)

      @portion = :all
      signal_emit('changed')
    end
  end
end