Reduce coupling between ValueEditors/Viewers and Set and StyleSheet.

- Adding of actions is done with an addAction function
 - Files are read from
     - getStylePackage for styling stuff (this is stylesheet)
     - getLocalPackage for symbol and image values (this was the set)


git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@970 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-06-04 00:21:06 +00:00
parent 6b0a0fd098
commit ae14784fd6
50 changed files with 413 additions and 184 deletions
+4 -7
View File
@@ -11,7 +11,6 @@
#include <gui/util.hpp>
#include <gui/thumbnail_thread.hpp>
#include <data/action/value.hpp>
#include <data/stylesheet.hpp>
#include <script/image.hpp>
#include <wx/imaglist.h>
@@ -28,7 +27,6 @@ class ChoiceThumbnailRequest : public ThumbnailRequest {
bool isThreadSafe;
virtual bool threadSafe() const {return isThreadSafe;}
private:
StyleSheetP stylesheet;
int id;
inline ChoiceStyle& style() { return *static_cast<ChoiceStyle*>(viewer().getStyle().get()); }
@@ -38,12 +36,11 @@ class ChoiceThumbnailRequest : public ThumbnailRequest {
ChoiceThumbnailRequest::ChoiceThumbnailRequest(ValueViewer* viewer, int id, bool from_disk, bool thread_safe)
: ThumbnailRequest(
static_cast<void*>(viewer),
viewer->viewer.stylesheet->name() + _("/") + viewer->getField()->name + _("/") << id,
from_disk ? viewer->viewer.stylesheet->lastModified()
viewer->getStylePackage().name() + _("/") + viewer->getField()->name + _("/") << id,
from_disk ? viewer->getStylePackage().lastModified()
: wxDateTime::Now()
)
, isThreadSafe(thread_safe)
, stylesheet(viewer->viewer.stylesheet)
, id(id)
{}
@@ -52,7 +49,7 @@ Image ChoiceThumbnailRequest::generate() {
String name = cannocial_name_form(s.field().choices->choiceName(id));
ScriptableImage& img = s.choice_images[name];
return img.isReady()
? img.generate(GeneratedImage::Options(16,16, stylesheet.get(), viewer().viewer.getSet().get(), ASPECT_BORDER, true))
? img.generate(GeneratedImage::Options(16,16, &viewer().getStylePackage(), &viewer().getLocalPackage(), ASPECT_BORDER, true))
: wxImage();
}
@@ -308,5 +305,5 @@ void ChoiceValueEditor::determineSize(bool) {
}
void ChoiceValueEditor::change(const Defaultable<String>& c) {
perform(value_action(card(), valueP(), c));
addAction(value_action(valueP(), c));
}
-1
View File
@@ -36,7 +36,6 @@ class ChoiceValueEditor : public ChoiceValueViewer, public ValueEditor {
private:
DropDownListP drop_down;
friend class DropDownChoiceList;
friend class ChoiceThumbnailRequest;
/// Change the choice
void change(const Defaultable<String>& c);
};
+1 -1
View File
@@ -150,7 +150,7 @@ void ColorValueEditor::determineSize(bool) {
}
void ColorValueEditor::change(const Defaultable<Color>& c) {
perform(value_action(card(), valueP(), c));
addAction(value_action(valueP(), c));
}
void ColorValueEditor::changeCustom() {
Color c = wxGetColourFromUser(0, value().value());
+7 -1
View File
@@ -7,6 +7,12 @@
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
//#include <gui/value/editor.hpp>
#include <gui/value/editor.hpp>
#include <data/action/value.hpp>
// ----------------------------------------------------------------------------- : ValueEditor
void ValueEditor::addAction(ValueAction* a) {
a->isOnCard(editor().getCard().get());
editor().addAction(a);
}
+9 -10
View File
@@ -14,6 +14,8 @@
#include <render/value/viewer.hpp>
class IconMenu;
class ValueAction;
DECLARE_POINTER_TYPE(ValueActionPerformer);
// ----------------------------------------------------------------------------- : ValueEditor
@@ -122,8 +124,13 @@ class ValueEditor {
/// The editor is shown or hidden
virtual void onShow(bool) {}
/// Redraw this viewer
virtual void redraw() = 0;
// --------------------------------------------------- : Helpers
protected:
/// Retrieve the parent editor object
virtual DataEditor& editor() const = 0;
/// Perform an action
void addAction(ValueAction* a);
};
// ----------------------------------------------------------------------------- : Utility
@@ -131,22 +138,14 @@ class ValueEditor {
#define DECLARE_VALUE_EDITOR(Type) \
Type##ValueEditor(DataEditor& parent, const Type##StyleP& style); \
virtual ValueEditor* getEditor() { return this; } \
virtual void redraw(); \
private: \
/** Retrieve the parent editor object */ \
inline DataEditor& editor() const { \
return static_cast<DataEditor&>(viewer); \
} \
/** Card this editor is on, or nullptr */ \
inline const Card* card() const { return viewer.getCard().get(); } \
/** Perform an action */ \
void perform(Action* a) { getSet().actions.add(a); } \
public:
#define IMPLEMENT_VALUE_EDITOR(Type) \
void Type##ValueEditor::redraw() { \
editor().redraw(*this); \
} \
ValueViewerP Type##Style::makeEditor(DataEditor& parent, const StyleP& thisP) { \
assert(thisP.get() == this); \
return ValueViewerP(new Type##ValueEditor(parent, static_pointer_cast<Type##Style>(thisP))); \
+6 -7
View File
@@ -11,7 +11,6 @@
#include <gui/image_slice_window.hpp>
#include <data/format/clipboard.hpp>
#include <data/action/value.hpp>
#include <data/stylesheet.hpp>
#include <wx/clipbrd.h>
// ----------------------------------------------------------------------------- : ImageValueEditor
@@ -34,7 +33,7 @@ void ImageValueEditor::sliceImage(const Image& image) {
AlphaMaskP mask;
if (!style().mask_filename().empty()) {
Image mask_image;
InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename);
InputStreamP image_file = getStylePackage().openIn(style().mask_filename);
if (mask_image.LoadFile(*image_file)) {
Image resampled(style().width, style().height);
resample(mask_image, resampled);
@@ -46,10 +45,10 @@ void ImageValueEditor::sliceImage(const Image& image) {
// clicked ok?
if (s.ShowModal() == wxID_OK) {
// store the image into the set
FileName new_image_file = getSet().newFileName(field().name,_("")); // a new unique name in the package
FileName new_image_file = getLocalPackage().newFileName(field().name,_("")); // a new unique name in the package
Image img = s.getImage();
img.SaveFile(getSet().nameOut(new_image_file), img.HasAlpha() ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_JPEG);
perform(value_action(card(), valueP(), new_image_file));
img.SaveFile(getLocalPackage().nameOut(new_image_file), img.HasAlpha() ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_JPEG);
addAction(value_action(valueP(), new_image_file));
}
}
@@ -66,7 +65,7 @@ bool ImageValueEditor::canPaste() const {
bool ImageValueEditor::doCopy() {
// load image
InputStreamP image_file = getSet().openIn(value().filename);
InputStreamP image_file = getLocalPackage().openIn(value().filename);
Image image;
if (!image.LoadFile(*image_file)) return false;
// set data
@@ -89,7 +88,7 @@ bool ImageValueEditor::doPaste() {
}
bool ImageValueEditor::doDelete() {
perform(value_action(card(), valueP(), FileName()));
addAction(value_action(valueP(), FileName()));
return true;
}
+2 -2
View File
@@ -174,9 +174,9 @@ void MultipleChoiceValueEditor::toggle(int id) {
if (i == id) toggled_choice = choice;
}
// store value
perform(value_action(card(), valueP(), new_value, toggled_choice));
addAction(value_action(valueP(), new_value, toggled_choice));
}
void MultipleChoiceValueEditor::toggleDefault() {
perform(value_action(card(), valueP(), Defaultable<String>(value().value(), !value().value.isDefault()), _("")));
addAction(value_action(valueP(), Defaultable<String>(value().value(), !value().value.isDefault()), _("")));
}
+1 -1
View File
@@ -111,7 +111,7 @@ void PackageChoiceValueEditor::determineSize(bool) {
}
void PackageChoiceValueEditor::change(const String& c) {
perform(value_action(card(), valueP(), c));
addAction(value_action(valueP(), c));
}
void PackageChoiceValueEditor::initDropDown() {
+13 -4
View File
@@ -9,6 +9,7 @@
#include <util/prec.hpp>
#include <gui/value/symbol.hpp>
#include <gui/symbol/window.hpp>
#include <data/action/value.hpp>
#include <gui/util.hpp>
// ----------------------------------------------------------------------------- : SymbolValueEditor
@@ -93,8 +94,7 @@ bool SymbolValueEditor::onLeftUp(const RealPoint& pos, wxMouseEvent&) {
// edit
button_down = -2;
viewer.redraw(*this);
SymbolWindow* wnd = new SymbolWindow(nullptr, viewer.getSet(), card(), valueP());
wnd->Show();
editSymbol();
return true;
} else if (button_down == 1) {
// gallery
@@ -110,11 +110,20 @@ bool SymbolValueEditor::onLeftUp(const RealPoint& pos, wxMouseEvent&) {
bool SymbolValueEditor::onLeftDClick(const RealPoint& pos, wxMouseEvent&) {
// Use SetWindow as parent? Maybe not, the symbol editor will stay open when mainwindow closes
SymbolWindow* wnd = new SymbolWindow(nullptr, viewer.getSet(), card(), valueP());
wnd->Show();
editSymbol();
return true;
}
void SymbolValueEditor::determineSize(bool) {
style().height = 50;
}
void SymbolValueEditor::editSymbol() {
SymbolWindow* wnd = new SymbolWindow(nullptr, getActionPerformer());
wnd->Show();
}
ValueActionPerformer* SymbolValueEditor::getActionPerformer() {
return new ValueActionPerformer(valueP(), editor().getCard().get(), editor().getSetForActions());
}
+6
View File
@@ -13,6 +13,8 @@
#include <gui/value/editor.hpp>
#include <render/value/symbol.hpp>
class ValueActionPerformer;
// ----------------------------------------------------------------------------- : SymbolValueEditor
/// An editor 'control' for editing SymbolValues
@@ -31,6 +33,10 @@ class SymbolValueEditor : public SymbolValueViewer, public ValueEditor {
void drawButton(RotatedDC& dc, int button, const String& text);
/// Is there a button at the given position? returns the button index, or -1 if there is no button
int findButton(const RealPoint& pos);
/// Show the symbol editor
void editSymbol();
/// Get an object to perform actions for us
ValueActionPerformer* getActionPerformer();
// button, or -1 for mouse down, but not on button, or -2 for mouse not down
int button_down;
+8 -9
View File
@@ -69,7 +69,6 @@ BEGIN_EVENT_TABLE(TextValueEditorScrollBar, wxEvtHandler)
END_EVENT_TABLE ()
// ----------------------------------------------------------------------------- : WordListPos
class WordListPos : public IntrusivePtrBase<WordListPos> {
@@ -770,19 +769,19 @@ void TextValueEditor::doFormat(int type) {
size_t ss = selection_start, se = selection_end;
switch (type) {
case ID_FORMAT_BOLD: {
perform(toggle_format_action(card(), valueP(), _("b"), selection_start_i, selection_end_i, selection_start, selection_end, _("Bold")));
addAction(toggle_format_action(valueP(), _("b"), selection_start_i, selection_end_i, selection_start, selection_end, _("Bold")));
break;
}
case ID_FORMAT_ITALIC: {
perform(toggle_format_action(card(), valueP(), _("i"), selection_start_i, selection_end_i, selection_start, selection_end, _("Italic")));
addAction(toggle_format_action(valueP(), _("i"), selection_start_i, selection_end_i, selection_start, selection_end, _("Italic")));
break;
}
case ID_FORMAT_SYMBOL: {
perform(toggle_format_action(card(), valueP(), _("sym"), selection_start_i, selection_end_i, selection_start, selection_end, _("Symbols")));
addAction(toggle_format_action(valueP(), _("sym"), selection_start_i, selection_end_i, selection_start, selection_end, _("Symbols")));
break;
}
case ID_FORMAT_REMINDER: {
perform(new TextToggleReminderAction(card(), valueP(), selection_start_i));
addAction(new TextToggleReminderAction(valueP(), selection_start_i));
break;
}
}
@@ -907,7 +906,7 @@ void TextValueEditor::replaceSelection(const String& replacement, const String&
fixSelection();
// execute the action before adding it to the stack,
// because we want to run scripts before action listeners see the action
TextValueAction* action = typing_action(card(), valueP(), selection_start_i, selection_end_i, select_on_undo ? selection_start : selection_end, selection_end, replacement, name);
TextValueAction* action = typing_action(valueP(), selection_start_i, selection_end_i, select_on_undo ? selection_start : selection_end, selection_end, replacement, name);
if (!action) {
// nothing changes, but move the selection anyway
moveSelection(TYPE_CURSOR, selection_end);
@@ -918,7 +917,7 @@ void TextValueEditor::replaceSelection(const String& replacement, const String&
size_t expected_cursor = min(selection_start, selection_end) + untag(replacement).size();
// perform the action
// NOTE: this calls our onAction, invalidating the text viewer and moving the selection around the new text
perform(action);
addAction(action);
// move cursor
{
String real_value = untag_for_cursor(value().value());
@@ -964,7 +963,7 @@ void TextValueEditor::replaceSelection(const String& replacement, const String&
void TextValueEditor::tryAutoReplace() {
size_t end = selection_start_i;
GameSettings& gs = settings.gameSettingsFor(*getSet().game);
GameSettings& gs = settings.gameSettingsFor(viewer.getGame());
if (!gs.use_auto_replace) return;
FOR_EACH(ar, gs.auto_replaces) {
if (ar->enabled && ar->match.size() <= end) {
@@ -1279,7 +1278,7 @@ void TextValueEditor::findWordLists() {
String name = str.substr(pos + 11, type_end - pos - 11);
WordListP word_list;
// find word list type
FOR_EACH(wl, getSet().game->word_lists) {
FOR_EACH(wl, viewer.getGame().word_lists) {
if (wl->name == name) {
word_list = wl;
break;