-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJewelDrag.cpp
113 lines (103 loc) · 2.78 KB
/
JewelDrag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**************************************************************************
** Qt Creator license header template
** Special keywords: dpinol 03/03/2014 2014
** Environment variables:
** To protect a percent sign, use '%'.
**************************************************************************/
#include "JewelDrag.h"
#include "JewelObject.h"
#include "JewelBoard.h"
#include <utils/log.h>
#include <utils/Effect.h>
#include "InputHandler.h"
JewelDrag::JewelDrag(JewelBoard &board)
:m_board(board),
m_modelSwap(board.getModel()),
m_swapEffect(NULL)
{
}
void JewelDrag::update()
{
}
bool JewelDrag::isSwapping() const
{
return m_toPos.isValid();
}
void JewelDrag::drag()
{
Vector2D const &pMousePos = TheInputHandler::Instance()->getMousePosition();
if (isSwapping())
return;
if (!m_fromPixel.isValid())
{
//drag start
BoardPos pos = m_board.getJewelAt(pMousePos);
if (pos.isValid())
{
m_fromPixel = pMousePos;
m_fromPos = pos;
}
}
else
{
//move jewel only vertical or horizontal
Vector2D shift = pMousePos - m_fromPixel;
BoardPos shiftPos;
int RATIO = 3;
if (shift.getX() > JewelObject::WIDTH / RATIO)
shiftPos = BoardPos(1,0);
else if (shift.getX() < -JewelObject::WIDTH / RATIO)
shiftPos = BoardPos(-1,0);
else if (shift.getY() > JewelObject::HEIGHT / RATIO)
shiftPos = BoardPos(0, 1);
else if (shift.getY() < -JewelObject::HEIGHT / RATIO)
shiftPos = BoardPos(0, -1);
else
{
LOG_DEBUG("shift too small: " << shift);
return;
}
LOG_DEBUG("shift: " << shift);
m_toPos = m_fromPos + shiftPos;
if (m_toPos.isValid())
{
m_modelSwap.setPositions(m_fromPos, m_toPos);
bool valid = m_modelSwap.isValid();
LOG_INFO("Going to swap " << m_fromPos << " to " << m_toPos);
m_board.getJewel(m_fromPos).swapWith(shiftPos, !valid);
m_swapEffect = &m_board.getJewel(m_toPos).swapWith(-shiftPos, !valid);
if (valid)
{
m_swapEffect->setNext([&]()
{
m_modelSwap.run();
m_swapEffect->setNext(NULL);
});
}
}
else
LOG_DEBUG("to_pos not valid: " << m_toPos);
}
}
void JewelDrag::drop()
{
if (m_fromPixel.isValid())
{
LOG_INFO("fromPixel clear");
m_fromPixel.clear();
m_fromPos.clear();
m_toPos.clear();
}
/*
Vector2D const &pMousePos = TheInputHandler::Instance()->getMousePosition();
if (m_dragging.isValid() && !TheInputHandler::Instance()->getMouseButtonState(LEFT))
{
BoardPos const dropping = getJewelAt(pMousePos);
if (dropping.isValid() && (m_dragging - dropping).isDirection())
swap(m_dragging, dropping);
else
LOG_DEBUG("Swap not valid because dropping outside grid or too far");
}
m_dragging = BoardPos();
*/
}