Module: Nyle
- Extended by:
- DefineTable
- Defined in:
- lib/nyle/frame.rb,
lib/nyle.rb,
lib/nyle/screen.rb,
lib/nyle/version.rb,
lib/nyle/define_table.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: DefineTable, 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.5"
Class Method Summary collapse
- .clear ⇒ Object
-
.cr ⇒ Object
module functions for user (need module_eval{} statement to use including Nyle).
- .create_frame(*args) ⇒ Object
- .create_screen(*args) ⇒ Object
- .cursor_x ⇒ Object
- .cursor_y ⇒ Object
- .draw_circle(x, y, r, weight: 2, color: :BLACK, fill: false, a: 1.0) ⇒ Object
- .draw_image(x, y, pixbuf, pos: :CORNER) ⇒ Object
- .draw_line(x1, y1, x2, y2, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0) ⇒ Object
- .draw_rect(x, y, w, h, weight: 2, cap: :BUTT, color: :BLACK, round: 0, fill: false, a: 1.0) ⇒ Object
- .draw_shape(points, weight: 2, cap: :BUTT, color: :BLACK, a: 1.0, close: false, fill: false) ⇒ Object
- .draw_text(x, y, str, size: 32, color: :BLACK, a: 1.0, font: "sans-serif", italic: false, bold: false) ⇒ Object
- .h ⇒ Object
- .key_down?(key) ⇒ Boolean
- .key_press?(key) ⇒ Boolean
- .key_release?(key) ⇒ Boolean
- .layer(id = :__) ⇒ Object
- .load_image(filename, color_key: nil, sx: 1.0, sy: 1.0, cx: nil, cy: nil, cw: nil, ch: nil) ⇒ Object
- .load_image_tiles(filename, xcount, ycount, color_key: nil, sx: 1.0, sy: 1.0) ⇒ Object
- .main ⇒ Object
- .mask_control? ⇒ Boolean
- .mask_shift? ⇒ Boolean
- .mouse_down?(button) ⇒ Boolean
- .mouse_press?(button) ⇒ Boolean
- .mouse_release?(button) ⇒ Boolean
- .mouse_x ⇒ Object
- .mouse_y ⇒ Object
- .os ⇒ Object
- .pixel(x, y) ⇒ Object
- .pixel?(x, y, color) ⇒ Boolean
- .quit ⇒ Object
- .rotate(angle) ⇒ Object
- .running_time ⇒ Object
- .save ⇒ Object
- .save_image(filename) ⇒ Object
- .scale(sx, sy) ⇒ Object
- .screen_height ⇒ Object
- .screen_width ⇒ Object
- .translate(tx, ty) ⇒ Object
- .w ⇒ Object
Methods included from DefineTable
Class Method Details
.clear ⇒ Object
390 391 392 |
# File 'lib/nyle.rb', line 390 module_function def clear Nyle.module_eval{ @__frame.current_screen.clear_screen } end |
.cr ⇒ Object
module functions for user (need module_eval{} statement to use including Nyle)
177 |
# File 'lib/nyle.rb', line 177 module_function def cr ; Nyle.module_eval{ @__cr } ; end |
.create_frame(*args) ⇒ Object
431 432 433 |
# File 'lib/nyle.rb', line 431 module_function def create_frame(*args) Nyle::Frame.new(*args) end |
.create_screen(*args) ⇒ Object
427 428 429 |
# File 'lib/nyle.rb', line 427 module_function def create_screen(*args) Nyle::Screen.new(*args) end |
.cursor_x ⇒ Object
192 |
# File 'lib/nyle.rb', line 192 module_function def cursor_x ; Nyle.module_eval{ @__cursor_x } ; end |
.cursor_y ⇒ Object
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
348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/nyle.rb', line 348 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 302 |
# 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) cr.new_path end end |
.h ⇒ Object
198 |
# File 'lib/nyle.rb', line 198 module_function def screen_height ; Nyle.module_eval{ @__screen_height } ; end |
.key_down?(key) ⇒ Boolean
186 |
# File 'lib/nyle.rb', line 186 module_function def key_down?(key) ; _check_key( @__key_down, key) ; end |
.key_press?(key) ⇒ Boolean
187 |
# File 'lib/nyle.rb', line 187 module_function def key_press?(key) ; _check_key( @__key_press, key) ; end |
.key_release?(key) ⇒ Boolean
188 |
# File 'lib/nyle.rb', line 188 module_function def key_release?(key) ; _check_key( @__key_release, key) ; end |
.layer(id = :__) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/nyle.rb', line 409 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
323 324 325 326 327 328 329 |
# File 'lib/nyle.rb', line 323 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
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/nyle.rb', line 331 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 |
.main ⇒ Object
440 441 442 |
# File 'lib/nyle.rb', line 440 module_function def main Gtk.main end |
.mask_control? ⇒ Boolean
190 |
# File 'lib/nyle.rb', line 190 module_function def mask_control? ; Nyle.module_eval{ @__mask_control } ; end |
.mask_shift? ⇒ Boolean
191 |
# File 'lib/nyle.rb', line 191 module_function def mask_shift? ; Nyle.module_eval{ @__mask_shift } ; end |
.mouse_down?(button) ⇒ Boolean
183 |
# File 'lib/nyle.rb', line 183 module_function def mouse_down?() ; (@__mouse_down, ) ; end |
.mouse_press?(button) ⇒ Boolean
184 |
# File 'lib/nyle.rb', line 184 module_function def mouse_press?() ; (@__mouse_press, ) ; end |
.mouse_release?(button) ⇒ Boolean
185 |
# File 'lib/nyle.rb', line 185 module_function def mouse_release?() ; (@__mouse_release, ) ; end |
.mouse_x ⇒ Object
180 |
# File 'lib/nyle.rb', line 180 module_function def mouse_x ; Nyle.module_eval{ @__mouse_x } ; end |
.mouse_y ⇒ Object
181 |
# File 'lib/nyle.rb', line 181 module_function def mouse_y ; Nyle.module_eval{ @__mouse_y } ; end |
.os ⇒ Object
195 |
# File 'lib/nyle.rb', line 195 module_function def os ; Nyle.module_eval{ @__os } ; end |
.pixel(x, y) ⇒ Object
381 382 383 |
# File 'lib/nyle.rb', line 381 module_function def pixel(x, y) Nyle::INNER.module_eval{ _pixel(x, y) } end |
.pixel?(x, y, color) ⇒ Boolean
385 386 387 388 |
# File 'lib/nyle.rb', line 385 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 |
.quit ⇒ Object
435 436 437 438 |
# File 'lib/nyle.rb', line 435 module_function def quit @__frame.close if @__frame # destroy # Gtk.main_quit end |
.rotate(angle) ⇒ Object
399 400 401 402 |
# File 'lib/nyle.rb', line 399 module_function def rotate(angle) cr = Nyle.module_eval{ @__cr } cr.rotate(angle) end |
.running_time ⇒ Object
194 |
# File 'lib/nyle.rb', line 194 module_function def running_time ; Nyle.module_eval{ @__running_time } ; end |
.save ⇒ Object
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
360 361 362 363 |
# File 'lib/nyle.rb', line 360 module_function def save_image(filename) cr = Nyle.module_eval{ @__cr } cr.target.write_to_png(filename) end |
.scale(sx, sy) ⇒ Object
404 405 406 407 |
# File 'lib/nyle.rb', line 404 module_function def scale(sx, sy) cr = Nyle.module_eval{ @__cr } cr.scale(sx, sy) end |
.screen_height ⇒ Object
179 |
# File 'lib/nyle.rb', line 179 module_function def screen_height ; Nyle.module_eval{ @__screen_height } ; end |
.screen_width ⇒ Object
178 |
# File 'lib/nyle.rb', line 178 module_function def screen_width ; Nyle.module_eval{ @__screen_width } ; end |
.translate(tx, ty) ⇒ Object
394 395 396 397 |
# File 'lib/nyle.rb', line 394 module_function def translate(tx, ty) cr = Nyle.module_eval{ @__cr } cr.translate(tx, ty) end |
.w ⇒ Object
197 |
# File 'lib/nyle.rb', line 197 module_function def screen_width ; Nyle.module_eval{ @__screen_width } ; end |