Mercurial > malikania
view libclient/malikania/window.cpp @ 76:858621081b95
Happy new year!
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 01 Jan 2017 13:35:37 +0100 |
parents | d639250fd2a3 |
children |
line wrap: on
line source
/* * window.cpp -- main window and basic drawing * * Copyright (c) 2013-2017 Malikania Authors * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <iostream> #include <stdexcept> #include "color.hpp" #include "frame.hpp" #include "layout.hpp" #include "theme.hpp" #include "widget.hpp" #include "window_backend.hpp" namespace mlk { bool window::is_frame_bound(frame& f, const point& ev_pos) noexcept { auto frame_size = f.size(*this); auto frame_padding = f.padding(); auto frame_pos = f.position(); return ev_pos.x() >= frame_pos.x() + static_cast<int>(frame_padding.width()) && ev_pos.y() >= frame_pos.y() + static_cast<int>(frame_padding.height()) && ev_pos.x() <= frame_pos.x() + static_cast<int>(frame_padding.width() + frame_size.width()) && ev_pos.y() <= frame_pos.y() + static_cast<int>(frame_padding.height() + frame_size.height()); } window::window(unsigned width, unsigned height, const std::string& title) : m_backend(std::make_unique<backend_impl>(*this, width, height, title)) , m_theme(std::make_unique<mlk::theme>()) { } window::window(window&&) noexcept = default; window::~window() noexcept = default; void window::clear() { m_backend->clear(); } void window::present() { m_backend->present(); } void window::close() noexcept { m_is_open = false; m_backend->close(); } color window::drawing_color() const { return m_backend->drawing_color(); } void window::set_drawing_color(const color& color) { m_backend->set_drawing_color(color); } void window::draw_line(const line& line) { m_backend->draw_line(line); } void window::draw_lines(const std::vector<point>& points) { m_backend->draw_lines(points); } void window::draw_point(const point& point) { m_backend->draw_point(point); } void window::draw_points(const std::vector<point>& points) { m_backend->draw_points(points); } void window::draw_rectangle(const rectangle& rectangle) { m_backend->draw_rectangle(rectangle); } void window::draw_rectangles(const std::vector<rectangle>& rectangles) { m_backend->draw_rectangles(rectangles); } void window::draw_frames() { for (const auto& f : m_frames) { f->draw(*this); } } void window::fill_rectangle(const rectangle& rectangle) { m_backend->fill_rectangle(rectangle); } void window::fill_rectangles(const std::vector<rectangle>& rectangles) { m_backend->fill_rectangles(rectangles); } void window::draw_text(const std::string& text, font& font, const rectangle& rectangle) { m_backend->draw_text(text, font, rectangle); } void window::draw_text(const std::string& text, font& font, const point& point) { m_backend->draw_text(text, font, point); } void window::poll() { m_backend->poll(*this); } void window::handle_key_down(const key_event&) { } void window::handle_key_up(const key_event&) { } void window::handle_mouse_down(const mouse_click_event& ev) { for (const auto& f : m_frames) { if (is_frame_bound(*f, ev.pos)) { f->layout()->handle_mouse_down({ ev.button, point(ev.pos.x() - f->position().x(), ev.pos.y() - f->position().y()) }); } } } void window::handle_mouse_up(const mouse_click_event&) { } void window::handle_mouse_wheel(const mouse_wheel_event&) { } void window::handle_quit() { m_is_open = false; } window& window::operator=(window&&) noexcept = default; } // !mlk