Skip to content

Commit

Permalink
Merge pull request #3 from chaosparrot/feature/parrot_modes
Browse files Browse the repository at this point in the history
Feature/parrot modes
  • Loading branch information
chaosparrot authored Aug 21, 2021
2 parents 383bab2 + d487bc9 commit dc45b9f
Show file tree
Hide file tree
Showing 70 changed files with 2,748 additions and 351 deletions.
244 changes: 132 additions & 112 deletions README.md

Large diffs are not rendered by default.

73 changes: 64 additions & 9 deletions base_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from abc import ABCMeta
import numpy
from user.talon_hud.widget_preferences import HeadUpDisplayUserWidgetPreferences
from user.talon_hud.content.typing import HudButton
import copy

class BaseWidget(metaclass=ABCMeta):
id = None
Expand All @@ -12,12 +14,20 @@ class BaseWidget(metaclass=ABCMeta):
theme = None
preferences = None

# Enables receiving of mouse events - Note, this blocks all the mouse events on the canvas so make sure the canvas stays as small as possible to avoid 'dead areas'
mouse_enabled = False

# Position dragging position offset - Used in manual dragging
drag_position = []

# Context menu buttons if applicable
buttons = []

allowed_setup_options = ["position", "dimension", "limit", "font_size"]
subscribed_content = ['mode']
subscribed_logs = []
subscribed_topics = []
topic = ''

content = {}

animation_tick = 0
Expand All @@ -27,10 +37,16 @@ class BaseWidget(metaclass=ABCMeta):
setup_vertical_direction = ""
setup_horizontal_direction = ""

def __init__(self, id, preferences_dict, theme):
def __init__(self, id, preferences_dict, theme, subscriptions = None):
self.id = id
self.theme = theme
self.preferences = copy.copy(self.preferences)

self.load(preferences_dict)
if subscriptions != None:
self.topic = subscriptions['current_topic'] if 'current_topic' in subscriptions else ''
self.subscribed_topics = subscriptions['topics'] if 'topics' in subscriptions else self.subscribed_topics
self.subscribed_logs = subscriptions['logs'] if 'logs' in subscriptions else self.subscribed_logs

# Load the widgets preferences
def load(self, dict, initialize = True):
Expand All @@ -43,28 +59,34 @@ def load(self, dict, initialize = True):
self.height = self.preferences.height
self.limit_x = self.preferences.limit_x
self.limit_y = self.preferences.limit_y
self.limit_width = self.preferences.limit_width
self.limit_width = self.preferences.limit_width
self.limit_height = self.preferences.limit_height
self.font_size = self.preferences.font_size
self.alignment = self.preferences.alignment
self.expand_direction = self.preferences.expand_direction
self.minimized = self.preferences.minimized

if initialize:
self.load_theme_values()

if (initialize and self.preferences.enabled and ('enabled' in dict and dict['enabled'])):
self.enable()

# Set the topic that has claimed this widget
def set_topic(self, topic:str):

self.topic = topic

def set_theme(self, theme):
self.theme = theme
self.load_theme_values()
if self.enabled:
self.load_theme_values()
self.canvas.resume()
self.animation_tick = self.animation_max_duration if self.show_animations else 0

def update_content(self, content):
if not self.sleep_enabled and "mode" in content:
if (content["mode"] == "sleep"):
if (content["mode"] == "sleep"):
self.disable()
elif self.preferences.enabled == True:
self.enable()
Expand All @@ -75,6 +97,19 @@ def update_content(self, content):

if self.enabled:
self.canvas.resume()

def update_panel(self, panel_content) -> bool:
if not panel_content.content[0] and self.enabled:
self.disable()

if not self.enabled and panel_content.show:
self.enable()

if self.enabled:
self.panel_content = panel_content
self.topic = panel_content.topic
self.canvas.resume()
return self.enabled and panel_content.topic

def enable(self, persisted=False):
if not self.enabled:
Expand Down Expand Up @@ -148,7 +183,9 @@ def draw_cycle(self, canvas):
continue_drawing = self.draw(canvas)

