Module: Nyle

Defined in:
lib/nyle/frame.rb,
lib/nyle.rb,
lib/nyle/screen.rb,
lib/nyle/version.rb

Overview

‘Nyle’

minimal graphics framework using Ruby/GTK3 and rcairo

Copyright (c) 2018 Koki Kitamura
Released under the MIT license
https://opensource.org/licenses/mit-license.php

Defined Under Namespace

Modules: INNER Classes: Error, Frame, Screen

Constant Summary collapse

DEFAULT_WIDTH =
640
DEFAULT_HEIGHT =
480
DEFAULT_TITLE =
'Nyle'
DEFAULT_INTERVAL =
15
MIN_INTERVAL =
5
MAX_INTERVAL =
1000
VERSION =
"0.7.3"

Class Method Summary collapse

Class Method Details

.clearObject



394
395
396
# File 'lib/nyle.rb', line 394

module_function def clear
  Nyle.module_eval{ @__frame.current_screen.clear_screen }
end

.crObject

module functions for user (need module_eval{} statement to use including Nyle)



183
# File 'lib/nyle.rb', line 183

module_function def cr                      ;  Nyle.module_eval{ @__cr                            } ; end

.create_frame(*args) ⇒ Object



435
436
437
# File 'lib/nyle.rb', line 435

module_function def create_frame(*args)
  Nyle::Frame.new(*args)
end

.create_screen(*args) ⇒ Object



431
432
433
# File 'lib/nyle.rb', line 431

module_function def create_screen(*args)
  Nyle::Screen.new(*args)
end

.cursor_xObject



196
# File 'lib/nyle.rb', line 196

module_function def cursor_x                ;  Nyle.module_eval{ @__cursor_x                      } ; end

.cursor_yObject



197
# File 'lib/nyle.rb', line 197

module_function def cursor_y                ;  Nyle.module_eval{ @__cursor_y                      } ; end

.draw_circle(x, y, r, weight: 2, color: :BLACK, fill: false, a: 1.0) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/nyle.rb', line 248

module_function def draw_circle(x, y, r, weight: 2, color: :BLACK, fill: false, a: 1.0)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    cr.line_width = weight
    cr.set_source_rgba(Cairo::Color.parse(color).r, 
                       Cairo::Color.parse(color).g, 
                       Cairo::Color.parse(color).b, a)
    cr.circle(x, y, r)
    if fill
      cr.fill
    else
      cr.stroke
    end
  end
end

.draw_image(x, y, pixbuf, pos: :CORNER) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
# File 'lib/nyle.rb', line 352

module_function def draw_image(x, y, pixbuf, pos: :CORNER)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    if pos == :CENTER
      cr.set_source_pixbuf(pixbuf, x - pixbuf.width / 2, y - pixbuf.height / 2)
    else
      cr.set_source_pixbuf(pixbuf, x, y)   # :CORNER
    end
    cr.paint
  end
end

