Changeset 447

Show
Ignore:
Timestamp:
12/20/11 13:59:12 (5 months ago)
Author:
Carsten
Message:

CaWE: Refactored class MorphPrimT to never modify the source map primitive it is attached to.

This change makes the CommandMorphT redundant.

Location:
cafu/trunk/CaWE
Files:
2 removed
5 modified

Legend:

Unmodified
Added
Removed
  • cafu/trunk/CaWE/MorphPrim.cpp

    r446 r447  
    4444 
    4545 
    46 MorphPrimT::MorphPrimT(MapPrimitiveT* MapPrim) 
     46MorphPrimT::MorphPrimT(const MapPrimitiveT* MapPrim) 
    4747    : m_MapPrim(MapPrim), 
    48       m_Modified(false) 
    49 { 
    50     MapBrushT*       MapBrush=dynamic_cast<MapBrushT*>      (m_MapPrim); 
    51     MapBezierPatchT* MapPatch=dynamic_cast<MapBezierPatchT*>(m_MapPrim); 
     48      m_Modified(false), 
     49      m_RenderBP(NULL) 
     50{ 
     51    const MapBrushT*       MapBrush=dynamic_cast<const MapBrushT*>      (m_MapPrim); 
     52    const MapBezierPatchT* MapPatch=dynamic_cast<const MapBezierPatchT*>(m_MapPrim); 
    5253 
    5354    wxASSERT(MapBrush!=NULL || MapPatch!=NULL);     // Assert they are not both NULL. 
     
    8283            } 
    8384        } 
     85 
     86        m_RenderBP=MapPatch->Clone(); 
    8487    } 
    8588} 
     
    8891MorphPrimT::~MorphPrimT() 
    8992{ 
     93    delete m_RenderBP; 
     94    m_RenderBP=NULL; 
     95 
    9096    for (unsigned long VertexNr=0; VertexNr<m_Vertices.Size(); VertexNr++) delete m_Vertices[VertexNr]; 
    9197    m_Vertices.Clear(); 
     
    99105 
    100106 
    101 bool MorphPrimT::ApplyMorphToMapPrim() 
    102 { 
    103     MapBrushT*       MapBrush=dynamic_cast<MapBrushT*>      (m_MapPrim); 
    104     MapBezierPatchT* MapPatch=dynamic_cast<MapBezierPatchT*>(m_MapPrim); 
     107MapPrimitiveT* MorphPrimT::GetMorphedMapPrim() const 
     108{ 
     109    const MapBrushT*       MapBrush=dynamic_cast<const MapBrushT*>      (m_MapPrim); 
     110    const MapBezierPatchT* MapPatch=dynamic_cast<const MapBezierPatchT*>(m_MapPrim); 
    105111 
    106112    wxASSERT(MapBrush!=NULL || MapPatch!=NULL);     // Assert they are not both NULL. 
    107113    wxASSERT(MapBrush==NULL || MapPatch==NULL);     // Assert they are not both non-NULL. 
    108  
    109     m_Modified=false; 
    110114 
    111115    if (MapBrush!=NULL) 
     
    116120            HullVertices.PushBack(m_Vertices[VNr]->pos); 
    117121 
    118         MapBrushT TestBrush(HullVertices, MapBrush->GetFaces()[0].GetMaterial(), Options.general.NewUVsFaceAligned, MapBrush); 
    119  
    120         if (!TestBrush.IsValid()) return false; 
    121  
    122         // The TestBrush worked fine, now copy the new faces into the Brush. 
    123         MapBrush->Assign(&TestBrush); 
    124         return true; 
    125     } 
    126  
    127     if (MapPatch!=NULL) 
    128     { 
    129      /* MapPatch->ClearCV(); 
    130  
    131         for ( unsigned long i = 0 ; i < m_Vertices.Size() ; i++ ) 
    132         { 
    133             Vector3fT v; 
    134             MP_VertexT& vertex = m_Vertices[i]; 
    135  
    136             vertex.GetPosition(v); 
    137             MapPatch->AddCV(v); 
    138         } */ 
    139  
    140         return true; 
    141     } 
    142  
    143     return false; 
     122        MapBrushT* MorphedBrush=new MapBrushT(HullVertices, MapBrush->GetFaces()[0].GetMaterial(), Options.general.NewUVsFaceAligned, MapBrush); 
     123 
     124        if (MorphedBrush->IsValid()) return MorphedBrush; 
     125 
     126        delete MorphedBrush; 
     127        return NULL; 
     128    } 
     129 
     130    if (MapPatch!=NULL && m_RenderBP!=NULL) 
     131    { 
     132        return m_RenderBP->Clone(); 
     133    } 
     134 
     135    return NULL; 
    144136} 
    145137 
     
    186178void MorphPrimT::Render(Renderer2DT& Renderer, bool RenderVertexHandles, bool RenderEdgeHandles) const 
    187179{ 
     180    if (m_RenderBP) 
     181    { 
     182        m_RenderBP->Render2D(Renderer); 
     183    } 
     184 
     185 
    188186    // Draw the outline of the morph primitive. 
    189187    Renderer.SetLineType(wxPENSTYLE_SOLID, Renderer2DT::LINE_THIN, wxColour(255, 0, 0)); 
     
    287285void MorphPrimT::Render(Renderer3DT& Renderer, bool RenderVertexHandles, bool RenderEdgeHandles) const 
    288286{ 
     287    if (m_RenderBP) 
     288    { 
     289        m_RenderBP->Render3D(Renderer); 
     290    } 
     291 
     292 
    289293    for (int Pass=1; Pass<=2; Pass++) 
    290294    { 
     
    389393void MorphPrimT::UpdateBrushFromVertices() 
    390394{ 
    391     if (dynamic_cast<MapBrushT*>(m_MapPrim)==NULL) return; 
     395    if (dynamic_cast<const MapBrushT*>(m_MapPrim)==NULL) return; 
    392396 
    393397    const float Epsilon=0.1f; 
     
    519523void MorphPrimT::UpdatePatch() 
    520524{ 
    521     MapBezierPatchT* MapPatch=(MapBezierPatchT*)m_MapPrim; 
    522  
    523     for (unsigned long y=0; y<MapPatch->GetHeight(); y++) 
    524     { 
    525         for (unsigned long x=0; x<MapPatch->GetWidth(); x++) 
    526         { 
    527             MapPatch->SetCvPos(x, y, m_Vertices[y*MapPatch->GetWidth()+x]->pos); 
    528         } 
    529     } 
    530 } 
     525    for (unsigned long y=0; y<m_RenderBP->GetHeight(); y++) 
     526    { 
     527        for (unsigned long x=0; x<m_RenderBP->GetWidth(); x++) 
     528        { 
     529            m_RenderBP->SetCvPos(x, y, m_Vertices[y*m_RenderBP->GetWidth()+x]->pos); 
     530        } 
     531    } 
     532} 
  • cafu/trunk/CaWE/MorphPrim.hpp

    r446 r447  
    104104    /// Note that this MorphPrimT does not become the "owner" of the MapPrim pointer, e.g. it does not attempt to delete it in its dtor. 
    105105    /// That also means that this MorphPrimT should not live longer than the MapPrim object. 
    106     MorphPrimT(MapPrimitiveT* MapPrim); 
     106    MorphPrimT(const MapPrimitiveT* MapPrim); 
    107107 
    108108    ~MorphPrimT(); 
    109109 
    110     MapPrimitiveT* GetMapPrim() const { return m_MapPrim; } 
    111     bool           IsModified() const { return m_Modified; } 
     110    const MapPrimitiveT* GetMapPrim() const { return m_MapPrim; } 
     111    bool                 IsModified() const { return m_Modified; } 
    112112 
    113     /// Updates the associated map element by applying the morphed geometry to it. 
    114     /// @returns if the update was successful. 
    115     bool ApplyMorphToMapPrim(); 
     113    /// Returns a newly created instance matching the morphed map primitive, or NULL if reconstruction was not possible. 
     114    /// It does not reset the modified-flag. 
     115    MapPrimitiveT* GetMorphedMapPrim() const; 
    116116 
    117117    /// Moves the selected handles by Delta. 
     
    147147 
    148148 
    149     MapPrimitiveT*      m_MapPrim;  ///< The "attached" map brush / bezier patch. 
    150     bool                m_Modified; ///< Whether the MorphPrimT contains any modifications to the "attached" map brush/bezier patch. 
     149    const MapPrimitiveT* m_MapPrim;     ///< The "attached" source/reference map brush / bezier patch. 
     150    bool                 m_Modified;    ///< Whether the MorphPrimT contains any modifications to the "attached" map brush/bezier patch. 
     151    MapBezierPatchT*     m_RenderBP;    ///< If m_MapPrim is a Bezier patch, this is the current morphed clone that we use for rendering. 
    151152}; 
    152153 
  • cafu/trunk/CaWE/ObserverPattern.hpp

    r316 r447  
    5656    MEMD_PRIMITIVE_PROPS_CHANGED,   ///< The properties of a map primitve have been modified. 
    5757    MEMD_SURFACE_INFO_CHANGED,      ///< The surface info of a map element has changed. Note that surface info changes also include the selection of faces. 
    58     MEMD_MORPH,                     ///< The vertices of a map primitve have been transformed. 
     58    MEMD_MORPH,                     ///< The vertices of a map primitive have been transformed. This is actually UNUSED now that \c CommandMorphT has been removed (r447, 2011-12-20). 
    5959    MEMD_ASSIGN_PRIM_TO_ENTITY,     ///< A map primitive has been assigned to another entity (the world or any custom entity). 
    6060    MEMD_VISIBILITY                 ///< The visibility of a map element has changed. 
  • cafu/trunk/CaWE/ToolMorph.cpp

    r446 r447  
    3737#include "MapBezierPatch.hpp" 
    3838#include "ToolOptionsBars.hpp" 
    39 #include "MapCommands/Morph.hpp" 
     39#include "MapCommands/AddPrim.hpp" 
     40#include "MapCommands/Delete.hpp" 
    4041 
    4142 
     
    179180    // Set the map elements of all morph primitives back to "visible" 
    180181    // and remove all unmodified morph primitives from our list. 
    181     ArrayT<MapElementT*> UnmodifiedMapElems; 
    182  
    183     for (unsigned long MPNr=0; MPNr<m_MorphPrims.Size(); MPNr++) 
    184     { 
    185         if (!m_MorphPrims[MPNr]->IsModified()) 
    186         { 
    187             UnmodifiedMapElems.PushBack(m_MorphPrims[MPNr]->GetMapPrim()); 
    188  
    189             delete m_MorphPrims[MPNr]; 
    190             m_MorphPrims.RemoveAt(MPNr); 
    191             MPNr--; 
    192         } 
    193     } 
    194  
    195     if (m_MorphPrims.Size()>0) 
    196     { 
    197         m_IsRecursiveSelfNotify=true; 
    198         m_MapDoc.GetHistory().SubmitCommand(new CommandMorphT(m_MapDoc, m_MorphPrims)); 
    199         m_IsRecursiveSelfNotify=false; 
    200  
    201         // Clear the list of morph primitives, as the remaining instances are now owned by the command. 
    202         m_MorphPrims.Clear(); 
    203     } 
    204  
    205     if (UnmodifiedMapElems.Size()>0) 
    206     { 
    207         // Visibility of unmodified map elements is no longer affected by IsHiddenByTool(). 
    208         m_IsRecursiveSelfNotify=true; 
    209         m_MapDoc.UpdateAllObservers_Modified(UnmodifiedMapElems, MEMD_VISIBILITY); 
    210         m_IsRecursiveSelfNotify=false; 
    211     } 
    212 } 
    213  
    214  
    215 void ToolMorphT::MorphPrims_TogglePrim(MapPrimitiveT* MapPrim) 
     182 
     183    // TODO: Doing it like this is not particularly efficient, but this is going to be refactored anyway. 
     184    while (m_MorphPrims.Size()>0) 
     185    { 
     186        MorphPrims_TogglePrim(m_MorphPrims[0]->GetMapPrim()); 
     187    } 
     188} 
     189 
     190 
     191void ToolMorphT::MorphPrims_TogglePrim(const MapPrimitiveT* MapPrim) 
    216192{ 
    217193    // Only needed for observer message. 
    218194    ArrayT<MapElementT*> MapElements; 
    219     MapElements.PushBack(MapPrim); 
     195    MapElements.PushBack(const_cast<MapPrimitiveT*>(MapPrim)); 
    220196 
    221197    const int MP_Index=MorphPrims_Find(MapPrim); 
     
    226202 
    227203        m_MorphPrims.RemoveAtAndKeepOrder(MP_Index); 
     204        wxASSERT(MapPrim == MorphPrim->GetMapPrim()); 
    228205 
    229206        if (MorphPrim->IsModified()) 
    230207        { 
    231             // Only needed for command. 
    232             ArrayT<MorphPrimT*> MPs; 
    233             MPs.PushBack(MorphPrim); 
    234  
    235             m_IsRecursiveSelfNotify=true; 
    236             m_MapDoc.GetHistory().SubmitCommand(new CommandMorphT(m_MapDoc, MPs)); 
    237             m_IsRecursiveSelfNotify=false; 
    238         } 
    239         else 
    240         { 
    241             delete MorphPrim;   // Only delete the MorphPrim if it isn't kept in command. 
    242         } 
     208            MapPrimitiveT* MorphedMapPrim=MorphPrim->GetMorphedMapPrim(); 
     209 
     210            if (MorphedMapPrim) 
     211            { 
     212                m_IsRecursiveSelfNotify=true; 
     213 
     214                ArrayT<CommandT*> Commands; 
     215                Commands.PushBack(new CommandDeleteT(m_MapDoc, const_cast<MapPrimitiveT*>(MorphPrim->GetMapPrim()))); 
     216                Commands.PushBack(new CommandAddPrimT(m_MapDoc, MorphedMapPrim, MorphPrim->GetMapPrim()->GetParent())); 
     217 
     218                m_MapDoc.GetHistory().SubmitCommand(new CommandMacroT(Commands, "Edit Vertices")); 
     219 
     220                m_IsRecursiveSelfNotify=false; 
     221            } 
     222        } 
     223 
     224        delete MorphPrim; 
    243225 
    244226        // Elem is now no longer mentioned in the m_MorphPrims list, and thus no longer affected by IsHiddenByTool(). 
    245227        m_IsRecursiveSelfNotify=true; 
    246         m_MapDoc.UpdateAllObservers_Modified(MapElements, MEMD_VISIBILITY); 
     228        m_MapDoc.UpdateAllObservers_Modified(MapElements, MEMD_VISIBILITY);     // TODO: Is this still needed? The element was *deleted* above, after all (but only if modified, mind'ya). 
    247229        m_IsRecursiveSelfNotify=false; 
    248230        return; 
    249231    } 
    250232 
    251     if (dynamic_cast<MapBrushT*>(MapPrim)==NULL && dynamic_cast<MapBezierPatchT*>(MapPrim)==NULL) return; 
     233    if (dynamic_cast<const MapBrushT*>(MapPrim)==NULL && dynamic_cast<const MapBezierPatchT*>(MapPrim)==NULL) return; 
    252234 
    253235    MorphPrimT* MorphPrim=new MorphPrimT(MapPrim); 
     
    389371bool ToolMorphT::IsHiddenByTool(const MapElementT* Elem) const 
    390372{ 
    391     // Only brushes that are currently being morphed are hidden by this tool from normal rendering. 
    392     return (Elem->GetType()==&MapBrushT::TypeInfo) && (MorphPrims_Find(Elem)>=0); 
     373    // Elements that are currently being morphed are hidden by this tool from normal rendering. 
     374    return MorphPrims_Find(Elem) >= 0; 
    393375} 
    394376 
     
    462444    } 
    463445 
    464     if (dynamic_cast<MapBrushT*>(m_MorphPrims[0]->GetMapPrim())==NULL) 
     446    if (dynamic_cast<const MapBrushT*>(m_MorphPrims[0]->GetMapPrim())==NULL) 
    465447    { 
    466448        wxMessageBox("The morph tool can add new vertices only to brushes (not to Bezier patches).\n" 
  • cafu/trunk/CaWE/ToolMorph.hpp

    r446 r447  
    115115    bool GetMorphHandleAt(ViewWindow3DT& ViewWindow, const wxPoint& Point, MorphHandleT& FoundMH); 
    116116 
    117     int  MorphPrims_Find(const MapElementT* Elem) const;    ///< Returns the array index number of the MorphPrimT for the given Elem, -1 if there is none. 
    118     void MorphPrims_CommitAndClear();                       ///< Commits all morphs in m_MorphPrims to their true map elements and resets the morph tool back to empty. 
    119     void MorphPrims_TogglePrim(MapPrimitiveT* MapPrim);     ///< Toggles the membership of MapPrim in the m_MorphPrims array. 
    120     void MoveSelectedHandles(const Vector3fT& Delta);       ///< WARNING: This method *DESTROYS* all handle pointers into any of the m_MorphPrims!! 
     117    int  MorphPrims_Find(const MapElementT* Elem) const;        ///< Returns the array index number of the MorphPrimT for the given Elem, -1 if there is none. 
     118    void MorphPrims_CommitAndClear();                           ///< Commits all morphs in m_MorphPrims to their true map elements and resets the morph tool back to empty. 
     119    void MorphPrims_TogglePrim(const MapPrimitiveT* MapPrim);   ///< Toggles the membership of MapPrim in the m_MorphPrims array. 
     120    void MoveSelectedHandles(const Vector3fT& Delta);           ///< WARNING: This method *DESTROYS* all handle pointers into any of the m_MorphPrims!! 
    121121    void NudgeSelectedHandles(const AxesInfoT& AxesInfo, const wxKeyEvent& KE); 
    122122    void OnEscape(ViewWindowT& ViewWindow);