def running_display(self): # get running display rd = engine.rdisp() self.gui.set_panel_text(rd) if self.thinking: return True else: return False
def key_press_event(self, widget, event): kp = Gdk.keyval_name(event.keyval) kp = kp.lower() # If in position edit mode don't allow key presses except 'Delete' if self.board.get_pos_edit(): if kp == 'delete': self.board.position_edit_clear_board() return # if computer is thinking don't allow key presses except for 'm' if self.thinking: # 'm' to interrupt computers thinking and make it move now if kp == "m" or kp == "g": engine.movenow() return if kp == "r": self.board.retract() elif kp == "bracketleft": self.board.rewind() elif kp == "bracketright": self.board.forward() elif kp == "braceleft": self.board.rewind_to_start() elif kp == "braceright": self.board.forward_to_end() elif kp == "g": self.go() elif kp == "2" or kp == "3" or kp == "4" or kp == "6" or kp == "k" or kp == "s": if kp == "2": kp = 2 if kp == "3": kp = 3 if kp == "4": kp = 4 if kp == "6": kp = 6 if kp == "c": kp = 7 if kp == "s": kp = 8 # add/delete/save from opening book opening_book_path = os.path.expanduser("~") + "/.samuel/opening.gbk" engine.openingbook(kp, opening_book_path) msg = engine.rdisp() self.gui.set_panel_text(msg)
def build_gui(self): # Create Main Window self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL) #self.window.set_resizable(False) self.window.set_title(NAME + " " + VERSION) self.window.set_default_size(550, 550) # startup size self.window.set_size_request(400, 400) # minimum size # Set a handler for delete_event that immediately # exits GTK. self.window.connect("delete_event", self.game.delete_event) self.window.connect("key_press_event", self.game.key_press_event) self.window.connect("configure_event", self.configure_event) main_vbox = Gtk.VBox(False, 0) self.window.add(main_vbox) main_vbox.show() # 1 eventbox per board square self.eb = [ \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ [Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox(), Gtk.EventBox()], \ ] # 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) # Create ActionGroups # main action group actiongroup = Gtk.ActionGroup('UIManagerAG') self.actiongroup = actiongroup # action group for setting search depth level search_depth_actiongroup = Gtk.ActionGroup('AGSearchDepth') search_depth_actiongroup.add_radio_actions([ ('Beginner', None, _('Beginner'), None, None, 0), ('Advanced', None, _('Advanced'), None, None, 1), ('Expert', None, _('Expert'), None, None, 2), ('Custom', None, _('User-defined'), None, None, 3), ], 0, self.game.set_level) # action group for showing/hiding panel panel_action_group = Gtk.ActionGroup('AGPanel') panel_action_group.add_toggle_actions([('showpanel', None, _('Information Panel'), None, None, self.set_info_panel)]) panel_action_group.add_toggle_actions([('statusbar', None, _('Status Bar'), None, None, self.set_status_bar)]) self.panel_action_group = panel_action_group # Computer Player computer_player_action_group = Gtk.ActionGroup('ComputerPlayer') computer_player_action_group.add_radio_actions([ ('ComputerPlaysWhite', None, _('Computer Plays White'), None, None, 0), ('ComputerPlaysRed', None, _('Computer Plays Red'), None, None, 1), ('ComputerPlaysWhiteAndRed', None, _('Computer Plays White and Red'), None, None, 2), ('ComputerOff', None, _('Computer Off'), None, None, 3), ], 0, self.game.set_computer_player) self.computer_player_action_group = computer_player_action_group # Create a ToggleAction for flipping the board flip_the_board_action_group = Gtk.ActionGroup('FlipTheBoard') flip_the_board_action_group.add_toggle_actions([ ('FlipTheBoard', None, _('Flip the Board'), None, _('Flip the Board'), self.game.flip_the_board) ]) self.flip_the_board_action_group = flip_the_board_action_group # Create actions actiongroup.add_actions([('Quit', Gtk.STOCK_QUIT, _('Quit'), None, _('Quit the Program'), self.game.quit_game), ('NewGame', Gtk.STOCK_NEW, _('New Game'), None, _('New Game'), self.game.new_game), ('LoadGame', Gtk.STOCK_OPEN, _('Load Game'), None, _('Load Game'), self.game.load_game), ('SaveGame', Gtk.STOCK_SAVE, _('Save Game'), None, _('Save Game'), self.game.save_game), ('MoveNow', None, _('Move Now (m)'), None, _('Move Now'), self.game.move_now), ('Game', None, _('Game')), ('PositionEdit', None, _('Position Edit'), None, _('Position Edit'), \ self.board.position_edit_init), ('CopyFenToCB', None, _('Copy FEN to clipboard'), None, _('Copy FEN to clipboard'), \ self.game.copy_FEN_to_clipboard), ('PasteFenFromCB', None, _('Paste FEN from clipboard'), None, _('Paste FEN from clipboard'), \ self.game.paste_FEN_from_clipboard), ('CopyPDNToCB', Gtk.STOCK_COPY, _('Copy PDN to clipboard'), None, _('Copy PDN to clipboard'), \ self.game.copy_PDN_to_clipboard), ('PastePDNFromCB', Gtk.STOCK_PASTE, _('Paste PDN from clipboard'), None, _('Paste PDN from clipboard'), \ self.game.paste_PDN_from_clipboard), ('Edit', None, _('Edit')), ('SetCustomLevelDepth', None, _('Set User-Defined Level'), None, _('Set Custom Level Depth'), \ self.game.set_custom_search_depth), ('Level', None, _('Level')), ('Options', None, _('Options')), ('About', Gtk.STOCK_ABOUT, _('About'), None, _('Show About Box'), self.about_box), ('samhelp', Gtk.STOCK_HELP, _('Help (online)'), None, _('Samuel Help (Online)'), \ self.game.open_help), ('Help', None, _('Help')), ]) actiongroup.get_action('Quit').set_property('short-label', _('Quit')) actiongroup.get_action('MoveNow').set_sensitive(False) # Add the actiongroups to the uimanager uimanager.insert_action_group(actiongroup, 0) uimanager.insert_action_group(search_depth_actiongroup, 1) uimanager.insert_action_group(panel_action_group, 2) uimanager.insert_action_group(computer_player_action_group, 3) uimanager.insert_action_group(flip_the_board_action_group, 4) # Action groups that need settings to be saved/restored on program exit/startup self.save_action_groups = [search_depth_actiongroup, panel_action_group, flip_the_board_action_group, \ computer_player_action_group] ui = '''<ui> <menubar name="MenuBar"> <menu action="Game"> <menuitem action="NewGame"/> <separator/> <menuitem action="LoadGame"/> <menuitem action="SaveGame"/> <separator/> <menuitem action="MoveNow"/> <separator/> <menuitem action="Quit"/> </menu> <menu action="Edit"> <menuitem action="PositionEdit"/> <separator/> <menuitem action="CopyFenToCB"/> <menuitem action="PasteFenFromCB"/> <separator/> <menuitem action="CopyPDNToCB"/> <menuitem action="PastePDNFromCB"/> </menu> <menu action="Level"> <menuitem action="Beginner"/> <menuitem action="Advanced"/> <menuitem action="Expert"/> <menuitem action="Custom"/> <separator/> <menuitem action="SetCustomLevelDepth"/> <separator/> </menu> <menu action="Options"> <menuitem action="ComputerPlaysWhite"/> <menuitem action="ComputerPlaysRed"/> <menuitem action="ComputerPlaysWhiteAndRed"/> <menuitem action="ComputerOff"/> <separator/> <menuitem action="showpanel"/> <menuitem action="statusbar"/> <separator/> <menuitem action="FlipTheBoard"/> </menu> <menu action="Help"> <menuitem action="samhelp"/> <separator/> <menuitem action="About"/> </menu> </menubar> </ui>''' # Add a UI description uimanager.add_ui_from_string(ui) # Create a MenuBar menubar = uimanager.get_widget('/MenuBar') main_vbox.pack_start(menubar, False, True, 0) self.load_images() # Create a 8x8 table self.table = Gtk.Table(8, 8, True) self.table.set_border_width(25) aspect_frame = Gtk.AspectFrame(label=None, xalign=0.5, yalign=0.5, ratio=1.0, obey_child=False) aspect_frame.add(self.table) eb = Gtk.EventBox() #eb.add(self.table) eb.add(aspect_frame) eb.show() eb.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("darkslategrey")) main_vbox.pack_start(eb, True, True, 0) bot_hbox = Gtk.HBox(False, 0) main_vbox.pack_start(bot_hbox, False, True, 7) bot_hbox.show() vbox = Gtk.VBox(False, 0) bot_hbox.pack_end(vbox, False, False, 5) hbox1 = Gtk.HBox(False, 0) vbox.pack_start(hbox1, True, False, 0) hbox2 = Gtk.HBox(False, 0) vbox.pack_start(hbox2, True, False, 7) frame = Gtk.Frame() frame.set_shadow_type(Gtk.ShadowType.IN) frame.show() vp = Gtk.Viewport() vp.add(frame) vp.show() vp.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#EDECEB')) bot_hbox.pack_end(vp, True, True, 7) self.bot_hbox = bot_hbox self.infolabel = Gtk.Label() self.infolabel.modify_font(Pango.FontDescription("monospace 8")) frame.add(self.infolabel) self.infolabel.show() # get positions loaded msg msg = engine.rdisp() self.infolabel.set_text(msg) # go button self.go_button = Gtk.Button(_("Go")) self.go_button.connect("clicked", self.game.callback, 'Go') hbox1.pack_start(self.go_button, True, False, 0) self.go_button.show() # retract button self.retract_button = Gtk.Button(_("Retract")) self.retract_button.connect("clicked", self.game.callback, 'Retract') hbox1.pack_start(self.retract_button, True, False, 0) self.retract_button.show() # rewind to start button self.rts_button = Gtk.Button("<|") self.rts_button.connect("clicked", self.game.callback, '<|') hbox2.pack_start(self.rts_button, True, False, 0) self.rts_button.show() # rewind 1 move buttton self.rom_button = Gtk.Button("<") self.rom_button.connect("clicked", self.game.callback, '<') hbox2.pack_start(self.rom_button, True, False, 0) self.rom_button.show() # forward 1 move buttton self.fom_button = Gtk.Button(">") self.fom_button.connect("clicked", self.game.callback, '>') hbox2.pack_start(self.fom_button, True, False, 0) self.fom_button.show() # forward to end of game button self.fteog_button = Gtk.Button("|>") self.fteog_button.connect("clicked", self.game.callback, '|>') hbox2.pack_start(self.fteog_button, True, False, 0) self.fteog_button.show() vbox.show() hbox1.show() hbox2.show() # # widgets for position edit # self.posedit_hbox = Gtk.HBox(False, 0) main_vbox.pack_start(self.posedit_hbox, False, True, 7) vbox = Gtk.VBox(False, 0) self.posedit_hbox.pack_end(vbox, False, False, 5) hbox1 = Gtk.HBox(False, 0) vbox.pack_start(hbox1, True, False, 0) label = Gtk.Label() label.set_text(_("Side to Move")) label.show() vbox.pack_start(label, True, False, 7) hbox2 = Gtk.HBox(False, 0) vbox.pack_start(hbox2, True, False, 0) frame = Gtk.Frame() frame.set_shadow_type(Gtk.ShadowType.IN) frame.show() vp = Gtk.Viewport() vp.add(frame) vp.show() vp.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#EDECEB')) self.posedit_hbox.pack_end(vp, True, True, 7) self.infolabel2 = Gtk.Label() self.infolabel2.modify_font(Pango.FontDescription("monospace 8")) frame.add(self.infolabel2) self.infolabel2.show() self.cancel_button = Gtk.Button(_("Cancel")) self.cancel_button.connect("clicked", self.game.callback, 'Cancel') hbox1.pack_start(self.cancel_button, True, False, 0) self.cancel_button.show() button = Gtk.Button(_("OK")) button.connect("clicked", self.game.callback, 'OK') hbox1.pack_start(button, True, False, 0) button.show() self.radio_button_red = Gtk.RadioButton.new_with_label_from_widget( None, _("Red")) hbox2.pack_start(self.radio_button_red, True, False, 0) self.radio_button_red.show() self.radio_button_white = Gtk.RadioButton.new_with_label_from_widget( self.radio_button_red, _("White")) hbox2.pack_start(self.radio_button_white, True, False, 0) self.radio_button_white.show() vbox.show() hbox1.show() hbox2.show() # status bar self.status_bar = Gtk.Statusbar() main_vbox.pack_start(self.status_bar, False, False, 0) self.context_id = self.status_bar.get_context_id("samuel statusbar") self.set_status_bar_msg(_("Red to Move")) self.window.show_all() self.posedit_hbox.hide() panel_action_group.get_action('showpanel').set_active(True) panel_action_group.get_action('statusbar').set_active(True)