Skip to content

Commit 93abeff

Browse files
committed
Refactor pointer follower feature following review
- Moved logic to ViewModel - Removed 'OnShiftClick' function in ControlBinding and simply add a keystate check on the 'OnClick' one - Removed unecessary mask - Unit Test added
1 parent 8998858 commit 93abeff

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

src/ui/viewmodels/MemoryViewerViewModel.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,18 @@ void MemoryViewerViewModel::OnClick(int nX, int nY)
896896
}
897897
}
898898

899-
void MemoryViewerViewModel::OnShiftClick(ra::ByteAddress nAddress)
899+
void MemoryViewerViewModel::OnShiftClick(int nX, int nY)
900900
{
901+
const auto& pEmulatorContext = ra::services::ServiceLocator::Get<ra::data::context::EmulatorContext>();
901902
const auto& pConsoleContext = ra::services::ServiceLocator::Get<ra::data::context::ConsoleContext>();
903+
904+
OnClick(nX, nY);
905+
906+
ra::ByteAddress nAddress = pEmulatorContext.ReadMemory(GetAddress(), GetSize());
902907
const auto nConvertedAddress = pConsoleContext.ByteAddressFromRealAddress(nAddress);
903908
if (nConvertedAddress != 0xFFFFFFFF)
904909
nAddress = nConvertedAddress;
910+
905911
SetAddress(nAddress);
906912
}
907913

src/ui/viewmodels/MemoryViewerViewModel.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public:
161161
void SetSize(MemSize value) { SetValue(SizeProperty, ra::etoi(value)); }
162162

163163
void OnClick(int nX, int nY);
164-
void OnShiftClick(ByteAddress address);
164+
void OnShiftClick(int nX, int nY);
165165
void OnResized(int nWidth, int nHeight);
166166
bool OnChar(char c);
167167
void OnGotFocus();

src/ui/win32/bindings/MemoryViewerControlBinding.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ constexpr UINT WM_USER_INVALIDATE = WM_USER + 1;
1616

