Rewrote Terrain Tag Editor to work with overly large tilesets

This commit is contained in:
Maruno17
2021-05-18 22:31:47 +01:00
parent 8af787abdd
commit 42b7988089

View File

@@ -12,62 +12,93 @@ class PokemonTilesetScene
TEXT_SHADOW_COLOR = Color.new(192, 192, 192) TEXT_SHADOW_COLOR = Color.new(192, 192, 192)
def initialize def initialize
@tilesets_data = load_data("Data/Tilesets.rxdata")
@viewport = Viewport.new(0, 0, Graphics.width, Graphics.height) @viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
@viewport.z = 99999 @viewport.z = 99999
@tilesets_data = load_data("Data/Tilesets.rxdata")
@tileset = @tilesets_data[1]
@tilehelper = TileDrawingHelper.fromTileset(@tileset)
@sprites = {} @sprites = {}
@sprites["title"] = Window_UnformattedTextPokemon.newWithSize(_INTL("Tileset Editor\r\nQ/W: SCROLL\r\nZ: MENU"), @sprites["title"] = Window_UnformattedTextPokemon.newWithSize(
_INTL("Tileset Editor\r\nA/S: SCROLL\r\nZ: MENU"),
TILESET_WIDTH, 0, Graphics.width - TILESET_WIDTH, 128, @viewport) TILESET_WIDTH, 0, Graphics.width - TILESET_WIDTH, 128, @viewport)
@sprites["tileset"] = IconSprite.new(0, 0, @viewport) @sprites["tileset"] = BitmapSprite.new(TILESET_WIDTH, Graphics.height, @viewport)
@sprites["tileset"].setBitmap("Graphics/Tilesets/#{@tileset.tileset_name}")
@sprites["tileset"].src_rect = Rect.new(0, 0, TILESET_WIDTH, Graphics.height)
@sprites["overlay"] = BitmapSprite.new(Graphics.width, Graphics.height, @viewport) @sprites["overlay"] = BitmapSprite.new(Graphics.width, Graphics.height, @viewport)
@sprites["overlay"].x = 0
@sprites["overlay"].y = 0
pbSetSystemFont(@sprites["overlay"].bitmap) pbSetSystemFont(@sprites["overlay"].bitmap)
@sprites["title"].visible = true @visible_height = @sprites["tileset"].bitmap.height / TILE_SIZE
@sprites["tileset"].visible = true load_tileset(1)
@sprites["overlay"].visible = true
@x = 0
@y = -TILE_SIZE
@topy = -TILE_SIZE
@height = @sprites["tileset"].bitmap.height
pbUpdateTileset
end end
def pbUpdateTileset def open_screen
@sprites["overlay"].bitmap.clear pbFadeInAndShow(@sprites)
@sprites["tileset"].src_rect = Rect.new(0, @topy, TILESET_WIDTH, Graphics.height) end
# Draw all text over tiles, along with their coordinates (and graphics for autotiles)
textpos = [] def close_screen
tilesize = @tileset.terrain_tags.xsize pbFadeOutAndHide(@sprites)
for yy in 0...Graphics.height / TILE_SIZE pbDisposeSpriteHash(@sprites)
ypos = (yy + (@topy / TILE_SIZE)) * TILES_PER_ROW + TILESET_START_ID @viewport.dispose
next if ypos >= tilesize @tilehelper.dispose
end
def load_tileset(id)
@tileset = @tilesets_data[id]
@tilehelper.dispose if @tilehelper
@tilehelper = TileDrawingHelper.fromTileset(@tileset)
@x = 0
@y = 0
@top_y = 0
@height = (@tileset.terrain_tags.xsize - TILESET_START_ID) / TILES_PER_ROW + 1
draw_tiles
draw_overlay
end
def choose_tileset
commands = []
for i in 1...@tilesets_data.length
commands.push(sprintf("%03d %s", i, @tilesets_data[i].name))
end
ret = pbShowCommands(nil, commands, -1)
load_tileset(ret + 1) if ret >= 0
end
def draw_tiles
@sprites["tileset"].bitmap.clear
for yy in 0...@visible_height
autotile_row = (@top_y == 0 && yy == 0) # Autotiles
id_y_offset = (autotile_row) ? 0 : TILESET_START_ID + (@top_y + yy - 1) * TILES_PER_ROW
for xx in 0...TILES_PER_ROW for xx in 0...TILES_PER_ROW
if ypos < TILESET_START_ID id_x_offset = (autotile_row) ? xx * TILES_PER_AUTOTILE : xx
@tilehelper.bltTile(@sprites["overlay"].bitmap, xx * TILE_SIZE, yy * TILE_SIZE, xx * TILES_PER_AUTOTILE) @tilehelper.bltTile(@sprites["tileset"].bitmap, xx * TILE_SIZE, yy * TILE_SIZE,
end id_y_offset + id_x_offset)
terr = (ypos < TILESET_START_ID) ? @tileset.terrain_tags[xx * TILES_PER_AUTOTILE] : @tileset.terrain_tags[ypos + xx] end
end
end
def draw_overlay
@sprites["overlay"].bitmap.clear
# Draw all text over tiles (terrain tag numbers)
textpos = []
for yy in 0...@visible_height
for xx in 0...TILES_PER_ROW
tile_id = tile_ID_from_coordinates(xx, @top_y + yy)
terr = @tileset.terrain_tags[tile_id]
textpos.push(["#{terr}", xx * TILE_SIZE + TILE_SIZE / 2, yy * TILE_SIZE - 6, 2, TEXT_COLOR, TEXT_SHADOW_COLOR]) textpos.push(["#{terr}", xx * TILE_SIZE + TILE_SIZE / 2, yy * TILE_SIZE - 6, 2, TEXT_COLOR, TEXT_SHADOW_COLOR])
end end
end end
pbDrawTextPositions(@sprites["overlay"].bitmap, textpos) pbDrawTextPositions(@sprites["overlay"].bitmap, textpos)
# Draw cursor # Draw cursor
@sprites["overlay"].bitmap.fill_rect(@x, @y - @topy, TILE_SIZE, 4, CURSOR_COLOR) cursor_x = @x * TILE_SIZE
@sprites["overlay"].bitmap.fill_rect(@x, @y - @topy, 4, TILE_SIZE, CURSOR_COLOR) cursor_y = (@y - @top_y) * TILE_SIZE
@sprites["overlay"].bitmap.fill_rect(@x, @y - @topy + TILE_SIZE - 4, TILE_SIZE, 4, CURSOR_COLOR) @sprites["overlay"].bitmap.fill_rect(cursor_x, cursor_y, TILE_SIZE, 4, CURSOR_COLOR)
@sprites["overlay"].bitmap.fill_rect(@x + TILE_SIZE - 4, @y - @topy, 4, TILE_SIZE, CURSOR_COLOR) @sprites["overlay"].bitmap.fill_rect(cursor_x, cursor_y, 4, TILE_SIZE, CURSOR_COLOR)
pbUpdateTileInformation @sprites["overlay"].bitmap.fill_rect(cursor_x, cursor_y + TILE_SIZE - 4, TILE_SIZE, 4, CURSOR_COLOR)
@sprites["overlay"].bitmap.fill_rect(cursor_x + TILE_SIZE - 4, cursor_y, 4, TILE_SIZE, CURSOR_COLOR)
# Draw information about selected tile on right side
draw_tile_details
end end
def pbUpdateTileInformation def draw_tile_details
overlay = @sprites["overlay"].bitmap overlay = @sprites["overlay"].bitmap
tile_x = Graphics.width * 3 / 4 - TILE_SIZE tile_x = Graphics.width * 3 / 4 - TILE_SIZE
tile_y = Graphics.height / 2 - TILE_SIZE tile_y = Graphics.height / 2 - TILE_SIZE
tile_id = pbGetSelected(@x, @y) || 0 tile_id = tile_ID_from_coordinates(@x, @y) || 0
# Draw tile (at 200% size) # Draw tile (at 200% size)
@tilehelper.bltSmallTile(overlay, tile_x, tile_y, TILE_SIZE * 2, TILE_SIZE * 2, tile_id) @tilehelper.bltSmallTile(overlay, tile_x, tile_y, TILE_SIZE * 2, TILE_SIZE * 2, tile_id)
# Draw box around tile image # Draw box around tile image
@@ -90,31 +121,12 @@ class PokemonTilesetScene
pbDrawTextPositions(overlay, textpos) pbDrawTextPositions(overlay, textpos)
end end
def pbChooseTileset def tile_ID_from_coordinates(x, y)
commands = [] return x * TILES_PER_AUTOTILE if y == 0 # Autotile
for i in 1...@tilesets_data.length return TILESET_START_ID + (y - 1) * TILES_PER_ROW + x
commands.push(sprintf("%03d %s", i, @tilesets_data[i].name))
end
ret = pbShowCommands(nil, commands, -1)
if ret >= 0
@tileset = @tilesets_data[ret + 1]
@tilehelper.dispose
@tilehelper = TileDrawingHelper.fromTileset(@tileset)
@sprites["tileset"].setBitmap("Graphics/Tilesets/#{@tileset.tileset_name}")
@x = 0
@y = -TILE_SIZE
@topy = -TILE_SIZE
pbUpdateTileset
@height = @sprites["tileset"].bitmap.height
end
end end
def pbGetSelected(x, y) def set_terrain_tag_for_tile_ID(i, value)
return TILES_PER_AUTOTILE * (x / TILE_SIZE) if y < 0 # Autotile
return TILESET_START_ID + (y / TILE_SIZE) * TILES_PER_ROW + (x / TILE_SIZE)
end
def pbSetSelected(i,value)
if i < TILESET_START_ID if i < TILESET_START_ID
for j in 0...TILES_PER_AUTOTILE for j in 0...TILES_PER_AUTOTILE
@tileset.terrain_tags[i + j] = value @tileset.terrain_tags[i + j] = value
@@ -127,21 +139,24 @@ class PokemonTilesetScene
def update_cursor_position(x_offset, y_offset) def update_cursor_position(x_offset, y_offset)
old_x = @x old_x = @x
old_y = @y old_y = @y
old_top_y = @top_y
if x_offset != 0 if x_offset != 0
@x += x_offset * TILE_SIZE @x += x_offset
@x = @x.clamp(0, TILESET_WIDTH - TILE_SIZE) @x = @x.clamp(0, TILES_PER_ROW - 1)
end end
if y_offset != 0 if y_offset != 0
@y += y_offset * TILE_SIZE @y += y_offset
@y = @y.clamp(-TILE_SIZE, @height - TILE_SIZE) @y = @y.clamp(0, @height - 1)
@topy = @y if @y < @topy @top_y = @y if @y < @top_y
@topy = @y - Graphics.height + TILE_SIZE if @y >= @topy + Graphics.height @top_y = @y - @visible_height + 1 if @y >= @top_y + @visible_height
@top_y = 0 if @top_y < 0
end end
pbUpdateTileset if @x != old_x || @y != old_y draw_tiles if @top_y != old_top_y
draw_overlay if @x != old_x || @y != old_y
end end
def pbStartScene def pbStartScene
pbFadeInAndShow(@sprites) open_screen
loop do loop do
Graphics.update Graphics.update
Input.update Input.update
@@ -154,9 +169,9 @@ class PokemonTilesetScene
elsif Input.repeat?(Input::RIGHT) elsif Input.repeat?(Input::RIGHT)
update_cursor_position(1, 0) update_cursor_position(1, 0)
elsif Input.repeat?(Input::JUMPUP) elsif Input.repeat?(Input::JUMPUP)
update_cursor_position(0, -Graphics.height / TILE_SIZE) update_cursor_position(0, -@visible_height)
elsif Input.repeat?(Input::JUMPDOWN) elsif Input.repeat?(Input::JUMPDOWN)
update_cursor_position(0, Graphics.height / TILE_SIZE) update_cursor_position(0, @visible_height)
elsif Input.trigger?(Input::ACTION) elsif Input.trigger?(Input::ACTION)
commands = [ commands = [
_INTL("Go to bottom"), _INTL("Go to bottom"),
@@ -164,17 +179,13 @@ class PokemonTilesetScene
_INTL("Change tileset"), _INTL("Change tileset"),
_INTL("Cancel") _INTL("Cancel")
] ]
case pbShowCommands(nil,commands,-1) case pbShowCommands(nil, commands, -1)
when 0 when 0
@y = @height - TILE_SIZE update_cursor_position(0, 99999)
@topy = @y - Graphics.height + TILE_SIZE if @y - @topy >= Graphics.height
pbUpdateTileset
when 1 when 1
@y = -TILE_SIZE update_cursor_position(0, -99999)
@topy = @y if @y < @topy
pbUpdateTileset
when 2 when 2
pbChooseTileset choose_tileset
end end
elsif Input.trigger?(Input::BACK) elsif Input.trigger?(Input::BACK)
if pbConfirmMessage(_INTL("Save changes?")) if pbConfirmMessage(_INTL("Save changes?"))
@@ -192,23 +203,21 @@ class PokemonTilesetScene
end end
break if pbConfirmMessage(_INTL("Exit from the editor?")) break if pbConfirmMessage(_INTL("Exit from the editor?"))
elsif Input.trigger?(Input::USE) elsif Input.trigger?(Input::USE)
selected = pbGetSelected(@x, @y) selected = tile_ID_from_coordinates(@x, @y)
params = ChooseNumberParams.new params = ChooseNumberParams.new
params.setRange(0, 99) params.setRange(0, 99)
params.setDefaultValue(@tileset.terrain_tags[selected]) params.setDefaultValue(@tileset.terrain_tags[selected])
pbSetSelected(selected,pbMessageChooseNumber(_INTL("Set the terrain tag."), params)) set_terrain_tag_for_tile_ID(selected, pbMessageChooseNumber(_INTL("Set the terrain tag."), params))
pbUpdateTileset draw_overlay
end end
end end
pbFadeOutAndHide(@sprites) close_screen
pbDisposeSpriteHash(@sprites)
@viewport.dispose
@tilehelper.dispose
end end
end end
#===============================================================================
#
#===============================================================================
def pbTilesetScreen def pbTilesetScreen
pbFadeOutIn { pbFadeOutIn {
scene = PokemonTilesetScene.new scene = PokemonTilesetScene.new