Skip to content

Commit

Permalink
52 look and headbang smoothness (#55)
Browse files Browse the repository at this point in the history
* Added a smooth version on mouse moves

* Implemented smooth movement for look and peek
  • Loading branch information
howroyd authored Nov 11, 2023
1 parent 7a97daa commit cb1f9e6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "simonsays_drgreengiant"
version = "2.0.1"
version = "2.0.2"
authors = [{ name = "Simon Howroyd", email = "howroydlsu@gmail.com" }]
description = "A Twitch Plays style programme to allow users in Twitch chats to control the broadcasters mouse and keyboard"
keywords = ["twitch", "chat", "twitchplays", "troll"]
Expand Down
2 changes: 2 additions & 0 deletions src/simonsays_drgreengiant/gameactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def run(self, *, force: bool = False) -> errorcodes.ErrorSet:
return hidactions.MoveMouseRelative(actionconfig.hidconfig).run(force=force)
elif isinstance(actionconfig.hidconfig, hidactions.MouseMoveDirectionActionConfig):
return hidactions.MoveMouseRelativeDirection(actionconfig.hidconfig).run(force=force)
elif isinstance(actionconfig.hidconfig, hidactions.MouseMoveDirectionSmoothActionConfig):
return hidactions.MoveMouseRelativeDirectionSmooth(actionconfig.hidconfig).run(force=force)
else:
return errorcodes.errorset(errorcodes.ErrorCode.NOT_IMPLEMENTED)

Expand Down
55 changes: 54 additions & 1 deletion src/simonsays_drgreengiant/hidactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ class MouseMoveCartesianActionConfig:
device: HidType = HidType.MOUSE_MOVE


@dataclasses.dataclass(slots=True)
class MouseMoveCartesianSmoothActionConfig:
"""An action to move the mouse in cartesian coordinates in a smooth way"""
x: int
y: int
repeats: int = 10
pause: float = 0.01
device: HidType = HidType.MOUSE_MOVE


class MouseMoveDirectionUnknown(Exception):
"""Unknown mouse move direction"""
pass
Expand Down Expand Up @@ -172,7 +182,17 @@ class MouseMoveDirectionActionConfig:
device: HidType = HidType.MOUSE_MOVE


Config = KeyboardActionConfig | MouseButtonActionConfig | MouseMoveCartesianActionConfig | MouseMoveDirectionActionConfig
@dataclasses.dataclass(slots=True)
class MouseMoveDirectionSmoothActionConfig:
"""An action to move the mouse smoothly in a direction"""
distance: int
direction: MouseMoveDirection
repeats: int = 10
pause: float = 0.01
device: HidType = HidType.MOUSE_MOVE


Config = KeyboardActionConfig | MouseButtonActionConfig | MouseMoveCartesianActionConfig | MouseMoveCartesianSmoothActionConfig | MouseMoveDirectionActionConfig | MouseMoveDirectionSmoothActionConfig

#####################################################################

Expand Down Expand Up @@ -244,6 +264,22 @@ def run(self, *, force: bool = False) -> errorcodes.ErrorSet:
return dummy_run(f"Moving mouse (relative) by: {self.config.x}, {self.config.y}")


@dataclasses.dataclass(slots=True)
class MoveMouseRelativeSmooth:
"""Move the mouse relative to its current position"""
config: MouseMoveCartesianSmoothActionConfig

def run(self, *, force: bool = False) -> errorcodes.ErrorSet:
"""Run the action"""
if not DEBUG:
x = self.config.x // self.config.repeats
y = self.config.y // self.config.repeats
look_action = MoveMouseRelative(MouseMoveCartesianActionConfig(x, y))

return actions.ActionRepeatWithWait(look_action, self.config.repeats, actions.Wait(self.config.pause)).run(force=force)
return dummy_run(f"Moving mouse smoothly (relative) by: {self.config.x}, {self.config.y} in {self.config.repeats} repeats with {self.config.pause} pause")


@dataclasses.dataclass(slots=True)
class MoveMouseRelativeDirection:
"""Move the mouse relative to its current position in a direction"""
Expand All @@ -256,6 +292,23 @@ def run(self, *, force: bool = False) -> errorcodes.ErrorSet:
return dummy_run(f"Moving mouse (relative direction) by: {self.config.distance} in direction {self.config.direction.value}")


@dataclasses.dataclass(slots=True)
class MoveMouseRelativeDirectionSmooth:
"""Move the mouse relative to its current position, smoothly in a direction"""
config: MouseMoveDirectionSmoothActionConfig

def run(self, *, force: bool = False) -> errorcodes.ErrorSet:
"""Run the action"""
if not DEBUG:
x, y = MouseMoveDirection.to_cartesian(self.config.direction, self.config.distance)
x = x // self.config.repeats
y = y // self.config.repeats
look_action = MoveMouseRelative(MouseMoveCartesianActionConfig(x, y))

return actions.ActionRepeatWithWait(look_action, self.config.repeats, actions.Wait(self.config.pause)).run(force=force)
return dummy_run(f"Moving mouse smoothly (relative direction) by: {self.config.distance} in directino {self.config.direction.value} repeats with {self.config.pause} pause")


@dataclasses.dataclass(slots=True)
class PressKey:
"""Press a key"""
Expand Down
20 changes: 10 additions & 10 deletions src/simonsays_drgreengiant/phasmoactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class LookUp(GenericAction):
@dataclasses.dataclass(slots=True)
class LookUpConfig(LookConfig):
"""Look up config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.UP))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.UP))


@dataclasses.dataclass(slots=True)
Expand All @@ -363,7 +363,7 @@ class LookDown(GenericAction):
@dataclasses.dataclass(slots=True)
class LookDownConfig(LookConfig):
"""Look down config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.DOWN))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.DOWN))


