Skip to content

Commit

Permalink
Code review. Simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterCouto committed Nov 13, 2021
1 parent 76a773b commit 0926f61
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 110 deletions.
80 changes: 12 additions & 68 deletions source/D2ItemsForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ void CD2EquippedItemStatic::SetUseAltImage(BOOL flag)
BEGIN_MESSAGE_MAP(CD2EquippedItemStatic, CStatic)
//{{AFX_MSG_MAP(CD2EquippedItemStatic)
ON_WM_ERASEBKGND()
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, &CD2EquippedItemStatic::OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, &CD2EquippedItemStatic::OnToolTipText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Expand Down Expand Up @@ -469,33 +467,6 @@ BOOL CD2EquippedItemStatic::OnEraseBkgnd(CDC* pDC)
return TRUE;
}
//---------------------------------------------------------------------------
BOOL CD2EquippedItemStatic::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
CString strTipText;
UINT_PTR nID = pNMHDR->idFrom;

CRect rect;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
::GetWindowRect((HWND)nID, &rect);
ScreenToClient(&rect);
nID = ((UINT_PTR)(DWORD)::GetDlgCtrlID((HWND)nID));
}

*pResult = 0;
if (nID != (UINT_PTR)GetDlgCtrlID())
{
return FALSE;
}

return FALSE;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// CD2ItemsGridStatic
Expand Down Expand Up @@ -563,8 +534,6 @@ INT_PTR CD2ItemsGridStatic::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
BEGIN_MESSAGE_MAP(CD2ItemsGridStatic, CStatic)
//{{AFX_MSG_MAP(CD2ItemsGridStatic)
ON_WM_ERASEBKGND()
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, &CD2ItemsGridStatic::OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, &CD2ItemsGridStatic::OnToolTipText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Expand Down Expand Up @@ -854,33 +823,6 @@ BOOL CD2ItemsGridStatic::OnEraseBkgnd(CDC* pDC)
return TRUE;
}
//---------------------------------------------------------------------------
BOOL CD2ItemsGridStatic::OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
CString strTipText;
UINT_PTR nID = pNMHDR->idFrom;

CRect rect;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
::GetWindowRect((HWND)nID, &rect);
ScreenToClient(&rect);
nID = ((UINT_PTR)(DWORD)::GetDlgCtrlID((HWND)nID));
}

*pResult = 0;
if (nID != (UINT_PTR)GetDlgCtrlID())
{
return FALSE;
}

return FALSE;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// CD2ItemsForm dialog
Expand Down Expand Up @@ -1720,17 +1662,19 @@ void CD2ItemsForm::OnBnClickedOk()
const d2ce::Item* CD2ItemsForm::InvHitTest(CPoint point, TOOLINFO* pTI) const
{
INT_PTR nHit = __super::OnToolHitTest(point, pTI);
if (nHit != -1)
if (nHit == -1)
{
switch (nHit)
{
case IDC_CORPSE_GROUP:
case IDC_MERC_GROUP:
case IDC_GOLEM_GROUP:
case IDC_INV_CUBE_GROUP:
case IDC_INV_BELT_GROUP:
return nullptr;
}
return nullptr;
}

switch (nHit)
{
case IDC_CORPSE_GROUP:
case IDC_MERC_GROUP:
case IDC_GOLEM_GROUP:
case IDC_INV_CUBE_GROUP:
case IDC_INV_BELT_GROUP:
return nullptr;
}

// Make sure we have hit an item
Expand Down
2 changes: 0 additions & 2 deletions source/D2ItemsForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class CD2EquippedItemStatic : public CStatic
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);

protected:
virtual void PreSubclassWindow();
Expand Down Expand Up @@ -100,7 +99,6 @@ class CD2ItemsGridStatic : public CStatic
//{{AFX_MSG(CCharNameEdit)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);

DECLARE_MESSAGE_MAP()

