| 1 | // SuperTux |
| 2 | // Copyright (C) 2015 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/object_icon.hpp" |
| 18 | |
| 19 | #include "math/rectf.hpp" |
| 20 | #include "util/reader_mapping.hpp" |
| 21 | #include "video/surface.hpp" |
| 22 | #include "video/drawing_context.hpp" |
| 23 | |
| 24 | ObjectIcon::ObjectIcon(const std::string& object_class, const std::string& icon) : |
| 25 | m_object_class(object_class), |
| 26 | m_surface(Surface::from_file(icon)), |
| 27 | m_offset() |
| 28 | { |
| 29 | calculate_offset(); |
| 30 | } |
| 31 | |
| 32 | ObjectIcon::ObjectIcon(const ReaderMapping& reader) : |
| 33 | m_object_class(), |
| 34 | m_surface(), |
| 35 | m_offset() |
| 36 | { |
| 37 | std::string icon = "images/engine/icons/supertux.png" ; |
| 38 | reader.get("class" , m_object_class); |
| 39 | reader.get("icon" , icon); |
| 40 | m_surface = Surface::from_file(icon); |
| 41 | calculate_offset(); |
| 42 | } |
| 43 | |
| 44 | ObjectIcon::~ObjectIcon() |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ObjectIcon::calculate_offset() |
| 51 | { |
| 52 | float w = static_cast<float>(m_surface->get_width()); |
| 53 | float h = static_cast<float>(m_surface->get_height()); |
| 54 | |
| 55 | if (w > h) { |
| 56 | m_offset.x = 0; |
| 57 | m_offset.y = 32/w * (w - h) / 2; |
| 58 | } else { |
| 59 | m_offset.y = 0; |
| 60 | m_offset.x = 32/h * (h - w) / 2; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | ObjectIcon::draw(DrawingContext& context, const Vector& pos) |
| 66 | { |
| 67 | context.color().draw_surface_scaled(m_surface, |
| 68 | Rectf(pos + m_offset, pos + Vector(32,32) - m_offset), LAYER_GUI - 9); |
| 69 | } |
| 70 | |
| 71 | /* EOF */ |
| 72 | |