Skip to content

Commit

Permalink
Added a smooth version on mouse moves
Browse files Browse the repository at this point in the history
  • Loading branch information
howroyd committed Nov 2, 2023
1 parent 7a97daa commit af377d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 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
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
12 changes: 6 additions & 6 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 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.MoveMouseRelativeDirection(hidactions.MouseMoveDirectionSmoothActionConfig(distance, hidactions.MouseMoveDirection.UP))
lookdown = hidactions.MoveMouseRelativeDirection(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 af377d8

Please sign in to comment.