Ejemplo n.º 1
0
# ---

# What's more, you can also access low level API for further usage.
with safe_connection(_DEVICE_ID) as connection:
    builder = CommandBuilder()
    builder.down(0, 400, 400, 50)
    builder.commit()
    builder.move(0, 500, 500, 50)
    builder.commit()
    builder.move(0, 800, 400, 50)
    builder.commit()
    builder.up(0)
    builder.commit()

    builder.publish(connection)
    # if you are using MNTDevice object, replace with this:
    # builder.publish(device.connection)

# ---

# Of course, you may want to operate it just like using minitouch itself.
# send raw text to it
_OPERATION = '''
d 0 150 150 50\n
c\n
u 0\n
c\n
'''

with safe_connection(_DEVICE_ID) as conn:
class TouchEventManager():
    '''
    Manages all touch events in each frame for a single device.

    At each frame, a new CommandBuilder is prepared for all widgets.
    '''
    def __init__(self, device):
        self.device = device
        self.touch_id_max = 10
        self.widget_touch_id = dict()
        self.command_builder = CommandBuilder()

    # touch id methods

    def get_touch_id(self, widget):
        '''
        assigns a touch id to a widget
        '''
        assigned_ids = self.widget_touch_id.values()
        for i in range(self.touch_id_max):
            if i not in assigned_ids:
                self.widget_touch_id[widget] = i
                return i
        logger.error(
            "Cannot assign new touch id to widget {widget_name}".format(
                widget_name=widget.name))

    def reclaim_touch_id(self, widget):
        '''
        reclaim the touch id previously assigned to a widget
        '''
        if self.widget_touch_id[widget] != -1:
            self.widget_touch_id[widget] = -1
            return
        logger.error(
            "Already reclaimed touch id of widget {widget_name}".format(
                widget_name=widget.name))

    # button methods

    def update(self):
        '''
        All actions are delayed until update of TouchEventManager is called, 
        so that in a single
        frame no contradictory actions are performed (for example, touching and
        releasing a button at the same frame should not be allowed because it is 
        not physically possible.)
        this method of TouchEventManager should be called only after all 
        widgets updates are called.
        '''

        # commit all changes
        # if nothing is to be done in this frame, don't send anything.
        if self.command_builder._content == '':
            pass
        else:
            self.command_builder.commit()
            # logger.debug( self.command_builder._content )
            self.command_builder.publish(self.device.connection)

        # refresh the command_builder
        self.command_builder = CommandBuilder()