<commit_before>/*
 * Copyright ©1998-2022 by <NAME>. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

package sheet

import (
	"fmt"
	"strconv"

	"github.com/richardwilkes/gcs/v5/model/gurps"
	"github.com/richardwilkes/gcs/v5/model/gurps/datafile"
	"github.com/richardwilkes/gcs/v5/model/theme"
	"github.com/richardwilkes/gcs/v5/res"
	"github.com/richardwilkes/gcs/v5/ui/widget"
	"github.com/richardwilkes/toolbox/i18n"
	"github.com/richardwilkes/unison"
)

// EncumbrancePanel holds the contents of the encumbrance block on the sheet.
type EncumbrancePanel struct {
	unison.Panel
	entity *gurps.Entity
	row    []unison.Paneler
}

// NewEncumbrancePanel creates a new encumbrance panel.
func NewEncumbrancePanel(entity *gurps.Entity) *EncumbrancePanel {
	p := &EncumbrancePanel{entity: entity}
	p.Self = p
	p.SetLayout(&unison.FlexLayout{
		Columns:  9,
		HSpacing: 4,
	})
	p.SetLayoutData(&unison.FlexLayoutData{
		HAlign: unison.FillAlignment,
		VAlign: unison.FillAlignment,
		HGrab:  true,
	})
	p.SetBorder(&widget.TitledBorder{Title: i18n.Text("Encumbrance, Move & Dodge")})
	p.DrawCallback = func(gc *unison.Canvas, rect unison.Rect) {
		r := p.Children()[0].FrameRect()
		r.X = rect.X
		r.Width = rect.Width
		gc.DrawRect(r, theme.HeaderColor.Paint(gc, r, unison.Fill))
		current := int(entity.EncumbranceLevel(true))
		for i, row := range p.row {
			var ink unison.Ink
			switch {
			case current == i:
				if entity.WeightCarried(false) > entity.MaximumCarry(datafile.ExtraHeavy) {
					ink = theme.OverloadedColor
				} else {
					ink = theme.MarkerColor
				}
			case i&1 == 1:
				ink = unison.BandingColor
			default:
				ink = unison.ContentColor
			}
			r = row.AsPanel().FrameRect()
			r.X = rect.X
			r.Width = rect.Width
			gc.DrawRect(r, ink.Paint(gc, r, unison.Fill))
		}
	}

	p.AddChild(widget.NewPageHeader(i18n.Text("Level"), 3))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Max Load"), 1))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Move"), 1))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Dodge"), 1))

	current := entity.EncumbranceLevel(true)
	for i, enc := range datafile.AllEncumbrance {
		marker := widget.NewPageLabel("")
		marker.SetBorder(unison.NewEmptyBorder(unison.Insets{Left: 4}))
		if enc == current {
			baseline := marker.Font.Baseline()
			marker.Drawable = &unison.DrawableSVG{
				SVG:  res.WeightSVG,
				Size: unison.Size{Width: baseline, Height: baseline},
			}
		}
		p.AddChild(marker)
		p.AddChild(p.createLevelField(enc))
		name := widget.NewPageLabel(enc.String())
		name.SetLayoutData(&unison.FlexLayoutData{
			HAlign: unison.FillAlignment,
			VAlign: unison.MiddleAlignment,
			HGrab:  true,
		})
		p.AddChild(name)
		p.row = append(p.row, name)
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createMaxCarryField(enc))
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createMoveField(enc))
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createDodgeField(enc))
	}

	return p
}

func (p *EncumbrancePanel) createLevelField(enc datafile.Encumbrance) *widget.NonEditablePageField {
	field := widget.NewNonEditablePageFieldEnd(func(f *widget.NonEditablePageField) {})
	field.Text = strconv.Itoa(int(enc))
	field.SetLayoutData(&unison.FlexLayoutData{
		HAlign: unison.FillAlignment,
		VAlign: unison.MiddleAlignment,
	})
	return field
}
<commit_msg>Fix #505: Weight icon does not move when encumbrance changes<commit_after>/*
 * Copyright ©1998-2022 by <NAME>. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 2.0. If a copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

package sheet

import (
	"fmt"
	"strconv"

	"github.com/richardwilkes/gcs/v5/model/gurps"
	"github.com/richardwilkes/gcs/v5/model/gurps/datafile"
	"github.com/richardwilkes/gcs/v5/model/theme"
	"github.com/richardwilkes/gcs/v5/res"
	"github.com/richardwilkes/gcs/v5/ui/widget"
	"github.com/richardwilkes/toolbox/i18n"
	"github.com/richardwilkes/unison"
)

// EncumbrancePanel holds the contents of the encumbrance block on the sheet.
type EncumbrancePanel struct {
	unison.Panel
	entity *gurps.Entity
	row    []unison.Paneler
}

// NewEncumbrancePanel creates a new encumbrance panel.
func NewEncumbrancePanel(entity *gurps.Entity) *EncumbrancePanel {
	p := &EncumbrancePanel{entity: entity}
	p.Self = p
	p.SetLayout(&unison.FlexLayout{
		Columns:  9,
		HSpacing: 4,
	})
	p.SetLayoutData(&unison.FlexLayoutData{
		HAlign: unison.FillAlignment,
		VAlign: unison.FillAlignment,
		HGrab:  true,
	})
	p.SetBorder(&widget.TitledBorder{Title: i18n.Text("Encumbrance, Move & Dodge")})
	p.DrawCallback = func(gc *unison.Canvas, rect unison.Rect) {
		r := p.Children()[0].FrameRect()
		r.X = rect.X
		r.Width = rect.Width
		gc.DrawRect(r, theme.HeaderColor.Paint(gc, r, unison.Fill))
		current := int(entity.EncumbranceLevel(true))
		for i, row := range p.row {
			var ink unison.Ink
			switch {
			case current == i:
				if entity.WeightCarried(false) > entity.MaximumCarry(datafile.ExtraHeavy) {
					ink = theme.OverloadedColor
				} else {
					ink = theme.MarkerColor
				}
			case i&1 == 1:
				ink = unison.BandingColor
			default:
				ink = unison.ContentColor
			}
			r = row.AsPanel().FrameRect()
			r.X = rect.X
			r.Width = rect.Width
			gc.DrawRect(r, ink.Paint(gc, r, unison.Fill))
		}
	}

	p.AddChild(widget.NewPageHeader(i18n.Text("Level"), 3))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Max Load"), 1))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Move"), 1))
	p.AddChild(widget.NewToolbarSeparator(0))
	p.AddChild(widget.NewPageHeader(i18n.Text("Dodge"), 1))

	for i, enc := range datafile.AllEncumbrance {
		p.AddChild(p.createMarker(entity, enc))
		p.AddChild(p.createLevelField(enc))
		name := widget.NewPageLabel(enc.String())
		name.SetLayoutData(&unison.FlexLayoutData{
			HAlign: unison.FillAlignment,
			VAlign: unison.MiddleAlignment,
			HGrab:  true,
		})
		p.AddChild(name)
		p.row = append(p.row, name)
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createMaxCarryField(enc))
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createMoveField(enc))
		if i == 0 {
			p.addSeparator()
		}
		p.AddChild(p.createDodgeField(enc))
	}

	return p
}

func (p *EncumbrancePanel) createMarker(entity *gurps.Entity, enc datafile.Encumbrance) *unison.Label {
	marker := widget.NewPageLabel("")
	marker.SetBorder(unison.NewEmptyBorder(unison.Insets{Left: 4}))
	baseline := marker.Font.Baseline()
	marker.Drawable = &unison.DrawableSVG{
		SVG:  res.WeightSVG,
		Size: unison.Size{Width: baseline, Height: baseline},
	}
	marker.DrawCallback = func(gc *unison.Canvas, rect unison.Rect) {
		if enc == entity.EncumbranceLevel(true) {
			marker.DefaultDraw(gc, rect)
		}
	}
	return marker
}

func (p *EncumbrancePanel) createLevelField(enc datafile.Encumbrance) *widget.NonEditablePageField {
	field := widget.NewNonEditablePageFieldEnd(func(f *widget.NonEditablePageField) {})
	field.Text = strconv.Itoa(int(enc))
	field.SetLayoutData(&unison.FlexLayoutData{
		HAlign: unison.FillAlignment,
		VAlign: unison.MiddleAlignment,
	})
	return field
}

{'fraction_non_alphanumeric': 0.13593106995884774, 'fraction_numerical': 0.012474279835390947, 'mean_word_length': 4.191588785046729, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3083980', 'n_tokens_mistral': 3204, 'n_tokens_neox': 2902, 'n_words': 553}