예제 #1
0
파일: cpu.py 프로젝트: statkclee/rur-ple-1
    def __init__(self,
                 avenues=1,
                 streets=1,
                 orient_key='E',
                 beepers=0,
                 name=None,
                 colour='grey',
                 parent=None):
        #UsedRobot.__init__(self, avenues=avenues, orient_key = orient_key,
        #        beepers=beepers, name=name, colour=colour, parent=parent)
        if parent == None:
            parent = Visible_world()

        true_robot = parent.addOneRobot(avenues=avenues,
                                        streets=streets,
                                        orient_key=orient_key,
                                        beepers=beepers,
                                        name=name,
                                        colour=colour,
                                        better=True)
        self.robot = parent.robot_dict[true_robot.name]
        self.name = true_robot.name
        self.program = rur_program()
        self.parent = parent
        self.parent.object_dict[self.name] = True
        self.program.wait_update_refresh(self.robot, self.name)
예제 #2
0
 def __init__(self, parent, id=-1, size=wx.DefaultSize):
     wx.ScrolledWindow.__init__(self,
                                parent,
                                id, (0, 0),
                                size=size,
                                style=wx.SUNKEN_BORDER)
     self.SetBackgroundColour("WHITE")
     self.world = Visible_world()
     self.editor = False  #
     self.InitialiseVariables()
     self.MakePopupMenu()
     self.bindEvents()
     self.SetFocus()
예제 #3
0
    def __init__(self, avenues=1, streets=1, orient_key='E',
                 beepers=0, name=None, colour='grey', parent = None):

        if parent == None:
            parent = Visible_world()
            
        true_robot = parent.addOneRobot(avenues=avenues,
                                        streets = streets,
                                        orient_key=orient_key,
                                        beepers=beepers,
                                        name = name,
                                        colour = colour)
        self.robot = parent.robot_dict[true_robot.name]
        self.name = true_robot.name
        self.program = rur_program()
        self.parent = parent
        self.parent.object_dict[self.name] = True
        self.program.wait_update_refresh(self.robot, self.name)
예제 #4
0
 def __init__(self, parent, id = -1, size = wx.DefaultSize):
     wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size,
                               style=wx.SUNKEN_BORDER)
     self.SetBackgroundColour("WHITE")
     self.world = Visible_world()
     self.editor = False  #
     self.InitialiseVariables()
     self.MakePopupMenu()
     self.bindEvents()
     self.SetFocus()
