首页 > 解决方案 > 文本选择和文本标题 Pyglet

问题描述

我在 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 复制选定的文本。我能怎么做?

另一个问题,总是与生俱来的。在控制台类中,我想在控制台上方创建一个矩形。在其中,我想报告多行的一些文本(如聊天)。最重要的问题是,一旦文本到达矩形的限制,我不知道如何编写文本。

最后,现在使用 crtl + F1(并添加符号“/”)或使用 crtl + F2 调用控制台。但是,我想用字母“U”和“T”来激活它。问题是我这样做了,控制台出现时按下字母来召回它。那么我该如何避免呢?

标签: python-3.xpyglet

解决方案


推荐阅读