Skip to content

Commit

Permalink
jx_layout_editor: drag to resize
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed Jan 5, 2024
1 parent 02342fb commit 0e3a20b
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 98 deletions.
8 changes: 2 additions & 6 deletions todo-jxlayout
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
resize
shift key to turn off grid
accumulate into single undo

-----

create widget by cmd-dragging rectangle
after release, select widget type and then create it - sets correct container

Expand Down Expand Up @@ -42,3 +36,5 @@ control tab order
show index in each item
cmd-click to select item without re-ordering
click to make object the next in list

when show/hide toolbar, change window size to preserve LayoutContainer size
135 changes: 122 additions & 13 deletions tools/jx_layout_editor/code/BaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <jx-af/jx/jXPainterUtil.h>
#include <jx-af/jx/JXMenu.h>
#include <jx-af/jx/JXDisplay.h>
#include <jx-af/jcore/JUndoRedoChain.h>
#include <jx-af/jcore/JColorManager.h>
#include <jx-af/jcore/jMouseUtil.h>
#include <X11/cursorfont.h>
Expand Down Expand Up @@ -260,10 +261,21 @@ BaseWidget::HandleMouseDown
const JXKeyModifiers& modifiers
)
{
itsStartPt = itsPrevPt = pt;
itsLastClickCount = 0;
itsWaitingForDragFlag = false;
itsClearIfNotDNDFlag = false;
itsStartPtG = itsPrevPtG = LocalToGlobal(pt);
itsLastClickCount = 0;
itsWaitingForDragFlag = false;
itsClearIfNotDNDFlag = false;
itsIsResizingFlag = false;

itsResizeDragType = 0;
for (const auto& r : itsHandles)
{
if (r.Contains(pt))
{
return;
}
itsResizeDragType++;
}

if (button == kJXLeftButton && clickCount == 1 && modifiers.shift())
{
Expand Down Expand Up @@ -315,21 +327,116 @@ BaseWidget::HandleMouseDrag
const JXKeyModifiers& modifiers
)
{
if (itsWaitingForDragFlag)
const JPoint ptG = LocalToGlobal(pt);
const bool moved = JMouseMoved(itsStartPtG, ptG);

if (itsWaitingForDragFlag && moved)
{
assert( itsLayout->HasSelection() );

if (JMouseMoved(itsStartPt, pt))
{
itsWaitingForDragFlag = false;
itsClearIfNotDNDFlag = false;
itsWaitingForDragFlag = false;
itsClearIfNotDNDFlag = false;

itsLayout->SetSelectedWidgetsVisible(false);
itsLayout->SetSelectedWidgetsVisible(false);

auto* data = jnew LayoutSelection(itsLayout, LocalToGlobal(itsStartPt));
BeginDND(pt, buttonStates, modifiers, data);
}
auto* data = jnew LayoutSelection(itsLayout, itsStartPtG);
BeginDND(pt, buttonStates, modifiers, data);
return;
}
else if (!itsIsResizingFlag && itsResizeDragType < kHandleCount && moved)
{
itsIsResizingFlag = true;
}
else if (!itsIsResizingFlag)
{
return;
}

JPoint gridPtG =
itsLayout->LocalToGlobal(
itsLayout->SnapToGrid(
itsLayout->GlobalToLocal(ptG)));

JPoint delta = gridPtG - itsPrevPtG;
if (delta.x == 0 && delta.y == 0)
{
return;
}

LayoutUndo* undo = nullptr;
if (!itsLayout->CurrentUndoIs(LayoutUndo::kDragResizeType))
{
undo = jnew LayoutUndo(itsLayout->GetDocument(), LayoutUndo::kDragResizeType);
}

if (itsResizeDragType == kTopLeftHandle)
{
Move(delta.x, delta.y);
AdjustSize(-delta.x, -delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(-delta.x, -delta.y);
}
else if (itsResizeDragType == kTopHandle)
{
Move(0, delta.y);
AdjustSize(0, -delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(0, -delta.y);
}
else if (itsResizeDragType == kTopRightHandle)
{
Move(0, delta.y);
AdjustSize(delta.x, -delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(delta.x, -delta.y);
}
else if (itsResizeDragType == kRightHandle)
{
AdjustSize(delta.x, 0);

delta = itsLayout->SnapToGrid(this);
AdjustSize(delta.x, 0);
}
else if (itsResizeDragType == kBottomRightHandle)
{
AdjustSize(delta.x, delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(delta.x, delta.y);
}
else if (itsResizeDragType == kBottomHandle)
{
AdjustSize(0, delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(0, delta.y);
}
else if (itsResizeDragType == kBottomLeftHandle)
{
Move(delta.x, 0);
AdjustSize(-delta.x, delta.y);

delta = itsLayout->SnapToGrid(this);
AdjustSize(-delta.x, delta.y);
}
else if (itsResizeDragType == kLeftHandle)
{
Move(delta.x, 0);
AdjustSize(-delta.x, 0);

delta = itsLayout->SnapToGrid(this);
AdjustSize(-delta.x, 0);
}

if (undo != nullptr)
{
itsLayout->NewUndo(undo);
}

itsPrevPtG = gridPtG;
}

/******************************************************************************
Expand All @@ -346,6 +453,8 @@ BaseWidget::HandleMouseUp
const JXKeyModifiers& modifiers
)
{
itsLayout->GetUndoRedoChain()->DeactivateCurrentUndo();

if (itsWaitingForDragFlag && itsLastClickCount == 2)
{
// TODO: edit parameters
Expand Down
8 changes: 5 additions & 3 deletions tools/jx_layout_editor/code/BaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ class BaseWidget : public JXWidget

// used during dragging

JPoint itsStartPt;
JPoint itsPrevPt;
JPoint itsStartPtG;
JPoint itsPrevPtG;
JSize itsLastClickCount;
bool itsWaitingForDragFlag;
bool itsClearIfNotDNDFlag;

// used during resize drag

bool itsIsResizingFlag;
JIndex itsResizeDragType;
JRect itsHandles[ kHandleCount ];
JCursorIndex itsCursors[ kHandleCount ];

Expand Down Expand Up @@ -163,7 +165,7 @@ inline JPoint
BaseWidget::GetDragStartPointGlobal()
const
{
return LocalToGlobal(itsStartPt);
return itsStartPtG;
}

#endif
2 changes: 1 addition & 1 deletion tools/jx_layout_editor/code/CustomWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ CustomWidget::Draw
const JRect& rect
)
{
p.String(rect, itsClassName, JPainter::HAlign::kCenter, JPainter::VAlign::kCenter);
p.String(GetFrameLocal(), itsClassName, JPainter::HAlign::kCenter, JPainter::VAlign::kCenter);
}

/******************************************************************************
Expand Down
10 changes: 5 additions & 5 deletions tools/jx_layout_editor/code/LayoutContainer-Arrange.jxm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jx_menu_editor 0
912 351
850 351
"LayoutContainer"
"Arrange"
'a
Expand All @@ -8,8 +8,8 @@ jx_menu_editor 0
200
100
40
200
200
162
163
75

0
Expand Down Expand Up @@ -98,7 +98,7 @@ jx_menu_editor 0
0 "expand_horiz.png"
"Fit horizontally"
""
'
'o
"ExpandHorizontally"
"kExpandHorizCmd"
0
Expand All @@ -108,7 +108,7 @@ jx_menu_editor 0
0 "expand_vert.png"
"Fit vertically"
""
'
'e
"ExpandVertically"
"kExpandVertCmd"
0
Expand Down
Loading

0 comments on commit 0e3a20b

Please sign in to comment.