Skip to content

Commit

Permalink
Enhanced grids linux + complete Sudoku (#897)
Browse files Browse the repository at this point in the history
Co-authored-by: LexiconCode <CasterVoice@protonmail.com>
  • Loading branch information
kendonB and LexiconCode committed Jun 10, 2021
1 parent 7a4a11b commit 5d16e4c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 49 deletions.
18 changes: 12 additions & 6 deletions castervoice/asynch/mouse/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
finally:
from castervoice.lib import settings, utilities
from castervoice.lib.actions import Mouse
from castervoice.lib.contexts import is_linux
from castervoice.lib.merge.communication import Communicator
settings.initialize()

if is_linux():
from tkinter import ttk,font

try:
from PIL import ImageGrab, ImageTk, ImageDraw, ImageFont
except ImportError:
Expand Down Expand Up @@ -100,10 +105,6 @@ def unhide(self):
''''''
self.deiconify()
self.lift()
time.sleep(0.1)
self.focus_force()
self.focus_set()
self.focus()

def hide(self):
self.withdraw()
Expand Down Expand Up @@ -147,7 +148,9 @@ def __init__(self, grid_size=None, square_size=None, square_alpha=None):

def refresh(self):
'''thread safe'''
self.hide()
if not sys.platform.startswith("linux"):
# When the grid is hidden on Linux it fails to draw on the correct monitor
self.hide()
self.after(10, self.draw)

def finalize(self):
Expand Down Expand Up @@ -197,7 +200,10 @@ def draw_squares(self):
ys_size = len(self.ys)
box_number = 0
colors_index = 0
font = ImageFont.truetype("arialbd.ttf", 15)
if is_linux():
font = ImageFont.truetype("FreeMono.ttf", 15)
else:
font = ImageFont.truetype("arialbd.ttf", 15)
draw = ImageDraw.Draw(self.img, 'RGBA')

for ly in range(0, ys_size - 1):
Expand Down
4 changes: 2 additions & 2 deletions castervoice/lib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def _get_platform_information():
else:
system_information.update({"binary path": str(Path(sys.exec_prefix).joinpath(sys.exec_prefix).joinpath("bin"))})
system_information.update(
{"main binary": str(Path(sys.exec_prefix).joinpath("bin", "python"))})
{"main binary": sys.executable})
system_information.update(
{"hidden console binary": str(Path(sys.exec_prefix).joinpath("bin", "python"))})
{"hidden console binary": sys.executable})
return system_information


Expand Down
20 changes: 9 additions & 11 deletions castervoice/rules/apps/mouse_grids/griddouglas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ def send_input(x, y, action):
s = control.nexus().comm.get_com("grids")
s.move_mouse(int(x), int(y))
int_a = int(action)
if (int_a == 0) | (int_a == 1) | (int_a == -1):
if (int_a == 0) | (int_a == 1) | (int_a == 2) | (int_a == -1):
s.kill()
Grid.wait_for_grid_exit()
if int_a == 0:
Mouse("left").execute()
elif int_a == 1:
if int_a == 1:
Mouse("left:2").execute()
elif int_a == 2:
Mouse("right").execute()


Expand Down Expand Up @@ -74,9 +76,9 @@ class DouglasGridRule(MappingRule):
R(Function(send_input_select)),
"<x1> [by] <y1> (grab | select) <x2>":
R(Function(send_input_select_short)),
"squat":
"squat {weight=2}":
R(Function(store_first_point)),
"bench":
"bench {weight=2}":
R(Function(select_text)),
SymbolSpecs.CANCEL + " {weight=2}":
R(Function(Grid.kill)),
Expand All @@ -90,18 +92,14 @@ class DouglasGridRule(MappingRule):
ShortIntegerRef("y2", 0, 300),
Choice("action", {
"kick": 0,
"psychic": 1,
"move": 2,
}),
Choice("point", {
"one": 1,
"two": 2,
"kick (double | 2)": 1,
"psychic": 2,
"move": 3,
}),
]
defaults = {
"action": -1,
}


def get_rule():
return DouglasGridRule, RuleDetails(name="douglas grid rule", function_context = lambda: Grid.is_grid_active("douglas"))
2 changes: 1 addition & 1 deletion castervoice/rules/apps/mouse_grids/gridlegion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from dragonfly import Function, Choice, MappingRule, Mouse, ShortIntegerRef
from castervoice.lib import control, navigation
from castervoice.lib import control
from castervoice.lib.navigation import Grid
from castervoice.lib.ctrl.mgr.rule_details import RuleDetails
from castervoice.lib.merge.state.short import R
Expand Down
24 changes: 11 additions & 13 deletions castervoice/rules/apps/mouse_grids/gridrainbow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def send_input(pre, color, n, action):
s = control.nexus().comm.get_com("grids")
s.move_mouse(int(pre), int(color), int(n))
int_a = int(action)
if (int_a == 0) | (int_a == 1) | (int_a == -1):
if (int_a == 0) | (int_a == 1) | (int_a == 2) | (int_a == -1):
s.kill()
Grid.wait_for_grid_exit()
time.sleep(0.1)
if int_a == 0:
Mouse("left").execute()
elif int_a == 1:
if int_a == 1:
Mouse("left:2").execute()
elif int_a == 2:
Mouse("right").execute()


