Пример #1
0
def load_image_from_json(image_data, level, image_type=None):
    global global_object_id
    image = None
    if image_type is None:
        try:
            image_type = image_data["type"]
        except KeyError:
            log("Error: No image_type for:" + str(image_data),1)
            return 
    pos = get_element(image_data, "pos")
    size = get_element(image_data, "size")
    layer = get_element(image_data, "layer")
    angle = get_element(image_data, "angle")
    object_id = get_element(image_data, "id")
    if object_id is None:
        object_id = global_object_id
    if angle is None:
        angle = 0
    if image_type == "GameObject":
        image = GameObject()
        image.pos = Vector2(pos)
        image.size = Vector2(size)
        image.update_rect()
        image.angle = angle
        image.id = object_id
    elif image_type == "Image":
        image = Image.parse_image(image_data, pos, size, angle)
        if image is not None:
            image.id = object_id
    elif image_type == "Text":
        font = get_element(image_data, "font")
        text = get_element(image_data, "text")
        color = get_element(image_data, "color")
        if font and text:
            font = CONST.path_prefix+font
        else:
            log("Invalid arg font and text not defined for Text",1)
            return
        if not color:
            color = [0,0,0]
        image = Text(pos, size, font, text, angle,color)
        image.id = object_id
    else:
        if not isinstance(image_type,CONST.string_type):
            log("image_type not a string",1)
            return
        for c in image_type:
            if c != '.' and c != '_' and not c.isalpha():
                return
        dir_list = image_type.split(".")
        try:
            exec('''from %s import %s'''%(".".join(dir_list[0:len(dir_list)-1]), dir_list[len(dir_list)-1]))
        except ImportError:
            log("Error: ImportError with: "+str(image_type),1)
            return
        try:
            d = locals()
            exec('''image = %s.parse_image(image_data, pos, size, angle)'''%(dir_list[len(dir_list)-1]),globals(),d)
            image = d['image']
        except Exception as e:
            import traceback
            log('Error with loading image_type: %s'%(image_type)+" "+str(e),1)
            traceback.print_exc()
            return
    physic_objects = get_element(image_data, "physic_objects")
    if physic_objects:
        load_physic_objects(physic_objects, image)
    
    event_path = get_element(image_data, "event")
    if image and event_path:
        image.event = load_event(event_path)
    if not layer or layer < 0:
        layer = 1
    elif layer > len(level.objects)-1:
        layer = len(level.objects)-1
    if image:
        level.objects[layer-1].append(image)
    global_object_id += 1
    return image
Пример #2
0
class Editor():
    def __init__(self):

        add_button('editor', 'LCTRL+e')
        self.editor_click = False

        self.editor = False
        self.text_size = 50

        self.mouse_clicked = (0, 0, 0)
        self.current_selected = None


        self.scale_clicked = (0, 0)  #red, enlarg

        self.save_clicked = False

        add_button('scale_y_down', ['DOWN'])
        add_button('scale_y_up', ['UP'])
        add_button('scale_x_down', ['LEFT'])
        add_button('scale_x_up', ['RIGHT'])

        add_button('angle_up',['a'])
        add_button('angle_down',['d'])

        add_button('save', ['LCTRL+s'])

        add_button('box',['LCTRL'])

        self.new_obj = None
        self.new_obj_pos = Vector2()


        self.obj_init_pos = Vector2()
        self.mouse_init_pos = Vector2()

        '''GUI'''
        self.gui_editor_mode = Text(Vector2(0, engine.get_screen_size().y-2*self.text_size), self.text_size, "data/font/pixel_arial.ttf", "Editor mode", relative=True)
        self.gui_current_selected = Text(Vector2(0, engine.get_screen_size().y-self.text_size), self.text_size, "data/font/pixel_arial.ttf", "",relative=True)
        self.gui_saved = Text(Vector2(0,0), self.text_size, "data/font/pixel_arial.ttf", "Saved",relative=True)

    def loop(self, screen,screen_pos):
        if not (self.editor_click or not get_button('editor')):
            self.editor = not self.editor
            self.lock = self.editor
            self.editor_click = True
            if self.editor:
                log("Editor mode activate")
            else:
                self.current_selected = None
        if not get_button('editor'):
            self.editor_click = False
        if self.editor:
            show_mouse()

        mouse_pos, pressed = get_mouse()
        if not self.editor:
            return

        self.gui_editor_mode.loop(screen,screen_pos)

        '''Save Level'''
        if self.save_clicked:
            self.gui_saved.loop(screen,screen_pos)
        if get_button('save') and not self.save_clicked:
            save_level(self)
            self.save_clicked = True
        elif not get_button('save'):
            self.save_clicked = False

        '''Left click,
        select a object and move it'''
        if not get_button('box'):
            self.new_obj = None
            if pressed[0] and not self.mouse_clicked[0]:
                '''Set current_selected'''
                self.current_selected = None
                for layer in self.objects:
                    for image in layer:
                        if not image.screen_relative and image.check_click(mouse_pos, self.screen_pos):
                            log("Current_object is: " + str(image))
                            self.current_selected = image
                            self.obj_init_pos = self.current_selected.pos
                            self.mouse_init_pos = mouse_pos + self.screen_pos
                self.mouse_clicked = (1, self.mouse_clicked[1], self.mouse_clicked[2])
            elif pressed[0] and self.mouse_clicked[0]:
                '''Move the current object'''
                if self.current_selected is not None:
                    self.current_selected.set_pos(self.obj_init_pos, mouse_pos + self.screen_pos - self.mouse_init_pos)
        else:
            """Create a static physic with CTRL-left mouse"""
            if pressed[0] and not self.mouse_clicked[0]:
                self.mouse_clicked = (1, self.mouse_clicked[1], self.mouse_clicked[2])
                self.new_obj = GameObject()
                self.new_obj.pos = mouse_pos + self.screen_pos
                self.objects[4].append(self.new_obj)
            elif self.mouse_clicked[0]:
                self.new_obj.size = mouse_pos + self.screen_pos - self.new_obj.pos
                if not pressed[0]:
                    """Create a static physics body"""
                    self.new_obj.body = add_static_object(self.new_obj, self.new_obj.pos+self.new_obj.size/2)
                    add_static_box(self.new_obj.body, Vector2(), self.new_obj.size/2, data=11)
                self.new_obj.update_rect()


        if not pressed[0] and self.mouse_clicked[0]:
            self.mouse_clicked = (0, self.mouse_clicked[1], self.mouse_clicked[2])

        if self.current_selected:
            self.gui_current_selected.change_text(str(self.current_selected.__class__)+" "+str(self.current_selected.id)
                                                  +" "+str(self.current_selected.angle))
            self.gui_current_selected.loop(screen, screen_pos)

        '''Size scale'''
        scale_x = get_button("scale_x_up")-get_button("scale_x_down")
        scale_y = get_button("scale_y_up")-get_button("scale_y_down")

        if self.current_selected:
            self.current_selected.scale(scale_x,scale_y)
        '''Angle'''
        angle = get_button("angle_up")-get_button("angle_down")
        if self.current_selected:
            self.current_selected.set_angle(self.current_selected.angle+angle)