require 'gtk' require 'drb' require 'amstd/errutil' module Gtk class Object def sigconn( sig ) signal_connect( sig ) {|args| begin yield args rescue StandardError, ScriptError => err $stderr.puts err.message + " (#{err.type})" err.backtrace.each do |i| $stderr.puts i end end } end end end class GtkRubyRobotView include DRbUndumped def initialize @r = [] @status = :idle @scan_timer = nil @explode_timer = nil @exp_gc = nil create_gui register_signal_handlers @win.show_all DRb.start_service @core = DRbObject.new( nil, ARGV[0] || 'druby://harmony:6852' ) @main_timer = Gtk.timeout_add( 500 ) { @core.add_canvas self false } start_explode_timer end def main Gtk.main end def inspect "#<#{type}:#{__id__}>" end COLORS = [ Gdk::Color.new( 255*255, 0, 0 ), Gdk::Color.new( 0, 255*255, 0 ), Gdk::Color.new( 0, 0, 255*255 ), Gdk::Color.new( 255*255, 0, 255*255 ) ] def create_gui @width = width = 506 @height = height = 506 @win = Gtk::Window.new( Gtk::WINDOW_TOPLEVEL ) @win.set_title 'drawing test' @win.border_width 0 #win.set_usize width, height @box = Gtk::VBox.new( false, 0 ) @win.add @box @area = Gtk::DrawingArea.new @area.set_usize width, height @box.pack_start @area, false, false, 0 @buf = Gdk::Pixmap.new( @win.window, width, height, Gdk::Visual.get_system.depth ) labelwidth = 150 labelheight = 20 COLORS.each do |c| l = Gtk::Label.new('') l.set_usize labelwidth, labelheight l.set_alignment 0.1, 0.5 b = Gtk::ProgressBar.new b.set_value 0 b.set_usize @width - labelwidth - 30, labelheight x = Gtk::HBox.new( false, 0 ) x.pack_start l, false, false, 0 x.pack_start b, false, false, 0 @r.push RobotItem.new( @win, @buf, l, b, c ) @box.pack_start x, false, false, 5 end @button_box = Gtk::HBox.new( false, 0 ) @box.pack_start @button_box, false, false, 5 @b_start = Gtk::Button.new( 'start' ) @button_box.pack_start @b_start, false, false, 10 @b_quit = Gtk::Button.new( 'quit' ) @button_box.pack_start @b_quit, false, false, 5 end def register_signal_handlers @win.sigconn( 'destroy' ) { exit 0 } @area.sigconn( 'expose_event' ) {|w,event| clear remap } @b_start.sigconn( 'clicked' ) { case @status when :idle then unless @core.start self then puts 'isolate' else show_start end when :started ; else @core.initNew self show_initNew end } @b_quit.sigconn( 'clicked' ) { @core.shutdown self sleep 1 exit } end def redraw clear @r.each do |r| r.draw @buf end remap end def clear # @buf.draw_rectangle @win.style.white_gc, true, 0, 0, @width, @height @buf.draw_rectangle @win.style.black_gc, true, 0, 0, @width, @height end def remap @area.window.draw_pixmap @win.style.black_gc, @buf, 0,0,0,0, @width, @height end def update( *commands ) # print '.'; $stdout.flush protect( 'update' ) { commands.each do |args| __send__ 'show_' + args.shift, *args end } end def show_start @status = :start @b_start.set_sensitive false end def show_initNew @status = :idle end def show_close sleep 5 exit 0 end def show_newrobot( ident, name ) @r[ident].name = name.to_s end def show_robot( ident, x, y ) x /= 2; y /= 2 @r[ident].pos x, y remap end def show_scan( ident, x, y, x1, y1, x2, y2 ) @r[ident].scan x1/2, y1/2, x2/2, y2/2 redraw_robots remap end def redraw_robots @r.each do |r| r.redraw_body end end def show_missile( ident, x, y ) x /= 2; y /= 2 @r[ident].missile x, y remap end def show_damage( ident, str, point ) @r[ident].rest_hp = (100 - point) end Q = [nil, nil, nil] def show_explode( ident, x, y ) @r[ident].missile nil, nil x /= 2; y /= 2 draw_explode x, y (Q[-1] ||= []).push [x,y] remap end def start_explode_timer @explode_timer = Gtk.timeout_add( 350 ) { Q.push nil i = Q.shift if i then r = EXPLODE_R i.each do |x,y| # @buf.draw_arc @win.style.white_gc, true, @buf.draw_arc @win.style.black_gc, true, x - r/2, y - r/2, r, r, 0, 360*64 end end true } end EXPLODE_R = 16 EXPLODE_COLOR = [ Gdk::Color.new( 255*255, 255*255, 0 ), Gdk::Color.new( 255*255, 255*127, 0 ), Gdk::Color.new( 255*255, 0, 0 ) ] def draw_explode( x, y ) unless @exp_gc then @exp_gc = EXPLODE_COLOR.collect {|c| Gdk::Colormap.get_system.alloc_color c, true, true gc = Gdk::GC.new( @buf ) gc.set_foreground c gc } end r = EXPLODE_R @exp_gc.each do |gc| @buf.draw_arc gc, true, x - r/2, y - r/2, r, r, 0, 360*64 r -= 6 end end def show_win( ident, d ) @status = :wait puts "robot \##{ident} win" @b_start.set_sensitive true end def show_dputs( r, str ) # $stderr.puts "#{r}: #{str}" end end class RobotItem def initialize( win, map, label, bar, c ) @win = win @map = map @label = label @bar = bar @color = c @pos = nil @m = nil @scan = nil @black_gc = nil @color_gc = nil end def black_gc @white_gc ||= @win.style.white_gc end def white_gc @black_gc ||= @win.style.black_gc end def color_gc @color_gc ||= new_pen(@color) end def scan_gc @scan_gc ||= new_pen( Gdk::Color.new(255*191, 255*191, 255*191) ) end def new_pen( c ) Gdk::Colormap.get_system.alloc_color c, true, true gc = Gdk::GC.new( @map ) gc.set_foreground c gc.set_background c gc end def name=( n ) s = @label.style.dup s.set_fg 0, @color.red, @color.green, @color.blue # s.set_bg 0, 255*255, 0, 0 @label.set_style s @label.set_text n @bar.set_value 100 end def rest_hp=( n ) if n <= 0 then @pos = nil draw_scan @s1, white_gc if @s1 draw_scan @s2, white_gc if @s2 @s1 = s2 = nil end @bar.set_value n end def draw( map ) draw_body @pos, color_gc if @pos draw_missile @m, black_gc if @m draw_scan @s1, color_gc if @s1 draw_scan @s2, color_gc if @s2 end def pos( x, y ) if x and @pos then draw_body @pos, white_gc end if x then @pos = [x,y] draw_body @pos, color_gc end end def draw_body( pos, gc ) @map.draw_rectangle gc, true, pos[0] - 3, pos[1] - 3, 6, 6 end def redraw_body draw_body @pos, color_gc if @pos end def scan( x1, y1, x2, y2 ) if @s2 then draw_scan @s2, white_gc end if @s1 then draw_scan @s1, white_gc end return unless @pos if x2 then @s2 = [x2,y2] + @pos draw_scan @s2, scan_gc else @s2 = nil end if x1 then @s1 = [x1,y1] + @pos draw_scan @s1, scan_gc else @s1 = nil end end def draw_wide_scan( x, y, ax, ay, bx, by, gc ) r = Math.sqrt( (ax-x)**2 + (ay-y)**2 ) r = 1 if r == 0 angle1 = xy2d( ax-x, ay-x ) * 64 angle2 = xy2d( bx-x, by-x ) * 64 @map.draw_arc gc, true, x-r, y-r, 2*r, 2*r, angle1, angle2 end def xy2d( x, y ) Math.atan2(x,y) * 57.297 # * 180/3.1415 (rad -> d) end def draw_scan( pos, gc ) @map.draw_line gc, pos[2], pos[3], pos[0], pos[1] end def missile( x, y ) if @m then draw_missile @m, white_gc end if x then @m = [x,y] draw_missile @m, black_gc else @m = nil end end def draw_missile( pos, gc ) @map.draw_rectangle gc, true, pos[0] - 2, pos[1] - 2, 4, 4 end end view = GtkRubyRobotView.new view.main