mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Partially working text editor
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@99 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <data/field/color.hpp>
|
||||
#include <data/field/image.hpp>
|
||||
#include <data/field/symbol.hpp>
|
||||
#include <util/tagged_string.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : ValueAction
|
||||
|
||||
@@ -35,16 +36,14 @@ class SimpleValueAction : public ValueAction {
|
||||
swap(static_cast<T&>(*valueP).*member, new_value);
|
||||
}
|
||||
|
||||
virtual bool merge(const Action* action) {
|
||||
virtual bool merge(const Action& action) {
|
||||
if (!ALLOW_MERGE) return false;
|
||||
if (const SimpleValueAction* sva = dynamic_cast<const SimpleValueAction*>(action)) {
|
||||
if (sva->valueP == valueP) {
|
||||
TYPE_CASE(action, SimpleValueAction) {
|
||||
if (action.valueP == valueP) {
|
||||
// adjacent actions on the same value, discard the other one,
|
||||
// because it only keeps an intermediate value
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -60,3 +59,85 @@ ValueAction* value_action(const SymbolValueP& value, const FileName&
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Text
|
||||
|
||||
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), new_value(new_value), name(name)
|
||||
, selection_start(start), selection_end(end), new_selection_end(new_end)
|
||||
{}
|
||||
|
||||
String TextValueAction::getName(bool to_undo) const { return name; }
|
||||
|
||||
void TextValueAction::perform(bool to_undo) {
|
||||
swap(value().value, new_value);
|
||||
// if (value().value.age < new_value.age) value().value.age = Age();
|
||||
swap(selection_end, new_selection_end);
|
||||
}
|
||||
|
||||
bool TextValueAction::merge(const Action& action) {
|
||||
TYPE_CASE(action, TextValueAction) {
|
||||
if (&action.value() == &value() && action.name == name && action.selection_start == selection_end) {
|
||||
// adjacent edits, keep old value of this, it is older
|
||||
selection_end = action.selection_end;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
TextValue& TextValueAction::value() const {
|
||||
return static_cast<TextValue&>(*valueP);
|
||||
}
|
||||
|
||||
|
||||
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start, size_t end, const String& action_name) {
|
||||
if (start > end) swap(start, end);
|
||||
String new_value;
|
||||
const String& str = value->value();
|
||||
// Are we inside the tag we are toggling?
|
||||
size_t tagpos = in_tag(str, _("<") + tag, start, end);
|
||||
if (tagpos == String::npos) {
|
||||
// we are not inside this tag, add it
|
||||
new_value = str.substr(0, start);
|
||||
new_value += _("<") + tag + _(">");
|
||||
new_value += str.substr(start, end - start);
|
||||
new_value += _("</") + tag + _(">");
|
||||
new_value += str.substr(end);
|
||||
} else {
|
||||
// we are inside this tag, _('remove') it
|
||||
new_value = str.substr(0, start);
|
||||
new_value += _("</") + tag + _(">");
|
||||
new_value += str.substr(start, end - start);
|
||||
new_value += _("<") + tag + _(">");
|
||||
new_value += str.substr(end);
|
||||
}
|
||||
// Build action
|
||||
if (start != end) {
|
||||
// don't simplify if start == end, this way we insert <b></b>, allowing the
|
||||
// user to press Ctrl+B and start typing bold text
|
||||
new_value = simplify_tagged(new_value);
|
||||
}
|
||||
if (value->value() == new_value) {
|
||||
return nullptr; // no changes
|
||||
} else {
|
||||
return new TextValueAction(value, start, end, end, new_value, action_name);
|
||||
}
|
||||
}
|
||||
|
||||
TextValueAction* typing_action(const TextValueP& value, size_t start, size_t end, const String& replacement, const String& action_name) {
|
||||
bool reverse = start > end;
|
||||
if (reverse) swap(start, end);
|
||||
String new_value = tagged_substr_replace(value->value(), start, end, replacement);
|
||||
if (value->value() == new_value) {
|
||||
// no change
|
||||
return nullptr;
|
||||
} else {
|
||||
// if (name == _("Backspace")) {
|
||||
// // HACK: put start after end
|
||||
if (reverse) {
|
||||
return new TextValueAction(value, end, start, start+replacement.size(), new_value, action_name);
|
||||
} else {
|
||||
return new TextValueAction(value, start, end, start+replacement.size(), new_value, action_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-16
@@ -37,18 +37,9 @@ class ValueAction : public Action {
|
||||
const ValueP valueP; ///< The modified value
|
||||
};
|
||||
|
||||
/// Utility macro for declaring classes derived from ValueAction
|
||||
#define DECLARE_VALUE_ACTION(Type) \
|
||||
protected: \
|
||||
inline Type##Value& value() const { \
|
||||
return static_cast<Type##Value&>(*valueP); \
|
||||
} \
|
||||
public: \
|
||||
virtual void perform(bool to_undo)
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Simple
|
||||
|
||||
/// Action that updates a Value to a new value
|
||||
ValueAction* value_action(const ChoiceValueP& value, const Defaultable<String>& new_value);
|
||||
ValueAction* value_action(const ColorValueP& value, const Defaultable<Color>& new_value);
|
||||
ValueAction* value_action(const ImageValueP& value, const FileName& new_value);
|
||||
@@ -56,17 +47,30 @@ ValueAction* value_action(const SymbolValueP& value, const FileName&
|
||||
|
||||
// ----------------------------------------------------------------------------- : Text
|
||||
|
||||
/*
|
||||
class ColorValueAction : public ValueAction {
|
||||
/// An action that changes a TextValue
|
||||
class TextValueAction : public ValueAction {
|
||||
public:
|
||||
ColorValueAction(const ColorValueP& value, const Defaultable<Color>& color);
|
||||
TextValueAction(const TextValueP& value, size_t start, size_t end, size_t new_end, const Defaultable<String>& new_value, const String& name);
|
||||
|
||||
DECLARE_VALUE_ACTION(Color);
|
||||
virtual String getName(bool to_undo) const;
|
||||
virtual void perform(bool to_undo);
|
||||
virtual bool merge(const Action& action);
|
||||
|
||||
/// The modified selection
|
||||
size_t selection_start, selection_end;
|
||||
private:
|
||||
Defaultable<Color> color; ///< The new/old color
|
||||
inline TextValue& value() const;
|
||||
|
||||
size_t new_selection_end;
|
||||
Defaultable<String> new_value;
|
||||
String name;
|
||||
};
|
||||
*/
|
||||
|
||||
/// Action for toggleing some formating tag on or off in some range
|
||||
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start, size_t end, const String& action_name);
|
||||
|
||||
/// Typing in a TextValue, replace the selection [start...end) with replacement
|
||||
TextValueAction* typing_action(const TextValueP& value, size_t start, size_t end, const String& replacement, const String& action_name);
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/defaultable.hpp>
|
||||
#include <util/rotation.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <data/font.hpp>
|
||||
#include <data/symbol_font.hpp>
|
||||
@@ -68,6 +69,11 @@ class TextStyle : public Style {
|
||||
virtual bool update(Context&);
|
||||
virtual void initDependencies(Context&, const Dependency&) const;
|
||||
|
||||
/// The rotation to use when drawing
|
||||
inline Rotation getRotation() const {
|
||||
return Rotation(angle, getRect());
|
||||
}
|
||||
|
||||
private:
|
||||
DECLARE_REFLECTION();
|
||||
};
|
||||
|
||||
@@ -76,6 +76,7 @@ class SymbolFont : public Packaged {
|
||||
|
||||
/// Size of a single symbol
|
||||
RealSize symbolSize (Context& ctx, double font_size, const DrawableSymbol& sym);
|
||||
public:
|
||||
/// Size of the default symbol
|
||||
RealSize defaultSymbolSize(Context& ctx, double font_size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user