Skip to content

Commit eefd38c

Browse files
committed
align mouse button semantics to DF
we, um, had it backwards
1 parent 8313737 commit eefd38c

17 files changed

Lines changed: 110 additions & 113 deletions

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Template for new versions:
6666
## API
6767

6868
## Lua
69+
- mouse key events are now aligned with internal DF semantics: ``_MOUSE_L`` indicates that the left mouse button has just been pressed and ``_MOUSE_L_DOWN`` indicates that the left mouse button is being held down. similar for ``_MOUSE_R`` and ``_MOUSE_M``. 3rd party scripts may have to adjust.
6970

7071
## Removed
7172

docs/dev/Lua API.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,10 +2528,10 @@ Supported callbacks and fields are:
25282528
Maps to an integer in range 0-255. Duplicates a separate "STRING_A???" code for convenience.
25292529

25302530
``_MOUSE_L, _MOUSE_R, _MOUSE_M``
2531-
If the left, right, and/or middle mouse button is being pressed.
2531+
If the left, right, and/or middle mouse button was just pressed.
25322532

25332533
``_MOUSE_L_DOWN, _MOUSE_R_DOWN, _MOUSE_M_DOWN``
2534-
If the left, right, and/or middle mouse button was just pressed.
2534+
If the left, right, and/or middle mouse button is being held down.
25352535

25362536
If this method is omitted, the screen is dismissed on reception of the ``LEAVESCREEN`` key.
25372537

library/LuaTools.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ void DFHack::Lua::GetVector(lua_State *state, std::vector<std::string> &pvec, in
131131
}
132132
}
133133

134-
static bool trigger_inhibit_l_down = false;
135-
static bool trigger_inhibit_r_down = false;
136-
static bool trigger_inhibit_m_down = false;
137-
static bool inhibit_l_down = false;
138-
static bool inhibit_r_down = false;
139-
static bool inhibit_m_down = false;
134+
static bool trigger_inhibit_l = false;
135+
static bool trigger_inhibit_r = false;
136+
static bool trigger_inhibit_m = false;
137+
static bool inhibit_l = false;
138+
static bool inhibit_r = false;
139+
static bool inhibit_m = false;
140140