예제 #5
0
class WorldGUI(wx.ScrolledWindow):
    def __init__(self, parent, id=-1, size=wx.DefaultSize):
        wx.ScrolledWindow.__init__(self,
                                   parent,
                                   id, (0, 0),
                                   size=size,
                                   style=wx.SUNKEN_BORDER)
        self.SetBackgroundColour("WHITE")
        self.world = Visible_world()
        self.editor = False  #
        self.InitialiseVariables()
        self.MakePopupMenu()
        self.bindEvents()
        self.SetFocus()

    def InitialiseVariables(self):
        # Create the world image
        self.world.updateImage = False
        self.world.DoDrawing()
        # Initialize the buffer bitmap.  No real DC is needed at this point.
        self.buffer = wx.EmptyBitmap(self.world.maxWidth, self.world.maxHeight)
        self.drawImage()

        # Set the size of the total window, of which only a small part
        # will be displayed; apparently SetVirtualSize needs
        # a single (tuple) argument, which explains the double (( )).
        self.SetVirtualSize((self.world.maxWidth, self.world.maxHeight))

        # Set the scrolling rate; use same value in both horizontal and
        # vertical directions.
        scrollRate = self.world.tile_narrow + self.world.tile_wide
        self.SetScrollRate(scrollRate, scrollRate)
        # yPos below set so that we are at the bottom of the scrolledWindow
        self.SetScrollbars(1,
                           1,
                           self.world.maxWidth,
                           self.world.maxHeight,
                           yPos=self.world.maxHeight)
        # allow for testing "new and improved" robot
        self.newRobot = False

    def bindEvents(self):
        wx.EVT_PAINT(self, self.OnPaint)
        wx.EVT_LEFT_DOWN(self, self.OnLeftDown)  # for "drawing"
        #wx.EVT_CHAR(self, self.MyKeys) # to test Robot actions
        wx.EVT_IDLE(self, self.OnIdle)
        wx.EVT_RIGHT_UP(self, self.OnRightUp)

    def OnIdle(self, event=None):
        """
        Do any needed updating on Idle time ...
        """
        if self.world.updateImage:
            self.drawImage()
        self.world.updateImage = False

    def OnPaint(self, event=None):
        try:
            dc = wx.BufferedPaintDC(self, self.buffer, wx.BUFFER_VIRTUAL_AREA)
        except:
            dc = wx.BufferedPaintDC(self,
                                    self.buffer)  # before wxPython 2.5.4.1

    def isActive(self, event):
        xView, yView = self.GetViewStart()
        xDelta, yDelta = self.GetScrollPixelsPerUnit()
        self.x, self.y = event.GetPositionTuple()
        self.x += xView * xDelta
        self.y += yView * yDelta
        # Test to see if in "active" zone i.e. inside world borders
        if (self.x > self.world.xOffset and self.y > self.world.yTopOffset
                and self.y < self.world.maxHeight - self.world.yOffset
                and self.x <
                self.world.maxWidth - self.world.right_scroller_space):
            return True
        else:
            return False

    def OnLeftDown(self, event):
        """Called when the left mouse button is pressed and build
           or remove walls."""
        self.SetFocus()
        if self.isActive(event):
            col, row = self.world.CalculatePosition(self.x, self.y)
            self.world.ChangeWall(col, row)
            if self.world.updateImage:
                self.drawImage()
                self.Refresh()
        else:
            pass  # added for clarity; we are on the outside

    def MakePopupMenu(self):
        """Make a menu for beepers that can be popped up later;
        this menu will offer the possibility of
        putting a given number of beepers
        at the current location (street/avenue intersection)."""
        menu = wx.Menu()
        self._beeperChoice = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \
                          11, 12, 13, 14, 15, 20, 40, 60, 80, 99]
        maxNumberOfBeepers = len(self._beeperChoice)
        # This version is revised from 0.9.1 to avoid "hard-wired"
        # IDs in appending the menu and let wxPython decide on suitable
        # values.  The previous version caused problems on MacOS.
        # Note that OnMenuSetBeepers() had to be modified as well.
        self.popID = []
        for index, beepers in enumerate(self._beeperChoice):
            self.popID.append(wx.NewId())
            menu.Append(self.popID[index], str(beepers))
            self.Bind(wx.EVT_MENU, self.OnMenuSetBeepers, id=self.popID[index])
        self.menu = menu

    def OnMenuSetBeepers(self, event):
        self._num = self._beeperChoice[self.popID.index(event.GetId())]

    def OnRightUp(self, event):
        """called when the right mouse button is released,
        will call the popup menu, offering the possibility of
        putting a given number of beepers
        at the current location (street/avenue intersection)."""
        if self.isActive(event):
            col, row = self.world.CalculatePosition(self.x, self.y)
            if row % 2:
                if col % 2:
                    pt = event.GetPosition()
                    self.PopupMenu(self.menu, pt)
                    av = (col + 1) / 2
                    st = (row + 1) / 2
                    self.world.setBeepers((av, st), self._num)
                    if self.world.updateImage:
                        self.drawImage()
                        self.Refresh()
        else:
            pass  # added for clarity; we are on the outside

    def MyKeys(self, event):
        code = event.GetKeyCode()
        if code == wx.WXK_UP:  # up arrow
            try:
                self.world.MoveRobot('robot')  # may raise an exception
                self.scrollWorld('robot')
            except dialogs.HitWallException, mesg:
                dialogs.DialogHitWallError(mesg)
        elif code == wx.WXK_LEFT:  # left arrow
            self.world.TurnRobotLeft('robot')
