Ticket #96: progress_1_92_II.patch

File progress_1_92_II.patch, 12.1 KB (added by trompetin17, 10 years ago)

Fix

  • 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        }
     72        else
     73        {
     74            //TODO: Show something when the clipboard couldnt open
     75        }
     76       
     77        //First do we need to check if it is a correct xml string
     78        wxInputStream* is = new wxStringInputStream(entities);
     79        wxXmlDocument doc;
     80        if (!doc.Load(*is))
     81        {
     82            return;
     83        }
    5584
     85        // Entities, Entity(1.*)
     86        wxXmlNode* root = doc.GetRoot();
     87        if (root->GetName() != wxT("Entities"))
     88        {
     89            return;
     90        }
     91        //  Template, position,orientation
     92        const wxXmlNode* child = root->GetChildren();
     93
     94        while (child)
     95        {
     96            if (child->GetName() != wxT("Entity"))
     97            {
     98                return;
     99            }
     100            //TODO Validate all attributes
     101            child = child->GetNext();
     102        }
     103       
     104        //Update selectedObjects??
     105        g_SelectedObjects.empty();
     106        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
     107
     108        // is the source code get here now you can add the objects to scene(preview)
     109        // store id to move
     110        child = root->GetChildren();
     111       
     112        while (child)
     113        {
     114            wxString objectId;
     115            Position objectPos;
     116            unsigned int actorSeed;
     117            double orientation;
     118
     119            const wxXmlNode* xmlData = child->GetChildren();
     120           
     121            while (xmlData)
     122            {
     123                if (xmlData->GetName() == wxT("Template"))
     124                {
     125                    objectId = xmlData->GetNodeContent();
     126                }
     127                else if (xmlData->GetName() == wxT("Position"))
     128                {
     129                    wxString x, z;
     130                    xmlData->GetAttribute(wxT("x"), &x);
     131                    xmlData->GetAttribute(wxT("z"), &z);
     132               
     133                    double aux,aux2;
     134                    x.ToDouble(&aux);
     135                    z.ToDouble(&aux2);
     136
     137                    objectPos = Position(aux,0,aux2);
     138                }
     139                else if (xmlData->GetName() == wxT("Orientation"))
     140                {
     141                    wxString y;
     142                    xmlData->GetAttribute(wxT("y"), &y);
     143                    y.ToDouble(&orientation);
     144                }
     145
     146                xmlData = xmlData->GetNext();
     147            }
     148
     149            POST_MESSAGE(ObjectPreview, ((std::wstring)objectId.c_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), objectPos, false, Position(), orientation, 0, false));
     150
     151            child = child->GetNext();
     152        }
     153
     154        //Set state paste for preview the new objects
     155        this->SetState(&Pasting);
     156    }
     157
     158    void OnMovingPaste()
     159    {
     160        //Move the preview(s) object(s)
     161        POST_MESSAGE(MoveObjectPreview, ((m_objPosition)));
     162    }
     163
     164    void OnPasteEnd(bool canceled)
     165    {
     166        if (canceled)
     167        {
     168            //delete previews objects
     169            POST_MESSAGE(ObjectPreview, (_T(""), GetScenarioEditor().GetObjectSettings().GetSettings(), Position(), false, Position(), 0, 0, true));
     170        }
     171        else
     172        {
     173            //Create new Objects and delete preview objects
     174        }
     175        //when all is done set default state
     176        this->SetState(&Waiting);
     177    }
     178
    56179    // TODO: keys to rotate/move object?
    57180
    58181    struct sWaiting : public State
     
    167290                obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
    168291                return true;
    169292            }
     293            else if (evt.GetModifiers() == wxMOD_CONTROL)
     294            {
     295                if (!g_SelectedObjects.empty())
     296                {
     297                    if (evt.GetKeyCode() == 'C')
     298                    {
     299                        //COPY current selections
     300                        AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
     301                        info.Post();
     302
     303                        //In xmldata is the configuration
     304                        //now send to clipboard
     305                        if (wxTheClipboard->Open())
     306                        {
     307                            wxString text(info.xmldata.c_str());
     308                            wxTheClipboard->SetData(new wxTextDataObject(text));
     309                            wxTheClipboard->Close();
     310                        }
     311                        else
     312                        {
     313                            //TODO: Say something about couldnt open clipboard
     314                        }
     315
     316                    }
     317                }
     318                if (evt.GetKeyCode() == 'V')
     319                {
     320                    obj->OnPasteStart();
     321                }
     322            }
    170323            else
    171324                return false;
    172325        }
     
    335488        }
    336489    }
    337490    SelectSimilar;
     491
     492    struct sPasting : public State
     493    {
     494        bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
     495        {
     496            if (evt.Moving())
     497            {
     498                //Move the object
     499                obj->m_objPosition = Position(evt.GetPosition());
     500                obj->OnMovingPaste();
     501                return true;
     502            }
     503            else if (evt.LeftDown())
     504            {
     505                //Place the object and update
     506                obj->OnPasteEnd(false);
     507                return true;
     508            }
     509            else
     510                return false;
     511        }
     512        bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
     513        {
     514            if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
     515            {
     516                obj->OnPasteEnd(true);
     517                return true;
     518            }
     519            else
     520                return false;
     521        }
     522    }
     523    Pasting;
    338524};
    339525
    340526IMPLEMENT_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(MoveObjectPreview)
     368{
     369    if (g_PreviewEntitiesID.size()==0)
     370    {
     371        return;
     372    }
     373
     374    //TODO:Change pivot
     375    entity_id_t pivot = *g_PreviewEntitiesID.begin();
     376
     377    // All selected objects move relative to a pivot object,
     378    //  so get its position and whether it's floating
     379    CVector3D pivotPos(0, 0, 0);
     380    bool pivotFloating = false;
     381   
     382    CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), pivot);
     383    if (cmpPosition && cmpPosition->IsInWorld())
     384    {
     385        pivotFloating = cmpPosition->IsFloating();
     386        CFixedVector3D pivotFixed = cmpPosition->GetPosition();
     387        pivotPos = CVector3D(pivotFixed.X.ToFloat(), pivotFixed.Y.ToFloat(), pivotFixed.Z.ToFloat());
     388    }
     389
     390    // Calculate directional vector of movement for pivot object,
     391    //  we apply the same movement to all objects
     392    CVector3D targetPos = GetUnitPos(msg->pos, pivotFloating);
     393    CVector3D dir = targetPos - pivotPos;
     394
     395    for (size_t i = 0; i < g_PreviewEntitiesID.size(); ++i)
     396    {
     397        entity_id_t id = (entity_id_t)g_PreviewEntitiesID[i];
     398        CVector3D posFinal = CVector3D(0, 0, 0);
     399        CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     400        if (cmpPosition && cmpPosition->IsInWorld())
     401        {
     402            // Calculate this object's position
     403            CFixedVector3D posFixed = cmpPosition->GetPosition();
     404            CVector3D pos = CVector3D(posFixed.X.ToFloat(), posFixed.Y.ToFloat(), posFixed.Z.ToFloat());
     405            CVector3D posFinal = pos + dir;
     406
     407            cmpPosition->JumpTo(entity_pos_t::FromFloat(posFinal.X), entity_pos_t::FromFloat(posFinal.Z));
     408        }
     409    }
     410}
     411
    314412MESSAGEHANDLER(ObjectPreview)
    315413{
    316414    // If the selection has changed...
    317     if (*msg->id != g_PreviewUnitName)
     415    if (*msg->id != g_PreviewUnitName || (!msg->cleanObjectPreviews))
    318416    {
    319417        // Delete old entity
    320         if (g_PreviewEntityID != INVALID_ENTITY)
    321             g_Game->GetSimulation2()->DestroyEntity(g_PreviewEntityID);
     418        if (g_PreviewEntityID != INVALID_ENTITY && msg->cleanObjectPreviews)
     419        {
     420            //Time to delete all preview objects
     421            if (g_PreviewEntitiesID.size() > 0)
     422            {
     423                std::vector<entity_id_t>::const_iterator i;
     424                for (i = g_PreviewEntitiesID.begin(); i != g_PreviewEntitiesID.end(); i++)
     425                {
     426                    g_Game->GetSimulation2()->DestroyEntity(*i);
     427                }
     428                g_PreviewEntitiesID.clear();
     429            }
     430        }
    322431
    323432        // Create the new entity
    324433        if ((*msg->id).empty())
    325434            g_PreviewEntityID = INVALID_ENTITY;
    326435        else
     436        {
    327437            g_PreviewEntityID = g_Game->GetSimulation2()->AddLocalEntity(L"preview|" + *msg->id);
     438            g_PreviewEntitiesID.push_back(g_PreviewEntityID);
     439        }
     440       
    328441
    329442        g_PreviewUnitName = *msg->id;
    330443    }
  • source/tools/atlas/GameInterface/Messages.h

     
    334334SHAREABLE_STRUCT(sObjectSettings);
    335335#endif
    336336
     337// Moving Preview(s) object together, default is using the firs element in vector
     338MESSAGE(MoveObjectPreview,
     339        ((Position,pos))
     340        );
     341
    337342// Preview object in the game world - creates a temporary unit at the given
    338343// position, and removes it when the preview is next changed
    339344MESSAGE(ObjectPreview,
     
    344349        ((Position, target))
    345350        ((float, angle))
    346351        ((unsigned int, actorseed))
     352        ((bool, cleanObjectPreviews))
    347353        );
    348354
    349355COMMAND(CreateObject, NOMERGE,
     
    590596        ((sObjectSettings, settings))
    591597        );
    592598
     599QUERY(GetObjectMapSettings,
     600        ((std::vector<ObjectID>, ids))
     601        ,
     602        ((std::wstring, xmldata))
     603        );
     604
     605
    593606QUERY(GetPlayerObjects,
    594607        ((int, player))
    595608        ,