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 "editor/scroller_widget.hpp"
18
19#include <math.h>
20
21#include "editor/editor.hpp"
22#include "video/drawing_context.hpp"
23#include "video/renderer.hpp"
24#include "video/video_system.hpp"
25#include "video/viewport.hpp"
26
27namespace {
28
29const float TOPLEFT = 16;
30const float MIDDLE = 48;
31const float BOTTOMRIGHT = 80;
32const float SIZE = 96;
33
34}
35
36bool EditorScrollerWidget::rendered = true;
37
38EditorScrollerWidget::EditorScrollerWidget(Editor& editor) :
39 m_editor(editor),
40 m_scrolling(),
41 m_scrolling_vec(0, 0),
42 m_mouse_pos(0, 0)
43{
44}
45
46bool
47EditorScrollerWidget::can_scroll() const
48{
49 return m_scrolling && m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE;
50}
51
52void
53EditorScrollerWidget::draw(DrawingContext& context)
54{
55 if (!rendered) return;
56
57 context.color().draw_filled_rect(Rectf(Vector(0, 0), Vector(SIZE, SIZE)),
58 Color(0.9f, 0.9f, 1.0f, 0.6f),
59 MIDDLE, LAYER_GUI-10);
60 context.color().draw_filled_rect(Rectf(Vector(40, 40), Vector(56, 56)),
61 Color(0.9f, 0.9f, 1.0f, 0.6f),
62 8, LAYER_GUI-20);
63 if (can_scroll()) {
64 draw_arrow(context, m_mouse_pos);
65 }
66
67 draw_arrow(context, Vector(TOPLEFT, MIDDLE));
68 draw_arrow(context, Vector(BOTTOMRIGHT, MIDDLE));
69 draw_arrow(context, Vector(MIDDLE, TOPLEFT));
70 draw_arrow(context, Vector(MIDDLE, BOTTOMRIGHT));
71}
72
73void
74EditorScrollerWidget::draw_arrow(DrawingContext& context, const Vector& pos)
75{
76 Vector dir = pos - Vector(MIDDLE, MIDDLE);
77 if (dir.x != 0 || dir.y != 0) {
78 // draw a triangle
79 dir = dir.unit() * 8;
80 Vector dir2 = Vector(-dir.y, dir.x);
81 context.color().draw_triangle(pos + dir, pos - dir + dir2, pos - dir - dir2,
82 Color(1, 1, 1, 0.5), LAYER_GUI-20);
83 }
84}
85
86void
87EditorScrollerWidget::update(float dt_sec)
88{
89 if (!rendered) return;
90 if (!can_scroll()) return;
91
92 m_editor.scroll(m_scrolling_vec * 32.0f * dt_sec);
93}
94
95bool
96EditorScrollerWidget::on_mouse_button_up(const SDL_MouseButtonEvent& button)
97{
98 m_scrolling = false;
99 return false;
100}
101
102bool
103EditorScrollerWidget::on_mouse_button_down(const SDL_MouseButtonEvent& button)
104{
105 if (button.button == SDL_BUTTON_LEFT) {
106 if (!rendered) return false;
107
108 if (m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE) {
109 m_scrolling = true;
110 return true;
111 } else {
112 return false;
113 }
114 } else {
115 return false;
116 }
117}
118
119bool
120EditorScrollerWidget::on_mouse_motion(const SDL_MouseMotionEvent& motion)
121{
122 if (!rendered) return false;
123
124 m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y);
125 if (m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE) {
126 m_scrolling_vec = m_mouse_pos - Vector(MIDDLE, MIDDLE);
127 if (m_scrolling_vec.x != 0 || m_scrolling_vec.y != 0) {
128 float norm = m_scrolling_vec.norm();
129 m_scrolling_vec *= powf(static_cast<float>(M_E), norm / 16.0f - 1.0f);
130 }
131 }
132 return false;
133}
134
135bool
136EditorScrollerWidget::on_key_down(const SDL_KeyboardEvent& key)
137{
138 if (key.keysym.sym == SDLK_F9) {
139 rendered = !rendered;
140 }
141 return false;
142}
143
144/* EOF */
145