Beispiel #1
0
def wait_for_event(mask=EventClassType.ANY_EVENT):
    '''
    Dismisses the process until an event occurs whose type is covered by
    the event mask.  The mask parameter is a combination of the events of
    interest.  For example, to wait for a mouse event or an action event,
    clients can use the following call:

    e = waitForEvent(MOUSE_EVENT + ACTION_EVENT);

    The mask parameter is optional.  If it is missing,
    waitForEvent accepts any event.

    As a more sophisticated example, the following code is the canonical
    event loop for an animated application that needs to respond to mouse,
    key, and timer events::

        timer = gtimer.GTimer(ANIMATION_DELAY_IN_MILLISECONDS);
        timer.start()
        while(True):
            e = gevents.waitForEvent(TIMER_EVENT + MOUSE_EVENT + KEY_EVENT)
            if(e.getEventClass == gevents.EventClassType.TIMER_EVENT):
                takeAnimationStep()
            elif(e.getEventClass == gevents.EventClassType.MOUSE_EVENT):
                handleMouseEvent(e)
            elif(e.getEventClass == gevents.EventClassType.KEY_EVENT):
                handleKeyEvent(e)

    @type mask: EventClassType
    @param mask: EventClassType to wait for, defaults to ANY_EVENT
    @rtype: GEvent
    '''
    import campy.private.platform as _platform
    return _platform.Platform().waitForEvent(mask)
Beispiel #2
0
    def selected_item(self, item):
        """Sets the chooser so that it shows the specified item.  If the item
        does not exist in the chooser, no change occurs.

        @type item: string
        @rtype: void"""
        _platform.Platform().setSelectedItem(self, item)
Beispiel #3
0
    def getText(self):
        '''
        Returns the contents of the text field.

        @rtype: string
        '''
        return _platform.Platform().getText(self)
Beispiel #4
0
    def clear(self):
        """Clear all content from this GWindow.

        Both the background and foreground layers are cleared with this function.
        """
        _platform.Platform().gwindow_clear(
            self)  # Remove from background layer.
        self._top.removeAll()  # Remove from foreground layer.
Beispiel #5
0
    def setText(self, str):
        '''
        Sets the text of the field to the specified string.

        @type str: string
        @rtype: void
        '''
        _platform.Platform().setText(self, str)
Beispiel #6
0
    def addItem(self, item):
        '''
        Adds a new item consisting of the specified string.

        @type item: string
        @rtype: void
        '''
        _platform.Platform().addItem(self, item)
Beispiel #7
0
    def _request_focus(self):
        """Ask the OS to assign keyboard focus to this GWindow.

        This brings it to the top and ensures that key events are delivered correctly.
        Clicking in the window automatically requests the focus.

        It is not guaranteed that the OS will give focus to this GWindow.
        """
        _platform.Platform().gwindow_request_focus(self)
Beispiel #8
0
    def getBounds(self):
        '''
        Returns the bounding GRectangle of the GInteractor

        @rtype: GRectangle
        '''
        size = _platform.Platform().getSize(self)
        return _gtypes.GRectangle(self.x, self.y, size.getWidth(),
                                  size.getHeight())
Beispiel #9
0
    def start(self):
        """Starts the timer.

        A timer continues to generate timer events until it
        is stopped; to achieve the effect of a one-shot timer, the simplest
        approach is to call the <code>stop</code> method inside the event
        handler.
        """
        _platform.Platform().gtimer_start(self)
Beispiel #10
0
 def show_message_dialog(cls,
                         message,
                         title="",
                         message_type=MessageType.PLAIN):
     if message_type not in MessageType:
         error("GOptionPane::showMessageDialog: Illegal dialog type.")
     if not title:
         title = "Message"
     _platform.Platform().goptionpane_show_message_dialog(
         message, title, message_type)
Beispiel #11
0
    def __del__(self):
        """Deletes the Java resources associated with this timer.

        This method is called when there are no more references to this object
        (Python keeps track), and is not guaranteed to be called if the object
        exists when the interpreter exits.

        TODO: Make sure module symbols from _platform haven't been cleaned up already.
        """
        print("In del")
        _platform.Platform().gtimer_delete(self)
Beispiel #12
0
    def setActionCommand(self, cmd):
        '''
        Sets the action command to the indicated string.  If the string is not
        empty, activating the interactor generates a GActionEvent.

        @type cmd: string
        @param cmd: action command
        @rtype: void
        '''
        self.actionCommand = cmd
        _platform.Platform().setActionCommand(self, cmd)
Beispiel #13
0
    def __init__(self):
        '''
        Creates a chooser that initially contains no items, which are added
        using the addItem method.  Assigning an action command
        to the chooser causes it to generate an action event whenever the
        user selects an item.

        @rtype: void
        '''
        GInteractor.__init__(self)
        _platform.Platform().createGChooser(self)
