Exemple #1
0
    def __init__(self, side):

        self.engine = "gshogi"
        self.path = ""
        self.engine_running = False
        self.newgame = False
        self.running_engine = ""
        self.stop_pending = False
        self.ponder_move = None
        self.side = side
        self.engine_debug = engine_debug.get_ref()
        self.engine_output = engine_output.get_ref()
Exemple #2
0
    def __init__(self, side):

        self.engine = "gshogi"
        self.path = ""
        self.engine_running = False
        self.newgame = False
        self.running_engine = ""
        self.stop_pending = False
        self.ponder_move = None
        self.side = side
        self.engine_debug = engine_debug.get_ref()
        self.engine_output = engine_output.get_ref()
Exemple #3
0
    def __init__(self, verbose, verbose_usi, side):

        self.engine = 'gshogi'
        self.path = ''
        self.engine_running = False
        self.newgame = False
        self.running_engine = ''
        self.stop_pending = False
        self.ponder_move = None
        self.verbose = verbose
        self.verbose_usi = verbose_usi
        self.side = side
        self.engine_debug = engine_debug.get_ref()
        self.engine_output = engine_output.get_ref()
Exemple #4
0
 def __init__(self, verbose, verbose_usi, side):
 
     self.engine = 'gshogi'
     self.path = ''        
     self.engine_running = False
     self.newgame = False
     self.running_engine = ''         
     self.stop_pending = False
     self.ponder_move = None
     self.verbose = verbose 
     self.verbose_usi = verbose_usi
     self.side = side        
     self.engine_debug = engine_debug.get_ref()
     self.engine_output = engine_output.get_ref()        
Exemple #5
0
    def __init__(self):
        gv.gshogi = self
        # set global variables for debug messages
        gv.verbose, gv.verbose_usi = utils.get_verbose()
        # prefix to find package files/folders
        self.prefix = utils.get_prefix()
        # set up .gshogi directory in the home directory
        self.gshogipath = utils.create_settings_dir()
        self.glade_dir = os.path.join(self.prefix, "glade")
        # get settings saved from previous game
        self.settings = utils.get_settings_from_file(self.gshogipath)

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = "00:10"
        self.stopped = True
        self.quitting = False
        self.src = ""
        self.src_x = ""
        self.src_y = ""
        self.startpos = "startpos"
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = "none"
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, gv.verbose)

        gv.gui = gui.Gui()

        gv.pieces = pieces.Pieces()
        # custom pieceset path
        if self.settings != "":
            gv.pieces.set_custom_pieceset_path(
                self.settings.custom_pieceset_path)
        gv.pieces.load_pieces(self.prefix)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        gv.usib = usi.Usi("b")
        gv.usiw = usi.Usi("w")

        # instantiate board, gui, classes
        gv.tc = time_control.Time_Control()
        gv.engine_manager = engine_manager.Engine_Manager()
        gv.board = board.Board()

        self.set_board_colours = set_board_colours.get_ref()
        # set colours to previous game (if any)
        if self.settings != "":
            self.set_board_colours.restore_colour_settings(
                self.settings.colour_settings)
        gv.gui.build_gui()
        gv.board.build_board()
        self.engine_output = engine_output.get_ref()

        # set level
        command = "level 0 " + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command("beep")

        # restore users settings to values from previous game
        self.restore_settings(self.settings)

        gv.usib.set_engine(self.player[BLACK], None)
        gv.usiw.set_engine(self.player[WHITE], None)
        gv.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        gv.tc.reset_clock()

        gv.gui.enable_go_button()
        gv.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        self.timer_active = False
        self.set_board_colours.apply_colour_settings()
Exemple #6
0
    def __init__(self):

        self.verbose = False
        self.verbose_usi = False
        for arg in sys.argv:
            if arg == '-v' or arg == '--verbose':
                self.verbose = True
            if arg == '-vusi':
                self.verbose_usi = True

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = '00:10'
        self.stopped = True
        self.quitting = False
        self.src = ''
        self.src_x = ''
        self.src_y = ''
        self.startpos = 'startpos'
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = 'none'
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        # set paths to images. opening book etc
        self.set_data_paths()
        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, self.verbose)

        self.glade_dir = os.path.join(self.prefix, 'glade')

        utils.set_game_ref(self)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        self.usib = usi.Usi(self.verbose, self.verbose_usi, 'b')
        self.usiw = usi.Usi(self.verbose, self.verbose_usi, 'w')
        utils.set_usi_refs(self.usib, self.usiw)

        # instantiate board, gui, classes
        self.tc = time_control.Time_Control(self.verbose)
        utils.set_tc_ref(self.tc)
        self.engine_manager = engine_manager.Engine_Manager(self.verbose)
        self.board = utils.get_board_ref()
        self.pieces = utils.get_pieces_ref()

        self.gui = utils.get_gui_ref()
        self.board.set_refs(self, self.gui)
        self.gui.set_refs(self, self.board, self.engine_manager, self.tc)
        self.gui.build_gui()
        self.board.build_board()
        self.engine_manager.set_refs(self, self.gui, self.usib, self.usiw)
        self.usib.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.usiw.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.tc.set_refs(self, self.gui)
        self.set_board_colours = set_board_colours.get_ref()
        self.engine_output = engine_output.get_ref()

        # set level
        command = 'level 0 ' + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command('beep')

        # restore users settings to values from previous game
        self.restore_settings()

        self.usib.set_engine(self.player[BLACK], None)
        self.usiw.set_engine(self.player[WHITE], None)
        self.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        self.tc.reset_clock()

        self.gui.enable_go_button()
        self.gui.disable_stop_button()

        self.stm = self.get_side_to_move()

        self.timer_active = False
