Skip to content

Implement Columns field of imported .gpl palettes. #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/Autoload/Palettes.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES }
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")
## All available palettes
var palettes := {}
Expand Down Expand Up @@ -447,6 +449,7 @@ func _import_gpl(path: String, text: String) -> Palette:
var line_number := 0
var palette_name := path.get_basename().get_file()
var comments := ""
var columns := 0
var colors := PackedColorArray()

for line in lines:
Expand All @@ -464,8 +467,11 @@ func _import_gpl(path: String, text: String) -> Palette:
elif line.begins_with("Name: "):
palette_name = line.replace("Name: ", "")
elif line.begins_with("Columns: "):
# Number of colors in this palette. Unnecessary and often wrong
continue
# The width of the palette.
line = line.trim_prefix("Columns: ")
if !line.is_valid_int():
continue
columns = line.to_int()
elif line_number > 0 && line.length() >= 9:
line = line.replace("\t", " ")
var color_data: PackedStringArray = line.split(" ", false, 4)
Expand All @@ -481,7 +487,7 @@ func _import_gpl(path: String, text: String) -> Palette:
line_number += 1

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


Expand Down Expand Up @@ -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)


## Fills a new [Palette] with colors. Used when importing files.
## TODO: Somehow let the users choose the fixed height or width, instead of hardcoding 8.
## Fills a new [Palette] with colors. Used when importing files. Dimensions are
## determined by taking colors as a one-dimensional array that is wrapped by
## width.
func _fill_imported_palette_with_colors(
palette_name: String, colors: PackedColorArray, comment := ""
palette_name: String,
colors: PackedColorArray,
comment := "",
width := Palette.DEFAULT_WIDTH,
) -> Palette:
var height := ceili(colors.size() / 8.0)
var result := Palette.new(palette_name, 8, height, comment)
if width <= 0:
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:
result.add_color(color)

Expand Down