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.
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
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.
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.
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.