我有一个我正在使用的脚本,在Pyglet中。在这个应用程序中,我想插入一个控制台,我从Pyglet的指南中作为例子“examples / text_input.py”。我修改了它并试图使它适应我的使用。结果如下:
import pyglet, pyperclip
from pyglet.gl import *
from pyglet import clock
from collections import OrderedDict
key = pyglet.window.key
class MainGame(pyglet.window.Window):
#VariableFrame
WDisplay, HDisplay = 1024, 576
Fullscreen = False
FPS = 60
Title = "test"
ConsoleVar = False
#VariableConsole
showFPS = False
showPOSITION = False
showENTITY = False
showINFO = False
showHELP = False
helpPAGE = 0
GAMEMODE = 0
GODMODE = False
def __init__(self, width=WDisplay, height=HDisplay, caption=Title, fullscreen=Fullscreen, *args, **kwargs):
super().__init__(width, height, caption, fullscreen, *args, **kwargs)
platform = pyglet.window.get_platform()
display = platform.get_default_display()
screen = display.get_default_screen()
self.infoScreen = (screen.width, screen.height)
self.xDisplay = int(screen.width / 2 - self.width / 2)
self.yDisplay = int(screen.height / 2 - self.height / 2)
self.set_location(self.xDisplay, self.yDisplay)
self._load()
def _load(self):
self.mapInfo = OrderedDict()
pyglet.resource.add_font('graphics/fonts/pkmndp.ttf')
pyglet.resource.add_font('graphics/fonts/pkmndpb.ttf')
self.consoleClass = Console(self, '')
self.text_cursor = self.get_system_mouse_cursor('text')
self.focus = None
self.set_focus(self.consoleClass)
def update(self, dt):
if self.ConsoleVar:
self.consoleClass.update()
def on_draw(self):
pyglet.gl.glClearColor(1, 1, 1, 1)
self.clear()
if self.ConsoleVar:
for sprite in self.mapInfo :
self.mapInfo[sprite].draw()
def on_mouse_press(self, x, y, button, modifiers):
if self.ConsoleVar:
if self.focus:
self.focus.caret.on_mouse_press(x, y, button, modifiers)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.ConsoleVar:
if self.focus:
self.focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
def on_text(self, text):
if self.ConsoleVar:
if self.focus:
self.focus.caret.on_text(text)
def on_text_motion(self, motion):
if self.ConsoleVar:
if self.focus:
self.focus.caret.on_text_motion(motion)
def on_text_motion_select(self, motion):
if self.ConsoleVar:
if self.focus:
self.focus.caret.on_text_motion_select(motion)
def on_key_press(self, symbol, modifiers):
if symbol == key.F1 and modifiers & key.MOD_CTRL:
if self.ConsoleVar:
self.ConsoleVar = False
self.focus.document.text = ""
else:
self.ConsoleVar = True
self.focus.document.text = "/"
self.focus.caret.position = 1
if symbol == key.F2 and modifiers & key.MOD_CTRL:
if self.ConsoleVar:
self.ConsoleVar = False
self.focus.document.text = ""
else:
self.ConsoleVar = True
self.focus.caret.position = 0
if symbol == key.ENTER:
if self.ConsoleVar:
self.ConsoleVar = False
self.consoleClass.Enter()
if modifiers & key.MOD_CTRL and symbol == key.V:
if self.ConsoleVar:
self.consoleClass.Copy_Paste("paste")
if modifiers & key.MOD_CTRL and symbol == key.C:
if self.ConsoleVar:
self.consoleClass.Copy_Paste("copy")
def set_focus(self, focus):
if self.focus:
self.focus.caret.visible = False
self.focus.caret.mark = self.focus.caret.position = 1
self.focus = focus
if self.focus:
self.focus.caret.visible = True
self.focus.caret.mark = 1
self.focus.caret.position = len(self.focus.document.text)
class Console(object):
ComandConsole = []
def __init__(self, game, text):
self.game = game
self.groups = self.game.mapInfo
self.image = pyglet.resource.image("graphics/other/alpha.png")
self.alpha = self.image.get_region(x=32, y=64, width=32, height=32)
self.alpha.width = self.game.WDisplay
self.sprite = pyglet.sprite.Sprite(self.alpha)
self.sprite.opacity = 128
self.sprite.x, self.sprite.y = 0, 10
self.groups["bg"] = self.sprite
self.document = pyglet.text.document.UnformattedDocument(text)
self.document.set_style(0, len(self.document.text), dict(font_name = "Power Clear", font_size = 15, color=(255,255, 255, 255)))
font = self.document.get_font()
height = font.ascent - font.descent
self.layout = pyglet.text.layout.IncrementalTextLayout(self.document, self.game.WDisplay, height, multiline=False)
self.caret = pyglet.text.caret.Caret(self.layout)
self.groups["text"] = self.layout
self.layout.x = 20
self.layout.y = (self.alpha.height / 2 + self.sprite.y) - (self.layout.height / 2)
pad = 2
def hit_test(self, x, y):
return (0 < x - self.layout.x < self.layout.width and
0 < y - self.layout.y < self.layout.height)
def Copy_Paste(self, function):
if function == "copy":
pyperclip.copy('Hello world!')
if function == "paste":
paste = pyperclip.paste()
self.document.text = self.document.text + paste
self.game.focus.caret.position = self.game.focus.caret.mark = len(self.document.text)
def Enter(self):
if len(self.document.text) == 0:
pass
else:
if self.document.text[0] == "/":
if self.document.text == "/ciao":
print("ciao")
else:
print("errore comando")
else:
print(self.document.text)
self.document.text = ""
def update(self):
pass
if __name__ == "__main__":
window = MainGame()
pyglet.clock.schedule_interval(window.update, 1/window.FPS)
pyglet.app.run()
现在,我希望能够通过按Shift +向左或向右突出显示控制台中的某些文本,然后按crtl + c,通过pyperclip复制所选文本。我该怎么办?
另一个问题,总是固有的。在Console类中,我想在控制台上方创建一个矩形。在其中,我想在几行(如聊天)上报告一些文字。问题,最重要的是,我不知道如何在文本到达矩形的极限时立即编写文本。
最后,现在使用crtl + F1(并添加符号“/”)或使用crtl + F2调用控制台。但是,我想用字母“U”和“T”激活它。问题是,我这样做,控制台出现时会按下字母来回忆它。那我怎么能避免呢?