Center Windows XP Edition

Center Windows XP Edition

By Kyonides
​

Difficulty Level: Intermediate

Introduction


The sole purpose of this tutorial is to show you how easily you can center any kind of windows at will.
Even so a few basic features will be reviewed quickly to remind you of some important and unavoidable aspects of creating or modifying anything on a game scene.

Planned Stages

1 - Introductory Stage

  • Display Windows
  • Unfreeze / Freeze Pictures
  • Create a Loop
  • Update Method
2 - Window Creation
  • Basic Window's Elements
  • Passing Parameters
  • Creating Bitmaps & Strings
  • Drawing Bitmaps & Strings
3 - Alter Windows
  • Modularity & Encapsulation
  • Relocate Windows
  • Improve Modularity by Creating Reusable Code
  • Center Windows

Introductory Stage
Optional Depending on Your End Goal​

Note: You may not need to go through this stage if you're just planning to modify a default script.

Step 1 - To properly Display Windows we need a scene.

Spoiler
The engine has plenty of them but a brand new one will let you concentrate on our new windows only.

Here's the barebones of our new scene:

Ruby:
class Scene_Test
end

Following the ancient RM class naming conventions, our scene is called Scene_Test.
class lets us create a brand new object that can be copied multiple times.

Why is this important?

Well, without that we could be forced to draw the last state of that very same scene only instead of getting a fresh scene on demand.
You wouldn't like to only open the inventory or the equip menu every single time you hit the Cancel button right?
You want some options to show up instead. Then a class is what you need here.

Now that we got one, what do we do next?

Step 2 - Unfreeze / Freeze all pictures on screen.

Spoiler
Ruby:
class Scene_Test
  def main
    Graphics.transition
    # some code here
    Graphics.freeze
  end
end

def means we are defining a method that will belong to our custom scene class from now on.

There we defined the main method. This is a method that all scenes need in order to be able to keep playing our game. The lack of this method would quickly throw an error in our faces here.

We need to call the transition and freeze methods defined in the Graphics module to be able to create a seamless transition between the previous scene, our scene and the next one. This is mandatory.

Usually, this means leaving the map, opening our scene, and then going back to the current map.

Step 3 - Create a loop for this new scene.

Spoiler
Ruby:
class Scene_Test
  def main
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
  end
end

Why do we create a loop?
That's because we need to stay in our current scene. Otherwise, it'd go back to the map. It's highly likely that you'd just notice that the map events stopped moving briefly and that would be all.

To keep our scene running smoothly, we need to update 2 things:
  1. Graphics
  2. Input
If we don't refresh our graphics, the maker will soon tell us that it got stuck.
Not updating the input function would render our new scene completely useless UNLESS we never intended to make it interactive.

Note: Usually, transitional scenes don't need any input at all. Instead they'd depend on a timer.

We also need to break that loop or will never be able to return to our map or any other scene you can think off. self stands for the current scene we're working on.

Note: So far we can't see anything on screen... except for an error message telling us that there's no update method.

Step 4- Adding an update method.

Spoiler: Tutorial 01 Code
Ruby:
class Scene_Test
  def main
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

Spoiler
Now that the update method does exist, we can leave our empty scene and return to the map.

Input.trigger? allows us to ask if the player has pressed a button, the Cancel button in this case, and released it. That's triggering a button. In all RGSS-based engines those buttons are represented by CONSTANTS like A, B, C, SHIFT and others. All of them are part of the Input module. The reference :: operator makes the connection for us.

The $game_system.se_play($data_system.cancel_se) method call is the long way RMXP relies upon to make any SE play in game. In this particular case it plays the Cancel SE.

$game_system is our running copy of the Game_System class.
se_play is one of its methods. It does play SE.
$data_system is a copy of the System tab's contents already loaded into memory.
cancel_se stands for the Cancel SE variable we once picked in the System tab.

If we had never added such a if statement, our condition, we wouldn't be able to ever get out of our test scene. 😮
You don't want to shut down your game by brute force, do you? 😛