1717
INT_PTR CALLBACK MemoryViewerControlBinding::WndProc(HWND hControl, UINT uMsg, WPARAM wParam, LPARAM lParam)
1818
{
19-
const bool bShiftHeld = (GetKeyState(VK_SHIFT) < 0);
2019
switch (uMsg)
2120
{
2221
case WM_PAINT:
@@ -35,10 +34,7 @@ INT_PTR CALLBACK MemoryViewerControlBinding::WndProc(HWND hControl, UINT uMsg, W
3534
return FALSE;
3635

3736
case WM_LBUTTONUP:
38-
if (bShiftHeld)
39-
OnShiftClick({GET_X_LPARAM(lParam) - MEMVIEW_MARGIN, GET_Y_LPARAM(lParam) - MEMVIEW_MARGIN});
40-
else
41-
OnClick({ GET_X_LPARAM(lParam) - MEMVIEW_MARGIN, GET_Y_LPARAM(lParam) - MEMVIEW_MARGIN });
37+
OnClick({ GET_X_LPARAM(lParam) - MEMVIEW_MARGIN, GET_Y_LPARAM(lParam) - MEMVIEW_MARGIN });
4238
return FALSE;
4339

4440
case WM_KEYDOWN:
@@ -260,23 +256,17 @@ void MemoryViewerControlBinding::OnClick(POINT point)
260256

261257
// multiple properties may change while typing, we'll do a single Invalidate after we're done
262258
m_bSuppressMemoryViewerInvalidate = true;
263-
m_pViewModel.OnClick(point.x, point.y);
259+
if (GetKeyState(VK_SHIFT) < 0)
260+
m_pViewModel.OnShiftClick(point.x, point.y);
261+
else
262+
m_pViewModel.OnClick(point.x, point.y);
264263
m_bSuppressMemoryViewerInvalidate = false;
265264

266265
SetFocus(m_hWnd);
267266

268267
Invalidate();
269268
}
270269

271-
void MemoryViewerControlBinding::OnShiftClick(POINT point)
272-
{
273-
OnClick(point);
274-
const auto& pEmulatorContext = ra::services::ServiceLocator::Get<ra::data::context::EmulatorContext>();
275-
ra::ByteAddress nAddress =
276-
pEmulatorContext.ReadMemory(m_pViewModel.GetAddress(), m_pViewModel.GetSize()) & 0xFFFFFF;
277-
m_pViewModel.OnShiftClick(nAddress);
278-
}
279-
280270
void MemoryViewerControlBinding::OnGotFocus()
281271
{
282272
m_pViewModel.OnGotFocus();

src/ui/win32/bindings/MemoryViewerControlBinding.hh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public:
4040
void ScrollDown();
4141

4242
void OnClick(POINT point);
43-
void OnShiftClick(POINT point);
4443

4544
bool OnKeyDown(UINT nChar);
4645
bool OnEditInput(UINT c);

tests/ui/viewmodels/MemoryViewerViewModel_Tests.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "tests\RA_UnitTestHelpers.h"
88
#include "tests\mocks\MockEmulatorContext.hh"
99
#include "tests\mocks\MockGameContext.hh"
10+
#include "tests\mocks\MockConsoleContext.hh"
1011
#include "tests\mocks\MockWindowManager.hh"
1112

1213
#undef GetMessage
@@ -1918,8 +1919,66 @@ TEST_CLASS(MemoryViewerViewModel_Tests)
19181919
Assert::IsTrue(viewer.NeedsRedraw());
19191920
viewer.MockRender();
19201921
}
1922+
1923+
TEST_METHOD(TestOnShiftClickEightBit)
1924+
{
1925+
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"Playstation");
1926+
1927+
MemoryViewerViewModelHarness viewer;
1928+
viewer.InitializeMemory(128); // 8 rows of 16 bytes
1929+
viewer.mockEmulatorContext.WriteMemoryByte(0U, 0x20);
1930+
viewer.mockEmulatorContext.WriteMemoryByte(1U, 0xff);
1931+
viewer.mockEmulatorContext.WriteMemoryByte(2U, 0x0);
1932+
1933+
Assert::AreEqual(MemSize::EightBit, viewer.GetSize());
1934+
Assert::AreEqual({ 0U }, viewer.GetAddress());
1935+
Assert::AreEqual({ 0U }, viewer.GetSelectedNibble());
1936+
1937+
// Shift click on the first byte containing 0x20 should lead to address 0x20
1938+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1939+
Assert::AreEqual({ 0x20U }, viewer.GetAddress());
1940+
1941+
// Shift click on the second byte containing 0xFF should lead to address 0x7F as 0xFF is bigger than the last
1942+
// address in memory
1943+
viewer.OnShiftClick(13 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1944+
Assert::AreEqual({0x7FU}, viewer.GetAddress());
1945+
1946+
// Shift click on the third byte containing 0x0 should lead back to address 0x0
1947+
viewer.OnShiftClick(16 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1948+
Assert::AreEqual({0x0U}, viewer.GetAddress());
1949+
}
1950+
1951+
TEST_METHOD(TestOnShiftClickSixTeenBit)
1952+
{
1953+
ra::data::context::mocks::MockConsoleContext mockConsole(PlayStation, L"Playstation");
1954+
1955+
MemoryViewerViewModelHarness viewer;
1956+
viewer.InitializeMemory(512); // 32 rows of 16 bytes
1957+
viewer.SetSize(MemSize::SixteenBit);
1958+
viewer.mockEmulatorContext.WriteMemory(0U, MemSize::SixteenBit, 0x20);
1959+
viewer.mockEmulatorContext.WriteMemory(2U, MemSize::SixteenBit, 0xff);
1960+
viewer.mockEmulatorContext.WriteMemory(4U, MemSize::SixteenBit, 0xffff);
1961+
1962+
Assert::AreEqual(MemSize::SixteenBit, viewer.GetSize());
1963+
Assert::AreEqual({0U}, viewer.GetAddress());
1964+
Assert::AreEqual({0U}, viewer.GetSelectedNibble());
1965+
1966+
// Shift click on the first word containing 0x20 should lead to address 0x20
1967+
viewer.OnShiftClick(10 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1968+
Assert::AreEqual({0x20U}, viewer.GetAddress());
1969+
1970+
// Shift click on the second word containing 0x40 should lead to address 0xFF
1971+
viewer.OnShiftClick(15 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1972+
Assert::AreEqual({0xFFU}, viewer.GetAddress());
1973+
1974+
// Shift click on the third word containing 0xFFFF should lead to address 0x1FF as 0xFFFF is bigger than the
1975+
// last address in memory
1976+
viewer.OnShiftClick(19 * CHAR_WIDTH, CHAR_HEIGHT + 4);
1977+
Assert::AreEqual({0x1FFU}, viewer.GetAddress());
1978+
}
19211979
};
19221980

1981+
19231982
} // namespace tests
19241983
} // namespace viewmodels
19251984
} // namespace ui

0 commit comments

Comments
 (0)