changeset 191:bfc6b9c9081a

Client: widget store position and size
author David Demelier <markand@malikania.fr>
date Fri, 26 Oct 2018 21:07:03 +0200
parents c7ad3adc7305
children 74afc5a41c83
files client/main.cpp libmlk-client/malikania/client/button.cpp libmlk-client/malikania/client/button.hpp libmlk-client/malikania/client/label.cpp libmlk-client/malikania/client/label.hpp libmlk-client/malikania/client/theme.hpp libmlk-client/malikania/client/widget.cpp libmlk-client/malikania/client/widget.hpp
diffstat 8 files changed, 88 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/client/main.cpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/client/main.cpp	Fri Oct 26 21:07:03 2018 +0200
@@ -17,14 +17,34 @@
  */
 
 #include <iostream>
+#include <chrono>
+#include <thread>
 
-#include <malikania/client/client.hpp>
-#include <malikania/client/connection.hpp>
-#include <malikania/client/state/login_state.hpp>
+#include <malikania/client/color.hpp>
+#include <malikania/client/button.hpp>
 #include <malikania/client/window.hpp>
 
+using namespace mlk;
+using namespace mlk::client;
+
 int main()
 {
+	window w;
+
+	//SDL_RenderSetScale(w.get_renderer(), 2, 2);
+	button b("click me");
+
+	b.set_position({50, 50});
+	b.set_size({100, 20});
+
+	w.set_drawing_color(color::from_name("white"));
+	w.clear();
+	b.draw(w);
+	w.present();
+	std::this_thread::sleep_for(std::chrono::seconds(10));
+
+
+#if 0
 	boost::asio::io_service service;
 
 	mlk::client::connection conn(service);
@@ -33,4 +53,5 @@
 
 	clt.set_state(std::make_unique<mlk::client::login_state>(clt));
 	clt.run();
+#endif
 }
--- a/libmlk-client/malikania/client/button.cpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/button.cpp	Fri Oct 26 21:07:03 2018 +0200
@@ -16,6 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <malikania/rectangle.hpp>
+
 #include "button.hpp"
 #include "theme.hpp"
 #include "window.hpp"
@@ -45,10 +47,24 @@
 	// TODO: implement release.
 }
 
-void button::draw(window& w, const size& size)
+void button::draw(window& w)
 {
-	(void)w;
-	(void)size;
+	const auto& font = get_theme().get_font();
+	const auto textsize = font.clip(text_);
+
+	w.set_drawing_color({128, 68, 21});
+	w.draw_rectangle({position_.x - 3, position_.y - 3, size_.width + 6, size_.height + 6});
+	w.set_drawing_color({85, 38, 0});
+	w.draw_rectangle({position_.x - 2, position_.y - 2, size_.width + 4, size_.height + 4});
+	w.set_drawing_color({128, 68, 21});
+	w.draw_rectangle({position_.x - 1, position_.y - 1, size_.width + 2, size_.height + 2});
+	w.set_drawing_color({255, 208, 170});
+	w.fill_rectangle({position_.x, position_.y, size_.width, size_.height});
+	w.set_drawing_color(color::from_name("black"));
+	w.draw_text(text_, font, point{
+		position_.x + (size_.width / 2) - (textsize.width / 2),
+		position_.y + (size_.height / 2) - (textsize.height / 2)
+	});
 }
 
 } // !mlk::client
--- a/libmlk-client/malikania/client/button.hpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/button.hpp	Fri Oct 26 21:07:03 2018 +0200
@@ -80,7 +80,7 @@
 	/**
 	 * \copydoc widget::draw
 	 */
-	void draw(window& w, const size& size) override;
+	void draw(window& w) override;
 };
 
 } // !mlk::client
--- a/libmlk-client/malikania/client/label.cpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/label.cpp	Fri Oct 26 21:07:03 2018 +0200
@@ -39,11 +39,10 @@
 	text_ = std::move(text);
 }
 
-void label::draw(window& w, const size& size)
+void label::draw(window& w)
 {
 	// TODO: implement.
 	(void)w;
-	(void)size;
 }
 
 } // !mlk::client
--- a/libmlk-client/malikania/client/label.hpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/label.hpp	Fri Oct 26 21:07:03 2018 +0200
@@ -62,7 +62,7 @@
 	/**
 	 * \copydoc widget::draw
 	 */
-	void draw(window& w, const size& size) override;
+	void draw(window& w) override;
 };
 
 } // !mlk::client
--- a/libmlk-client/malikania/client/theme.hpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/theme.hpp	Fri Oct 26 21:07:03 2018 +0200
@@ -54,8 +54,18 @@
 	color text_color_;
 
 public:
+	/**
+	 * Get the default theme for whole game.
+	 *
+	 * \return the theme
+	 */
 	static auto get_default() noexcept -> theme&;
 
+	/**
+	 * Set the default theme.
+	 *
+	 * \param theme the theme
+	 */
 	static void set_default(std::unique_ptr<theme> theme) noexcept;
 
 	/**
--- a/libmlk-client/malikania/client/widget.cpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/widget.cpp	Fri Oct 26 21:07:03 2018 +0200
@@ -36,14 +36,24 @@
 	position_ = std::move(position);
 }
 
+auto widget::get_size() const noexcept -> const size&
+{
+	return size_;
+}
+
+void widget::set_size(size size) noexcept
+{
+	size_ = std::move(size);
+}
+
 auto widget::get_theme() const noexcept -> const theme&
 {
-	return *theme_;
+	return theme_ ? *theme_ : theme::get_default();
 }
 
 auto widget::get_theme() noexcept -> theme&
 {
-	return *theme_;
+	return theme_ ? *theme_ : theme::get_default();
 }
 
 void widget::set_theme(theme& theme) noexcept
--- a/libmlk-client/malikania/client/widget.hpp	Fri Oct 26 21:06:39 2018 +0200
+++ b/libmlk-client/malikania/client/widget.hpp	Fri Oct 26 21:07:03 2018 +0200
@@ -25,14 +25,12 @@
  */
 
 #include <malikania/point.hpp>
+#include <malikania/size.hpp>
 
 #include "dispatcher.hpp"
 
 namespace mlk {
 
-struct rectangle;
-struct size;
-
 namespace client {
 
 class window;
@@ -45,6 +43,7 @@
 protected:
 	theme* theme_;                  //!< widget theme
 	point position_;                //!< widget position
+	size size_;                     //!< widget size
 
 public:
 	/**
@@ -74,8 +73,25 @@
 	void set_position(point position) noexcept;
 
 	/**
+	 * Get the widget size.
+	 *
+	 * \return the size
+	 */
+	auto get_size() const noexcept -> const size&;
+
+	/**
+	 * Set the widget position.
+	 *
+	 * \param size the new size
+	 */
+	void set_size(size size) noexcept;
+
+	/**
 	 * Get the theme.
 	 *
+	 * If no theme is provided in this widget, theme::get_default is
+	 * returned instead.
+	 *
 	 * \return the theme
 	 */
 	auto get_theme() const noexcept -> const theme&;
@@ -98,9 +114,8 @@
 	 * Draw the widget at the specified position.
 	 *
 	 * \param w the window
-	 * \param size the maximum size allowed
 	 */
-	virtual void draw(window& w, const size& size) = 0;
+	virtual void draw(window& w) = 0;
 };
 
 } // !client