return stands for returning to the method that originally called it. In this case it's the loop found inside the main method.

Note: Keep in mind that right now we can only watch a big black nothing on screen. :awink:

In my next chapter I'll start dealing with defining the actual windows.
2 - Window Creation
  • Basic Window's Elements
  • Passing Parameters
  • Creating Bitmaps & Strings
  • Drawing Bitmaps & Strings
Window Creation​

We can't center something we have never created so now we'll be dealing with how to define and setup 2 of the most common windows, namely the Window_Base and its child class Window_Command.

We shouldn't create any simple Window for it'd lack lots of basic features that have been implemented not in C or C++ like the Window class itself but in Ruby or RGSS technically speaking.

Because it was defined in another programming language, its contents are not freely available to us. This is why the help guide only mentions its methods and properties and what they're supposed to do. We can use them at any time. We just need to know why we need any of them and when we're gonna use them.

Thus we go for the second most basic thing available and that is the...

Step 1 - A Basic Window's Elements

Spoiler
View the Window_Base class as our basic window maker code.
All of the windows we can find in RMXP are based on Window_Base itself.

How does that happen?

By inheriting from this Window_Base class.

Note: If you still ignore what inheriting from another class means, let me tell you that it's all about calling this other class your new class's parent. So you gotta have a parent class aka superclass before you can define a child class.

This is how the Window_Base defines its parent class.

Ruby:
class Window_Base < Window
  # lots of code inbetween
end

The benefits of doing this are quite relevant for us.
  1. You inherit all of the superclass's methods at once.
  2. You can add methods to the child class without altering the superclass.
  3. The parent class is still able to be used as a base for other custom classes we might create later on, just like it happened in RMXP.
In Ruby and its derivative RGSS1, we have to initialize classes if we want to create initial values there that will be used later on. Sometimes we define them inside a very unique method called initialize, Ruby's constructor.

There are times when we need to send them to that class as arguments. This happens whenever we see a method definition like this one:

Ruby:
def method_name(argument1, argument2)

Some people omit the parentheses, but the default scripts always include them. At the end it's quite helpful, even if it means you gotta make a little extra effort to type them while coding.

Once the parenthesis closes the list, you can now for certain how many parameters it will take.

Step 2 - Passing Parameters or Arguments

Spoiler
These are the values that a custom method needs to be able to perform any calculation or positioning, etc.

They can be either mandatory or optional. The latter are following by an = equal sign used to assign a default value to that parameter. If a script doesn't pass that optional value, the default one will be used instead.

Optional arguments should always be listed at the end of the list.
You can't simply intercalate a mandatory parameter. It won't work so never forget this requirement.

Ruby:
def method_name(arg1, arg2, arg3=nil)

There argument #3 might be omitted and it will be equal to nil, the non existing value.
nil can also work as a false boolean, the falsy value. And true is a truthy value just as expected.

Basic Window's Parameters

There are four from a scripter's standpoint, and they are:
  1. X Coordinate
  2. Y Coordinate
  3. Width in pixels
  4. Height in pixels
Never forget about that because any child class that's close enough to its parent class, namely Window_Base, will have to pass those 4 parameters. This can change radically depending on what kind of special child class we are creating.

Let's now focus on the most common case, passing all those 4 values without complaining.

Ruby:
class Window_Base
  def initialize(x, y, width, height)
    super()
    @windowskin_name = $game_system.windowskin_name
    self.windowskin = RPG::Cache.windowskin(@windowskin_name)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.z = 100
  end
# lots of lines of code
end

Above we can find that Window_Base receives those 4 parameters and does NOT pass them to its superclass directly. Instead it calls each property separately. One by one.

self means the current class Window_Base. Here it's calling several of its methods that actually contains non Ruby code like Window's x, y, width and height methods. Since Window class is closed source or not visible for us, we only count on a single way to properly access any of its methods.

Ruby:
self.method_name
self.method_name = 1

The former method simply calls it, while the latter changes it to the new value we've entered there.
Not all of those methods are properties, meaning that they possess both abilities.

