Checkbox cleanup
- Switch to using helpers to detect state and render boxes, - Make state-coloring declarative as per combobox, - Use the clicked state, No behavioral changes.
This commit is contained in:
parent
cbecfffe04
commit
b62c5f3020
1 changed files with 25 additions and 31 deletions
|
@ -2,44 +2,38 @@ package raygui
|
||||||
|
|
||||||
import "github.com/gen2brain/raylib-go/raylib"
|
import "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
|
||||||
|
// checkboxColoring describes the per-state properties for a CheckBox control.
|
||||||
|
type checkboxColoring struct {
|
||||||
|
Border, Inside Property
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkboxColors lists the styling for each supported state.
|
||||||
|
var checkboxColors = map[ControlState]checkboxColoring{
|
||||||
|
Normal: {CheckboxDefaultBorderColor, CheckboxDefaultInsideColor},
|
||||||
|
Clicked: {CheckboxDefaultBorderColor, CheckboxDefaultInsideColor},
|
||||||
|
Pressed: {CheckboxClickBorderColor, CheckboxClickInsideColor},
|
||||||
|
Focused: {CheckboxHoverBorderColor, CheckboxHoverInsideColor},
|
||||||
|
}
|
||||||
|
|
||||||
// CheckBox - Check Box element, returns true when active
|
// CheckBox - Check Box element, returns true when active
|
||||||
func CheckBox(bounds rl.Rectangle, checked bool) bool {
|
func CheckBox(bounds rl.Rectangle, checked bool) bool {
|
||||||
b := bounds.ToInt32()
|
state := GetInteractionState(bounds)
|
||||||
state := Normal
|
colors, exists := checkboxColors[state]
|
||||||
mousePoint := rl.GetMousePosition()
|
if !exists {
|
||||||
|
return checked
|
||||||
|
}
|
||||||
|
|
||||||
// Update control
|
// Update control
|
||||||
if rl.CheckCollisionPointRec(mousePoint, bounds) {
|
if state == Clicked {
|
||||||
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
|
checked = !checked
|
||||||
state = Pressed
|
|
||||||
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) || rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
|
||||||
state = Normal
|
|
||||||
checked = !checked
|
|
||||||
} else {
|
|
||||||
state = Focused
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw control
|
|
||||||
switch state {
|
|
||||||
case Normal:
|
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[CheckboxDefaultBorderColor])))
|
|
||||||
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[CheckboxDefaultInsideColor])))
|
|
||||||
break
|
|
||||||
case Focused:
|
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[CheckboxHoverBorderColor])))
|
|
||||||
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[CheckboxHoverInsideColor])))
|
|
||||||
break
|
|
||||||
case Pressed:
|
|
||||||
rl.DrawRectangle(b.X, b.Y, b.Width, b.Height, rl.GetColor(int32(style[CheckboxClickBorderColor])))
|
|
||||||
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[CheckboxClickInsideColor])))
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render control
|
||||||
|
box := bounds.ToInt32()
|
||||||
|
DrawBorderedRectangle(box, GetStyle32(ToggleBorderWidth), GetStyleColor(colors.Border), GetStyleColor(colors.Inside))
|
||||||
if checked {
|
if checked {
|
||||||
rl.DrawRectangle(b.X+int32(style[CheckboxInsideWidth]), b.Y+int32(style[CheckboxInsideWidth]), b.Width-(2*int32(style[CheckboxInsideWidth])), b.Height-(2*int32(style[CheckboxInsideWidth])), rl.GetColor(int32(style[CheckboxDefaultActiveColor])))
|
// Show the inner button.
|
||||||
|
DrawInsetRectangle(box, GetStyle32(CheckboxInsideWidth), GetStyleColor(CheckboxDefaultActiveColor))
|
||||||
}
|
}
|
||||||
|
|
||||||
return checked
|
return checked
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue