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:
parent
b62c5f3020
commit
da00f22f9c
1 changed files with 27 additions and 46 deletions
|
@ -2,62 +2,43 @@ package raygui
|
||||||
|
|
||||||
import "github.com/gen2brain/raylib-go/raylib"
|
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
|
// Button - Button element, returns true when clicked
|
||||||
func Button(bounds rl.Rectangle, text string) bool {
|
func Button(bounds rl.Rectangle, text string) bool {
|
||||||
b := bounds.ToInt32()
|
|
||||||
state := Normal
|
|
||||||
mousePoint := rl.GetMousePosition()
|
|
||||||
clicked := false
|
|
||||||
|
|
||||||
textHeight := int32(style[GlobalTextFontsize])
|
textHeight := int32(style[GlobalTextFontsize])
|
||||||
textWidth := rl.MeasureText(text, textHeight)
|
textWidth := rl.MeasureText(text, textHeight)
|
||||||
|
|
||||||
// Update control
|
// Update control
|
||||||
if b.Width < textWidth {
|
if int32(bounds.Height) < textHeight {
|
||||||
b.Width = textWidth + int32(style[ButtonTextPadding])
|
bounds.Height = float32(textHeight + GetStyle32(ButtonTextPadding)/2)
|
||||||
|
}
|
||||||
|
if int32(bounds.Width) < textWidth {
|
||||||
|
bounds.Width = float32(textWidth + GetStyle32(ButtonTextPadding))
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Height < textHeight {
|
// Determine what state we're in and whether its valid.
|
||||||
b.Height = textHeight + int32(style[ButtonTextPadding])/2
|
state := GetInteractionState(bounds)
|
||||||
}
|
colors, exist := buttonColors[state]
|
||||||
|
if !exist {
|
||||||
if rl.CheckCollisionPointRec(mousePoint, bounds) {
|
return false
|
||||||
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
|
|
||||||
state = Pressed
|
|
||||||
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
|
||||||
clicked = true
|
|
||||||
} else {
|
|
||||||
state = Focused
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw control
|
// Draw control
|
||||||
switch state {
|
b := bounds.ToInt32()
|
||||||
case Normal:
|
DrawBorderedRectangle(b, GetStyle32(ButtonBorderWidth), GetStyleColor(colors.Border), GetStyleColor(colors.Inside))
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[ButtonDefaultBorderColor])))
|
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+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
|
|
||||||
|
|
||||||
case Focused:
|
return state == Clicked
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue