How to make Battlelog width adjust to contents width?

So I would like the Battlelog window width (or rather, the background sprite) to fit the width of the text.

This is what it looks like now:

MEGuHAF.png

(width = 1024px)



And this is what I'd like to have:

t778OFH.png


These are what I tried (but it didn't work):

Window_BattleLog.prototype.windowWidth = function() {
1. return Graphics.boxWidth;
2. return this.contentsHeight();
3. this.width;
};

Maybe someone did this once and could help me out?
this.contents.measureTextWidth()
But you have to be sure to set the default window width at Graphics.boxWidth as the text width begins at 0. Also, you may need to resize the window to full screen everytime before drawing the text.

I've moved this thread to Learning Javascript. Thank you.

Also Window_BattleLog accepts multiple lines of text (up to 10 by default) so you may need to iterate over all lines to find the maximum text width. Something like this may work (untested):
Code:
Window_BattleLog.prototype.windowWidth = function() {
  return this._lines.reduce(function(a, c) {
    var w = this.contents.measureTextWidth(c);
    return w > a ? w : a;
  }, 0);
};
Might also need to refresh the window width every time a new line is pushed, not sure how that works. Pretty sure it should work windowed or full-screen, though. [Edit: oh, misunderstood that part, whoops. 😴]
this.contents.measureTextWidth()
But you have to be sure to set the default window width at Graphics.boxWidth as the text width begins at 0. Also, you may need to resize the window to full screen everytime before drawing the text.
Also Window_BattleLog accepts multiple lines of text (up to 10 by default) so you may need to iterate over all lines to find the maximum text width. Something like this may work (untested):
Code:
Window_BattleLog.prototype.windowWidth = function() {
  return this._lines.reduce(function(a, c) {
    var w = this.contents.measureTextWidth(c);
    return w > a ? w : a;
  }, 0);
};
Might also need to refresh the window width every time a new line is pushed, not sure how that works. Pretty sure it should work windowed or full-screen, though. 😅

Thank you, but it seems I put it in the wrong spot because now I get an error...
I need to put this into
Window_BattleLog.prototype.windowWidth
right?
But you have to be sure to set the default window width at Graphics.boxWidth
The battle log window, just likr any other window, by default uses windowWidth in its initialize function. So no, it shouldn't be inside windowWidth. You'll need to expand this or define it in a different function and use that function as a width parameter for resize.
Code:
Window_BattleLog.prototype.resizeToText = function (resizeToText) {
if (resizeToText) {
this.width = this.contents.measureTextWidth() + this.padding * 2;
this.contents.resize (this.contents.measureTextWidth(), this.contentsHeight());
}else {
this.width = Graphics.boxWidth;
this.contents.resize(Graphics.boxWidth, this.contentsHeight());
}
}
Then you'll need to find the function that draws text. Before you draw the texts, you call this.resizeToText(0); and after you draw the texts, you use this.resizeToText(1);
The battle log window, just likr any other window, by default uses windowWidth in its initialize function. So no, it shouldn't be inside windowWidth. You'll need to expand this or define it in a different function and use that function as a width parameter for resize.
Code:
Window_Battlelog.prototype.resizeToText = function () {
this.width = this.contents.measureTextWidth() + this.padding * 2;
this.contents.resize (this.contents.measureTextWidth(), this.contentsHeight());
}
If you wait for 15 minutes, I might finish it when I return home.
AAa thank you so much, that would be amazing
Done. Hopefully it works.
The battle log window, just likr any other window, by default uses windowWidth in its initialize function. So no, it shouldn't be inside windowWidth. You'll need to expand this or define it in a different function and use that function as a width parameter for resize.
Code:
Window_BattleLog.prototype.resizeToText = function (resizeToText) {
if (resizeToText) {
this.width = this.contents.measureTextWidth() + this.padding * 2;
this.contents.resize (this.contents.measureTextWidth(), this.contentsHeight());
}else {
this.width = Graphics.boxWidth;
this.contents.resize(Graphics.boxWidth, this.contentsHeight());
}
}
Then you'll need to find the function that draws text. Before you draw the texts, you call this.resizeToText(0); and after you draw the texts, you use this.resizeToText(1);

Thank you!! I tested it and it does cut off the width, but for some reason it's always the same width... (see image attached)

I tried putting this.resizeToText(0); into these functions (one at a time):

Code:
Window_BattleLog.prototype.drawLineText = function(index) {
    var rect = this.itemRectForText(index);
    this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
    this.resizeToText(0);
    this.drawTextEx(this._lines[index], rect.x, rect.y, rect.width);
    this.resizeToText(1);
};

Window_BattleLog.prototype.drawBackground = function() {
    this.resizeToText(0);
    var rect = this.backRect();
    var color = this.backColor();
    this._backBitmap.clear();
    this._backBitmap.paintOpacity = this.backPaintOpacity();
    this._backBitmap.fillRect(rect.x, rect.y, rect.width, rect.height, color);
    this._backBitmap.paintOpacity = 255;
    this.resizeToText(1);
};

Window_BattleLog.prototype.createBackSprite = function() {
    this.resizeToText(0);
    this._backSprite = new Sprite();
    this._backSprite.bitmap = this._backBitmap;
    this._backSprite.y = this.y;
    this.addChildToBack(this._backSprite);
    this.resizeToText(1);
};


Now, when I put it inside here, no text shows anymore:

Code:
Window_BattleLog.prototype.refresh = function() {
    this.drawBackground();
    this.contents.clear();
    for (var i = 0; i < this._lines.length; i++) {
        this.resizeToText(0);
        this.drawLineText(i);
        this.resizeToText(1);
    }
};

Hmmmm where did I do it wrong?
Code:
Window_BattleLog.prototype.resizeToText = function () {
    if (!this._lines.length) return;
    this._maxLineWidth = 0;
    for (var i in this._lines) {
        var width = this.contents.measureTextWidth(this._lines[i]);
        if (width > this._maxLineWidth) this._maxLineWidth = width;
    }
    this.width = this._maxLineWidth + this.padding * 2 + 10;
    this.contents.resize (this._maxLineWidth + 10, this.contentsHeight());
}

Window_BattleLog.prototype.addText = function(text) {
    this._lines.push(text);
    this.resizeToText();
    this.refresh();
    this.wait();
};

Alright, took me a while, but got it working. Replace the custom code with this.
Code:
Window_BattleLog.prototype.resizeToText = function () {
    if (!this._lines.length) return;
    this._maxLineWidth = 0;
    for (var i in this._lines) {
        var width = this.contents.measureTextWidth(this._lines[i]);
        if (width > this._maxLineWidth) this._maxLineWidth = width;
    }
    this.width = this._maxLineWidth + this.padding * 2 + 10;
    this.contents.resize (this._maxLineWidth + 10, this.contentsHeight());
}

Window_BattleLog.prototype.addText = function(text) {
    this._lines.push(text);
    this.resizeToText();
    this.refresh();
    this.wait();
};

Alright, took me a while, but got it working. Replace the custom code with this.


Now I tried implementing this, and suddenly this happened!?

giphy.gif


(thanks a million times, it works perfectly now!!!)
Welcome.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-battlelog-width-adjust-to-contents-width.112930/