A getter is the first method and only cares about delivering a value or process some GUI stuff, etc.
A setter uses the = equal sign to change its value. It might also include some extra internal processing but it's not mandatory.

What this reminds us is that Window_Base got 4 parameters and started calling Window's properties to set their initial values. self.z also gets a new value of its own, 100.

The Z coordinate is important. We don't always depend on it, but it can change how things end up being displayed on screen. Our first windows will include one example of how NOT to ignore the current or final location of any windows visible on our screen.

Above you can also notice that there is another method called self.windowskin and it takes care of the picture we use as the graphical part of our windows. It's important for several reasons like preventing the game from throwing a fatal error in your face if the engine can't find it. Even so, we'll only get into a few of its curious details here.

We already finished defining (or reading the default script's) initialize method and now we need to draw something like some text on screen.

Step 3 - Creating Bitmaps & Strings

Spoiler
Before we can print actual texts on this window, we need to create another method typically called refresh in the default scripts. Whenever we use any of Window_Base's child classes, we'll have to do this:

Ruby:
class Window_Test < Window_Base
  # lots of code here
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
  end
end

There we were introduced to another property called self.contents that's in charge of holding the Bitmap itself. Without bitmaps, our windows are worthless by default.

They are the internal representation of any image we can see on a computer screen. The Bitmap class takes care of its technical details for us, yet, we need to pass it 2 arguments, a width and a height. There we can call them width or self.width and the engine will look for either a local variable called width or Window's width property on our behalf.

Now we got a window and its corresponding bitmap. Why can't we see any text on it?

The answer is simple. There's not a single call to draw any single string of text. 😛
Nonetheless, we can easily change that now by adding extra lines of code below.

Let's create a window class called Window_Test.

Ruby:
class Window_Test < Window_Base
  # lots of code here
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

It happens that the Bitmap class has a method known as draw_text that allows it to draw our strings on screen. It's a versatile method like pretty much no other method in the default scripts.

It's so special that it has 2 separate sets of parameters we can pass it there.
As you have noticed by now, we used the typical way to call it, by sending it 5 mandatory arguments. Just like any Window_Base class, it needs x, y, width and height. Add the last one, namely our "Text" and it's done.

Actually, there's just another parameter called align (alignment) and it's optional.
Did you think that I spent some good time talking about optional parameters for nothing?
Now you know why I had to do it here! 😛

That align argument can be set to 0, its default value that stands for left alignment, 1 for centered text, and 2 for right alignment.

There's another way to draw text by passing a Rect object, RGSS1 representation of a rectangle, instead of the whole enchilada over there. Then as a second parameter you'd enter the "Text" and optionally align's value as a final parameter. We won't be using Rect for this purpose here.

Whenever we call that refresh method internally or externally (from our custom scene script), it will destroy the previous contents bitmap if found. Then it'll create a new contents bitmap and finally draw our text on screen.

Step 4 - Drawing Bitmaps & Strings

To make this happen and get some real screenshot to show off here, let's focus now on the scene we were creating a while ago. Just don't forget to add our Window_Test code as well like I did here:

Spoiler: Tutorial 02 Code
Ruby:
class Window_Test < Window_Base
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Scene_Test
  def main
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    @test_window.dispose
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

Spoiler
In the code above, you can find a @variable called @test_window that will hold our Window_Test object for as long as our scene is being executed.

There I placed it right below def main and before I called Graphics.transition and the rest of that method's contents. At the same time, I called that window again after Graphics.freeze is being executed. Those are the right places, the typical ones, to define our new test window and to destroy it once we no longer need it.

The number of windows doesn't matter as long as you create and destroy them in the right places.

All windows have the dispose method. Always use it before leaving the scene.
And I mean it when I say use it ALWAYS.

You don't wanna deal with floating windows that don't disappear after leaving the scene and returning to the current map. :S

Now let's create a new event on the first map of our demo.
The event should call a script like this:

Ruby:
$scene = Scene_Test.new

And save the event and later the whole project as well.
It's about time we playtest our game!

Spoiler: Screenshot 01
center_windows_xp01.jpg

Isn't it the most beautiful thing you've ever seen in your life? XD

OK, in my next chapter I'll let you know how you can create windows with different dimensions and finally teach you how to center them either horizontally or vertically.
3 - Alter Windows
  • Modularity & Encapsulation
  • Relocate Windows
  • Improve Modularity by Creating Reusable Code
  • Center Windows

Alter Windows​

Step 1 - Modularity & Encapsulation

Before we relocate or change the dimensions of our test window, we better review another topic, Modularity.

If our code is very modular, we can easily find a specific piece of code or alter it at will without affecting the rest of the script negatively. A way to accomplish this is by relying on encapsulation to deal with it.

In this tutorial this will mean that we better create a few extra methods to be able to relocate our windows and their respective destruction before finally exiting our custom scene.

Translation: We will define the create_windows and the terminate methods now. Then we move the window we created to the former and its disposal to the latter.

Spoiler: Tutorial 03-1 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
  end

  def terminate
    @test_window.dispose
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

The code above has no real impact in our scene. If you take a screenshot right now, it will look identical to the one I posted on my previous post.

Now that we can count on those 2 new methods, we can start adding some extra windows there! 😀

Let's define a new window class called Window_List where we'll display a list of 4 things we can find at home. Being a window, its parent class has to be Window_Base just as it was the case with Window_Test. It will also include its own refresh method.

Spoiler: Tutorial 03-2 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Window_List < Window_Base
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0,      0, width - 32, 32, "Scissors")
    self.contents.draw_text(0, 32 * 1, width - 32, 32, "Chair")
    self.contents.draw_text(0, 32 * 2, width - 32, 32, "Pan")
    self.contents.draw_text(0, 32 * 3, width - 32, 32, "Microwave")
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    @list_window = Window_List.new(0, 0, 160, 32 * 5)
    @list_window.refresh
  end

  def terminate
    @list_window.dispose
    @test_window.dispose
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

