mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-11 05:07:00 -04:00
Rotation and reflection should now work correctly;
Finished the symmetry editor git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@538 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -16,11 +16,13 @@
|
||||
|
||||
// ----------------------------------------------------------------------------- : SymbolSymmetryEditor
|
||||
|
||||
SymbolSymmetryEditor::SymbolSymmetryEditor(SymbolControl* control)
|
||||
SymbolSymmetryEditor::SymbolSymmetryEditor(SymbolControl* control, const SymbolSymmetryP& sym)
|
||||
: SymbolEditorBase(control)
|
||||
, mode(ID_SYMMETRY_ROTATION)
|
||||
, drawing(false)
|
||||
, symmetry(control->selected_symmetry)
|
||||
, symmetryMoveAction(nullptr)
|
||||
{
|
||||
symmetry = sym;
|
||||
control->selected_symmetry = symmetry;
|
||||
control->SetCursor(*wxCROSS_CURSOR);
|
||||
}
|
||||
|
||||
@@ -28,7 +30,23 @@ SymbolSymmetryEditor::SymbolSymmetryEditor(SymbolControl* control)
|
||||
|
||||
void SymbolSymmetryEditor::draw(DC& dc) {
|
||||
if (symmetry) {
|
||||
control.highlightPart(dc, *symmetry);
|
||||
control.highlightPart(dc, *symmetry, HIGHLIGHT_BORDER);
|
||||
Color color(255,100,0);
|
||||
Vector2D center = control.rotation.tr(symmetry->center);
|
||||
Vector2D handle = control.rotation.tr(symmetry->center + symmetry->handle);
|
||||
if (symmetry->kind == SYMMETRY_REFLECTION) {
|
||||
// draw line to handle
|
||||
dc.SetPen(wxPen(color,1,wxDOT));
|
||||
dc.DrawLine(center.x, center.y, handle.x, handle.y);
|
||||
// draw handle
|
||||
dc.SetPen(*wxBLACK_PEN);
|
||||
dc.SetBrush(color);
|
||||
dc.DrawCircle(handle.x, handle.y, hovered == SELECTION_HANDLE ? 7 : 4);
|
||||
}
|
||||
// draw center
|
||||
dc.SetPen(*wxBLACK_PEN);
|
||||
dc.SetBrush(color);
|
||||
dc.DrawCircle(center.x, center.y, hovered == SELECTION_CENTER ? 8 : 5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,29 +57,41 @@ void SymbolSymmetryEditor::initUI(wxToolBar* tb, wxMenuBar* mb) {
|
||||
copies->SetHelpText(_HELP_("copies"));
|
||||
copies->SetSize(50, -1);
|
||||
tb->AddSeparator();
|
||||
tb->AddTool(ID_ADD_SYMMETRY, _TOOL_("add symmetry"), load_resource_tool_image(_("symmetry_add")), wxNullBitmap, wxITEM_CHECK, _TOOLTIP_("add symmetry"), _HELP_("add symmetry"));
|
||||
tb->AddSeparator();
|
||||
tb->AddTool(ID_SYMMETRY_ROTATION, _TOOL_("rotation"), load_resource_image(_("symmetry_rotation")), wxNullBitmap, wxITEM_CHECK, _TOOLTIP_("rotation"), _HELP_("rotation"));
|
||||
tb->AddTool(ID_SYMMETRY_REFLECTION, _TOOL_("reflection"), load_resource_image(_("symmetry_reflection")), wxNullBitmap, wxITEM_CHECK, _TOOLTIP_("reflection"), _HELP_("reflection"));
|
||||
tb->AddSeparator();
|
||||
tb->AddControl(copies);
|
||||
tb->Realize();
|
||||
control.SetCursor(*wxCROSS_CURSOR);
|
||||
stopActions(); // set status text
|
||||
resetActions(); // set status text
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::destroyUI(wxToolBar* tb, wxMenuBar* mb) {
|
||||
tb->DeleteTool(ID_SYMMETRY_REFLECTION);
|
||||
tb->DeleteTool(ID_SYMMETRY_ROTATION);
|
||||
tb->DeleteTool(ID_ADD_SYMMETRY);
|
||||
// HACK: hardcoded size of rest of toolbar
|
||||
tb->DeleteToolByPos(7); // delete separator
|
||||
tb->DeleteTool(ID_COPIES); // delete sides
|
||||
tb->DeleteToolByPos(7); // delete separator
|
||||
tb->DeleteToolByPos(7); // delete separator
|
||||
tb->DeleteTool(ID_COPIES); // delete copies
|
||||
#if wxVERSION_NUMBER < 2600
|
||||
delete sides;
|
||||
delete copies;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::onUpdateUI(wxUpdateUIEvent& ev) {
|
||||
if (ev.GetId() >= ID_SYMMETRY && ev.GetId() < ID_SYMMETRY_MAX) {
|
||||
ev.Check(ev.GetId() == mode);
|
||||
ev.Enable(symmetry);
|
||||
ev.Check(symmetry && symmetry->kind == ev.GetId() - ID_SYMMETRY);
|
||||
} else if (ev.GetId() == ID_COPIES) {
|
||||
ev.Enable(symmetry);
|
||||
if (symmetry) {
|
||||
copies->SetValue(symmetry->copies);
|
||||
}
|
||||
} else if (ev.GetId() == ID_ADD_SYMMETRY) {
|
||||
ev.Enable(true);
|
||||
} else {
|
||||
ev.Enable(false); // we don't know about this item
|
||||
@@ -70,8 +100,28 @@ void SymbolSymmetryEditor::onUpdateUI(wxUpdateUIEvent& ev) {
|
||||
|
||||
void SymbolSymmetryEditor::onCommand(int id) {
|
||||
if (id >= ID_SYMMETRY && id < ID_SYMMETRY_MAX) {
|
||||
mode = id;
|
||||
stopActions();
|
||||
SymbolSymmetryType kind = id == ID_SYMMETRY_ROTATION ? SYMMETRY_ROTATION : SYMMETRY_REFLECTION;
|
||||
if (symmetry && symmetry->kind != kind) {
|
||||
getSymbol()->actions.add(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()));
|
||||
control.Refresh(false);
|
||||
}
|
||||
resetActions();
|
||||
} else if (id == ID_ADD_SYMMETRY) {
|
||||
symmetry = new_intrusive<SymbolSymmetry>();
|
||||
symmetry->kind = SYMMETRY_ROTATION;
|
||||
symmetry->copies = 2;
|
||||
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));
|
||||
control.selected_parts.select(symmetry);
|
||||
control.Refresh(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,81 +130,70 @@ int SymbolSymmetryEditor::modeToolId() { return ID_MODE_SYMMETRY; }
|
||||
// ----------------------------------------------------------------------------- : Mouse events
|
||||
|
||||
void SymbolSymmetryEditor::onLeftDown (const Vector2D& pos, wxMouseEvent& ev) {
|
||||
// Start drawing
|
||||
drawing = true;
|
||||
center = handle = pos;
|
||||
SetStatusText(_HELP_("drag to draw symmetry"));
|
||||
if (!symmetry) return;
|
||||
selection = findSelection(pos);
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::onLeftUp (const Vector2D& pos, wxMouseEvent& ev) {
|
||||
if (drawing && symmetry) {
|
||||
// Finalize the symmetry
|
||||
getSymbol()->actions.add(new AddSymbolPartAction(*getSymbol(), symmetry));
|
||||
// Select the part
|
||||
control.selectPart(symmetry);
|
||||
// No need to clean up, this editor gets destroyed
|
||||
//stopActions();
|
||||
if (!symmetry) return;
|
||||
if (isEditing()) {
|
||||
resetActions();
|
||||
}
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::onMouseDrag (const Vector2D& from, const Vector2D& to, wxMouseEvent& ev) {
|
||||
if (!symmetry) return;
|
||||
// Resize the object
|
||||
if (drawing) {
|
||||
handle = to;
|
||||
makePart(center, handle, ev.ControlDown(), settings.symbol_grid_snap != ev.ShiftDown());
|
||||
control.Refresh(false);
|
||||
if (selection == SELECTION_NONE) return;
|
||||
if (!symmetryMoveAction) {
|
||||
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);
|
||||
}
|
||||
symmetryMoveAction->move(to - from);
|
||||
control.Refresh(false);
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::onMouseMove (const Vector2D& from, const Vector2D& to, wxMouseEvent& ev) {
|
||||
Selection old_hovered = hovered;
|
||||
hovered = findSelection(to);
|
||||
if (hovered != old_hovered) control.Refresh(false);
|
||||
// TODO: set status text
|
||||
}
|
||||
|
||||
SymbolSymmetryEditor::Selection SymbolSymmetryEditor::findSelection(const Vector2D& pos) {
|
||||
if (!symmetry) return SELECTION_NONE;
|
||||
Vector2D pos_pixel = control.rotation.tr(pos);
|
||||
Vector2D center = control.rotation.tr(symmetry->center);
|
||||
if ((center - pos_pixel).lengthSqr() < 5*5) return SELECTION_CENTER;
|
||||
if (symmetry->kind == SYMMETRY_REFLECTION) {
|
||||
Vector2D handle = control.rotation.tr(symmetry->center + symmetry->handle);
|
||||
if ((handle - pos_pixel).lengthSqr() < 5*5) return SELECTION_HANDLE;
|
||||
}
|
||||
return SELECTION_NONE;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Other events
|
||||
|
||||
void SymbolSymmetryEditor::onKeyChange(wxKeyEvent& ev) {
|
||||
if (drawing) {
|
||||
if (symmetryMoveAction) {
|
||||
if (ev.GetKeyCode() == WXK_CONTROL || ev.GetKeyCode() == WXK_SHIFT) {
|
||||
// changed constrains
|
||||
makePart(center, handle, ev.ControlDown(), settings.symbol_grid_snap != ev.ShiftDown());
|
||||
symmetryMoveAction->constrain = ev.ControlDown();
|
||||
symmetryMoveAction->snap = ev.ShiftDown() != settings.symbol_grid_snap ? settings.symbol_grid_size : 0;
|
||||
control.Refresh(false);
|
||||
} else if (ev.GetKeyCode() == WXK_ESCAPE) {
|
||||
// cancel drawing
|
||||
stopActions();
|
||||
resetActions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SymbolSymmetryEditor::isEditing() { return drawing; }
|
||||
|
||||
// ----------------------------------------------------------------------------- : Generating shapes
|
||||
|
||||
void SymbolSymmetryEditor::stopActions() {
|
||||
symmetry = SymbolSymmetryP();
|
||||
drawing = false;
|
||||
SetStatusText(_HELP_("draw symmetry"));
|
||||
control.Refresh(false);
|
||||
bool SymbolSymmetryEditor::isEditing() {
|
||||
return symmetryMoveAction;
|
||||
}
|
||||
|
||||
void SymbolSymmetryEditor::makePart(Vector2D a, Vector2D b, bool constrained, bool snap) {
|
||||
// snap
|
||||
if (snap) {
|
||||
a = snap_vector(a, settings.symbol_grid_size);
|
||||
b = snap_vector(b, settings.symbol_grid_size);
|
||||
}
|
||||
// constrain
|
||||
Vector2D dir = b - a;
|
||||
if (constrained) {
|
||||
double angle = atan2(dir.y, dir.x);
|
||||
// multiples of 2pi/24 i.e. 24 stops
|
||||
double mult = (2 * M_PI) / 24;
|
||||
angle = floor(angle / mult + 0.5) * mult;
|
||||
dir = Vector2D(cos(angle), sin(angle)) * dir.length();
|
||||
}
|
||||
// make part
|
||||
if (!symmetry) {
|
||||
symmetry = new_intrusive<SymbolSymmetry>();
|
||||
}
|
||||
symmetry->kind = mode == ID_SYMMETRY_ROTATION ? SYMMETRY_ROTATION : SYMMETRY_REFLECTION;
|
||||
symmetry->copies = copies->GetValue();
|
||||
symmetry->center = a;
|
||||
symmetry->handle = dir;
|
||||
symmetry->name = capitalize(mode == ID_SYMMETRY_ROTATION ? _TYPE_("rotation") : _TYPE_("reflection"))
|
||||
+ String::Format(_(" (%d)"), symmetry->copies);
|
||||
void SymbolSymmetryEditor::resetActions() {
|
||||
symmetryMoveAction = nullptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user