Hot Door CORE Forum

Full Version: Understanding CORE tool class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I'm having some difficulties understanding lifecycles of hdi_core Tool class and hope you'll point me in the right direction.
For simplicity lets say I'm building a tool that makes a triangle from a base line and a point.

Code:
Tool = hdi::core::Tool(hdi::core::Tool(), NULL,"Tool","Tool",ID, ID);
    Tool.setTrackCallback(HDI_CORE_CALLBACK(Toolset, this, TrackTool));
    Tool.setDragCallback(HDI_CORE_CALLBACK(Toolset, this, DragTool));
    Tool.setMouseUpCallback(HDI_CORE_CALLBACK(Toolset, this, MouseUpTool));

I've created a tool. I want it to draw a line when dragged and then create a triangle from a point where user made a click. So I make a drag callback
Code:
void Toolset::DragTool()
{
    std::auto_ptr<hdi::core::HitData> startHit = Tool.mouseDownHitData(0, hdi::core::AnyHitRequestNoGuides);
    if( !startHit.get() )
        return;
    std::auto_ptr<hdi::core::HitData> endHit = Tool.cursorHitData(hdi::core::AnyHitRequestNoGuides);
    if( !endHit.get() )
        return;
    hdi::core::draw::line(startHit->point(), endHit->point());
}
Line drawn while dragging doesnt stay on the artboard. I assume this is due to some undo calls under the hood. Everything drawn outside of this callback stays. What I want is to make a mouse up counter for this tool that gets incremented on mouse up callback. Lets say I have a mouse up callback like this
Code:
void Toolset::MouseUpTool()
{
   mouseUp++;
   if(mouseUp == 1)   //save points for base
   if(mouseUp == 2)
   {
      //draw triangle
      mouseUp = 0;
   }
}

So far so good.
What I want is to get a behaviour similar to drag callback inside the track callback - user moves cursor, triangle draft gets drawn until a click is made. However I do not fully understand how to handle undo function in such a scenario. I imagine track function to look something like this
Code:
void Toolset::TrackTool()
{
   if(mouseUp != 1)   return;
   //get current end hit, draw triangle based on saved base and this point
}
However I am not sure when should I call undo for current document to give my track callback behaviour similar to dragging(or if this is even a correct approach to this sort of scenarios).
Illustrator does indeed handle art drawn during tool drags by undoing the art on each drag event. If you coerce Illustrator into not undoing the art created during a tool drag then a nasty mess of thousands of annotated paths (one for each event) will appear on screen. So, during a tool drag event, draw whatever art you wish and then draw the art again during the final mouse-up event — it will not be undone by Illustrator in that context. The "final" mouse up event is determined by your tool's logic, as it might require 1, 2, ..., n clicks for your use-case.

While you can create art during other tool events, it is not recommended. Adobe intends you to use annotators for "drawing" during e.g. mouse-track events. Personally, I recommend storing & updating the points/beziers for the desired art during your mouse track and/or drag events in class members, using them to annotate during track events and to create actual art during drag/mouse-up events.