Class: GPLType::TypingWindow
- Inherits:
-
Object
- Object
- GPLType::TypingWindow
- Defined in:
- lib/curses_color.rb
Overview
Main Typing Window
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(section, score_widnow) ⇒ TypingWindow
constructor
A new instance of TypingWindow.
- #next_line ⇒ Object
- #show ⇒ Object
- #show_result(new_record) ⇒ Object
- #start ⇒ Object
- #update ⇒ Object
- #update_cursor ⇒ Object
Constructor Details
#initialize(section, score_widnow) ⇒ TypingWindow
Returns a new instance of TypingWindow.
462 463 464 465 466 467 468 469 470 |
# File 'lib/curses_color.rb', line 462 def initialize(section, score_widnow) @section = section @subject = section.subject @lines = section.body_lines @score_window = score_widnow @ln = 0 @typing_line = TypingLine.new(@lines[0]) @typed_chars = 0 end |
Instance Method Details
#destroy ⇒ Object
651 652 653 654 655 |
# File 'lib/curses_color.rb', line 651 def destroy @window.clear @window.close @window = nil end |
#next_line ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/curses_color.rb', line 520 def next_line @typed_chars += @lines[@ln].size + 1 @ln += 1 if @ln == @lines.size false else @typing_line.next_line(@lines[@ln]) update true end end |
#show ⇒ Object
472 473 474 475 476 477 478 479 480 |
# File 'lib/curses_color.rb', line 472 def show return if @window @window = Curses::stdscr.subwin(Curses::stdscr.height - 7, Curses::stdscr.width - 2, 6, 1) update @window.refresh end |
#show_result(new_record) ⇒ Object
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
# File 'lib/curses_color.rb', line 616 def show_result(new_record) lines = []; line_color = [] lines << ""; line_color << :default lines << "You finished typing:"; line_color << :default lines << "\"#{@subject}\""; line_color << :default lines << ""; line_color << :default if new_record lines << "You made new record!"; line_color << :highscore lines << ""; line_color << :default end lines << "Push any key to go back to menu."; line_color << :default w = (lines.map{|line| line.size}).max + 6 h = lines.size + 2 x = (Curses::stdscr.width - w) / 2 y = (Curses::stdscr.height - h) / 2 win = Curses::stdscr.subwin(h, w, y, x) win.box("|"[0], "-"[0]) win.set_title("CONGRATULATIONS!", :center) win.setpos(0, 0) lines.each_index do |i| line = lines[i] color = line_color[i] win.next_line win.addstr(line, color, :center, nil) end win.refresh c = Curses::getch win.clear win.close end |
#start ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/curses_color.rb', line 535 def start c = Curses::getch @start_time = Time.new while true if Curses::Key::match?(:escape, c) break elsif Curses::Key::match?(:backspace, c) if @typing_line.col == 0 @score_window.inc_mistype Curses::beep else @typing_line.backspace(@window) end elsif Curses::Key::match?(:printable, c) if @typing_line.col == @lines[@ln].size # end of this line @score_window.inc_mistype Curses::beep else if @typing_line.append(@window, c) @score_window.inc_mistype end end elsif Curses::Key::match?([:enter, :space], c) if @typing_line.col == @lines[@ln].size # end of this line if @typing_line.correct? finish = !next_line if finish chars = @typed_chars elapsed_sec = (Time.now - @start_time).to_i chars_min = elapsed_sec != 0 ? (chars * 60 / elapsed_sec).to_i : 0 @score_window.set_current_score(elapsed_sec, chars_min) @window.clear if elapsed_sec < @section.high_score[:sec] @section.high_score[:sec] = elapsed_sec @section.high_score[:chars] = chars_min new_record = true else new_record = false end show_result(new_record) break end else @score_window.inc_mistype Curses::beep end else if @typing_line.append(@window, " ") @score_window.inc_mistype end end end chars = @typed_chars + @typing_line.col elapsed_sec = (Time.now - @start_time).to_i chars_min = elapsed_sec != 0 ? (chars * 60 / elapsed_sec).to_i : 0 @score_window.set_current_score(elapsed_sec, chars_min) @window.refresh c = nil while c == nil begin timeout(0.1) { c = Curses::getch } rescue Timeout::Error tmp = (Time.now - @start_time).to_i if elapsed_sec != tmp elapsed_sec = tmp chars_min = elapsed_sec != 0 ? (chars * 60 / elapsed_sec).to_i : 0 @score_window.set_current_score(elapsed_sec, chars_min) @window.refresh end end end end end |
#update ⇒ Object
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/curses_color.rb', line 496 def update @window.clear @window.set_title("= #{@subject} =", :left) n = @ln < 2 ? 0 : @ln - 2 y = 2 while (y + 2) < @window.height @window.setpos(y, 1) @window.addstr(@lines[n]) if n < @ln @window.setpos(y+1, 1) @window.addstr(@lines[n], :correct) end n += 1 y += 3 end update_cursor end |
#update_cursor ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/curses_color.rb', line 482 def update_cursor case(@ln) when 0 y = 3 when 1 y = 6 else y = 9 end x = @typing_line.col + 1 @window.setpos(y, x) @window.refresh end |