Commit 3e0ee813 authored by twanvl's avatar twanvl

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)
parent 52281e68
......@@ -17,6 +17,7 @@
#include <data/field/symbol.hpp>
#include <data/field/package_choice.hpp>
#include <util/tagged_string.hpp>
#include <data/set.hpp> // for ValueActionPerformer
// ----------------------------------------------------------------------------- : ValueAction
......@@ -24,6 +25,10 @@ String ValueAction::getName(bool to_undo) const {
return _ACTION_1_("change", valueP->fieldP->name);
}
void ValueAction::isOnCard(Card* card) {
const_cast<ValueAction*>(this)->card = card;
}
// ----------------------------------------------------------------------------- : Simple
/// Swap the value in a Value object with a new one
......@@ -42,8 +47,8 @@ inline void swap_value(MultipleChoiceValue& a, MultipleChoiceValue::ValueType& b
template <typename T, bool ALLOW_MERGE>
class SimpleValueAction : public ValueAction {
public:
inline SimpleValueAction(const Card* card, const intrusive_ptr<T>& value, const typename T::ValueType& new_value)
: ValueAction(card, value), new_value(new_value)
inline SimpleValueAction(const intrusive_ptr<T>& value, const typename T::ValueType& new_value)
: ValueAction(value), new_value(new_value)
{}
virtual void perform(bool to_undo) {
......@@ -67,21 +72,21 @@ class SimpleValueAction : public ValueAction {
typename T::ValueType new_value;
};
ValueAction* value_action(const Card* card, const ChoiceValueP& value, const Defaultable<String>& new_value) { return new SimpleValueAction<ChoiceValue, true> (card, value, new_value); }
ValueAction* value_action(const Card* card, const ColorValueP& value, const Defaultable<Color>& new_value) { return new SimpleValueAction<ColorValue, true> (card, value, new_value); }
ValueAction* value_action(const Card* card, const ImageValueP& value, const FileName& new_value) { return new SimpleValueAction<ImageValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const SymbolValueP& value, const FileName& new_value) { return new SimpleValueAction<SymbolValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const PackageChoiceValueP& value, const String& new_value) { return new SimpleValueAction<PackageChoiceValue, false>(card, value, new_value); }
ValueAction* value_action(const Card* card, const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change) {
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value) { return new SimpleValueAction<ChoiceValue, true> (value, new_value); }
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value) { return new SimpleValueAction<ColorValue, true> (value, new_value); }
ValueAction* value_action(const ImageValueP& value, const FileName& new_value) { return new SimpleValueAction<ImageValue, false>(value, new_value); }
ValueAction* value_action(const SymbolValueP& value, const FileName& new_value) { return new SimpleValueAction<SymbolValue, false>(value, new_value); }
ValueAction* value_action(const PackageChoiceValueP& value, const String& new_value) { return new SimpleValueAction<PackageChoiceValue, false>(value, new_value); }
ValueAction* value_action(const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change) {
MultipleChoiceValue::ValueType v = { new_value, last_change };
return new SimpleValueAction<MultipleChoiceValue, false>(card, value, v);
return new SimpleValueAction<MultipleChoiceValue, false>(value, v);
}
// ----------------------------------------------------------------------------- : Text
TextValueAction::TextValueAction(const Card* card, const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name)
: ValueAction(card, value)
TextValueAction::TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name)
: ValueAction(value)
, selection_start(start), selection_end(end), new_selection_end(new_end)
, new_value(new_value)
, name(name)
......@@ -111,7 +116,7 @@ TextValue& TextValueAction::value() const {
}
TextValueAction* toggle_format_action(const Card* card, const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name) {
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name) {
if (start > end) {
swap(start, end);
swap(start_i, end_i);
......@@ -144,11 +149,11 @@ TextValueAction* toggle_format_action(const Card* card, const TextValueP& value,
if (value->value() == new_value) {
return nullptr; // no changes
} else {
return new TextValueAction(card, value, start, end, end, new_value, action_name);
return new TextValueAction(value, start, end, end, new_value, action_name);
}
}
TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) {
TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) {
bool reverse = start > end;
if (reverse) {
swap(start, end);
......@@ -160,17 +165,17 @@ TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t
return nullptr;
} else {
if (reverse) {
return new TextValueAction(card, value, end, start, start+untag(replacement).size(), new_value, action_name);
return new TextValueAction(value, end, start, start+untag(replacement).size(), new_value, action_name);
} else {
return new TextValueAction(card, value, start, end, start+untag(replacement).size(), new_value, action_name);
return new TextValueAction(value, start, end, start+untag(replacement).size(), new_value, action_name);
}
}
}
// ----------------------------------------------------------------------------- : Reminder text
TextToggleReminderAction::TextToggleReminderAction(const Card* card, const TextValueP& value, size_t pos_in)
: ValueAction(card, value)
TextToggleReminderAction::TextToggleReminderAction(const TextValueP& value, size_t pos_in)
: ValueAction(value)
{
pos = in_tag(value->value(), _("<kw-"), pos_in, pos_in);
if (pos == String::npos) {
......@@ -217,3 +222,19 @@ String ScriptStyleEvent::getName(bool) const {
void ScriptStyleEvent::perform(bool) {
assert(false); // this action is just an event, it should not be performed
}
// ----------------------------------------------------------------------------- : Action performer
ValueActionPerformer::ValueActionPerformer(const ValueP& value, Card* card, const SetP& set)
: value(value), card(card), set(set)
{}
ValueActionPerformer::~ValueActionPerformer() {}
void ValueActionPerformer::addAction(ValueAction* action) {
action->isOnCard(card);
set->actions.addAction(action);
}
Package& ValueActionPerformer::getLocalPackage() {
return *set;
}
......@@ -20,6 +20,7 @@
class Card;
class StyleSheet;
DECLARE_POINTER_TYPE(Set);
DECLARE_POINTER_TYPE(Value);
DECLARE_POINTER_TYPE(Style);
DECLARE_POINTER_TYPE(TextValue);
......@@ -35,10 +36,13 @@ DECLARE_POINTER_TYPE(PackageChoiceValue);
/// An Action the changes a Value
class ValueAction : public Action {
public:
inline ValueAction(const Card* card, const ValueP& value) : valueP(value), card(card) {}
inline ValueAction(const ValueP& value) : valueP(value), card(nullptr) {}
virtual String getName(bool to_undo) const;
/// We know that the value is on the given card, add that information
void isOnCard(Card* card);
const ValueP valueP; ///< The modified value
const Card* card; ///< The card the value is on, or null if it is not a card value
};
......@@ -46,19 +50,19 @@ class ValueAction : public Action {
// ----------------------------------------------------------------------------- : Simple
/// Action that updates a Value to a new value
ValueAction* value_action(const Card* card, const ChoiceValueP& value, const Defaultable<String>& new_value);
ValueAction* value_action(const Card* card, const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change);
ValueAction* value_action(const Card* card, const ColorValueP& value, const Defaultable<Color>& new_value);
ValueAction* value_action(const Card* card, const ImageValueP& value, const FileName& new_value);
ValueAction* value_action(const Card* card, const SymbolValueP& value, const FileName& new_value);
ValueAction* value_action(const Card* card, const PackageChoiceValueP& value, const String& new_value);
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value);
ValueAction* value_action(const MultipleChoiceValueP& value, const Defaultable<String>& new_value, const String& last_change);
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value);
ValueAction* value_action(const ImageValueP& value, const FileName& new_value);
ValueAction* value_action(const SymbolValueP& value, const FileName& new_value);
ValueAction* value_action(const PackageChoiceValueP& value, const String& new_value);
// ----------------------------------------------------------------------------- : Text
/// An action that changes a TextValue
class TextValueAction : public ValueAction {
public:
TextValueAction(const Card* card, const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name);
TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
......@@ -77,18 +81,18 @@ class TextValueAction : public ValueAction {
};
/// Action for toggling some formating tag on or off in some range
TextValueAction* toggle_format_action(const Card* card, const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name);
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name);
/// Typing in a TextValue, replace the selection [start...end) with replacement
/** start and end are cursor positions, start_i and end_i are indices*/
TextValueAction* typing_action(const Card* card, const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name);
TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name);
// ----------------------------------------------------------------------------- : Reminder text
/// Toggle reminder text for a keyword on or off
class TextToggleReminderAction : public ValueAction {
public:
TextToggleReminderAction(const Card* card, const TextValueP& value, size_t pos);
TextToggleReminderAction(const TextValueP& value, size_t pos);
virtual String getName(bool to_undo) const;
virtual void perform(bool to_undo);
......@@ -150,5 +154,24 @@ class ScriptStyleEvent : public Action {
const Style* style; ///< The modified style
};
// ----------------------------------------------------------------------------- : Action performer
/// A loose object for performing ValueActions on a certain value.
/** Used to reduce coupling */
class ValueActionPerformer {
public:
ValueActionPerformer(const ValueP& value, Card* card, const SetP& set);
~ValueActionPerformer();
/// Perform an action. The performer takes ownerwhip of the action.
void addAction(ValueAction* action);
const ValueP value; ///< The value
Package& getLocalPackage();
private:
Card* card; ///< Card the value is on (if any)
SetP set; ///< Set for the actions
};
// ----------------------------------------------------------------------------- : EOF
#endif
......@@ -96,6 +96,7 @@ String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def) {
return loc->tr(subcat, key, def); \
}
IMPLEMENT_TR_TYPE(Package, game_translations) //%% TODO!
IMPLEMENT_TR_TYPE(Game, game_translations)
IMPLEMENT_TR_TYPE(StyleSheet, stylesheet_translations)
IMPLEMENT_TR_TYPE(SymbolFont, symbol_font_translations)
......
......@@ -149,7 +149,7 @@ class SetView : public ActionListener {
~SetView();
/// Get the set that is currently being viewed
inline SetP getSet() { return set; }
//inline SetP getSet() const { return set; }
/// Change the set that is being viewed
void setSet(const SetP& set);
......
......@@ -60,6 +60,10 @@ ValueViewer* DataEditor::focusedViewer() const {
return FindFocus() == this ? current_viewer : nullptr;
}
void DataEditor::addAction(Action* action) {
set->actions.addAction(action);
}
// ----------------------------------------------------------------------------- : Selection
bool DataEditor::AcceptsFocus() const {
......
......@@ -22,7 +22,7 @@ class DataEditor : public CardViewer {
public:
DataEditor(Window* parent, int id, long style = 0);
// --------------------------------------------------- : Utility for ValueViewers
// --------------------------------------------------- : Utility for ValueViewers/Editors
virtual bool drawBorders() const;
virtual bool drawEditing() const;
......@@ -30,6 +30,9 @@ class DataEditor : public CardViewer {
virtual wxPen borderPen(bool active) const;
virtual ValueViewer* focusedViewer() const;
virtual void addAction(Action* action);
inline SetP getSetForActions() { return set; }
// --------------------------------------------------- : Selection
/// Select the given viewer, sends focus events
......
......@@ -162,7 +162,7 @@ bool CardListBase::doPaste() {
ok = data.getCards(set, new_cards);
if (!ok) return false;
// add card to set
set->actions.add(new AddCardAction(ADD, *set, new_cards));
set->actions.addAction(new AddCardAction(ADD, *set, new_cards));
return true;
}
bool CardListBase::doDelete() {
......@@ -176,7 +176,7 @@ bool CardListBase::doDelete() {
}
if (cards_to_delete.empty()) return false;
// delete cards
set->actions.add(new AddCardAction(REMOVE, *set, cards_to_delete));
set->actions.addAction(new AddCardAction(REMOVE, *set, cards_to_delete));
return true;
}
......@@ -345,7 +345,7 @@ void CardListBase::onDrag(wxMouseEvent& ev) {
findSelectedItemPos();
if (item != selected_item_pos) {
// move card in the set
set->actions.add(new ReorderCardsAction(*set, item, selected_item_pos));
set->actions.addAction(new ReorderCardsAction(*set, item, selected_item_pos));
}
}
}
......
......@@ -96,9 +96,9 @@ void KeywordList::updateUsageStatistics() {
// ----------------------------------------------------------------------------- : Clipboard
bool KeywordList::canCopy() const { return !!selected_item; }
bool KeywordList::canCut() const { return canCopy() && !getKeyword()->fixed; }
bool KeywordList::canPaste() const {
bool KeywordList::canDelete() const { return !getKeyword()->fixed; }
bool KeywordList::canCopy() const { return !!selected_item; }
bool KeywordList::canPaste() const {
return wxTheClipboard->IsSupported(KeywordDataObject::format);
}
......@@ -127,14 +127,14 @@ bool KeywordList::doPaste() {
// add keyword to set
KeywordP keyword = data.getKeyword(set);
if (keyword) {
set->actions.add(new AddKeywordAction(ADD, *set, keyword));
set->actions.addAction(new AddKeywordAction(ADD, *set, keyword));
return true;
} else {
return false;
}
}
bool KeywordList::doDelete() {
set->actions.add(new AddKeywordAction(REMOVE, *set, getKeyword()));
set->actions.addAction(new AddKeywordAction(REMOVE, *set, getKeyword()));
return true;
}
......
......@@ -53,9 +53,9 @@ class KeywordList : public ItemList, public SetView {
// --------------------------------------------------- : Clipboard
bool canCut() const;
bool canCopy() const;
bool canPaste() const;
bool canDelete() const;
bool canCopy() const;
bool canPaste() const;
// Try to perform a clipboard operation, return success
bool doCut();
bool doCopy();
......
......@@ -205,6 +205,12 @@ void SetInfoEditor::onChangeSet() {
setData(set->data);
}
Package& SetInfoEditor::getStylePackage() const {
return DataEditor::getStylePackage();
// TODO: Use the game
//return getGame();
}
// ----------------------------------------------------------------------------- : StylingEditor
StylingEditor::StylingEditor(Window* parent, int id, long style)
......@@ -232,7 +238,11 @@ ExportOptionsEditor::ExportOptionsEditor(Window* parent, int id, long style)
{}
void ExportOptionsEditor::showExport(const ExportTemplateP& export_template) {
this->export_template = export_template;
setStyles(set->stylesheet, export_template->option_style);
setData(settings.exportOptionsFor(*export_template));
}
Package& ExportOptionsEditor::getStylePackage() const {
return *export_template;
}
......@@ -58,6 +58,8 @@ class NativeLookEditor : public DataEditor {
class SetInfoEditor : public NativeLookEditor {
public:
SetInfoEditor(Window* parent, int id, long style = 0);
virtual Package& getStylePackage() const;
protected:
virtual void onChangeSet();
};
......@@ -73,7 +75,6 @@ class StylingEditor : public NativeLookEditor {
void showStylesheet(const StyleSheetP& stylesheet);
/// Show the styling for given card
void showCard(const CardP& card);
protected:
virtual void onChangeSet();
};
......@@ -87,6 +88,10 @@ class ExportOptionsEditor : public NativeLookEditor {
/// Show the options for given export template
void showExport(const ExportTemplateP& export_template);
virtual Package& getStylePackage() const;
private:
ExportTemplateP export_template;
};
// ----------------------------------------------------------------------------- : EOF
......
......@@ -207,7 +207,7 @@ void CardsPanel::onCommand(int id) {
card_list->selectNext();
break;
case ID_CARD_ADD:
set->actions.add(new AddCardAction(*set));
set->actions.addAction(new AddCardAction(*set));
break;
case ID_CARD_REMOVE:
card_list->doDelete();
......
......@@ -154,12 +154,12 @@ void KeywordsPanel::onCommand(int id) {
list->selectNext();
break;
case ID_KEYWORD_ADD:
set->actions.add(new AddKeywordAction(*set));
set->actions.addAction(new AddKeywordAction(*set));
break;
case ID_KEYWORD_REMOVE:
if (!list->getKeyword()->fixed) {
if (list->canDelete()) {
// only remove set keywords
set->actions.add(new AddKeywordAction(REMOVE, *set, list->getKeyword()));
list->doDelete();
}
break;
case ID_KEYWORD_ADD_PARAM: {
......@@ -336,7 +336,7 @@ void KeywordsPanel::onModeChange(wxCommandEvent& ev) {
if (!list->getKeyword()) return;
int sel = mode->GetSelection();
if (sel >= 0 && (size_t)sel < set->game->keyword_modes.size()) {
set->actions.add(new ChangeKeywordModeAction(*list->getKeyword(), set->game->keyword_modes[sel]->name));
set->actions.addAction(new ChangeKeywordModeAction(*list->getKeyword(), set->game->keyword_modes[sel]->name));
}
}
......
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <gui/set/random_pack_panel.hpp>
#include <gui/control/card_viewer.hpp>
#include <gui/control/filtered_card_list.hpp>
// ----------------------------------------------------------------------------- : RandomPackPanel
RandomPackPanel::RandomPackPanel(Window* parent, int id)
: SetWindowPanel(parent, id)
{
// init controls
preview = new CardViewer(this, wxID_ANY);
card_list = new FilteredCardList(this, wxID_ANY);
// init sizer
wxSizer* s = new wxBoxSizer(wxHORIZONTAL);
s->Add(preview, 0, wxRIGHT, 2);
wxSizer* s2 = new wxBoxSizer(wxVERTICAL);
s2->Add(card_list, 1, wxEXPAND | wxTOP, 4);
s->Add(s2, 1, wxEXPAND, 8);
s->SetSizeHints(this);
SetSizer(s);
}
void RandomPackPanel::onChangeSet() {
preview ->setSet(set);
card_list->setSet(set);
}
// ----------------------------------------------------------------------------- : UI
void RandomPackPanel::initUI(wxToolBar* tb, wxMenuBar* mb) {
// ?
}
void RandomPackPanel::destroyUI(wxToolBar* tb, wxMenuBar* mb) {
// ?
}
void RandomPackPanel::onUpdateUI(wxUpdateUIEvent& ev) {
// ?
}
void RandomPackPanel::onCommand(int id) {
// ?
}
// ----------------------------------------------------------------------------- : Clipboard
bool RandomPackPanel::canCopy() const { return card_list->canCopy(); }
void RandomPackPanel::doCopy() { card_list->doCopy(); }
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2008 Twan van Laarhoven and "coppro" |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_GUI_SET_RANDOM_PACK_PANEL
#define HEADER_GUI_SET_RANDOM_PACK_PANEL
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <gui/set/panel.hpp>
class CardViewer;
class FilteredCardList;
// ----------------------------------------------------------------------------- : RandomPackPanel
/// A SetWindowPanel for creating random booster packs
class RandomPackPanel : public SetWindowPanel {
public:
RandomPackPanel(Window* parent, int id);
// --------------------------------------------------- : UI
virtual void onChangeSet();
virtual void initUI (wxToolBar* tb, wxMenuBar* mb);
virtual void destroyUI(wxToolBar* tb, wxMenuBar* mb);
virtual void onUpdateUI(wxUpdateUIEvent&);
virtual void onCommand(int id);
// --------------------------------------------------- : Clipboard
virtual bool canCopy() const;
virtual void doCopy();
private:
CardViewer* preview; ///< Card preview
FilteredCardList* card_list; ///< The list of cards
};
// ----------------------------------------------------------------------------- : EOF
#endif
......@@ -131,18 +131,18 @@ void StylePanel::onStyleSelect(wxCommandEvent&) {
// select no special style when selecting the same style as the set default
stylesheet = StyleSheetP();
}
set->actions.add(new ChangeCardStyleAction(card, stylesheet));
set->actions.addAction(new ChangeCardStyleAction(card, stylesheet));
Layout();
}
}
void StylePanel::onUseForAll(wxCommandEvent&) {
set->actions.add(new ChangeSetStyleAction(*set, card));
set->actions.addAction(new ChangeSetStyleAction(*set, card));
Layout();
}
void StylePanel::onUseCustom(wxCommandEvent&) {
set->actions.add(new ChangeCardHasStylingAction(*set, card));
set->actions.addAction(new ChangeCardHasStylingAction(*set, card));
}
BEGIN_EVENT_TABLE(StylePanel, wxPanel)
......
......@@ -100,7 +100,7 @@ void SymbolBasicShapeEditor::onLeftDown (const Vector2D& pos, wxMouseEvent& ev
void SymbolBasicShapeEditor::onLeftUp (const Vector2D& pos, wxMouseEvent& ev) {
if (drawing && shape) {
// Finalize the shape
getSymbol()->actions.add(new AddSymbolPartAction(*getSymbol(), shape));
addAction(new AddSymbolPartAction(*getSymbol(), shape));
// Select the part
control.selectPart(shape);
// no need to clean up, this editor is replaced
......
......@@ -15,3 +15,7 @@
void SymbolEditorBase::SetStatusText(const String& text) {
control.parent->SetStatusText(text);
}
void SymbolEditorBase::addAction(Action* action, bool allow_merge) {
getSymbol()->actions.addAction(action, allow_merge);
}
......@@ -28,6 +28,10 @@ class SymbolEditorBase : public IntrusivePtrVirtualBase {
SymbolControl& control;
inline SymbolP getSymbol() { return control.getSymbol(); }
/// Perform an action
void addAction(Action* action, bool allow_merge = true);
void SetStatusText(const String& text);
public:
......
......@@ -166,9 +166,9 @@ void SymbolPartList::onLeftUp(wxMouseEvent& ev) {
if (par != drop_parent && par->parts.size() == 1 && !par->isSymbolSymmetry()) {
// this leaves a group without elements, remove it
findParent(*par, par, drag_position); // parent of the group
symbol->actions.add(new UngroupReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
symbol->actions.addAction(new UngroupReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
} else {
symbol->actions.add(new ReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
symbol->actions.addAction(new ReorderSymbolPartsAction(*par, drag_position, *drop_parent, drop_position));
}
} else {
Refresh(false);
......@@ -253,14 +253,14 @@ void SymbolPartList::onChar(wxKeyEvent& ev) {
if (cursor > 0 && cursor <= typing_in->name.size()) {
String new_name = typing_in->name;
new_name.erase(cursor - 1, 1);
symbol->actions.add(new SymbolPartNameAction(typing_in, new_name, cursor, cursor - 1));
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor - 1));
}
break;
case WXK_DELETE:
if (cursor < typing_in->name.size()) {
String new_name = typing_in->name;
new_name.erase(cursor, 1);
symbol->actions.add(new SymbolPartNameAction(typing_in, new_name, cursor, cursor));
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor));
}
break;
default:
......@@ -277,7 +277,7 @@ void SymbolPartList::onChar(wxKeyEvent& ev) {
#endif
String new_name = typing_in->name;
new_name.insert(cursor, 1, key);
symbol->actions.add(new SymbolPartNameAction(typing_in, new_name, cursor, cursor + 1));
symbol->actions.addAction(new SymbolPartNameAction(typing_in, new_name, cursor, cursor + 1));
}
}
}
......
......@@ -254,7 +254,7 @@ void SymbolPointEditor::onLeftDClick(const Vector2D& pos, wxMouseEvent& ev) {
if (hovering == SELECTED_NEW_POINT) {
// Add point
ControlPointAddAction* act = new ControlPointAddAction(part, hover_line_1_idx, hover_line_t);
getSymbol()->actions.add(act);
addAction(act);
// select the new point
selectPoint(act->getNewPoint(), false);
selection = SELECTED_POINTS;
......@@ -262,7 +262,7 @@ void SymbolPointEditor::onLeftDClick(const Vector2D& pos, wxMouseEvent& ev) {
// Delete point
selected_points.clear();
selectPoint(hover_handle.point, false);
getSymbol()->actions.add(control_point_remove_action(part, selected_points));
addAction(control_point_remove_action(part, selected_points));
selected_points.clear();
selection = SELECTED_NONE;
}
......@@ -288,7 +288,7 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
if (controlPointMoveAction) controlPointMoveAction = 0;
if (!curveDragAction) {
curveDragAction = new CurveDragAction(selected_line1, selected_line2);
getSymbol()->actions.add(curveDragAction);
addAction(curveDragAction);
}
curveDragAction->move(delta, selected_line_t);
control.Refresh(false);
......@@ -298,7 +298,7 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
if (!controlPointMoveAction) {
// create action we can add this movement to
controlPointMoveAction = new ControlPointMoveAction(selected_points);
getSymbol()->actions.add(controlPointMoveAction);
addAction(controlPointMoveAction);
}
controlPointMoveAction->constrain = ev.ControlDown(); // ctrl constrains
controlPointMoveAction->snap = snap(ev);
......@@ -309,7 +309,7 @@ void SymbolPointEditor::onMouseDrag(const Vector2D& from, const Vector2D& to, wx
// Move the selected handle
if (!handleMoveAction) {
handleMoveAction = new HandleMoveAction(selected_handle);
getSymbol()->actions.add(handleMoveAction);
addAction(handleMoveAction);
}
handleMoveAction->constrain = ev.ControlDown(); // ctrl constrains
handleMoveAction->snap = snap(ev);
......@@ -368,14 +368,14 @@ void SymbolPointEditor::onChar(wxKeyEvent& ev) {
if (selection == SELECTED_POINTS || selection == SELECTED_LINE) {
// Move all selected points
controlPointMoveAction = new ControlPointMoveAction(selected_points);
getSymbol()->actions.add(controlPointMoveAction);
addAction(controlPointMoveAction);
controlPointMoveAction->move(delta);
new_point += delta;
control.Refresh(false);
} else if (selection == SELECTED_HANDLE) {
// Move the selected handle
handleMoveAction = new HandleMoveAction(selected_handle);
getSymbol()->actions.add(handleMoveAction);
addAction(handleMoveAction);
handleMoveAction->move(delta);
control.Refresh(false);
}
......@@ -466,7 +466,7 @@ void SymbolPointEditor::resetActions() {
void SymbolPointEditor::deleteSelection() {
if (!selected_points.empty()) {
getSymbol()->actions.add(control_point_remove_action(part, selected_points));
addAction(control_point_remove_action(part, selected_points));
selected_points.clear();
resetActions();
control.Refresh(false);
......@@ -477,12 +477,12 @@ void SymbolPointEditor::onChangeSegment(SegmentMode mode) {
assert(selected_line1);
assert(selected_line2);
if (selected_line1->segment_after == mode) return;
getSymbol()->actions.add(new SegmentModeAction(selected_line1, selected_line2, mode));
addAction(new SegmentModeAction(selected_line1, selected_line2, mode));
control.Refresh(false);
}
void SymbolPointEditor::onChangeLock(LockMode mode) {
getSymbol()->actions.add(new LockModeAction(*selected_points.begin(), mode));
addAction(new LockModeAction(*selected_points.begin(), mode));
control.Refresh(false);
}
......
......@@ -173,22 +173,22 @@ void SymbolSelectEditor::onUpdateUI(wxUpdateUIEvent& ev) {
void SymbolSelectEditor::onCommand(int id) {
if (id >= ID_SYMBOL_COMBINE && id < ID_SYMBOL_COMBINE_MAX) {
// change combine mode
getSymbol()->actions.add(new CombiningModeAction(
addAction(new CombiningModeAction(
control.selected_parts.get(),
static_cast<SymbolShapeCombine>(id - ID_SYMBOL_COMBINE)
));
control.Refresh(false);
} else if (id == ID_EDIT_DUPLICATE && !isEditing()) {
// duplicate selection, not when dragging
getSymbol()->actions.add(new DuplicateSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(new DuplicateSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
control.Refresh(false);
} else if (id == ID_EDIT_GROUP && !isEditing()) {
// group selection, not when dragging
getSymbol()->actions.add(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), new_intrusive<SymbolGroup>()));
addAction(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), new_intrusive<SymbolGroup>()));
control.Refresh(false);
} else if (id == ID_EDIT_UNGROUP && !isEditing()) {
// ungroup selection, not when dragging
getSymbol()->actions.add(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
control.Refresh(false);
}
}
......@@ -313,23 +313,23 @@ void SymbolSelectEditor::onMouseDrag (const Vector2D& from, const Vector2D& to,
if (scaleX == 0 || scaleY == 0) {
// shear, center/fixed point on the opposite side
shearAction = new SymbolPartShearAction(control.selected_parts.get(), handlePos(-scaleX, -scaleY));
getSymbol()->actions.add(shearAction);
addAction(shearAction);
} else {
// rotate
rotateAction = new SymbolPartRotateAction(control.selected_parts.get(), center);
getSymbol()->actions.add(rotateAction);
addAction(rotateAction);
startAngle = angleTo(to);
}
} else {
// we are on a handle; start scaling
scaleAction = new SymbolPartScaleAction(control.selected_parts.get(), scaleX, scaleY);
getSymbol()->actions.add(scaleAction);
addAction(scaleAction);
}
} else {
// move
click_mode = CLICK_MOVE;
moveAction = new SymbolPartMoveAction(control.selected_parts.get());
getSymbol()->actions.add(moveAction);
addAction(moveAction);
}
}
......@@ -399,7 +399,7 @@ void SymbolSelectEditor::onKeyChange (wxKeyEvent& ev) {
void SymbolSelectEditor::onChar(wxKeyEvent& ev) {
if (ev.GetKeyCode() == WXK_DELETE) {
// delete selected parts
getSymbol()->actions.add(new RemoveSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(new RemoveSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
if (control.selected_parts.selected(highlightPart)) highlightPart = SymbolPartP(); // deleted it
control.selected_parts.clear();
resetActions();
......@@ -416,7 +416,7 @@ void SymbolSelectEditor::onChar(wxKeyEvent& ev) {
ev.Skip();
return;
}
getSymbol()->actions.add(new SymbolPartMoveAction(control.selected_parts.get(), delta));
addAction(new SymbolPartMoveAction(control.selected_parts.get(), delta));
}
}
......
......@@ -107,13 +107,13 @@ void SymbolSymmetryEditor::onCommand(int id) {
if (id >= ID_SYMMETRY && id < ID_SYMMETRY_MAX) {
SymbolSymmetryType kind = id == ID_SYMMETRY_ROTATION ? SYMMETRY_ROTATION : SYMMETRY_REFLECTION;
if (symmetry && symmetry->kind != kind) {
getSymbol()->actions.add(new SymmetryTypeAction(*symmetry, kind));
addAction(new SymmetryTypeAction(*symmetry, kind));
control.Refresh(false);
}
resetActions();
} else if (id == ID_COPIES) {
if (symmetry && symmetry->copies != copies->GetValue()) {
getSymbol()->actions.add(new SymmetryCopiesAction(*symmetry, copies->GetValue()));
addAction(new SymmetryCopiesAction(*symmetry, copies->GetValue()));
control.Refresh(false);
}
resetActions();
......@@ -124,11 +124,11 @@ void SymbolSymmetryEditor::onCommand(int id) {
symmetry->center = Vector2D(0.5,0.5);
symmetry->handle = Vector2D(0.2,0);
symmetry->name = symmetry->expectedName();
getSymbol()->actions.add(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), symmetry));
addAction(new GroupSymbolPartsAction(*getSymbol(), control.selected_parts.get(), symmetry));
control.selected_parts.select(symmetry);
control.Refresh(false);
} else if (id == ID_REMOVE_SYMMETRY) {
getSymbol()->actions.add(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
addAction(new UngroupSymbolPartsAction(*getSymbol(), control.selected_parts.get()));
symmetry = SymbolSymmetryP();
control.Refresh(false);
}
......@@ -158,7 +158,7 @@ void SymbolSymmetryEditor::onMouseDrag (const Vector2D& from, const Vector2D& t
symmetryMoveAction = new SymmetryMoveAction(*symmetry, selection == SELECTION_HANDLE);
symmetryMoveAction->constrain = ev.ControlDown();
symmetryMoveAction->snap = ev.ShiftDown() != settings.symbol_grid_snap ? settings.symbol_grid_size : 0;
getSymbol()->actions.add(symmetryMoveAction);
addAction(symmetryMoveAction);
}
symmetryMoveAction->move(to - from);
control.Refresh(false);
......
......@@ -12,23 +12,28 @@
#include <gui/symbol/part_list.hpp>
#include <gui/icon_menu.hpp>
#include <gui/util.hpp>
#include <data/set.hpp>
#include <data/field/symbol.hpp>
#include <data/format/image_to_symbol.hpp>
#include <data/action/value.hpp>
#include <data/set.hpp> // :(
#include <util/window_id.hpp>
#include <util/io/reader.hpp>
#include <util/io/package.hpp>
#include <util/error.hpp>
#include <wx/filename.h>
#include <wx/wfstream.h>
// ----------------------------------------------------------------------------- : Constructor
SymbolWindow::SymbolWindow(Window* parent) {
SymbolWindow::SymbolWindow(Window* parent)
: performer(nullptr)
{
init(parent, default_symbol());
}
SymbolWindow::SymbolWindow(Window* parent, const String& filename) {
SymbolWindow::SymbolWindow(Window* parent, const String& filename)
: performer(nullptr)
{
// open file
Reader reader(new_shared1<wxFileInputStream>(filename), nullptr, filename);
SymbolP symbol;
......@@ -36,15 +41,17 @@ SymbolWindow::SymbolWindow(Window* parent, const String& filename) {
init(parent, symbol);
}
SymbolWindow::SymbolWindow(Window* parent, const SetP& set, const Card* card, const SymbolValueP& value)
: value(value), card(card), set(set)
SymbolWindow::SymbolWindow(Window* parent, ValueActionPerformer* performer)
: performer(performer)
{
// attempt to load symbol
SymbolP symbol;
SymbolValueP value = static_pointer_cast<SymbolValue>(performer->value);
if (!value->filename.empty()) {
try {
// load symbol
symbol = set->readFile<SymbolP>(value->filename);
Package& package = performer->getLocalPackage();
symbol = package.readFile<SymbolP>(value->filename);
} catch (const Error& e) {
handle_error(e);
}
......@@ -52,6 +59,9 @@ SymbolWindow::SymbolWindow(Window* parent, const SetP& set, const Card* card, co
if (!symbol) symbol = default_symbol();
init(parent, symbol);
}
SymbolWindow::~SymbolWindow() {
delete performer;
}
void SymbolWindow::init(Window* parent, SymbolP symbol) {
Create(parent, wxID_ANY, _TITLE_("symbol editor"), wxDefaultPosition, wxSize(650,600), wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
......@@ -233,11 +243,13 @@ void SymbolWindow::onFileSaveAs(wxCommandEvent& ev) {
}
void SymbolWindow::onFileStore(wxCommandEvent& ev) {
if (value) {
FileName new_filename = set->newFileName(value->field().name,_(".mse-symbol")); // a new unique name in the package
Writer writer(set->openOut(new_filename));
if (performer) {
SymbolValueP value = static_pointer_cast<SymbolValue>(performer->value);
Package& package = performer->getLocalPackage();
FileName new_filename = package.newFileName(value->field().name,_(".mse-symbol")); // a new unique name in the package
Writer writer(package.openOut(new_filename));
writer.handle(control->getSymbol());
set->actions.add(value_action(card, value, new_filename));
performer->addAction(value_action(value, new_filename));
}
}
......@@ -273,7 +285,7 @@ void SymbolWindow::onUpdateUI(wxUpdateUIEvent& ev) {
switch(ev.GetId()) {
// file menu
case ID_FILE_STORE: {
ev.Enable(value);
ev.Enable(performer);
break;
// undo/redo
} case ID_EDIT_UNDO: {
......
......@@ -15,9 +15,7 @@
class SymbolControl;
class SymbolPartList;
class Card;
DECLARE_POINTER_TYPE(SymbolValue);
DECLARE_POINTER_TYPE(Set);
DECLARE_POINTER_TYPE(ValueActionPerformer);
// ----------------------------------------------------------------------------- : SymbolWindow
......@@ -29,7 +27,8 @@ class SymbolWindow : public Frame {
/// Construct a SymbolWindow showing a symbol from a file
SymbolWindow(Window* parent, const String& filename);
/// Construct a SymbolWindow showing a symbol value in a set
SymbolWindow(Window* parent, const SetP& set, const Card* card, const SymbolValueP& value);
SymbolWindow(Window* parent, ValueActionPerformer* performer);
~SymbolWindow();
private:
// --------------------------------------------------- : Children
......@@ -41,9 +40,7 @@ class SymbolWindow : public Frame {
SymbolPartList* parts; ///< A list of parts in the symbol
// when editing a symbol field
SymbolValueP value;
const Card* card;
SetP set;
ValueActionPerformer* performer;
// --------------------------------------------------- : Event handling
DECLARE_EVENT_TABLE();
......
......@@ -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));
}
......@@ -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);
};
......
......@@ -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,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);
}
......@@ -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))); \
......
......@@ -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;
}
......
......@@ -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()), _("")));
}
......@@ -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() {
......
......@@ -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());
}
......@@ -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;
......
......@@ -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;
......
......@@ -110,6 +110,16 @@ Rotation DataViewer::getRotation() const {
return Rotation(ss.card_angle(), stylesheet->getCardRect(), ss.card_zoom(), 1.0, ROTATION_ATTACH_TOP_LEFT);
}
Package& DataViewer::getStylePackage() const {
return *stylesheet;
}
Package& DataViewer::getLocalPackage() const {
return *set;
}
Game& DataViewer::getGame() const {
return *set->game;
}
// ----------------------------------------------------------------------------- : Setting data
void DataViewer::setCard(const CardP& card, bool refresh) {
......
......@@ -62,6 +62,13 @@ class DataViewer : public SetView {
/// Invalidate and redraw (the area of) a single value viewer
virtual void redraw(const ValueViewer&) {}
/// The package containing style stuff like images
virtual Package& getStylePackage() const;
/// The local package for loading/saving files
Package& getLocalPackage() const;
/// Return the game to use for information
Game& getGame() const;
// --------------------------------------------------- : Setting data
/// Display a card in this viewer
......@@ -100,7 +107,6 @@ class DataViewer : public SetView {
vector<ValueViewerP> viewers; ///< The viewers for the different values in the data
CardP card; ///< The card that is currently displayed, if any
bool drawing; ///< Are we currently drawing?
public:
mutable StyleSheetP stylesheet; ///< Stylesheet being used
};
......
......@@ -9,28 +9,27 @@
#include <util/prec.hpp>
#include <render/value/choice.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
// ----------------------------------------------------------------------------- : ChoiceValueViewer
IMPLEMENT_VALUE_VIEWER(Choice);
void get_options(Rotation& rot, DataViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts);
void get_options(Rotation& rot, ValueViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts);
bool ChoiceValueViewer::prepare(RotatedDC& dc) {
return prepare_choice_viewer(dc, viewer, style(), value().value());
return prepare_choice_viewer(dc, *this, style(), value().value());
}
void ChoiceValueViewer::draw(RotatedDC& dc) {
drawFieldBorder(dc);
if (style().render_style & RENDER_HIDDEN) return;
draw_choice_viewer(dc, viewer, style(), value().value());
draw_choice_viewer(dc, *this, style(), value().value());
}
bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value) {
bool prepare_choice_viewer(RotatedDC& dc, ValueViewer& viewer, ChoiceStyle& style, const String& value) {
if (style.render_style & RENDER_IMAGE) {
style.initImage();
CachedScriptableImage& img = style.image;
Context& ctx = viewer.getContext();
Context& ctx = viewer.viewer.getContext();
ctx.setVariable(SCRIPT_VAR_input, to_script(value));
// generate to determine the size
if (img.update(ctx) && img.isReady()) {
......@@ -39,7 +38,7 @@ bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style
// Generate image/bitmap (whichever is available)
// don't worry, we cache the image
ImageCombine combine = style.combine;
style.loadMask(*viewer.stylesheet);
style.loadMask(viewer.getStylePackage());
Bitmap bitmap; Image image;
RealSize size;
img.generateCached(img_options, &style.mask, &combine, &bitmap, &image, &size);
......@@ -54,7 +53,7 @@ bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style
return false;
}
void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value) {
void draw_choice_viewer(RotatedDC& dc, ValueViewer& viewer, ChoiceStyle& style, const String& value) {
if (value.empty()) return;
double margin = 0;
if (style.render_style & RENDER_IMAGE) {
......@@ -65,7 +64,7 @@ void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, c
get_options(dc, viewer, style, img_options);
// Generate image/bitmap
ImageCombine combine = style.combine;
style.loadMask(*viewer.stylesheet);
style.loadMask(viewer.getStylePackage());
Bitmap bitmap; Image image;
RealSize size;
img.generateCached(img_options, &style.mask, &combine, &bitmap, &image, &size);
......@@ -85,16 +84,16 @@ void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, c
}
}
if (style.render_style & RENDER_TEXT) {
String text = tr(*viewer.stylesheet, value, capitalize_sentence);
String text = tr(viewer.getStylePackage(), value, capitalize_sentence);
dc.SetFont(style.font, 1.0);
RealPoint pos = align_in_rect(ALIGN_MIDDLE_LEFT, RealSize(0, dc.GetCharHeight()), dc.getInternalRect()) + RealSize(margin, 0);
dc.DrawTextWithShadow(text, style.font, pos);
}
}
void get_options(Rotation& rot, DataViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts) {
opts.package = viewer.stylesheet.get();
opts.local_package = viewer.getSet().get();
void get_options(Rotation& rot, ValueViewer& viewer, const ChoiceStyle& style, GeneratedImage::Options& opts) {
opts.package = &viewer.getStylePackage();
opts.local_package = &viewer.getLocalPackage();
opts.angle = rot.trAngle(0);
if (viewer.nativeLook()) {
opts.width = opts.height = 16;
......
......@@ -25,8 +25,8 @@ class ChoiceValueViewer : public ValueViewer {
virtual void onStyleChange(int);
};
bool prepare_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value);
void draw_choice_viewer(RotatedDC& dc, DataViewer& viewer, ChoiceStyle& style, const String& value);
bool prepare_choice_viewer(RotatedDC& dc, ValueViewer& viewer, ChoiceStyle& style, const String& value);
void draw_choice_viewer(RotatedDC& dc, ValueViewer& viewer, ChoiceStyle& style, const String& value);
// ----------------------------------------------------------------------------- : EOF
#endif
......@@ -9,7 +9,6 @@
#include <util/prec.hpp>
#include <render/value/color.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
DECLARE_TYPEOF_COLLECTION(ColorField::ChoiceP);
......@@ -100,7 +99,7 @@ void ColorValueViewer::loadMask(const Rotation& rot) const {
if (alpha_mask && alpha_mask->size == wxSize(w,h)) return; // mask loaded and right size
// (re) load the mask
Image image;
InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename);
InputStreamP image_file = getStylePackage().openIn(style().mask_filename);
if (image.LoadFile(*image_file)) {
Image resampled(w,h);
resample(image, resampled);
......
......@@ -9,8 +9,6 @@
#include <util/prec.hpp>
#include <render/value/image.hpp>
#include <render/card/viewer.hpp>
#include <data/set.hpp>
#include <data/stylesheet.hpp>
#include <gui/util.hpp>
DECLARE_TYPEOF_COLLECTION(wxPoint);
......@@ -35,7 +33,7 @@ void ImageValueViewer::draw(RotatedDC& dc) {
// load from file
if (!value().filename.empty()) {
try {
InputStreamP image_file = getSet().openIn(value().filename);
InputStreamP image_file = getLocalPackage().openIn(value().filename);
if (image.LoadFile(*image_file)) {
image.Rescale(w, h);
}
......@@ -45,7 +43,7 @@ void ImageValueViewer::draw(RotatedDC& dc) {
}
// nice placeholder
if (!image.Ok() && style().default_image.isReady()) {
image = style().default_image.generate(GeneratedImage::Options(w, h, viewer.stylesheet.get(), &getSet()));
image = style().default_image.generate(GeneratedImage::Options(w, h, &getStylePackage(), &getLocalPackage()));
is_default = true;
if (viewer.drawEditing()) {
bitmap = imagePlaceholder(dc, w, h, image, viewer.drawEditing());
......@@ -127,7 +125,7 @@ void ImageValueViewer::loadMask(const Rotation& rot) const {
if (alpha_mask && alpha_mask->size == wxSize(w,h)) return; // mask loaded and right size
// (re) load the mask
Image image;
InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename);
InputStreamP image_file = getStylePackage().openIn(style().mask_filename);
if (image.LoadFile(*image_file)) {
alpha_mask = new_intrusive1<AlphaMask>(resample(image,w,h));
}
......
......@@ -10,7 +10,6 @@
#include <render/value/multiple_choice.hpp>
#include <render/value/choice.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
#include <gui/util.hpp>
DECLARE_TYPEOF_COLLECTION(String);
......@@ -21,7 +20,7 @@ IMPLEMENT_VALUE_VIEWER(MultipleChoice);
bool MultipleChoiceValueViewer::prepare(RotatedDC& dc) {
if (style().render_style & (RENDER_CHECKLIST | RENDER_LIST)) return false;
return prepare_choice_viewer(dc, viewer, style(), value().value());
return prepare_choice_viewer(dc, *this, style(), value().value());
}
void MultipleChoiceValueViewer::draw(RotatedDC& dc) {
......@@ -47,7 +46,7 @@ void MultipleChoiceValueViewer::draw(RotatedDC& dc) {
drawChoice(dc, pos, choice);
}
} else {
draw_choice_viewer(dc, viewer, style(), value().value());
draw_choice_viewer(dc, *this, style(), value().value());
}
}
......@@ -62,7 +61,7 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const
map<String,ScriptableImage>::iterator it = style().choice_images.find(cannocial_name_form(choice));
if (it != style().choice_images.end() && it->second.isReady()) {
// TODO: caching
GeneratedImage::Options options(0,0, viewer.stylesheet.get(),&getSet());
GeneratedImage::Options options(0,0, &getStylePackage(), &getLocalPackage());
options.zoom = dc.getZoom();
options.angle = dc.trAngle(style().angle);
Image image = it->second.generate(options);
......@@ -74,7 +73,7 @@ void MultipleChoiceValueViewer::drawChoice(RotatedDC& dc, RealPoint& pos, const
}
if (style().render_style & RENDER_TEXT) {
// draw text
String text = tr(*viewer.stylesheet, choice, capitalize_sentence);
String text = tr(getStylePackage(), choice, capitalize_sentence);
RealSize text_size = dc.GetTextExtent(text);
dc.DrawText(text, align_in_rect(ALIGN_MIDDLE_LEFT, text_size,
RealRect(pos + RealSize(size.width + 1, 0), RealSize(0,size.height))));
......
......@@ -7,9 +7,9 @@
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/io/package.hpp>
#include <render/value/symbol.hpp>
#include <render/symbol/filter.hpp>
#include <data/set.hpp>
#include <data/symbol.hpp>
#include <gui/util.hpp> // draw_checker
#include <util/error.hpp>
......@@ -29,7 +29,7 @@ void SymbolValueViewer::draw(RotatedDC& dc) {
if (symbols.empty() && !value().filename.empty()) {
try {
// load symbol
SymbolP symbol = getSet().readFile<SymbolP>(value().filename);
SymbolP symbol = getLocalPackage().readFile<SymbolP>(value().filename);
// aspect ratio
double ar = symbol->aspectRatio();
ar = min(style().max_aspect_ratio, max(style().min_aspect_ratio, ar));
......
......@@ -9,7 +9,6 @@
#include <util/prec.hpp>
#include <render/value/text.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
// ----------------------------------------------------------------------------- : TextValueViewer
......@@ -19,7 +18,7 @@ bool TextValueViewer::prepare(RotatedDC& dc) {
if (!style().mask_filename.empty() && !style().mask.ok()) {
// load contour mask
Image image;
InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename);
InputStreamP image_file = getStylePackage().openIn(style().mask_filename);
if (image.LoadFile(*image_file)) {
style().mask.load(image);
}
......
......@@ -16,7 +16,8 @@ ValueViewer::ValueViewer(DataViewer& parent, const StyleP& style)
: StyleListener(style), viewer(parent)
{}
Set& ValueViewer::getSet() const { return *viewer.getSet(); }
Package& ValueViewer::getStylePackage() const { return viewer.getStylePackage(); }
Package& ValueViewer::getLocalPackage() const { return viewer.getLocalPackage(); }
void ValueViewer::setValue(const ValueP& value) {
assert(value->fieldP == styleP->fieldP); // matching field
......@@ -47,6 +48,10 @@ void ValueViewer::drawFieldBorder(RotatedDC& dc) {
}
}
void ValueViewer::redraw() {
viewer.redraw(*this);
}
bool ValueViewer::nativeLook() const {
return viewer.nativeLook();
}
......
......@@ -15,6 +15,7 @@
#include <data/field.hpp>
class Set;
class Package;
class DataViewer;
class Action;
DECLARE_POINTER_TYPE(Style);
......@@ -74,16 +75,23 @@ class ValueViewer : public StyleListener {
protected:
ValueP valueP; ///< The value we are currently viewing
/// Draws a border around the field
void drawFieldBorder(RotatedDC& dc);
/// Redraw this viewer
void redraw();
public:
/// Should this viewer render using a platform native look?
bool nativeLook() const;
/// Is this the currently selected viewer?
/** Usually only the editor allows selection of viewers */
bool isCurrent() const;
/// Draws a border around the field
void drawFieldBorder(RotatedDC& dc);
Set& getSet() const;
/// The package containing style stuff like images
Package& getStylePackage() const;
/// The local package for loading/saving files
Package& getLocalPackage() const;
};
// ----------------------------------------------------------------------------- : Utility
......
......@@ -26,7 +26,7 @@ ActionStack::~ActionStack() {
FOR_EACH(a, redo_actions) delete a;
}
void ActionStack::add(Action* action, bool allow_merge) {
void ActionStack::addAction(Action* action, bool allow_merge) {
if (!action) return; // no action
action->perform(false); // TODO: delete action if perform throws
tellListeners(*action, false);
......
......@@ -67,9 +67,11 @@ class ActionStack {
~ActionStack();
/// Add an action to the stack, and perform that action.
/// Tells all listeners about the action.
/// The ActionStack takes ownership of the action
void add(Action* action, bool allowMerge = true);
/** Tells all listeners about the action.
* The ActionStack takes ownership of the action.
* If allow_merge == true then we attempt to merge this action with previous ones
*/
void addAction(Action* action, bool allow_merge = true);
/// Undoes the last action that was (re)done
/** @pre canUndo() */
......
......@@ -19,6 +19,7 @@
#include <util/prec.hpp>
#include <util/string.hpp>
class Package;
class Game;
class StyleSheet;
class SymbolFont;
......@@ -46,6 +47,8 @@ String warn_and_identity(const String&);
/// Translate 'key' in the category 'cat' using the current locale
String tr(LocaleCategory cat, const String& key, DefaultLocaleFun def = warn_and_identity);
/// Translate 'key' in the for a Package using the current locale
String tr(const Package&, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a Game using the current locale
String tr(const Game&, const String& key, DefaultLocaleFun def);
/// Translate 'key' in the for a StyleSheet using the current locale
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment