Commit 40241ca8 authored by twanvl's avatar twanvl

implemented most of DataEditor; fixed some bugs in Settings

parent 4fac3f44
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <data/settings.hpp> #include <data/settings.hpp>
#include <data/game.hpp> #include <data/game.hpp>
#include <data/stylesheet.hpp>
#include <data/field.hpp> #include <data/field.hpp>
#include <util/reflect.hpp> #include <util/reflect.hpp>
#include <util/io/reader.hpp> #include <util/io/reader.hpp>
...@@ -39,6 +40,10 @@ IMPLEMENT_REFLECTION(ColumnSettings) { ...@@ -39,6 +40,10 @@ IMPLEMENT_REFLECTION(ColumnSettings) {
REFLECT(visible); REFLECT(visible);
} }
GameSettings::GameSettings()
: sort_cards_ascending(true)
{}
IMPLEMENT_REFLECTION(GameSettings) { IMPLEMENT_REFLECTION(GameSettings) {
REFLECT(default_stylesheet); REFLECT(default_stylesheet);
REFLECT(default_export); REFLECT(default_export);
...@@ -47,6 +52,23 @@ IMPLEMENT_REFLECTION(GameSettings) { ...@@ -47,6 +52,23 @@ IMPLEMENT_REFLECTION(GameSettings) {
REFLECT(sort_cards_ascending); REFLECT(sort_cards_ascending);
} }
StyleSheetSettings::StyleSheetSettings()
: card_zoom (1.0, true)
, card_angle (0, true)
, card_anti_alias (true, true)
, card_borders (true, true)
, card_normal_export(true, true)
{}
void StyleSheetSettings::useDefault(const StyleSheetSettings& ss) {
if (card_zoom .isDefault()) card_zoom .assignDefault(ss.card_zoom());
if (card_angle .isDefault()) card_angle .assignDefault(ss.card_angle());
if (card_anti_alias .isDefault()) card_anti_alias .assignDefault(ss.card_anti_alias());
if (card_borders .isDefault()) card_borders .assignDefault(ss.card_borders());
if (card_normal_export.isDefault()) card_normal_export.assignDefault(ss.card_normal_export());
}
IMPLEMENT_REFLECTION(StyleSheetSettings) { IMPLEMENT_REFLECTION(StyleSheetSettings) {
REFLECT(card_zoom); REFLECT(card_zoom);
REFLECT(card_angle); REFLECT(card_angle);
...@@ -85,7 +107,7 @@ void Settings::addRecentFile(const String& filename) { ...@@ -85,7 +107,7 @@ void Settings::addRecentFile(const String& filename) {
} }
GameSettings& Settings::gameSettingsFor(const Game& game) { GameSettings& Settings::gameSettingsFor(const Game& game) {
GameSettingsP& gs = settings.game_settings[game.name()]; GameSettingsP& gs = game_settings[game.name()];
if (!gs) gs.reset(new GameSettings); if (!gs) gs.reset(new GameSettings);
return *gs; return *gs;
} }
...@@ -102,14 +124,12 @@ ColumnSettings& Settings::columnSettingsFor(const Game& game, const Field& field ...@@ -102,14 +124,12 @@ ColumnSettings& Settings::columnSettingsFor(const Game& game, const Field& field
} }
return cs; return cs;
} }
/* StyleSheetSettings& Settings::stylesheetSettingsFor(const StyleSheet& stylesheet) {
StyleSettings& Settings::styleSettingsFor(const CardStyle& style) { StyleSheetSettingsP& ss = stylesheet_settings[stylesheet.name()];
StyleSettingsP& ss = settings.styleSettings#(style.name()); if (!ss) ss.reset(new StyleSheetSettings);
if (!ss) ss = new_shared<StyleSettings>(); ss->useDefault(default_stylesheet_settings); // update default settings
ss->useDefault(defaultStyleSettings); // update default settings
return *ss; return *ss;
} }
*/
String user_settings_dir() { String user_settings_dir() {
return _(""); // TODO return _(""); // TODO
......
...@@ -43,6 +43,8 @@ class ColumnSettings { ...@@ -43,6 +43,8 @@ class ColumnSettings {
/// Settings for a Game /// Settings for a Game
class GameSettings { class GameSettings {
public: public:
GameSettings();
String default_stylesheet; String default_stylesheet;
String default_export; String default_export;
map<String, ColumnSettings> columns; map<String, ColumnSettings> columns;
...@@ -55,6 +57,8 @@ class GameSettings { ...@@ -55,6 +57,8 @@ class GameSettings {
/// Settings for a StyleSheet /// Settings for a StyleSheet
class StyleSheetSettings { class StyleSheetSettings {
public: public:
StyleSheetSettings();
// Rendering/display settings // Rendering/display settings
Defaultable<double> card_zoom; Defaultable<double> card_zoom;
Defaultable<int> card_angle; Defaultable<int> card_angle;
...@@ -62,10 +66,10 @@ class StyleSheetSettings { ...@@ -62,10 +66,10 @@ class StyleSheetSettings {
Defaultable<bool> card_borders; Defaultable<bool> card_borders;
Defaultable<bool> card_normal_export; Defaultable<bool> card_normal_export;
DECLARE_REFLECTION(); /// Where the settings are the default, use the value from ss
void useDefault(const StyleSheetSettings& ss);
// /// Where the settings are the default, use the value from ss DECLARE_REFLECTION();
// void useDefault(const StyleSheetSettings& ss);
}; };
// ----------------------------------------------------------------------------- : Settings // ----------------------------------------------------------------------------- : Settings
...@@ -102,7 +106,7 @@ class Settings { ...@@ -102,7 +106,7 @@ class Settings {
/// Get the settings for a column for a specific field in a game /// Get the settings for a column for a specific field in a game
ColumnSettings& columnSettingsFor (const Game& game, const Field& field); ColumnSettings& columnSettingsFor (const Game& game, const Field& field);
/// Get the settings object for a specific stylesheet /// Get the settings object for a specific stylesheet
StyleSheetSettings& styleSheetSettingsFor(const StyleSheet& stylesheet); StyleSheetSettings& stylesheetSettingsFor(const StyleSheet& stylesheet);
private: private:
map<String,GameSettingsP> game_settings; map<String,GameSettingsP> game_settings;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <util/prec.hpp> #include <util/prec.hpp>
#include <util/io/package.hpp> #include <util/io/package.hpp>
#include <util/real_point.hpp>
#include <script/scriptable.hpp> #include <script/scriptable.hpp>
DECLARE_POINTER_TYPE(Game); DECLARE_POINTER_TYPE(Game);
...@@ -47,6 +48,8 @@ class StyleSheet : public Packaged { ...@@ -47,6 +48,8 @@ class StyleSheet : public Packaged {
bool dependencies_initialized; ///< are the script dependencies comming from this stylesheet all initialized? bool dependencies_initialized; ///< are the script dependencies comming from this stylesheet all initialized?
inline RealRect getCardRect() const { return RealRect(0, 0, card_width, card_height); }
/// Load a StyleSheet, given a Game and the name of the StyleSheet /// Load a StyleSheet, given a Game and the name of the StyleSheet
static StyleSheetP byGameAndName(const Game& game, const String& name); static StyleSheetP byGameAndName(const Game& game, const String& name);
/// name of the package without the game name /// name of the package without the game name
......
...@@ -8,23 +8,189 @@ ...@@ -8,23 +8,189 @@
#include <gui/control/card_editor.hpp> #include <gui/control/card_editor.hpp>
#include <gui/value/editor.hpp> #include <gui/value/editor.hpp>
#include <gui/icon_menu.hpp>
#include <data/field.hpp> #include <data/field.hpp>
#include <data/stylesheet.hpp>
#include <data/settings.hpp>
DECLARE_TYPEOF_COLLECTION(ValueViewerP);
// ----------------------------------------------------------------------------- : DataEditor // ----------------------------------------------------------------------------- : DataEditor
#define FOR_EACH_EDITOR \ #define FOR_EACH_EDITOR \
FOR_EACH(v, viewers) \ FOR_EACH(v, viewers) \
if (ValueEditor* e = v->getEditor())
#define FOR_EACH_EDITOR_REVERSE \
FOR_EACH_REVERSE(v, viewers) \
if (ValueEditor* e = v->getEditor()) if (ValueEditor* e = v->getEditor())
DataEditor::DataEditor(Window* parent, int id, long style) DataEditor::DataEditor(Window* parent, int id, long style)
: CardViewer(parent, id, style) : CardViewer(parent, id, style)
, current_viewer(nullptr)
, current_editor(nullptr)
{} {}
ValueViewerP DataEditor::makeViewer(const StyleP& style) { ValueViewerP DataEditor::makeViewer(const StyleP& style) {
return style->makeEditor(*this, style); return style->makeEditor(*this, style);
} }
// ----------------------------------------------------------------------------- : Utility for ValueViewers
bool DataEditor::drawBorders() const {
return !nativeLook() && settings.stylesheetSettingsFor(*set->stylesheetFor(card)).card_borders();
}
bool DataEditor::drawEditing() const {
return true;
}
wxPen DataEditor::borderPen(bool active) const {
return active ? wxPen(Color(0,128,255), 1, wxSOLID)
: wxPen(Color(128,128,128), 1, wxDOT);
}
ValueViewer* DataEditor::focusedViewer() const {
return current_viewer;
}
// ----------------------------------------------------------------------------- : Selection
// ----------------------------------------------------------------------------- : Clipboard & Formatting
bool DataEditor::canCut() const { return current_editor && current_editor->canCut(); }
bool DataEditor::canCopy() const { return current_editor && current_editor->canCopy(); }
bool DataEditor::canPaste() const { return current_editor && current_editor->canPaste(); }
bool DataEditor::canFormat(int type) const { return current_editor && current_editor->canFormat(type); }
bool DataEditor::hasFormat(int type) const { return current_editor && current_editor->hasFormat(type); }
void DataEditor::doCut() { if (current_editor) current_editor->doCut(); }
void DataEditor::doCopy() { if (current_editor) current_editor->doCopy(); }
void DataEditor::doPaste() { if (current_editor) current_editor->doPaste(); }
void DataEditor::doFormat(int type) { if (current_editor) current_editor->doFormat(type); }
// ----------------------------------------------------------------------------- : Mouse events
void DataEditor::onLeftDown(wxMouseEvent& ev) {
ev.Skip(); // for focus
CaptureMouse();
// change selection?
selectField(ev, &ValueEditor::onLeftDown);
}
void DataEditor::onLeftUp(wxMouseEvent& ev) {
if (HasCapture()) ReleaseMouse();
if (current_editor) current_editor->onLeftUp(mousePoint(ev), ev);
}
void DataEditor::onLeftDClick(wxMouseEvent& ev) {
if (current_editor) current_editor->onLeftDClick(mousePoint(ev), ev);
}
void DataEditor::onRightDown(wxMouseEvent& ev) {
ev.Skip(); // for context menu
// change selection?
selectField(ev, &ValueEditor::onRightDown);
}
void DataEditor::onMouseWheel(wxMouseEvent& ev) {
if (current_editor) current_editor->onMouseWheel(mousePoint(ev), ev);
}
void DataEditor::onMotion(wxMouseEvent& ev) {
RealPoint pos = mousePoint(ev);
if (current_editor && ev.LeftIsDown()) {
current_editor->onMotion(pos, ev);
}
if (!HasCapture()) {
// change cursor and set status text
wxFrame* frame = dynamic_cast<wxFrame*>( wxGetTopLevelParent(this) );
FOR_EACH_EDITOR_REVERSE { // find high z index fields first
if (v->containsPoint(pos) && v->getField()->editable) {
wxCursor c = e->cursor();
if (c.Ok()) SetCursor(c);
else SetCursor(wxCURSOR_ARROW);
if (frame) frame->SetStatusText(v->getField()->description);
return;
}
}
// no field under cursor
SetCursor(wxCURSOR_ARROW);
if (frame) frame->SetStatusText(wxEmptyString);
}
}
void DataEditor::onMouseLeave(wxMouseEvent& ev) {
wxFrame* frame = dynamic_cast<wxFrame*>( wxGetTopLevelParent(this) );
if (frame) frame->SetStatusText(wxEmptyString);
}
void DataEditor::selectField(wxMouseEvent& ev, void (ValueEditor::*event)(const RealPoint&, wxMouseEvent&)) {
RealPoint pos = mousePoint(ev);
// change viewer/editor
ValueEditor* old_editor = current_editor;
selectFieldNoEvents(pos);
if (old_editor != current_editor) {
// selection has changed, send focus events
if (old_editor) old_editor->onLoseFocus();
if (current_editor) current_editor->onFocus();
}
// pass event
if (current_editor) (current_editor->*event)(pos, ev);
// refresh?
if (old_editor != current_editor) {
// selection has changed, refresh viewers
// NOTE: after passing mouse down event, otherwise opening combo box produces flicker
onChange();
}
}
void DataEditor::selectFieldNoEvents(const RealPoint& p) {
FOR_EACH_EDITOR_REVERSE { // find high z index fields first
if (v->containsPoint(p) && v->getField()->editable) {
current_viewer = v.get();
current_editor = e;
return;
}
}
current_viewer = nullptr;
current_editor = nullptr;
}
RealPoint DataEditor::mousePoint(const wxMouseEvent& ev) {
StyleSheetP stylesheet = set->stylesheetFor(card);
StyleSheetSettings& ss = settings.stylesheetSettingsFor(*stylesheet);
Rotation rot(ss.card_angle(), stylesheet->getCardRect(), ss.card_zoom());
return rot.trInv(RealPoint(ev.GetX(), ev.GetY()));
}
// ----------------------------------------------------------------------------- : Keyboard events
// ----------------------------------------------------------------------------- : Menu events
void DataEditor::onContextMenu(wxContextMenuEvent& ev) {
if (current_editor) {
IconMenu m;
m.Append(wxID_CUT, _("TOOL_CUT"), _("Cu&t"), _("Move the selected text to the clipboard"));
m.Append(wxID_COPY, _("TOOL_COPY"), _("&Copy"), _("Place the selected text on the clipboard"));
m.Append(wxID_PASTE, _("TOOL_PASTE"), _("&Paste"), _("Inserts the text from the clipboard"));
m.Enable(wxID_CUT, canCut());
m.Enable(wxID_COPY, canCopy());
m.Enable(wxID_PASTE, canPaste());
if (current_editor->onContextMenu(m, ev)) {
PopupMenu(&m);
}
}
}
// ----------------------------------------------------------------------------- : Focus events
// ----------------------------------------------------------------------------- : Event table // ----------------------------------------------------------------------------- : Event table
BEGIN_EVENT_TABLE(DataEditor, CardViewer) BEGIN_EVENT_TABLE(DataEditor, CardViewer)
EVT_LEFT_DOWN (DataEditor::onLeftDown)
EVT_LEFT_UP (DataEditor::onLeftUp)
EVT_LEFT_DCLICK (DataEditor::onLeftDClick)
EVT_RIGHT_DOWN (DataEditor::onRightDown)
EVT_MOTION (DataEditor::onMotion)
EVT_MOUSEWHEEL (DataEditor::onMouseWheel)
EVT_LEAVE_WINDOW (DataEditor::onMouseLeave)
// EVT_CONTEXT_MENU (DataEditor::onContextMenu)
// EVT_CHAR (DataEditor::onChar)
// EVT_SET_FOCUS (DataEditor::onFocus)
// EVT_KILL_FOCUS (DataEditor::onLoseFocus)
// EVT_MENU (wxID_ANY, DataEditor::onMenu)
END_EVENT_TABLE () END_EVENT_TABLE ()
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include <util/prec.hpp> #include <util/prec.hpp>
#include <gui/control/card_viewer.hpp> #include <gui/control/card_viewer.hpp>
class ValueEditor;
// ----------------------------------------------------------------------------- : DataEditor // ----------------------------------------------------------------------------- : DataEditor
/// An editor for data values (usually a card) /// An editor for data values (usually a card)
...@@ -19,12 +21,72 @@ class DataEditor : public CardViewer { ...@@ -19,12 +21,72 @@ class DataEditor : public CardViewer {
public: public:
DataEditor(Window* parent, int id, long style = 0); DataEditor(Window* parent, int id, long style = 0);
// --------------------------------------------------- : Utility for ValueViewers
virtual bool drawBorders() const;
virtual bool drawEditing() const;
virtual wxPen borderPen(bool active) const;
virtual ValueViewer* focusedViewer() const;
// --------------------------------------------------- : Selection
// TODO
// --------------------------------------------------- : Clipboard
bool canCut() const;
bool canCopy() const;
bool canPaste() const;
void doCut();
void doCopy();
void doPaste();
// --------------------------------------------------- : Formatting
bool canFormat(int type) const;
bool hasFormat(int type) const;
void doFormat (int type);
// --------------------------------------------------- : ValueViewers
protected: protected:
/// Create an editor for the given style (as opposed to a normal viewer) /// Create an editor for the given style (as opposed to a normal viewer)
virtual ValueViewerP makeViewer(const StyleP&); virtual ValueViewerP makeViewer(const StyleP&);
// --------------------------------------------------- : Data
private: private:
DECLARE_EVENT_TABLE(); DECLARE_EVENT_TABLE();
ValueViewer* current_viewer; ///< The currently selected viewer
ValueEditor* current_editor; ///< The currently selected editor, corresponding to the viewer
// --------------------------------------------------- : Events
void onLeftDown (wxMouseEvent&);
void onLeftUp (wxMouseEvent&);
void onLeftDClick(wxMouseEvent&);
void onRightDown (wxMouseEvent&);
void onMotion (wxMouseEvent&);
void onMouseWheel(wxMouseEvent&);
void onMouseLeave(wxMouseEvent&);
void onChar (wxKeyEvent&);
void onContextMenu(wxContextMenuEvent&);
void onMenu (wxCommandEvent& e);
void onFocus (wxFocusEvent&);
void onLoseFocus(wxFocusEvent&);
// --------------------------------------------------- : Functions
/// Changes the selection to the field at the specified coordinates
/** Sends an event to the event function of the current viewer */
void selectField(wxMouseEvent& ev, void (ValueEditor::*event)(const RealPoint&, wxMouseEvent&));
// selectField, but don't send events
void selectFieldNoEvents(const RealPoint& pos);
/// Convert mouse coordinates to internal coordinates
RealPoint mousePoint(const wxMouseEvent& e);
}; };
/// By default a DataEditor edits cards /// By default a DataEditor edits cards
......
...@@ -39,21 +39,21 @@ class ValueEditor { ...@@ -39,21 +39,21 @@ class ValueEditor {
virtual void onLoseFocus() {} virtual void onLoseFocus() {}
/// Handle mouse events /// Handle mouse events
virtual void onMouseLeftDown (RealPoint pos, wxMouseEvent& ev) {} virtual void onLeftDown (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseLeftUp (RealPoint pos, wxMouseEvent& ev) {} virtual void onLeftUp (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseDClick (RealPoint pos, wxMouseEvent& ev) {} virtual void onLeftDClick (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseRightDown(RealPoint pos, wxMouseEvent& ev) {} virtual void onRightDown (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseMove (RealPoint pos, wxMouseEvent& ev) {} virtual void onMotion (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseWheel (RealPoint pos, wxMouseEvent& ev) {} virtual void onMouseWheel (const RealPoint& pos, wxMouseEvent& ev) {}
/// Key events /// Key events
virtual void onChar(wxKeyEvent ev) {} virtual void onChar(wxKeyEvent ev) {}
/// A menu item was selected
virtual void onMenu(wxCommandEvent& ev) { ev.Skip(); }
/// a context menu is requested, add extra items to the menu m /// a context menu is requested, add extra items to the menu m
/** return false to suppress menu */ /** return false to suppress menu */
virtual bool onContextMenu(wxMenu& m, wxContextMenuEvent& ev) { return true; } virtual bool onContextMenu(wxMenu& m, wxContextMenuEvent& ev) { return true; }
/// A menu item was selected
virtual void onMenu(wxCommandEvent& ev) { ev.Skip(); }
// --------------------------------------------------- : Clipboard // --------------------------------------------------- : Clipboard
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <data/stylesheet.hpp> #include <data/stylesheet.hpp>
#include <data/card.hpp> #include <data/card.hpp>
#include <data/field.hpp> #include <data/field.hpp>
#include <data/settings.hpp>
DECLARE_TYPEOF_COLLECTION(ValueViewerP); DECLARE_TYPEOF_COLLECTION(ValueViewerP);
typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP; typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP;
...@@ -23,8 +24,9 @@ DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP); ...@@ -23,8 +24,9 @@ DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP);
// ----------------------------------------------------------------------------- : Drawing // ----------------------------------------------------------------------------- : Drawing
void DataViewer::draw(DC& dc) { void DataViewer::draw(DC& dc) {
// RotatedDC rdc(dc, rotation, settings.styleSettingsFor(*style).cardAntiAlias && !nativeLook()) StyleSheetP stylesheet = set->stylesheetFor(card);
RotatedDC rdc(dc, 0, RealRect(0,0,400,400), 1.0, false); StyleSheetSettings& ss = settings.stylesheetSettingsFor(*stylesheet);
RotatedDC rdc(dc, ss.card_angle(), stylesheet->getCardRect(), ss.card_zoom(), ss.card_anti_alias() && !nativeLook());
draw(rdc); draw(rdc);
} }
void DataViewer::draw(RotatedDC& dc) { void DataViewer::draw(RotatedDC& dc) {
......
...@@ -73,9 +73,7 @@ class DataViewer : public SetView { ...@@ -73,9 +73,7 @@ class DataViewer : public SetView {
/// Notification that the total image has changed /// Notification that the total image has changed
virtual void onChange() {} virtual void onChange() {}
private:
vector<ValueViewerP> viewers; ///< The viewers for the different values in the data vector<ValueViewerP> viewers; ///< The viewers for the different values in the data
protected:
CardP card; ///< The card that is currently displayed, if any CardP card; ///< The card that is currently displayed, if any
}; };
......
...@@ -20,14 +20,19 @@ ...@@ -20,14 +20,19 @@
template <typename T> template <typename T>
class Defaultable { class Defaultable {
public: public:
inline Defaultable() : is_default(true) {} inline Defaultable() : is_default(true) {}
inline Defaultable(const T& v) : value(v), is_default(false) {} inline Defaultable(const T& v, bool def = false) : value(v), is_default(def) {}
/// Assigning a value takes this object out of the default state /// Assigning a value takes this object out of the default state
inline void assign(const T& new_value) { inline void assign(const T& new_value) {
value = new_value; value = new_value;
is_default = false; is_default = false;
} }
/// Assigning a value keep this object in the default state
inline void assignDefault(const T& new_value) {
assert(is_default);
value = new_value;
}
/// Get access to the value /// Get access to the value
inline const T& operator () () const { return value; } inline const T& operator () () const { return value; }
......
...@@ -124,7 +124,7 @@ String cannocial_name_form(const String& str) { ...@@ -124,7 +124,7 @@ String cannocial_name_form(const String& str) {
FOR_EACH_CONST(c, str) { FOR_EACH_CONST(c, str) {
if ((c == _('_') || c == _(' ')) && !leading) { if ((c == _('_') || c == _(' ')) && !leading) {
ret += _(' '); ret += _(' ');
} else if (isAlnum(c)) { } else if (isAlnum(c) || c == _('-')) {
ret += toLower(c); ret += toLower(c);
leading = false; leading = false;
} else { } else {
......
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