ChocolateBird/widgets/HexStringViewer.gd

61 lines
1.9 KiB
GDScript

extends Control
const hex2font_shader := preload('res://shaders/hex2font.gdshader')
var hexbox: TextureRect
var hexbox_rows: int
var hexbox_cols: int
func make_hexview(data: PoolByteArray, width: int = 256):
var hex2font_mat := ShaderMaterial.new()
hex2font_mat.shader = hex2font_shader
hex2font_mat.set_shader_param('glyph_atlas', SpriteLoader.font_atlas_texture)
var hex_img := Image.new()
var l := len(RomLoader.snes_bytes)
self.hexbox_cols = width
self.hexbox_rows = l / self.hexbox_cols
var remainder = l % self.hexbox_cols
if remainder:
self.hexbox_rows += 1
var padding = PoolByteArray()
padding.resize(self.hexbox_cols - remainder)
padding.fill(0)
data = data + padding
hex2font_mat.set_shader_param('tilemap_size', Vector2(self.hexbox_cols, self.hexbox_rows))
hex_img.create_from_data(self.hexbox_cols, self.hexbox_rows, false, Image.FORMAT_L8, data)
hexbox = TextureRect.new()
hexbox.texture = SpriteLoader.texture_from_image(hex_img)
hexbox.material = hex2font_mat
hexbox.rect_scale *= 4
add_child(hexbox)
func _ready() -> void:
ProjectSettings.set_setting('display/window/size/snap_to_integer', false)
make_hexview(RomLoader.snes_bytes, 480)
var fontbox := TextureRect.new()
fontbox.texture = SpriteLoader.font_atlas_texture
add_child(fontbox)
func scroll_hexview(rows: int):
var current_row: int = (-hexbox.rect_position.y)/4
var next_row = current_row + rows
if next_row > (hexbox_rows - 270):
next_row = hexbox_rows - 270
if next_row < 0:
next_row = 0
hexbox.rect_position.y = -next_row * 4
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
match event.button_index:
BUTTON_WHEEL_DOWN:
scroll_hexview(16)
BUTTON_WHEEL_UP:
scroll_hexview(-16)
elif event is InputEventMouseMotion:
var row: int = (event.position.y - hexbox.rect_position.y) / 4
var col: int = event.position.x / 4
var address: int = col + (row * 480)
hexbox.set_tooltip('0x%06X' % address)