Skip to content

Commit

Permalink
Merge pull request #129 from ebitenui/Add-Error-Messaging
Browse files Browse the repository at this point in the history
Add error messaging for required fields
  • Loading branch information
mcarpenter622 committed Mar 31, 2024
2 parents 2faaa24 + 9711ff5 commit d5405be
Show file tree
Hide file tree
Showing 24 changed files with 418 additions and 110 deletions.
3 changes: 2 additions & 1 deletion _examples/widget_demos/manual_focus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func main() {
btn9.AddFocus(widget.FOCUS_WEST, btn8)
btn9.AddFocus(widget.FOCUS_NORTHWEST, btn5)
btn9.AddFocus(widget.FOCUS_NORTH, btn6)

// construct the UI
ui := ebitenui.UI{
Container: rootContainer,
Expand Down Expand Up @@ -158,7 +159,7 @@ func createButton(label string) *widget.Button {
widget.ButtonOpts.Image(buttonImage),

// specify the button's text, the font face, and the color
widget.ButtonOpts.Text("Hello, World!", face, &widget.ButtonTextColor{
widget.ButtonOpts.Text(label, face, &widget.ButtonTextColor{
Idle: color.NRGBA{0xdf, 0xf4, 0xff, 0xff},
}),

Expand Down
10 changes: 4 additions & 6 deletions _examples/widget_demos/progressbar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ func main() {
widget.WidgetOpts.MinSize(200, 20),
),
widget.ProgressBarOpts.Images(
// Set the track images (Idle, Hover, Disabled).
// Set the track images (Idle, Disabled).
&widget.ProgressBarImage{
Idle: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}),
Hover: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}),
Idle: image.NewNineSliceColor(color.NRGBA{100, 100, 100, 255}),
},
// Set the progress images (Idle, Hover, Disabled).
// Set the progress images (Idle, Disabled).
&widget.ProgressBarImage{
Idle: image.NewNineSliceColor(color.NRGBA{0, 0, 255, 255}),
Hover: image.NewNineSliceColor(color.NRGBA{0, 0, 255, 255}),
Idle: image.NewNineSliceColor(color.NRGBA{0, 0, 255, 255}),
},
),
// Set the min, max, and current values.
Expand Down
4 changes: 3 additions & 1 deletion _examples/widget_demos/tabbook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func main() {
widget.ButtonOpts.WidgetOpts(widget.WidgetOpts.MinSize(98, 0)),
),
widget.TabBookOpts.Tabs(tabDisabled, game.TabRed, game.TabGreen, game.TabBlue),
// widget.TabBookOpts.InitialTab(tabGreen),

// Set the Initial Tab
// widget.TabBookOpts.InitialTab(game.TabGreen),
)
// add the tabBook as a child of the container
rootContainer.AddChild(game.TabBook)
Expand Down
5 changes: 3 additions & 2 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ func (u *UI) handleFocusChangeRequest() {
func (u *UI) ChangeFocus(direction widget.FocusDirection) {
if u.focusedWidget != nil {
if next := u.focusedWidget.(widget.Focuser).GetFocus(direction); next != nil {

next.Focus(true)
if !next.GetWidget().Disabled {
next.Focus(true)
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,35 @@ func NewButton(opts ...ButtonOpt) *Button {
o(b)
}

b.validate()

return b
}

func (b *Button) validate() {
if b.Image == nil {
panic("Button: Image is required.")
}
if b.Image.Idle == nil {
panic("Button: Image.Idle is required.")
}
if b.Image.Pressed == nil {
panic("Button: Image.Pressed is required.")
}

if len(b.textLabel) > 0 {
if b.textFace == nil {
panic("Button: TextFace is required if TextLabel is set.")
}
if b.TextColor == nil {
panic("Button: TextColor is required if TextLabel is set.")
}
if b.TextColor.Idle == nil {
panic("Button: TextColor.Idle is required if TextLabel is set.")
}
}
}

func (o ButtonOptions) WidgetOpts(opts ...WidgetOpt) ButtonOpt {
return func(b *Button) {
b.widgetOpts = append(b.widgetOpts, opts...)
Expand Down Expand Up @@ -468,15 +494,15 @@ func (b *Button) Render(screen *ebiten.Image, def DeferredRenderFunc) {

if b.autoUpdateTextAndGraphic {
if b.graphic != nil {
if b.widget.Disabled {
if b.widget.Disabled && b.GraphicImage.Disabled != nil {
b.graphic.Image = b.GraphicImage.Disabled
} else {
b.graphic.Image = b.GraphicImage.Idle
}
}

if b.text != nil {
if b.widget.Disabled {
if b.widget.Disabled && b.TextColor.Disabled != nil {
b.text.Color = b.TextColor.Disabled
} else {
b.text.Color = b.TextColor.Idle
Expand Down Expand Up @@ -589,6 +615,7 @@ func (b *Button) initText() {
b.container.AddChild(b.text)

b.autoUpdateTextAndGraphic = true

}

func (b *Button) createWidget() {
Expand Down
8 changes: 8 additions & 0 deletions widget/caret.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@ func NewCaret(opts ...CaretOpt) *Caret {
o(c)
}

c.validate()

return c
}

func (c *Caret) validate() {
if c.face == nil {
panic("Caret: Font Face is required.")
}
}

func (o CaretOptions) Color(c color.Color) CaretOpt {
return func(ca *Caret) {
ca.Color = c
Expand Down
24 changes: 24 additions & 0 deletions widget/checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,33 @@ func NewCheckbox(opts ...CheckboxOpt) *Checkbox {
o(c)
}

c.validate()

return c
}

func (c *Checkbox) validate() {
if len(c.buttonOpts) == 0 {
panic("Checkbox: ButtonOpts are required.")
}
if c.image == nil {
panic("Checkbox: Image is required.")
}
if c.image.Checked == nil {
panic("Checkbox: Image.Checked is required.")
}
if c.image.Checked.Idle == nil {
panic("Checkbox: Image.Checked.Idle is required.")
}

if c.image.Unchecked == nil {
panic("Checkbox: Image.Unchecked is required.")
}
if c.image.Unchecked.Idle == nil {
panic("Checkbox: Image.Unchecked.Idle is required.")
}
}

func (o CheckboxOptions) ButtonOpts(opts ...ButtonOpt) CheckboxOpt {
return func(c *Checkbox) {
c.buttonOpts = append(c.buttonOpts, opts...)
Expand Down
10 changes: 10 additions & 0 deletions widget/combobutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ func NewComboButton(opts ...ComboButtonOpt) *ComboButton {
o(c)
}

c.validate()

return c
}

func (c *ComboButton) validate() {
if c.content == nil {
panic("ComboButton: Content is required.")
}
if len(c.buttonOpts) == 0 {
panic("ComboButton: ButtonOpts are required.")
}
}
func (o ComboButtonOptions) ButtonOpts(opts ...ButtonOpt) ComboButtonOpt {
return func(c *ComboButton) {
c.buttonOpts = append(c.buttonOpts, opts...)
Expand Down
8 changes: 8 additions & 0 deletions widget/dnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ func NewDragAndDrop(opts ...DragAndDropOpt) *DragAndDrop {
o(d)
}

d.validate()

return d
}

func (d *DragAndDrop) validate() {
if d.contentsCreater == nil {
panic("DragAndDrop: ContentsCreater is required.")
}
}

func (o DragAndDropOptions) ContentsCreater(c DragContentsCreater) DragAndDropOpt {
return func(d *DragAndDrop) {
d.contentsCreater = c
Expand Down
16 changes: 15 additions & 1 deletion widget/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,23 @@ func NewLabel(opts ...LabelOpt) *Label {
o(l)
}

l.validate()

return l
}

func (l *Label) validate() {
if l.color == nil {
panic("Label: LabelColor is required.")
}
if l.color.Idle == nil {
panic("Label: LabelColor.Idle is required.")
}
if l.face == nil {
panic("Label: Font Face is required.")
}
}

func (o LabelOptions) TextOpts(opts ...TextOpt) LabelOpt {
return func(l *Label) {
l.textOpts = append(l.textOpts, opts...)
Expand Down Expand Up @@ -79,7 +93,7 @@ func (l *Label) Render(screen *ebiten.Image, def DeferredRenderFunc) {

l.text.Label = l.Label

if l.text.GetWidget().Disabled {
if l.text.GetWidget().Disabled && l.color.Disabled != nil {
l.text.Color = l.color.Disabled
} else {
l.text.Color = l.color.Idle
Expand Down
11 changes: 11 additions & 0 deletions widget/labeledcheckbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ func NewLabeledCheckbox(opts ...LabeledCheckboxOpt) *LabeledCheckbox {
o(l)
}

l.validate()

return l
}

func (l *LabeledCheckbox) validate() {
if len(l.checkboxOpts) == 0 {
panic("LabeledCheckbox: CheckboxOpts are required.")
}
if len(l.labelOpts) == 0 {
panic("LabeledCheckbox: LabelOpts are required.")
}
}

func (o LabeledCheckboxOptions) WidgetOpts(opts ...WidgetOpt) LabeledCheckboxOpt {
return func(l *LabeledCheckbox) {
l.widgetOpts = append(l.widgetOpts, opts...)
Expand Down
23 changes: 23 additions & 0 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,32 @@ func NewList(opts ...ListOpt) *List {

l.resetFocusIndex()

l.validate()

return l
}

func (l *List) validate() {
if len(l.scrollContainerOpts) == 0 {
panic("List: ScrollContainerOpts are required.")
}
if len(l.sliderOpts) == 0 {
panic("List: SliderOpts are required.")
}
if l.entryFace == nil {
panic("List: EntryFontFace is required.")
}
if l.entryLabelFunc == nil {
panic("List: EntryLabelFunc is required.")
}
if l.entryTextColor == nil || l.entryTextColor.Idle == nil {
panic("List: ListEntryColor.Selected is required.")
}
if l.entryUnselectedTextColor == nil || l.entryUnselectedTextColor.Idle == nil {
panic("List: ListEntryColor.Unselected is required.")
}
}

func (o ListOptions) ContainerOpts(opts ...ContainerOpt) ListOpt {
return func(l *List) {
l.containerOpts = append(l.containerOpts, opts...)
Expand Down
11 changes: 11 additions & 0 deletions widget/listcombobutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,20 @@ func NewListComboButton(opts ...ListComboButtonOpt) *ListComboButton {
o(l)
}

l.validate()

return l
}

func (l *ListComboButton) validate() {
if len(l.buttonOpts) == 0 {
panic("ListComboButton: ButtonOpts are required.")
}
if len(l.listOpts) == 0 {
panic("ListComboButton: ListOpts are required.")
}
}

func (o ListComboButtonOptions) SelectComboButtonOpts(opts ...SelectComboButtonOpt) ListComboButtonOpt {
return func(l *ListComboButton) {
l.buttonOpts = append(l.buttonOpts, opts...)
Expand Down
17 changes: 16 additions & 1 deletion widget/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,21 @@ func NewProgressBar(opts ...ProgressBarOpt) *ProgressBar {
o(pb)
}

pb.validate()

return pb
}

func (pb *ProgressBar) validate() {
if pb.trackImage == nil {
panic("ProgressBar: TrackImage is required.")
}
if pb.trackImage.Idle == nil {
panic("ProgressBar: TrackImage.Idle is required")
}

}

func (o ProgressBarOptions) WidgetOpts(opts ...WidgetOpt) ProgressBarOpt {
return func(s *ProgressBar) {
s.widgetOpts = append(s.widgetOpts, opts...)
Expand Down Expand Up @@ -139,7 +151,10 @@ func (s *ProgressBar) Render(screen *ebiten.Image, def DeferredRenderFunc) {

func (s *ProgressBar) draw(screen *ebiten.Image) {
i := s.trackImage.Idle
fill := s.fillImage.Idle
var fill *image.NineSlice
if s.fillImage != nil {
fill = s.fillImage.Idle
}
if s.widget.Disabled {
if s.trackImage.Disabled != nil {
i = s.trackImage.Disabled
Expand Down
14 changes: 14 additions & 0 deletions widget/scrollcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func NewScrollContainer(opts ...ScrollContainerOpt) *ScrollContainer {
o(s)
}

s.validate()

s.content.GetWidget().ContextMenuEvent.AddHandler(func(args interface{}) {
a := args.(*WidgetContextMenuEventArgs)
s.GetWidget().FireContextMenuEvent(a.Widget, a.Location)
Expand All @@ -70,6 +72,18 @@ func NewScrollContainer(opts ...ScrollContainerOpt) *ScrollContainer {
return s
}

func (s *ScrollContainer) validate() {
if s.image == nil {
panic("ScrollContainer: Image is required.")
}
if s.image.Idle == nil {
panic("ScrollContainer: Image.Idle is required.")
}
if s.image.Mask == nil {
panic("ScrollContainer: Image.Mask is required.")
}
}

func (o ScrollContainerOptions) WidgetOpts(opts ...WidgetOpt) ScrollContainerOpt {
return func(s *ScrollContainer) {
s.widgetOpts = append(s.widgetOpts, opts...)
Expand Down

0 comments on commit d5405be

Please sign in to comment.