Skip to content

Commit

Permalink
internal/driver: move some definitions to internal/ui
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Feb 6, 2022
1 parent 149736c commit 6f72b15
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 199 deletions.
26 changes: 14 additions & 12 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@

package ebiten

import "github.com/hajimehoshi/ebiten/v2/internal/driver"
import (
"github.com/hajimehoshi/ebiten/v2/internal/ui"
)

// CursorModeType represents a render and coordinate mode of a mouse cursor.
type CursorModeType = driver.CursorMode
type CursorModeType = ui.CursorMode

// CursorModeTypes
const (
CursorModeVisible CursorModeType = CursorModeType(driver.CursorModeVisible)
CursorModeHidden CursorModeType = CursorModeType(driver.CursorModeHidden)
CursorModeCaptured CursorModeType = CursorModeType(driver.CursorModeCaptured)
CursorModeVisible CursorModeType = CursorModeType(ui.CursorModeVisible)
CursorModeHidden CursorModeType = CursorModeType(ui.CursorModeHidden)
CursorModeCaptured CursorModeType = CursorModeType(ui.CursorModeCaptured)
)

// CursorShapeType represents a shape of a mouse cursor.
type CursorShapeType = driver.CursorShape
type CursorShapeType = ui.CursorShape

// CursorShapeTypes
const (
CursorShapeDefault CursorShapeType = CursorShapeType(driver.CursorShapeDefault)
CursorShapeText CursorShapeType = CursorShapeType(driver.CursorShapeText)
CursorShapeCrosshair CursorShapeType = CursorShapeType(driver.CursorShapeCrosshair)
CursorShapePointer CursorShapeType = CursorShapeType(driver.CursorShapePointer)
CursorShapeEWResize CursorShapeType = CursorShapeType(driver.CursorShapeEWResize)
CursorShapeNSResize CursorShapeType = CursorShapeType(driver.CursorShapeNSResize)
CursorShapeDefault CursorShapeType = CursorShapeType(ui.CursorShapeDefault)
CursorShapeText CursorShapeType = CursorShapeType(ui.CursorShapeText)
CursorShapeCrosshair CursorShapeType = CursorShapeType(ui.CursorShapeCrosshair)
CursorShapePointer CursorShapeType = CursorShapeType(ui.CursorShapePointer)
CursorShapeEWResize CursorShapeType = CursorShapeType(ui.CursorShapeEWResize)
CursorShapeNSResize CursorShapeType = CursorShapeType(ui.CursorShapeNSResize)
)
60 changes: 0 additions & 60 deletions internal/driver/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,9 @@
package driver

import (
"errors"
"image"
"time"
)

type UIContext interface {
UpdateFrame() error
ForceUpdateFrame() error
Layout(outsideWidth, outsideHeight float64)

// AdjustPosition can be called from a different goroutine from Update's or Layout's.
AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64)
}

// RegularTermination represents a regular termination.
// Run can return this error, and if this error is received,
// the game loop should be terminated as soon as possible.
var RegularTermination = errors.New("regular termination")

type UI interface {
Run(context UIContext) error
RunWithoutMainLoop(context UIContext)

DeviceScaleFactor() float64
IsFocused() bool
ScreenSizeInFullscreen() (int, int)
ResetForFrame()

CursorMode() CursorMode
SetCursorMode(mode CursorMode)

CursorShape() CursorShape
SetCursorShape(shape CursorShape)

IsFullscreen() bool
SetFullscreen(fullscreen bool)

IsRunnableOnUnfocused() bool
SetRunnableOnUnfocused(runnableOnUnfocused bool)

FPSMode() FPSMode
SetFPSMode(mode FPSMode)
ScheduleFrame()

IsScreenTransparent() bool
SetScreenTransparent(transparent bool)
SetInitFocused(focused bool)

Vibrate(duration time.Duration, magnitude float64)

Input() Input
Window() Window
Graphics() Graphics
}

