class CreateActions:
    """Create and script entry actions extracted from main world."""

    def __init__(self, app):
        self.app = app

    def __getattr__(self, name):
        return getattr(self.app, name)

    def __setattr__(self, name, value):
        if name == "app" or name in self.__dict__ or hasattr(type(self), name):
            object.__setattr__(self, name, value)
        else:
            setattr(self.app, name, value)


    def _on_create_empty_object(self):
        """创建空对象"""
        try:
            result = self.createEmptyObject()
            if result:
                self.add_success_message("空对象创建成功")
            else:
                self.add_error_message("空对象创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建空对象失败: {str(e)}")
    

    def _on_create_cube(self):
        """创建立方体"""
        try:
            result = self.createCube()
            if result:
                self.add_success_message("立方体创建成功")
            else:
                self.add_error_message("立方体创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建立方体失败: {str(e)}")
    

    def _on_create_sphere(self):
        """创建球体"""
        try:
            result = self.createSphere()
            if result:
                self.add_success_message("球体创建成功")
            else:
                self.add_error_message("球体创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建球体失败: {str(e)}")
    

    def _on_create_cylinder(self):
        """创建圆柱体"""
        try:
            result = self.createCylinder()
            if result:
                self.add_success_message("圆柱体创建成功")
            else:
                self.add_error_message("圆柱体创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建圆柱体失败: {str(e)}")
    

    def _on_create_plane(self):
        """创建平面"""
        try:
            result = self.createPlane()
            if result:
                self.add_success_message("平面创建成功")
            else:
                self.add_error_message("平面创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建平面失败: {str(e)}")
    
    def _on_create_spot_light(self):
        """创建聚光灯"""
        self.show_spot_light_dialog = True
    

    def _on_create_point_light(self):
        """创建点光源"""
        self.show_point_light_dialog = True
    

    def _on_create_flat_terrain(self):
        """创建平面地形"""
        self.show_terrain_dialog = True
    

    def _on_create_heightmap_terrain(self):
        """从高度图创建地形"""
        self.show_heightmap_browser = True
    

    def _on_create_script(self):
        """创建脚本"""
        self.show_script_dialog = True
    

    def _on_load_script(self):
        """加载脚本文件"""
        self.show_script_browser = True
    

    def _on_reload_all_scripts(self):
        """重载所有脚本"""
        try:
            if hasattr(self, 'script_manager') and self.script_manager:
                self.script_manager.reloadAllScripts()
                self.add_success_message("所有脚本重载成功")
            else:
                self.add_error_message("脚本管理器未初始化")
        except Exception as e:
            self.add_error_message(f"重载脚本失败: {str(e)}")
    

    def _on_open_scripts_manager(self):
        """打开脚本管理器"""
        self.showScriptPanel = True
        self.add_info_message("脚本管理器已打开")
    

    def _on_create_2d_sample_panel(self):
        """创建2D示例面板"""
        try:
            result = self.create2DSamplePanel()
            if result:
                self.add_success_message("2D示例面板创建成功")
            else:
                self.add_error_message("2D示例面板创建失败")
        except Exception as e:
            self.add_error_message(f"创建2D示例面板失败: {str(e)}")
    

    def _on_create_3d_sample_panel(self):
        """创建3D实例面板"""
        try:
            result = self.create3DSamplePanel()
            if result:
                self.add_success_message("3D实例面板创建成功")
            else:
                self.add_error_message("3D实例面板创建失败")
        except Exception as e:
            self.add_error_message(f"创建3D实例面板失败: {str(e)}")
    

    def _on_create_web_panel(self):
        """创建Web面板"""
        try:
            result = self.editor_panels._on_create_web_panel()
            if result:
                self.add_success_message("Web面板创建成功")
            else:
                self.add_error_message("Web面板创建失败")
            return result
        except Exception as e:
            self.add_error_message(f"创建Web面板失败: {str(e)}")
            return None
    
    # ==================== 3D对象和GUI创建方法 ====================
