Module: Visuals

Includes:
Ncurses, SDL2
Defined in:
lib/curses.rb,
lib/frontend.rb,
lib/graphical.rb

Constant Summary collapse

KEY_ESCAPE =
27
KEY_RETURN =
10
KEY_TAB =
9
KEY_PAGEDOWN =
KEY_NPAGE
KEY_PAGEUP =
KEY_PPAGE
COLORS =
{
black:   [   0,    0,    0],
red:     [ 180,  100,  100],
green:   [ 100,  180,  100],
yellow:  [ 180,  180,  100],
blue:    [ 100,  100,  180],
magenta: [ 180,  100,  180],
cyan:    [ 100,  180,  180],
white:   [ 255,  255,  255],
grey:    [ 158,  158,  158] }
WINDOW =
Window.create("visuals",
Window::POS_CENTERED,
Window::POS_CENTERED,
1324, 768, 0)
GO =
WINDOW.create_renderer(-1,
Renderer::Flags::ACCELERATED|Renderer::Flags::TARGETTEXTURE)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.columnObject



31
# File 'lib/curses.rb', line 31

def self.column; Ncurses.getcurx $screen end

.exitObject



33
34
35
36
37
38
# File 'lib/curses.rb', line 33

def self.exit
  Ncurses.echo
  Ncurses.nocbreak
  Ncurses.nl
  Ncurses.endwin
end

.heightObject



29
# File 'lib/curses.rb', line 29

def self.height; Ncurses.LINES end

.init(args = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/curses.rb', line 83

def self.init args={}
  $creen = Ncurses.initscr
  Ncurses.cbreak
  Ncurses.noecho
  Ncurses.nl
  Ncurses.curs_set 0
  Ncurses.start_color
  Ncurses.stdscr.intrflush(false)
  Ncurses.stdscr.keypad(true)

  $default = [
    color_for( args[:foreground] || :grey80 ),
    color_for( args[:background] || :grey10 ) ]
  $color = $default.dup

  COLORS.each_with_index{ |c,i|
    $pairs << [ i, $default[1] ]
    Ncurses.init_color i, *c[1]
    Ncurses.init_pair i, i, $default[1] }

  Ncurses.bkgd Ncurses::COLOR_PAIR( pair_for $default )

end

.rowObject



30
# File 'lib/curses.rb', line 30

def self.row; Ncurses.getcury $screen end

.widthObject



28
# File 'lib/curses.rb', line 28

def self.width;  Ncurses.COLS end

.xObject



22
# File 'lib/graphical.rb', line 22

def self.x; $x end

.yObject



23
# File 'lib/graphical.rb', line 23

def self.y; $y end

Instance Method Details

#background(color) ⇒ Object



70
71
72
# File 'lib/curses.rb', line 70

def background color;
  $color[1] = (color ? color_for( color ) : $default[1]); change
end

#changeObject



65
# File 'lib/curses.rb', line 65

def change; Ncurses.color_set pair_for( $color ), 0 end

#clear(type = :screen) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/curses.rb', line 58

def clear type=:screen;
  case type
    when :screen; Ncurses.erase
    when :line; Ncurses.clrtoeol
    when :down; Ncurses.clrtobot
  end
end

#color(c) ⇒ Object



77
78
79
80
81
# File 'lib/curses.rb', line 77

def color c
  c = [c] unless c.is_a? Array and c.count == 2
  foreground c[0] if c[0]
  background c[1] if c[1]
end

#color_for(c) ⇒ Object



25
26
27
28
29
# File 'lib/graphical.rb', line 25

def color_for c
  return unless c
  c = COLORS[c] if c.is_a?( Symbol )
  c.map{ |c| (c/1000.0*256).to_i }
end

#foreground(color) ⇒ Object



73
74
75
# File 'lib/curses.rb', line 73

def foreground color;
  $color[0] = (color ? color_for( color ) : $default[0]); change
end

#get_backgroundObject



68
# File 'lib/curses.rb', line 68

def get_background; $color[1] end

#get_foregroundObject



69
# File 'lib/curses.rb', line 69

def get_foreground; $color[0] end

#move(col, row) ⇒ Object



56
# File 'lib/curses.rb', line 56

def move x=0,y=0; Ncurses.move y, x end

#read(input) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/curses.rb', line 40

def read input
  case input
    when :key
      key = Ncurses.getch
      key = key.chr if key.between? 32, 126
      return key
    when :line
      Ncurses.echo
      Ncurses.curs_set 1
      str = Readline.readline
      Ncurses.curs_set 0
      Ncurses.noecho
      return str
  end
end

#refreshObject



57
# File 'lib/curses.rb', line 57

def refresh; Ncurses.refresh end

#show(str) ⇒ Object



66
# File 'lib/curses.rb', line 66

def show string; Ncurses.addstr string.to_s end