type Window interface {
IsDecorated() bool
SetDecorated(decorated bool)
Expand Down Expand Up @@ -102,11 +50,3 @@ type Window interface {
SetClosingHandled(handled bool)
IsClosingHandled() bool
}

type FPSMode int

const (
FPSModeVsyncOn FPSMode = iota
FPSModeVsyncOffMaximum
FPSModeVsyncOffMinimum
)
2 changes: 1 addition & 1 deletion internal/ui/input_cbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Input struct {
m sync.Mutex
}

func (i *Input) update(context driver.UIContext) {
func (i *Input) update(context Context) {
i.m.Lock()
defer i.m.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion internal/ui/input_glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{
}

// update must be called from the main thread.
func (i *Input) update(window *glfw.Window, context driver.UIContext) error {
func (i *Input) update(window *glfw.Window, context Context) error {
i.ui.m.Lock()
defer i.ui.m.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion internal/ui/input_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (i *Input) updateFromEvent(e js.Value) {
}

func (i *Input) setMouseCursorFromEvent(e js.Value) {
if i.ui.cursorMode == driver.CursorModeCaptured {
if i.ui.cursorMode == CursorModeCaptured {
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
i.origCursorX, i.origCursorY = x, y
dx, dy := e.Get("movementX").Int(), e.Get("movementY").Int()
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/run_notsinglethread.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
package ui

import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
)

func (u *UserInterface) Run(uicontext driver.UIContext) error {
func (u *UserInterface) Run(uicontext Context) error {
u.context = uicontext

// Initialize the main thread first so the thread is available at u.run (#809).
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/run_singlethread.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
package ui

import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
)

func (u *UserInterface) Run(uicontext driver.UIContext) error {
func (u *UserInterface) Run(uicontext Context) error {
u.context = uicontext

// Initialize the main thread first so the thread is available at u.run (#809).
Expand Down
30 changes: 28 additions & 2 deletions internal/driver/cursor.go → internal/ui/ui.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 The Ebiten Authors
// Copyright 2022 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package driver
package ui

import (
"errors"
)

type Context interface {
UpdateFrame() error
ForceUpdateFrame() error
Layout(outsideWidth, outsideHeight float64)

// AdjustPosition can be called from a different goroutine from Update's or Layout's.
AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64)
}

// RegularTermination represents a regular termination.
// Run can return this error, and if this error is received,
// the game loop should be terminated as soon as possible.
var RegularTermination = errors.New("regular termination")

type FPSMode int

const (
FPSModeVsyncOn FPSMode = iota
FPSModeVsyncOffMaximum
FPSModeVsyncOffMinimum
)

type CursorMode int

Expand Down
22 changes: 11 additions & 11 deletions internal/ui/ui_cbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Get() *UserInterface {
return &theUserInterface
}

func (u *UserInterface) Run(context driver.UIContext) error {
func (u *UserInterface) Run(context Context) error {
cbackend.InitializeGame()
for {
w, h := cbackend.ScreenSize()
Expand All @@ -58,7 +58,7 @@ func (u *UserInterface) Run(context driver.UIContext) error {
}
}

func (*UserInterface) RunWithoutMainLoop(context driver.UIContext) {
func (*UserInterface) RunWithoutMainLoop(context Context) {
panic("ui: RunWithoutMainLoop is not implemented")
}

Expand All @@ -77,18 +77,18 @@ func (*UserInterface) ScreenSizeInFullscreen() (int, int) {
func (*UserInterface) ResetForFrame() {
}

func (*UserInterface) CursorMode() driver.CursorMode {
return driver.CursorModeHidden
func (*UserInterface) CursorMode() CursorMode {
return CursorModeHidden
}

func (*UserInterface) SetCursorMode(mode driver.CursorMode) {
func (*UserInterface) SetCursorMode(mode CursorMode) {
}

func (*UserInterface) CursorShape() driver.CursorShape {
return driver.CursorShapeDefault
func (*UserInterface) CursorShape() CursorShape {
return CursorShapeDefault
}

func (*UserInterface) SetCursorShape(shape driver.CursorShape) {
func (*UserInterface) SetCursorShape(shape CursorShape) {
}

func (*UserInterface) IsFullscreen() bool {
Expand All @@ -105,11 +105,11 @@ func (*UserInterface) IsRunnableOnUnfocused() bool {
func (*UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
}

func (*UserInterface) FPSMode() driver.FPSMode {
return driver.FPSModeVsyncOn
func (*UserInterface) FPSMode() FPSMode {
return FPSModeVsyncOn
}

func (*UserInterface) SetFPSMode(mode driver.FPSMode) {
func (*UserInterface) SetFPSMode(mode FPSMode) {
}

func (*UserInterface) ScheduleFrame() {
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/ui_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ package ui
import "C"

import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
)

Expand Down Expand Up @@ -179,7 +178,7 @@ func (u *UserInterface) isNativeFullscreen() bool {
return bool(C.isNativeFullscreen(C.uintptr_t(u.window.GetCocoaWindow())))
}

func (u *UserInterface) setNativeCursor(shape driver.CursorShape) {
func (u *UserInterface) setNativeCursor(shape CursorShape) {
C.setNativeCursor(C.int(shape))
}

Expand Down
Loading

0 comments on commit 6f72b15

Please sign in to comment.