# HG changeset patch # User David Demelier # Date 1482149552 -3600 # Node ID 58444cf5f2271e3c619b232b09b8291b792a2250 # Parent fce7e41875db9e81881382399277b716f3e0b67f Client: add minimalist label diff -r fce7e41875db -r 58444cf5f227 client/main.cpp --- a/client/main.cpp Mon Dec 19 12:58:53 2016 +0100 +++ b/client/main.cpp Mon Dec 19 13:12:32 2016 +0100 @@ -24,6 +24,7 @@ #include "malikania/button.hpp" #include "malikania/color.hpp" #include "malikania/frame.hpp" +#include "malikania/label.hpp" #include "malikania/point.hpp" #include "malikania/unique_layout.hpp" #include "malikania/window.hpp" @@ -33,12 +34,9 @@ mlk::window win; auto f = std::make_shared(); - auto b = std::make_shared("click me!"); - auto l = std::make_shared(b); + auto w = std::make_shared("Malikania"); + auto l = std::make_shared(w); - b->on_clicked.connect([] { - puts("clicked!!!!"); - }); f->move({50, 50}); f->set_layout(l); win.add_frame(f); diff -r fce7e41875db -r 58444cf5f227 libclient/CMakeLists.txt --- a/libclient/CMakeLists.txt Mon Dec 19 12:58:53 2016 +0100 +++ b/libclient/CMakeLists.txt Mon Dec 19 13:12:32 2016 +0100 @@ -29,6 +29,7 @@ # ${libmlk-client_SOURCE_DIR}/malikania/grid_layout.hpp ${libmlk-client_SOURCE_DIR}/malikania/image.hpp ${libmlk-client_SOURCE_DIR}/malikania/key.hpp + ${libmlk-client_SOURCE_DIR}/malikania/label.hpp ${libmlk-client_SOURCE_DIR}/malikania/layout.hpp ${libmlk-client_SOURCE_DIR}/malikania/line.hpp ${libmlk-client_SOURCE_DIR}/malikania/mouse.hpp @@ -55,6 +56,7 @@ ${libmlk-client_SOURCE_DIR}/malikania/frame.cpp # ${libmlk-client_SOURCE_DIR}/malikania/grid_layout.cpp ${libmlk-client_SOURCE_DIR}/malikania/image.cpp + ${libmlk-client_SOURCE_DIR}/malikania/label.cpp ${libmlk-client_SOURCE_DIR}/malikania/sprite.cpp ${libmlk-client_SOURCE_DIR}/malikania/theme.cpp ${libmlk-client_SOURCE_DIR}/malikania/unique_layout.cpp diff -r fce7e41875db -r 58444cf5f227 libclient/malikania/label.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/label.cpp Mon Dec 19 13:12:32 2016 +0100 @@ -0,0 +1,41 @@ +/* + * label.cpp -- GUI label element + * + * Copyright (c) 2013-2016 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 "label.hpp" +#include "size.hpp" +#include "theme.hpp" +#include "window.hpp" + +namespace mlk { + +label::label(std::string text) noexcept + : m_text(std::move(text)) +{ +} + +void label::draw(window& w, const rectangle& rect) +{ + w.theme().draw_label(w, *this, rect); +} + +mlk::size label::size(window& w) const noexcept +{ + return w.theme().size_label(*this); +} + +} // !mlk diff -r fce7e41875db -r 58444cf5f227 libclient/malikania/label.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/label.hpp Mon Dec 19 13:12:32 2016 +0100 @@ -0,0 +1,93 @@ +/* + * label.hpp -- GUI label element + * + * Copyright (c) 2013-2016 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. + */ + +#ifndef MALIKANIA_CLIENT_LABEL_HPP +#define MALIKANIA_CLIENT_LABEL_HPP + +/** + * \file label.hp + * \brief GUI label element. + */ + +#include + +#include "widget.hpp" + +namespace mlk { + +/** + * \brief Basic label for displaying test. + */ +class label : public widget { +private: + std::string m_text; + +public: + /** + * Create a label with an optional text. + * + * \param text the text + */ + label(std::string text = "") noexcept; + + /** + * Get the text. + * + * \return the label text + */ + inline const std::string& text() const noexcept + { + return m_text; + } + + /** + * Overloaded function. + * + * \return the text + */ + inline std::string& text() noexcept + { + return m_text; + } + + /** + * Set the label text. + * + * \param text the text + */ + inline void set_text(std::string text) noexcept + { + m_text = std::move(text); + } + + using widget::draw; + + /** + * \copydoc widget::draw + */ + void draw(window& w, const rectangle& rect) override; + + /** + * \copydoc widget::size + */ + mlk::size size(window& w) const noexcept override; +}; + +} // !mlk + +#endif // !MALIKANIA_CLIENT_LABEL_HPP diff -r fce7e41875db -r 58444cf5f227 libclient/malikania/theme.cpp --- a/libclient/malikania/theme.cpp Mon Dec 19 12:58:53 2016 +0100 +++ b/libclient/malikania/theme.cpp Mon Dec 19 13:12:32 2016 +0100 @@ -19,6 +19,7 @@ #include "button.hpp" #include "color.hpp" #include "frame.hpp" +#include "label.hpp" #include "layout.hpp" #include "point.hpp" #include "rectangle.hpp" @@ -52,6 +53,11 @@ }; } +size theme::size_label(const label& l) +{ + return m_font.clip(l.text()); +} + void theme::draw_frame(window& win, const frame &f) { auto layout_size = f.layout()->size(win); @@ -101,4 +107,10 @@ w.draw_text(b.text(), m_font, point{rect.x() + 5, rect.y() + 5}); } +void theme::draw_label(window& w, const label& label, const rectangle& rect) +{ + w.set_drawing_color(m_text_color); + w.draw_text(label.text(), m_font, mlk::point(rect.x(), rect.y())); +} + } // !mlk diff -r fce7e41875db -r 58444cf5f227 libclient/malikania/theme.hpp --- a/libclient/malikania/theme.hpp Mon Dec 19 12:58:53 2016 +0100 +++ b/libclient/malikania/theme.hpp Mon Dec 19 13:12:32 2016 +0100 @@ -32,6 +32,7 @@ class button; class frame; +class label; class rectangle; class point; class window; @@ -82,6 +83,14 @@ virtual size size_button(const button& b); /** + * Get the required bouding rectangle size for this label. + * + * \param l the label + * \return the size + */ + virtual size size_label(const label& l); + + /** * Draw the frame. * * \param w the main window @@ -96,6 +105,15 @@ * \param b the button */ virtual void draw_button(window& w, const button& b, const rectangle& rect); + + /** + * Draw a label. + * + * \param w the main window + * \param l the label + * \param rect the bounding rectangle. + */ + virtual void draw_label(window& w, const label& l, const rectangle& rect); }; } // !mlk