Exemple #7
0
    def __init__(self):
        gv.gshogi = self
        # set global variables for debug messages
        gv.verbose, gv.verbose_usi = utils.get_verbose()
        # prefix to find package files/folders
        self.prefix = utils.get_prefix()
        # set up .gshogi directory in the home directory
        self.gshogipath = utils.create_settings_dir()
        self.glade_dir = os.path.join(self.prefix, "glade")
        # get settings saved from previous game
        self.settings = utils.get_settings_from_file(self.gshogipath)

        self.ask_before_promoting = False
        self.gameover = False
        self.time_limit = "00:10"
        self.stopped = True
        self.quitting = False
        self.src = ""
        self.src_x = ""
        self.src_y = ""
        self.startpos = "startpos"
        self.start_stm = BLACK

        self.search_depth = 39
        self.thinking = False
        self.cmove = "none"
        self.movelist = []
        self.redolist = []
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")
        engine.init(opening_book_path, gv.verbose)

        gv.gui = gui.Gui()

        gv.pieces = pieces.Pieces()
        # custom pieceset path
        if self.settings != "":
            gv.pieces.set_custom_pieceset_path(
                self.settings.custom_pieceset_path)
        gv.pieces.load_pieces(self.prefix)

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        gv.usib = usi.Usi("b")
        gv.usiw = usi.Usi("w")

        # instantiate board, gui, classes
        gv.tc = time_control.Time_Control()
        gv.engine_manager = engine_manager.Engine_Manager()
        gv.board = board.Board()

        self.set_board_colours = set_board_colours.get_ref()
        # set colours to previous game (if any)
        if self.settings != "":
            self.set_board_colours.restore_colour_settings(
                self.settings.colour_settings)
        gv.gui.build_gui()
        gv.board.build_board()
        self.engine_output = engine_output.get_ref()

        # set level
        command = "level 0 " + self.time_limit
        engine.command(command)
        # turn off beeps
        if not BEEP:
            engine.command("beep")

        # restore users settings to values from previous game
        self.restore_settings(self.settings)

        gv.usib.set_engine(self.player[BLACK], None)
        gv.usiw.set_engine(self.player[WHITE], None)
        gv.gui.update_toolbar(self.player)

        self.move_list = move_list.get_ref()

        gv.tc.reset_clock()

        gv.gui.enable_go_button()
        gv.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        self.timer_active = False
        self.set_board_colours.apply_colour_settings()
Exemple #8
0
    def __init__(self):

        self.verbose = False
        self.verbose_usi = False
        for arg in sys.argv:
            if arg == '-v' or arg == '--verbose':
                self.verbose = True
            if arg == '-vusi':
                self.verbose_usi = True

        self.ask_before_promoting = False
        self.gameover = False       
        self.time_limit = '00:10'
        self.stopped = True
        self.quitting = False        
        self.src = ''
        self.src_x = ''
        self.src_y = ''
        self.startpos = 'startpos'
        self.start_stm = BLACK        

        self.search_depth = 39
        self.thinking = False
        self.cmove = 'none'
        self.movelist = []
        self.redolist = []        
        self.player = ["Human", "gshogi"]
        self.pondermove = [None, None]

       # set paths to images. opening book etc        
        self.set_data_paths() 
        opening_book_path = os.path.join(self.prefix, "data/opening.bbk")              
        engine.init(opening_book_path, self.verbose)

        self.glade_dir = os.path.join(self.prefix, 'glade')             
        
        utils.set_game_ref(self)            

        # usiw is the instance that plays white (gote)
        # usib is the instance that plays black (sente)
        self.usib = usi.Usi(self.verbose, self.verbose_usi, 'b')
        self.usiw = usi.Usi(self.verbose, self.verbose_usi, 'w')
        utils.set_usi_refs(self.usib, self.usiw)  

        # instantiate board, gui, classes 
        self.tc = time_control.Time_Control(self.verbose) 
        utils.set_tc_ref(self.tc)
        self.engine_manager = engine_manager.Engine_Manager(self.verbose)        
        self.board = utils.get_board_ref()
        self.pieces = utils.get_pieces_ref()
        
        self.gui = utils.get_gui_ref() 
        self.board.set_refs(self, self.gui)
        self.gui.set_refs(self, self.board, self.engine_manager, self.tc)
        self.gui.build_gui()
        self.board.build_board()
        self.engine_manager.set_refs(self, self.gui, self.usib, self.usiw)
        self.usib.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.usiw.set_refs(self, self.engine_manager, self.gui, self.tc)
        self.tc.set_refs(self, self.gui)               
        self.set_board_colours = set_board_colours.get_ref()
        self.engine_output = engine_output.get_ref()                     

        # set level        
        command = 'level 0 ' + self.time_limit                  
        engine.command(command)   
        # turn off beeps
        if not BEEP:
            engine.command('beep')

        # restore users settings to values from previous game
        self.restore_settings()

        self.usib.set_engine(self.player[BLACK], None)
        self.usiw.set_engine(self.player[WHITE], None)        
        self.gui.update_toolbar(self.player)
               
        self.move_list = move_list.get_ref()

        self.tc.reset_clock()
        
        self.gui.enable_go_button()            
        self.gui.disable_stop_button()

        self.stm = self.get_side_to_move()
        
        self.timer_active = False       
