mirror of
https://github.com/Orama-Interactive/Pixelorama.git
synced 2025-01-18 17:19:50 +00:00
Implement Columns field of imported .gpl palettes. (#1025)
* Implement Columns field of imported .gpl palettes. - Fixes Columns field being ignored when imported. - _fill_imported_palette_with_colors is modified to accept an optional width parameter, which defaults to Palette.DEFAULT_WIDTH instead of a hard-coded 8. - A maximum of width of 16384 is enforced to mitigate maliciously large values. * Run gdformat. * Run gdlint. * Consume all whitespace between field and value.
This commit is contained in:
parent
8ec5653d67
commit
f69e2d06eb
|
@ -10,6 +10,8 @@ enum NewPalettePresetType {EMPTY, FROM_CURRENT_PALETTE, FROM_CURRENT_SPRITE, FRO
|
||||||
## Color options when user creates a new palette from current sprite or selection
|
## Color options when user creates a new palette from current sprite or selection
|
||||||
enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES }
|
enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES }
|
||||||
const DEFAULT_PALETTE_NAME := "Default"
|
const DEFAULT_PALETTE_NAME := "Default"
|
||||||
|
## Maximum allowed width of imported palettes.
|
||||||
|
const MAX_IMPORT_PAL_WIDTH = 1 << 14
|
||||||
var palettes_write_path := Global.home_data_directory.path_join("Palettes")
|
var palettes_write_path := Global.home_data_directory.path_join("Palettes")
|
||||||
## All available palettes
|
## All available palettes
|
||||||
var palettes := {}
|
var palettes := {}
|
||||||
|
@ -447,6 +449,7 @@ func _import_gpl(path: String, text: String) -> Palette:
|
||||||
var line_number := 0
|
var line_number := 0
|
||||||
var palette_name := path.get_basename().get_file()
|
var palette_name := path.get_basename().get_file()
|
||||||
var comments := ""
|
var comments := ""
|
||||||
|
var columns := 0
|
||||||
var colors := PackedColorArray()
|
var colors := PackedColorArray()
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
|
@ -464,8 +467,11 @@ func _import_gpl(path: String, text: String) -> Palette:
|
||||||
elif line.begins_with("Name: "):
|
elif line.begins_with("Name: "):
|
||||||
palette_name = line.replace("Name: ", "")
|
palette_name = line.replace("Name: ", "")
|
||||||
elif line.begins_with("Columns: "):
|
elif line.begins_with("Columns: "):
|
||||||
# Number of colors in this palette. Unnecessary and often wrong
|
# The width of the palette.
|
||||||
|
line = line.trim_prefix("Columns: ").strip_edges()
|
||||||
|
if !line.is_valid_int():
|
||||||
continue
|
continue
|
||||||
|
columns = line.to_int()
|
||||||
elif line_number > 0 && line.length() >= 9:
|
elif line_number > 0 && line.length() >= 9:
|
||||||
line = line.replace("\t", " ")
|
line = line.replace("\t", " ")
|
||||||
var color_data: PackedStringArray = line.split(" ", false, 4)
|
var color_data: PackedStringArray = line.split(" ", false, 4)
|
||||||
|
@ -481,7 +487,7 @@ func _import_gpl(path: String, text: String) -> Palette:
|
||||||
line_number += 1
|
line_number += 1
|
||||||
|
|
||||||
if line_number > 0:
|
if line_number > 0:
|
||||||
return _fill_imported_palette_with_colors(palette_name, colors, comments)
|
return _fill_imported_palette_with_colors(palette_name, colors, comments, columns)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@ -522,13 +528,20 @@ func _import_image_palette(path: String, image: Image) -> Palette:
|
||||||
return _fill_imported_palette_with_colors(path.get_basename().get_file(), colors)
|
return _fill_imported_palette_with_colors(path.get_basename().get_file(), colors)
|
||||||
|
|
||||||
|
|
||||||
## Fills a new [Palette] with colors. Used when importing files.
|
## Fills a new [Palette] with colors. Used when importing files. Dimensions are
|
||||||
## TODO: Somehow let the users choose the fixed height or width, instead of hardcoding 8.
|
## determined by taking colors as a one-dimensional array that is wrapped by
|
||||||
|
## width.
|
||||||
func _fill_imported_palette_with_colors(
|
func _fill_imported_palette_with_colors(
|
||||||
palette_name: String, colors: PackedColorArray, comment := ""
|
palette_name: String,
|
||||||
|
colors: PackedColorArray,
|
||||||
|
comment := "",
|
||||||
|
width := Palette.DEFAULT_WIDTH,
|
||||||
) -> Palette:
|
) -> Palette:
|
||||||
var height := ceili(colors.size() / 8.0)
|
if width <= 0:
|
||||||
var result := Palette.new(palette_name, 8, height, comment)
|
width = Palette.DEFAULT_WIDTH
|
||||||
|
width = clampi(width, 1, MAX_IMPORT_PAL_WIDTH)
|
||||||
|
var height := ceili(colors.size() / float(width))
|
||||||
|
var result := Palette.new(palette_name, width, height, comment)
|
||||||
for color in colors:
|
for color in colors:
|
||||||
result.add_color(color)
|
result.add_color(color)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue