Button cleanup

- Switch to using helpers to detect state and render boxes,
- Make state-coloring declarative as per combobox/checkbox,
- Use the clicked state,

No behavioral changes.
This commit is contained in:
Oliver 'kfsone' Smith 2021-02-24 12:59:43 -08:00
parent b62c5f3020
commit da00f22f9c

View file

@ -2,62 +2,43 @@ package raygui
import "github.com/gen2brain/raylib-go/raylib"
// buttonColoring describes the per-state properties for a Button control.
type buttonColoring struct {
Border, Inside, Text Property
}
// buttonColors lists the styling for each supported state.
var buttonColors = map[ControlState]buttonColoring{
Normal: {ButtonDefaultBorderColor, ButtonDefaultInsideColor, ButtonDefaultTextColor},
Clicked: {ButtonDefaultBorderColor, ButtonDefaultInsideColor, ButtonDefaultTextColor},
Focused: {ButtonHoverBorderColor, ButtonHoverInsideColor, ButtonHoverTextColor},
Pressed: {ButtonPressedBorderColor, ButtonPressedInsideColor, ButtonPressedTextColor},
}
// Button - Button element, returns true when clicked
func Button(bounds rl.Rectangle, text string) bool {
b := bounds.ToInt32()
state := Normal
mousePoint := rl.GetMousePosition()
clicked := false
textHeight := int32(style[GlobalTextFontsize])
textWidth := rl.MeasureText(text, textHeight)
// Update control
if b.Width < textWidth {
b.Width = textWidth + int32(style[ButtonTextPadding])
if int32(bounds.Height) < textHeight {
bounds.Height = float32(textHeight + GetStyle32(ButtonTextPadding)/2)
}
if int32(bounds.Width) < textWidth {
bounds.Width = float32(textWidth + GetStyle32(ButtonTextPadding))
}
if b.Height < textHeight {
b.Height = textHeight + int32(style[ButtonTextPadding])/2
}
if rl.CheckCollisionPointRec(mousePoint, bounds) {
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
state = Pressed
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsMouseButtonPressed(rl.MouseLeftButton) {
clicked = true
} else {
state = Focused
}
// Determine what state we're in and whether its valid.
state := GetInteractionState(bounds)
colors, exist := buttonColors[state]
if !exist {
return false
}
// Draw control
switch state {
case Normal:
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ButtonDefaultBorderColor])))
rl.DrawRectangle(b.X+int32(style[ButtonBorderWidth]), b.Y+int32(style[ButtonBorderWidth]), b.Width-(2*int32(style[ButtonBorderWidth])), b.Height-(2*int32(style[ButtonBorderWidth])), rl.GetColor(int32(style[ButtonDefaultInsideColor])))
rl.DrawText(text, b.X+((b.Width/2)-(rl.MeasureText(text, textHeight)/2)), b.Y+((b.Height/2)-(textHeight/2)), textHeight, rl.GetColor(int32(style[ButtonDefaultTextColor])))
break
b := bounds.ToInt32()
DrawBorderedRectangle(b, GetStyle32(ButtonBorderWidth), GetStyleColor(colors.Border), GetStyleColor(colors.Inside))
rl.DrawText(text, b.X+((b.Width/2)-(rl.MeasureText(text, textHeight)/2)), b.Y+((b.Height/2)-(textHeight/2)), textHeight, GetStyleColor(colors.Text))
case Focused:
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ButtonHoverBorderColor])))
rl.DrawRectangle(b.X+int32(style[ButtonBorderWidth]), b.Y+int32(style[ButtonBorderWidth]), b.Width-(2*int32(style[ButtonBorderWidth])), b.Height-(2*int32(style[ButtonBorderWidth])), rl.GetColor(int32(style[ButtonHoverInsideColor])))
rl.DrawText(text, b.X+((b.Width/2)-(rl.MeasureText(text, textHeight)/2)), b.Y+((b.Height/2)-(textHeight/2)), textHeight, rl.GetColor(int32(style[ButtonHoverTextColor])))
break
case Pressed:
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ButtonPressedBorderColor])))
rl.DrawRectangle(b.X+int32(style[ButtonBorderWidth]), b.Y+int32(style[ButtonBorderWidth]), b.Width-(2*int32(style[ButtonBorderWidth])), b.Height-(2*int32(style[ButtonBorderWidth])), rl.GetColor(int32(style[ButtonPressedInsideColor])))
rl.DrawText(text, b.X+((b.Width/2)-(rl.MeasureText(text, textHeight)/2)), b.Y+((b.Height/2)-(textHeight/2)), textHeight, rl.GetColor(int32(style[ButtonPressedTextColor])))
break
default:
break
}
if clicked {
return true
}
return false
return state == Clicked
}