Last active
November 26, 2017 21:25
-
-
Save MBurvill/d7921fdc724827cd7c79707969edcbf9 to your computer and use it in GitHub Desktop.
glyph navigator test 1 for RF Forum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mojo.canvas import Canvas | |
from mojo.drawingTools import * | |
import mojo.drawingTools as mojoDrawingTools | |
from vanilla import * | |
from mojo.events import addObserver,removeObserver | |
from defconAppKit.windows.baseWindow import BaseWindowController | |
import AppKit | |
class ExampleWindow(BaseWindowController): | |
def __init__(self): | |
self.size = 50 | |
self.offset = 0, 0 | |
self.w = Window((400, 400), minSize=(200, 200)) | |
self.w.slider = Slider((10, 5, -10, 22), value=self.size, callback=self.sliderCallback) | |
self.w.canvas = Canvas((0, 30, -0, 400), canvasSize=(1000, 1000),delegate=self, hasHorizontalScroller=True, hasVerticalScroller=True)#,acceptsMouseMoved=True) | |
# acceptsMouseMoved=True sounds useful, but it is an unexpected keyword? | |
# ----->>>> 'acceptsMouseMoved' is a method, return True to receive mouseMoved callback in the delegate (see below) | |
self.setUpBaseWindowBehavior() | |
self.w.open() | |
print help(mojoDrawingTools) | |
# module docs link broken http://docs.python.org/library/mojo.drawingTools | |
# ----->>>>> euh funny :) | |
print help(Canvas) | |
## HELP | |
## if I use these observers, I can only get information about the mouse when clicking in a glyph window, | |
## I Want the information when I click in the canvas I created | |
## If I don't use these observers and use the methods directly, I do get the event when clicking on the canvas, but it is NSEvent, the information for instance location is relative to this window, which is what I want. | |
## how can I get this inforation as python dict or tuples or lists? | |
addObserver(self, "_mouseMoved", "mouseMoved") | |
addObserver(self, "_mouseDown", "mouseDown") | |
# ----->>>>> | |
# this object acts a delegate for the canvas object | |
# every event can be redirected to this object from the canvas object | |
# this will provide you info about events within the canvas object | |
# obserers just observer a specific action and send a notification to a given method | |
# < <<<<<----- | |
def windowCloseCallback(self, sender): | |
#when this window w is closed, remove the observer | |
removeObserver(self, "mouseMoved") | |
removeObserver(self, "mouseDown") | |
# super I do not know what it's for, but it is in the example | |
# ------>>>>> you need this as the BaseWindowController also does stuff while closing the window | |
super(ExampleWindow, self).windowCloseCallback(sender) | |
def sliderCallback(self, sender): | |
self.size = sender.get() | |
self.w.canvas.update() | |
def draw(self): | |
# offset the canvas | |
x, y = self.offset | |
translate(x, y) | |
#set scale of canvas | |
scale(self.size*0.1) | |
#print CurrentGlyph() | |
if CurrentGlyph(): | |
#draw current glyph to canvas (will need to observe when current glyph changes, but this is just to test) | |
drawGlyph(CurrentGlyph()) | |
def acceptsMouseMoved(self): | |
return False | |
# mouse down that is not from observer, gives NSEvent for click relative to window | |
def mouseDown(self, event): | |
# ----->>>> see https://developer.apple.com/documentation/appkit/nsevent?language=objc | |
print type(event) | |
view = self.w.canvas.getNSView() | |
print view.convertPoint_fromView_(event.locationInWindow(), None) | |
# mouse down from observer give info as dict | |
def _mouseDown(self, notification): | |
print notification["event"] | |
print notification["glyph"] | |
print notification["point"] | |
# mouse moved that is not from observer, gives NSEvent for mouse relative to window | |
def mouseMoved(self, event): | |
print event.locationInWindow() | |
# mouse moved from observer give info as dict | |
def _mouseMoved(self, notification): | |
print notification | |
def mouseDragged(self, event): | |
# how to grab and drag the canvas? from sender I get information in an NSEvent, how can I access that information and do something useful with it? | |
print 'drag?', event | |
x, y = self.offset | |
self.offset = x + event.deltaX(), y - event.deltaY() | |
self.w.canvas.update() | |
ExampleWindow() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment