Class: Sortviz::Canvas
- Inherits:
-
Object
- Object
- Sortviz::Canvas
- Extended by:
- Forwardable
- Defined in:
- lib/sortviz/canvas.rb
Overview
Canvas is the portion of the screen that contains the sorting bar charts. It displays the partially sorted list of numbers and redraws on every iteration marking the currently selected bar (current index in the partially sorted list)
Constant Summary collapse
- MARGIN =
5
Instance Attribute Summary collapse
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#draw(partially_sorted, selection) ⇒ Object
Draws the partially sorted list and highlights the current index It also attempts to center the graph in the display area, does well but not always, sometimes it’ll be shifted to the right a bit.
-
#initialize(title, cursor, screen_dim) ⇒ Canvas
constructor
Initializes a new instance with a title to display, the current cursor object (
Sortviz::Cursor
) and the modified screen dimensions from the parent window/screen (standard screen created inSortviz::Visualizer
). -
#setup ⇒ Object
Does the initial setup of creating an actual curses window, adding a border to it and setting up non-blocking
Curses::Window#getch
.
Constructor Details
#initialize(title, cursor, screen_dim) ⇒ Canvas
Initializes a new instance with a title to display, the current cursor object (Sortviz::Cursor
) and the modified screen dimensions from the parent window/screen (standard screen created in Sortviz::Visualizer
)
14 15 16 17 18 19 |
# File 'lib/sortviz/canvas.rb', line 14 def initialize(title, cursor, screen_dim) @screen_dim = screen_dim @cursor = cursor @title = title @red_highlight = Curses.color_pair(Curses.const_get("COLOR_RED")) end |
Instance Attribute Details
#window ⇒ Object (readonly)
Returns the value of attribute window.
9 10 11 |
# File 'lib/sortviz/canvas.rb', line 9 def window @window end |
Instance Method Details
#draw(partially_sorted, selection) ⇒ Object
Draws the partially sorted list and highlights the current index It also attempts to center the graph in the display area, does well but not always, sometimes it’ll be shifted to the right a bit.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sortviz/canvas.rb', line 34 def draw(partially_sorted, selection) clear draw_title len = partially_sorted.join.center(4).length # We draw bottom up, this sets our y-position at the very bottom of # the canvas and our x-position half way through the canvas @cursor.move(@window.maxy - 1, (@window.maxx - len) / MARGIN) partially_sorted.each_with_index do |number, i| draw_number(number) (number, i == selection) end Curses.doupdate end |
#setup ⇒ Object
Does the initial setup of creating an actual curses window, adding a border to it and setting up non-blocking Curses::Window#getch
23 24 25 26 27 28 29 |
# File 'lib/sortviz/canvas.rb', line 23 def setup @window ||= Curses::Window.new( @screen_dim[:lines] - MARGIN, @screen_dim[:cols] - MARGIN, @cursor.y, @cursor.x) @window.nodelay = 1 # Non-blocking mode for #getch end |