Skip to content

Commit

Permalink
Added "zoom drag" functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-axis committed Apr 20, 2024
1 parent a879186 commit 921489a
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 80 deletions.
17 changes: 10 additions & 7 deletions assets/color_loupe.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ wheel.pivot=1
lattice=0
rail_mode=1


[tip_drag]
keys.button=2
keys.ctrl=0
Expand All @@ -42,6 +41,16 @@ chrome_margin_v=4
chrome_pad_h=10
chrome_pad_v=3

[zoom_drag]
keys.button=0
keys.ctrl=-1
keys.shift=-1
keys.alt=-1
range.distance=-1
range.timespan=0
expand_up=1
num_steps=1
len_per_step=20

[exedit_drag]
keys.button=1
Expand All @@ -57,7 +66,6 @@ wheel.pivot=1
fake_shift=0
fake_alt=0


[zoom]
wheel.enabled=1
wheel.reversed=0
Expand All @@ -66,15 +74,13 @@ wheel.pivot=0
level_min=-13
level_max=20


[color]
chrome=0x767676
back_top=0xffffff
back_bottom=0xe4ecf7
text=0x3b3d3f
blank=0xf0f0f0


[toast]
notify_scale=1
notify_follow_cursor=1
Expand All @@ -92,12 +98,10 @@ chrome_margin_v=4
chrome_pad_h=6
chrome_pad_v=4


[grid]
least_zoom_thin=8
least_zoom_thick=12


[commands]
left.click=0
left.dblclk=1
Expand All @@ -120,7 +124,6 @@ step_zoom_num_steps=1
copy_color_fmt=0
copy_coord_fmt=0


[state]
zoom_level=8
zoom_second=0
Expand Down
63 changes: 58 additions & 5 deletions color_loupe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,55 @@ static inline constinit class TipDrag : public DragState {
Settings::WheelZoom WheelZoom() override { return settings.tip_drag.wheel; }
} tip_drag;

static bool apply_zoom(int new_level, double win_ox, double win_oy);
static inline constinit class ZoomDrag : public DragState {
int revert_level{};
constexpr static auto& level = loupe_state.zoom.zoom_level;
constexpr static auto& level_min = settings.zoom.level_min;
constexpr static auto& level_max = settings.zoom.level_max;

static bool apply_zoom(int new_level) {
return ::apply_zoom(new_level, 0, 0);
}

protected:
bool Ready_core(context& cxt) override
{
if (!image.is_valid()) return false;

revert_level = level;

using namespace resources::cursor;
::SetCursor(get(sizens));
return true;
}
void Delta_core(const POINT& curr, context& cxt) override
{
constexpr auto& cfg = settings.zoom_drag;

auto diff = curr.y - drag_start.y;
if (cfg.expand_up) diff = -diff;

constexpr auto floor_div = [](int n, int d) {
auto div = std::div(n, d);
if ((div.rem ^ d) < 0) div.quot--;
return div.quot;
};
diff = floor_div(diff + cfg.len_per_step / 2, cfg.len_per_step);

cxt.redraw_loupe |= apply_zoom(revert_level + cfg.num_steps * diff);
}
void Cancel_core(context& cxt) override
{
cxt.redraw_loupe |= apply_zoom(revert_level);
}

DragInvalidRange InvalidRange() override { return settings.zoom_drag.range; }
Settings::KeysActivate KeysActivate() override { return settings.zoom_drag.keys; }
// intensionally disable.
Settings::WheelZoom WheelZoom() override { return Settings::WheelZoom{ .enabled = false }; }
} zoom_drag;

