2016-05-18 20:17:50 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-10-01 16:24:42 +02:00
|
|
|
//go:build android || ios
|
2016-05-18 20:17:50 +02:00
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
2016-05-18 20:17:50 +02:00
|
|
|
|
|
|
|
import (
|
2022-12-28 09:03:49 +01:00
|
|
|
stdcontext "context"
|
2019-02-23 12:41:52 +01:00
|
|
|
"fmt"
|
2024-01-07 16:18:03 +01:00
|
|
|
"runtime"
|
2019-02-23 12:41:52 +01:00
|
|
|
"runtime/debug"
|
2017-12-31 13:01:48 +01:00
|
|
|
"sync"
|
2020-08-28 17:24:30 +02:00
|
|
|
"sync/atomic"
|
2018-04-06 19:14:23 +02:00
|
|
|
|
2022-02-04 07:47:22 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
|
2020-10-12 17:39:45 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-03-21 16:02:37 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2023-10-06 06:49:42 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hook"
|
2016-05-18 20:17:50 +02:00
|
|
|
)
|
|
|
|
|
2018-04-06 19:14:23 +02:00
|
|
|
var (
|
2019-07-31 18:07:19 +02:00
|
|
|
// renderCh receives when updating starts.
|
2019-05-30 18:44:01 +02:00
|
|
|
renderCh = make(chan struct{})
|
|
|
|
|
|
|
|
// renderEndCh receives when updating finishes.
|
|
|
|
renderEndCh = make(chan struct{})
|
2022-03-21 15:00:50 +01:00
|
|
|
)
|
2019-05-30 18:44:01 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) init() error {
|
|
|
|
u.userInterfaceImpl = userInterfaceImpl{
|
2023-10-15 10:21:56 +02:00
|
|
|
graphicsLibraryInitCh: make(chan struct{}),
|
|
|
|
errCh: make(chan error),
|
2020-02-11 14:58:59 +01:00
|
|
|
|
|
|
|
// Give a default outside size so that the game can start without initializing them.
|
|
|
|
outsideWidth: 640,
|
|
|
|
outsideHeight: 480,
|
2020-01-21 17:58:59 +01:00
|
|
|
}
|
2024-04-29 12:02:23 +02:00
|
|
|
u.foreground.Store(true)
|
2023-10-14 17:06:07 +02:00
|
|
|
return nil
|
2019-04-07 03:42:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 05:43:58 +01:00
|
|
|
// Update is called from mobile/ebitenmobileview.
|
2020-08-28 17:18:42 +02:00
|
|
|
//
|
|
|
|
// Update must be called on the rendering thread.
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) Update() error {
|
2020-02-11 15:19:43 +01:00
|
|
|
select {
|
|
|
|
case err := <-u.errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-03-20 16:08:40 +01:00
|
|
|
if !u.IsFocused() {
|
2020-02-11 15:19:43 +01:00
|
|
|
return nil
|
2020-01-21 17:58:59 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:20:49 +02:00
|
|
|
if err := gamepad.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-16 17:32:50 +02:00
|
|
|
|
2022-12-28 09:03:49 +01:00
|
|
|
ctx, cancel := stdcontext.WithCancel(stdcontext.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-05-31 20:11:09 +02:00
|
|
|
renderCh <- struct{}{}
|
2020-08-29 19:10:49 +02:00
|
|
|
go func() {
|
|
|
|
<-renderEndCh
|
2022-12-28 09:03:49 +01:00
|
|
|
cancel()
|
2020-08-29 19:10:49 +02:00
|
|
|
}()
|
2022-12-28 09:03:49 +01:00
|
|
|
|
2024-01-02 08:54:07 +01:00
|
|
|
graphicscommand.LoopRenderThread(ctx)
|
2020-02-11 15:19:43 +01:00
|
|
|
return nil
|
2016-06-09 18:21:46 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
type userInterfaceImpl struct {
|
2023-10-15 10:21:56 +02:00
|
|
|
graphicsDriver graphicsdriver.Graphics
|
|
|
|
graphicsLibraryInitCh chan struct{}
|
2022-03-21 16:02:37 +01:00
|
|
|
|
2020-02-11 09:53:50 +01:00
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
2019-12-16 02:57:57 +01:00
|
|
|
|
2024-04-29 12:02:23 +02:00
|
|
|
foreground atomic.Bool
|
2022-02-13 18:17:45 +01:00
|
|
|
errCh chan error
|
2017-12-31 13:01:48 +01:00
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
context *context
|
2019-06-26 17:07:39 +02:00
|
|
|
|
2022-12-16 10:34:14 +01:00
|
|
|
inputState InputState
|
2023-09-17 14:48:42 +02:00
|
|
|
touches []TouchForInput
|
2019-04-07 11:55:56 +02:00
|
|
|
|
2024-04-29 14:50:08 +02:00
|
|
|
fpsMode atomic.Int32
|
2021-07-22 18:07:28 +02:00
|
|
|
renderRequester RenderRequester
|
|
|
|
|
2017-12-31 13:01:48 +01:00
|
|
|
m sync.RWMutex
|
2016-05-18 20:17:50 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetForeground(foreground bool) error {
|
2024-04-29 12:02:23 +02:00
|
|
|
u.foreground.Store(foreground)
|
2020-01-21 17:58:59 +01:00
|
|
|
|
|
|
|
if foreground {
|
2023-10-06 06:49:42 +02:00
|
|
|
return hook.ResumeAudio()
|
2020-01-21 17:58:59 +01:00
|
|
|
} else {
|
2023-10-06 06:49:42 +02:00
|
|
|
return hook.SuspendAudio()
|
2020-01-21 17:58:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) Run(game Game, options *RunOptions) error {
|
2024-01-07 16:18:03 +01:00
|
|
|
return fmt.Errorf("internal/ui: Run is not implemented for GOOS=%s", runtime.GOOS)
|
2019-04-10 03:44:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) RunWithoutMainLoop(game Game, options *RunOptions) {
|
2019-04-10 03:44:02 +02:00
|
|
|
go func() {
|
2024-01-07 16:18:03 +01:00
|
|
|
if err := u.runMobile(game, options); err != nil {
|
2020-02-11 15:19:43 +01:00
|
|
|
u.errCh <- err
|
2019-04-10 03:44:02 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:18:03 +01:00
|
|
|
func (u *UserInterface) runMobile(game Game, options *RunOptions) (err error) {
|
2019-02-23 12:41:52 +01:00
|
|
|
// Convert the panic to a regular error so that Java/Objective-C layer can treat this easily e.g., for
|
|
|
|
// Crashlytics. A panic is treated as SIGABRT, and there is no way to handle this on Java/Objective-C layer
|
|
|
|
// unfortunately.
|
|
|
|
// TODO: Panic on other goroutines cannot be handled here.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2021-01-01 18:08:34 +01:00
|
|
|
err = fmt.Errorf("%v\n%s", r, string(debug.Stack()))
|
2019-02-23 12:41:52 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-07 16:18:03 +01:00
|
|
|
graphicscommand.SetOSThreadAsRenderThread()
|
2018-03-23 17:07:36 +01:00
|
|
|
|
2023-10-27 04:01:01 +02:00
|
|
|
u.setRunning(true)
|
|
|
|
defer u.setRunning(false)
|
|
|
|
|
|
|
|
u.context = newContext(game)
|
|
|
|
|
2024-08-27 16:55:33 +02:00
|
|
|
g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{
|
|
|
|
colorSpace: options.ColorSpace,
|
|
|
|
}, options.GraphicsLibrary)
|
2022-11-13 06:51:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.graphicsDriver = g
|
2023-10-15 08:50:39 +02:00
|
|
|
u.setGraphicsLibrary(lib)
|
2023-10-15 10:21:56 +02:00
|
|
|
close(u.graphicsLibraryInitCh)
|
2022-11-13 06:51:45 +01:00
|
|
|
|
2016-08-31 19:38:47 +02:00
|
|
|
for {
|
2020-04-02 17:06:42 +02:00
|
|
|
if err := u.update(); err != nil {
|
2016-08-31 19:38:47 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-09-01 18:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 18:48:28 +01:00
|
|
|
// outsideSize must be called on the same goroutine as update().
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) outsideSize() (float64, float64) {
|
2020-08-28 20:02:20 +02:00
|
|
|
u.m.RLock()
|
2024-01-07 16:18:03 +01:00
|
|
|
defer u.m.RUnlock()
|
2017-12-31 13:01:48 +01:00
|
|
|
|
2024-01-07 16:18:03 +01:00
|
|
|
return u.outsideWidth, u.outsideHeight
|
2018-02-01 18:08:03 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) update() error {
|
2020-01-21 17:58:59 +01:00
|
|
|
<-renderCh
|
2018-02-01 18:08:03 +01:00
|
|
|
defer func() {
|
2019-05-30 18:44:01 +02:00
|
|
|
renderEndCh <- struct{}{}
|
2018-02-01 18:08:03 +01:00
|
|
|
}()
|
|
|
|
|
2022-02-13 18:48:28 +01:00
|
|
|
w, h := u.outsideSize()
|
2024-02-12 07:03:07 +01:00
|
|
|
if err := u.context.updateFrame(u.graphicsDriver, w, h, theMonitor.DeviceScaleFactor(), u); err != nil {
|
2016-09-01 18:34:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-05-18 20:17:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:53:50 +01:00
|
|
|
// SetOutsideSize is called from mobile/ebitenmobileview.
|
2020-08-28 17:18:42 +02:00
|
|
|
//
|
|
|
|
// SetOutsideSize is concurrent safe.
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) {
|
2017-12-31 13:01:48 +01:00
|
|
|
u.m.Lock()
|
2024-02-24 11:48:17 +01:00
|
|
|
defer u.m.Unlock()
|
2020-02-11 09:53:50 +01:00
|
|
|
if u.outsideWidth != outsideWidth || u.outsideHeight != outsideHeight {
|
|
|
|
u.outsideWidth = outsideWidth
|
|
|
|
u.outsideHeight = outsideHeight
|
2017-12-31 13:01:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) CursorMode() CursorMode {
|
2022-02-06 09:43:52 +01:00
|
|
|
return CursorModeHidden
|
2017-08-12 08:39:41 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetCursorMode(mode CursorMode) {
|
2016-09-03 10:17:54 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) CursorShape() CursorShape {
|
2022-02-06 09:43:52 +01:00
|
|
|
return CursorShapeDefault
|
2021-04-11 07:21:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetCursorShape(shape CursorShape) {
|
2021-04-11 07:21:38 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) IsFullscreen() bool {
|
2017-09-13 20:37:38 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetFullscreen(fullscreen bool) {
|
2017-06-29 17:35:34 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) IsFocused() bool {
|
2024-04-29 12:02:23 +02:00
|
|
|
return u.foreground.Load()
|
2020-01-16 02:47:23 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) IsRunnableOnUnfocused() bool {
|
2017-06-29 17:35:34 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
|
2017-08-02 16:37:50 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2023-10-15 09:10:13 +02:00
|
|
|
func (u *UserInterface) FPSMode() FPSModeType {
|
2024-04-29 14:50:08 +02:00
|
|
|
return FPSModeType(u.fpsMode.Load())
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 09:10:13 +02:00
|
|
|
func (u *UserInterface) SetFPSMode(mode FPSModeType) {
|
2024-04-29 14:50:08 +02:00
|
|
|
u.fpsMode.Store(int32(mode))
|
2023-10-15 09:10:13 +02:00
|
|
|
u.updateExplicitRenderingModeIfNeeded(mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) updateExplicitRenderingModeIfNeeded(fpsMode FPSModeType) {
|
2021-07-22 18:07:28 +02:00
|
|
|
if u.renderRequester == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-10-15 09:10:13 +02:00
|
|
|
u.renderRequester.SetExplicitRenderingMode(fpsMode == FPSModeVsyncOffMinimum)
|
2018-07-14 13:50:09 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) readInputState(inputState *InputState) {
|
2022-12-20 01:52:13 +01:00
|
|
|
u.m.Lock()
|
|
|
|
defer u.m.Unlock()
|
2023-01-21 16:04:30 +01:00
|
|
|
u.inputState.copyAndReset(inputState)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) Window() Window {
|
2022-05-31 18:26:28 +02:00
|
|
|
return &nullWindow{}
|
2019-12-24 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
2024-02-12 07:03:07 +01:00
|
|
|
type Monitor struct {
|
2024-02-24 11:48:17 +01:00
|
|
|
deviceScaleFactor float64
|
|
|
|
deviceScaleFactorOnce sync.Once
|
2024-02-12 07:03:07 +01:00
|
|
|
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
2023-08-30 14:02:04 +02:00
|
|
|
|
|
|
|
var theMonitor = &Monitor{}
|
|
|
|
|
|
|
|
func (m *Monitor) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-02-12 07:03:07 +01:00
|
|
|
func (m *Monitor) DeviceScaleFactor() float64 {
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
2024-02-24 11:48:17 +01:00
|
|
|
|
|
|
|
// The device scale factor can be obtained after the main function starts, especially on Android.
|
|
|
|
// Initialize this lazily.
|
|
|
|
m.deviceScaleFactorOnce.Do(func() {
|
|
|
|
// Assume that the device scale factor never changes on mobiles.
|
|
|
|
m.deviceScaleFactor = deviceScaleFactorImpl()
|
|
|
|
})
|
2024-02-12 07:03:07 +01:00
|
|
|
return m.deviceScaleFactor
|
|
|
|
}
|
|
|
|
|
2024-03-23 14:31:08 +01:00
|
|
|
func (m *Monitor) Size() (int, int) {
|
|
|
|
// TODO: Return a valid value.
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) AppendMonitors(mons []*Monitor) []*Monitor {
|
2023-08-30 14:02:04 +02:00
|
|
|
return append(mons, theMonitor)
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) Monitor() *Monitor {
|
2023-08-30 14:02:04 +02:00
|
|
|
return theMonitor
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
|
2023-09-17 07:22:58 +02:00
|
|
|
u.updateInputStateFromOutside(keys, runes, touches)
|
2024-04-29 14:50:08 +02:00
|
|
|
if FPSModeType(u.fpsMode.Load()) == FPSModeVsyncOffMinimum {
|
2021-10-16 13:37:01 +02:00
|
|
|
u.renderRequester.RequestRenderIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
type RenderRequester interface {
|
|
|
|
SetExplicitRenderingMode(explicitRendering bool)
|
|
|
|
RequestRenderIfNeeded()
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) {
|
2021-07-22 18:07:28 +02:00
|
|
|
u.renderRequester = renderRequester
|
2024-04-29 14:50:08 +02:00
|
|
|
u.updateExplicitRenderingModeIfNeeded(FPSModeType(u.fpsMode.Load()))
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) ScheduleFrame() {
|
2024-04-29 14:50:08 +02:00
|
|
|
if u.renderRequester != nil && FPSModeType(u.fpsMode.Load()) == FPSModeVsyncOffMinimum {
|
2021-07-22 18:07:28 +02:00
|
|
|
u.renderRequester.RequestRenderIfNeeded()
|
|
|
|
}
|
2019-04-07 11:28:50 +02:00
|
|
|
}
|
2022-09-25 17:34:11 +02:00
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (u *UserInterface) updateIconIfNeeded() error {
|
2023-04-19 17:56:08 +02:00
|
|
|
return nil
|
2022-12-29 15:48:53 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:23:41 +01:00
|
|
|
func IsScreenTransparentAvailable() bool {
|
|
|
|
return false
|
|
|
|
}
|