sup

A curses threads-with-tags style email client

ncursesw-ruby.git

git clone https://supmua.dev/git/ncursesw-ruby/

examples/rain.rb (6960B) - raw

      1 #!/usr/bin/env ruby
      2 
      3 # $Id: rain.rb,v 1.6 2005-08-22 21:41:49 t-peters Exp $
      4 
      5 # This program is a translation of the popular rain.c demo program from the
      6 # ncurses library distribution.
      7 #
      8 # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
      9 #
     10 # I do not impose any additional restrictions over the copyright of the
     11 # ncurses library distribution. It has the following Copyright notice
     12 
     13 #/****************************************************************************
     14 # * Copyright (c) 1998 Free Software Foundation, Inc.                        *
     15 # *                                                                          *
     16 # * Permission is hereby granted, free of charge, to any person obtaining a  *
     17 # * copy of this software and associated documentation files (the            *
     18 # * "Software"), to deal in the Software without restriction, including      *
     19 # * without limitation the rights to use, copy, modify, merge, publish,      *
     20 # * distribute, distribute with modifications, sublicense, and/or sell       *
     21 # * copies of the Software, and to permit persons to whom the Software is    *
     22 # * furnished to do so, subject to the following conditions:                 *
     23 # *                                                                          *
     24 # * The above copyright notice and this permission notice shall be included  *
     25 # * in all copies or substantial portions of the Software.                   *
     26 # *                                                                          *
     27 # * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
     28 # * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
     29 # * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
     30 # * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
     31 # * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
     32 # * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
     33 # * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
     34 # *                                                                          *
     35 # * Except as contained in this notice, the name(s) of the above copyright   *
     36 # * holders shall not be used in advertising or otherwise to promote the     *
     37 # * sale, use or other dealings in this Software without prior written       *
     38 # * authorization.                                                           *
     39 # ****************************************************************************/
     40 
     41 
     42 
     43 require "ncursesw"
     44 
     45 
     46 # A class responsible for raindrop drawing
     47 class Raindrop
     48   def initialize (window, color_pair = nil)
     49     @window = window
     50     @color_pair = color_pair
     51     lines   = []
     52     columns = []
     53     window.getmaxyx(lines,columns)
     54     lines = (lines[0] <= 4) ? 1 : (lines[0] - 4)
     55     columns = (columns[0] <= 4) ? 1 : (columns[0] - 4)
     56     @current_phase = 0
     57     @x = rand(columns)+2
     58     @y = rand(lines)+2
     59   end
     60 
     61   # draw_next_phase draws the next phase of a raindrop. If this was the last
     62   # phase, returns 0, otherwise returns the raindrop.
     63   def draw_next_phase
     64     if (@color_pair)
     65       if Ncurses.respond_to?(:color_set)
     66         @window.color_set(@color_pair, nil)
     67       else
     68         @window.attrset(Ncurses.COLOR_PAIR(@color_pair))
     69       end
     70     end
     71     if (DRAWING_PROCS[@current_phase].call(@window,@y,@x))
     72       @current_phase += 1
     73       self
     74     end
     75   end
     76 
     77   DRAWING_PROCS = [
     78     Proc.new{|window,y,x|
     79       window.mvaddstr(y,x, ".")
     80     },
     81     Proc.new{|window,y,x|
     82       window.mvaddstr(y, x, "o")
     83     },
     84     Proc.new{|window,y,x|
     85       window.mvaddstr(y, x, "O")
     86     },
     87     Proc.new{|window,y,x|
     88       window.mvaddstr(y-1, x,    "-")
     89       window.mvaddstr(y,   x-1, "|.|")
     90       window.mvaddstr(y+1, x,    "-")
     91     },
     92     Proc.new{|window,y,x|
     93       window.mvaddstr(y-2, x,     "-")
     94       window.mvaddstr(y-1, x-1,  "/ \\")
     95       window.mvaddstr(y,   x-2,  "| O |")
     96       window.mvaddstr(y+1, x-1,  "\\ /")
     97       window.mvaddstr(y+2, x,     "-")
     98     },
     99     Proc.new{|window,y,x|
    100       window.mvaddstr(y-2, x,     " ")
    101       window.mvaddstr(y-1, x-1,  "   ")
    102       window.mvaddstr(y,   x-2, "     ")
    103       window.mvaddstr(y+1, x-1,  "   ")
    104       window.mvaddstr(y+2, x,     " ")
    105       nil   # signal the last raindrop phase
    106     }
    107   ]
    108   NUMBER_OF_PHASES = DRAWING_PROCS.size - 1
    109 end
    110 
    111 
    112 # This class creates raindrops and tells them to draw on the screen
    113 class Rain
    114   AVERAGE_RAINDROP_SPACE = 475.1  # 4 simultaneous raindrops in a 80x24 Window
    115 
    116   def Rain.sigwinch_handler(sig = nil)
    117     ObjectSpace.each_object(Rain){|rain|
    118       rain.window_size_changed = true
    119     }
    120   end
    121 
    122   attr_writer :window_size_changed
    123 
    124   def initialize(window)
    125     @window = window
    126     @window_size_changed = true
    127     @raindrops = []
    128     @has_colors = Ncurses.has_colors?
    129     if (@has_colors)
    130       @current_color = 1
    131     end
    132   end
    133 
    134   def fall_for_a_moment
    135     adjust_to_new_window_size if (@window_size_changed)
    136 
    137     current_number_of_new_raindrops.times{
    138       if (@has_colors)
    139 	@raindrops.push(Raindrop.new(@window, @current_color))
    140 	@current_color = 3 - @current_color  # alternate between 1 and 2
    141       else
    142 	@raindrops.push(Raindrop.new(@window))
    143       end
    144     }
    145 
    146     @raindrops = @raindrops.collect{|raindrop|
    147       raindrop.draw_next_phase
    148     }.compact # erase raindrops that have expired from the list
    149   end
    150 
    151   def adjust_to_new_window_size
    152     @window_size_changed = false
    153     window_size = @window.getmaxx * @window.getmaxy
    154     average_number_of_raindrops = window_size / AVERAGE_RAINDROP_SPACE
    155     @average_number_of_new_raindrops =
    156       average_number_of_raindrops / Raindrop::NUMBER_OF_PHASES
    157   end
    158 
    159   def current_number_of_new_raindrops
    160     num_floor = @average_number_of_new_raindrops.floor
    161     num_ceil  = @average_number_of_new_raindrops.ceil
    162     chance = @average_number_of_new_raindrops - num_floor
    163     if (rand > chance)
    164         num_floor
    165     else
    166       num_ceil
    167     end
    168   end
    169 
    170   def fall(pause = 0.1)
    171     begin
    172       fall_for_a_moment
    173       @window.refresh
    174       sleep(pause)
    175     end while (true)
    176   end
    177 end
    178 
    179 Ncurses.initscr
    180 begin
    181   if (Ncurses.has_colors?)
    182     bg = Ncurses::COLOR_BLACK
    183     Ncurses.start_color
    184     if (Ncurses.respond_to?("use_default_colors"))
    185       if (Ncurses.use_default_colors == Ncurses::OK)
    186         bg = -1
    187       end
    188     end
    189     Ncurses.init_pair(1, Ncurses::COLOR_BLUE, bg);
    190     Ncurses.init_pair(2, Ncurses::COLOR_CYAN, bg);
    191   end
    192   Ncurses.nl()
    193   Ncurses.noecho()
    194   Ncurses.curs_set(0)
    195   Ncurses.stdscr.nodelay(true)
    196 
    197   rain = Rain.new(Ncurses.stdscr)
    198 
    199   begin
    200     case(Ncurses.getch())
    201     when 'q'.ord, 'Q'.ord
    202       Ncurses.curs_set(1)
    203       Ncurses.endwin()
    204       exit
    205     when 's'.ord
    206       Ncurses.stdscr.nodelay(false)
    207     when ' '.ord
    208       Ncurses.stdscr.nodelay(true)
    209     when Ncurses::KEY_RESIZE
    210       Rain.sigwinch_handler
    211     end
    212     sleep(0.050)
    213     rain.fall_for_a_moment
    214     Ncurses.refresh
    215   end while true
    216 ensure
    217   Ncurses.curs_set(1)
    218   Ncurses.endwin()
    219 end