static inline constinit class ExEditDrag : public DragState {
POINT revert{}, last{};

Expand Down Expand Up @@ -1184,7 +1233,7 @@ inline void DragState::InitiateDrag(HWND hwnd, const POINT& drag_start, context&
shift = (cxt.wparam & MK_SHIFT) != 0,
alt = ::GetKeyState(VK_MENU) < 0;

constexpr DragState* drags[] = { &loupe_drag, &tip_drag, &exedit_drag };
constexpr DragState* drags[] = { &loupe_drag, &tip_drag, &zoom_drag, &exedit_drag };
void_drag.surrogate = nullptr;
for (auto* drag : drags) {
// check the button/key-combination condition.
Expand Down Expand Up @@ -1259,7 +1308,11 @@ static inline bool apply_zoom(int new_level, double win_ox, double win_oy)

// toast message.
if (!settings.toast.notify_scale) return true;
wchar_t scale[std::max(std::size(L"x 123/456"), std::max(std::size(L"x 123.45"), std::size(L"123.45%")))];
wchar_t scale[std::max({
std::size(L"x 123/456"),
std::size(L"x 123.45"),
std::size(L"123.45%"),
})];
switch (settings.toast.scale_format) {
using enum Settings::Toast::ScaleFormat;
case decimal:
Expand All @@ -1271,8 +1324,8 @@ static inline bool apply_zoom(int new_level, double win_ox, double win_oy)
break;
case fraction:
default:
auto [n, d] = loupe_state.zoom.scale_ratio_Q();
if (d == 1) std::swprintf(scale, std::size(scale), L"x %d", n);
if (auto [n, d] = loupe_state.zoom.scale_ratio_Q();
d == 1) std::swprintf(scale, std::size(scale), L"x %d", n);
else std::swprintf(scale, std::size(scale), L"x %d/%d", n, d);
break;
}
Expand Down Expand Up @@ -1845,7 +1898,7 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID lpvReserved)
// 看板.
////////////////////////////////
#define PLUGIN_NAME "色ルーペ"
#define PLUGIN_VERSION "v2.10"
#define PLUGIN_VERSION "v2.20-beta1"
#define PLUGIN_AUTHOR "sigma-axis"
#define PLUGIN_INFO_FMT(name, ver, author) (name##" "##ver##" by "##author)
#define PLUGIN_INFO PLUGIN_INFO_FMT(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
Expand Down
Binary file modified color_loupe.rc
Binary file not shown.
77 changes: 76 additions & 1 deletion dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,74 @@ class tip_drag_metrics : public dialog_base {
};


////////////////////////////////
// 拡大率ドラッグ固有.
////////////////////////////////
class zoom_drag : public dialog_base {
using ZoomDrag = Settings::ZoomDrag;

public:
ZoomDrag& drag;
zoom_drag(ZoomDrag& drag) : drag{ drag } {}

private:
void on_change_expand_up(bool data_new) { drag.expand_up = data_new; }
void on_change_num_steps(uint8_t data_new) {
drag.num_steps = std::clamp(data_new, ZoomDrag::num_steps_min, ZoomDrag::num_steps_max);
}
void on_change_len_per_step(uint16_t data_new) {
drag.len_per_step = std::clamp(data_new, ZoomDrag::len_per_step_min, ZoomDrag::len_per_step_max);
}

protected:
uintptr_t template_id() const override { return IDD_SETTINGS_FORM_DRAG_ZOOM; }

bool on_init(HWND) override
{
// suppress notifications from controls.
auto sc = suppress_callback();
::SendMessageW(::GetDlgItem(hwnd, drag.expand_up ? IDC_RADIO1 : IDC_RADIO2), BM_SETCHECK, BST_CHECKED, {});
init_spin(::GetDlgItem(hwnd, IDC_SPIN1), drag.len_per_step, drag.len_per_step_min, drag.len_per_step_max);
init_spin(::GetDlgItem(hwnd, IDC_SPIN2), drag.num_steps, drag.num_steps_min, drag.num_steps_max);

::SendMessageW(::GetDlgItem(hwnd, IDC_EDIT3), WM_SETTEXT,
{}, reinterpret_cast<LPARAM>(res_str::get(IDS_DESC_DRAG_ZOOM)));

return false;
}

intptr_t handler(UINT message, WPARAM wparam, LPARAM lparam) override
{
auto ctrl = reinterpret_cast<HWND>(lparam);
switch (message) {
case WM_COMMAND:
switch (auto id = 0xffff & wparam, code = wparam >> 16; code) {
case BN_CLICKED:
switch (id) {
case IDC_RADIO1:
case IDC_RADIO2:
on_change_expand_up(id == IDC_RADIO1);
return true;
}
break;
case EN_CHANGE:
switch (id) {
case IDC_EDIT1:
on_change_len_per_step(get_spin_value(::GetDlgItem(hwnd, IDC_SPIN1)));
return true;
case IDC_EDIT2:
on_change_num_steps(get_spin_value(::GetDlgItem(hwnd, IDC_SPIN2)));
return true;
}
break;
}
break;
}
return false;
}
};


////////////////////////////////
// 拡張編集ドラッグ固有.
////////////////////////////////
Expand Down Expand Up @@ -1802,8 +1870,8 @@ class setting_dlg : public dialog_base {

drag_move_loupe,
drag_show_tip,
drag_zoom,
drag_exedit,
//drag_zoom,

wheel_zoom,

Expand All @@ -1817,6 +1885,7 @@ class setting_dlg : public dialog_base {
constexpr static CtrlData<tab_kind> headers[] = {
{ IDS_DLG_TAB_DRAG_LOUPE, tab_kind::drag_move_loupe },
{ IDS_DLG_TAB_DRAG_TIP, tab_kind::drag_show_tip },
{ IDS_DLG_TAB_DRAG_ZOOM, tab_kind::drag_zoom },
{ IDS_DLG_TAB_DRAG_EXEDIT, tab_kind::drag_exedit },
{ IDS_DLG_TAB_SEPARATOR, tab_kind::separator },
{ IDS_DLG_TAB_CLK_LEFT, tab_kind::action_left },
Expand Down Expand Up @@ -1858,6 +1927,12 @@ class setting_dlg : public dialog_base {
},
new tip_drag_format{ curr.tip_drag, update_target },
};
case tab_kind::drag_zoom:
return new vscroll_form{
new drag_keys{ curr.zoom_drag.keys },
new drag_range{ curr.zoom_drag.range },
new zoom_drag{ curr.zoom_drag },
};
case tab_kind::drag_exedit:
return new vscroll_form{
new drag_keys{ curr.exedit_drag.keys },
Expand Down
2 changes: 1 addition & 1 deletion dialogs_basics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace dialogs::basics
ret = that->handler(message, wparam, lparam);
if (message == WM_NCDESTROY) that->hwnd = nullptr;
}

return ret;
}
bool no_callback = false;
Expand Down
Loading

0 comments on commit 921489a

Please sign in to comment.