<commit_before>
require_relative 'ui_tileset_editor'

class TilesetEditorDialog < Qt::Dialog
  BACKGROUND_BRUSH = Qt::Brush.new(Qt::Color.new(200, 200, 200, 255))
  RED_PEN_COLOR = Qt::Pen.new(Qt::Color.new(255, 0, 0))
  
  slots "select_tile_by_x_y(int, int, const Qt::MouseButton&)"
  slots "select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"
  slots "gfx_page_changed(int)"
  slots "palette_changed(int)"
  slots "toggle_flips(int)"
  slots "button_box_clicked(QAbstractButton*)"
  
  def initialize(main_window, fs, renderer, room)
    super(main_window, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    @ui = Ui_TilesetEditor.new
    @ui.setup_ui(self)
    
    @fs = fs
    @renderer = renderer
    @room = room
    @layer = @room.layers.first
    
    @tileset_graphics_scene = ClickableGraphicsScene.new
    @ui.tileset_graphics_view.setScene(@tileset_graphics_scene)
    @tileset_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    connect(@tileset_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_by_x_y(int, int, const Qt::MouseButton&)"))
    connect(@tileset_graphics_scene, SIGNAL("moved(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_by_x_y(int, int, const Qt::MouseButton&)"))
    
    @gfx_page_graphics_scene = ClickableGraphicsScene.new
    @ui.gfx_page_graphics_view.scale(3, 3)
    @ui.gfx_page_graphics_view.setScene(@gfx_page_graphics_scene)
    @gfx_page_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    connect(@gfx_page_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"))
    connect(@gfx_page_graphics_scene, SIGNAL("moved(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"))
    
    @selected_tile_graphics_scene = Qt::GraphicsScene.new
    @ui.selected_tile_graphics_view.scale(3, 3)
    @ui.selected_tile_graphics_view.setScene(@selected_tile_graphics_scene)
    @selected_tile_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    
    @ui.tileset_pointer.text = "%08X" % @layer.ram_pointer_to_tileset_for_layer
    @ui.colors_per_palette.text = "%02X" % @layer.colors_per_palette
    @ui.gfx_list_pointer.text = "%08X" % @room.tileset_wrapper_A_ram_pointer
    @ui.palette_list_pointer.text = "%08X" % @room.palette_offset
    load_tileset()
    
    connect(@ui.gfx_page_index, SIGNAL("activated(int)"), self, SLOT("gfx_page_changed(int)"))
    connect(@ui.palette_index, SIGNAL("activated(int)"), self, SLOT("palette_changed(int)"))
    connect(@ui.horizontal_flip, SIGNAL("stateChanged(int)"), self, SLOT("toggle_flips(int)"))
    connect(@ui.vertical_flip, SIGNAL("stateChanged(int)"), self, SLOT("toggle_flips(int)"))
    
    connect(@ui.buttonBox, SIGNAL("clicked(QAbstractButton*)"), self, SLOT("button_box_clicked(QAbstractButton*)"))
    
    self.show()
  end
  
  def create_tile_pixmap_item(tile)
    return if tile.is_blank
    
    graphic_tile_data_file = @room.graphic_tilesets_for_room[tile.tile_page]
    palette_list = @renderer.generate_palettes(@room.palette_offset, @layer.colors_per_palette)
    palette = palette_list[tile.palette_index]
    chunky_tile = @renderer.render_graphic_tile(graphic_tile_data_file, palette, tile.index_on_tile_page)
    if tile.horizontal_flip
      chunky_tile.mirror!
    end
    if tile.vertical_flip
      chunky_tile.flip!
    end
    
    pixmap = Qt::Pixmap.new()
    blob = chunky_tile.to_blob
    pixmap.loadFromData(blob, blob.length)
    tile_item = Qt::GraphicsPixmapItem.new(pixmap)
    
    return tile_item
  end
  
  def load_tileset
    @tileset_graphics_scene.clear()
    
    @tileset_pointer = @ui.tileset_pointer.text.to_i(16)
    @colors_per_palette = @ui.colors_per_palette.text.to_i(16)
    
    return if @tileset_pointer == 0 || @colors_per_palette == 0
    
    @tileset = Tileset.new(@tileset_pointer, @fs)
    
    @palettes = @renderer.generate_palettes(@room.palette_offset, @colors_per_palette)
    
    @ui.gfx_page_index.clear()
    @room.graphic_tilesets_for_room.each_with_index do |gfx_page, i|
      @ui.gfx_page_index.addItem("%02X" % i)
    end
    @ui.gfx_page_index.setCurrentIndex(0)
    
    @ui.palette_index.clear()
    @palettes.each_with_index do |palette, i|
      @ui.palette_index.addItem("%02X" % i)
    end
    
    select_tile(0)
    
    palette_changed(0)
    
    render_tileset()
  end
  
  def render_tileset
    @tileset_graphics_scene.clear()
    
    @tileset_pixmap_items = []
    @tileset.tiles.each_with_index do |tile, index_on_tileset|
      if tile.is_blank
        @tileset_pixmap_items << nil
        next
      end
      
      tile_pixmap_item = Qt::GraphicsPixmapItem.new
      @tileset_pixmap_items << tile_pixmap_item
      
      render_tile_on_tileset(index_on_tileset)
      
      x_on_tileset = index_on_tileset % 16
      y_on_tileset = index_on_tileset / 16
      tile_pixmap_item.setPos(x_on_tileset*16 + 8, y_on_tileset*16 + 8)
      
      @tileset_graphics_scene.addItem(tile_pixmap_item)
    end
  end
  
  def render_tile_on_tileset(tile_index)
    tile = @tileset.tiles[tile_index]
    
    if tile.is_blank
      return
    end
    
    tile_pixmap_item = @tileset_pixmap_items[tile_index]
    
    graphic_tile_data_file = @room.graphic_tilesets_for_room[tile.tile_page]
    if graphic_tile_data_file.nil?
      return # TODO: figure out why this sometimes happens.
    end
    
    if tile.palette_index == 0xFF # TODO. 255 seems to have some special meaning besides an actual palette index.
      puts "Palette index is 0xFF, tileset %08X" % @tileset_pointer
      return
    end
    palette = @palettes[tile.palette_index]
    if palette.nil?
      puts "Palette index #{tile.palette_index} out of range, tileset %08X" % @tileset_pointer
      return # TODO: figure out why this sometimes happens.
    end
    
    chunky_tile = @renderer.render_graphic_tile(graphic_tile_data_file, palette, tile.index_on_tile_page)
    
    pixmap = Qt::Pixmap.new
    blob = chunky_tile.to_blob
    pixmap.loadFromData(blob, blob.length)
    tile_pixmap_item.pixmap = pixmap
    
    xscale = 1
    yscale = 1
    if tile.horizontal_flip
      xscale = -1
    end
    if tile.vertical_flip
      yscale = -1
    end
    tile_pixmap_item.setOffset(-8, -8)
    tile_pixmap_item.scale(xscale, yscale)<commit_msg>Tileset editor: Fix flips being wrong<commit_after>
require_relative 'ui_tileset_editor'

class TilesetEditorDialog < Qt::Dialog
  BACKGROUND_BRUSH = Qt::Brush.new(Qt::Color.new(200, 200, 200, 255))
  RED_PEN_COLOR = Qt::Pen.new(Qt::Color.new(255, 0, 0))
  
  slots "select_tile_by_x_y(int, int, const Qt::MouseButton&)"
  slots "select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"
  slots "gfx_page_changed(int)"
  slots "palette_changed(int)"
  slots "toggle_flips(int)"
  slots "button_box_clicked(QAbstractButton*)"
  
  def initialize(main_window, fs, renderer, room)
    super(main_window, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    @ui = Ui_TilesetEditor.new
    @ui.setup_ui(self)
    
    @fs = fs
    @renderer = renderer
    @room = room
    @layer = @room.layers.first
    
    @tileset_graphics_scene = ClickableGraphicsScene.new
    @ui.tileset_graphics_view.setScene(@tileset_graphics_scene)
    @tileset_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    connect(@tileset_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_by_x_y(int, int, const Qt::MouseButton&)"))
    connect(@tileset_graphics_scene, SIGNAL("moved(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_by_x_y(int, int, const Qt::MouseButton&)"))
    
    @gfx_page_graphics_scene = ClickableGraphicsScene.new
    @ui.gfx_page_graphics_view.scale(3, 3)
    @ui.gfx_page_graphics_view.setScene(@gfx_page_graphics_scene)
    @gfx_page_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    connect(@gfx_page_graphics_scene, SIGNAL("clicked(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"))
    connect(@gfx_page_graphics_scene, SIGNAL("moved(int, int, const Qt::MouseButton&)"), self, SLOT("select_tile_index_on_gfx_page(int, int, const Qt::MouseButton&)"))
    
    @selected_tile_graphics_scene = Qt::GraphicsScene.new
    @ui.selected_tile_graphics_view.scale(3, 3)
    @ui.selected_tile_graphics_view.setScene(@selected_tile_graphics_scene)
    @selected_tile_graphics_scene.setBackgroundBrush(BACKGROUND_BRUSH)
    
    @ui.tileset_pointer.text = "%08X" % @layer.ram_pointer_to_tileset_for_layer
    @ui.colors_per_palette.text = "%02X" % @layer.colors_per_palette
    @ui.gfx_list_pointer.text = "%08X" % @room.tileset_wrapper_A_ram_pointer
    @ui.palette_list_pointer.text = "%08X" % @room.palette_offset
    load_tileset()
    
    connect(@ui.gfx_page_index, SIGNAL("activated(int)"), self, SLOT("gfx_page_changed(int)"))
    connect(@ui.palette_index, SIGNAL("activated(int)"), self, SLOT("palette_changed(int)"))
    connect(@ui.horizontal_flip, SIGNAL("stateChanged(int)"), self, SLOT("toggle_flips(int)"))
    connect(@ui.vertical_flip, SIGNAL("stateChanged(int)"), self, SLOT("toggle_flips(int)"))
    
    connect(@ui.buttonBox, SIGNAL("clicked(QAbstractButton*)"), self, SLOT("button_box_clicked(QAbstractButton*)"))
    
    self.show()
  end
  
  def create_tile_pixmap_item(tile)
    return if tile.is_blank
    
    graphic_tile_data_file = @room.graphic_tilesets_for_room[tile.tile_page]
    palette_list = @renderer.generate_palettes(@room.palette_offset, @layer.colors_per_palette)
    palette = palette_list[tile.palette_index]
    chunky_tile = @renderer.render_graphic_tile(graphic_tile_data_file, palette, tile.index_on_tile_page)
    if tile.horizontal_flip
      chunky_tile.mirror!
    end
    if tile.vertical_flip
      chunky_tile.flip!
    end
    
    pixmap = Qt::Pixmap.new()
    blob = chunky_tile.to_blob
    pixmap.loadFromData(blob, blob.length)
    tile_item = Qt::GraphicsPixmapItem.new(pixmap)
    
    return tile_item
  end
  
  def load_tileset
    @tileset_graphics_scene.clear()
    
    @tileset_pointer = @ui.tileset_pointer.text.to_i(16)
    @colors_per_palette = @ui.colors_per_palette.text.to_i(16)
    
    return if @tileset_pointer == 0 || @colors_per_palette == 0
    
    @tileset = Tileset.new(@tileset_pointer, @fs)
    
    @palettes = @renderer.generate_palettes(@room.palette_offset, @colors_per_palette)
    
    @ui.gfx_page_index.clear()
    @room.graphic_tilesets_for_room.each_with_index do |gfx_page, i|
      @ui.gfx_page_index.addItem("%02X" % i)
    end
    @ui.gfx_page_index.setCurrentIndex(0)
    
    @ui.palette_index.clear()
    @palettes.each_with_index do |palette, i|
      @ui.palette_index.addItem("%02X" % i)
    end
    
    select_tile(0)
    
    palette_changed(0)
    
    render_tileset()
  end
  
  def render_tileset
    @tileset_graphics_scene.clear()
    
    @tileset_pixmap_items = []
    @tileset.tiles.each_with_index do |tile, index_on_tileset|
      if tile.is_blank
        @tileset_pixmap_items << nil
        next
      end
      
      tile_pixmap_item = Qt::GraphicsPixmapItem.new
      @tileset_pixmap_items << tile_pixmap_item
      
      render_tile_on_tileset(index_on_tileset)
      
      x_on_tileset = index_on_tileset % 16
      y_on_tileset = index_on_tileset / 16
      tile_pixmap_item.setPos(x_on_tileset*16 + 8, y_on_tileset*16 + 8)
      
      @tileset_graphics_scene.addItem(tile_pixmap_item)
    end
  end
  
  def render_tile_on_tileset(tile_index)
    tile = @tileset.tiles[tile_index]
    
    if tile.is_blank
      return
    end
    
    tile_pixmap_item = @tileset_pixmap_items[tile_index]
    
    graphic_tile_data_file = @room.graphic_tilesets_for_room[tile.tile_page]
    if graphic_tile_data_file.nil?
      return # TODO: figure out why this sometimes happens.
    end
    
    if tile.palette_index == 0xFF # TODO. 255 seems to have some special meaning besides an actual palette index.
      puts "Palette index is 0xFF, tileset %08X" % @tileset_pointer
      return
    end
    palette = @palettes[tile.palette_index]
    if palette.nil?
      puts "Palette index #{tile.palette_index} out of range, tileset %08X" % @tileset_pointer
      return # TODO: figure out why this sometimes happens.
    end
    
    chunky_tile = @renderer.render_graphic_tile(graphic_tile_data_file, palette, tile.index_on_tile_page)
    
    pixmap = Qt::Pixmap.new
    blob = chunky_tile.to_blob
    pixmap.loadFromData(blob, blob.length)
    tile_pixmap_item.pixmap = pixmap
    
    xscale = 1
    yscale = 1
    if tile.horizontal_flip
      xscale = -1
    end
    if tile.vertical_flip
      yscale = -1
    end
    tile_pixmap_item.setOffset(-8, -8)
    tile_pixmap_item.setTransform(Qt::Transform::fromScale(xscale, yscale))
{'fraction_non_alphanumeric': 0.10859623285016666, 'fraction_numerical': 0.010386791721571972, 'mean_word_length': 4.083530338849488, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1342167', 'n_tokens_mistral': 4886, 'n_tokens_neox': 4778, 'n_words': 708}