141141
void DFHack::Lua::PushInterfaceKeys(lua_State *L,
142142
const std::set<df::interface_key> &keys) {
@@ -161,32 +161,32 @@ void DFHack::Lua::PushInterfaceKeys(lua_State *L,
161161
}
162162

163163
if (df::global::enabler) {
164-
if (!inhibit_l_down && df::global::enabler->mouse_lbut_down) {
164+
if (!inhibit_l && df::global::enabler->mouse_lbut) {
165165
lua_pushboolean(L, true);
166-
lua_setfield(L, -2, "_MOUSE_L_DOWN");
167-
trigger_inhibit_l_down = true;
166+
lua_setfield(L, -2, "_MOUSE_L");
167+
trigger_inhibit_l = true;
168168
}
169-
if (!inhibit_r_down && df::global::enabler->mouse_rbut_down) {
169+
if (!inhibit_r && df::global::enabler->mouse_rbut) {
170170
lua_pushboolean(L, true);
171-
lua_setfield(L, -2, "_MOUSE_R_DOWN");
172-
trigger_inhibit_r_down = true;
171+
lua_setfield(L, -2, "_MOUSE_R");
172+
trigger_inhibit_r = true;
173173
}
174-
if (!inhibit_m_down && df::global::enabler->mouse_mbut_down) {
174+
if (!inhibit_m && df::global::enabler->mouse_mbut) {
175175
lua_pushboolean(L, true);
176-
lua_setfield(L, -2, "_MOUSE_M_DOWN");
177-
trigger_inhibit_m_down = true;
176+
lua_setfield(L, -2, "_MOUSE_M");
177+
trigger_inhibit_m = true;
178178
}
179-
if (df::global::enabler->mouse_lbut) {
179+
if (df::global::enabler->mouse_lbut_down) {
180180
lua_pushboolean(L, true);
181-
lua_setfield(L, -2, "_MOUSE_L");
181+
lua_setfield(L, -2, "_MOUSE_L_DOWN");
182182
}
183-
if (df::global::enabler->mouse_rbut) {
183+
if (df::global::enabler->mouse_rbut_down) {
184184
lua_pushboolean(L, true);
185-
lua_setfield(L, -2, "_MOUSE_R");
185+
lua_setfield(L, -2, "_MOUSE_R_DOWN");
186186
}
187-
if (df::global::enabler->mouse_mbut) {
187+
if (df::global::enabler->mouse_mbut_down) {
188188
lua_pushboolean(L, true);
189-
lua_setfield(L, -2, "_MOUSE_M");
189+
lua_setfield(L, -2, "_MOUSE_M_DOWN");
190190
}
191191
}
192192
}
@@ -2159,25 +2159,25 @@ void DFHack::Lua::Core::Reset(color_ostream &out, const char *where)
21592159
lua_settop(State, 0);
21602160
}
21612161

2162-
if (trigger_inhibit_l_down) {
2163-
trigger_inhibit_l_down = false;
2164-
inhibit_l_down = true;
2162+
if (trigger_inhibit_l) {
2163+
trigger_inhibit_l = false;
2164+
inhibit_l = true;
21652165
}
2166-
if (trigger_inhibit_r_down) {
2167-
trigger_inhibit_r_down = false;
2168-
inhibit_r_down = true;
2166+
if (trigger_inhibit_r) {
2167+
trigger_inhibit_r = false;
2168+
inhibit_r = true;
21692169
}
2170-
if (trigger_inhibit_m_down) {
2171-
trigger_inhibit_m_down = false;
2172-
inhibit_m_down = true;
2170+
if (trigger_inhibit_m) {
2171+
trigger_inhibit_m = false;
2172+
inhibit_m = true;
21732173
}
21742174

21752175
if (df::global::enabler) {
2176-
if (!df::global::enabler->mouse_lbut)
2177-
inhibit_l_down = false;
2178-
if (!df::global::enabler->mouse_rbut)
2179-
inhibit_r_down = false;
2180-
if (!df::global::enabler->mouse_mbut)
2181-
inhibit_m_down = false;
2176+
if (!df::global::enabler->mouse_lbut_down)
2177+
inhibit_l = false;
2178+
if (!df::global::enabler->mouse_rbut_down)
2179+
inhibit_r = false;
2180+
if (!df::global::enabler->mouse_mbut_down)
2181+
inhibit_m = false;
21822182
}
21832183
}

library/lua/gui.lua

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -696,16 +696,12 @@ end
696696

697697
DEFAULT_INITIAL_PAUSE = true
698698

699-
local zscreen_inhibit_mouse_l = false
700-
701699
-- ensure underlying DF screens don't also react to handled clicks
702700
function markMouseClicksHandled(keys)
703-
if keys._MOUSE_L_DOWN then
704-
-- note we can't clear mouse_lbut here. otherwise we break dragging,
705-
df.global.enabler.mouse_lbut_down = 0
706-
zscreen_inhibit_mouse_l = true
701+
if keys._MOUSE_L then
702+
df.global.enabler.mouse_lbut = 0
707703
end
708-
if keys._MOUSE_R_DOWN then
704+
if keys._MOUSE_R then
709705
df.global.enabler.mouse_rbut_down = 0
710706
df.global.enabler.mouse_rbut = 0
711707
end
@@ -789,7 +785,7 @@ function ZScreen:onInput(keys)
789785
local has_mouse = self:isMouseOver()
790786
if not self:hasFocus() then
791787
if has_mouse and
792-
(keys._MOUSE_L_DOWN or keys._MOUSE_R_DOWN or
788+
(keys._MOUSE_L or keys._MOUSE_R or
793789
keys.CONTEXT_SCROLL_UP or keys.CONTEXT_SCROLL_DOWN or
794790
keys.CONTEXT_SCROLL_PAGEUP or keys.CONTEXT_SCROLL_PAGEDOWN) then
795791
self:raise()
@@ -804,22 +800,15 @@ function ZScreen:onInput(keys)
804800
return
805801
end
806802

807-
if self.pass_mouse_clicks and keys._MOUSE_L_DOWN and not has_mouse then
803+
if self.pass_mouse_clicks and keys._MOUSE_L and not has_mouse then
808804
self.defocused = self.defocusable
809805
self:sendInputToParent(keys)
810806
return
811-
elseif keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
807+
elseif keys.LEAVESCREEN or keys._MOUSE_R then
812808
self:dismiss()
813809
markMouseClicksHandled(keys)
814810
return
815811
else
816-
if zscreen_inhibit_mouse_l then
817-
if keys._MOUSE_L then
818-
return
819-
else
820-
zscreen_inhibit_mouse_l = false
821-
end
822-
end
823812
local passit = self.pass_pause and keys.D_PAUSE
824813
if not passit and self.pass_mouse_clicks then
825814
if keys.CONTEXT_SCROLL_UP or keys.CONTEXT_SCROLL_DOWN or

library/lua/gui/dialogs.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ function MessageBox:onDestroy()
5757
end
5858

5959
function MessageBox:onInput(keys)
60-
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
60+
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R then
6161
self:dismiss()
6262
if keys.SELECT and self.on_accept then
6363
self.on_accept()
64-
elseif (keys.LEAVESCREEN or keys._MOUSE_R_DOWN) and self.on_cancel then
64+
elseif (keys.LEAVESCREEN or keys._MOUSE_R) and self.on_cancel then
6565
self.on_cancel()
6666
end
6767
gui.markMouseClicksHandled(keys)
@@ -129,7 +129,7 @@ function InputBox:onInput(keys)
129129
self.on_input(self.subviews.edit.text)
130130
end
131131
return true
132-
elseif keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
132+
elseif keys.LEAVESCREEN or keys._MOUSE_R then
133133
self:dismiss()
134134
if self.on_cancel then
135135
self.on_cancel()
@@ -231,7 +231,7 @@ function ListBox:getWantedFrameSize()
231231
end
232232

233233
function ListBox:onInput(keys)
234-
if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
234+
if keys.LEAVESCREEN or keys._MOUSE_R then
235235
self:dismiss()
236236
if self.on_cancel then
237237
self.on_cancel()

library/lua/gui/widgets.lua

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,14 @@ end
273273

274274
function Panel:onInput(keys)
275275
if self.kbd_get_pos then
276-
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
276+
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R then
277277
Panel_end_drag(self, not keys.SELECT and self.saved_frame or nil,
278278
not not keys.SELECT)
279279
return true
280280
end
281281
for code in pairs(keys) do
282282
local dx, dy = guidm.get_movement_delta(code, 1, 10)
283283
if dx then
284-
local frame_rect = self.frame_rect
285284
local kbd_pos = self.kbd_get_pos()
286285
kbd_pos.x = kbd_pos.x + dx
287286
kbd_pos.y = kbd_pos.y + dy
@@ -292,17 +291,17 @@ function Panel:onInput(keys)
292291
return
293292
end
294293
if self.drag_offset then
295-
if keys._MOUSE_R_DOWN then
294+
if keys._MOUSE_R then
296295
Panel_end_drag(self, self.saved_frame)
297-
elseif keys._MOUSE_L then
296+
elseif keys._MOUSE_L_DOWN then
298297
Panel_update_frame(self, Panel_make_frame(self))
299298
end
300299
return true
301300
end
302301
if Panel.super.onInput(self, keys) then
303302
return true
304303
end
305-
if not keys._MOUSE_L_DOWN then return end
304+
if not keys._MOUSE_L then return end
306305
local x,y = self:getMouseFramePos()
307306
if not x then return end
308307

@@ -489,7 +488,7 @@ function Panel:onRenderFrame(dc, rect)
489488
dc:seek(pos.x, pos.y):pen(pen):char(string.char(0xDB))
490489
end
491490
if self.drag_offset and not self.kbd_get_pos
492-
and df.global.enabler.mouse_lbut == 0 then
491+
and df.global.enabler.mouse_lbut_down == 0 then
493492
Panel_end_drag(self, nil, true)
494493
end
495494
end
@@ -718,7 +717,7 @@ function EditField:onInput(keys)
718717
end
719718
end
720719

721-
if self.key and (keys.LEAVESCREEN or keys._MOUSE_R_DOWN) then
720+
if self.key and (keys.LEAVESCREEN or keys._MOUSE_R) then
722721
self:setText(self.saved_text)
723722
self:setFocus(false)
724723
return true
@@ -740,8 +739,8 @@ function EditField:onInput(keys)
740739
end
741740
end
742741
return not not self.key
743-
elseif keys._MOUSE_L then
744-
local mouse_x, mouse_y = self:getMousePos()
742+
elseif keys._MOUSE_L_DOWN then
743+
local mouse_x = self:getMousePos()
745744
if mouse_x then
746745
self:setCursor(self.start_pos + mouse_x - (self.text_offset or 0))
747746
return true
@@ -986,7 +985,7 @@ function Scrollbar:onRenderBody(dc)
986985
if self.is_dragging then
987986
scrollbar_do_drag(self)
988987
end
989-
if df.global.enabler.mouse_lbut == 0 then
988+
if df.global.enabler.mouse_lbut_down == 0 then
990989
self.last_scroll_ms = 0
991990
self.is_dragging = false
992991
self.scroll_spec = nil
@@ -1023,7 +1022,7 @@ function Scrollbar:onInput(keys)
10231022
return true
10241023
end
10251024
end
1026-
if not keys._MOUSE_L_DOWN then return false end
1025+
if not keys._MOUSE_L then return false end
10271026
local _,y = self:getMousePos()
10281027
if not y then return false end
10291028
local scroll_spec = nil
@@ -1386,11 +1385,11 @@ function Label:onInput(keys)
13861385
if self:inputToSubviews(keys) then
13871386
return true
13881387
end
1389-
if keys._MOUSE_L_DOWN and self:getMousePos() and self.on_click then
1388+
if keys._MOUSE_L and self:getMousePos() and self.on_click then
13901389
self.on_click()
13911390
return true
13921391
end
1393-
if keys._MOUSE_R_DOWN and self:getMousePos() and self.on_rclick then
1392+
if keys._MOUSE_R and self:getMousePos() and self.on_rclick then
13941393
self.on_rclick()
13951394
return true
13961395
end
@@ -1498,7 +1497,7 @@ end
14981497
function HotkeyLabel:onInput(keys)
14991498
if HotkeyLabel.super.onInput(self, keys) then
15001499
return true
1501-
elseif keys._MOUSE_L_DOWN and self:getMousePos() and self.on_activate
1500+
elseif keys._MOUSE_L and self:getMousePos() and self.on_activate
15021501
and not is_disabled(self) then
15031502
self.on_activate()
15041503
return true
@@ -1658,7 +1657,7 @@ end
16581657
function CycleHotkeyLabel:onInput(keys)
16591658
if CycleHotkeyLabel.super.onInput(self, keys) then
16601659
return true
1661-
elseif keys._MOUSE_L_DOWN and self:getMousePos() and not is_disabled(self) then
1660+
elseif keys._MOUSE_L and self:getMousePos() and not is_disabled(self) then
16621661
self:cycle()
16631662
return true
16641663
end
@@ -1962,7 +1961,7 @@ function List:onInput(keys)
19621961
return self:submit()
19631962
elseif keys.CUSTOM_SHIFT_ENTER then
19641963
return self:submit2()
1965-
elseif keys._MOUSE_L_DOWN then
1964+
elseif keys._MOUSE_L then
19661965
local idx = self:getIdxUnderMouse()
19671966
if idx then
19681967
local now_ms = dfhack.getTickCount()
@@ -2317,7 +2316,7 @@ end
23172316

23182317
function Tab:onInput(keys)
23192318
if Tab.super.onInput(self, keys) then return true end
2320-
if keys._MOUSE_L_DOWN and self:getMousePos() then
2319+
if keys._MOUSE_L and self:getMousePos() then
23212320
self.on_select(self.id)
23222321
return true
23232322
end
@@ -2419,7 +2418,7 @@ local function rangeslider_get_width_per_idx(self)
24192418
end
24202419

24212420
function RangeSlider:onInput(keys)
2422-
if not keys._MOUSE_L_DOWN then return false end
2421+
if not keys._MOUSE_L then return false end
24232422
local x = self:getMousePos()
24242423
if not x then return false end
24252424
local left_idx, right_idx = self.get_left_idx_fn(), self.get_right_idx_fn()
@@ -2527,7 +2526,7 @@ function RangeSlider:onRenderBody(dc, rect)
25272526
if self.is_dragging_target then
25282527
rangeslider_do_drag(self, width_per_idx)
25292528
end
2530-
if df.global.enabler.mouse_lbut == 0 then
2529+
if df.global.enabler.mouse_lbut_down == 0 then
25312530
self.is_dragging_target = nil
25322531
self.is_dragging_idx = nil
25332532
end

library/modules/Screen.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,23 @@ dfhack_lua_viewscreen::~dfhack_lua_viewscreen()
10041004

10051005
void dfhack_lua_viewscreen::render()
10061006
{
1007+
using df::global::enabler;
1008+
10071009
if (Screen::isDismissed(this))
10081010
{
10091011
if (parent)
10101012
parent->render();
10111013
return;
10121014
}
10131015

1016+
if (enabler &&
1017+
(enabler->mouse_lbut_down || enabler->mouse_rbut_down || enabler->mouse_mbut_down))
1018+
{
1019+
// synthesize feed events for held mouse buttons
1020+
std::set<df::interface_key> keys;
1021+
feed(&keys);
1022+
}
1023+
10141024
dfhack_viewscreen::render();
10151025

10161026
safe_call_lua(do_render, 0, 0);

0 commit comments

Comments
 (0)