if not continue_drawing:
self.canvas.pause()
if self.canvas:
self.canvas.pause()

self.animation_tick = 0
if not self.enabled:
self.clear()
Expand Down Expand Up @@ -184,7 +221,19 @@ def draw_setup_mode(self, canvas) -> skia.Paint:

def on_mouse(self, event):
"""This is where the mouse events get sent if mouse_enabled is set to True"""
pass
# Mouse dragging of elements that have mouse enabled
if event.button == 0:
actions.user.hide_context_menu()
if len(self.drag_position) == 0 and event.event == "mousedown":
self.drag_position = [event.gpos.x - self.limit_x, event.gpos.y - self.limit_y]
elif event.event == "mouseup" and len(self.drag_position) > 0:
self.drag_position = []
self.start_setup("")
if len(self.drag_position) > 0 and event.event == "mousemove":
if self.setup_type != "position":
self.start_setup("position")
else:
self.setup_move(event.gpos)

def draw(self, canvas) -> bool:
"""Implement your canvas drawing logic here, returning False will stop the rendering, returning True will continue it"""
Expand All @@ -206,7 +255,6 @@ def start_setup(self, setup_type):
"""Starts a setup mode that is used for moving, resizing and other various changes that the user might setup"""
if (setup_type not in self.allowed_setup_options and setup_type not in ["", "cancel"] ):
return

# Persist the user preferences when we end our setup
if (self.setup_type != "" and not setup_type):
rect = self.canvas.get_rect()
Expand Down Expand Up @@ -273,6 +321,10 @@ def setup_move(self, pos):
"""Responds to global mouse movements when a widget is in a setup mode"""
if (self.setup_type == "position"):
x, y = pos
if len(self.drag_position) > 0:
x = x - self.drag_position[0]
y = y - self.drag_position[1]

horizontal_diff = x - self.limit_x
vertical_diff = y - self.limit_y
self.limit_x = x
Expand Down Expand Up @@ -332,4 +384,7 @@ def setup_move(self, pos):
scale_multiplier = 0.033
self.font_size = max(8, int(total_distance * scale_multiplier ))
self.canvas.resume()


def click_button(self, button_index):
if button_index > -1 and button_index < len(self.buttons):
self.buttons[button_index].callback(self)
11 changes: 11 additions & 0 deletions choice_commands.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tag: user.talon_hud_available
tag: user.talon_hud_visible
tag: user.talon_hud_choices_visible
-
[option] {user.talon_hud_choices}: user.hud_activate_choice(talon_hud_choices)
option {user.talon_hud_numerical_choices}+$: user.hud_activate_choices(talon_hud_numerical_choices_list)

# This allows for the text 'option one five four confirm' to make quick confirmation possible
option {user.talon_hud_numerical_choices}+ {user.talon_hud_choices}$:
user.hud_activate_choices(talon_hud_numerical_choices_list)
user.hud_activate_choice(talon_hud_choices)
49 changes: 26 additions & 23 deletions commands.talon
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
tag: user.talon_hud_available
-
# General HUD commands
^head up (show|open)$: user.enable_hud()
^head up theme dark$: user.switch_hud_theme("dark")
^head up theme light$: user.switch_hud_theme("light")
^head up (drop|stop|confirm)$: user.set_hud_setup_mode("*", "")
^head up cancel$: user.set_hud_setup_mode("*", "cancel")
^head up (hide|close)$: user.disable_hud()

