rexrainbow / phaser3-rex-notes

Notes of phaser3 engine
MIT License
1.18k stars 260 forks source link

rtl not working properly in bbcode text #356

Closed mizunokazumi closed 1 year ago

mizunokazumi commented 1 year ago

The documentation for bbcodetext specifies the rtl flag, but when it is turned on, the text is severely truncated. This can be seen in the example from the documentation (if you add rtl: true): https://codepen.io/rexrainbow/pen/OZbOyg?editors=0010

Снимок экрана 2023-05-29 в 22 34 17
rexrainbow commented 1 year ago

Here is a test case of using rtl. Adding padding (padding: {left, right, top, bottom}) cam fix this truncated issue.

mizunokazumi commented 1 year ago

Sorry, wrong example. In fact, the problem arose with the rtl change after initialization. text.style.rtl = true I already figured out that I need to call text.initRTL()

text.style.rtl = true
text.initRTL()

but after that I can't disable RTL (there is no reverse text.initRTL() method) - this is sad. In a project that uses localization switching, it is difficult to switch back to ltr (en -> ar -> en)

https://codepen.io/Mizuno-Kazumi/pen/oNaRJMr?editors=001

Снимок экрана 2023-05-30 в 18 43 57
mizunokazumi commented 1 year ago

Maybe instead of initRTL something like

    enableRTL() {
        if (this.style.rtl) return

        this.style.rtl = true
        this.canvas.dir = 'rtl';
        this.context.direction = 'rtl';

        Object.values(this.imageManager.images).forEach((image) => {
            image.originX = 1 - image.originX
        })

        this.canvas.style.display = 'none';
        AddToDOM(this.canvas, this.scene.sys.canvas);

        //  And finally we set the x origin
        this.originX = 1 - this.originX;
    }

    disableRTL() {
        if (!this.style.rtl) return

        this.style.rtl = false
        this.canvas.dir = 'ltr';
        this.context.direction = 'ltr';

        Object.values(this.imageManager.images).forEach((image) => {
            image.originX = 1 - image.originX
        })

        if (this.canvas.parentElement == this.scene.sys.canvas) {
            this.scene.sys.canvas.removeChild(this.canvas)
        }

        //  And finally we set the x origin
        this.originX = 1 - this.originX;
    }
rexrainbow commented 1 year ago

Built-in text gameobject and BBCodeText do not support changing rtl after creating it.

mizunokazumi commented 1 year ago

For embedded gameobject text text.style.rtl = true works quite well For bbcodetext, the methods from my previous comment also work quite well — So I will use them

Thank you for your help and quick replies

rexrainbow commented 1 year ago

Ah, I see, it is feature request about setting rtl during runtime, not a bug reporting of rendering in rtl mode.

rexrainbow commented 1 year ago

Add setRTL method to change rtl setting during runtime, invoke setRTL method before setting new content. Demo, any click to toggle rtl of last text game object.

mizunokazumi commented 1 year ago

Thank you!