module Nyle

'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

'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

Constants

DEFAULT_HEIGHT
DEFAULT_INTERVAL
DEFAULT_TITLE
DEFAULT_WIDTH
MAX_INTERVAL
MIN_INTERVAL
VERSION

Public Class Methods

h()
Alias for: screen_height
w()
Alias for: screen_width

Private Class Methods

_clear_key_state() click to toggle source
# File lib/nyle.rb, line 154
        def _clear_key_state
  @__key_down.clear
  @__key_down_last.clear
  @__key_press.clear
  @__key_release.clear
end
_clear_layer() click to toggle source
# File lib/nyle.rb, line 169
        def _clear_layer
  Screen::Layer.clear
end
_clear_mouse_state() click to toggle source
# File lib/nyle.rb, line 147
        def _clear_mouse_state
  @__mouse_down.clear
  @__mouse_down_last.clear
  @__mouse_press.clear
  @__mouse_release.clear
end
_clear_running_time() click to toggle source
# File lib/nyle.rb, line 165
        def _clear_running_time
  @__running_time = 0
end
_set_cr(cr) click to toggle source
# File lib/nyle.rb, line 73
        def _set_cr(cr)
  @__cr = cr
end
_set_frame(frame) click to toggle source

private methods for classes in module 'Nyle'

# File lib/nyle.rb, line 69
        def _set_frame(frame)
  @__frame = frame
end
_set_key_down(keyval, status) click to toggle source
# File lib/nyle.rb, line 91
        def _set_key_down(keyval, status)
  @__key_down[keyval] = status
end
_set_mouse_down(button, status) click to toggle source
# File lib/nyle.rb, line 87
        def _set_mouse_down(button, status)
  @__mouse_down[button] = status
end
_set_mouse_pos(x, y) click to toggle source
# File lib/nyle.rb, line 82
        def _set_mouse_pos(x, y)
  @__mouse_x = x
  @__mouse_y = y
end
_set_running_time(rt) click to toggle source
# File lib/nyle.rb, line 161
        def _set_running_time(rt)
  @__running_time = rt
end
_set_screen_size(w, h) click to toggle source
# File lib/nyle.rb, line 77
        def _set_screen_size(w, h)
  @__screen_width  = w
  @__screen_height = h
end
_update_key_state() click to toggle source
# File lib/nyle.rb, line 106
        def _update_key_state
  @__key_down.each_key do |k|
    change  = @__key_down[k] ^ @__key_down_last[k]           # Get change of key press status (XOR)
    press   = change &  @__key_down[k]                       # Detect change to press from release
    release = change & !@__key_down[k]                       # Detect change to release from press
    @__key_press[k] = press                                  # Set key_press status
    @__key_release[k] = release                              # Set key_release status
    @__key_down_last[k] = @__key_down[k]

    if @__key_down[Gdk::Keyval::KEY_Control_L] or
      @__key_down[Gdk::Keyval::KEY_Control_R]
      @__mask_control = true                                 # Set true to mask_control status
    else
      @__mask_control = false                                # Set false to mask_control status
    end

    if @__key_down[Gdk::Keyval::KEY_Shift_L] or
      @__key_down[Gdk::Keyval::KEY_Shift_R]
      @__mask_shift = true                                   # Set true to mask_shift status
    else
      @__mask_shift = false                                  # Set false to mask_shift status
    end

    if @__key_down[KEY_Right]
      @__cursor_x = +1                                       # Set +1 to cursor_x value
    elsif @__key_down[KEY_Left]
      @__cursor_x = -1                                       # Set -1 to cursor_x value
    else
      @__cursor_x =  0
    end

    if @__key_down[KEY_Down]
      @__cursor_y = +1                                       # Set +1 to cursor_y value
    elsif @__key_down[KEY_Up]
      @__cursor_y = -1                                       # Set -1 to cursor_y value
    else
      @__cursor_y =  0
    end
  end
end
_update_mouse_state() click to toggle source
# File lib/nyle.rb, line 95
        def _update_mouse_state
  @__mouse_down.each_key do |k|
    change  = @__mouse_down[k] ^ @__mouse_down_last[k]       # Get change of mouse press status (XOR)
    press   = change &  @__mouse_down[k]                     # Detect change to press from release
    release = change & !@__mouse_down[k]                     # Detect change to release from press
    @__mouse_press[k] = press                                # Set mouse_press status
    @__mouse_release[k] = release                            # Set mouse_release status
    @__mouse_down_last[k] = @__mouse_down[k]
  end
end

Public Instance Methods

clear() click to toggle source
# File lib/nyle.rb, line 390
                def clear
  Nyle.module_eval{ @__frame.current_screen.clear_screen }
end
cr() click to toggle source

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

# File lib/nyle.rb, line 177
def cr                      ;  Nyle.module_eval{ @__cr                            } ; end
create_frame(*args) click to toggle source
# File lib/nyle.rb, line 431
                def create_frame(*args)
  Nyle::Frame.new(*args)