# Status bar commands
^head up (show|open) status bar$: user.enable_hud_id("status_bar")
^head up (hide|close) status bar$: user.disable_hud_id("status_bar")
^head up resize status bar$: user.set_hud_setup_mode("status_bar", "dimension")
^head up drag status bar$: user.set_hud_setup_mode("status_bar", "position")
^head up basic status bar$: user.set_widget_preference("status_bar", "show_animations", 0)
^head up fancy status bar$: user.set_widget_preference("status_bar", "show_animations", 1)
^head up text scale status bar$: user.set_hud_setup_mode("status_bar", "font_size")
^head up hide status bar on sleep$: user.set_widget_preference("status_bar", "sleep_enabled", 0)
^head up show status bar on sleep$: user.set_widget_preference("status_bar", "sleep_enabled", 1)
# Widget setup commands
^head up (show|open) {user.talon_hud_widget_names}$: user.enable_hud_id(talon_hud_widget_names)
^head up (hide|close) {user.talon_hud_widget_names}$: user.disable_hud_id(talon_hud_widget_names)
^head up resize {user.talon_hud_widget_names}$: user.set_hud_setup_mode(talon_hud_widget_names, "dimension")
^head up expand {user.talon_hud_widget_names}$: user.set_hud_setup_mode(talon_hud_widget_names, "limit")
^head up text scale {user.talon_hud_widget_names}$: user.set_hud_setup_mode(talon_hud_widget_names, "font_size")
^head up drag {user.talon_hud_widget_names}$: user.set_hud_setup_mode(talon_hud_widget_names, "position")
^head up basic {user.talon_hud_widget_names}$: user.set_widget_preference(talon_hud_widget_names, "show_animations", 0)
^head up fancy {user.talon_hud_widget_names}$: user.set_widget_preference(talon_hud_widget_names, "show_animations", 1)
^head up hide {user.talon_hud_widget_names} on sleep$: user.set_widget_preference(talon_hud_widget_names, "sleep_enabled", 0)
^head up show {user.talon_hud_widget_names} on sleep$: user.set_widget_preference(talon_hud_widget_names, "sleep_enabled", 1)
^head up align {user.talon_hud_widget_names} right$: user.set_widget_preference(talon_hud_widget_names, "alignment", "right")
^head up align {user.talon_hud_widget_names} left$: user.set_widget_preference(talon_hud_widget_names, "alignment", "left")
^head up align {user.talon_hud_widget_names} top$: user.set_widget_preference(talon_hud_widget_names, "expand_direction", "down")
^head up align {user.talon_hud_widget_names} bottom$: user.set_widget_preference(talon_hud_widget_names, "expand_direction", "up")

# Event log commands
^head up (show|open) event log$: user.enable_hud_id("event_log")
^head up (hide|close) event log$: user.disable_hud_id("event_log")
^head up resize event log$: user.set_hud_setup_mode("event_log", "dimension")
^head up expand event log$: user.set_hud_setup_mode("event_log", "limit")
^head up drag event log$: user.set_hud_setup_mode("event_log", "position")
^head up text scale event log$: user.set_hud_setup_mode("event_log", "font_size")
^head up align event log right$: user.set_widget_preference("event_log", "alignment", "right")
^head up align event log left$: user.set_widget_preference("event_log", "alignment", "left")
^head up align event log top$: user.set_widget_preference("event_log", "expand_direction", "down")
^head up align event log bottom$: user.set_widget_preference("event_log", "expand_direction", "up")
^head up basic event log$: user.set_widget_preference("event_log", "show_animations", 0)
^head up fancy event log$: user.set_widget_preference("event_log", "show_animations", 1)
# Widget content commands
^{user.talon_hud_widget_names} (show|open)$: user.enable_hud_id(talon_hud_widget_names)
^{user.talon_hud_widget_names} (hide|close)$: user.disable_hud_id(talon_hud_widget_names)
^{user.talon_hud_widget_names} minimize$: user.set_widget_preference(talon_hud_widget_names, "minimized", 1)
^{user.talon_hud_widget_names} maximize$: user.set_widget_preference(talon_hud_widget_names, "minimized", 0)
^{user.talon_hud_widget_names} next: user.increase_widget_page(talon_hud_widget_names)
^{user.talon_hud_widget_names} (back|previous): user.decrease_widget_page(talon_hud_widget_names)
^{user.talon_hud_widget_names} options: user.hud_widget_options(talon_hud_widget_names)
Loading

0 comments on commit dc45b9f

Please sign in to comment.