Expand Down
61 changes: 21 additions & 40 deletions source/D2MercenaryForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,45 +820,27 @@ void CD2MercenaryForm::CheckToolTipCtrl()
}
}
//---------------------------------------------------------------------------
const d2ce::Item* CD2MercenaryForm::InvHitTest(UINT id, CPoint /*point*/, TOOLINFO* /*pTI*/) const
const d2ce::Item* CD2MercenaryForm::InvHitTest(UINT id, CPoint point, TOOLINFO* pTI) const
{
const d2ce::Item* currItem = nullptr;
d2ce::EnumEquippedId equippedId = d2ce::EnumEquippedId::NONE;
ScreenToClient(&point);

switch (id)
{
case IDC_INV_MERC_HEAD:
equippedId = d2ce::EnumEquippedId::HEAD;
break;
return InvMercHeadBox.InvHitTest(point, pTI);

case IDC_INV_MERC_HAND_RIGHT:
equippedId = d2ce::EnumEquippedId::HAND_RIGHT;
break;
return InvMercHandRightBox.InvHitTest(point, pTI);

case IDC_INV_MERC_TORSO:
equippedId = d2ce::EnumEquippedId::TORSO;
break;
return InvMercTorsoBox.InvHitTest(point, pTI);

case IDC_INV_MERC_HAND_LEFT:
equippedId = d2ce::EnumEquippedId::HAND_LEFT;
return InvMercHandLeftBox.InvHitTest(point, pTI);
break;
}

if (equippedId != d2ce::EnumEquippedId::NONE)
{
for (const auto& item : MainForm.getMercItems())
{
if ((item.getLocation() != d2ce::EnumItemLocation::EQUIPPED) ||
(item.getEquippedId() != equippedId))
{
continue;
}

currItem = &item;
break;
}
}

return currItem;
return nullptr;
}
//---------------------------------------------------------------------------
BOOL CD2MercenaryForm::OnInitDialog()
Expand Down Expand Up @@ -1128,27 +1110,26 @@ void CD2MercenaryForm::OnClickedResurrectedCheck()
UpdateModified();
}
//---------------------------------------------------------------------------
INT_PTR CD2MercenaryForm::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
const d2ce::Item* CD2MercenaryForm::InvHitTest(CPoint point, TOOLINFO* pTI) const
{
// check child windows first by calling CMFCBaseToolBar
INT_PTR nHit = __super::OnToolHitTest(point, pTI);
if (nHit != -1)
if (nHit == -1)
{
return nHit;
return nullptr;
}

// Make sure we have hit an item
ClientToScreen(&point);
return InvHitTest((UINT)nHit, point, pTI);
}
//---------------------------------------------------------------------------
INT_PTR CD2MercenaryForm::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
TOOLINFO ti = { 0 };
auto pToolTip = AfxGetModuleThreadState()->m_pToolTip;
if (pToolTip != NULL && (pToolTip->GetOwner() == this))
{
TOOLINFO* pTi = pTI = nullptr ? &ti : pTI;
if (pToolTip->HitTest((CWnd*)this, point, pTi))
{
return pTi->uId;
}
}
ti.cbSize = sizeof(TOOLINFO);

return nHit;
TOOLINFO* pTi = (pTI == nullptr) ? &ti : pTI;
return InvHitTest(point, pTI) == nullptr ? (INT_PTR)-1 : pTi->uId;
}
//---------------------------------------------------------------------------
void CD2MercenaryForm::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
Expand Down
1 change: 1 addition & 0 deletions source/D2MercenaryForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CD2MercenaryForm : public CDialogEx, CD2ItemToolTipCtrlCallback

bool MercChanged() const;

const d2ce::Item* InvHitTest(CPoint point, TOOLINFO* pTI = nullptr) const; // get item at point
virtual INT_PTR OnToolHitTest(CPoint point, TOOLINFO* pTI) const;

// Dialog Data
Expand Down

0 comments on commit 0926f61

Please sign in to comment.