Exemple #9
0
    def build_gui(self):

        self.gobactive = False
        self.engine_debug = engine_debug.get_ref()
        self.engine_output = engine_output.get_ref()
        self.move_list = move_list.get_ref()
        self.gamelist = gamelist.get_ref()
        self.set_board_colours = set_board_colours.get_ref()
        self.drag_and_drop = drag_and_drop.get_ref()
        self.enable_dnd = True
        self.load_save = load_save.get_ref()
        self.show_coords = True
        self.highlight_moves = True

        # Create Main Window
        glade_dir = gv.gshogi.get_glade_dir()
        self.glade_file = os.path.join(glade_dir, "main_window.glade")
        self.glade_file_preferences = os.path.join(
            glade_dir, "preferences.glade")
        self.builder = Gtk.Builder()
        self.builder.add_from_file(self.glade_file)
        self.builder.connect_signals(self)

        self.window = self.builder.get_object("main_window")
        screen = self.window.get_screen()
        self.screen_width = screen.get_width()
        self.screen_height = screen.get_height()
        self.set_window_size()

        # self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.window.set_title(NAME + " " + VERSION)

        # 1 eventbox per board square
        self.eb = [[Gtk.EventBox() for x in range(9)] for x in range(9)]

        # 1 eventbox per komadai board square
        self.keb = [[Gtk.EventBox() for x in range(7)] for x in range(2)]

        # Set a handler for delete_event that immediately exits GTK.
        self.window.connect("delete_event", gv.gshogi.delete_event)

        main_vbox = self.builder.get_object("main_vbox")
        main_vbox.show()

        # menu
        # Create a UIManager instance
        uimanager = Gtk.UIManager()

        # Add the accelerator group to the toplevel window
        accelgroup = uimanager.get_accel_group()
        self.window.add_accel_group(accelgroup)

        # main action group
        actiongroup = Gtk.ActionGroup("UIManagerAG")
        self.actiongroup = actiongroup

        # action group for toggleactions
        ta_action_group = Gtk.ActionGroup("AGPromoteMode")
        ta_action_group.add_toggle_actions([(
            "promotemode", None, "_Ask Before Promoting", None, None,
            self.promote_mode)])

        self.ta_action_group = ta_action_group

        # Create actions
        actiongroup.add_actions([
            # NewGame and the handicap names are used in gshogi.py NewGame
            # routine. don't change them.
            ("NewGame", Gtk.STOCK_NEW, "_New Game", None, "New Game",
             gv.gshogi.new_game_cb),
            ("LanceHandicap", None, "_Lance", None,
             "Lance Handicap", gv.gshogi.new_game_cb),
            ("BishopHandicap", None, "_Bishop", None,
             "Bishop Handicap", gv.gshogi.new_game_cb),
            ("RookHandicap", None, "_Rook", None,
             "Rook Handicap", gv.gshogi.new_game_cb),
            ("RookandLanceHandicap", None, "_Rook and Lance", None,
             "Rook and Lance Handicap", gv.gshogi.new_game_cb),
            ("TwoPieceHandicap", None, "_2 Pieces", None,
             "Two Piece Handicap", gv.gshogi.new_game_cb),
            ("FourPieceHandicap", None, "_4 Pieces", None,
             "Four Piece Handicap", gv.gshogi.new_game_cb),
            ("SixPieceHandicap", None, "_6 Pieces", None,
             "Six Piece Handicap", gv.gshogi.new_game_cb),
            ("EightPieceHandicap", None, "_8 Pieces", None,
             "Eight Piece Handicap", gv.gshogi.new_game_cb),
            ("TenPieceHandicap", None, "_10 Pieces", None,
             "Ten Piece Handicap", gv.gshogi.new_game_cb),
            ("NewHandicapGame", None, "_New Handicap Game"),
            #
            ("Quit", Gtk.STOCK_QUIT, "_Quit", None,
             "Quit the Program", gv.gshogi.quit_game),
            ("LoadGame", Gtk.STOCK_OPEN, "_Load Game", None,
             "Load Game", self.load_save.load_game),
            ("SaveGame", Gtk.STOCK_SAVE, "_Save Game", None,
             "Save Game", self.load_save.save_game),
            ("File", None, "_File"),
            ("Edit", None, "_Edit"),
            ("Undo", Gtk.STOCK_UNDO, "_Undo Move", "<Control>U",
             "Undo Move", gv.gshogi.undo_single_move),
            ("Redo", Gtk.STOCK_REDO, "_Redo Move", "<Control>R",
             "Redo Move", gv.gshogi.redo_single_move),
            ("MoveNow", None, "_Move Now", "<Control>M",
             "Move Now", gv.gshogi.move_now),
            ("SetBoardColours", None, "_Set Board Colours", None,
             "Set Board Colours", self.set_board_colours.show_dialog),
            ("SetPieces", None, "_Set Pieces", None,
             "Set Pieces", self.set_board_colours.show_pieces_dialog),
            ("TimeControl", None, "_Time Control", None,
             "Time Control", gv.tc.time_control),
            # ConfigureEngine1 - this name is used in engine_manager.
            # don't change it.
            ("ConfigureEngine1", None, "_Configure Engine 1", None,
             "Configure Engine 1", gv.engine_manager.configure_engine),
            ("ConfigureEngine2", None, "_Configure Engine 2", None,
             "Configure Engine 2", gv.engine_manager.configure_engine),
            ("Players", None, "_Players", None,
             "Players", gv.gshogi.set_players),
            ("Engines", None, "_Engines", None,
             "Engines", gv.engine_manager.engines),
            ("CommonEngineSettings", None, "_Common Engine Settings", None,
             "Common Engine Settings", gv.engine_manager.common_settings),
            ("MoveList", None, "_Move List", None,
             "Move List", self.move_list.show_movelist_window),
            ("GameList", None, "_Game List", None,
             "Game List", self.gamelist.show_gamelist_window_cb),
            ("EngineOutput", None, "_Engine Output", None,
             "Engine Output", self.engine_output.show_engine_output_window),
            ("EngineDebug", None, "_Engine Debug", None,
             "Engine Debug", self.engine_debug.show_debug_window),
            ("Options", None, "_Options"),
            ("View", None, "_View"),
            ("About", Gtk.STOCK_ABOUT, "_About", None,
             "Show About Box", self.about_box),
            ("Help", None, "_Help"),
            ("CopyPosition", None, "_Copy Position", None,
             "Copy Position", utils.copy_SFEN_to_clipboard),
            ("PastePosition", None, "_Paste Position", None,
             "Paste Position", utils.paste_clipboard_to_SFEN),
            ("CopyGame", None, "_Copy Game", None,
             "Copy Game", utils.copy_game_to_clipboard),
            ("PasteGame", None, "_Paste Game", None,
             "Paste Game", utils.paste_game_from_clipboard),
            ("EditPosition", None, "_Edit Position", None,
             "Edit Position", self.enable_edit_mode),
            ("Preferences", None, "_Preferences", None,
             "Preferences", self.preferences),
            ]
        )

        # Add the actiongroups to the uimanager
        uimanager.insert_action_group(actiongroup, 0)
        uimanager.insert_action_group(ta_action_group, 1)

        ui = """<ui>
        <menubar name="MenuBar">
            <menu action="File">
                <menuitem action="NewGame"/>
                <menu action="NewHandicapGame">
                    <menuitem action="LanceHandicap"/>
                    <menuitem action="BishopHandicap"/>
                    <menuitem action="RookHandicap"/>
                    <menuitem action="RookandLanceHandicap"/>
                    <menuitem action="TwoPieceHandicap"/>
                    <menuitem action="FourPieceHandicap"/>
                    <menuitem action="SixPieceHandicap"/>
                    <menuitem action="EightPieceHandicap"/>
                    <menuitem action="TenPieceHandicap"/>
                </menu>
                <separator/>
                <menuitem action="LoadGame"/>
                <menuitem action="SaveGame"/>
                <separator/>
                <menuitem action="Quit"/>
            </menu>
            <menu action="Edit">
                <menuitem action="CopyPosition"/>
                <menuitem action="PastePosition"/>
                <separator/>
                <menuitem action="CopyGame"/>
                <menuitem action="PasteGame"/>
                <separator/>
                <menuitem action="EditPosition"/>
                <separator/>
                <menuitem action="Preferences"/>
            </menu>
            <menu action="Options">
                <menuitem action="Undo"/>
                <menuitem action="Redo"/>
                <menuitem action="MoveNow"/>
                <separator/>
                <menuitem action="promotemode"/>
                <menuitem action="SetBoardColours"/>
                <menuitem action="SetPieces"/>
                <separator/>
                <menuitem action="TimeControl"/>
                <menuitem action="ConfigureEngine1"/>
                <menuitem action="ConfigureEngine2"/>
                <menuitem action="Players"/>
                <menuitem action="Engines"/>
                <menuitem action="CommonEngineSettings"/>
            </menu>
            <menu action="View">
                <menuitem action="MoveList"/>
                <menuitem action="GameList"/>
                <menuitem action="EngineOutput"/>
                <menuitem action="EngineDebug"/>
            </menu>
            <menu action="Help">
                <menuitem action="About"/>
            </menu>
        </menubar>
        </ui>"""

        # Add a UI description
        uimanager.add_ui_from_string(ui)

        # Use an event box and set its background colour.
        # This was needed on Ubuntu 12.04 to set the toolbar bg colour.
        # Otherwise ubuntu uses the window bg colour which is not
        # correct.
        # don't need this on Fedora though.
        eb_1 = self.builder.get_object("eb_1")
        # eb_1 = Gtk.EventBox()
        vbox2 = Gtk.VBox(False, 0)
        # main_vbox.pack_start(vbox2, False)
        # main_vbox.pack_start(eb_1, False)
        eb_1.add(vbox2)
        eb_1.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#EDECEB"))

        # Create a MenuBar
        menubar = uimanager.get_widget("/MenuBar")
        vbox2.pack_start(menubar, False, True, 0)

        # create a toolbar
        toolbar = Gtk.Toolbar()
        vbox2.pack_start(toolbar, False, True, 0)

        # populate toolbar
        toolitem = Gtk.ToolItem()

        # 2 rows, 4 columns, not homogeneous
        tb = Gtk.Table(2, 5, False)

        # Gtk.ShadowType.NONE, SHADOW_IN, SHADOW_OUT, SHADOW_ETCHED_IN,
        # SHADOW_ETCHED_OUT

        self.side_to_move = [
            Gtk.Arrow(Gtk.ArrowType.RIGHT, Gtk.ShadowType.ETCHED_IN),
            Gtk.Arrow(Gtk.ArrowType.RIGHT, Gtk.ShadowType.ETCHED_IN)]
        self.side_to_move[BLACK].set_alignment(0.5, 0.4)
        self.side_to_move[WHITE].set_alignment(0.5, 0.4)

        tb.attach(self.side_to_move[WHITE], 0, 1, 0, 1)
        tb.attach(self.side_to_move[BLACK], 0, 1, 1, 2)

        lw = Gtk.Label(label="White: ")
        lb = Gtk.Label(label="Black: ")
        lw.set_alignment(0, 0.5)
        lb.set_alignment(0, 0.5)
        tb.attach(lw, 1, 2, 0, 1)
        tb.attach(lb, 1, 2, 1, 2)

        self.engines_lblw = Gtk.Label(label="gshogi")
        self.engines_lblw.set_use_markup(True)
        self.engines_lblw.set_alignment(0, 0.5)

        self.engines_lblb = Gtk.Label(label="human")
        self.engines_lblb.set_use_markup(True)
        self.engines_lblb.set_alignment(0, 0.5)

        tb.attach(self.engines_lblw, 2, 3, 0, 1)
        tb.attach(self.engines_lblb, 2, 3, 1, 2)

        # time control
        self.tc_lbl = [
            Gtk.Label(label="00:45:00 00/10"),
            Gtk.Label(label="00:45:00 00/10")]
        self.tc_lbl[BLACK].set_alignment(0, 0.5)
        self.tc_lbl[WHITE].set_alignment(0, 0.5)

        tb.attach(self.tc_lbl[WHITE], 3, 4, 0, 1)
        tb.attach(self.tc_lbl[BLACK], 3, 4, 1, 2)

        toolitem.add(tb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # stop/go buttons
        hb = Gtk.HBox(False, 0)
        self.gobutton = Gtk.ToolButton(Gtk.STOCK_YES)
        self.gobutton.connect("clicked", gv.gshogi.go_clicked)
        self.stopbutton = Gtk.ToolButton(Gtk.STOCK_NO)
        self.stopbutton.connect("clicked", gv.gshogi.stop_clicked)
        self.gobutton.set_tooltip_text("go")
        self.stopbutton.set_tooltip_text("stop")
        hb.pack_start(self.stopbutton, False, True, 0)
        hb.pack_start(self.gobutton, False, True, 0)

        toolitem = Gtk.ToolItem()
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # game review buttons
        hb = Gtk.HBox(False, 0)
        self.go_first = Gtk.ToolButton(Gtk.STOCK_GOTO_FIRST)
        self.go_first.connect("clicked", gv.gshogi.undo_all)

        self.go_back = Gtk.ToolButton(Gtk.STOCK_GO_BACK)
        self.go_back.connect("clicked", gv.gshogi.undo_single_move)

        self.go_forward = Gtk.ToolButton(Gtk.STOCK_GO_FORWARD)
        self.go_forward.connect("clicked", gv.gshogi.redo_single_move)

        self.go_last = Gtk.ToolButton(Gtk.STOCK_GOTO_LAST)
        self.go_last.connect("clicked", gv.gshogi.redo_all)

        hb.pack_start(self.go_first, False, True, 0)
        hb.pack_start(self.go_back, False, True, 0)
        hb.pack_start(self.go_forward, False, True, 0)
        hb.pack_start(self.go_last, False, True, 0)

        toolitem = Gtk.ToolItem()
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        main_grid = self.builder.get_object("grid1")
        main_grid.set_row_homogeneous(True)
        main_grid.set_column_homogeneous(True)
        # main_hbox = Gtk.HBox(False, 0)

        # Create komadai grids for captured pieces
        self.setup_komadai(WHITE, main_grid)
        self.setup_komadai(BLACK, main_grid)

        # Create a 9x9 table for the main board
        self.boardgrid = Gtk.Grid.new()
        self.boardgrid.set_row_homogeneous(True)
        self.boardgrid.set_column_homogeneous(True)
        self.boardgrid.set_border_width(1)

        #
        # init board squares (add event box, set up drag and drop,
        # button clicks)
        # x, y = 0, 0 is the top left square of the board
        #
        for x in range(9):
            for y in range(9):
                self.init_board_square(x, y)

        eb2 = Gtk.EventBox()

        eb2.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("black"))
        eb2.add(self.boardgrid)
        eb = Gtk.EventBox()
        eb.add(eb2)
        eb.show()

        aspect_frame = Gtk.AspectFrame(label=None, xalign=0.5, yalign=0.5,
                                       ratio=1.0, obey_child=False)
        aspect_frame.add(eb)

        eb2.set_border_width(20)
        self.grid_eb = eb2

        main_grid.attach(aspect_frame, 6, 0, 20, 20)
        aspect_frame.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("black"))

        eb.connect_after("draw", self.draw_coords)
        self.border_eb = eb

        # status bar
        self.status_bar = self.builder.get_object("status_bar")

        # set status bar bg color
        # Use an event box and set its background colour.
        # This was needed on Fedora 19 to set the statusbar bg colour.
        # Otherwise it uses the window bg colour which is not
        # correct. This was not needed on F17.
        eb_2 = self.builder.get_object("eb_2")
        eb_2.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#EDECEB"))
        # self.status_bar = Gtk.Statusbar()
        # main_vbox.pack_start(self.status_bar, False, False, 0)
        self.context_id = self.status_bar.get_context_id("gshogi statusbar")

        self.actiongroup.get_action("MoveNow").set_sensitive(False)

        self.window.show_all()
        self.side_to_move[WHITE].hide()
        self.gobutton.set_sensitive(False)
        self.stopbutton.set_sensitive(False)

        # mask = Gdk.WindowHints.BASE_SIZE|
        #            Gdk.WindowHints.MIN_SIZE|
        #            Gdk.WindowHints.MAX_SIZE|
        #            Gdk.WindowHints.RESIZE_INC|Gdk.WindowHints.ASPECT
        #mask = Gdk.WindowHints.MIN_SIZE | Gdk.WindowHints.MAX_SIZE
        mask = Gdk.WindowHints.MIN_SIZE
        geometry = Gdk.Geometry()
        # geometry.base_width = -1
        # geometry.base_height = -1
        # geometry.max_width = -1
        # geometry.max_height = -1
        geometry.min_width = 378
        geometry.min_height = 378
        # geometry.width_inc = -1
        # geometry.height_inc = -1
        # geometry.min_aspect = -1.0
        # geometry.max_aspect = -1.0
        #self.window.set_geometry_hints(self.window, geometry, mask)
        # self.window.set_geometry_hints(
        #   self.window, min_width=378, min_height=378, max_width=-1,
        #   max_height=-1, base_width=-1, base_height=-1, width_inc=-1,
        #   height_inc=-1, min_aspect=-1.0, max_aspect=-1.0)

        self.build_edit_popup()
