1// SuperTux
2// Copyright (C) 2016 Hume2 <teratux.mail@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include <algorithm>
18
19#include "editor/resize_marker.hpp"
20
21ResizeMarker::ResizeMarker(Rectf* rect, Side vert, Side horz) :
22 m_rect(rect),
23 m_vert(vert),
24 m_horz(horz)
25{
26 editor_update();
27}
28
29void
30ResizeMarker::editor_update()
31{
32 refresh_pos();
33}
34
35void
36ResizeMarker::refresh_pos()
37{
38 Vector new_pos;
39
40 switch (m_vert)
41 {
42 case Side::NONE:
43 new_pos.y = (m_rect->get_top() + m_rect->get_bottom())/2 - 8;
44 break;
45
46 case Side::LEFT_UP:
47 new_pos.y = m_rect->get_top() - 16;
48 break;
49
50 case Side::RIGHT_DOWN:
51 new_pos.y = m_rect->get_bottom();
52 break;
53 }
54
55 switch (m_horz)
56 {
57 case Side::NONE:
58 new_pos.x = (m_rect->get_left() + m_rect->get_right())/2 - 8;
59 break;
60
61 case Side::LEFT_UP:
62 new_pos.x = m_rect->get_left() - 16;
63 break;
64
65 case Side::RIGHT_DOWN:
66 new_pos.x = m_rect->get_right();
67 break;
68 }
69
70 set_pos(new_pos);
71}
72
73void
74ResizeMarker::move_to(const Vector& pos)
75{
76 switch (m_vert) {
77 case Side::NONE:
78 break;
79 case Side::LEFT_UP:
80 m_rect->set_top(std::min(pos.y + 16, m_rect->get_bottom() - 2));
81 break;
82 case Side::RIGHT_DOWN:
83 m_rect->set_bottom(std::max(pos.y, m_rect->get_top() + 2));
84 break;
85 }
86
87 switch (m_horz) {
88 case Side::NONE:
89 break;
90 case Side::LEFT_UP:
91 m_rect->set_left(std::min(pos.x + 16, m_rect->get_right() - 2));
92 break;
93 case Side::RIGHT_DOWN:
94 m_rect->set_right(std::max(pos.x, m_rect->get_left() + 2));
95 break;
96 }
97
98 refresh_pos();
99}
100
101Vector
102ResizeMarker::get_point_vector() const
103{
104 Vector result;
105
106 switch (m_vert) {
107 case Side::NONE:
108 result.y = 0;
109 break;
110 case Side::LEFT_UP:
111 result.y = -1;
112 break;
113 case Side::RIGHT_DOWN:
114 result.y = 1;
115 break;
116 }
117
118 switch (m_horz) {
119 case Side::NONE:
120 result.x = 0;
121 break;
122 case Side::LEFT_UP:
123 result.x = -1;
124 break;
125 case Side::RIGHT_DOWN:
126 result.x = 1;
127 break;
128 }
129
130 return result;
131}
132
133Vector
134ResizeMarker::get_offset() const
135{
136 return Vector((m_horz == Side::LEFT_UP) ? 16.0f : 0.0f, (m_vert == Side::LEFT_UP) ? 16.0f : 0.0f);
137}
138
139/* EOF */
140