I also declared a new @instance variable called @list_window, while still providing it with all 4 parameters, and made sure it'd get properly destroyed at the very end. And yes, I also called the refresh method to allow it to print our list on screen.

Spoiler: Screenshot 02
center_windows_xp02.jpg

We managed to display our second window on screen, too! 😀
But there's a problem with it. :S
We can't read the first line because it gets mixed with our test window's text! 😮
Don't worry about this! There's an easy way to fix it! 😀

Step 2 - Relocate Windows

There are 2 ways to do it:
  • We can either change 1 of the 4 parameters we all know very well.
  • OR we call that coordinate directly to assign it a new value.
In this case we will opt for the first option here.
Let's modify our code now to lower our list window 64 pixels only. This means we gotta alter its Y coordinate there.
Why 64 pixels? Because that's the height of the test window!
Again there are 2 ways to do it, and we'll do it manually here for the time being.

Spoiler: Tutorial 03-3 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Window_List < Window_Base
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0,      0, width - 32, 32, "Scissors")
    self.contents.draw_text(0, 32 * 1, width - 32, 32, "Chair")
    self.contents.draw_text(0, 32 * 2, width - 32, 32, "Pan")
    self.contents.draw_text(0, 32 * 3, width - 32, 32, "Microwave")
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    @list_window = Window_List.new(0, 64, 160, 32 * 5)
    @list_window.refresh
  end

  def terminate
    @list_window.dispose
    @test_window.dispose
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

Spoiler: Screenshot 03
center_windows_xp03.jpg

What a big improvement! 😀
Now we can clearly read the contents of both windows at any moment.

Nevertheless, we should notice something important here: it's quite inconvenient to enter the list right in the list window's refresh methods.

Why you ask? Because we can no longer use it to display anything else.

This means that we better alter its initialize method, where we can predefine our custom stuff, now that our windows are working fine.

Step 3 - Improve Modularity by Creating Reusable Code