Exemple #10
0
    def build_gui(self):

        self.gobactive = False
        self.engine_debug = engine_debug.get_ref()
        self.engine_output = engine_output.get_ref()
        self.move_list = move_list.get_ref()
        self.gamelist = gamelist.get_ref()
        self.set_board_colours = set_board_colours.get_ref()
        self.drag_and_drop = drag_and_drop.get_ref()
        self.enable_dnd = True
        self.load_save = load_save.get_ref()
        self.show_coords = True
        self.highlight_moves = True

        # Create Main Window
        glade_dir = gv.gshogi.get_glade_dir()
        self.glade_file = os.path.join(glade_dir, "main_window.glade")
        self.glade_file_preferences = os.path.join(glade_dir,
                                                   "preferences.glade")
        self.builder = Gtk.Builder()
        self.builder.add_from_file(self.glade_file)
        self.builder.connect_signals(self)

        self.window = self.builder.get_object("main_window")
        screen = self.window.get_screen()
        self.screen_width = screen.get_width()
        self.screen_height = screen.get_height()
        self.set_window_size()

        # self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.window.set_title(NAME + " " + VERSION)

        # 1 eventbox per board square
        self.eb = [[Gtk.EventBox() for x in range(9)] for x in range(9)]

        # 1 eventbox per komadai board square
        self.keb = [[Gtk.EventBox() for x in range(7)] for x in range(2)]

        # Set a handler for delete_event that immediately exits GTK.
        self.window.connect("delete_event", gv.gshogi.delete_event)

        main_vbox = self.builder.get_object("main_vbox")
        main_vbox.show()

        # menu
        # Create a UIManager instance
        uimanager = Gtk.UIManager()

        # Add the accelerator group to the toplevel window
        accelgroup = uimanager.get_accel_group()
        self.window.add_accel_group(accelgroup)

        # main action group
        actiongroup = Gtk.ActionGroup("UIManagerAG")
        self.actiongroup = actiongroup

        # action group for toggleactions
        ta_action_group = Gtk.ActionGroup("AGPromoteMode")
        ta_action_group.add_toggle_actions([
            ("promotemode", None, "_Ask Before Promoting", None, None,
             self.promote_mode)
        ])

        self.ta_action_group = ta_action_group

        # Create actions
        actiongroup.add_actions([
            # NewGame and the handicap names are used in gshogi.py NewGame
            # routine. don't change them.
            ("NewGame", Gtk.STOCK_NEW, "_New Game", None, "New Game",
             gv.gshogi.new_game_cb),
            ("LanceHandicap", None, "_Lance", None, "Lance Handicap",
             gv.gshogi.new_game_cb),
            ("BishopHandicap", None, "_Bishop", None, "Bishop Handicap",
             gv.gshogi.new_game_cb),
            ("RookHandicap", None, "_Rook", None, "Rook Handicap",
             gv.gshogi.new_game_cb),
            ("RookandLanceHandicap", None, "_Rook and Lance", None,
             "Rook and Lance Handicap", gv.gshogi.new_game_cb),
            ("TwoPieceHandicap", None, "_2 Pieces", None, "Two Piece Handicap",
             gv.gshogi.new_game_cb),
            ("FourPieceHandicap", None, "_4 Pieces", None,
             "Four Piece Handicap", gv.gshogi.new_game_cb),
            ("SixPieceHandicap", None, "_6 Pieces", None, "Six Piece Handicap",
             gv.gshogi.new_game_cb),
            ("EightPieceHandicap", None, "_8 Pieces", None,
             "Eight Piece Handicap", gv.gshogi.new_game_cb),
            ("TenPieceHandicap", None, "_10 Pieces", None,
             "Ten Piece Handicap", gv.gshogi.new_game_cb),
            ("NewHandicapGame", None, "_New Handicap Game"),
            #
            ("Quit", Gtk.STOCK_QUIT, "_Quit", None, "Quit the Program",
             gv.gshogi.quit_game),
            ("LoadGame", Gtk.STOCK_OPEN, "_Load Game", None, "Load Game",
             self.load_save.load_game),
            ("SaveGame", Gtk.STOCK_SAVE, "_Save Game", None, "Save Game",
             self.load_save.save_game),
            ("File", None, "_File"),
            ("Edit", None, "_Edit"),
            ("Undo", Gtk.STOCK_UNDO, "_Undo Move", "<Control>U", "Undo Move",
             gv.gshogi.undo_single_move),
            ("Redo", Gtk.STOCK_REDO, "_Redo Move", "<Control>R", "Redo Move",
             gv.gshogi.redo_single_move),
            ("MoveNow", None, "_Move Now", "<Control>M", "Move Now",
             gv.gshogi.move_now),
            ("SetBoardColours", None, "_Set Board Colours", None,
             "Set Board Colours", self.set_board_colours.show_dialog),
            ("SetPieces", None, "_Set Pieces", None, "Set Pieces",
             self.set_board_colours.show_pieces_dialog),
            ("TimeControl", None, "_Time Control", None, "Time Control",
             gv.tc.time_control),
            # ConfigureEngine1 - this name is used in engine_manager.
            # don't change it.
            ("ConfigureEngine1", None, "_Configure Engine 1", None,
             "Configure Engine 1", gv.engine_manager.configure_engine),
            ("ConfigureEngine2", None, "_Configure Engine 2", None,
             "Configure Engine 2", gv.engine_manager.configure_engine),
            ("Players", None, "_Players", None, "Players",
             gv.gshogi.set_players),
            ("Engines", None, "_Engines", None, "Engines",
             gv.engine_manager.engines),
            ("CommonEngineSettings", None, "_Common Engine Settings", None,
             "Common Engine Settings", gv.engine_manager.common_settings),
            ("MoveList", None, "_Move List", None, "Move List",
             self.move_list.show_movelist_window),
            ("GameList", None, "_Game List", None, "Game List",
             self.gamelist.show_gamelist_window_cb),
            ("EngineOutput", None, "_Engine Output", None, "Engine Output",
             self.engine_output.show_engine_output_window),
            ("EngineDebug", None, "_Engine Debug", None, "Engine Debug",
             self.engine_debug.show_debug_window),
            ("Options", None, "_Options"),
            ("View", None, "_View"),
            ("About", Gtk.STOCK_ABOUT, "_About", None, "Show About Box",
             self.about_box),
            ("Help", None, "_Help"),
            ("CopyPosition", None, "_Copy Position", None, "Copy Position",
             utils.copy_SFEN_to_clipboard),
            ("PastePosition", None, "_Paste Position", None, "Paste Position",
             utils.paste_clipboard_to_SFEN),
            ("CopyGame", None, "_Copy Game", None, "Copy Game",
             utils.copy_game_to_clipboard),
            ("PasteGame", None, "_Paste Game", None, "Paste Game",
             utils.paste_game_from_clipboard),
            ("EditPosition", None, "_Edit Position", None, "Edit Position",
             self.enable_edit_mode),
            ("Preferences", None, "_Preferences", None, "Preferences",
             self.preferences),
        ])

        # Add the actiongroups to the uimanager
        uimanager.insert_action_group(actiongroup, 0)
        uimanager.insert_action_group(ta_action_group, 1)

        ui = """<ui>
        <menubar name="MenuBar">
            <menu action="File">
                <menuitem action="NewGame"/>
                <menu action="NewHandicapGame">
                    <menuitem action="LanceHandicap"/>
                    <menuitem action="BishopHandicap"/>
                    <menuitem action="RookHandicap"/>
                    <menuitem action="RookandLanceHandicap"/>
                    <menuitem action="TwoPieceHandicap"/>
                    <menuitem action="FourPieceHandicap"/>
                    <menuitem action="SixPieceHandicap"/>
                    <menuitem action="EightPieceHandicap"/>
                    <menuitem action="TenPieceHandicap"/>
                </menu>
                <separator/>
                <menuitem action="LoadGame"/>
                <menuitem action="SaveGame"/>
                <separator/>
                <menuitem action="Quit"/>
            </menu>
            <menu action="Edit">
                <menuitem action="CopyPosition"/>
                <menuitem action="PastePosition"/>
                <separator/>
                <menuitem action="CopyGame"/>
                <menuitem action="PasteGame"/>
                <separator/>
                <menuitem action="EditPosition"/>
                <separator/>
                <menuitem action="Preferences"/>
            </menu>
            <menu action="Options">
                <menuitem action="Undo"/>
                <menuitem action="Redo"/>
                <menuitem action="MoveNow"/>
                <separator/>
                <menuitem action="promotemode"/>
                <menuitem action="SetBoardColours"/>
                <menuitem action="SetPieces"/>
                <separator/>
                <menuitem action="TimeControl"/>
                <menuitem action="ConfigureEngine1"/>
                <menuitem action="ConfigureEngine2"/>
                <menuitem action="Players"/>
                <menuitem action="Engines"/>
                <menuitem action="CommonEngineSettings"/>
            </menu>
            <menu action="View">
                <menuitem action="MoveList"/>
                <menuitem action="GameList"/>
                <menuitem action="EngineOutput"/>
                <menuitem action="EngineDebug"/>
            </menu>
            <menu action="Help">
                <menuitem action="About"/>
            </menu>
        </menubar>
        </ui>"""

        # Add a UI description
        uimanager.add_ui_from_string(ui)

        # Use an event box and set its background colour.
        # This was needed on Ubuntu 12.04 to set the toolbar bg colour.
        # Otherwise ubuntu uses the window bg colour which is not
        # correct.
        # don't need this on Fedora though.
        eb_1 = self.builder.get_object("eb_1")
        # eb_1 = Gtk.EventBox()
        vbox2 = Gtk.VBox(False, 0)
        # main_vbox.pack_start(vbox2, False)
        # main_vbox.pack_start(eb_1, False)
        eb_1.add(vbox2)
        eb_1.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#EDECEB"))

        # Create a MenuBar
        menubar = uimanager.get_widget("/MenuBar")
        vbox2.pack_start(menubar, False, True, 0)

        # create a toolbar
        toolbar = Gtk.Toolbar()
        vbox2.pack_start(toolbar, False, True, 0)

        # populate toolbar
        toolitem = Gtk.ToolItem()

        # 2 rows, 4 columns, not homogeneous
        tb = Gtk.Table(2, 5, False)

        # Gtk.ShadowType.NONE, SHADOW_IN, SHADOW_OUT, SHADOW_ETCHED_IN,
        # SHADOW_ETCHED_OUT

        self.side_to_move = [
            Gtk.Arrow(Gtk.ArrowType.RIGHT, Gtk.ShadowType.ETCHED_IN),
            Gtk.Arrow(Gtk.ArrowType.RIGHT, Gtk.ShadowType.ETCHED_IN)
        ]
        self.side_to_move[BLACK].set_alignment(0.5, 0.4)
        self.side_to_move[WHITE].set_alignment(0.5, 0.4)

        tb.attach(self.side_to_move[WHITE], 0, 1, 0, 1)
        tb.attach(self.side_to_move[BLACK], 0, 1, 1, 2)

        lw = Gtk.Label(label="White: ")
        lb = Gtk.Label(label="Black: ")
        lw.set_alignment(0, 0.5)
        lb.set_alignment(0, 0.5)
        tb.attach(lw, 1, 2, 0, 1)
        tb.attach(lb, 1, 2, 1, 2)

        self.engines_lblw = Gtk.Label(label="gshogi")
        self.engines_lblw.set_use_markup(True)
        self.engines_lblw.set_alignment(0, 0.5)

        self.engines_lblb = Gtk.Label(label="human")
        self.engines_lblb.set_use_markup(True)
        self.engines_lblb.set_alignment(0, 0.5)

        tb.attach(self.engines_lblw, 2, 3, 0, 1)
        tb.attach(self.engines_lblb, 2, 3, 1, 2)

        # time control
        self.tc_lbl = [
            Gtk.Label(label="00:45:00 00/10"),
            Gtk.Label(label="00:45:00 00/10")
        ]
        self.tc_lbl[BLACK].set_alignment(0, 0.5)
        self.tc_lbl[WHITE].set_alignment(0, 0.5)

        tb.attach(self.tc_lbl[WHITE], 3, 4, 0, 1)
        tb.attach(self.tc_lbl[BLACK], 3, 4, 1, 2)

        toolitem.add(tb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # stop/go buttons
        hb = Gtk.HBox(False, 0)
        self.gobutton = Gtk.ToolButton(Gtk.STOCK_YES)
        self.gobutton.connect("clicked", gv.gshogi.go_clicked)
        self.stopbutton = Gtk.ToolButton(Gtk.STOCK_NO)
        self.stopbutton.connect("clicked", gv.gshogi.stop_clicked)
        self.gobutton.set_tooltip_text("go")
        self.stopbutton.set_tooltip_text("stop")
        hb.pack_start(self.stopbutton, False, True, 0)
        hb.pack_start(self.gobutton, False, True, 0)

        toolitem = Gtk.ToolItem()
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # game review buttons
        hb = Gtk.HBox(False, 0)
        self.go_first = Gtk.ToolButton(Gtk.STOCK_GOTO_FIRST)
        self.go_first.connect("clicked", gv.gshogi.undo_all)

        self.go_back = Gtk.ToolButton(Gtk.STOCK_GO_BACK)
        self.go_back.connect("clicked", gv.gshogi.undo_single_move)

        self.go_forward = Gtk.ToolButton(Gtk.STOCK_GO_FORWARD)
        self.go_forward.connect("clicked", gv.gshogi.redo_single_move)

        self.go_last = Gtk.ToolButton(Gtk.STOCK_GOTO_LAST)
        self.go_last.connect("clicked", gv.gshogi.redo_all)

        hb.pack_start(self.go_first, False, True, 0)
        hb.pack_start(self.go_back, False, True, 0)
        hb.pack_start(self.go_forward, False, True, 0)
        hb.pack_start(self.go_last, False, True, 0)

        toolitem = Gtk.ToolItem()
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        # add a vertical separator
        hb = Gtk.HBox(False, 0)
        toolitem = Gtk.ToolItem()
        vsep = Gtk.VSeparator()
        hb.pack_start(vsep, True, True, 10)
        toolitem.add(hb)
        toolbar.insert(toolitem, -1)

        main_grid = self.builder.get_object("grid1")
        main_grid.set_row_homogeneous(True)
        main_grid.set_column_homogeneous(True)
        # main_hbox = Gtk.HBox(False, 0)

        # Create komadai grids for captured pieces
        self.setup_komadai(WHITE, main_grid)
        self.setup_komadai(BLACK, main_grid)

        # Create a 9x9 table for the main board
        self.boardgrid = Gtk.Grid.new()
        self.boardgrid.set_row_homogeneous(True)
        self.boardgrid.set_column_homogeneous(True)
        self.boardgrid.set_border_width(1)

        #
        # init board squares (add event box, set up drag and drop,
        # button clicks)
        # x, y = 0, 0 is the top left square of the board
        #
        for x in range(9):
            for y in range(9):
                self.init_board_square(x, y)

        eb2 = Gtk.EventBox()

        eb2.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("black"))
        eb2.add(self.boardgrid)
        eb = Gtk.EventBox()
        eb.add(eb2)
        eb.show()

        aspect_frame = Gtk.AspectFrame(label=None,
                                       xalign=0.5,
                                       yalign=0.5,
                                       ratio=1.0,
                                       obey_child=False)
        aspect_frame.add(eb)

        eb2.set_border_width(20)
        self.grid_eb = eb2

        main_grid.attach(aspect_frame, 6, 0, 20, 20)
        aspect_frame.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("black"))

        eb.connect_after("draw", self.draw_coords)
        self.border_eb = eb

        # status bar
        self.status_bar = self.builder.get_object("status_bar")

        # set status bar bg color
        # Use an event box and set its background colour.
        # This was needed on Fedora 19 to set the statusbar bg colour.
        # Otherwise it uses the window bg colour which is not
        # correct. This was not needed on F17.
        eb_2 = self.builder.get_object("eb_2")
        eb_2.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#EDECEB"))
        # self.status_bar = Gtk.Statusbar()
        # main_vbox.pack_start(self.status_bar, False, False, 0)
        self.context_id = self.status_bar.get_context_id("gshogi statusbar")

        self.actiongroup.get_action("MoveNow").set_sensitive(False)

        self.window.show_all()
        self.side_to_move[WHITE].hide()
        self.gobutton.set_sensitive(False)
        self.stopbutton.set_sensitive(False)

        # mask = Gdk.WindowHints.BASE_SIZE|
        #            Gdk.WindowHints.MIN_SIZE|
        #            Gdk.WindowHints.MAX_SIZE|
        #            Gdk.WindowHints.RESIZE_INC|Gdk.WindowHints.ASPECT
        #mask = Gdk.WindowHints.MIN_SIZE | Gdk.WindowHints.MAX_SIZE
        mask = Gdk.WindowHints.MIN_SIZE
        geometry = Gdk.Geometry()
        # geometry.base_width = -1
        # geometry.base_height = -1
        # geometry.max_width = -1
        # geometry.max_height = -1
        geometry.min_width = 378
        geometry.min_height = 378
        # geometry.width_inc = -1
        # geometry.height_inc = -1
        # geometry.min_aspect = -1.0
        # geometry.max_aspect = -1.0
        #self.window.set_geometry_hints(self.window, geometry, mask)
        # self.window.set_geometry_hints(
        #   self.window, min_width=378, min_height=378, max_width=-1,
        #   max_height=-1, base_width=-1, base_height=-1, width_inc=-1,
        #   height_inc=-1, min_aspect=-1.0, max_aspect=-1.0)

        self.build_edit_popup()