Changeset 122

Show
Ignore:
Timestamp:
08/19/10 19:21:08 (18 months ago)
Author:
Carsten
Message:

CaWE GUI editor:

  • Lua errors on loading a GUI are now presented to the user.
  • Window names are checked for unique-ness now (and silently repaired, if necessary) when the GUI is saved.

Fixes #37.

Location:
cafu/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • cafu/trunk/CaWE/GuiEditor/Commands/Paste.cpp

    r36 r122  
    2323 
    2424#include "Paste.hpp" 
    25  
    2625#include "../GuiDocument.hpp" 
    27  
    2826#include "../EditorData/Window.hpp" 
    29  
    3027#include "GuiSys/Window.hpp" 
    3128 
     
    7875{ 
    7976    wxASSERT(!m_Done); 
    80  
    8177    if (m_Done) return false; 
    8278 
     
    8480    { 
    8581        m_Windows[WinNr]->Parent=m_NewParent; 
    86  
    8782        m_NewParent->Children.PushBack(m_Windows[WinNr]); 
    8883 
    89         // If current window name is not unique within the window structure it was pasted into, 
    90         // create a new unique name. 
    91         wxString CurrentWindowName=m_Windows[WinNr]->Name; 
    92         unsigned long Counter=1; 
    93  
    94         while (!(((EditorDataWindowT*)m_Windows[WinNr]->EditorData)->CheckNameUniqueness(m_Windows[WinNr]->Name))) 
    95         { 
    96             std::ostringstream NameStream; 
    97             NameStream << CurrentWindowName << "_" << Counter; 
    98             Counter++; 
    99  
    100             m_Windows[WinNr]->Name=NameStream.str(); 
    101         } 
     84        // If the name of the window is not unique among its siblings, find a new unique name. 
     85        ((EditorDataWindowT*)m_Windows[WinNr]->EditorData)->RepairNameUniqueness(); 
    10286    } 
    10387 
     
    10589 
    10690    m_Done=true; 
    107  
    10891    return true; 
    10992} 
     
    11396{ 
    11497    wxASSERT(m_Done); 
    115  
    11698    if (!m_Done) return; 
    11799 
  • cafu/trunk/CaWE/GuiEditor/EditorData/Window.cpp

    r36 r122  
    3434 
    3535 
    36 static unsigned int Counter=1; 
    37  
    38  
    3936EditorDataWindowT::EditorDataWindowT(cf::GuiSys::WindowT* Window, GuiDocumentT* GuiDocument) 
    4037    : cf::GuiSys::EditorDataT(Window), 
    4138      Selected(false), 
    42       m_GuiDocument(GuiDocument) 
     39      m_GuiDocument(GuiDocument), 
     40      m_Counter(1) 
    4341{ 
    4442    // If window has no name, create default name. 
    45     if (Window->Name=="") 
    46     { 
    47         std::ostringstream NameStream; 
    48         NameStream << "Window" << "_" << Counter; 
    49         Counter++; 
    50         Window->Name=NameStream.str(); 
    51     } 
     43    if (Window->Name=="") Window->Name="Window"; 
    5244 
    5345    // Check window name uniqueness and repair it. 
     
    8072 
    8173 
    82 void EditorDataWindowT::RepairNameUniqueness() 
    83 { 
    84     // Check if windows current name is unique. 
    85     std::string CurrentName=m_GuiWindow->Name; 
    86     std::string NewName    =CurrentName; 
    87  
    88     // While current window name is not unique. 
    89     while (!CheckNameUniqueness(NewName)) 
    90     { 
    91         // Append continous number to name until the name is unique. 
    92         std::ostringstream NameStream; 
    93         NameStream << CurrentName << "_" << Counter; 
    94         NewName=NameStream.str(); 
    95         Counter++; 
    96     } 
    97  
    98     m_GuiWindow->Name=NewName; 
    99 } 
    100  
    101  
    102 bool EditorDataWindowT::CheckNameUniqueness(wxString Name) 
     74bool EditorDataWindowT::CheckNameUniqueness(const wxString& Name) const 
    10375{ 
    10476    if (m_GuiWindow->GetRoot()==m_GuiWindow) return true; // Root window can have any name. 
     
    11890    return true; 
    11991} 
     92 
     93 
     94static wxString StripSuffix(const wxString& Str) 
     95{ 
     96    const size_t Pos=Str.rfind("_"); 
     97 
     98    if (Pos==std::string::npos) return Str; 
     99 
     100    for (size_t i=Pos+1; i<Str.length(); i++) 
     101        if (!wxIsdigit(Str[i])) return Str; 
     102 
     103    if (Pos==0) return "Window"; 
     104 
     105    return Str.Left(Pos); 
     106} 
     107 
     108 
     109void EditorDataWindowT::RepairNameUniqueness() 
     110{ 
     111    const wxString BaseName=StripSuffix(m_GuiWindow->Name); 
     112    wxString       NewName =m_GuiWindow->Name; 
     113 
     114    while (!CheckNameUniqueness(NewName)) 
     115    { 
     116        NewName=BaseName+"_"+wxString::Format("%u", m_Counter); 
     117        m_Counter++; 
     118    } 
     119 
     120    m_GuiWindow->Name=NewName; 
     121} 
  • cafu/trunk/CaWE/GuiEditor/EditorData/Window.hpp

    r36 r122  
    5050 
    5151        /// Checks the name uniqueness of a new name string within the windows siblings. 
    52         /// @param Name Name to check for uniqueness. 
     52        /// @param Name    Name to check for uniqueness. 
    5353        /// @return Whether this name is unique. 
    54         bool CheckNameUniqueness(wxString Name); 
     54        bool CheckNameUniqueness(const wxString& Name) const; 
     55 
     56        /// Helper method to check and auto-repair the uniqueness of the name of this window. 
     57        void RepairNameUniqueness(); 
    5558 
    5659        GuiDocumentT* GetGuiDoc() { return m_GuiDocument; } 
     
    6265 
    6366        GuiDocumentT* m_GuiDocument; 
    64  
    65         /// Helper method to check and repair the uniqueness of the name of this window. 
    66         void RepairNameUniqueness(); 
     67        unsigned int  m_Counter; 
    6768    }; 
    6869} 
  • cafu/trunk/CaWE/GuiEditor/GuiDocument.cpp

    r36 r122  
    6161        cf::GuiSys::GuiImplT Gui(gifn); 
    6262 
     63        if (Gui.GetScriptInitResult()!="") 
     64        { 
     65            // GuiImplT::InitErrorT excecptions are caught in the caller code. 
     66            // Here we handle the case that initializing the GUI succeeded "halfway". 
     67            wxMessageBox("GUI file "+GuiInitFileName+" was loaded, but errors occurred:\n\n"+ 
     68                         Gui.GetScriptInitResult()+"\n\n"+ 
     69                         "You may choose to ignore this error and proceed,\n"+ 
     70                         "but it is probably better to edit the file and manually fix the problem first.", 
     71                         "GUI initialization warning", wxOK | wxICON_EXCLAMATION); 
     72        } 
     73 
    6374        m_GuiProperties=GuiPropertiesT(Gui); 
    6475 
     
    159170 
    160171 
     172// Recursively makes sure that the children of each window have unique names. 
     173// Normally the other GUI editor code should make sure that that is always true, but right now it 
     174// is possible to create violations of this constraint via drag-and-drop in the window hierarchy tree 
     175// (can drop a window as a child of another window that already has a child with the same name). 
     176static void CheckWindowNames(cf::GuiSys::WindowT* Window) 
     177{ 
     178    const std::string OldName=Window->Name; 
     179 
     180    ((EditorDataWindowT*)Window->EditorData)->RepairNameUniqueness(); 
     181 
     182    if (Window->Name!=OldName) 
     183        ((EditorDataWindowT*)Window->EditorData)->GetGuiDoc()->UpdateAllObservers_Modified(Window, WMD_PROPERTY_CHANGED, "Name"); 
     184 
     185    for (unsigned long ChildNr=0; ChildNr<Window->Children.Size(); ChildNr++) 
     186        CheckWindowNames(Window->Children[ChildNr]); 
     187} 
     188 
     189 
    161190// Recursively saves the window instantiation of the passed window and all of its children. 
    162191static void SaveWindowInstantiation(std::ostream& OutFile, cf::GuiSys::WindowT* Window, const wxString& ParentName) 
     
    239268bool GuiDocumentT::SaveInit_cgui(std::ostream& OutFile) 
    240269{ 
     270    CheckWindowNames(m_RootWindow); 
     271 
    241272    OutFile << "-- This is a Cafu engine GUI script file, written by CaWE, the Cafu World Editor.\n"; 
    242273    OutFile << "-- You CAN edit this file manually, but note that CaWE may overwrite your changes.\n"; 
     
    262293    OutFile << "\n"; 
    263294 
    264     SaveWindowHierarchy   (OutFile, m_RootWindow, ""); 
     295    SaveWindowHierarchy(OutFile, m_RootWindow, ""); 
    265296 
    266297    OutFile << "\n"; 
  • cafu/trunk/Libs/GuiSys/GuiImpl.cpp

    r102 r122  
    5454 
    5555using namespace cf::GuiSys; 
     56 
     57 
     58GuiImplT::InitErrorT::InitErrorT(const std::string& Message) 
     59    : std::runtime_error(Message) 
     60{ 
     61} 
    5662 
    5763 
     
    219225        // problem. That is, there might still be a case left where we might want to throw. 
    220226        lua_close(LuaState); 
    221         throw InitErrorT(/*"No root window set. Probable cause:\n"+ScriptInitResult*/); 
     227        throw InitErrorT("No root window set. Probable cause:\n"+ScriptInitResult); 
    222228    } 
    223229 
  • cafu/trunk/Libs/GuiSys/GuiImpl.hpp

    r36 r122  
    4949            public: 
    5050 
    51             /// GUI initialization error. 
    52             class InitErrorT { }; 
     51            class InitErrorT; 
    5352 
    5453 
     
    158157} 
    159158 
     159 
     160/// A class that is thrown on GUI initialization errors. 
     161class cf::GuiSys::GuiImplT::InitErrorT : public std::runtime_error 
     162{ 
     163    public: 
     164 
     165    InitErrorT(const std::string& Message); 
     166}; 
     167 
    160168#endif