At this point we add all the required arguments to our initialize method.
How many do we need there? Just 5 parameters.
We also need to call super to let Window_Base get the 4 basic ones.

Once we managed to do that, we still need to define a new variable called @Data to store the 5th parameter, our short list.

Since we can now count on initialize to take care of it, we better call the refresh method there as well. This will make it possible for us to display the contents right away.

Spoiler: Modification of Window_List
Ruby:
class Window_List < Window_Base
  def initialize(x, y, w, h, list)
    super(x, y, w, h)
    @data = list
    refresh
  end

  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    @item_max = @data.size
    @item_max.times do |n|
      self.contents.draw_text(0, n * 32, width - 32, 32, @data[n])
    end
  end
end

Spoiler: Long Explanation
Let's stop here for a moment to analyze the second part of this code.
Inside the refresh method we can find that we no longer print all 4 lines manually.

There's some @item_max variable getting a new value called @data.size that represents the number of elements we might have added to our custom list, namely 4 texts.

Then it will call @item_max's items method that's available for any variable holding a number.
What it does is very simple: it will throw numbers starting at 0 till it gets too close to the last one, which would be 4 in this example.
Note: It'll never reach the limit or number 4 here, and that's fine. We don't need it at all.

The times method is a numeric loop, meaning it will repeat itself until it reaches number 3.
Inside this new loop we've placed the old self.contents.draw_text call.
But there's a catch! 😮

The y coordinate has been replaced by a short formula: n * 32.
Why is it 32 pixels tall? Because that's the normal line height for RMXP.

On the same line we find the @Data list, an Array object, there trying to access just 1 of its current elements, especifically number n.

What's n here?

Well, it will follow this pattern every single time it runs: 0, 1, 2, and finally 3.
This my friends, is the way we can easily print multiple lines without worrying about typing every single line manually! 😀

The final step of this section would be adding this new code to our main code that we've been expanding from the very beginning of this tutorial.
Please notice that we defined a new list Array there:

Ruby:
list = ["Scissors", "Chair", "Pan", "Microwave"]

This makes it easy for us to come back later and add or remove elements at will.

By the way, we also removed the external call to @list_window's refresh method. We no longer needed it so why should we keep it, right?

Spoiler: Tutorial 03-4 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Window_List < Window_Base
  def initialize(x, y, w, h, list)
    super(x, y, w, h)
    @data = list
    refresh
  end

  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    @item_max = @data.size
    @item_max.times do |n|
      self.contents.draw_text(0, n * 32, width - 32, 32, @data[n])
    end
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    list = ["Scissors", "Chair", "Pan", "Microwave"]
    @list_window = Window_List.new(0, 64, 160, 32 * 5, list)
  end

  def terminate
    @list_window.dispose
    @test_window.dispose
  end

  def update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

Aren't we forgetting something important here? :asad:
Right! 😅 We haven't centered our windows! 😮

Step 4 - Center Windows

To make it a little bit more challenging than in our previous steps, let's define a Window_Command object now! 😀

This kind of default window class has 2 basic demands: it needs a width and a list of options.
We gotta respect that order no matter what we think about it! 😛
And yes, it needs to be activated to make it work as our brand new menu. This is why we call its active method and set it to true to make it happen.

Here are our list and the creation of our Window_Command object.

Ruby:
options = ["It's fine", "It sucks!"]
@command_window = Window_Command.new(160, options)
@command_window.active = true

We can't make the same mistake as the one that happened before with the list window. Thus, we will move this new @command_window to the center of the game screen.

How do we do that!?

Spoiler: Long Explanation
By using a simple formula that heavily depends on 2 very specific widths, the game screen's width and the new window's.

The former is 640 px wide while the new one's just 160 px. We need to divide the result of this subtraction by 2 to make sure that we'll keep 1 half of the empty space to the lefthand side and another half to the righthand side.

And please don't forget to lower that window just 64 px to let us clearly read its contents from the very beginning. :awink:

Ruby:
@command_window.x = (640 - 160) / 2
@command_window.y = 64

