Skip to content

Commit

Permalink
Fix for #908: Sorting by points
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Dec 10, 2024
1 parent a5edbc9 commit 368c62c
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/richardwilkes/pdf v1.24.10
github.com/richardwilkes/rpgtools v1.10.1
github.com/richardwilkes/toolbox v1.122.0
github.com/richardwilkes/unison v0.75.2-0.20241124230154-d368a4e81aff
github.com/richardwilkes/unison v0.76.0
github.com/rjeczalik/notify v0.9.3
github.com/tc-hib/winres v0.3.1
github.com/vearutop/statigz v1.4.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ github.com/richardwilkes/rpgtools v1.10.1 h1:TFElIf4JTbJrehDY51gFNvwNBVKTROctm+J
github.com/richardwilkes/rpgtools v1.10.1/go.mod h1:qHg6Worl8Eyfv+o2+tKALvVQFxjWS8ce1SYDHRm3x8M=
github.com/richardwilkes/toolbox v1.122.0 h1:+pG7gEvgL97BuVl9nlbja/or+M+Yb7vo25/Pfs3grSs=
github.com/richardwilkes/toolbox v1.122.0/go.mod h1:Dq8hbb1n6eTAEjYcOKiFctlDvByKC9fDaGemsKLhYHU=
github.com/richardwilkes/unison v0.75.2-0.20241124230154-d368a4e81aff h1:LOuQFr0AzM0PpWvkKKdpLXNhw1tqCII31wk+Fsx8XeI=
github.com/richardwilkes/unison v0.75.2-0.20241124230154-d368a4e81aff/go.mod h1:IQq7PAmNgu3a5S7ITIfxsAyBj2tLh8yY3R9vRSUzGHo=
github.com/richardwilkes/unison v0.76.0 h1:T23fVmj5sdOk+SzR0RzduJuvySdZZj3LyGljdwudcMg=
github.com/richardwilkes/unison v0.76.0/go.mod h1:I78UqI3cKzKBlyBhmi3zzo5nqWrtNPYHDmi7Z7jAMpU=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
Expand Down
5 changes: 5 additions & 0 deletions model/fxp/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,8 @@ func Extract(in string) (value Int, remainder string) {
func SecondsToDuration(value Int) time.Duration {
return time.Duration(As[int64](value.Mul(Thousand))) * time.Millisecond
}

// IntLessFromString compares two strings as Ints.
func IntLessFromString(a, b string) bool {
return FromStringForced(a) < FromStringForced(b)
}
5 changes: 5 additions & 0 deletions model/fxp/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ func (w *Weight) UnmarshalJSON(in []byte) error {
*w, err = WeightFromString(s, Pound)
return err
}

// WeightLessFromStringFunc returns a func to compare two strings as Weights.
func WeightLessFromStringFunc(units WeightUnit) func(a, b string) bool {
return func(a, b string) bool { return WeightFromStringForced(a, units) < WeightFromStringForced(b, units) }
}
1 change: 1 addition & 0 deletions model/gurps/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
type HeaderData struct {
Title string
Detail string
Less func(a, b string) bool
TitleIsImageKey bool
Primary bool
}
Expand Down
2 changes: 2 additions & 0 deletions model/gurps/conditional_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func ConditionalModifiersHeaderData(columnID int) HeaderData {
case ConditionalModifierValueColumn:
data.Title = i18n.Text("±")
data.Detail = i18n.Text("Modifier")
data.Less = fxp.IntLessFromString
case ConditionalModifierDescriptionColumn:
data.Title = i18n.Text("Condition")
data.Primary = true
Expand All @@ -195,6 +196,7 @@ func ReactionModifiersHeaderData(columnID int) HeaderData {
case ConditionalModifierValueColumn:
data.Title = i18n.Text("±")
data.Detail = i18n.Text("Modifier")
data.Less = fxp.IntLessFromString
case ConditionalModifierDescriptionColumn:
data.Title = i18n.Text("Reaction")
data.Primary = true
Expand Down
6 changes: 6 additions & 0 deletions model/gurps/equipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func EquipmentHeaderData(columnID int, entity *Entity, carried, forPage bool) He
case EquipmentQuantityColumn:
data.Title = i18n.Text("#")
data.Detail = i18n.Text("Quantity")
data.Less = fxp.IntLessFromString
case EquipmentDescriptionColumn:
data.Title = i18n.Text("Equipment")
if forPage && entity != nil {
Expand All @@ -318,6 +319,7 @@ func EquipmentHeaderData(columnID int, entity *Entity, carried, forPage bool) He
case EquipmentUsesColumn:
data.Title = i18n.Text("Uses")
data.Detail = i18n.Text("The number of uses remaining")
data.Less = fxp.IntLessFromString
case EquipmentTLColumn:
data.Title = i18n.Text("TL")
data.Detail = i18n.Text("Tech Level")
Expand All @@ -328,18 +330,22 @@ func EquipmentHeaderData(columnID int, entity *Entity, carried, forPage bool) He
data.Title = HeaderCoins
data.TitleIsImageKey = true
data.Detail = i18n.Text("The value of one of these pieces of equipment")
data.Less = fxp.IntLessFromString
case EquipmentExtendedCostColumn:
data.Title = HeaderStackedCoins
data.TitleIsImageKey = true
data.Detail = i18n.Text("The value of all of these pieces of equipment, plus the value of any contained equipment")
data.Less = fxp.IntLessFromString
case EquipmentWeightColumn:
data.Title = HeaderWeight
data.TitleIsImageKey = true
data.Detail = i18n.Text("The weight of one of these pieces of equipment")
data.Less = fxp.WeightLessFromStringFunc(SheetSettingsFor(entity).DefaultWeightUnits)
case EquipmentExtendedWeightColumn:
data.Title = HeaderStackedWeight
data.TitleIsImageKey = true
data.Detail = i18n.Text("The weight of all of these pieces of equipment, plus the weight of any contained equipment")
data.Less = fxp.WeightLessFromStringFunc(SheetSettingsFor(entity).DefaultWeightUnits)
case EquipmentTagsColumn:
data.Title = i18n.Text("Tags")
case EquipmentReferenceColumn:
Expand Down
1 change: 1 addition & 0 deletions model/gurps/trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func TraitsHeaderData(columnID int) HeaderData {
case TraitPointsColumn:
data.Title = i18n.Text("Pts")
data.Detail = i18n.Text("Points")
data.Less = fxp.IntLessFromString
case TraitTagsColumn:
data.Title = i18n.Text("Tags")
case TraitReferenceColumn:
Expand Down
37 changes: 22 additions & 15 deletions ux/editor_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ import (
)

// NewEditorListHeader creates a new list header for an editor.
func NewEditorListHeader[T gurps.NodeTypes](title, tooltip string, forPage bool) unison.TableColumnHeader[*Node[T]] {
func NewEditorListHeader[T gurps.NodeTypes](title, tooltip string, less func(a, b string) bool, forPage bool) unison.TableColumnHeader[*Node[T]] {
if forPage {
return NewPageTableColumnHeader[T](title, tooltip)
return NewPageTableColumnHeader[T](title, tooltip, less)
}
return NewTableColumnHeader[T](title, tooltip)
return NewTableColumnHeader[T](title, tooltip, less)
}

// NewEditorListSVGHeader creates a new list header with an SVG image as its content rather than text.
func NewEditorListSVGHeader[T gurps.NodeTypes](icon *unison.SVG, tooltip string, forPage bool) unison.TableColumnHeader[*Node[T]] {
func NewEditorListSVGHeader[T gurps.NodeTypes](icon *unison.SVG, tooltip string, less func(a, b string) bool, forPage bool) unison.TableColumnHeader[*Node[T]] {
if forPage {
header := NewPageTableColumnHeader[T]("", tooltip)
header := NewPageTableColumnHeader[T]("", tooltip, less)
baseline := header.Font.Baseline()
header.Drawable = &unison.DrawableSVG{
SVG: icon,
Size: unison.NewSize(baseline, baseline),
}
return header
}
header := NewTableColumnHeader[T]("", tooltip)
header := NewTableColumnHeader[T]("", tooltip, less)
baseline := header.Font.Baseline()
header.Drawable = &unison.DrawableSVG{
SVG: icon,
Expand All @@ -49,9 +49,9 @@ func NewEditorListSVGHeader[T gurps.NodeTypes](icon *unison.SVG, tooltip string,
}

// NewEditorListSVGPairHeader creates a new list header with a pair of SVG images as its content rather than text.
func NewEditorListSVGPairHeader[T gurps.NodeTypes](leftSVG, rightSVG *unison.SVG, tooltip string, forPage bool) unison.TableColumnHeader[*Node[T]] {
func NewEditorListSVGPairHeader[T gurps.NodeTypes](leftSVG, rightSVG *unison.SVG, tooltip string, less func(a, b string) bool, forPage bool) unison.TableColumnHeader[*Node[T]] {
if forPage {
header := NewPageTableColumnHeader[T]("", tooltip)
header := NewPageTableColumnHeader[T]("", tooltip, less)
baseline := header.Font.Baseline()
header.Drawable = &DrawableSVGPair{
Left: leftSVG,
Expand All @@ -60,7 +60,7 @@ func NewEditorListSVGPairHeader[T gurps.NodeTypes](leftSVG, rightSVG *unison.SVG
}
return header
}
header := NewTableColumnHeader[T]("", tooltip)
header := NewTableColumnHeader[T]("", tooltip, less)
baseline := header.Font.Baseline()
header.Drawable = &DrawableSVGPair{
Left: leftSVG,
Expand Down Expand Up @@ -92,18 +92,18 @@ func headerFromData[T gurps.NodeTypes](data gurps.HeaderData, forPage bool) unis
img2 = svg.Weight
}
if img2 != nil {
return NewEditorListSVGPairHeader[T](img1, img2, data.Detail, forPage)
return NewEditorListSVGPairHeader[T](img1, img2, data.Detail, data.Less, forPage)
}
if img1 != nil {
return NewEditorListSVGHeader[T](img1, data.Detail, forPage)
return NewEditorListSVGHeader[T](img1, data.Detail, data.Less, forPage)
}
}
return NewEditorListHeader[T](data.Title, data.Detail, forPage)
return NewEditorListHeader[T](data.Title, data.Detail, data.Less, forPage)
}

// NewTableColumnHeader creates a new table column header panel with the given title in small caps.
func NewTableColumnHeader[T gurps.NodeTypes](title, tooltip string) *unison.DefaultTableColumnHeader[*Node[T]] {
header := unison.NewTableColumnHeader[*Node[T]](title, tooltip)
func NewTableColumnHeader[T gurps.NodeTypes](title, tooltip string, less func(a, b string) bool) *unison.DefaultTableColumnHeader[*Node[T]] {
header := unison.NewTableColumnHeader[*Node[T]](title, tooltip, less)
header.Text = unison.NewSmallCapsText(title, &header.TextDecoration)
return header
}
Expand All @@ -126,13 +126,15 @@ var _ unison.TableColumnHeader[*Node[*gurps.Trait]] = &PageTableColumnHeader[*gu
// PageTableColumnHeader provides a default page table column header panel.
type PageTableColumnHeader[T gurps.NodeTypes] struct {
*unison.Label
less func(a, b string) bool
sortState unison.SortState
}

// NewPageTableColumnHeader creates a new page table column header panel with the given title.
func NewPageTableColumnHeader[T gurps.NodeTypes](title, tooltip string) *PageTableColumnHeader[T] {
func NewPageTableColumnHeader[T gurps.NodeTypes](title, tooltip string, less func(a, b string) bool) *PageTableColumnHeader[T] {
h := &PageTableColumnHeader[T]{
Label: unison.NewLabel(),
less: less,
sortState: unison.SortState{
Order: -1,
Ascending: true,
Expand Down Expand Up @@ -179,6 +181,11 @@ func (h *PageTableColumnHeader[T]) DefaultDraw(gc *unison.Canvas, dirty unison.R
}
}

// Less returns the current less function.
func (h *PageTableColumnHeader[T]) Less() func(a, b string) bool {
return h.less
}

// SortState returns the current SortState.
func (h *PageTableColumnHeader[T]) SortState() unison.SortState {
return h.sortState
Expand Down

0 comments on commit 368c62c

Please sign in to comment.