This is a design question regarding an MVC implementation. I am creating a 2D graphic app using QT and OpenGL but I do not think the technology matters.
So my view is an openGL widget, whatever is to be drawn is stored n the model and the controller should modify the model and have the OpenGL widget redraw the scene.
The view should capture the following mouse events, MouseRelease, MouseDown and MouseMove and then transfer them to the controller to make the proper decision on what to do when the user clicks or drags the mouse.
I am debating between 2 approaches, incapsulate the mouse handling inside the OpenGL widget and just report the click and drag back to the controller?
Or transfer the mouse events as is to the controller and let it handle all the logic to determine the clicks and drags.
Any advise is very apreciated.
Thank you
I think the widget is going to be getting mouse coordinates in the viewport/"view space" coordinate system, which may not make much sense to the controller. I think your widget should convert the co-ordinates of any clicks and drags to world space, then pass them to the controller.
Why is this good? Because it avoids your controller needing any special knowledge of the viewport/widget, and so preserves encapulation. If you add more viewports/widgets or maybe even a console or a script that also wants to feed the controller, they can all pass their "instructions" in world space and the controller will function quite happily. Your viewport is already "aware" of "world space" and "view space" or it couldn't have rendered your model.
Related
I have a SectionList which scrolls/swipes vertically. Can I bind a swipe gesture on it, so that it results in swiping a completely separate View which is behind it? Furthermore, is there an example someone could lead me to, in which a swipe gesture on a SectionList is bound to the animation of another object or objects? Here is a good example of what I am trying to achieve:
When the view is scrolled vertically in the example above it causes a menu to animate in from the top. That is close to the desired effect I'm seeking. I'd also love to know how to achieve that rotational animation tied to the horizontal swipe. I'm thinking that I will likely need the Animated API for this kind of behavior. Any tutorials or code examples you have would really help.
Is there any sample code or article/guide suggesting how to best implement a click+scroll feature as common in apps like Adobe XYZ, Pixelmator, ..:
The user clicks in a view while holding down the Space key to scroll the view
Should we just move the containing scroll view every time the mouse moves by the delta amount since the previous mouse moved event?
Is there any built-in functionality in NSScrollView that we can hook up to in order to perform the click-&-drag scrolling?
Or should we simulate clicking&dragging the scrollbar via the NSScroller APIs?
Any hints appreciated!
Not looking for sample code to copy&paste here, more what's the best/most compatible/least code strategy!
I am wanting to implement history navigation in my app that mimics the slide away animation found in Safari on Lion and in XCode where a top view slides away at the speed of swipe to reveal the view underneath it.
I was looking for pointers on how to do this. I know how to detect the swipe. I assume I could implement the animation via a CALayer animation slide transition on the top view revealing a view underneath it. Has anybody else done this and can offer some further pointers?
It's a new NSEvent method, -trackSwipeEventWithOptions:.... You should call it from within your regular scroll/swipe event handler, whenever you decide the gesture should begin. Unfortunately it doesn't automatically handle the page animations — it just gives you updates with the gesture amount, and you have to do the animations yourself (using layers or views or somesuch). You'll probably want to save images of each page so you can animate them around during a gesture.
I have an application with custom widgets and custom event handling model (I'm rendering in OpenGL). I would like to implement a text edit view taking advantage of Cocoa text input structures, but I don't know how to generate NSEvent objects to pass to NSTextInputContext. In particular, I'm having problems providing a window number, graphic context and mouse cursor coordinates (since i have to provide them in the window's coordinate system). Probably graphic context isn't needed but mouse coordinates are necessary to handle mouse selection events.
Is there any way I can solve this?
I have a main view (subclass of NSView) and as i'm new to cocoa, i'd like to know how to update the view in function of events.
I know there are many methods that take events such as -(void)mouseMoved:(NSEvent*)event or - (void)mouseClicked:(NSEvent*)event My algorithm to determine what to do is ready. I want to know where I should update the main view: is it in the -(void)mouseMoved:(NSEvent*)event or in the - (void)drawRect:(NSRect)dirtyRect. And if it is in drawRect, then how should i pass the information to it?
Thanks in advance!
Here's a quick explanation that will hopefully get you on your way:
Handle events
User actions are communicated to your views and windows by events (keyboard + mouse) and actions (events interpreted by buttons and other controls). Your views should react to these events and actions by updating the model, which are the lower-level data structures that represent whatever your program displays to the user. If Cocoa, the view typically communicates through a controller object to make changes to the model.
Invalidate display / trigger redraw
After you have updated your model, you need to inform the view that it needs to redraw. This can be done in several ways, but the simplest way to do it is -setNeedsDisplay:YES. This will ensure that at some point in the immediate future, your view will redraw itself to display the updated model data.
Draw
At some point Cocoa will call -drawRect: on your view. Inside -drawRect:, you should read the requisite data from your model and draw the necessary graphics. You shouldn't do any manipulation of the model in this method.