OK, we've managed to center our window so nothing will ever stop us from playtesting our project now, right?

Not so fast, my dear friends!

I didn't tell you this before, but selectable windows or menus as we'd normally call it in real life have another feature that normal windows have never dreamed of. It's call the update method!

Nope, I'm not kidding you here. 😛

That old method that we met when we wanted to draw our black scene has made a comeback now. Sort of.

The thing here is that scenes are not the only ones that own an update method. Selectable windows also need one. And it's mandatory to call that method or else nothing will ever work as intended. :LZSlol:

Fine. Where do we call it then?

That's a very easy question indeed! We simply go to our custom scene's update method and add it there manually, right below def update.

Ruby:
  def update
    @command_window.update
    if Input.trigger?(Input::B)
    # several lines of code here
  end
end

At this point we just need to add our new code to the main one as usual.
Here's how it'd look like:

Spoiler: Tutorial 03-5 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Window_List < Window_Base
  def initialize(x, y, w, h, list)
    super(x, y, w, h)
    @data = list
    refresh
  end

  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    @item_max = @data.size
    @item_max.times do |n|
      self.contents.draw_text(0, n * 32, width - 32, 32, @data[n])
    end
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    list = ["Scissors", "Chair", "Pan", "Microwave"]
    @list_window = Window_List.new(0, 64, 160, 32 * 5, list)
    options = ["It's fine", "It sucks!"]
    @command_window = Window_Command.new(160, options)
    @command_window.active = true
    @command_window.x = (640 - 160) / 2
    @command_window.y = 64
  end

  def terminate
    @command_window.dispose
    @list_window.dispose
    @test_window.dispose
  end

  def update
    @command_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

And this is how it looks like after all these changes have been applied to our code.

Spoiler: Screenshot 04
center_windows_xp04.jpg

Very well. You can't complain about it anymore.
Now you do know how to center windows as promised!

What now? 😅
You still wanna know how to center our windows vertically!? 😮
Fine. 😛 Let's get the job done then.

Go back to line 50 of our beloved code and replace 64 with the following:

Ruby:
    @command_window.y = (480 - 32 * 3) / 2

The explanation is excessively simple. Our game screen is 480 px tall and we just need to subtract the 2 lines of text we printed on that window PLUS 32 px that cover the area of both its top and bottom borders. Finally, we divide the result by 2 and that's it! 😀

After saving our code, it should look just like the one below:

Spoiler: Tutorial 03-6 Code
Ruby:
class Window_Test < Window_Base
  # The rest of the code can be found inside the Window_Base class.
  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.draw_text(0, 0, width - 32, 32, "Hello world!")
  end
end

class Window_List < Window_Base
  def initialize(x, y, w, h, list)
    super(x, y, w, h)
    @data = list
    refresh
  end

  def refresh
    self.contents.dispose if self.contents != nil
    self.contents = Bitmap.new(width - 32, height - 32)
    @item_max = @data.size
    @item_max.times do |n|
      self.contents.draw_text(0, n * 32, width - 32, 32, @data[n])
    end
  end
end

class Scene_Test
  def main
    create_windows
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    terminate
  end

  def create_windows
    @test_window = Window_Test.new(0, 0, 640, 64)
    @test_window.refresh
    list = ["Scissors", "Chair", "Pan", "Microwave"]
    @list_window = Window_List.new(0, 64, 160, 32 * 5, list)
    options = ["It's fine", "It sucks!"]
    @command_window = Window_Command.new(160, options)
    @command_window.active = true
    @command_window.x = (640 - 160) / 2
    @command_window.y = (480 - 32 * 3) / 2
  end

  def terminate
    @command_window.dispose
    @list_window.dispose
    @test_window.dispose
  end

  def update
    @command_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
  end
end

And this is exactly what we get after saving our script, closing the script editor, and hitting the green arrow button or the F12 button.

Spoiler: Screenshot 05
center_windows_xp05.jpg

Original thread: https://forums.rpgmakerweb.com/threads/center-windows-xp-edition.167070/