Ticket #96: progress_1_96_III.patch

File progress_1_96_III.patch, 14.1 KB (added by trompetin17, 10 years ago)

Fixed

  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/PlaceObject.cpp

     
    4949            + (m_ScreenPos.type1.y-m_Target.type1.y)*(m_ScreenPos.type1.y-m_Target.type1.y);
    5050        bool useTarget = (dragDistSq >= 16*16);
    5151        if (preview)
    52             POST_MESSAGE(ObjectPreview, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed));
     52            POST_MESSAGE(ObjectPreview, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed, true));
    5353        else
    5454        {
    5555            POST_COMMAND(CreateObject, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed));
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.cpp

     
    2323#include "Common/MiscState.h"
    2424#include "Common/ObjectSettings.h"
    2525#include "GameInterface/Messages.h"
     26#include <wx/clipbrd.h>
     27#include <wx/xml/xml.h>
     28#include <wx/sstream.h>
    2629
    2730using AtlasMessage::Position;
    2831
     
    3336    int m_dx, m_dy;
    3437    AtlasMessage::ObjectID m_lastSelected;
    3538    wxPoint m_startPoint;
     39    Position m_objPosition;
    3640
    3741    // TODO: If we don't plan to change hotkeys, just replace with evt.ShiftDown(), etc.
    3842    static const wxKeyCode SELECTION_ADD_HOTKEY = WXK_SHIFT;
     
    5256        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
    5357    }
    5458
     59    void OnPasteStart()
     60    {
     61        //PASTE
     62        wxString entities;
     63        if (wxTheClipboard->Open())
     64        {
     65            if (wxTheClipboard->IsSupported(wxDF_TEXT))
     66            {
     67                wxTextDataObject data;
     68                wxTheClipboard->GetData(data);
     69                entities = data.GetText();
     70            }
     71            wxTheClipboard->Close();
     72        }
     73        else
     74        {
     75            //TODO: Show something when the clipboard couldnt open
     76        }
     77       
     78        //First do we need to check if it is a correct xml string
     79        wxInputStream* is = new wxStringInputStream(entities);
     80        wxXmlDocument doc;
     81        if (!doc.Load(*is))
     82        {
     83            return;
     84        }
    5585
     86        // Entities, Entity(1.*)
     87        wxXmlNode* root = doc.GetRoot();
     88        if (root->GetName() != wxT("Entities"))
     89        {
     90            return;
     91        }
     92        //  Template, position,orientation
     93        const wxXmlNode* child = root->GetChildren();
     94
     95        while (child)
     96        {
     97            if (child->GetName() != wxT("Entity"))
     98            {
     99                return;
     100            }
     101            //TODO Validate all attributes
     102            child = child->GetNext();
     103        }
     104       
     105        //Update selectedObjects??
     106        g_SelectedObjects.empty();
     107        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
     108
     109        // is the source code get here now you can add the objects to scene(preview)
     110        // store id to move
     111        child = root->GetChildren();
     112       
     113        while (child)
     114        {
     115            wxString objectId;
     116            Position objectPos;
     117            unsigned int actorSeed;
     118            double orientation;
     119
     120            const wxXmlNode* xmlData = child->GetChildren();
     121           
     122            while (xmlData)
     123            {
     124                if (xmlData->GetName() == wxT("Template"))
     125                {
     126                    objectId = xmlData->GetNodeContent();
     127                }
     128                else if (xmlData->GetName() == wxT("Position"))
     129                {
     130                    wxString x, z;
     131                    xmlData->GetAttribute(wxT("x"), &x);
     132                    xmlData->GetAttribute(wxT("z"), &z);
     133               
     134                    double aux,aux2;
     135                    x.ToDouble(&aux);
     136                    z.ToDouble(&aux2);
     137
     138                    objectPos = Position(aux,0,aux2);
     139                }
     140                else if (xmlData->GetName() == wxT("Orientation"))
     141                {
     142                    wxString y;
     143                    xmlData->GetAttribute(wxT("y"), &y);
     144                    y.ToDouble(&orientation);
     145                }
     146
     147                xmlData = xmlData->GetNext();
     148            }
     149
     150            POST_MESSAGE(ObjectPreview, ((std::wstring)objectId.c_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), objectPos, false, Position(), orientation, 0, false));
     151
     152            child = child->GetNext();
     153        }
     154
     155        //Set state paste for preview the new objects
     156        this->SetState(&Pasting);
     157    }
     158
     159    void OnMovingPaste()
     160    {
     161        //Move the preview(s) object(s)
     162        POST_MESSAGE(MoveObjectPreview, ((m_objPosition)));
     163    }
     164
     165    void OnPasteEnd(bool canceled)
     166    {
     167        if (canceled)
     168        {
     169            //delete previews objects
     170            POST_MESSAGE(ObjectPreview, (_T(""), GetScenarioEditor().GetObjectSettings().GetSettings(), Position(), false, Position(), 0, 0, true));
     171        }
     172        else
     173        {
     174            //Create new Objects and delete preview objects
     175            POST_MESSAGE(ObjectPreviewToEntity,());
     176        }
     177        //when all is done set default state
     178        this->SetState(&Waiting);
     179    }
     180
    56181    // TODO: keys to rotate/move object?
    57182
    58183    struct sWaiting : public State
     
    167292                obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
    168293                return true;
    169294            }
     295            else if (evt.GetModifiers() == wxMOD_CONTROL)
     296            {
     297                if (!g_SelectedObjects.empty())
     298                {
     299                    if (evt.GetKeyCode() == 'C')
     300                    {
     301                        //COPY current selections
     302                        AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
     303                        info.Post();
     304
     305                        //In xmldata is the configuration
     306                        //now send to clipboard
     307                        if (wxTheClipboard->Open())
     308                        {
     309                            wxString text(info.xmldata.c_str());
     310                            wxTheClipboard->SetData(new wxTextDataObject(text));
     311                            wxTheClipboard->Close();
     312                        }
     313                        else
     314                        {
     315                            //TODO: Say something about couldnt open clipboard
     316                        }
     317                        return true;
     318                    }
     319                }
     320                if (evt.GetKeyCode() == 'V')
     321                {
     322                    obj->OnPasteStart();
     323                    return true;
     324                }
     325            }
    170326            else
    171327                return false;
    172328        }
     
    335491        }
    336492    }
    337493    SelectSimilar;
     494
     495    struct sPasting : public State
     496    {
     497        bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
     498        {
     499            if (evt.Moving())
     500            {
     501                //Move the object
     502                obj->m_objPosition = Position(evt.GetPosition());
     503                obj->OnMovingPaste();
     504                return true;
     505            }
     506            else if (evt.LeftDown())
     507            {
     508                //Place the object and update
     509                obj->OnPasteEnd(false);
     510                return true;
     511            }
     512            else
     513                return false;
     514        }
     515        bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
     516        {
     517            if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
     518            {
     519                obj->OnPasteEnd(true);
     520                return true;
     521            }
     522            else
     523                return false;
     524        }
     525    }
     526    Pasting;
    338527};
    339528
    340529IMPLEMENT_DYNAMIC_CLASS(TransformObject, StateDrivenTool<TransformObject>);
  • source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp

     
    4949#include "simulation2/components/ICmpTemplateManager.h"
    5050#include "simulation2/components/ICmpVisual.h"
    5151#include "simulation2/helpers/Selection.h"
     52#include "ps/XML/XMLWriter.h"
    5253
    53 
    5454namespace AtlasMessage {
    5555
    5656namespace
     
    218218    msg->settings = settings;
    219219}
    220220
     221QUERYHANDLER(GetObjectMapSettings)
     222{
     223    std::vector<entity_id_t> ids = *msg->ids;
     224   
     225    CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     226    ENSURE(cmpTemplateManager);
     227   
     228    XML_Start();
     229    {
     230        XML_Element("Entities");
     231        {
     232            for (size_t i = 0; i < ids.size(); i++)
     233            {
     234                entity_id_t id = (entity_id_t)ids[i];
     235                XML_Element("Entity");
     236                {
     237                    //Template name
     238                    XML_Setting("Template", cmpTemplateManager->GetCurrentTemplateName(id));
     239                   
     240                    //Player
     241                    CmpPtr<ICmpOwnership> cmpOwnership(*g_Game->GetSimulation2(), id);
     242                    if (cmpOwnership)
     243                        XML_Setting("Player", (int)cmpOwnership->GetOwner());
     244                   
     245                    //Adding position to make some relative position later
     246                    CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     247                    if (cmpPosition)
     248                    {
     249                        CFixedVector3D pos = cmpPosition->GetPosition();
     250                        CFixedVector3D rot = cmpPosition->GetRotation();
     251                        {
     252                            XML_Element("Position");
     253                            XML_Attribute("x", pos.X);
     254                            XML_Attribute("z", pos.Z);
     255                            // TODO: height offset etc
     256                        }
     257                        {
     258                            XML_Element("Orientation");
     259                            XML_Attribute("y", rot.Y);
     260                                                        // TODO: X, Z maybe
     261                        }
     262                    }
     263                }
     264            }
     265        }
     266    }
     267   
     268    const CStr& data = XML_GetOutput();
     269    msg->xmldata = std::wstring(data.begin(), data.end());
     270}
     271
     272
    221273BEGIN_COMMAND(SetObjectSettings)
    222274{
    223275    player_id_t m_PlayerOld, m_PlayerNew;
     
    283335
    284336static CStrW g_PreviewUnitName;
    285337static entity_id_t g_PreviewEntityID = INVALID_ENTITY;
     338static std::vector<entity_id_t> g_PreviewEntitiesID;
    286339
    287340static CVector3D GetUnitPos(const Position& pos, bool floating)
    288341{
     
    311364    return vec;
    312365}
    313366
     367MESSAGEHANDLER(ObjectPreviewToEntity)
     368{
     369    UNUSED2(msg);
     370
     371    if (g_PreviewEntitiesID.size() == 0)
     372    {
     373        return;
     374    }
     375
     376    CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     377    ENSURE(cmpTemplateManager);
     378
     379    //I need to re create the objects finally delete preview objects
     380    std::vector<entity_id_t>::const_iterator i;
     381    for (i = g_PreviewEntitiesID.begin(); i != g_PreviewEntitiesID.end(); i++)
     382    {
     383        //Get template
     384        entity_id_t ent = *i;
     385        std::string templateName = cmpTemplateManager->GetCurrentTemplateName(ent);
     386        std::wstring wTemplateName(templateName.begin() + 8, templateName.end());
     387        //Create new entity
     388        entity_id_t new_ent = g_Game->GetSimulation2()->AddEntity(wTemplateName);
     389        if (new_ent == INVALID_ENTITY)
     390            continue;
     391
     392        //get position, get rotation
     393        CmpPtr<ICmpPosition> cmpPositionNew(*g_Game->GetSimulation2(), new_ent);
     394        CmpPtr<ICmpPosition> cmpPositionOld(*g_Game->GetSimulation2(), ent);
     395
     396        if (cmpPositionNew && cmpPositionOld)
     397        {
     398            CVector3D pos = cmpPositionOld->GetPosition();
     399            cmpPositionNew->JumpTo(entity_pos_t::FromFloat(pos.X), entity_pos_t::FromFloat(pos.Z));
     400
     401            //now rotate
     402            CFixedVector3D rotation = cmpPositionOld->GetRotation();
     403            cmpPositionNew->SetYRotation(rotation.Y);
     404        }
     405
     406        //get owner
     407        CmpPtr<ICmpOwnership> cmpOwnershipNew(*g_Game->GetSimulation2(), new_ent);
     408        CmpPtr<ICmpOwnership> cmpOwnershipOld(*g_Game->GetSimulation2(), ent);
     409        if (cmpOwnershipNew && cmpOwnershipOld)
     410        {
     411            cmpOwnershipNew->SetOwner(cmpOwnershipOld->GetOwner());
     412        }
     413
     414        //getVisual
     415        CmpPtr<ICmpVisual> cmpVisualNew(*g_Game->GetSimulation2(), new_ent);
     416        CmpPtr<ICmpVisual> cmpVisualOld(*g_Game->GetSimulation2(), ent);
     417        if (cmpVisualNew && cmpVisualOld)
     418        {
     419            cmpVisualNew->SetActorSeed(cmpVisualOld->GetActorSeed());
     420        }
     421
     422        g_Game->GetSimulation2()->DestroyEntity(ent);
     423    }
     424    g_PreviewEntitiesID.clear();
     425}
     426
     427MESSAGEHANDLER(MoveObjectPreview)
     428{
     429    if (g_PreviewEntitiesID.size()==0)
     430    {
     431        return;
     432    }
     433
     434    //TODO:Change pivot
     435    entity_id_t pivot = *g_PreviewEntitiesID.begin();
     436
     437    // All selected objects move relative to a pivot object,
     438    //  so get its position and whether it's floating
     439    CVector3D pivotPos(0, 0, 0);
     440    bool pivotFloating = false;
     441   
     442    CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), pivot);
     443    if (cmpPosition && cmpPosition->IsInWorld())
     444    {
     445        pivotFloating = cmpPosition->IsFloating();
     446        CFixedVector3D pivotFixed = cmpPosition->GetPosition();
     447        pivotPos = CVector3D(pivotFixed.X.ToFloat(), pivotFixed.Y.ToFloat(), pivotFixed.Z.ToFloat());
     448    }
     449
     450    // Calculate directional vector of movement for pivot object,
     451    //  we apply the same movement to all objects
     452    CVector3D targetPos = GetUnitPos(msg->pos, pivotFloating);
     453    CVector3D dir = targetPos - pivotPos;
     454
     455    for (size_t i = 0; i < g_PreviewEntitiesID.size(); ++i)
     456    {
     457        entity_id_t id = (entity_id_t)g_PreviewEntitiesID[i];
     458        CVector3D posFinal = CVector3D(0, 0, 0);
     459        CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     460        if (cmpPosition && cmpPosition->IsInWorld())
     461        {
     462            // Calculate this object's position
     463            CFixedVector3D posFixed = cmpPosition->GetPosition();
     464            CVector3D pos = CVector3D(posFixed.X.ToFloat(), posFixed.Y.ToFloat(), posFixed.Z.ToFloat());
     465            posFinal = pos + dir;
     466        }
     467        cmpPosition->JumpTo(entity_pos_t::FromFloat(posFinal.X), entity_pos_t::FromFloat(posFinal.Z));
     468    }
     469}
     470
    314471MESSAGEHANDLER(ObjectPreview)
    315472{
    316473    // If the selection has changed...
    317     if (*msg->id != g_PreviewUnitName)
     474    if (*msg->id != g_PreviewUnitName || (!msg->cleanObjectPreviews))
    318475    {
    319476        // Delete old entity
    320         if (g_PreviewEntityID != INVALID_ENTITY)
    321             g_Game->GetSimulation2()->DestroyEntity(g_PreviewEntityID);
     477        if (g_PreviewEntityID != INVALID_ENTITY && msg->cleanObjectPreviews)
     478        {
     479            //Time to delete all preview objects
     480            if (g_PreviewEntitiesID.size() > 0)
     481            {
     482                std::vector<entity_id_t>::const_iterator i;
     483                for (i = g_PreviewEntitiesID.begin(); i != g_PreviewEntitiesID.end(); i++)
     484                {
     485                    g_Game->GetSimulation2()->DestroyEntity(*i);
     486                }
     487                g_PreviewEntitiesID.clear();
     488            }
     489        }
    322490
    323491        // Create the new entity
    324492        if ((*msg->id).empty())
    325493            g_PreviewEntityID = INVALID_ENTITY;
    326494        else
     495        {
    327496            g_PreviewEntityID = g_Game->GetSimulation2()->AddLocalEntity(L"preview|" + *msg->id);
     497            g_PreviewEntitiesID.push_back(g_PreviewEntityID);
     498        }
     499       
    328500
    329501        g_PreviewUnitName = *msg->id;
    330502    }
  • source/tools/atlas/GameInterface/Messages.h

     
    334334SHAREABLE_STRUCT(sObjectSettings);
    335335#endif
    336336
     337// transform de local entity to a real entity
     338MESSAGE(ObjectPreviewToEntity,);
     339
     340// Moving Preview(s) object together, default is using the firs element in vector
     341MESSAGE(MoveObjectPreview,
     342        ((Position,pos))
     343        );
     344
    337345// Preview object in the game world - creates a temporary unit at the given
    338346// position, and removes it when the preview is next changed
    339347MESSAGE(ObjectPreview,
     
    344352        ((Position, target))
    345353        ((float, angle))
    346354        ((unsigned int, actorseed))
     355        ((bool, cleanObjectPreviews))
    347356        );
    348357
    349358COMMAND(CreateObject, NOMERGE,
     
    590599        ((sObjectSettings, settings))
    591600        );
    592601
     602QUERY(GetObjectMapSettings,
     603        ((std::vector<ObjectID>, ids))
     604        ,
     605        ((std::wstring, xmldata))
     606        );
     607
     608
    593609QUERY(GetPlayerObjects,
    594610        ((int, player))
    595611        ,