Beispiel #14
0
    def remove_from_region(self, gobj, region):
        """Remove an interactor from the control strip in a given region.

        The region argument must be some region from the :class:`Region` enum.

        :param gobj: The interactor to remove from a region.
        :param region: The region from which the interactor will be removed.
        :type region: Region
        """
        # TODO(sredmond): Either here, or at the platform level, convert into the region as a string.
        _platform.Platform().gwindow_remove_from_region(self, gobj, region)
Beispiel #15
0
    def __init__(self, label):
        """Construct a GButton with the specified label.

        Also sets the action command for this button to the label.

        :param str label: A label for this GButton.
        """
        super().__init__()
        self.label = label
        self.action_command = label
        _platform.Platform().gbutton_constructor(self, label)
Beispiel #16
0
    def __init__(self, milliseconds):
        """
        Creates a GTimer object that generates a GTimerEvent
        each time the specified number of milliseconds has elapsed.

        No events are generated until the client calls start on the timer.
        For more details on using timers, see the documentation for the GTimerEvent class.

        :type milliseconds: int
        :param milliseconds: interval for timer event
        """
        _platform.Platform().gtimer_constructor(self, milliseconds)
Beispiel #17
0
    def __init__(self, nChars=10):
        '''
        Creates a text field capable of holding nChars characters,
        which defaults to 10.  Assigning an action command to the text field
        causes it to generate an action event whenever the user types the
        ENTER key.

        @type nChars: int
        @rtype: void
        '''
        GInteractor.__init__(self)
        _platform.Platform().createGTextField(self, nChars)
Beispiel #18
0
    def __init__(self, label):
        """
        Creates a GCheckBox with the specified label.

        In contrast to the GButton constructor, this constructor does not set an
        action command.

        :param str label: A label for this GCheckBox.
        """
        super().__init__()
        self.label = label
        _platform.Platform().gcheckbox_constructor(self, label)
Beispiel #19
0
    def __init__(self, filename):
        """Creates a Sound object.
        The default constructor creates an empty sound that cannot be played.
        The second form initializes the sound by reading in the contents of
        the specified file.

        @type filename: string
        @param filename: sound file
        @rtype: void
        """
        self.filename = filename
        _platform.Platform().create_sound(self, filename)
Beispiel #20
0
    def setSize(self, width=0.0, height=0.0, size=None):
        '''
        Changes the size of the interactor to the specified width and height.

        @type width: float
        @type height: float
        @type size: GDimension, overrides width and height parameters
        @rtype: void
        '''
        if (size != None):
            width = size.width
            height = size.height
        _platform.Platform().gobject_set_size(self, width, height)
Beispiel #21
0
def pause(milliseconds):
    """Pause for the given number of milliseconds.

    This is useful for animation where the graphical updates would otherwise be
    too fast to observe.

    To pause for half a second::

        pause(500)

    :param milliseconds: The number of milliseconds to pause.
    """
    _platform.Platform().gtimer_pause(milliseconds)
Beispiel #22
0
def get_next_event(mask=EventClassType.ANY_EVENT):
    '''
    Checks to see if there are any events of the desired type waiting on the
    event queue.  If so, this function returns the event in exactly the same
    fashion as waitForEvent; if not, getNextEvent
    returns an invalid event.  The mask parameter is optional.
    If it is missing, getNextEvent accepts any event.

    @type mask: EventClassType
    @param mask: EventClasstype to check for, defaults to ANY_EVENT
    @rtype: GEvent
    '''
    import campy.private.platform as _platform
    return _platform.Platform().getNextEvent(mask)
Beispiel #23
0
 def show_option_dialog(cls,
                        message,
                        options,
                        title="",
                        initially_selected=""):
     if not title:
         title = "Select an option"
     index = _platform.Platform().goptionpane_show_option_dialog(
         message, title, options, initially_selected)
     if cls.InternalResult(
             index
     ) != cls.InternalResult.CLOSED_OPTION and 0 <= index < len(options):
         return options[index]
     return ""
Beispiel #24
0
    def draw(self, gobj, x=None, y=None):
        """Draw a GObject on this GWindow's background layer.

        The background layer is for static drawings that cannot be modified once drawn.

        If both x and y are supplied, the object is moved to that location before drawing.

        :param gobj: The GObject to draw to this GWindow's background layer.
        :param x: The x-coordinate at which to draw the GObject.
        :param y: The y-coordinate at which to draw the GObject.
        """
        if x is not None and y is not None:
            gobj.location = x, y
        _platform.Platform().gwindow_draw(self, gobj)
