changeset 130:f58075b58fa1

Client: create dispatcher class, closes #698 The dispatcher class is the main class that contains several functions to be called to dispatch UI events. It is defined as a class because it has to be passed into C++ source files which can be complicated with templates and our client backend code.
author David Demelier <markand@malikania.fr>
date Wed, 27 Sep 2017 06:37:18 +0200
parents 24ec19139f08
children d51a648d5276
files client/main.cpp libclient/CMakeLists.txt libclient/malikania/client/dispatcher.hpp libclient/malikania/client/window.hpp
diffstat 4 files changed, 172 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/client/main.cpp	Fri Sep 22 13:12:50 2017 +0200
+++ b/client/main.cpp	Wed Sep 27 06:37:18 2017 +0200
@@ -16,19 +16,34 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <malikania/client/connection.hpp>
-#include <malikania/client/client.hpp>
+#include <iostream>
+
+#include <malikania/client/button.hpp>
+#include <malikania/client/color.hpp>
+#include <malikania/client/frame.hpp>
+#include <malikania/client/unique_layout.hpp>
+#include <malikania/client/window.hpp>
 
 int main()
 {
-    boost::asio::io_service service;
+    mlk::client::window w;
+
+    auto b = std::make_unique<mlk::client::button>("Click me!");
 
-    mlk::client::connection cn(service);
-    mlk::client::client client(service, cn);
+    b->on_clicked.connect([] () {
+        std::cout << "clicked successfully!" << std::endl;
+    });
 
-    client.connect("localhost", 3320);
+    auto l = std::make_unique<mlk::client::unique_layout>(std::move(b));
+    auto f = std::make_unique<mlk::client::frame>(std::move(l));
+
+    w.add_frame(std::move(f));
 
-    for (;;) {
-        service.run();
+    while (w.is_open()) {
+        w.poll();
+        w.set_drawing_color(0xffffffff);
+        w.clear();
+        w.draw_frames();
+        w.present();
     }
 }
--- a/libclient/CMakeLists.txt	Fri Sep 22 13:12:50 2017 +0200
+++ b/libclient/CMakeLists.txt	Wed Sep 27 06:37:18 2017 +0200
@@ -26,6 +26,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/client/client.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/color.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/connection.hpp
+    ${libmlk-client_SOURCE_DIR}/malikania/client/dispatcher.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/font.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/frame.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/image.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/client/dispatcher.hpp	Wed Sep 27 06:37:18 2017 +0200
@@ -0,0 +1,147 @@
+/*
+ * dispatcher.hpp -- client event dispatcher
+ *
+ * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
+ *
+ * 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_DISPATCHER_HPP
+#define MALIKANIA_CLIENT_DISPATCHER_HPP
+
+/**
+ * \file dispatcher.hpp
+ * \brief Client event dispatcher.
+ */
+
+#include "mouse.hpp"
+#include "key.hpp"
+#include "point.hpp"
+
+namespace mlk {
+
+namespace client {
+
+/**
+ * \brief Describe key event.
+ */
+class key_event {
+public:
+    mlk::client::key key{key::unknown};         //!< layout-dependant key
+    mlk::client::key scancode{key::unknown};    //!< physical key
+    mlk::client::mod modifiers{mod::none};      //!< optional modifiers
+};
+
+/**
+ * \brief Describe mouse click event.
+ */
+class mouse_click_event {
+public:
+    mouse button{mouse::none};          //!< which mouse button
+    point pos;                          //!< current mouse position
+};
+
+/**
+ * \brief Describe a mouse motion event.
+ */
+class mouse_motion_event {
+public:
+    point pos;                          //!< current mouse position
+};
+
+/**
+ * \brief Describe mouse wheel (up/down) event.
+ */
+class mouse_wheel_event {
+public:
+    point pos;                          //!< current mouse position
+};
+
+/**
+ * \brief Client event dispatcher.
+ */
+class dispatcher {
+public:
+    /**
+     * Default constructor.
+     */
+    dispatcher() noexcept = default;
+
+    /**
+     * Virtual destructor defaulted.
+     */
+    virtual ~dispatcher() noexcept = default;
+
+    /**
+     * Key down event.
+     *
+     * \param ev the event
+     */
+    virtual void handle_key_down(const key_event& ev)
+    {
+        (void)ev;
+    }
+
+    /**
+     * Key released event.
+     *
+     * \param ev the event
+     */
+    virtual void handle_key_up(const key_event& ev)
+    {
+        (void)ev;
+    }
+
+    /**
+     * Mouse click event.
+     *
+     * \param ev the event
+     */
+    virtual void handle_mouse_down(const mouse_click_event& ev)
+    {
+        (void)ev;
+    }
+
+    /**
+     * Mouse click release event.
+     *
+     * \param ev the event
+     */
+    virtual void handle_mouse_up(const mouse_click_event& ev)
+    {
+        (void)ev;
+    }
+
+    /**
+     * Mouse wheel event.
+     *
+     * \param ev the event
+     */
+    virtual void handle_mouse_wheel(const mouse_wheel_event& ev)
+    {
+        (void)ev;
+    }
+
+    /**
+     * Quit request.
+     */
+    virtual void handle_quit()
+    {
+    }
+};
+
+} // !client
+
+} // !mlk
+
+#endif // !MALIKANIA_CLIENT_DISPATCHER_HPP
--- a/libclient/malikania/client/window.hpp	Fri Sep 22 13:12:50 2017 +0200
+++ b/libclient/malikania/client/window.hpp	Wed Sep 27 06:37:18 2017 +0200
@@ -30,6 +30,7 @@
 #include <string>
 #include <unordered_set>
 
+#include "dispatcher.hpp"
 #include "key.hpp"
 #include "mouse.hpp"
 #include "point.hpp"
@@ -46,41 +47,6 @@
 class theme;
 
 /**
- * \brief Describe key event.
- */
-class key_event {
-public:
-    mlk::client::key key{key::unknown};         //!< layout-dependant key
-    mlk::client::key scancode{key::unknown};    //!< physical key
-    mlk::client::mod modifiers{mod::none};      //!< optional modifiers
-};
-
-/**
- * \brief Describe mouse click event.
- */
-class mouse_click_event {
-public:
-    mouse button{mouse::none};          //!< which mouse button
-    point pos;                          //!< current mouse position
-};
-
-/**
- * \brief Describe a mouse motion event.
- */
-class mouse_motion_event {
-public:
-    point pos;                          //!< current mouse position
-};
-
-/**
- * \brief Describe mouse wheel (up/down) event.
- */
-class mouse_wheel_event {
-public:
-    point pos;                          //!< current mouse position
-};
-
-/**
  * \brief Main window class and drawing.
  */
 class window {