mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 12:08:58 +01:00
examples/ui: Use image.Rectangle as struct members
This commit is contained in:
parent
ce7dc79697
commit
ce126fbcdd
@ -168,11 +168,8 @@ func drawNinePatches(dst *ebiten.Image, dstRect image.Rectangle, srcRect image.R
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
X int
|
Rect image.Rectangle
|
||||||
Y int
|
Text string
|
||||||
Width int
|
|
||||||
Height int
|
|
||||||
Text string
|
|
||||||
|
|
||||||
mouseDown bool
|
mouseDown bool
|
||||||
pressed bool
|
pressed bool
|
||||||
@ -182,7 +179,7 @@ func (b *Button) Update() {
|
|||||||
b.pressed = false
|
b.pressed = false
|
||||||
if theInput.IsMouseButtonPressed() {
|
if theInput.IsMouseButtonPressed() {
|
||||||
x, y := ebiten.CursorPosition()
|
x, y := ebiten.CursorPosition()
|
||||||
if b.X <= x && x < (b.X+b.Width) && b.Y <= y && y < (b.Y+b.Height) {
|
if b.Rect.Min.X <= x && x < b.Rect.Max.X && b.Rect.Min.Y <= y && y < b.Rect.Max.Y {
|
||||||
b.mouseDown = true
|
b.mouseDown = true
|
||||||
} else {
|
} else {
|
||||||
b.mouseDown = false
|
b.mouseDown = false
|
||||||
@ -200,13 +197,12 @@ func (b *Button) Draw(dst *ebiten.Image) {
|
|||||||
if b.mouseDown {
|
if b.mouseDown {
|
||||||
t = imageTypeButtonPressed
|
t = imageTypeButtonPressed
|
||||||
}
|
}
|
||||||
r := image.Rect(b.X, b.Y, b.X+b.Width, b.Y+b.Height)
|
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
||||||
drawNinePatches(dst, r, imageSrcRects[t])
|
|
||||||
|
|
||||||
bounds, _ := font.BoundString(uiFont, b.Text)
|
bounds, _ := font.BoundString(uiFont, b.Text)
|
||||||
w := (bounds.Max.X - bounds.Min.X).Ceil()
|
w := (bounds.Max.X - bounds.Min.X).Ceil()
|
||||||
x := b.X + (b.Width-w)/2
|
x := b.Rect.Min.X + (b.Rect.Dx()-w)/2
|
||||||
y := (b.Y + b.Height) - (b.Height-uiFontMHeight)/2
|
y := b.Rect.Max.Y - (b.Rect.Dy()-uiFontMHeight)/2
|
||||||
text.Draw(dst, b.Text, uiFont, x, y, color.Black)
|
text.Draw(dst, b.Text, uiFont, x, y, color.Black)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,11 +303,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TextBox struct {
|
type TextBox struct {
|
||||||
X int
|
Rect image.Rectangle
|
||||||
Y int
|
Text string
|
||||||
Width int
|
|
||||||
Height int
|
|
||||||
Text string
|
|
||||||
|
|
||||||
contentBuf *ebiten.Image
|
contentBuf *ebiten.Image
|
||||||
vScrollBar *VScrollBar
|
vScrollBar *VScrollBar
|
||||||
@ -331,9 +324,9 @@ func (t *TextBox) Update() {
|
|||||||
if t.vScrollBar == nil {
|
if t.vScrollBar == nil {
|
||||||
t.vScrollBar = &VScrollBar{}
|
t.vScrollBar = &VScrollBar{}
|
||||||
}
|
}
|
||||||
t.vScrollBar.X = t.X + t.Width - VScrollBarWidth
|
t.vScrollBar.X = t.Rect.Max.X - VScrollBarWidth
|
||||||
t.vScrollBar.Y = t.Y
|
t.vScrollBar.Y = t.Rect.Min.Y
|
||||||
t.vScrollBar.Height = t.Height
|
t.vScrollBar.Height = t.Rect.Dy()
|
||||||
|
|
||||||
_, h := t.contentSize()
|
_, h := t.contentSize()
|
||||||
t.vScrollBar.Update(h)
|
t.vScrollBar.Update(h)
|
||||||
@ -344,11 +337,11 @@ func (t *TextBox) Update() {
|
|||||||
|
|
||||||
func (t *TextBox) contentSize() (int, int) {
|
func (t *TextBox) contentSize() (int, int) {
|
||||||
h := len(strings.Split(t.Text, "\n")) * lineHeight
|
h := len(strings.Split(t.Text, "\n")) * lineHeight
|
||||||
return t.Width, h
|
return t.Rect.Dx(), h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TextBox) viewSize() (int, int) {
|
func (t *TextBox) viewSize() (int, int) {
|
||||||
return t.Width - VScrollBarWidth - textBoxPaddingLeft, t.Height
|
return t.Rect.Dx() - VScrollBarWidth - textBoxPaddingLeft, t.Rect.Dy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TextBox) contentOffset() (int, int) {
|
func (t *TextBox) contentOffset() (int, int) {
|
||||||
@ -356,8 +349,7 @@ func (t *TextBox) contentOffset() (int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TextBox) Draw(dst *ebiten.Image) {
|
func (t *TextBox) Draw(dst *ebiten.Image) {
|
||||||
r := image.Rect(t.X, t.Y, t.X+t.Width, t.Y+t.Height)
|
drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox])
|
||||||
drawNinePatches(dst, r, imageSrcRects[imageTypeTextBox])
|
|
||||||
|
|
||||||
if t.contentBuf != nil {
|
if t.contentBuf != nil {
|
||||||
vw, vh := t.viewSize()
|
vw, vh := t.viewSize()
|
||||||
@ -386,7 +378,7 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
|
|||||||
text.Draw(t.contentBuf, line, uiFont, x, y, color.Black)
|
text.Draw(t.contentBuf, line, uiFont, x, y, color.Black)
|
||||||
}
|
}
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(float64(t.X), float64(t.Y))
|
op.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
|
||||||
dst.DrawImage(t.contentBuf, op)
|
dst.DrawImage(t.contentBuf, op)
|
||||||
|
|
||||||
t.vScrollBar.Draw(dst)
|
t.vScrollBar.Draw(dst)
|
||||||
@ -460,18 +452,12 @@ func (c *CheckBox) CheckChanged() bool {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
button1 = &Button{
|
button1 = &Button{
|
||||||
X: 16,
|
Rect: image.Rect(16, 16, 144, 48),
|
||||||
Y: 16,
|
Text: "Button 1",
|
||||||
Width: 128,
|
|
||||||
Height: 32,
|
|
||||||
Text: "Button 1",
|
|
||||||
}
|
}
|
||||||
button2 = &Button{
|
button2 = &Button{
|
||||||
X: 160,
|
Rect: image.Rect(160, 16, 288, 48),
|
||||||
Y: 16,
|
Text: "Button 2",
|
||||||
Width: 128,
|
|
||||||
Height: 32,
|
|
||||||
Text: "Button 2",
|
|
||||||
}
|
}
|
||||||
checkBox = &CheckBox{
|
checkBox = &CheckBox{
|
||||||
X: 16,
|
X: 16,
|
||||||
@ -479,10 +465,7 @@ var (
|
|||||||
Text: "Check Box!",
|
Text: "Check Box!",
|
||||||
}
|
}
|
||||||
textBoxLog = &TextBox{
|
textBoxLog = &TextBox{
|
||||||
X: 16,
|
Rect: image.Rect(16, 96, 624, 464),
|
||||||
Y: 96,
|
|
||||||
Width: 608,
|
|
||||||
Height: 368,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user