EG/plugins/user/example_plugin/plugin.py
2025-12-12 16:16:15 +08:00

96 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
示例插件
演示如何创建一个简单的插件
"""
from plugins.plugin_manager import BasePlugin
class Plugin(BasePlugin):
"""
示例插件
演示如何创建一个简单的插件
"""
def __init__(self, plugin_manager, name):
super().__init__(plugin_manager, name)
self.config = {
"version": "1.0.0",
"author": "EG Team",
"description": "示例插件,演示插件基本功能"
}
def initialize(self) -> bool:
"""
初始化插件
"""
print(f"初始化示例插件: {self.name}")
return True
def enable(self) -> bool:
"""
启用插件
"""
if not super().enable():
return False
print(f"启用示例插件: {self.name}")
# 注册一个示例事件处理器
self.plugin_manager.world.accept("f10", self.on_f10_pressed)
# 在场景中添加一个示例文本
self.example_text = self.plugin_manager.world.render.attachNewNode("example_text")
self.example_text.setPos(0, 0, 5)
# 如果有GUI管理器添加一个示例按钮
if hasattr(self.plugin_manager.world, 'gui_manager') and self.plugin_manager.world.gui_manager:
self.example_button = self.plugin_manager.world.gui_manager.createGUIButton(
pos=(0.5, 0, 0.9),
text="示例插件按钮",
size=0.08
)
# 注册按钮点击事件
self.plugin_manager.world.accept("example_button_click", self.on_button_click)
return True
def disable(self) -> bool:
"""
禁用插件
"""
if not super().disable():
return False
print(f"禁用示例插件: {self.name}")
# 移除事件处理器
self.plugin_manager.world.ignore("f10")
# 清理示例文本
if hasattr(self, 'example_text'):
self.example_text.removeNode()
# 清理示例按钮
if hasattr(self, 'example_button'):
self.plugin_manager.world.gui_manager.deleteGUIElement(self.example_button)
self.plugin_manager.world.ignore("example_button_click")
return True
def finalize(self):
"""
清理插件资源
"""
print(f"清理示例插件资源: {self.name}")
def on_f10_pressed(self):
"""
F10按键事件处理器
"""
print("示例插件: F10按键被按下")
def on_button_click(self):
"""
按钮点击事件处理器
"""
print("示例插件: 按钮被点击")