Beispiel #25
0
    def __init__(self,
                 width=DEFAULT_WIDTH,
                 height=DEFAULT_HEIGHT,
                 visible=True,
                 title="",
                 color=None,
                 top=None):
        """Create a new GWindow. Optionally, supply a specific width and height.

        Initial visibility, window title, marker color, and top compound can also
        be specified, but will default to reasonable values.

        To create a new GWindow of a default size::

            window = GWindow()

        To create a new GWindow with a specific size::

            window = GWindow(width=1280, height=800)

        To create a new GWindow with the default size but a specific title and marker color::

            window = GWindow(title="My Window", color=GColor.RED)

        :param width: The initial width of the GWindow.
        :param height: The initial height of the GWindow.
        :param visible: The default visibility of the GWindow.
        :param title: The displayed title on the GWindow.
        :param color: The marker color used to draw GObjects.
        :param top: The topmost GCompound in the GWindow.
        """
        self._width = width
        self._height = height
        self._visible = visible
        self._title = title

        if not color:
            color = _gcolor.color_to_rgb("BLACK")
        self._color = color

        if not top:
            top = _gobjects.GCompound()
        self._top = top

        # TODO(sredmond): Isn't it a little silly to pass this object along with its attributes?
        _platform.Platform().gwindow_constructor(self, self._width,
                                                 self._height, self._top)
Beispiel #26
0
    def set_region_alignment(self, region, align):
        """Set an alignment for a given region.

        Both the region and alignment arguments must be from the :class:`Region`
        and :class:`Alignment` enums respectively.

        To CENTER the content in the SOUTH region::

            window = GWindow()
            window.set_region_alignment(Region.SOUTH, Alignment.CENTER)

        :param region: The region in which to set alignment.
        :type region: Region
        :param align: The alignment to set in the region.
        :type align: Alignment
        """
        # TODO(sredmond): Either here, or at the platform level, convert into the region and alignment as a string.
        _platform.Platform().gwindow_set_region_alignment(self, region, align)
Beispiel #27
0
def _test():
    import campy.graphics.gevents as gevents
    print('Creating timer...')
    timer = GTimer(1000)
    print('Starting timer...')
    timer.start()
    print('Started!')
    count = 0
    while count < 10:
        print('Waiting for event')
        event = _platform.Platform().getNextEvent(
            gevents.EventType.TIMER_TICKED)
        if event.getEventType() == gevents.EventType.TIMER_TICKED:
            print('Received a TICK')
            count += 1
            if count == 5:
                print('Pausing for 5s')
                timer.pause(4000)
    timer.stop()
Beispiel #28
0
    def add_to_region(self, gobj, region):
        """Add an interactor to the control strip in a given region.

        The interactor could also be a GLabel.

        The region argument must be some region from the :class:`Region` enum.

        To add a button to the SOUTH region::

            window = GWindow()
            button = GButton("Click me!")
            window.add_to_region(button, Region.SOUTH)

        :param gobj: The interactor to add to a region.
        :param region: The region to which the interactor will be added.
        :type region: Region
        """
        # TODO(sredmond): Either here, or at the platform level, convert into the region as a string.
        _platform.Platform().gwindow_add_to_region(self, gobj, region)
Beispiel #29
0
    def __init__(self, min_value=0, max_value=100, starting_value=50):
        """Create a horizontal GSlider.

        The client can specify a minimum value, maximum value, and starting
        value.

        :param int min_value: The minimum value of this GSlider.
        :param int max_value: The maximum value of this GSlider. Should be >=
            min_value, but this is not currently enforced.
        :param int starting_value: The starting value of this GSlider. Should be
            between min_value and max_value. If not, is set to min_value.
        """
        super().__init__()
        self.min_value = min_value
        self.max_value = max_value
        # TODO(sredmond): Error check that max_value >= min_value.
        if not min_value <= starting_value <= max_value:
            starting_value = min_value
        _platform.Platform().gslider_constructor(self, min_value, max_value,
                                                 starting_value)
Beispiel #30
0
    def show_confirm_dialog(cls,
                            message,
                            title="",
                            confirm_type=ConfirmType.YES_NO):
        if confirm_type not in ConfirmType:
            error("GOptionPane::show_confirm_dialog: Illegal dialog type.")

        if not title:
            title = "Select an option"

        result = cls.InternalResult(
            _platform.Platform().goptionpane_show_confirm_dialog(
                message, title, confirm_type))
        print("from platform: ", result)
        if result in [
                cls.InternalResult.OK_OPTION, cls.InternalResult.YES_OPTION
        ]:
            # this is weird code because JOptionPane thinks of OK and Yes as the same,
            #and differentiates based on context of whether this is an OK/Cancel or Yes/No dialog
            return ConfirmResult.OK if confirm_type == ConfirmType.OK_CANCEL else ConfirmResult.YES
        elif result == cls.InternalResult.NO_OPTION:
            return ConfirmResult.NO
        else:
            return ConfirmResult.CANCEL