Files
AVA2/src/eter_ui/PythonWindow.cpp
T

1781 lines
58 KiB
C++

#include "PythonWindow.h"
#include "PythonWindowManager.h"
#include "src/eter_image/GrpDevice.h"
#include "eter_base/Timer.h"
#include "eter_render/CRenderTarget.h"
#include "shared/common/CRC32.h"
#include <SDL3/SDL.h>
#include <cassert>
namespace UI
{
DWORD CWindow::Type()
{
static DWORD s_dwType = GetCRC32("CWindow", 7);
return s_dwType;
}
BOOL CWindow::IsType(DWORD dwType)
{
return OnIsType(dwType);
}
BOOL CWindow::OnIsType(DWORD dwType)
{
if (CWindow::Type() == dwType)
return TRUE;
return FALSE;
}
CWindow::CWindow(PyObject * ppyObject)
: m_HorizontalAlign(HORIZONTAL_ALIGN_LEFT),
m_VerticalAlign(VERTICAL_ALIGN_TOP),
m_x(0), m_y(0),
m_lWidth(0), m_lHeight(0),
m_bMovable(false),
m_bShow(false),
m_dwFlag(0),
m_poHandler(ppyObject),
m_pParent(nullptr),
m_isUpdatingChildren(FALSE)
{
memset(&m_rect, 0, sizeof(m_rect));
memset(&m_limitBiasRect, 0, sizeof(m_limitBiasRect));
m_pChildList.clear();
m_pReserveChildList.clear();
}
CWindow::~CWindow()
{
Clear();
}
void CWindow::Clear()
{
if (m_pParent)
{
m_pParent->m_pChildList.remove(this);
m_pParent->m_pReserveChildList.remove(this);
m_pParent = nullptr;
}
for (auto* child : m_pChildList)
{
if (child && child->m_pParent == this)
{
child->m_pParent = nullptr;
}
}
m_pChildList.clear();
m_pReserveChildList.clear();
DestroyHandle();
Hide();
}
void CWindow::DestroyHandle()
{
m_poHandler = nullptr;
}
void CWindow::Show()
{
m_bShow = true;
}
void CWindow::Hide()
{
m_bShow = false;
}
bool CWindow::IsRendering()
{
if (!IsShow())
return false;
if (!m_pParent)
return true;
return m_pParent->IsRendering();
}
void CWindow::__RemoveReserveChildren()
{
if (m_pReserveChildList.empty())
return;
for (auto* child : m_pReserveChildList)
m_pChildList.remove(child);
m_pReserveChildList.clear();
}
void CWindow::Update()
{
if (!IsShow())
return;
__RemoveReserveChildren();
OnUpdate();
m_isUpdatingChildren = TRUE;
for (auto it = m_pChildList.begin(); it != m_pChildList.end();)
{
auto next = it;
++next;
CWindow* child = *it;
if (child && child->IsShow())
{
bool is_reserved = false;
for (auto* res : m_pReserveChildList)
{
if (res == child)
{
is_reserved = true;
break;
}
}
if (!is_reserved)
{
child->Update();
}
}
it = next;
}
m_isUpdatingChildren = FALSE;
__RemoveReserveChildren();
}
void CWindow::Render()
{
if (!IsShow())
return;
OnRender();
for (auto* child : m_pChildList)
child->Render();
}
void CWindow::OnUpdate()
{
if (!m_poHandler || !IsShow())
return;
PyCallClassMemberFunc(m_poHandler, "OnUpdate", BuildEmptyTuple());
}
void CWindow::OnRender()
{
if (!m_poHandler || !IsShow())
return;
PyCallClassMemberFunc(m_poHandler, "OnRender", BuildEmptyTuple());
}
void CWindow::SetName(const char * c_szName)
{
m_strName = c_szName ? c_szName : "";
}
void CWindow::SetSize(long width, long height)
{
m_lWidth = width;
m_lHeight = height;
UpdateRect();
}
void CWindow::SetPosition(long x, long y)
{
m_x = x;
m_y = y;
UpdateRect();
}
void CWindow::GetPosition(long * plx, long * ply)
{
if (plx) *plx = m_x;
if (ply) *ply = m_y;
}
void CWindow::SetHorizontalAlign(DWORD dwAlign)
{
m_HorizontalAlign = (EHorizontalAlign)dwAlign;
UpdateRect();
}
void CWindow::SetVerticalAlign(DWORD dwAlign)
{
m_VerticalAlign = (EVerticalAlign)dwAlign;
UpdateRect();
}
long CWindow::UpdateRect()
{
m_rect.top = m_y;
if (m_pParent)
{
switch (m_VerticalAlign)
{
case VERTICAL_ALIGN_BOTTOM:
m_rect.top = m_pParent->GetHeight() - m_rect.top;
break;
case VERTICAL_ALIGN_CENTER:
m_rect.top = (m_pParent->GetHeight() - GetHeight()) / 2 + m_rect.top;
break;
case VERTICAL_ALIGN_TOP:
default:
break;
}
m_rect.top += m_pParent->m_rect.top;
}
m_rect.bottom = m_rect.top + m_lHeight;
m_rect.left = m_x;
if (m_pParent)
{
switch (m_HorizontalAlign)
{
case HORIZONTAL_ALIGN_RIGHT:
m_rect.left = ::abs(m_pParent->GetWidth()) - m_rect.left;
break;
case HORIZONTAL_ALIGN_CENTER:
m_rect.left = m_pParent->GetWidth() / 2 - GetWidth() / 2 + m_rect.left;
break;
case HORIZONTAL_ALIGN_LEFT:
default:
break;
}
m_rect.left += 0L < m_pParent->GetWidth() ? m_pParent->m_rect.left : m_pParent->m_rect.right + ::abs(m_pParent->GetWidth());
}
m_rect.right = m_rect.left + m_lWidth;
for (auto* child : m_pChildList)
child->UpdateRect();
OnChangePosition();
return 1;
}
void CWindow::GetLocalPosition(long & rlx, long & rly)
{
rlx = rlx - m_rect.left;
rly = rly - m_rect.top;
}
void CWindow::GetMouseLocalPosition(long & rlx, long & rly)
{
CWindowManager::Instance().GetMousePosition(rlx, rly);
rlx = rlx - m_rect.left;
rly = rly - m_rect.top;
}
void CWindow::AddChild(CWindow * pWin)
{
if (!pWin) return;
for (auto* child : m_pChildList)
{
if (child == pWin)
return;
}
m_pChildList.push_back(pWin);
pWin->m_pParent = this;
}
CWindow * CWindow::GetRoot()
{
if (m_pParent && m_pParent->IsWindow())
return m_pParent->GetRoot();
return this;
}
CWindow * CWindow::GetParent()
{
return m_pParent;
}
bool CWindow::IsChild(CWindow * pWin)
{
for (auto* child : m_pChildList)
{
if (child == pWin || child->IsChild(pWin))
return true;
}
return false;
}
void CWindow::DeleteChild(CWindow * pWin)
{
if (m_isUpdatingChildren)
m_pReserveChildList.push_back(pWin);
else
m_pChildList.remove(pWin);
}
void CWindow::SetTop(CWindow * pWin)
{
if (!pWin) return;
auto it = std::find(m_pChildList.begin(), m_pChildList.end(), pWin);
if (it != m_pChildList.end())
{
m_pChildList.erase(it);
m_pChildList.push_back(pWin);
}
}
bool CWindow::IsIn(long x, long y)
{
if (x >= m_rect.left && x <= m_rect.right && y >= m_rect.top && y <= m_rect.bottom)
return true;
return false;
}
bool CWindow::IsIn()
{
long lx = 0, ly = 0;
CWindowManager::Instance().GetMousePosition(lx, ly);
return IsIn(lx, ly);
}
CWindow * CWindow::PickWindow(long x, long y)
{
for (auto it = m_pChildList.rbegin(); it != m_pChildList.rend(); ++it)
{
CWindow * pWin = (*it)->PickWindow(x, y);
if (pWin) return pWin;
}
if (IsFlag(FLAG_NOT_PICK)) return nullptr;
if (IsIn(x, y)) return this;
return nullptr;
}
CWindow * CWindow::PickTopWindow(long x, long y)
{
for (auto it = m_pChildList.rbegin(); it != m_pChildList.rend(); ++it)
{
if ((*it)->IsIn(x, y)) return *it;
}
return nullptr;
}
void CWindow::OnSetFocus()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnSetFocus", BuildEmptyTuple());
}
void CWindow::OnKillFocus()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnKillFocus", BuildEmptyTuple());
}
void CWindow::OnMouseDrag(long lx, long ly)
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnMouseDrag", Py_BuildValue("(ii)", lx, ly));
}
void CWindow::OnMouseOverIn()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnMouseOverIn", BuildEmptyTuple());
}
void CWindow::OnMouseOverOut()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnMouseOverOut", BuildEmptyTuple());
}
void CWindow::OnMouseOver()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnMouseOver", BuildEmptyTuple());
}
void CWindow::OnDrop()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnDrop", BuildEmptyTuple());
}
void CWindow::OnTop()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnTop", BuildEmptyTuple());
}
void CWindow::OnIMEUpdate()
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnIMEUpdate", BuildEmptyTuple());
}
void CWindow::OnMoveWindow(long x, long y)
{
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "OnMoveWindow", Py_BuildValue("(ii)", x, y));
}
BOOL CWindow::RunIMETabEvent() { return OnIMETabEvent(); }
BOOL CWindow::RunIMEReturnEvent() { return OnIMEReturnEvent(); }
BOOL CWindow::RunIMEKeyDownEvent(int ikey) { return OnIMEKeyDownEvent(ikey); }
CWindow * CWindow::RunKeyDownEvent(int ikey)
{
if (OnKeyDown(ikey)) return this;
for (auto* child : m_pChildList)
{
CWindow* hit = child->RunKeyDownEvent(ikey);
if (hit) return hit;
}
return nullptr;
}
BOOL CWindow::RunKeyUpEvent(int ikey)
{
if (OnKeyUp(ikey)) return TRUE;
for (auto* child : m_pChildList)
{
if (child->RunKeyUpEvent(ikey)) return TRUE;
}
return FALSE;
}
BOOL CWindow::RunPressEscapeKeyEvent() { return OnPressEscapeKey(); }
BOOL CWindow::RunPressExitKeyEvent() { return OnPressExitKey(); }
BOOL CWindow::OnIMETabEvent() { return FALSE; }
BOOL CWindow::OnIMEReturnEvent() { return FALSE; }
BOOL CWindow::OnIMEKeyDownEvent(int ikey) { return FALSE; }
BOOL CWindow::OnIMEChangeCodePage() { return FALSE; }
BOOL CWindow::OnIMEOpenCandidateListEvent() { return FALSE; }
BOOL CWindow::OnIMECloseCandidateListEvent() { return FALSE; }
BOOL CWindow::OnIMEOpenReadingWndEvent() { return FALSE; }
BOOL CWindow::OnIMECloseReadingWndEvent() { return FALSE; }
BOOL CWindow::OnMouseLeftButtonDown()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseLeftButtonDown", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnMouseLeftButtonUp()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseLeftButtonUp", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnMouseLeftButtonDoubleClick()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseLeftButtonDoubleClick", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnMouseRightButtonDown()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseRightButtonDown", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnMouseRightButtonUp()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseRightButtonUp", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnMouseRightButtonDoubleClick()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnMouseRightButtonDoubleClick", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
// EterPythonLib/PythonWindow.cpp CWindow::OnMouseMiddleButtonDown/Up, RunMouseWheelEvent,
// OnMouseWheel/OnMouseWheelScroll (ENABLE_MOUSEWHEEL_EVENT is on in AVA2 Locale_inc.h).
BOOL CWindow::OnMouseMiddleButtonDown()
{
if (!m_poHandler) return FALSE;
long ret = 0;
if (PyCallClassMemberFunc(m_poHandler, "OnMouseMiddleButtonDown", BuildEmptyTuple(), &ret) && ret)
return TRUE;
return FALSE;
}
BOOL CWindow::OnMouseMiddleButtonUp()
{
if (!m_poHandler) return FALSE;
long ret = 0;
if (PyCallClassMemberFunc(m_poHandler, "OnMouseMiddleButtonUp", BuildEmptyTuple(), &ret) && ret)
return TRUE;
return FALSE;
}
BOOL CWindow::RunMouseWheelEvent(long nLen)
{
if (!m_poHandler) return FALSE;
long ret = 0;
if (PyCallClassMemberFunc(m_poHandler, "OnRunMouseWheel", Py_BuildValue("(l)", nLen), &ret))
return ret ? TRUE : FALSE;
return FALSE;
}
BOOL CWindow::OnMouseWheel(int nLen)
{
if (!m_poHandler) return FALSE;
long ret = 0;
if (PyCallClassMemberFunc(m_poHandler, "OnMouseWheel", Py_BuildValue("(i)", nLen), &ret) && ret)
return TRUE;
return FALSE;
}
BOOL CWindow::OnMouseWheelScroll(short wDelta)
{
if (m_poHandler)
PyCallClassMemberFunc(m_poHandler, "OnMouseWheelScroll", Py_BuildValue("(s)", wDelta > 0 ? "UP" : "DOWN"));
return m_bIsScrollable ? TRUE : FALSE;
}
BOOL CWindow::OnKeyDown(int ikey)
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnKeyDown", Py_BuildValue("(i)", ikey), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnKeyUp(int ikey)
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnKeyUp", Py_BuildValue("(i)", ikey), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnPressEscapeKey()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnPressEscapeKey", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
BOOL CWindow::OnPressExitKey()
{
if (!m_poHandler) return FALSE;
long ret = 0;
PyCallClassMemberFunc(m_poHandler, "OnPressExitKey", BuildEmptyTuple(), &ret);
return ret ? TRUE : FALSE;
}
void CBox::OnRender()
{
if (!IsShow()) return;
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
uint8_t a = (m_dwColor >> 24) & 0xFF;
uint8_t r = (m_dwColor >> 16) & 0xFF;
uint8_t g = (m_dwColor >> 8) & 0xFF;
uint8_t b = m_dwColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)m_rect.left, (float)m_rect.top, (float)GetWidth(), (float)GetHeight() };
SDL_RenderRect(renderer, &rect);
}
void CBar::OnRender()
{
if (!IsShow()) return;
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
uint8_t a = (m_dwColor >> 24) & 0xFF;
uint8_t r = (m_dwColor >> 16) & 0xFF;
uint8_t g = (m_dwColor >> 8) & 0xFF;
uint8_t b = m_dwColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)m_rect.left, (float)m_rect.top, (float)GetWidth(), (float)GetHeight() };
SDL_RenderFillRect(renderer, &rect);
}
void CLine::OnRender()
{
if (!IsShow()) return;
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
uint8_t a = (m_dwColor >> 24) & 0xFF;
uint8_t r = (m_dwColor >> 16) & 0xFF;
uint8_t g = (m_dwColor >> 8) & 0xFF;
uint8_t b = m_dwColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_RenderLine(renderer, (float)m_rect.left, (float)m_rect.top, (float)m_rect.right, (float)m_rect.bottom);
}
// Type Registrations
DWORD CBar3D::Type() { static DWORD s = GetCRC32("CBar3D", 6); return s; }
BOOL CBar3D::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
void CBar3D::OnRender()
{
if (!IsShow()) return;
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
uint8_t ca = (m_dwCenterColor >> 24) & 0xFF, cr = (m_dwCenterColor >> 16) & 0xFF, cg = (m_dwCenterColor >> 8) & 0xFF, cb = m_dwCenterColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, cr, cg, cb, ca);
SDL_FRect rect{ (float)m_rect.left, (float)m_rect.top, (float)GetWidth(), (float)GetHeight() };
SDL_RenderFillRect(renderer, &rect);
uint8_t la = (m_dwLeftColor >> 24) & 0xFF, lr = (m_dwLeftColor >> 16) & 0xFF, lg = (m_dwLeftColor >> 8) & 0xFF, lb = m_dwLeftColor & 0xFF;
SDL_SetRenderDrawColor(renderer, lr, lg, lb, la);
SDL_RenderLine(renderer, (float)m_rect.left, (float)m_rect.top, (float)m_rect.right, (float)m_rect.top);
SDL_RenderLine(renderer, (float)m_rect.left, (float)m_rect.top, (float)m_rect.left, (float)m_rect.bottom);
uint8_t ra = (m_dwRightColor >> 24) & 0xFF, rr = (m_dwRightColor >> 16) & 0xFF, rg = (m_dwRightColor >> 8) & 0xFF, rb = m_dwRightColor & 0xFF;
SDL_SetRenderDrawColor(renderer, rr, rg, rb, ra);
SDL_RenderLine(renderer, (float)m_rect.left, (float)m_rect.bottom, (float)m_rect.right, (float)m_rect.bottom);
SDL_RenderLine(renderer, (float)m_rect.right, (float)m_rect.top, (float)m_rect.right, (float)m_rect.bottom);
}
void CGraphicTextInstance::Render()
{
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
std::string displayText = m_text;
if (m_secret)
displayText.assign(m_text.length(), '*');
if (displayText.empty() && !m_showCursor)
return;
const float charWidth = 8.0f;
const float charHeight = 8.0f;
float textWidth = static_cast<float>(displayText.length()) * charWidth;
float textHeight = charHeight;
float startX = m_v2Position.x;
float startY = m_v2Position.y;
switch (m_hAlign)
{
case HORIZONTAL_ALIGN_CENTER:
startX -= textWidth * 0.5f;
break;
case HORIZONTAL_ALIGN_RIGHT:
startX -= textWidth;
break;
default:
break;
}
switch (m_vAlign)
{
case VERTICAL_ALIGN_CENTER:
startY -= textHeight * 0.5f;
break;
case VERTICAL_ALIGN_BOTTOM:
startY -= textHeight;
break;
default:
break;
}
uint8_t a = (m_color >> 24) & 0xff;
uint8_t r = (m_color >> 16) & 0xff;
uint8_t g = (m_color >> 8) & 0xff;
uint8_t b = m_color & 0xff;
if (a == 0 && m_color != 0) a = 255;
if (m_outline && !displayText.empty())
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, a);
SDL_RenderDebugText(renderer, startX - 1.0f, startY, displayText.c_str());
SDL_RenderDebugText(renderer, startX + 1.0f, startY, displayText.c_str());
SDL_RenderDebugText(renderer, startX, startY - 1.0f, displayText.c_str());
SDL_RenderDebugText(renderer, startX, startY + 1.0f, displayText.c_str());
}
if (!displayText.empty())
{
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_RenderDebugText(renderer, startX, startY, displayText.c_str());
}
if (m_showCursor)
{
float cursorX = startX + static_cast<float>(displayText.length()) * charWidth;
SDL_SetRenderDrawColor(renderer, 240, 240, 240, 255);
SDL_FRect cursorRect{ cursorX + 1.0f, startY, 2.0f, charHeight + 2.0f };
SDL_RenderFillRect(renderer, &cursorRect);
}
}
DWORD CTextLine::Type() { static DWORD s = GetCRC32("CTextLine", 9); return s; }
BOOL CTextLine::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CTextLine::CTextLine(PyObject * ppyObject) : CWindow(ppyObject) {}
CTextLine::~CTextLine() {}
void CTextLine::SetText(const char * c_szText)
{
m_TextInstance.SetValue(c_szText);
m_TextInstance.Update();
}
void CTextLine::OnRender()
{
if (IsShow())
m_TextInstance.Render();
}
void CTextLine::OnChangePosition()
{
m_TextInstance.SetPosition(static_cast<float>(m_rect.left), static_cast<float>(m_rect.top));
}
DWORD CNumberLine::Type() { static DWORD s = GetCRC32("CNumberLine", 11); return s; }
BOOL CNumberLine::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
// CImageBox
DWORD CImageBox::Type() { static DWORD s = GetCRC32("CImageBox", 9); return s; }
BOOL CImageBox::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CImageBox::CImageBox(PyObject * ppyObject) : CWindow(ppyObject), m_fCoolTime(0.0f), m_fCoolTimeStart(0.0f)
{
m_pImageInstance = nullptr;
}
CImageBox::~CImageBox()
{
OnDestroyInstance();
}
void CImageBox::LeftRightReverse()
{
if (m_pImageInstance)
m_pImageInstance->LeftRightReverse();
}
void CImageBox::Clear()
{
OnDestroyInstance();
}
void CImageBox::OnCreateInstance()
{
OnDestroyInstance();
m_pImageInstance = CGraphicImageInstance::New();
}
void CImageBox::OnDestroyInstance()
{
if (m_pImageInstance)
{
CGraphicImageInstance::Delete(m_pImageInstance);
m_pImageInstance = nullptr;
}
}
BOOL CImageBox::LoadImage(CGraphicImageInstance* imageInstance)
{
if (!imageInstance)
return FALSE;
OnCreateInstance();
m_pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(imageInstance->GetGraphicImagePointer()));
if (m_pImageInstance->IsEmpty())
return FALSE;
SetSize(m_pImageInstance->GetWidth(), m_pImageInstance->GetHeight());
UpdateRect();
CGraphicImageInstance::Delete(imageInstance);
return TRUE;
}
BOOL CImageBox::LoadImage(const char * c_szFileName)
{
if (!c_szFileName || !c_szFileName[0])
return FALSE;
OnCreateInstance();
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return FALSE;
m_pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (m_pImageInstance->IsEmpty())
return FALSE;
SetSize(m_pImageInstance->GetWidth(), m_pImageInstance->GetHeight());
UpdateRect();
return TRUE;
}
void CImageBox::SetDiffuseColor(float fr, float fg, float fb, float fa)
{
if (m_pImageInstance)
m_pImageInstance->SetDiffuseColor(fr, fg, fb, fa);
}
void CImageBox::SetScale(float sx, float sy)
{
if (!m_pImageInstance)
return;
m_pImageInstance->SetScale(sx, sy);
CWindow::SetSize(long(float(GetWidth()) * sx), long(float(GetHeight()) * sy));
}
int CImageBox::GetWidth()
{
return m_pImageInstance ? m_pImageInstance->GetWidth() : 0;
}
int CImageBox::GetHeight()
{
return m_pImageInstance ? m_pImageInstance->GetHeight() : 0;
}
void CImageBox::OnUpdate() {}
void CImageBox::OnRender()
{
if (!m_pImageInstance)
return;
if (IsShow())
{
if (m_fCoolTime == 0.0f)
m_pImageInstance->Render();
else
m_pImageInstance->RenderCoolTime((CTimer::Instance().GetCurrentSecond() - m_fCoolTimeStart) / m_fCoolTime);
}
}
void CImageBox::OnChangePosition()
{
if (m_pImageInstance)
m_pImageInstance->SetPosition(m_rect.left, m_rect.top);
}
// CMarkBox
DWORD CMarkBox::Type() { static DWORD s = GetCRC32("CMarkBox", 8); return s; }
BOOL CMarkBox::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CMarkBox::CMarkBox(PyObject * ppyObject) : CWindow(ppyObject)
{
m_pMarkInstance = nullptr;
}
CMarkBox::~CMarkBox()
{
OnDestroyInstance();
}
void CMarkBox::OnCreateInstance()
{
OnDestroyInstance();
m_pMarkInstance = CGraphicMarkInstance::New();
}
void CMarkBox::OnDestroyInstance()
{
if (m_pMarkInstance)
{
CGraphicMarkInstance::Delete(m_pMarkInstance);
m_pMarkInstance = nullptr;
}
}
void CMarkBox::LoadImage(const char * c_szFilename)
{
OnCreateInstance();
m_pMarkInstance->SetImageFileName(c_szFilename);
m_pMarkInstance->Load();
SetSize(m_pMarkInstance->GetWidth(), m_pMarkInstance->GetHeight());
UpdateRect();
}
void CMarkBox::SetScale(FLOAT fScale)
{
if (m_pMarkInstance)
m_pMarkInstance->SetScale(fScale);
}
void CMarkBox::SetIndex(UINT uIndex)
{
if (m_pMarkInstance)
m_pMarkInstance->SetIndex(uIndex);
}
void CMarkBox::SetDiffuseColor(float fr, float fg, float fb, float fa)
{
if (m_pMarkInstance)
m_pMarkInstance->SetDiffuseColor(fr, fg, fb, fa);
}
void CMarkBox::OnUpdate() {}
void CMarkBox::OnRender()
{
if (m_pMarkInstance && IsShow())
m_pMarkInstance->Render();
}
void CMarkBox::OnChangePosition()
{
if (m_pMarkInstance)
m_pMarkInstance->SetPosition(m_rect.left, m_rect.top);
}
// CExpandedImageBox
DWORD CExpandedImageBox::Type() { static DWORD s = GetCRC32("CExpandedImageBox", 18); return s; }
BOOL CExpandedImageBox::OnIsType(DWORD t) { return t == Type() || CImageBox::OnIsType(t); }
CExpandedImageBox::CExpandedImageBox(PyObject * ppyObject) : CImageBox(ppyObject) {}
CExpandedImageBox::~CExpandedImageBox()
{
OnDestroyInstance();
}
void CExpandedImageBox::OnCreateInstance()
{
OnDestroyInstance();
m_pImageInstance = CGraphicExpandedImageInstance::New();
}
void CExpandedImageBox::OnDestroyInstance()
{
if (m_pImageInstance)
{
CGraphicExpandedImageInstance::Delete((CGraphicExpandedImageInstance*)m_pImageInstance);
m_pImageInstance = nullptr;
}
}
void CExpandedImageBox::SetScale(float fx, float fy)
{
if (!m_pImageInstance)
return;
((CGraphicExpandedImageInstance*)m_pImageInstance)->SetScale(fx, fy);
CWindow::SetSize(long(float(GetWidth()) * fx), long(float(GetHeight()) * fy));
}
void CExpandedImageBox::SetOrigin(float fx, float fy)
{
if (m_pImageInstance)
((CGraphicExpandedImageInstance*)m_pImageInstance)->SetOrigin(fx, fy);
}
void CExpandedImageBox::SetRotation(float fRotation)
{
if (m_pImageInstance)
((CGraphicExpandedImageInstance*)m_pImageInstance)->SetRotation(fRotation);
}
void CExpandedImageBox::SetRenderingRect(float fLeft, float fTop, float fRight, float fBottom)
{
if (m_pImageInstance)
((CGraphicExpandedImageInstance*)m_pImageInstance)->SetRenderingRect(fLeft, fTop, fRight, fBottom);
}
void CExpandedImageBox::SetRenderingMode(int iMode)
{
if (m_pImageInstance)
((CGraphicExpandedImageInstance*)m_pImageInstance)->SetRenderingMode(iMode);
}
void CExpandedImageBox::OnUpdate() {}
void CExpandedImageBox::OnRender()
{
if (!m_pImageInstance)
return;
if (IsShow())
{
if (m_fCoolTime == 0.0f)
m_pImageInstance->Render();
else
((CGraphicExpandedImageInstance*)m_pImageInstance)->RenderCoolTime((CTimer::Instance().GetCurrentSecond() - m_fCoolTimeStart) / m_fCoolTime);
}
}
// CAniImageBox
DWORD CAniImageBox::Type() { static DWORD s = GetCRC32("CAniImageBox", 12); return s; }
BOOL CAniImageBox::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CAniImageBox::CAniImageBox(PyObject * ppyObject)
: CWindow(ppyObject), m_bycurDelay(0), m_byDelay(4), m_bycurIndex(0)
{
m_ImageVector.clear();
}
CAniImageBox::~CAniImageBox()
{
for (auto* pImg : m_ImageVector)
CGraphicExpandedImageInstance::DeleteExpandedImageInstance(pImg);
m_ImageVector.clear();
}
void CAniImageBox::SetDelay(int iDelay) { m_byDelay = (BYTE)iDelay; }
void CAniImageBox::AppendImage(const char * c_szFileName, float r, float g, float b, float a)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return;
CGraphicExpandedImageInstance * pImageInstance = CGraphicExpandedImageInstance::New();
pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
pImageInstance->SetDiffuseColor(r, g, b, a);
if (pImageInstance->IsEmpty())
{
CGraphicExpandedImageInstance::Delete(pImageInstance);
return;
}
m_ImageVector.push_back(pImageInstance);
m_bycurIndex = rand() % m_ImageVector.size();
SetSize(pImageInstance->GetWidth(), pImageInstance->GetHeight());
}
void CAniImageBox::AppendImageScale(const char * c_szFileName, float scale_x, float scale_y)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return;
CGraphicExpandedImageInstance * pImageInstance = CGraphicExpandedImageInstance::New();
pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
pImageInstance->SetScale(scale_x, scale_y);
if (pImageInstance->IsEmpty())
{
CGraphicExpandedImageInstance::Delete(pImageInstance);
return;
}
m_ImageVector.push_back(pImageInstance);
m_bycurIndex = rand() % m_ImageVector.size();
}
void CAniImageBox::SetRenderingRect(float fLeft, float fTop, float fRight, float fBottom)
{
for (auto* pImg : m_ImageVector)
pImg->SetRenderingRect(fLeft, fTop, fRight, fBottom);
}
void CAniImageBox::SetRenderingMode(int iMode)
{
for (auto* pImg : m_ImageVector)
pImg->SetRenderingMode(iMode);
}
void CAniImageBox::ResetFrame() { m_bycurIndex = 0; }
void CAniImageBox::SetRotation(float fRotation)
{
for (auto* pImg : m_ImageVector)
pImg->SetRotation(fRotation);
}
void CAniImageBox::SetMoveStatus(bool bFlag) {}
void CAniImageBox::SetMoveSpeed(float speed) {}
void CAniImageBox::SetMovePos(float directionX, float directionY) {}
float CAniImageBox::GetRotation()
{
return m_ImageVector.empty() ? 0.0f : m_ImageVector[0]->GetRotation();
}
void CAniImageBox::OnUpdate()
{
++m_bycurDelay;
if (m_bycurDelay < m_byDelay)
return;
m_bycurDelay = 0;
++m_bycurIndex;
if (m_bycurIndex >= m_ImageVector.size())
{
m_bycurIndex = 0;
OnEndFrame();
}
}
void CAniImageBox::OnRender()
{
if (m_bycurIndex < m_ImageVector.size())
m_ImageVector[m_bycurIndex]->Render();
}
void CAniImageBox::OnChangePosition()
{
for (auto* pImg : m_ImageVector)
pImg->SetPosition(m_rect.left, m_rect.top);
}
void CAniImageBox::OnEndFrame()
{
if (m_poHandler)
PyCallClassMemberFunc(m_poHandler, "OnEndFrame", BuildEmptyTuple());
}
// CButton
DWORD CButton::Type() { static DWORD s = GetCRC32("CButton", 7); return s; }
BOOL CButton::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CButton::CButton(PyObject * ppyObject)
: CWindow(ppyObject), m_pcurVisual(nullptr), m_bEnable(TRUE), m_isPressed(FALSE),
m_isFlashEx(FALSE), m_isFlash(FALSE)
{
CWindow::AddFlag(CWindow::FLAG_NOT_CAPTURE);
}
CButton::~CButton() {}
void CButton::LeftRightReverse()
{
m_upVisual.LeftRightReverse();
m_overVisual.LeftRightReverse();
m_downVisual.LeftRightReverse();
m_disableVisual.LeftRightReverse();
}
BOOL CButton::SetUpVisual(const char * c_szFileName)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return FALSE;
m_upVisual.SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (m_upVisual.IsEmpty())
return FALSE;
SetSize(m_upVisual.GetWidth(), m_upVisual.GetHeight());
SetCurrentVisual(&m_upVisual);
return TRUE;
}
BOOL CButton::SetOverVisual(const char * c_szFileName)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return FALSE;
m_overVisual.SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (m_overVisual.IsEmpty())
return FALSE;
SetSize(m_overVisual.GetWidth(), m_overVisual.GetHeight());
return TRUE;
}
BOOL CButton::SetDownVisual(const char * c_szFileName)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return FALSE;
m_downVisual.SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (m_downVisual.IsEmpty())
return FALSE;
SetSize(m_downVisual.GetWidth(), m_downVisual.GetHeight());
return TRUE;
}
BOOL CButton::SetDisableVisual(const char * c_szFileName)
{
CResource * pResource = CResourceManager::Instance().GetResourcePointer(c_szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return FALSE;
m_disableVisual.SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (m_disableVisual.IsEmpty())
return FALSE;
SetSize(m_disableVisual.GetWidth(), m_disableVisual.GetHeight());
return TRUE;
}
const char * CButton::GetUpVisualFileName()
{
CGraphicImage* pImg = m_upVisual.GetGraphicImagePointer();
return pImg ? pImg->GetFileName() : "";
}
const char * CButton::GetOverVisualFileName()
{
CGraphicImage* pImg = m_overVisual.GetGraphicImagePointer();
return pImg ? pImg->GetFileName() : "";
}
const char * CButton::GetDownVisualFileName()
{
CGraphicImage* pImg = m_downVisual.GetGraphicImagePointer();
return pImg ? pImg->GetFileName() : "";
}
void CButton::SetUp()
{
SetCurrentVisual(&m_upVisual);
m_isPressed = FALSE;
}
void CButton::Up()
{
if (IsIn())
SetCurrentVisual(&m_overVisual);
else
SetCurrentVisual(&m_upVisual);
m_isPressed = FALSE;
}
void CButton::Over()
{
SetCurrentVisual(&m_overVisual);
}
void CButton::Down()
{
m_isPressed = TRUE;
SetCurrentVisual(&m_downVisual);
}
void CButton::SetCurrentVisual(CGraphicImageInstance* pVisual)
{
m_pcurVisual = pVisual;
if (m_pcurVisual)
m_pcurVisual->SetPosition(m_rect.left, m_rect.top);
}
void CButton::OnUpdate() {}
void CButton::OnRender()
{
if (!IsShow())
return;
if (m_pcurVisual)
m_pcurVisual->Render();
}
void CButton::OnChangePosition()
{
if (m_pcurVisual)
m_pcurVisual->SetPosition(m_rect.left, m_rect.top);
}
BOOL CButton::OnMouseLeftButtonDown()
{
if (!m_bEnable) return FALSE;
m_isPressed = TRUE;
Down();
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "DownEvent", BuildEmptyTuple());
return TRUE;
}
BOOL CButton::OnMouseLeftButtonUp()
{
if (!m_bEnable) return FALSE;
m_isPressed = FALSE;
SetUp();
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "CallEvent", BuildEmptyTuple());
return TRUE;
}
void CButton::OnMouseOverIn()
{
if (!m_bEnable) return;
Over();
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "ShowToolTip", BuildEmptyTuple());
}
void CButton::OnMouseOverOut()
{
if (!m_bEnable) return;
SetUp();
if (m_poHandler) PyCallClassMemberFunc(m_poHandler, "HideToolTip", BuildEmptyTuple());
}
DWORD CRadioButton::Type() { static DWORD s = GetCRC32("CRadioButton", 12); return s; }
BOOL CRadioButton::OnIsType(DWORD t) { return t == Type() || CButton::OnIsType(t); }
DWORD CToggleButton::Type() { static DWORD s = GetCRC32("CToggleButton", 13); return s; }
BOOL CToggleButton::OnIsType(DWORD t) { return t == Type() || CButton::OnIsType(t); }
DWORD CDragButton::Type() { static DWORD s = GetCRC32("CDragButton", 11); return s; }
BOOL CDragButton::OnIsType(DWORD t) { return t == Type() || CButton::OnIsType(t); }
DWORD CUiRenderTarget::Type() { static DWORD s = GetCRC32("CUiRenderTarget", 15); return s; }
BOOL CUiRenderTarget::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
void CUiRenderTarget::OnRender()
{
if (!IsShow()) return;
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
if (!pDevice) return;
SDL_Renderer* renderer = pDevice->GetRenderer();
if (!renderer) return;
auto target = ava2::CRenderTargetManager::Instance().GetRenderTarget((int)m_dwIndex);
if (target && target->IsVisible())
{
SDL_FRect rect{ (float)m_rect.left, (float)m_rect.top, (float)GetWidth(), (float)GetHeight() };
target->Render(renderer, rect);
}
}
DWORD CSlotWindow::Type() { static DWORD s = GetCRC32("CSlotWindow", 11); return s; }
BOOL CSlotWindow::OnIsType(DWORD t) { return t == Type() || CWindow::OnIsType(t); }
CSlotWindow::CSlotWindow(PyObject * ppyObject)
: CWindow(ppyObject), m_dwSlotType(0), m_dwSlotStyle(0), m_pBaseImageInstance(nullptr)
{
}
CSlotWindow::~CSlotWindow()
{
Destroy();
}
void CSlotWindow::Destroy()
{
ClearAllSlot();
if (m_pBaseImageInstance) {
CGraphicImageInstance::Delete(m_pBaseImageInstance);
m_pBaseImageInstance = nullptr;
}
}
void CSlotWindow::AppendSlot(DWORD dwIndex, int ix, int iy, int iw, int ih)
{
SSlot slot;
slot.dwIndex = dwIndex;
slot.ixPosition = ix;
slot.iyPosition = iy;
slot.ixCellSize = iw;
slot.iyCellSize = ih;
slot.bRenderBaseSlotImage = true;
slot.isItem = false;
slot.dwItemIndex = 0;
slot.pInstance = nullptr;
slot.pCoverInstance = nullptr;
m_slotMap[dwIndex] = slot;
if (!HasSlot(dwIndex)) m_slots.push_back(dwIndex);
}
void CSlotWindow::SetSlotBaseImage(const char* szFileName, float fr, float fg, float fb, float fa)
{
CResource* pResource = CResourceManager::Instance().GetResourcePointer(szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return;
if (!m_pBaseImageInstance)
m_pBaseImageInstance = CGraphicImageInstance::New();
m_pBaseImageInstance->SetImagePointer((CGraphicImage*)pResource);
m_pBaseImageInstance->SetDiffuseColor(fr, fg, fb, fa);
}
void CSlotWindow::SetCoverButton(DWORD dwIndex, const char* c_szUpImageName, const char*, const char*, const char*, BOOL, BOOL)
{
auto it = m_slotMap.find(dwIndex);
if (it == m_slotMap.end()) return;
CResource* pRes = CResourceManager::Instance().GetResourcePointer(c_szUpImageName);
if (!pRes || !pRes->IsType(CGraphicImage::Type())) return;
if (!it->second.pCoverInstance)
it->second.pCoverInstance = CGraphicImageInstance::New();
it->second.pCoverInstance->SetImagePointer((CGraphicImage*)pRes);
}
void CSlotWindow::ShowSlotBaseImage(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bRenderBaseSlotImage = true;
}
void CSlotWindow::HideSlotBaseImage(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bRenderBaseSlotImage = false;
}
BOOL CSlotWindow::HasSlot(DWORD dwIndex)
{
return m_slotMap.find(dwIndex) != m_slotMap.end();
}
void CSlotWindow::ClearSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) {
it->second.dwItemIndex = 0;
it->second.isItem = false;
it->second.dwItemCount = 0;
if (it->second.pInstance) {
CGraphicImageInstance::Delete(it->second.pInstance);
it->second.pInstance = nullptr;
}
if (it->second.pCoverInstance) {
CGraphicImageInstance::Delete(it->second.pCoverInstance);
it->second.pCoverInstance = nullptr;
}
}
}
void CSlotWindow::ClearAllSlot()
{
for (auto& pair : m_slotMap) {
if (pair.second.pInstance) {
CGraphicImageInstance::Delete(pair.second.pInstance);
pair.second.pInstance = nullptr;
}
if (pair.second.pCoverInstance) {
CGraphicImageInstance::Delete(pair.second.pCoverInstance);
pair.second.pCoverInstance = nullptr;
}
}
m_slotMap.clear();
m_slots.clear();
}
void CSlotWindow::SetSlot(DWORD dwIndex, DWORD dwItemIndex, BYTE byWidth, BYTE byHeight, CGraphicImage* pImage, const D3DXCOLOR& diffuseColor)
{
auto it = m_slotMap.find(dwIndex);
if (it == m_slotMap.end()) {
AppendSlot(dwIndex, 0, 0, 32, 32);
it = m_slotMap.find(dwIndex);
}
SSlot& slot = it->second;
slot.dwItemIndex = dwItemIndex;
slot.byWidth = byWidth;
slot.byHeight = byHeight;
slot.isItem = (pImage != nullptr);
if (pImage) {
if (!slot.pInstance)
slot.pInstance = CGraphicImageInstance::New();
slot.pInstance->SetImagePointer(pImage);
slot.pInstance->SetDiffuseColor(diffuseColor.r, diffuseColor.g, diffuseColor.b, diffuseColor.a);
} else {
if (slot.pInstance) {
CGraphicImageInstance::Delete(slot.pInstance);
slot.pInstance = nullptr;
}
}
}
void CSlotWindow::SetSlotScale(DWORD dwIndex, DWORD dwItemIndex, BYTE byWidth, BYTE byHeight, CGraphicImage* pImage, float sx, float sy, const D3DXCOLOR& diffuseColor)
{
SetSlot(dwIndex, dwItemIndex, byWidth, byHeight, pImage, diffuseColor);
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end() && it->second.pInstance) {
it->second.pInstance->SetScale(sx, sy);
}
}
void CSlotWindow::SetSlotCount(DWORD dwIndex, DWORD dwCount)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) {
it->second.dwItemCount = dwCount;
}
}
void CSlotWindow::SetSlotCoolTime(DWORD dwIndex, float fCoolTime, float fElapsedTime)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) {
it->second.fCoolTime = fCoolTime;
it->second.fStartCoolTime = (SDL_GetTicks() / 1000.0f) - fElapsedTime;
SDL_Log("CSlotWindow::SetSlotCoolTime slot=%u cd=%.1f elapsed=%.1f", (unsigned)dwIndex, fCoolTime, fElapsedTime);
}
}
bool CSlotWindow::IsSlotCoolTime(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end() && it->second.fCoolTime > 0.0f) {
float elapsed = (SDL_GetTicks() / 1000.0f) - it->second.fStartCoolTime;
return elapsed < it->second.fCoolTime;
}
return false;
}
float CSlotWindow::GetSlotCoolTime(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) return it->second.fCoolTime;
return 0.0f;
}
float CSlotWindow::GetSlotRemainingCoolTime(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end() && it->second.fCoolTime > 0.0f) {
float elapsed = (SDL_GetTicks() / 1000.0f) - it->second.fStartCoolTime;
return std::max(0.0f, it->second.fCoolTime - elapsed);
}
return 0.0f;
}
void CSlotWindow::SetSlotCountNew(DWORD dwIndex, DWORD dwGrade, DWORD dwCount)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) {
it->second.dwGrade = dwGrade;
it->second.dwItemCount = dwCount;
}
}
void CSlotWindow::ActivateSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bActive = true;
}
void CSlotWindow::DeactivateSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bActive = false;
}
void CSlotWindow::LockSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bLocked = true;
}
void CSlotWindow::UnlockSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) it->second.bLocked = false;
}
BOOL CSlotWindow::IsLockSlot(DWORD dwIndex)
{
auto it = m_slotMap.find(dwIndex);
if (it != m_slotMap.end()) return it->second.bLocked ? TRUE : FALSE;
return FALSE;
}
void CSlotWindow::OnRender()
{
if (m_pBaseImageInstance) {
for (const auto& pair : m_slotMap) {
const SSlot& slot = pair.second;
if (!slot.bRenderBaseSlotImage) continue;
m_pBaseImageInstance->SetPosition(m_rect.left + slot.ixPosition, m_rect.top + slot.iyPosition);
m_pBaseImageInstance->Render();
}
}
for (const auto& pair : m_slotMap) {
const SSlot& slot = pair.second;
if (slot.isItem && slot.pInstance) {
slot.pInstance->SetPosition(m_rect.left + slot.ixPosition, m_rect.top + slot.iyPosition);
slot.pInstance->Render();
}
if (slot.pCoverInstance) {
slot.pCoverInstance->SetPosition(m_rect.left + slot.ixPosition, m_rect.top + slot.iyPosition);
slot.pCoverInstance->Render();
}
}
// Render cooltime overlay & countdown text
CGraphicDevice* pDevice = CGraphicDevice::InstancePtr();
SDL_Renderer* ren = pDevice ? pDevice->GetRenderer() : nullptr;
float curTime = CTimer::Instance().GetCurrentSecond();
for (auto& pair : m_slotMap) {
SSlot& slot = pair.second;
if (slot.fCoolTime > 0.0f) {
float elapsed = curTime - slot.fStartCoolTime;
if (elapsed < slot.fCoolTime) {
if (ren) {
float percent = (curTime - slot.fStartCoolTime) / slot.fCoolTime;
float fRadius = slot.ixCellSize * 0.5f;
float fxCenter = m_rect.left + slot.ixPosition + fRadius;
float fyCenter = m_rect.top + slot.iyPosition + fRadius;
if (percent < 1.0f) {
if (percent < 0.0f) percent = 0.0f;
const int c_iTriangleCountPerBox = 8;
static const SDL_FPoint s_v2BoxPos[c_iTriangleCountPerBox] = {
{ -1.0f, -1.0f },
{ -1.0f, 0.0f },
{ -1.0f, +1.0f },
{ 0.0f, +1.0f },
{ +1.0f, +1.0f },
{ +1.0f, 0.0f },
{ +1.0f, -1.0f },
{ 0.0f, -1.0f },
};
int iTriCount = static_cast<int>(8.0f - 8.0f * percent);
float fLastPercentage = (8.0f - 8.0f * percent) - iTriCount;
std::vector<SDL_Vertex> vertices;
SDL_FColor color{ 0.0f, 0.0f, 0.0f, 0.55f };
vertices.push_back({ { fxCenter, fyCenter }, color, { 0.0f, 0.0f } });
vertices.push_back({ { fxCenter, fyCenter - fRadius }, color, { 0.0f, 0.0f } });
for (int j = 0; j < iTriCount; ++j) {
vertices.push_back({
{ fxCenter + s_v2BoxPos[j].x * fRadius, fyCenter + s_v2BoxPos[j].y * fRadius },
color,
{ 0.0f, 0.0f }
});
}
if (fLastPercentage > 0.0f) {
const SDL_FPoint* pv2LastPos = &s_v2BoxPos[(iTriCount - 1 + 8) % 8];
const SDL_FPoint* pv2Pos = &s_v2BoxPos[(iTriCount + 8) % 8];
float lx = (pv2Pos->x - pv2LastPos->x) * fLastPercentage + pv2LastPos->x;
float ly = (pv2Pos->y - pv2LastPos->y) * fLastPercentage + pv2LastPos->y;
vertices.push_back({
{ fxCenter + lx * fRadius, fyCenter + ly * fRadius },
color,
{ 0.0f, 0.0f }
});
++iTriCount;
}
if (vertices.size() >= 3 && iTriCount > 0) {
std::vector<int> indices;
indices.reserve(iTriCount * 3);
for (int k = 0; k < iTriCount; ++k) {
indices.push_back(0);
indices.push_back(k + 1);
indices.push_back(k + 2);
}
SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND);
SDL_RenderGeometry(ren, nullptr, vertices.data(), static_cast<int>(vertices.size()), indices.data(), static_cast<int>(indices.size()));
}
}
int remainSec = static_cast<int>(std::ceil(slot.fCoolTime - elapsed));
if (remainSec > 0 && slot.ixCellSize >= 24) {
char buf[16];
std::snprintf(buf, sizeof(buf), "%d", remainSec);
SDL_SetRenderScale(ren, 1.0f, 1.0f);
SDL_SetRenderDrawColor(ren, 255, 220, 60, 255);
float tx = static_cast<float>(m_rect.left + slot.ixPosition + (remainSec >= 10 ? 8 : 12));
float ty = static_cast<float>(m_rect.top + slot.iyPosition + 10);
SDL_RenderDebugText(ren, tx, ty, buf);
}
}
} else {
slot.fCoolTime = 0.0f;
slot.fStartCoolTime = 0.0f;
}
}
}
// Render locked slot overlay
if (ren) {
for (const auto& pair : m_slotMap) {
const SSlot& slot = pair.second;
if (slot.bLocked) {
SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(ren, 10, 10, 10, 185);
BYTE wCells = slot.byWidth ? slot.byWidth : 1;
BYTE hCells = slot.byHeight ? slot.byHeight : 1;
SDL_FRect lockRect{
static_cast<float>(m_rect.left + slot.ixPosition),
static_cast<float>(m_rect.top + slot.iyPosition),
static_cast<float>(slot.ixCellSize * wCells),
static_cast<float>(slot.iyCellSize * hCells)
};
SDL_RenderFillRect(ren, &lockRect);
}
}
}
// Render active slot highlight border
if (ren) {
for (const auto& pair : m_slotMap) {
const SSlot& slot = pair.second;
if (slot.bActive) {
SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND);
Uint8 alpha = static_cast<Uint8>(180 + 75 * std::sin(curTime * 6.0f));
SDL_SetRenderDrawColor(ren, 255, 215, 0, alpha);
BYTE wCells = slot.byWidth ? slot.byWidth : 1;
BYTE hCells = slot.byHeight ? slot.byHeight : 1;
SDL_FRect borderRect{
static_cast<float>(m_rect.left + slot.ixPosition - 1),
static_cast<float>(m_rect.top + slot.iyPosition - 1),
static_cast<float>(slot.ixCellSize * wCells + 2),
static_cast<float>(slot.iyCellSize * hCells + 2)
};
SDL_RenderRect(ren, &borderRect);
SDL_FRect innerRect{
static_cast<float>(m_rect.left + slot.ixPosition),
static_cast<float>(m_rect.top + slot.iyPosition),
static_cast<float>(slot.ixCellSize * wCells),
static_cast<float>(slot.iyCellSize * hCells)
};
SDL_RenderRect(ren, &innerRect);
}
}
}
// Render item stack count / skill grade
if (ren) {
for (const auto& pair : m_slotMap) {
const SSlot& slot = pair.second;
char buf[32]{};
if (slot.dwGrade > 0) {
switch (slot.dwGrade) {
case 1: std::snprintf(buf, sizeof(buf), "m%u", (unsigned)slot.dwItemCount); break;
case 2: std::snprintf(buf, sizeof(buf), "g%u", (unsigned)slot.dwItemCount); break;
case 3: std::snprintf(buf, sizeof(buf), "p"); break;
default: std::snprintf(buf, sizeof(buf), "%u", (unsigned)slot.dwItemCount); break;
}
} else if (slot.isItem && slot.dwItemCount > 1) {
std::snprintf(buf, sizeof(buf), "%u", (unsigned)slot.dwItemCount);
}
if (buf[0] != '\0') {
int len = static_cast<int>(std::strlen(buf));
BYTE hCells = slot.byHeight ? slot.byHeight : 1;
float tx = static_cast<float>(m_rect.left + slot.ixPosition + slot.ixCellSize - (len * 8) - 1);
float ty = static_cast<float>(m_rect.top + slot.iyPosition + (slot.iyCellSize * hCells) - 10);
SDL_SetRenderScale(ren, 1.0f, 1.0f);
// Drop shadow
SDL_SetRenderDrawColor(ren, 0, 0, 0, 220);
SDL_RenderDebugText(ren, tx + 1.0f, ty + 1.0f, buf);
// Text color
if (slot.dwGrade == 3) {
SDL_SetRenderDrawColor(ren, 120, 200, 255, 255);
} else if (slot.dwGrade > 0) {
SDL_SetRenderDrawColor(ren, 255, 215, 60, 255);
} else {
SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
}
SDL_RenderDebugText(ren, tx, ty, buf);
}
}
}
CWindow::OnRender();
}
DWORD CGridSlotWindow::Type() { static DWORD s = GetCRC32("CGridSlotWindow", 15); return s; }
BOOL CGridSlotWindow::OnIsType(DWORD t) { return t == Type() || CSlotWindow::OnIsType(t); }
void CGridSlotWindow::ArrangeGridSlot(DWORD dwStartIndex, DWORD dwxCount, DWORD dwyCount, int ixSlotSize, int iySlotSize, int ixTemporarySize, int iyTemporarySize)
{
ClearAllSlot();
for (DWORD y = 0; y < dwyCount; ++y) {
for (DWORD x = 0; x < dwxCount; ++x) {
DWORD dwIndex = dwStartIndex + x + y * dwxCount;
int ixPos = x * (ixSlotSize + ixTemporarySize);
int iyPos = y * (iySlotSize + iyTemporarySize);
AppendSlot(dwIndex, ixPos, iyPos, ixSlotSize, iySlotSize);
}
}
SetSize(dwxCount * (ixSlotSize + ixTemporarySize), dwyCount * (iySlotSize + iyTemporarySize));
}
}