예제 #6
0
class WorldGUI(wx.ScrolledWindow):
    def __init__(self, parent, id = -1, size = wx.DefaultSize):
        wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size,
                                  style=wx.SUNKEN_BORDER)
        self.SetBackgroundColour("WHITE")
        self.world = Visible_world()
        self.editor = False  #
        self.InitialiseVariables()
        self.MakePopupMenu()
        self.bindEvents()
        self.SetFocus()

    def InitialiseVariables(self):
        # Create the world image
        self.world.updateImage = False
        self.world.DoDrawing()
        # Initialize the buffer bitmap.  No real DC is needed at this point.
        self.buffer = wx.EmptyBitmap(self.world.maxWidth, self.world.maxHeight)
        self.drawImage()

        # Set the size of the total window, of which only a small part
        # will be displayed; apparently SetVirtualSize needs
        # a single (tuple) argument, which explains the double (( )).
        self.SetVirtualSize((self.world.maxWidth, self.world.maxHeight))

        # Set the scrolling rate; use same value in both horizontal and
        # vertical directions.
        scrollRate = self.world.tile_narrow + self.world.tile_wide
        self.SetScrollRate(scrollRate, scrollRate)
        # yPos below set so that we are at the bottom of the scrolledWindow
        self.SetScrollbars(1, 1, self.world.maxWidth,
                           self.world.maxHeight, yPos=self.world.maxHeight)
        # allow for testing "new and improved" robot
        self.newRobot = False

    def bindEvents(self):
        wx.EVT_PAINT(self, self.OnPaint)
        wx.EVT_LEFT_DOWN(self, self.OnLeftDown) # for "drawing"
        wx.EVT_CHAR(self, self.MyKeys) # to test Robot actions
        wx.EVT_IDLE(self, self.OnIdle)
        wx.EVT_RIGHT_UP(self, self.OnRightUp)

    def OnIdle(self, event=None):
        """
        Do any needed updating on Idle time ...
        """
        if self.world.updateImage:
            self.drawImage()
        self.world.updateImage = False

    def OnPaint(self, event=None):
        try:
            dc = wx.BufferedPaintDC(self, self.buffer, wx.BUFFER_VIRTUAL_AREA)
        except:
            dc = wx.BufferedPaintDC(self, self.buffer) # before wxPython 2.5.4.1

    def isActive(self, event):
        xView, yView = self.GetViewStart()
        xDelta, yDelta = self.GetScrollPixelsPerUnit()
        self.x, self.y = event.GetPositionTuple()
        self.x += xView*xDelta
        self.y += yView*yDelta
        # Test to see if in "active" zone i.e. inside world borders
        if (self.x > self.world.xOffset and
           self.y > self.world.yTopOffset and
           self.y < self.world.maxHeight - self.world.yOffset and
           self.x < self.world.maxWidth - self.world.right_scroller_space):
            return True
        else:
            return False

    def OnLeftDown(self, event):
        """Called when the left mouse button is pressed and build
           or remove walls."""
        self.SetFocus()
        if self.isActive(event):
            col, row = self.world.CalculatePosition(self.x, self.y)
            self.world.ChangeWall(col, row)
            if self.world.updateImage:
                self.drawImage()
                self.Refresh()
        else:
            pass   # added for clarity; we are on the outside

    def MakePopupMenu(self):
        """Make a menu for beepers that can be popped up later;
        this menu will offer the possibility of
        putting a given number of beepers
        at the current location (street/avenue intersection)."""
        menu = wx.Menu()
        self._beeperChoice = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \
                          11, 12, 13, 14, 15, 20, 40, 60, 80, 99]
        maxNumberOfBeepers = len(self._beeperChoice)
        # This version is revised from 0.9.1 to avoid "hard-wired"
        # IDs in appending the menu and let wxPython decide on suitable
        # values.  The previous version caused problems on MacOS.
        # Note that OnMenuSetBeepers() had to be modified as well.
        self.popID = []
        for index, beepers in enumerate(self._beeperChoice):
            self.popID.append(wx.NewId())
            menu.Append(self.popID[index], str(beepers))
            self.Bind(wx.EVT_MENU, self.OnMenuSetBeepers, 
                      id=self.popID[index])
        self.menu = menu
        
    def OnMenuSetBeepers(self, event):
        self._num = self._beeperChoice[self.popID.index(event.GetId())]

    def OnRightUp(self, event):
        """called when the right mouse button is released,
        will call the popup menu, offering the possibility of
        putting a given number of beepers
        at the current location (street/avenue intersection)."""
        if self.isActive(event):
            col, row = self.world.CalculatePosition(self.x, self.y)
            if row%2:
                if col%2:
                    pt = event.GetPosition()
                    self.PopupMenu(self.menu, pt)
                    av = (col+1)/2
                    st = (row+1)/2
                    self.world.setBeepers((av, st), self._num)
                    if self.world.updateImage:
                        self.drawImage()
                        self.Refresh()
        else:
            pass   # added for clarity; we are on the outside

    def MyKeys(self, event):
        message = """
            F5: Help (i.e. this message)

Editing world:
            lower case e: toggle in/out of edit walls mode
            Left mouse button: add or remove walls (if in edit walls mode)
            Right mouse button: put beepers at intersection

Robot actions:
            Up arrow:     move robot forward
            Left arrow:   turn robot left
            lower case p: grab_beeper
            upper case P: drop_beeper

            Errors will occur if you attempt to move through a wall,
            put a beeper when you carry none,
            or try to pick up a beeper where there is none.

For testing only (future features):
            F7: creates New_improved_robot
            Right arrow:   turn robot right (only New_improved_robot)
            F8: creates Used_robot (default)"""
        code = keycode(event)
        if code == wx.WXK_UP:           # up arrow
            if 'robot' in self.world.robot_dict:
                try:
                    self.world.MoveRobot('robot') # may raise an exception
                    self.scrollWorld('robot')
                except dialogs.HitWallException, mesg:
                    dialogs.DialogHitWallError(mesg)
        elif code == wx.WXK_LEFT:       # left arrow
            self.world.TurnRobotLeft('robot')