2

我试图弄清楚如何让文本在 Pyglet 的 ScrollableTextLayout 中向上滚动,而不是向下滚动。为了清楚起见,这里有一个快速快照来说明我所说的“向上”是什么意思。(以防万一)

它目前的行为方式

我希望它如何表现:

在此处输入图像描述

根据文档,这种行为可以通过 view_y 属性来实现,但我尝试了各种不同的值,但都没有明显的变化。

编码:

import pyglet

class LoadDialog(pyglet.sprite.Sprite):
    def __init__(self):
        self.lbatch = pyglet.graphics.Batch()

        self.loading_window = pyglet.image.load('..\\resources\\loading_base.png')
        super(LoadDialog, self).__init__(self.loading_window, batch=self.lbatch)


        self.doc = pyglet.text.decode_text('Hello world!'.ljust(40))
        self.doc.set_style(0,12, dict(font_name='Arial', font_size=12,
                                    color=(0,0,0,255)))

        self.layout = pyglet.text.layout.ScrollableTextLayout(self.doc, 
                                            width=self.load_animation.width, 
                                            height=100, multiline=True, batch=self.lbatch)
        self.layout.x = 220
        self.layout.y = 160
        self.layout.view_y = -80

    def update(self, dx):
        self.doc.insert_text(0, "New line".ljust(40))






sprite = LoadDialog()
window = pyglet.window.Window(width=640, height=480)

pyglet.gl.glClearColor(1, 1, 1, 1)

@window.event
def on_draw():
    window.clear()
    sprite.lbatch.draw()
    sprite.layout.draw()

@window.event
def update(dx):
    sprite.update(dx)

pyglet.clock.schedule_interval(update, 1.0)
pyglet.app.run()

我已经尝试了大量的值layout.view_y,从-1到荒谬的值-3000,或者500只是为了看看是否有变化。但它总是给出如第一张图片所示的确切行为。

我究竟做错了什么?

4

1 回答 1

1

首先,您的示例取决于图像文件及其宽度(未提供),这会使测试稍微复杂。

其次,您正在通过调用pyglet.text.decode_text创建一个UnformattedDocument ,然后在此行的位置 0(开始)处将文本显式地重复插入UnformattedDocument :

def update(self, dx):
    self.doc.insert_text(0, "New line".ljust(40))

如果您希望文本出现在末尾,正如您在图形中所暗示的那样,请将其插入到末尾!

def update(self, dx):
    # Fix the implied bug
    self.doc.insert_text(-1, "New line".ljust(40))

第三,让我们回答您实际提出的问题。如果您阅读属性ScrollableTextLayout.view_y的 API 文档,您会发现...

超出范围 [height - content_height, 0] 的值会自动在范围内裁剪。

...因此当 content_height 为 0 时将 view_y 设置为 -80,会导致 view_y 被剪裁为 0,然后您再也不会尝试设置 view_y。滚动问题的解决方案是在每次内容高度发生变化时设置 view_y 。对于一个简单的修复,您可以简单地设置 view_y 以便您的内容的底部将始终向上滚动到您的框架的底部:

def update(self, dx):
    # Fix the implied bug
    self.doc.insert_text(-1, "New line".ljust(40))
    # The answer to the stated question
    self.layout.view_y = -self.layout.content_height
于 2012-09-24T01:25:40.347 回答