Ticket #2642: 2642_3.diff

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

     
    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&)
     642{
     643    if (GetToolManager().GetCurrentToolName() == _T("TransformObject"))
     644    {
     645        GetToolManager().GetCurrentTool()->OnCommand(_T("copy"), NULL);
     646    }
     647}
     648
     649void ScenarioEditor::OnPaste(wxCommandEvent&)
     650{
     651    if (GetToolManager().GetCurrentToolName() != _T("TransformObject"))
     652    {
     653        GetToolManager().SetCurrentTool(_T("TransformObject"), NULL);
     654    }
     655
     656    GetToolManager().GetCurrentTool()->OnCommand(_T("paste"), NULL);
     657}
     658
    624659//////////////////////////////////////////////////////////////////////////
    625660
    626661void ScenarioEditor::OnNew(wxCommandEvent& WXUNUSED(event))
    void ScenarioEditor::OnDumpState(wxCommandEvent& event)  
    866901    }
    867902}
    868903
     904void ScenarioEditor::OnSelectedObjectsChange(std::vector<ObjectID> const& selectedObjects)
     905{
     906    GetMenuBar()->Enable(ID_Copy, !selectedObjects.empty());
     907}
     908
     909void ScenarioEditor::OnMenuOpen(wxMenuEvent& event)
     910{
     911    // This could be done far more elegantly if wxMenuItem had changeable id.
     912    wxMenu* pasteMenuItem = NULL;
     913    event.GetMenu()->FindItem(ID_Paste, &pasteMenuItem);
     914
     915    if (!pasteMenuItem)
     916    {
     917        GetMenuBar()->Enable(ID_Paste, false);
     918        return;
     919    }
     920
     921    wxString content;
     922    if (wxTheClipboard->Open())
     923    {
     924        if (wxTheClipboard->IsSupported(wxDF_TEXT))
     925        {
     926            wxTextDataObject data;
     927            wxTheClipboard->GetData(data);
     928            content = data.GetText();
     929        }
     930
     931        wxTheClipboard->Close();
     932    }
     933    else
     934    {
     935        GetMenuBar()->Enable(ID_Paste, false);
     936        return;
     937    }
     938
     939    if (content.empty())
     940    {
     941        GetMenuBar()->Enable(ID_Paste, false);
     942        return;
     943    }
     944
     945    wxInputStream* is = new wxStringInputStream(content);
     946    wxXmlDocument doc;
     947    {
     948        wxLogNull stopComplaining;
     949        static_cast<void>(stopComplaining);
     950        if (!doc.Load(*is))
     951        {
     952            GetMenuBar()->Enable(ID_Paste, false);
     953            return;
     954        }
     955    }
     956
     957    wxXmlNode* root = doc.GetRoot();
     958    if (root->GetName() != wxT("Entities"))
     959    {
     960        GetMenuBar()->Enable(ID_Paste, false);
     961        return;
     962    }
     963
     964    GetMenuBar()->Enable(ID_Paste, true);
     965}
     966
    869967
    870968//////////////////////////////////////////////////////////////////////////
    871969
  • source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h

    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(std::vector<AtlasMessage::ObjectID> const& 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

    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(wxString const&, void*) {}
    3132} dummy;
    3233
    3334struct ToolManagerImpl
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h

    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(wxString const& 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(wxString const&, void*)
     172    {
     173    }
    169174};
    170175
    171176
  • source/tools/atlas/AtlasUI/ScenarioEditor/Tools/TransformObject.cpp

    public:  
    5050        SetState(&Waiting);
    5151    }
    5252
     53    virtual void OnCommand(wxString const& command, void*)
     54    {
     55        if (command == _T("copy"))
     56        {
     57            OnCopy();
     58        }
     59        else if (command == _T("paste"))
     60        {
     61            OnPasteStart();
     62        }
     63    }
     64
    5365    void OnDisable()
    5466    {
    5567        g_SelectedObjects.clear();
    public:  
    5769        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
    5870    }
    5971
     72    bool OnCopy() const
     73    {
     74        if (g_SelectedObjects.empty())
     75        {
     76            return false;
     77        }
     78
     79        AtlasMessage::qGetObjectMapSettings info(g_SelectedObjects);
     80        info.Post();
     81
     82        if (wxTheClipboard->Open())
     83        {
     84            wxString text(info.xmldata.c_str());
     85            wxTheClipboard->SetData(new wxTextDataObject(text));
     86            wxTheClipboard->Close();
     87        }
     88
     89        return true;
     90    }
     91
    6092    void OnPasteStart()
    6193    {
    62         //PASTE
    6394        wxString entities;
    6495        if (wxTheClipboard->Open())
    6596        {
    public:  
    71102            }
    72103            wxTheClipboard->Close();
    73104        }
    74         else
    75         {
    76             //TODO: Show something when the clipboard couldnt open
    77         }
    78105
    79         //First do we need to check if it is a correct xml string
     106        // First do we need to check if it is a correct xml string
    80107        wxInputStream* is = new wxStringInputStream(entities);
    81108        wxXmlDocument doc;
    82109        if (!doc.Load(*is))
    public:  
    87114        if (root->GetName() != wxT("Entities"))
    88115            return;
    89116
    90         //  Template, position,orientation
     117        // Template, position,orientation
    91118        const wxXmlNode* child = root->GetChildren();
    92119
    93120        while (child)
    public:  
    95122            if (child->GetName() != wxT("Entity"))
    96123                return;
    97124
    98             //TODO Validate all attributes
    99125            child = child->GetNext();
    100126        }
    101127
    102         //Update selectedObjects??
    103128        g_SelectedObjects.clear();
    104129        POST_MESSAGE(SetSelectionPreview, (g_SelectedObjects));
    105130
    public:  
    329354            {
    330355                if (evt.GetKeyCode() == 'C')
    331356                {
    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                     }
     357                    return obj->OnCopy();
    352358                }
    353359                else if (evt.GetKeyCode() == 'V')
    354360                {