.draw_line(x1, y1, x2, y2, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/nyle.rb', line 213

module_function def draw_line(x1, y1, x2, y2, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    cr.line_width = weight
    cr.line_cap   = cap
    cr.set_source_rgba(Cairo::Color.parse(color).r, 
                       Cairo::Color.parse(color).g, 
                       Cairo::Color.parse(color).b, a)
    cr.move_to(x1, y1)
    cr.line_to(x2, y2)
    cr.stroke
  end
end

.draw_rect(x, y, w, h, weight: 2, cap: :BUTT, color: :BLACK, round: 0, fill: false, a: 1.0) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/nyle.rb', line 227

module_function def draw_rect(x, y, w, h, weight: 2, cap: :BUTT, color: :BLACK, round: 0, fill: false, a: 1.0)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    cr.line_width = weight
    cr.line_cap   = cap
    cr.set_source_rgba(Cairo::Color.parse(color).r, 
                       Cairo::Color.parse(color).g, 
                       Cairo::Color.parse(color).b, a)
    if round
      cr.rounded_rectangle(x, y, w, h, round, round)
    else
      cr.rectangle(x, y, w, h)
    end
    if fill
      cr.fill
    else
      cr.stroke
    end
  end
end

.draw_shape(points, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0, close: false, fill: false) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/nyle.rb', line 264

module_function def draw_shape(points, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0, close: false, fill: false)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    cr.line_width = weight
    cr.line_cap   = cap
    cr.line_join  = :ROUND if cap == :ROUND
    cr.set_source_rgba(Cairo::Color.parse(color).r, 
                       Cairo::Color.parse(color).g, 
                       Cairo::Color.parse(color).b, a)
    vertex = points.dup
    vertex << vertex.first if close   # closed shape
    vertex.each do |v|
      cr.line_to(v[0], v[1])
    end
    if fill
      cr.fill
    else
      cr.stroke
    end
  end
end

.draw_text(x, y, str, size: 32, color: :BLACK, a: 1.0, font: "sans-serif", italic: false, bold: false) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/nyle.rb', line 286

module_function def draw_text(x, y, str, size: 32, color: :BLACK, a: 1.0, font: "sans-serif", italic: false, bold: false)
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    pango_layout = cr.create_pango_layout
    pango_layout.text = str
    font_description        = Pango::FontDescription.new
    font_description.family = font
    font_description.style  = :italic if italic
    font_description.weight = 700 if bold
    font_description.size   = size * (72.0 / 96.0) * Pango::SCALE
    pango_layout.font_description = font_description
    cr.update_pango_layout(pango_layout)
    cr.set_source_rgba(Cairo::Color.parse(color).r,
                       Cairo::Color.parse(color).g,
                       Cairo::Color.parse(color).b, a)
    delta = (Nyle.module_eval{ @__os } == :mac ? size * (72.0 / 96.0) : size)
    cr.move_to(x, y - delta)   # Why :mac?
    cr.show_pango_layout(pango_layout)
    cr.new_path
  end
end

.hObject



202
# File 'lib/nyle.rb', line 202

module_function def screen_height           ;  Nyle.module_eval{ @__screen_height                 } ; end

.key_down?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


191
# File 'lib/nyle.rb', line 191

module_function def key_down?(keyval)       ;  Nyle.module_eval{ @__key_down     [keyval] == true } ; end

.key_press?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


192
# File 'lib/nyle.rb', line 192

module_function def key_press?(keyval)      ;  Nyle.module_eval{ @__key_press    [keyval] == true } ; end

.key_release?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


193
# File 'lib/nyle.rb', line 193

module_function def key_release?(keyval)    ;  Nyle.module_eval{ @__key_release  [keyval] == true } ; end

.layer(id = :__) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/nyle.rb', line 413

module_function def layer(id = :__)
  if block_given?
    cr = Nyle.cr
    layer = Screen::Layer.create(id, Nyle.w, Nyle.h)
    Cairo::Context.new(layer) do |cr_layer|
      Nyle.module_eval {
        _set_cr(cr_layer)
      }
      yield(layer)
    end
    Nyle.module_eval {
      _set_cr(cr)
    }
    Nyle.cr.set_source(layer, 0, 0)
    Nyle.cr.paint
  end
end

.load_image(filename, color_key: nil, sx: 1.0, sy: 1.0, cx: nil, cy: nil, cw: nil, ch: nil) ⇒ Object



327
328
329
330
331
332
333
# File 'lib/nyle.rb', line 327

module_function def load_image(filename, color_key: nil, sx: 1.0, sy: 1.0, cx: nil, cy: nil, cw: nil, ch: nil)
  loaded = Nyle::INNER.module_eval{ _load_image(filename, color_key: color_key) }
  if !(cx.nil? or cy.nil? or cw.nil? or ch.nil?)
    loaded = loaded.subpixbuf(cx, cy, cw, ch)
  end
  loaded = loaded.scale(loaded.width * sx, loaded.height * sy)
end

.load_image_tiles(filename, xcount, ycount, color_key: nil, sx: 1.0, sy: 1.0) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/nyle.rb', line 335

module_function def load_image_tiles(filename, xcount, ycount, color_key: nil, sx: 1.0, sy: 1.0)
  loaded = Nyle::INNER.module_eval{ _load_image(filename, color_key: color_key) }
  xcount = 1 if xcount < 1
  ycount = 1 if ycount < 1
  cw = loaded.width  / xcount
  ch = loaded.height / ycount
  tiles = []
  for i in (0...xcount) do
    line = []
    for j in (0...ycount) do
      line << loaded.subpixbuf(cw * i, ch * j, cw, ch).scale(cw * sx, ch * sy)
    end
    tiles << line
  end
  tiles
end

.mainObject



444
445
446
# File 'lib/nyle.rb', line 444

module_function def main
  Gtk.main
end

.mask_control?Boolean

Returns:

  • (Boolean)


194
# File 'lib/nyle.rb', line 194

module_function def mask_control?           ;  Nyle.module_eval{ @__mask_control                  } ; end

.mask_shift?Boolean

Returns:

  • (Boolean)


195
# File 'lib/nyle.rb', line 195

module_function def mask_shift?             ;  Nyle.module_eval{ @__mask_shift                    } ; end

.mouse_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


188
# File 'lib/nyle.rb', line 188

module_function def mouse_down?(button)     ;  Nyle.module_eval{ @__mouse_down   [button] == true } ; end

.mouse_press?(button) ⇒ Boolean

Returns:

  • (Boolean)


189
# File 'lib/nyle.rb', line 189

module_function def mouse_press?(button)    ;  Nyle.module_eval{ @__mouse_press  [button] == true } ; end

.mouse_release?(button) ⇒ Boolean

Returns:

  • (Boolean)


190
# File 'lib/nyle.rb', line 190

module_function def mouse_release?(button)  ;  Nyle.module_eval{ @__mouse_release[button] == true } ; end

.mouse_xObject



186
# File 'lib/nyle.rb', line 186

module_function def mouse_x                 ;  Nyle.module_eval{ @__mouse_x                       } ; end

.mouse_yObject



187
# File 'lib/nyle.rb', line 187

module_function def mouse_y                 ;  Nyle.module_eval{ @__mouse_y                       } ; end

.osObject



199
# File 'lib/nyle.rb', line 199

module_function def os                      ;  Nyle.module_eval{ @__os                            } ; end

.pixel(x, y) ⇒ Object



385
386
387
# File 'lib/nyle.rb', line 385

module_function def pixel(x, y)
  Nyle::INNER.module_eval{ _pixel(x, y) }
end

.pixel?(x, y, color) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
# File 'lib/nyle.rb', line 389

module_function def pixel?(x, y, color)
  c = Nyle::INNER.module_eval{ _pixel(x, y) }
  return (c == Cairo::Color.parse(color).to_s[0, 7] ? true : false)
end

.quitObject



439
440
441
442
# File 'lib/nyle.rb', line 439

module_function def quit
  @__frame.close if @__frame   # destroy
#    Gtk.main_quit
end

.rotate(angle) ⇒ Object



403
404
405
406
# File 'lib/nyle.rb', line 403

module_function def rotate(angle)
  cr = Nyle.module_eval{ @__cr }
  cr.rotate(angle)
end

.running_timeObject



198
# File 'lib/nyle.rb', line 198

module_function def running_time            ;  Nyle.module_eval{ @__running_time                  } ; end

.saveObject



206
207
208
209
210
211
# File 'lib/nyle.rb', line 206

module_function def save
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    yield
  end
end

.save_image(filename) ⇒ Object



364
365
366
367
# File 'lib/nyle.rb', line 364

module_function def save_image(filename)
  cr = Nyle.module_eval{ @__cr }
  cr.target.write_to_png(filename)
end

.scale(sx, sy) ⇒ Object



408
409
410
411
# File 'lib/nyle.rb', line 408

module_function def scale(sx, sy)
  cr = Nyle.module_eval{ @__cr }
  cr.scale(sx, sy)
end

.screen_heightObject



185
# File 'lib/nyle.rb', line 185

module_function def screen_height           ;  Nyle.module_eval{ @__screen_height                 } ; end

.screen_widthObject



184
# File 'lib/nyle.rb', line 184

module_function def screen_width            ;  Nyle.module_eval{ @__screen_width                  } ; end

.translate(tx, ty) ⇒ Object



398
399
400
401
# File 'lib/nyle.rb', line 398

module_function def translate(tx, ty)
  cr = Nyle.module_eval{ @__cr }
  cr.translate(tx, ty)
end

.wObject



201
# File 'lib/nyle.rb', line 201

module_function def screen_width            ;  Nyle.module_eval{ @__screen_width                  } ; end