ToggleButton cleanup

This commit is contained in:
Oliver 'kfsone' Smith 2021-02-24 13:42:34 -08:00
parent bc456c6912
commit e30bdc3edc

View file

@ -2,60 +2,47 @@ package raygui
import "github.com/gen2brain/raylib-go/raylib" import "github.com/gen2brain/raylib-go/raylib"
// togglebuttonColoring describes the per-state colors for a ToggleBox control.
type togglebuttonColoring struct {
Border, Inside, Text Property
}
// togglebuttonColors lists the styling for each supported state.
var togglebuttonColors = map[ControlState]togglebuttonColoring {
Normal: {ToggleDefaultBorderColor, ToggleDefaultInsideColor, ToggleDefaultTextColor},
// Hijacking 'Clicked' for the 'active' state.
Clicked: {ToggleActiveBorderColor, ToggleActiveInsideColor, ToggleDefaultTextColor},
Pressed: {TogglePressedBorderColor, TogglePressedInsideColor, TogglePressedTextColor},
Focused: {ToggleHoverBorderColor, ToggleHoverInsideColor, ToggleHoverTextColor},
}
// ToggleButton - Toggle Button element, returns true when active // ToggleButton - Toggle Button element, returns true when active
func ToggleButton(bounds rl.Rectangle, text string, active bool) bool { func ToggleButton(bounds rl.Rectangle, text string, active bool) bool {
b := bounds.ToInt32()
state := Normal
mousePoint := rl.GetMousePosition()
textHeight := int32(style[GlobalTextFontsize]) textHeight := int32(style[GlobalTextFontsize])
textWidth := rl.MeasureText(text, textHeight) textWidth := rl.MeasureText(text, textHeight)
// Update control ConstrainRectangle(&bounds, textWidth, textWidth + GetStyle32(ToggleTextPadding), textHeight, textHeight + GetStyle32(ToggleTextPadding))
if b.Width < textWidth {
b.Width = textWidth + int32(style[ToggleTextPadding]) state := GetInteractionState(bounds)
} if state == Clicked {
if b.Height < textHeight { active = !active
b.Height = textHeight + int32(style[ToggleTextPadding])/2 state = Normal
} }
if rl.CheckCollisionPointRec(mousePoint, bounds) { // Hijack 'Clicked' as the 'active' state
if rl.IsMouseButtonDown(rl.MouseLeftButton) { if state == Normal && active {
state = Pressed state = Clicked
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsMouseButtonPressed(rl.MouseLeftButton) { }
state = Normal
active = !active colors, exists := togglebuttonColors[state]
} else { if !exists {
state = Focused return active
}
} }
// Draw control // Draw control
switch state { b := bounds.ToInt32()
case Normal: DrawBorderedRectangle(b, GetStyle32(ToggleBorderWidth), GetStyleColor(colors.Border), GetStyleColor(colors.Inside))
if active { rl.DrawText(text, b.X+((b.Width/2)-(rl.MeasureText(text, textHeight)/2)), b.Y+((b.Height/2)-(textHeight/2)), textHeight, GetStyleColor(colors.Text))
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ToggleActiveBorderColor])))
rl.DrawRectangle(b.X+int32(style[ToggleBorderWidth]), b.Y+int32(style[ToggleBorderWidth]), b.Width-(2*int32(style[ToggleBorderWidth])), b.Height-(2*int32(style[ToggleBorderWidth])), rl.GetColor(int32(style[ToggleActiveInsideColor])))
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[ToggleDefaultTextColor])))
} else {
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ToggleDefaultBorderColor])))
rl.DrawRectangle(b.X+int32(style[ToggleBorderWidth]), b.Y+int32(style[ToggleBorderWidth]), b.Width-(2*int32(style[ToggleBorderWidth])), b.Height-(2*int32(style[ToggleBorderWidth])), rl.GetColor(int32(style[ToggleDefaultInsideColor])))
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[ToggleDefaultTextColor])))
}
break
case Focused:
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ToggleHoverBorderColor])))
rl.DrawRectangle(b.X+int32(style[ToggleBorderWidth]), b.Y+int32(style[ToggleBorderWidth]), b.Width-(2*int32(style[ToggleBorderWidth])), b.Height-(2*int32(style[ToggleBorderWidth])), rl.GetColor(int32(style[ToggleHoverInsideColor])))
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[ToggleHoverTextColor])))
break
case Pressed:
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[TogglePressedBorderColor])))
rl.DrawRectangle(b.X+int32(style[ToggleBorderWidth]), b.Y+int32(style[ToggleBorderWidth]), b.Width-(2*int32(style[ToggleBorderWidth])), b.Height-(2*int32(style[ToggleBorderWidth])), rl.GetColor(int32(style[TogglePressedInsideColor])))
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[TogglePressedTextColor])))
break
default:
break
}
return active return active
} }