Expand Down Expand Up @@ -72,13 +74,13 @@ class RainbowGridRule(MappingRule):
mapping = {
"[<pre>] <color> <n> [<action>]":
R(Function(send_input)),
"[<pre1>] <color1> <n1> select [<pre2>] <color2> <n2>":
"[<pre1>] <color1> <n1> (grab | select) [<pre2>] <color2> <n2>":
R(Function(send_input_select)),
"[<pre1>] <color1> <n1> select <n2>":
"[<pre1>] <color1> <n1> (grab | select) <n2>":
R(Function(send_input_select_short)),
"squat":
"squat {weight=2}":
R(Function(store_first_point)),
"bench":
"bench {weight=2}":
R(Function(select_text)),
SymbolSpecs.CANCEL + " {weight=2}":
R(Function(Grid.kill)),
Expand Down Expand Up @@ -119,12 +121,9 @@ class RainbowGridRule(MappingRule):
ShortIntegerRef("n2", 0, 100),
Choice("action", {
"kick": 0,
"psychic": 1,
"move": 2,
}),
Choice("point", {
"one": 1,
"two": 2,
"kick (double | 2)": 1,
"psychic": 2,
"move": 3,
}),
]
defaults = {
Expand All @@ -134,6 +133,5 @@ class RainbowGridRule(MappingRule):
"action": -1,
}


def get_rule():
return RainbowGridRule, RuleDetails(name="rainbow grid rule", function_context=lambda: Grid.is_grid_active("rainbow"))
57 changes: 43 additions & 14 deletions castervoice/rules/apps/mouse_grids/gridsudoku.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from dragonfly import Function, Choice, MappingRule, ShortIntegerRef
from dragonfly.actions.mouse import get_cursor_position
from castervoice.lib import control
from castervoice.lib.navigation import Grid
from castervoice.lib.actions import Mouse
Expand All @@ -11,11 +12,11 @@
# Perform an action based on the passed in action number
# action - optional mouse action after movement
def perform_mouse_action(action):
if action == 1:
if action == 0:
Mouse("left").execute()
if action == 2:
if action == 1:
Mouse("left:2").execute()
elif action == 3:
elif action == 2:
Mouse("right").execute()


Expand All @@ -26,12 +27,14 @@ def perform_mouse_action(action):
def move_mouse(n, s, action):
sudoku = control.nexus().comm.get_com("grids")
sudoku.move_mouse(int(n), int(s))
sudoku.kill()
Grid.wait_for_grid_exit()
int_a = int(action)
if (int_a == 0) | (int_a == 1) | (int_a == 2) | (int_a == -1):
sudoku.kill()
Grid.wait_for_grid_exit()

time.sleep(0.1)
perform_mouse_action(int(action))


# Command to drag the mouse from the current position
# n0 - optional square to drag from
# s0 - optional inner square to drag from
Expand All @@ -40,7 +43,9 @@ def move_mouse(n, s, action):
# action - optional mouse action after movement
def drag_mouse(n0, s0, n, s, action):
sudoku = control.nexus().comm.get_com("grids")
# These numbers are internal to the monitor the screen is on
x, y = sudoku.get_mouse_pos(int(n), int(s))

# If dragging from a different location, move there first
if int(n0) > 0:
sudoku.move_mouse(int(n0), int(s0))
Expand All @@ -54,6 +59,27 @@ def drag_mouse(n0, s0, n, s, action):
Mouse("left:up/30").execute()
perform_mouse_action(int(action))

# This is used for the fine movement dragging
def drag_from_to(x1, y1, x2, y2):
Mouse("[{}, {}]".format(x1, y1)).execute()
time.sleep(0.5)
Mouse("left:down").execute()
time.sleep(0.5)
Mouse("[{}, {}]".format(x2, y2)).execute()
time.sleep(0.5)
Mouse("left:up").execute()

def store_first_point():
global x1, y1
x1, y1 = get_cursor_position()

def select_text():
global x1, y1, x2, y2
x2, y2 = get_cursor_position()
s = control.nexus().comm.get_com("grids")
s.kill()
Grid.wait_for_grid_exit()
drag_from_to(x1, y1, x2, y2)

'''
Rules for sudoku grid. We can either move the mouse or drag it.
Expand All @@ -68,9 +94,13 @@ class SudokuGridRule(MappingRule):
mapping = {
"<n> [grid <s>] [<action>]":
R(Function(move_mouse)),
"[<n0>] [grid <s0>] drag <n> [grid <s>] [<action>]":
"[<n0>] [grid <s0>] (grab | select | drag) <n> [grid <s>] [<action>]":
R(Function(drag_mouse)),
SymbolSpecs.CANCEL + " {weight=2}":
"squat {weight=2}":
R(Function(store_first_point)),
"bench {weight=2}":
R(Function(select_text)),
SymbolSpecs.CANCEL + "{weight=2}":
R(Function(Grid.kill)),
}
extras = [
Expand All @@ -79,21 +109,20 @@ class SudokuGridRule(MappingRule):
ShortIntegerRef("s", 0, 10),
ShortIntegerRef("s0", 0, 10),
Choice("action", {
"move": 0,
"kick": 1,
"kick (double | 2)": 2,
"psychic": 3,
"kick": 0,
"kick (double | 2)": 1,
"psychic": 2,
"move": 3,
}),
]
defaults = {
"n": 0,
"n0": 0,
"s": 0,
"s0": 0,
"action": 0,
"action": -1,
}


def get_rule():
Details = RuleDetails(name="Sudoku Grid", function_context=lambda: Grid.is_grid_active("sudoku"))
return SudokuGridRule, Details
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dragonfly2>=0.29.0
wxpython==4.0.7
pillow>=6.2.2
pillow>=8.0.0
tomlkit>=0.5.11
future>=0.18.2
mock>=3.0.5
Expand Down
2 changes: 1 addition & 1 deletion requirements-mac-linux.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dragonfly2[kaldi]>=0.29.0
pillow>=6.2.2
pillow>=8.0.0
tomlkit>=0.5.11
future>=0.18.2
mock>=3.0.5
Expand Down

0 comments on commit 5d16e4c

Please sign in to comment.