Monday, February 4, 2008

2. What I need?

I spent the latest hours to find resources about handling tablet and/or mouse events. This to understand how difficult can be to develop such things. Of course there are several informations about it.

In the Chapter 2 of the Core Image Programming Guide, the part Imaging Dynamical System, describes a MicroPaint application that is able to capture mouse events and draw on a canvas. The guide is available either in the Cocoa area of Apple Developer Connection site and in the Xcode included documentation. Though, even if it will be soon useful, the MicroPaint application example focuses on the use of CIImageAccumulator class and not on how to actualy handle mouse events.
There is also a CIMicroPaint example in the /Developer/Examples/Quartz/Core Image/CIMicroPaint folder installed with development tools. But it is based on OpenGL and at this point it confuses me a little.
Talking about event handling there is the Cocoa Event-Handling Guide at ADC, available also in the Xcode documentation. But it does not make any example/tutorial and I want a pratical tutorial to learn quickly what to do.

So, I found the Mouse Events article at Cocoa Dev Central that introduces the reader to a tutorial for mouse event handling. I will use it for quickly understand the needed knowledge and then I will explore the examples provided by Wacom at Mac Software Development Support page.

But before to do it I want to write a list of features for my application, to understand what I actually need to know to go on.
  1. Tablet support with pressure sensitivity;
  2. Layers support.
  3. Alpha channel support, for each layer and the global image.
  4. Blending modes support.
  5. Layer transparency support.
  6. Tablet strokes smoothing to avoid stright lines when user moves the pen really fast.
Reading the Cocoa Drawing Guide, I discovered that Quartz gives support for:
  • Layers
  • Gradients (also called shadings)
  • Image data sources
  • Blend modes (Cocoa uses compositing modes instead)
  • Masking images
  • Transparency layers (for grouping content)
  • Arbitrary patterns (other than images)
That means that I can skip to project my self made layers system and use the one provided by Apple and that I can choose between two different blending systems: Quartz blend modes or Cocoa compositing modes.

1. Quartz subclassing NSView

My aim is to create a simple 2D painting application using Cocoa. I have already tried some tutorials on the basics of coding in Objective-C and Cocoa with the Apple IDE Xcode.

Xcode with its counterpart Interface Builder are two extremelly powerfull tools. Most of the work is done using interface, and they prepare automatically a skeleton of the code. Even if, for the lovers of pure coding, this could seem to be not so good, there is the advantage that the code generated this way is quite clean, compliant to Apple standards an less error prone.

I am collecting informations from a lot of different resources on line. There are two tutorial that I found particularly interesting. Those are: the Cocoa Dev Central, Intro To Quartz II, and the Episode 18 - Custom Views of CocoaCast. They basically talk about the same thing: subclassing a NSView class to create a drawing canvas.

All my experience comes from Java so I have no problems to understand Object Oriented languages, I just have some little problem to understand some C sintax but it does not rapresent a great obstacle.

So, watching this two tutorials I discovered that a class can be subclassed in two differnt ways.

  1. Using Xcode: in the Project window left list, it is possible to right click on the Classes subfolder (or any other project subfolder) and choose Add -> New File.... The Assistant will appear and it is possible to choose from a list preconfigured subclasses. Choose Objective-C NSView subclass in the Cocoa section. Then click Next. In the following view give a name to your class and click Finish. After these steps, it is necessary to drag the custom class header to Interface Builder, in the Instances panel of the NIB window to make Interface Builder aware of the new custom class.


  2. Using Interface Builder: in the Classes panel of the NIB window, search for NSView class right click on it an choose Subclass NSView. Giva a name to the subclass and right click over it. Choose Create Files for SubclassName. It will appear a dialog window. Leave it as it is, except for Insert into targets: box. There you have to make sure that your project name check box is selected. To finish click Choose.