1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-09 18:17:07 -05:00
OpenDiablo2/d2core/d2systems/scene_mouse_cursor.go

157 lines
2.9 KiB
Go
Raw Normal View History

package d2systems
import (
"math"
"time"
"github.com/gravestench/akara"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
)
const (
sceneKeyMouseCursor = "Mouse Cursor"
)
const (
fadeTimeout = time.Second * 4
fadeTime = time.Second
)
func NewMouseCursorScene() *MouseCursorScene {
scene := &MouseCursorScene{
BaseScene: NewBaseScene(sceneKeyMouseCursor),
}
return scene
}
// static check that MouseCursorScene implements the scene interface
var _ d2interface.Scene = &MouseCursorScene{}
type MouseCursorScene struct {
*BaseScene
booted bool
cursor akara.EID
lastTimeMoved time.Time
2020-12-04 03:11:19 -05:00
debug struct {
enabled bool
}
test bool
}
func (s *MouseCursorScene) Init(world *akara.World) {
s.World = world
changes to d2components, d2systems, d2ui, d2enum go.mod, go.sum: * updating akara, bugfix in akara.EntityManager.RemoveEntity d2core * adding d2core/d2label * adding d2core/d2bitmapfont d2ui * exporting some constants for use elsewhere d2components * added bitmap font component (for ui labels) * added FileLoaded tag component to simplify asset loading filters * added locale component * FilePath component renamed to File * sprite component now contains the sprite and palette path as strings * adding ui label component d2enum * added locale as file type for file "/data/local/use" d2systems * changed most info prints to debug prints * removed unused scene graph testing file (oops!) * terminal is now rendered above mouse cursor scene * adding ui widget system for use by the game object factory * adding test scene for ui labels created with the ui widget system d2systems/AppBootstrap * added command line args for profiler * `--testscene labels` launches the label test * now adds the local file for processing * game loop init logic now inside of Init method (the call to world.Update does this) d2systems/AssetLoader * loads the locale file and adds a locale component that other systems can use * adds a FileLoaded component after finished loading a file which other systems can use (like the loading scene) d2systems/FileSourceResolver * Now looks for and uses the locale for language/charset filepath substitution d2systems/GameClientBootstrap * game loop init moved to end of AppBootstrap.Init d2systems/GameObjectFactory * embedding UI widget factory system d2systems/BaseScene * made base scene a little more clear by breaking the process into more methods d2systems/LoadingScene * simplified the entity subscriptions by using the new FileLoaded component d2systems/SceneObjectFactory * adding method for adding labels, buttons to scenes (buttons still WIP) d2systems/SceneSpriteSystem * the sprite system now maintains a cache of rendered sprites
2020-12-08 13:42:19 -05:00
s.Debug("initializing ...")
}
func (s *MouseCursorScene) boot() {
if !s.BaseScene.booted {
s.BaseScene.boot()
return
}
2020-12-04 03:11:19 -05:00
s.registerTerminalCommands()
s.createMouseCursor()
s.booted = true
}
func (s *MouseCursorScene) createMouseCursor() {
changes to d2components, d2systems, d2ui, d2enum go.mod, go.sum: * updating akara, bugfix in akara.EntityManager.RemoveEntity d2core * adding d2core/d2label * adding d2core/d2bitmapfont d2ui * exporting some constants for use elsewhere d2components * added bitmap font component (for ui labels) * added FileLoaded tag component to simplify asset loading filters * added locale component * FilePath component renamed to File * sprite component now contains the sprite and palette path as strings * adding ui label component d2enum * added locale as file type for file "/data/local/use" d2systems * changed most info prints to debug prints * removed unused scene graph testing file (oops!) * terminal is now rendered above mouse cursor scene * adding ui widget system for use by the game object factory * adding test scene for ui labels created with the ui widget system d2systems/AppBootstrap * added command line args for profiler * `--testscene labels` launches the label test * now adds the local file for processing * game loop init logic now inside of Init method (the call to world.Update does this) d2systems/AssetLoader * loads the locale file and adds a locale component that other systems can use * adds a FileLoaded component after finished loading a file which other systems can use (like the loading scene) d2systems/FileSourceResolver * Now looks for and uses the locale for language/charset filepath substitution d2systems/GameClientBootstrap * game loop init moved to end of AppBootstrap.Init d2systems/GameObjectFactory * embedding UI widget factory system d2systems/BaseScene * made base scene a little more clear by breaking the process into more methods d2systems/LoadingScene * simplified the entity subscriptions by using the new FileLoaded component d2systems/SceneObjectFactory * adding method for adding labels, buttons to scenes (buttons still WIP) d2systems/SceneSpriteSystem * the sprite system now maintains a cache of rendered sprites
2020-12-08 13:42:19 -05:00
s.Debug("creating mouse cursor")
s.cursor = s.Add.Sprite(0, 0, d2resource.CursorDefault, d2resource.PaletteUnits)
}
func (s *MouseCursorScene) Update() {
for _, id := range s.Viewports {
s.Components.Priority.Add(id).Priority = scenePriorityMouseCursor
}
if s.Paused() {
return
}
if !s.booted {
s.boot()
}
s.updateCursorTransform()
s.handleCursorFade()
s.BaseScene.Update()
}
func (s *MouseCursorScene) updateCursorTransform() {
transform, found := s.Components.Transform.Get(s.cursor)
if !found {
return
}
2020-12-13 17:41:19 -05:00
node, _ := s.Components.SceneGraphNode.Get(s.cursor)
_ = node
cx, cy := s.Input.CursorPosition()
tx, ty := transform.Translation.XY()
if int(tx) != cx || int(ty) != cy {
s.lastTimeMoved = time.Now()
2020-12-04 03:11:19 -05:00
switch s.debug.enabled {
case true:
s.Infof("transform: (%d, %d)", int(tx), int(ty))
2020-12-04 03:11:19 -05:00
default:
s.Debugf("transform: (%d, %d)", int(tx), int(ty))
2020-12-04 03:11:19 -05:00
}
}
transform.Translation.X, transform.Translation.Y = float64(cx), float64(cy)
}
func (s *MouseCursorScene) handleCursorFade() {
alpha, found := s.Components.Alpha.Get(s.cursor)
if !found {
return
}
shouldFadeOut := time.Now().Sub(s.lastTimeMoved) > fadeTimeout
if shouldFadeOut {
alpha.Alpha = math.Max(alpha.Alpha*0.825, 0)
} else {
alpha.Alpha = math.Min(alpha.Alpha+0.125, 1)
}
2020-12-04 03:11:19 -05:00
if alpha.Alpha > 1e-1 && alpha.Alpha < 1 {
switch s.debug.enabled {
case true:
s.Infof("fading %.2f", alpha.Alpha)
default:
s.Debugf("fading %.2f", alpha.Alpha)
}
}
}
func (s *MouseCursorScene) registerTerminalCommands() {
s.registerDebugCommand()
}
func (s *MouseCursorScene) registerDebugCommand() {
changes to d2components, d2systems, d2ui, d2enum go.mod, go.sum: * updating akara, bugfix in akara.EntityManager.RemoveEntity d2core * adding d2core/d2label * adding d2core/d2bitmapfont d2ui * exporting some constants for use elsewhere d2components * added bitmap font component (for ui labels) * added FileLoaded tag component to simplify asset loading filters * added locale component * FilePath component renamed to File * sprite component now contains the sprite and palette path as strings * adding ui label component d2enum * added locale as file type for file "/data/local/use" d2systems * changed most info prints to debug prints * removed unused scene graph testing file (oops!) * terminal is now rendered above mouse cursor scene * adding ui widget system for use by the game object factory * adding test scene for ui labels created with the ui widget system d2systems/AppBootstrap * added command line args for profiler * `--testscene labels` launches the label test * now adds the local file for processing * game loop init logic now inside of Init method (the call to world.Update does this) d2systems/AssetLoader * loads the locale file and adds a locale component that other systems can use * adds a FileLoaded component after finished loading a file which other systems can use (like the loading scene) d2systems/FileSourceResolver * Now looks for and uses the locale for language/charset filepath substitution d2systems/GameClientBootstrap * game loop init moved to end of AppBootstrap.Init d2systems/GameObjectFactory * embedding UI widget factory system d2systems/BaseScene * made base scene a little more clear by breaking the process into more methods d2systems/LoadingScene * simplified the entity subscriptions by using the new FileLoaded component d2systems/SceneObjectFactory * adding method for adding labels, buttons to scenes (buttons still WIP) d2systems/SceneSpriteSystem * the sprite system now maintains a cache of rendered sprites
2020-12-08 13:42:19 -05:00
s.Debug("registering debug command")
2020-12-04 03:11:19 -05:00
const (
command = "debug_mouse"
description = "show debug information about the mouse"
)
s.RegisterTerminalCommand(command, description, func(val bool) {
s.setDebug(val)
})
}
func (s *MouseCursorScene) setDebug(val bool) {
s.debug.enabled = val
}