Hot Door CORE Forum
Understanding CORE tool class - Printable Version

+- Hot Door CORE Forum (http://hotdoorcore.com/forum)
+-- Forum: All forums (http://hotdoorcore.com/forum/forumdisplay.php?fid=1)
+--- Forum: Getting started (http://hotdoorcore.com/forum/forumdisplay.php?fid=6)
+--- Thread: Understanding CORE tool class (/showthread.php?tid=141)



Understanding CORE tool class - Gaxx - 06-09-2017

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).


RE: Understanding CORE tool class - garrett - 06-09-2017

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.