Ticket #2642: 2642_4.diff

File 2642_4.diff, 9.6 KB (added by stilz_, 9 years ago)
  • source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp

    Commit:  [33ma767c6dc3829bda8c0d46881547807809a05bbce
    Author:  [32mSławomir Zborowski <slawomir.zborowski@nsn.com>
    Date:    ([31m4 months ago[m) 2014-07-09 18:27:23 +0200
    Subject:  #2642: Add Copy and Paste to the Edit menu in Atlas
    
    
    
    diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp b/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp
    index 0065c13..d30eac3 100644
    a b  
    2020#include "ScenarioEditor.h"
    2121
    2222#include "wx/busyinfo.h"
     23#include "wx/clipbrd.h"
    2324#include "wx/config.h"
    2425#include "wx/dir.h"
    2526#include "wx/evtloop.h"
    2627#include "wx/ffile.h"
    2728#include "wx/filename.h"
    2829#include "wx/image.h"
     30#include "wx/sstream.h"
    2931#include "wx/sysopt.h"
    3032#include "wx/tooltip.h"
     33#include "wx/xml/xml.h"
    3134
    3235#include "General/AtlasEventLoop.h"
    3336#include "General/Datafile.h"
    enum  
    310313    ID_SaveAs,
    311314    ID_ImportHeightmap,
    312315
     316    ID_Copy,
     317    ID_Paste,
     318
    313319    ID_Wireframe,
    314320    ID_MessageTrace,
    315321    ID_Screenshot,
    BEGIN_EVENT_TABLE(ScenarioEditor, wxFrame)  
    338344    EVT_MENU(ID_Quit, ScenarioEditor::OnQuit)
    339345    EVT_MENU(wxID_UNDO, ScenarioEditor::OnUndo)
    340346    EVT_MENU(wxID_REDO, ScenarioEditor::OnRedo)
     347    EVT_MENU(ID_Copy, ScenarioEditor::OnCopy)
     348    EVT_MENU(ID_Paste, ScenarioEditor::OnPaste)
    341349
    342350    EVT_MENU(ID_Wireframe, ScenarioEditor::OnWireframe)
    343351    EVT_MENU(ID_MessageTrace, ScenarioEditor::OnMessageTrace)
    BEGIN_EVENT_TABLE(ScenarioEditor, wxFrame)  
    350358    EVT_MENU(ID_RenderPathFixed, ScenarioEditor::OnRenderPath)
    351359    EVT_MENU(ID_RenderPathShader, ScenarioEditor::OnRenderPath)
    352360
     361    EVT_MENU_OPEN(ScenarioEditor::OnMenuOpen)
     362
    353363    EVT_IDLE(ScenarioEditor::OnIdle)
    354364END_EVENT_TABLE()
    355365
    ScenarioEditor::ScenarioEditor(wxWindow* parent)  
    435445    {
    436446        menuEdit->Append(wxID_UNDO, _("&Undo"));
    437447        menuEdit->Append(wxID_REDO, _("&Redo"));
     448        menuEdit->AppendSeparator();
     449        menuEdit->Append(ID_Copy, _("&Copy"));
     450        menuEdit->Enable(ID_Copy, false);
     451        menuEdit->Append(ID_Paste, _("&Paste"));
     452        menuEdit->Enable(ID_Paste, false);
    438453    }
    439454
     455    g_SelectedObjects.RegisterObserver(0, &ScenarioEditor::OnSelectedObjectsChange, this);
     456
    440457    GetCommandProc().SetEditMenu(menuEdit);
    441458    GetCommandProc().Initialize();
    442459
    void ScenarioEditor::OnRedo(wxCommandEvent&)  
    621638    GetCommandProc().Redo();
    622639}
    623640
     641void ScenarioEditor::OnCopy(wxCommandEvent& WXUNUSED(event))
     642{
     643    if (GetToolManager().GetCurrentToolName() == _T("TransformObject"))
     644        GetToolManager().GetCurrentTool()->OnCommand(_T("copy"), NULL);
     645}
     646
     647void ScenarioEditor::OnPaste(wxCommandEvent& WXUNUSED(event))
     648{
     649    if (GetToolManager().GetCurrentToolName() != _T("TransformObject"))
     650        GetToolManager().SetCurrentTool(_T("TransformObject"), NULL);
     651
     652    GetToolManager().GetCurrentTool()->OnCommand(_T("paste"), NULL);
     653}
     654
    624655//////////////////////////////////////////////////////////////////////////
    625656
    626657void ScenarioEditor::OnNew(wxCommandEvent& WXUNUSED(event))
    void ScenarioEditor::OnDumpState(wxCommandEvent& event)  
    866897    }
    867898}
    868899
     900void ScenarioEditor::OnSelectedObjectsChange(const std::vector<ObjectID>& selectedObjects)
     901{
     902    GetMenuBar()->Enable(ID_Copy, !selectedObjects.empty());
     903}
     904
     905void ScenarioEditor::OnMenuOpen(wxMenuEvent& event)
     906{
     907    // This could be done far more elegantly if wxMenuItem had changeable id.
     908    wxMenu* pasteMenuItem = NULL;
     909    event.GetMenu()->FindItem(ID_Paste, &pasteMenuItem);
     910
     911    GetMenuBar()->Enable(ID_Paste, false);
     912
     913    if (!pasteMenuItem)
     914        return;
     915
     916    wxString content;
     917    if (wxTheClipboard->Open())
     918    {
     919        if (wxTheClipboard->IsSupported(wxDF_TEXT))
     920        {
     921            wxTextDataObject data;
     922            wxTheClipboard->GetData(data);
     923            content = data.GetText();
     924        }
     925
     926        wxTheClipboard->Close();
     927    }
     928
     929    if (content.empty())
     930        return;
     931
     932    wxInputStream* is = new wxStringInputStream(content);
     933    wxXmlDocument doc;
     934    {
     935        wxLogNull stopComplaining;
     936        static_cast<void>(stopComplaining);
     937        if (!doc.Load(*is))
     938            return;
     939    }
     940
     941    wxXmlNode* root = doc.GetRoot();
     942    if (!root || root->GetName() != wxT("Entities"))
     943        return;
     944
     945    GetMenuBar()->Enable(ID_Paste, true);
     946}
     947
    869948
    870949//////////////////////////////////////////////////////////////////////////
    871950
  • source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h

    diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h b/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h
    index e3c16ea..9eb95b8 100644
    a b public:  
    4646    void OnQuit(wxCommandEvent& event);
    4747    void OnUndo(wxCommandEvent& event);
    4848    void OnRedo(wxCommandEvent& event);
     49    void OnCopy(wxCommandEvent& event);
     50    void OnPaste(wxCommandEvent& event);
    4951
    5052    void OnWireframe(wxCommandEvent& event);
    5153    void OnMessageTrace(wxCommandEvent& event);
    public:  
    5557    void OnCameraReset(wxCommandEvent& event);
    5658    void OnRenderPath(wxCommandEvent& event);
    5759    void OnDumpState(wxCommandEvent& event);
     60    void OnSelectedObjectsChange(const std::vector<AtlasMessage::ObjectID>& selectedObjects);
     61
     62    void OnMenuOpen(wxMenuEvent& event);
    5863
    5964    bool OpenFile(const wxString& name, const wxString& filename);
    6065
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.cpp

    diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.cpp b/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.cpp
    index 7327362..767a357 100644
    a b class DummyTool : public ITool  
    2828    bool OnMouse(wxMouseEvent& WXUNUSED(evt)) { return false; }
    2929    bool OnKey(wxKeyEvent& WXUNUSED(evt), KeyEventType) { return false; }
    3030    void OnTick(float) {}
     31    void OnCommand(const wxString& WXUNUSED(comand), void* WXUNUSED(userData)) {}
    3132} dummy;
    3233
    3334struct ToolManagerImpl
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h

    diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h b/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h
    index de338f4..181666d 100644
    a b public:  
    3535    virtual bool OnMouse(wxMouseEvent& evt) = 0; // return true if handled
    3636    virtual bool OnKey(wxKeyEvent& evt, KeyEventType dir) = 0; // return true if handled
    3737    virtual void OnTick(float dt) = 0; // dt in seconds
     38    virtual void OnCommand(const wxString& command, void* userData) = 0;
    3839
    3940    virtual ~ITool() {};
    4041};
    private:  
    166167    {
    167168        m_CurrentState->OnTick(static_cast<T*>(this), dt);
    168169    }
     170
     171    virtual void OnCommand(const wxString& WXUNUSED(command), void* WXUNUSED(userData))
     172    {
     173    }
    169174};
    170175
    171176
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.cpp

    diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.cpp b/source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.cpp
    index 375e0c8..50f95b6 100644
    a b public:  
    5050        SetState(&Waiting);
    5151    }
    5252
     53    virtual void OnCommand(const wxString& command, void* WXUNUSED(userData))
     54    {
     55        if (command == _T("copy"))
     56            OnCopy();
     57        else if (command == _T("paste"))
     58            OnPasteStart();
     59    }
     60
    5361    void OnDisable()
    5462    {
    5563        g_SelectedObjects.clear();
    public:  
    5765        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
    5866    }
    5967
     68    bool OnCopy() const
     69    {
     70        if (g_SelectedObjects.empty())
     71            return false;
     72
     73        AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
     74        info.Post();
     75
     76        if (wxTheClipboard->Open())
     77        {
     78            wxString text(info.xmldata.c_str());
     79            wxTheClipboard->SetData(new wxTextDataObject(text));
     80            wxTheClipboard->Close();
     81        }
     82
     83        return true;
     84    }
     85
    6086    void OnPasteStart()
    6187    {
    62         //PASTE
    6388        wxString entities;
    6489        if (wxTheClipboard->Open())
    6590        {
    public:  
    7196            }
    7297            wxTheClipboard->Close();
    7398        }
    74         else
    75         {
    76             //TODO: Show something when the clipboard couldnt open
    77         }
    7899
    79         //First do we need to check if it is a correct xml string
     100        // First do we need to check if it is a correct xml string
    80101        wxInputStream* is = new wxStringInputStream(entities);
    81102        wxXmlDocument doc;
    82103        if (!doc.Load(*is))
    public:  
    87108        if (root->GetName() != wxT("Entities"))
    88109            return;
    89110
    90         //  Template, position,orientation
     111        // Template, position,orientation
    91112        const wxXmlNode* child = root->GetChildren();
    92113
    93114        while (child)
    public:  
    95116            if (child->GetName() != wxT("Entity"))
    96117                return;
    97118
    98             //TODO Validate all attributes
    99119            child = child->GetNext();
    100120        }
    101121
    102         //Update selectedObjects??
    103122        g_SelectedObjects.clear();
    104123        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
    105124
    public:  
    329348            {
    330349                if (evt.GetKeyCode() == 'C')
    331350                {
    332                     if (!g_SelectedObjects.empty())
    333                     {
    334                         //COPY current selections
    335                         AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
    336                         info.Post();
    337 
    338                         //In xmldata is the configuration
    339                         //now send to clipboard
    340                         if (wxTheClipboard->Open())
    341                         {
    342                             wxString text(info.xmldata.c_str());
    343                             wxTheClipboard->SetData(new wxTextDataObject(text));
    344                             wxTheClipboard->Close();
    345                         }
    346                         else
    347                         {
    348                             //TODO: Say something about couldnt open clipboard
    349                         }
    350                         return true;
    351                     }
     351                    return obj->OnCopy();
    352352                }
    353353                else if (evt.GetKeyCode() == 'V')
    354354                {