Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorColour.cpp
blobd1b61756b2bc77404a483763dfb49821c30bfd14
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorColour.h"
22 #include "EditorOLC.h"
23 #include "UBSocket.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
26 #include "Array.h"
27 #include "Command.h"
28 #include "Permission.h"
29 #include "Account.h"
30 #include "Colour.h"
31 #include "ColourManager.h"
32 #include "Managers.h"
34 using mud::Colour;
36 typedef EditorColour E;
37 typedef CommandInfoObject<E> O;
38 typedef CommandBinding<E> B;
40 // name function need: object lock
41 static O editName( "Name", &E::editName, true, true);
42 static O editColourString("ColourString",&E::editColourString,true,true);
43 static O saveColour("Save", &E::saveColour, true, true);
44 static O showColour("Show", &E::showColour, true, false);
46 static const B commands[] = {
47 B("colourstring", editColourString),
48 B("name", editName),
49 B("save", saveColour),
50 B("show", showColour),
53 EditorColour::EditorColour(UBSocket* sock) :
54 OLCEditor(sock),
55 m_commands(commands, array_size(commands)),
56 m_colour()
58 listCommands(Global::Get()->EmptyString);
61 EditorColour::~EditorColour(void)
66 std::string EditorColour::lookup(const std::string& action)
68 std::string name = OLCEditor::lookup(action);
69 if(name.size() != 0)
70 return name;
72 const ColourCommand* act = (ColourCommand*)m_commands.getObject(action);
73 if(act)
74 return act->getName();
76 return Global::Get()->EmptyString;
79 void EditorColour::dispatch(const std::string& action, const std::string& argument)
81 const ColourCommand* act = (ColourCommand*)m_commands.getObject(action);
83 if(!act)
85 OLCEditor::dispatch(action, argument);
86 return;
89 if(!m_colour)
91 m_sock->Send("You need to be editing a colour first.\n");
92 m_sock->Send("(Use the 'edit' command.)\n");
93 return;
96 if(act->needLock())
98 try {
99 m_colour->Lock();
100 } catch(SavableLocked& e) {
101 m_sock->Send("The colour you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
102 m_sock->Send("Please try again later.\n");
103 return;
107 act->Run(this, argument);
108 return;
111 SavablePtr EditorColour::getEditing()
113 return m_colour;
116 TableImplPtr EditorColour::getTable()
118 return db::TableImpls::Get()->COLOURS;
121 KeysPtr EditorColour::addNew()
123 return mud::Managers::Get()->Colour->Add();
126 std::vector<std::string> EditorColour::getList()
128 return mud::Managers::Get()->Colour->List();
131 void EditorColour::setEditing(KeysPtr keys)
133 if(!keys->size())
135 m_colour.reset();
136 return;
139 m_colour = mud::Managers::Get()->Colour->GetByKey(keys->first()->getIntegerValue());
140 return;
143 std::vector<std::string> EditorColour::getCommands()
145 return m_commands.getCommandsVector();
148 void EditorColour::editName(const std::string& argument)
150 if(argument.size() == 0)
152 m_sock->Send("Colour name can't be zero length!\n");
153 return;
156 m_sock->Sendf("Colour name changed from '%s' to '%s'.\n", m_colour->getName().c_str(), argument.c_str());
157 m_colour->setName(argument);
158 return;
161 void EditorColour::editColourString(const std::string& argument)
163 if(!m_colour->Exists())
165 m_sock->Send("For some reason the colour you are editing does not exist.\n");
166 return;
169 if(argument.size() == 0)
171 m_sock->Send("No argument, dropping you into the string editor!\n");
172 return;
175 m_sock->Sendf("Colour ColourString changed from '%s' to '%s'.\n", m_colour->getColourString().c_str(), argument.c_str());
176 m_colour->setColourString(argument);
177 return;
180 void EditorColour::saveColour(const std::string& argument)
182 m_sock->Send("Saving...\n");
183 m_colour->Save();
184 m_sock->Send("Saved.\n");
185 return;
188 void EditorColour::showColour(const std::string& argument)
190 m_sock->Send(m_colour->toString());
191 return;