Ticket #96: progress_1_96_IV.patch

File progress_1_96_IV.patch, 14.5 KB (added by trompetin17, 10 years ago)

Finally?

  • 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_entPosition;
    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            return;
    5583
     84        // Entities, Entity(1.*)
     85        wxXmlNode* root = doc.GetRoot();
     86        if (root->GetName() != wxT("Entities"))
     87            return;
     88
     89        //  Template, position,orientation
     90        const wxXmlNode* child = root->GetChildren();
     91
     92        while (child)
     93        {
     94            if (child->GetName() != wxT("Entity"))
     95                return;
     96
     97            //TODO Validate all attributes
     98            child = child->GetNext();
     99        }
     100       
     101        //Update selectedObjects??
     102        g_SelectedObjects.empty();
     103        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
     104
     105        // is the source code get here now you can add the objects to scene(preview)
     106        // store id to move
     107        child = root->GetChildren();
     108       
     109        while (child)
     110        {
     111            wxString templateName;
     112            Position entityPos;
     113            double orientation = 0;
     114
     115            const wxXmlNode* xmlData = child->GetChildren();
     116           
     117            while (xmlData)
     118            {
     119                if (xmlData->GetName() == wxT("Template"))
     120                    templateName = xmlData->GetNodeContent();
     121                else if (xmlData->GetName() == wxT("Position"))
     122                {
     123                    wxString x, z;
     124                    xmlData->GetAttribute(wxT("x"), &x);
     125                    xmlData->GetAttribute(wxT("z"), &z);
     126               
     127                    double aux,aux2;
     128                    x.ToDouble(&aux);
     129                    z.ToDouble(&aux2);
     130
     131                    entityPos = Position(aux, 0, aux2);
     132                }
     133                else if (xmlData->GetName() == wxT("Orientation"))
     134                {
     135                    wxString y;
     136                    xmlData->GetAttribute(wxT("y"), &y);
     137                    y.ToDouble(&orientation);
     138                }
     139
     140                xmlData = xmlData->GetNext();
     141            }
     142
     143            POST_MESSAGE(ObjectPreview, ((std::wstring)templateName.c_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), entityPos, false, Position(), orientation, 0, false));
     144
     145            child = child->GetNext();
     146        }
     147
     148        //Set state paste for preview the new objects
     149        this->SetState(&Pasting);
     150    }
     151
     152    void OnMovingPaste()
     153    {
     154        //Move the preview(s) object(s)
     155        POST_MESSAGE(MoveObjectPreview, ((m_entPosition)));
     156    }
     157
     158    void OnPasteEnd(bool canceled)
     159    {
     160        if (canceled)
     161            //delete previews objects
     162            POST_MESSAGE(ObjectPreview, (_T(""), GetScenarioEditor().GetObjectSettings().GetSettings(), Position(), false, Position(), 0, 0, true));
     163        else
     164        {
     165            //Create new Objects and delete preview objects
     166            POST_MESSAGE(ObjectPreviewToEntity, ());
     167
     168            AtlasMessage::qGetCurrentSelection currentSelection;
     169            currentSelection.Post();
     170
     171            g_SelectedObjects = *currentSelection.ids;
     172        }
     173       
     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            }
    170             else
    171                 return false;
     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                        return true;
     316                    }
     317                }
     318                else if (evt.GetKeyCode() == 'V')
     319                {
     320                    obj->OnPasteStart();
     321                    return true;
     322                }
     323            }
     324           
     325            return false;
    172326        }
    173327    }
    174328    Waiting;
     
    335489        }
    336490    }
    337491    SelectSimilar;
     492
     493    struct sPasting : public State
     494    {
     495        bool OnMouse(TransformObject* obj, wxMouseEvent& evt)
     496        {
     497            if (evt.Moving())
     498            {
     499                //Move the object
     500                obj->m_entPosition = Position(evt.GetPosition());
     501                obj->OnMovingPaste();
     502                return true;
     503            }
     504            else if (evt.LeftDown())
     505            {
     506                //Place the object and update
     507                obj->OnPasteEnd(false);
     508                return true;
     509            }
     510            else
     511                return false;
     512        }
     513        bool OnKey(TransformObject* obj, wxKeyEvent& evt, KeyEventType type)
     514        {
     515            if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
     516            {
     517                obj->OnPasteEnd(true);
     518                return true;
     519            }
     520            else
     521                return false;
     522        }
     523    }
     524    Pasting;
    338525};
    339526
    340527IMPLEMENT_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
     367QUERYHANDLER(GetCurrentSelection)
     368{
     369    msg->ids = g_Selection;
     370}
     371
     372MESSAGEHANDLER(ObjectPreviewToEntity)
     373{
     374    UNUSED2(msg);
     375
     376    if (g_PreviewEntitiesID.size() == 0)
     377        return;
     378
     379    CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     380    ENSURE(cmpTemplateManager);
     381
     382    PlayerColorMap playerColor;
     383
     384    //I need to re create the objects finally delete preview objects
     385    std::vector<entity_id_t>::const_iterator i;
     386    for (i = g_PreviewEntitiesID.begin(); i != g_PreviewEntitiesID.end(); i++)
     387    {
     388        //Get template
     389        entity_id_t ent = *i;
     390        std::string templateName = cmpTemplateManager->GetCurrentTemplateName(ent);
     391        std::wstring wTemplateName(templateName.begin() + 8, templateName.end());
     392        //Create new entity
     393        entity_id_t new_ent = g_Game->GetSimulation2()->AddEntity(wTemplateName);
     394        if (new_ent == INVALID_ENTITY)
     395            continue;
     396
     397        //get position, get rotation
     398        CmpPtr<ICmpPosition> cmpPositionNew(*g_Game->GetSimulation2(), new_ent);
     399        CmpPtr<ICmpPosition> cmpPositionOld(*g_Game->GetSimulation2(), ent);
     400
     401        if (cmpPositionNew && cmpPositionOld)
     402        {
     403            CVector3D pos = cmpPositionOld->GetPosition();
     404            cmpPositionNew->JumpTo(entity_pos_t::FromFloat(pos.X), entity_pos_t::FromFloat(pos.Z));
     405
     406            //now rotate
     407            CFixedVector3D rotation = cmpPositionOld->GetRotation();
     408            cmpPositionNew->SetYRotation(rotation.Y);
     409        }
     410
     411        //get owner
     412        CmpPtr<ICmpOwnership> cmpOwnershipNew(*g_Game->GetSimulation2(), new_ent);
     413        CmpPtr<ICmpOwnership> cmpOwnershipOld(*g_Game->GetSimulation2(), ent);
     414        if (cmpOwnershipNew && cmpOwnershipOld)
     415            cmpOwnershipNew->SetOwner(cmpOwnershipOld->GetOwner());
     416
     417        //getVisual
     418        CmpPtr<ICmpVisual> cmpVisualNew(*g_Game->GetSimulation2(), new_ent);
     419        CmpPtr<ICmpVisual> cmpVisualOld(*g_Game->GetSimulation2(), ent);
     420        if (cmpVisualNew && cmpVisualOld)
     421            cmpVisualNew->SetActorSeed(cmpVisualOld->GetActorSeed());
     422
     423        //Update g_selectedObject and higligth
     424        g_Selection.push_back(new_ent);
     425        CmpPtr<ICmpSelectable> cmpSelectable(*g_Game->GetSimulation2(), new_ent);
     426        if (cmpSelectable)
     427            cmpSelectable->SetSelectionHighlight(GetOwnerPlayerColor(playerColor, new_ent), true);
     428
     429        g_Game->GetSimulation2()->DestroyEntity(ent);
     430    }
     431    g_PreviewEntitiesID.clear();
     432
     433}
     434
     435MESSAGEHANDLER(MoveObjectPreview)
     436{
     437    if (g_PreviewEntitiesID.size()==0)
     438        return;
     439
     440    //TODO:Change pivot
     441    entity_id_t referenceEntity = *g_PreviewEntitiesID.begin();
     442
     443    // All selected objects move relative to a pivot object,
     444    //  so get its position and whether it's floating
     445    CFixedVector3D referencePos;
     446   
     447    CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), referenceEntity);
     448    if (cmpPosition && cmpPosition->IsInWorld())
     449        referencePos = cmpPosition->GetPosition();
     450       
     451
     452    // Calculate directional vector of movement for pivot object,
     453    //  we apply the same movement to all objects
     454    CVector3D targetPos = GetUnitPos(msg->pos, true);
     455    CFixedVector3D fTargetPos(entity_pos_t::FromFloat(targetPos.X), entity_pos_t::FromFloat(targetPos.Y), entity_pos_t::FromFloat(targetPos.Z));
     456    CFixedVector3D dir = fTargetPos - referencePos;
     457
     458    for (size_t i = 0; i < g_PreviewEntitiesID.size(); ++i)
     459    {
     460        entity_id_t id = (entity_id_t)g_PreviewEntitiesID[i];
     461        CFixedVector3D posFinal;
     462        CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     463        if (cmpPosition && cmpPosition->IsInWorld())
     464        {
     465            // Calculate this object's position
     466            CFixedVector3D posFixed = cmpPosition->GetPosition();
     467            posFinal = posFixed + dir;
     468        }
     469        cmpPosition->JumpTo(posFinal.X, posFinal.Z);
     470    }
     471}
     472
    314473MESSAGEHANDLER(ObjectPreview)
    315474{
    316475    // If the selection has changed...
    317     if (*msg->id != g_PreviewUnitName)
     476    if (*msg->id != g_PreviewUnitName || (!msg->cleanObjectPreviews))
    318477    {
    319478        // Delete old entity
    320         if (g_PreviewEntityID != INVALID_ENTITY)
    321             g_Game->GetSimulation2()->DestroyEntity(g_PreviewEntityID);
     479        if (g_PreviewEntityID != INVALID_ENTITY && msg->cleanObjectPreviews)
     480        {
     481            //Time to delete all preview objects
     482            if (g_PreviewEntitiesID.size() > 0)
     483            {
     484                std::vector<entity_id_t>::const_iterator i;
     485                for (i = g_PreviewEntitiesID.begin(); i != g_PreviewEntitiesID.end(); i++)
     486                {
     487                    g_Game->GetSimulation2()->DestroyEntity(*i);
     488                }
     489                g_PreviewEntitiesID.clear();
     490            }
     491        }
    322492
    323493        // Create the new entity
    324494        if ((*msg->id).empty())
    325495            g_PreviewEntityID = INVALID_ENTITY;
    326496        else
     497        {
    327498            g_PreviewEntityID = g_Game->GetSimulation2()->AddLocalEntity(L"preview|" + *msg->id);
     499            g_PreviewEntitiesID.push_back(g_PreviewEntityID);
     500        }
     501       
    328502
    329503        g_PreviewUnitName = *msg->id;
    330504    }
  • 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//Query for get selected objects
     341QUERY(GetCurrentSelection,
     342    , //No inputs
     343    ((std::vector<ObjectID>, ids))
     344    );
     345
     346// Moving Preview(s) object together, default is using the firs element in vector
     347MESSAGE(MoveObjectPreview,
     348        ((Position,pos))
     349        );
     350
    337351// Preview object in the game world - creates a temporary unit at the given
    338352// position, and removes it when the preview is next changed
    339353MESSAGE(ObjectPreview,
     
    344358        ((Position, target))
    345359        ((float, angle))
    346360        ((unsigned int, actorseed))
     361        ((bool, cleanObjectPreviews))
    347362        );
    348363
    349364COMMAND(CreateObject, NOMERGE,
     
    590605        ((sObjectSettings, settings))
    591606        );
    592607
     608QUERY(GetObjectMapSettings,
     609        ((std::vector<ObjectID>, ids))
     610        ,
     611        ((std::wstring, xmldata))
     612        );
     613
     614
    593615QUERY(GetPlayerObjects,
    594616        ((int, player))
    595617        ,