1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 "trigger/scripttrigger.hpp"
18
19#include "editor/editor.hpp"
20#include "supertux/debug.hpp"
21#include "supertux/sector.hpp"
22#include "util/log.hpp"
23#include "util/reader_mapping.hpp"
24#include "video/drawing_context.hpp"
25
26ScriptTrigger::ScriptTrigger(const ReaderMapping& reader) :
27 triggerevent(),
28 script(),
29 new_size(),
30 must_activate(false),
31 oneshot(false),
32 runcount(0)
33{
34 reader.get("x", m_col.m_bbox.get_left());
35 reader.get("y", m_col.m_bbox.get_top());
36 float w = 32, h = 32;
37 reader.get("width", w);
38 reader.get("height", h);
39 m_col.m_bbox.set_size(w, h);
40 new_size.x = w;
41 new_size.y = h;
42 reader.get("script", script);
43 reader.get("button", must_activate);
44 reader.get("oneshot", oneshot);
45 if (script.empty()) {
46 log_warning << "No script set in script trigger" << std::endl;
47 }
48
49 if (must_activate)
50 triggerevent = EVENT_ACTIVATE;
51 else
52 triggerevent = EVENT_TOUCH;
53}
54
55ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script_) :
56 triggerevent(EVENT_TOUCH),
57 script(script_),
58 new_size(),
59 must_activate(),
60 oneshot(false),
61 runcount(0)
62{
63 m_col.m_bbox.set_pos(pos);
64 m_col.m_bbox.set_size(32, 32);
65}
66
67ObjectSettings
68ScriptTrigger::get_settings()
69{
70 new_size.x = m_col.m_bbox.get_width();
71 new_size.y = m_col.m_bbox.get_height();
72
73 ObjectSettings result(_("Script Trigger"));
74
75 result.add_text(_("Name"), &m_name);
76 result.add_float(_("Width"), &new_size.x, "width");
77 result.add_float(_("Height"), &new_size.y, "height");
78 result.add_float(_("X"), &m_col.m_bbox.get_left(), "x", {}, OPTION_HIDDEN);
79 result.add_float(_("Y"), &m_col.m_bbox.get_top(), "y", {}, OPTION_HIDDEN);
80 result.add_script(_("Script"), &script, "script");
81 result.add_bool(_("Button"), &must_activate, "button");
82 result.add_bool(_("Oneshot"), &oneshot, "oneshot", false);
83
84 result.reorder({"script", "button", "width", "height", "x", "y"});
85
86 return result;
87}
88
89void
90ScriptTrigger::after_editor_set() {
91 m_col.m_bbox.set_size(new_size.x, new_size.y);
92 if (must_activate) {
93 triggerevent = EVENT_ACTIVATE;
94 } else {
95 triggerevent = EVENT_TOUCH;
96 }
97}
98
99void
100ScriptTrigger::event(Player& , EventType type)
101{
102 if (type != triggerevent)
103 return;
104
105 if (oneshot && runcount >= 1) {
106 return;
107 }
108
109 Sector::get().run_script(script, "ScriptTrigger");
110 runcount++;
111}
112
113void
114ScriptTrigger::draw(DrawingContext& context)
115{
116 if (Editor::is_active() || g_debug.show_collision_rects) {
117 context.color().draw_filled_rect(m_col.m_bbox, Color(1.0f, 0.0f, 1.0f, 0.6f),
118 0.0f, LAYER_GUI);
119 }
120}
121
122/* EOF */
123