Skip to content

Commit

Permalink
Merge pull request #36 from yohamta/not-button
Browse files Browse the repository at this point in the history
Add NotButton Interface
  • Loading branch information
yohamta authored Dec 3, 2022
2 parents 4fa0ad2 + 33c4b15 commit bf5f0cc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ go get github.com/yohamta/furex/v2
Assets by [Kenney](https://kenney.nl).

<p align="center">
<img width="480" height="640" src="./assets/example.gif">
<img width="480" src="./assets/example.gif">
</p>

## Usage
Expand Down Expand Up @@ -105,7 +105,7 @@ func (b *Box) HandleDraw(screen *ebiten.Image, frame image.Rectangle) {
```

<p align="center">
<img width="592" height="780" src="./assets/greens.png">
<img width="592" src="./assets/greens.png">
</p>

## Debug
Expand All @@ -116,7 +116,7 @@ furex.Debug = true
```

<p align="center">
<img width="592" height="780" src="./assets/debug.png">
<img height="780" src="./assets/debug.png">
</p>

## How to contribute?
Expand Down
38 changes: 23 additions & 15 deletions child.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *child) HandleJustReleasedTouchID(

func (c *child) checkTouchHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
touchHandler, ok := c.item.Handler.(TouchHandler)
if ok && touchHandler != nil {
if ok {
if isInside(frame, x, y) {
if touchHandler.HandleJustPressedTouchID(touchID, x, y) {
c.handledTouchID = touchID
Expand All @@ -63,7 +63,7 @@ func (c *child) checkTouchHandlerStart(frame *image.Rectangle, touchID ebiten.To

func (c *child) checkTouchHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) {
touchHandler, ok := c.item.Handler.(TouchHandler)
if ok && touchHandler != nil {
if ok {
if c.handledTouchID == touchID {
touchHandler.HandleJustReleasedTouchID(touchID, x, y)
c.handledTouchID = -1
Expand All @@ -72,8 +72,8 @@ func (c *child) checkTouchHandlerEnd(frame *image.Rectangle, touchID ebiten.Touc
}

func (c *child) checkSwipeHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
swipeHandler, ok := c.item.Handler.(SwipeHandler)
if ok && swipeHandler != nil {
_, ok := c.item.Handler.(SwipeHandler)
if ok {
if isInside(frame, x, y) {
c.swipeTouchID = touchID
c.swipe.downTime = time.Now()
Expand All @@ -86,7 +86,7 @@ func (c *child) checkSwipeHandlerStart(frame *image.Rectangle, touchID ebiten.To

func (c *child) checkSwipeHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
swipeHandler, ok := c.item.Handler.(SwipeHandler)
if ok && swipeHandler != nil {
if ok {
if c.swipeTouchID != touchID {
return false
}
Expand Down Expand Up @@ -135,24 +135,32 @@ func (c *child) checkSwipe() bool {

func (c *child) checkButtonHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
button, ok := c.item.Handler.(ButtonHandler)
if ok && button != nil {
if isInside(frame, x, y) {
if !c.isButtonPressed {
c.isButtonPressed = true
c.handledTouchID = touchID
button.HandlePress(x, y, touchID)
if ok {
for {
if button, ok := c.item.Handler.(NotButton); ok {
if !button.IsButton() {
break
}
}
return true
} else if c.handledTouchID == touchID {
c.handledTouchID = -1
if isInside(frame, x, y) {
if !c.isButtonPressed {
c.isButtonPressed = true
c.handledTouchID = touchID
button.HandlePress(x, y, touchID)
}
return true
} else if c.handledTouchID == touchID {
c.handledTouchID = -1
}
break
}
}
return false
}

func (c *child) checkButtonHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) {
button, ok := c.item.Handler.(ButtonHandler)
if ok && button != nil {
if ok {
if c.handledTouchID == touchID {
if c.isButtonPressed {
c.isButtonPressed = false
Expand Down
20 changes: 14 additions & 6 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@ func (ct *containerEmbed) handleMouseButtonLeftPressed(x, y int) bool {

button, ok := child.item.Handler.(ButtonHandler)
if ok {
if !result && isInside(childFrame, x, y) {
if !child.isButtonPressed {
child.isButtonPressed = true
child.isMouseLeftButtonHandler = true
result = true
button.HandlePress(x, y, -1)
for {
if button, ok := child.item.Handler.(NotButton); ok {
if !button.IsButton() {
break
}
}
if !result && isInside(childFrame, x, y) {
if !child.isButtonPressed {
child.isButtonPressed = true
child.isMouseLeftButtonHandler = true
result = true
button.HandlePress(x, y, -1)
}
}
break
}
}

Expand Down
7 changes: 7 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type ButtonHandler interface {
HandleRelease(x, y int, isCancel bool)
}

// NotButton represents a component that is not a button.
// TODO: update HandlePress to return bool in the next major version.
type NotButton interface {
// IsButton returns true if the handler is a button.
IsButton() bool
}

// TouchHandler represents a component that handle touches.
type TouchHandler interface {
// HandleJustPressedTouchID handles the touchID just pressed and returns true if it handles the TouchID
Expand Down

0 comments on commit bf5f0cc

Please sign in to comment.