end
create_screen(*args) click to toggle source
# File lib/nyle.rb, line 427
                def create_screen(*args)
  Nyle::Screen.new(*args)
end
cursor_x() click to toggle source
# File lib/nyle.rb, line 192
def cursor_x                ;  Nyle.module_eval{ @__cursor_x                      } ; end
cursor_y() click to toggle source
# File lib/nyle.rb, line 193
def cursor_y                ;  Nyle.module_eval{ @__cursor_y                      } ; end
draw_circle(x, y, r, weight: 2, color: :BLACK, fill: false, a: 1.0) click to toggle source
# File lib/nyle.rb, line 244
                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) click to toggle source
# File lib/nyle.rb, line 348
                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) click to toggle source
# File lib/nyle.rb, line 209
                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) click to toggle source
# File lib/nyle.rb, line 223
                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) click to toggle source
# File lib/nyle.rb, line 260
                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) click to toggle source
# File lib/nyle.rb, line 282
                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
key_down?(key) click to toggle source
# File lib/nyle.rb, line 186
def key_down?(key)          ; _check_key(   @__key_down,      key)                  ; end
key_press?(key) click to toggle source
# File lib/nyle.rb, line 187
def key_press?(key)         ; _check_key(   @__key_press,     key)                  ; end
key_release?(key) click to toggle source
# File lib/nyle.rb, line 188
def key_release?(key)       ; _check_key(   @__key_release,   key)                  ; end
layer(id = :__) { |layer| ... } click to toggle source
# File lib/nyle.rb, line 409
                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) click to toggle source
# File lib/nyle.rb, line 323
                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) click to toggle source
# File lib/nyle.rb, line 331
                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
main() click to toggle source
# File lib/nyle.rb, line 440
                def main
  Gtk.main
end
mask_control?() click to toggle source
# File lib/nyle.rb, line 190
def mask_control?           ;  Nyle.module_eval{ @__mask_control                  } ; end
mask_shift?() click to toggle source
# File lib/nyle.rb, line 191
def mask_shift?             ;  Nyle.module_eval{ @__mask_shift                    } ; end
mouse_down?(button) click to toggle source
# File lib/nyle.rb, line 183
def mouse_down?(button)     ; _check_button(@__mouse_down,    button)               ; end
mouse_press?(button) click to toggle source
# File lib/nyle.rb, line 184
def mouse_press?(button)    ; _check_button(@__mouse_press,   button)               ; end
mouse_release?(button) click to toggle source
# File lib/nyle.rb, line 185
def mouse_release?(button)  ; _check_button(@__mouse_release, button)               ; end
mouse_x() click to toggle source
# File lib/nyle.rb, line 180
def mouse_x                 ;  Nyle.module_eval{ @__mouse_x                       } ; end
mouse_y() click to toggle source
# File lib/nyle.rb, line 181
def mouse_y                 ;  Nyle.module_eval{ @__mouse_y                       } ; end
os() click to toggle source
# File lib/nyle.rb, line 195
def os                      ;  Nyle.module_eval{ @__os                            } ; end
pixel(x, y) click to toggle source
# File lib/nyle.rb, line 381
                def pixel(x, y)
  Nyle::INNER.module_eval{ _pixel(x, y) }
end
pixel?(x, y, color) click to toggle source
# File lib/nyle.rb, line 385
                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
quit() click to toggle source
# File lib/nyle.rb, line 435
                  def quit
    @__frame.close if @__frame   # destroy
#    Gtk.main_quit
  end
rotate(angle) click to toggle source
# File lib/nyle.rb, line 399
                def rotate(angle)
  cr = Nyle.module_eval{ @__cr }
  cr.rotate(angle)
end
running_time() click to toggle source
# File lib/nyle.rb, line 194
def running_time            ;  Nyle.module_eval{ @__running_time                  } ; end
save() { || ... } click to toggle source
# File lib/nyle.rb, line 202
                def save
  cr = Nyle.module_eval{ @__cr }
  cr.save do
    yield
  end
end
save_image(filename) click to toggle source
# File lib/nyle.rb, line 360
                def save_image(filename)
  cr = Nyle.module_eval{ @__cr }
  cr.target.write_to_png(filename)
end
scale(sx, sy) click to toggle source
# File lib/nyle.rb, line 404
                def scale(sx, sy)
  cr = Nyle.module_eval{ @__cr }
  cr.scale(sx, sy)
end
screen_height() click to toggle source
# File lib/nyle.rb, line 179
def screen_height           ;  Nyle.module_eval{ @__screen_height                 } ; end
Also aliased as: h
screen_width() click to toggle source
# File lib/nyle.rb, line 178
def screen_width            ;  Nyle.module_eval{ @__screen_width                  } ; end
Also aliased as: w
translate(tx, ty) click to toggle source
# File lib/nyle.rb, line 394
                def translate(tx, ty)
  cr = Nyle.module_eval{ @__cr }
  cr.translate(tx, ty)
end

Private Instance Methods

h()
Alias for: screen_height
w()
Alias for: screen_width