@dataclasses.dataclass(slots=True)
Expand All @@ -376,7 +376,7 @@ class LookLeft(GenericAction):
@dataclasses.dataclass(slots=True)
class LookLeftConfig(LookConfig):
"""Look left config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.LEFT))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.LEFT))


@dataclasses.dataclass(slots=True)
Expand All @@ -389,7 +389,7 @@ class LookRight(GenericAction):
@dataclasses.dataclass(slots=True)
class LookRightConfig(LookConfig):
"""Look right config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.RIGHT))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.LOOK_DISTANCE, hidactions.MouseMoveDirection.RIGHT))


PeekConfig = LookConfig
Expand All @@ -405,7 +405,7 @@ class PeekUp(GenericAction):
@dataclasses.dataclass(slots=True)
class PeekUpConfig(PeekConfig):
"""Peek up config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.UP))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.UP))


@dataclasses.dataclass(slots=True)
Expand All @@ -418,7 +418,7 @@ class PeekDown(GenericAction):
@dataclasses.dataclass(slots=True)
class PeekDownConfig(PeekConfig):
"""Peek down config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.DOWN))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.DOWN))


@dataclasses.dataclass(slots=True)
Expand All @@ -431,7 +431,7 @@ class PeekLeft(GenericAction):
@dataclasses.dataclass(slots=True)
class PeekLeftConfig(PeekConfig):
"""Peek left config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.LEFT))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.LEFT))


@dataclasses.dataclass(slots=True)
Expand All @@ -444,7 +444,7 @@ class PeekRight(GenericAction):
@dataclasses.dataclass(slots=True)
class PeekRightConfig(PeekConfig):
"""Peek right config"""
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.RIGHT))
hidconfig: hidactions.Config = dataclasses.field(default_factory=lambda: hidactions.MouseMoveDirectionSmoothActionConfig(DEFAULTS.PEEK_DISTANCE, hidactions.MouseMoveDirection.RIGHT))

#####################################################################

Expand Down Expand Up @@ -734,8 +734,8 @@ def run(self, *, force: bool = False) -> errorcodes.ErrorSet:

distance = actionconfig.distance or DEFAULTS.PEEK_DISTANCE

lookup = hidactions.MoveMouseRelativeDirection(hidactions.MouseMoveDirectionActionConfig(distance, hidactions.MouseMoveDirection.UP))
lookdown = hidactions.MoveMouseRelativeDirection(hidactions.MouseMoveDirectionActionConfig(distance, hidactions.MouseMoveDirection.DOWN))
lookup = hidactions.MoveMouseRelativeDirectionSmooth(hidactions.MouseMoveDirectionSmoothActionConfig(distance, hidactions.MouseMoveDirection.UP))
lookdown = hidactions.MoveMouseRelativeDirectionSmooth(hidactions.MouseMoveDirectionSmoothActionConfig(distance, hidactions.MouseMoveDirection.DOWN))

once = actions.ActionSequence([lookup, actions.Wait(pause), lookdown])
return actions.ActionRepeatWithWait(once, repeats, actions.Wait(pause)).run(force=force)
Expand Down

0 comments on commit cb1f9e6

Please sign in to comment.