Beispiel #1
0
def show_context_menu():
    global modify_object_id
    global object_ids

    if len(modify_object_id) > 0:
        return

    guiMan.setTransparency(1.0)

    sim_context = OpenNero.getSimContext()

    # find the screen position at which to open the context menu
    cursor = sim_context.getMousePosition()
    location = sim_context.getClickedPosition(cursor.x, cursor.y)
    selected_object_id = sim_context.getClickedEntityId(cursor.x, cursor.y)

    if selected_object_id not in object_ids and abs(location.z) > 10: return

    print "location:", location

    contextMenu = gui.create_context_menu(guiMan, 'context', cursor)

    def add_wall():
        obj_id = common.addObject(
            "data/shapes/cube/Cube.xml",
            OpenNero.Vector3f(location.x, location.y,
                              constants.HEIGHT + constants.OFFSET),
            OpenNero.Vector3f(0, 0, 90),
            scale=OpenNero.Vector3f(5, 30, constants.HEIGHT * 2),
            type=1)
        object_ids[obj_id] = set(['rotate', 'move', 'scale', 'remove'])

    def rotate_object():
        modify_object_id['rot'] = selected_object_id

    def scale_object():
        modify_object_id['scale'] = selected_object_id

    def move_object():
        modify_object_id['move'] = selected_object_id

    def remove_flag():
        module.getMod().remove_flag()

    def place_flag():
        module.getMod().change_flag([location.x, location.y, 0])

    def place_basic_turret():
        obj_id = module.getMod().place_basic_turret(
            [location.x, location.y, 0])
        object_ids[obj_id] = set(['move', 'remove'])

    def set_spawn_1():
        module.getMod().set_spawn(location.x, location.y,
                                  constants.OBJECT_TYPE_TEAM_0)

    def set_spawn_2():
        module.getMod().set_spawn(location.x, location.y,
                                  constants.OBJECT_TYPE_TEAM_1)

    def remove_wall():
        common.removeObject(selected_object_id)

    if selected_object_id in object_ids:
        operations = object_ids[selected_object_id]

        if 'rotate' in operations:
            rotateButton = gui.create_button(guiMan, 'rotate',
                                             OpenNero.Pos2i(0, 0),
                                             OpenNero.Pos2i(0, 0), '')
            rotateButton.OnMouseLeftClick = lambda: rotate_object()
            contextMenu.addItem('Rotate Object', rotateButton)

        if 'scale' in operations:
            scaleButton = gui.create_button(guiMan, 'scale',
                                            OpenNero.Pos2i(0, 0),
                                            OpenNero.Pos2i(0, 0), '')
            scaleButton.OnMouseLeftClick = lambda: scale_object()
            contextMenu.addItem('Scale Object', scaleButton)

        if 'move' in operations:
            moveButton = gui.create_button(guiMan, 'move',
                                           OpenNero.Pos2i(0, 0),
                                           OpenNero.Pos2i(0, 0), '')
            moveButton.OnMouseLeftClick = lambda: move_object()
            contextMenu.addItem('Move Object', moveButton)

        if 'remove' in operations:
            removeButton = gui.create_button(guiMan, 'remove',
                                             OpenNero.Pos2i(0, 0),
                                             OpenNero.Pos2i(0, 0), '')
            removeButton.OnMouseLeftClick = lambda: remove_wall()
            contextMenu.addItem('Remove Object', removeButton)

    else:
        wallButton = gui.create_button(guiMan, 'wall', OpenNero.Pos2i(0, 0),
                                       OpenNero.Pos2i(0, 0), '')
        wallButton.OnMouseLeftClick = lambda: add_wall()
        contextMenu.addItem('Add wall', wallButton)

        rmFlagButton = gui.create_button(guiMan, 'flag', OpenNero.Pos2i(0, 0),
                                         OpenNero.Pos2i(0, 0), '')
        rmFlagButton.OnMouseLeftClick = lambda: remove_flag()
        contextMenu.addItem('Remove Flag', rmFlagButton)

        flagButton = gui.create_button(guiMan, 'flag', OpenNero.Pos2i(0, 0),
                                       OpenNero.Pos2i(0, 0), '')
        flagButton.OnMouseLeftClick = lambda: place_flag()
        contextMenu.addItem('Place Flag', flagButton)

        turretButton = gui.create_button(guiMan, 'b_turret',
                                         OpenNero.Pos2i(0, 0),
                                         OpenNero.Pos2i(0, 0), '')
        turretButton.OnMouseLeftClick = lambda: place_basic_turret()
        contextMenu.addItem('Place Basic Turret', turretButton)

        spawn1Button = gui.create_button(guiMan, 'blue spawn',
                                         OpenNero.Pos2i(0, 0),
                                         OpenNero.Pos2i(0, 0), '')
        spawn1Button.OnMouseLeftClick = lambda: set_spawn_1()
        contextMenu.addItem('Set Blue Spawn Location', spawn1Button)

        spawn2Button = gui.create_button(guiMan, 'red spawn',
                                         OpenNero.Pos2i(0, 0),
                                         OpenNero.Pos2i(0, 0), '')
        spawn2Button.OnMouseLeftClick = lambda: set_spawn_2()
        contextMenu.addItem('Set Red Spawn Location', spawn2Button)