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.2"

Class Method Summary collapse

Class Method Details

.clearObject



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

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)



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

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

.create_frame(*args) ⇒ Object



412
413
414
# File 'lib/nyle.rb', line 412

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

.create_screen(*args) ⇒ Object



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

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

.cursor_xObject



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

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

.cursor_yObject



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

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/nyle.rb', line 244

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



347
348
349
350
351
352
353
354
355
356
357
# File 'lib/nyle.rb', line 347

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



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/nyle.rb', line 209

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/nyle.rb', line 223

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/nyle.rb', line 260

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



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/nyle.rb', line 282

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)
  end
end

.hObject



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

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

.key_down?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.key_press?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.key_release?(keyval) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



322
323
324
325
326
327
328
# File 'lib/nyle.rb', line 322

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



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/nyle.rb', line 330

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



421
422
423
# File 'lib/nyle.rb', line 421

module_function def main
  Gtk.main
end

.mask_control?Boolean

Returns:

  • (Boolean)


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

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

.mask_shift?Boolean

Returns:

  • (Boolean)


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

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

.mouse_down?(button) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.mouse_press?(button) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.mouse_release?(button) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.mouse_xObject



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

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

.mouse_yObject



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

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

.osObject



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

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

.pixel(x, y) ⇒ Object



380
381
382
# File 'lib/nyle.rb', line 380

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

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

Returns:

  • (Boolean)


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

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



416
417
418
419
# File 'lib/nyle.rb', line 416

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

.rotate(angle) ⇒ Object



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

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

.running_timeObject



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

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

.saveObject



202
203
204
205
206
207
# File 'lib/nyle.rb', line 202

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

.save_image(filename) ⇒ Object



359
360
361
362
# File 'lib/nyle.rb', line 359

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

.scale(sx, sy) ⇒ Object



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

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

.screen_heightObject



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

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

.screen_widthObject



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

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

.translate(tx, ty) ⇒ Object



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

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

.wObject



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

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