changeset 139:b80d37e71b87

Client: rework dispatching between client and window, closes #711 The window backend is able to produce user events but they need to be dispatched into the client, the state and the window. This change makes client owner of window and dispatches events from client to the window and the future state mechanism. Remove the client network code temporarily to rework in the dispatcher later.
author David Demelier <markand@malikania.fr>
date Wed, 27 Sep 2017 20:34:59 +0200
parents 532f259557dd
children a83fff870983
files client/main.cpp libclient/malikania/client/backend/sdl/window_backend.cpp libclient/malikania/client/backend/sdl/window_backend.hpp libclient/malikania/client/client.cpp libclient/malikania/client/client.hpp libclient/malikania/client/connection.cpp libclient/malikania/client/dispatcher.hpp libclient/malikania/client/state.hpp libclient/malikania/client/window.cpp libclient/malikania/client/window.hpp
diffstat 10 files changed, 140 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/client/main.cpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/client/main.cpp	Wed Sep 27 20:34:59 2017 +0200
@@ -18,33 +18,17 @@
 
 #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/client.hpp>
+#include <malikania/client/connection.hpp>
 #include <malikania/client/window.hpp>
 
 int main()
 {
-    mlk::client::window w;
-
-    auto b = std::make_unique<mlk::client::button>("Click me!");
-
-    b->on_clicked.connect([] () {
-        std::cout << "clicked successfully!" << std::endl;
-    });
-
-    auto l = std::make_unique<mlk::client::unique_layout>(std::move(b));
-    auto f = std::make_unique<mlk::client::frame>(std::move(l));
+    boost::asio::io_service service;
 
-    w.add_frame(std::move(f));
-    w.start_edit();
+    mlk::client::connection conn(service);
+    mlk::client::window w;
+    mlk::client::client clt(service, conn, w);
 
-    while (w.is_open()) {
-        w.poll();
-        w.set_drawing_color(0xffffffff);
-        w.clear();
-        w.draw_frames();
-        w.present();
-    }
+    clt.run();
 }
--- a/libclient/malikania/client/backend/sdl/window_backend.cpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/backend/sdl/window_backend.cpp	Wed Sep 27 20:34:59 2017 +0200
@@ -333,7 +333,7 @@
 
 } // !namespace
 
-void window::backend_impl::handle_key(window& self, const SDL_Event& ev)
+void window::backend_impl::dispatch_key_event(dispatcher& dp, const SDL_Event& ev)
 {
     assert(ev.type == SDL_KEYDOWN || ev.type == SDL_KEYUP);
 
@@ -352,12 +352,12 @@
             kev.modifiers |= pair.second;
 
     if (ev.type == SDL_KEYDOWN)
-        self.handle_key_down(kev);
+        dp.handle_key_down(kev);
     else
-        self.handle_key_up(kev);
+        dp.handle_key_up(kev);
 }
 
-void window::backend_impl::handle_mouse(window& self, const SDL_Event& ev)
+void window::backend_impl::dispatch_mouse_event(dispatcher& dp, const SDL_Event& ev)
 {
     assert(ev.type == SDL_MOUSEBUTTONDOWN || ev.type == SDL_MOUSEBUTTONUP);
 
@@ -372,25 +372,23 @@
     };
 
     if (ev.type == SDL_MOUSEBUTTONDOWN)
-        self.handle_mouse_down(mev);
+        dp.handle_mouse_down(mev);
     else
-        self.handle_mouse_up(mev);
+        dp.handle_mouse_up(mev);
 }
 
-void window::backend_impl::handle_mouse_wheel(window& self, const SDL_Event& ev)
+void window::backend_impl::dispatch_mouse_wheel_event(dispatcher& dp, const SDL_Event& ev)
 {
     assert(ev.type == SDL_MOUSEWHEEL);
 
-    mouse_wheel_event wev{{ev.wheel.x, ev.wheel.y}};
-
-    self.handle_mouse_wheel(wev);
+    dp.handle_mouse_wheel({{ev.wheel.x, ev.wheel.y}});
 }
 
-void window::backend_impl::handle_text(window& self, const SDL_Event& ev)
+void window::backend_impl::dispatch_text_event(dispatcher& dp, const SDL_Event& ev)
 {
     assert(ev.type == SDL_TEXTINPUT);
 
-    self.handle_text_event({unicode::to_utf32(ev.text.text)});
+    dp.handle_text({unicode::to_utf32(ev.text.text)});
 }
 
 window::backend_impl::backend_impl(window&, unsigned width, unsigned height, const std::string& title)
@@ -583,7 +581,7 @@
     SDL_StopTextInput();
 }
 
-void window::backend_impl::poll(window& self)
+void window::backend_impl::poll(dispatcher& dp)
 {
     SDL_Event event;
 
@@ -591,20 +589,20 @@
         switch (event.type) {
         case SDL_KEYUP:
         case SDL_KEYDOWN:
-            handle_key(self, event);
+            dispatch_key_event(dp, event);
             break;
         case SDL_MOUSEBUTTONDOWN:
         case SDL_MOUSEBUTTONUP:
-            handle_mouse(self, event);
+            dispatch_mouse_event(dp, event);
             break;
         case SDL_MOUSEWHEEL:
-            handle_mouse_wheel(self, event);
+            dispatch_mouse_wheel_event(dp, event);
             break;
         case SDL_TEXTINPUT:
-            handle_text(self, event);
+            dispatch_text_event(dp, event);
             break;
         case SDL_QUIT:
-            self.handle_quit();
+            dp.handle_quit();
             break;
         default:
             break;
--- a/libclient/malikania/client/backend/sdl/window_backend.hpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/backend/sdl/window_backend.hpp	Wed Sep 27 20:34:59 2017 +0200
@@ -35,10 +35,10 @@
     std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> window_;
     std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer *)> renderer_;
 
-    void handle_key(window&, const SDL_Event&);
-    void handle_mouse(window&, const SDL_Event&);
-    void handle_mouse_wheel(window&, const SDL_Event&);
-    void handle_text(window&, const SDL_Event&);
+    void dispatch_key_event(dispatcher&, const SDL_Event&);
+    void dispatch_mouse_event(dispatcher&, const SDL_Event&);
+    void dispatch_mouse_wheel_event(dispatcher&, const SDL_Event&);
+    void dispatch_text_event(dispatcher&, const SDL_Event&);
 
 public:
     backend_impl(window& self, unsigned width, unsigned height, const std::string& title);
@@ -82,7 +82,7 @@
 
     void draw_text(const std::string& text, font& font, const point& point);
 
-    void poll(window &self);
+    void poll(dispatcher& dp);
 };
 
 } // !client
--- a/libclient/malikania/client/client.cpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/client.cpp	Wed Sep 27 20:34:59 2017 +0200
@@ -21,50 +21,74 @@
 #include "client.hpp"
 #include "connection.hpp"
 #include "state.hpp"
+#include "window.hpp"
 
 namespace mlk {
 
 namespace client {
 
-client::client(boost::asio::io_service& service, mlk::client::connection& connection) noexcept
+client::client(boost::asio::io_service& service,
+               mlk::client::connection& connection,
+               mlk::client::window& window) noexcept
     : service_(service)
     , connection_(connection)
+    , window_(window)
 {
 }
 
 client::~client() noexcept = default;
 
-void client::handle_disconnect()
-{
-    // TODO: add error
-    std::cout << "client::handle_disconnect\n";
-}
-
-void client::handle_error(boost::system::error_code)
-{
-    // TODO: add error
-    std::cout << "client::handle_error\n";
-}
-
-void client::handle_connect(boost::system::error_code)
-{
-    connection_.do_read(*this);
-}
-
-void client::handle_read(boost::system::error_code, nlohmann::json msg)
-{
-    connection_.do_read(*this);
-
-    if (state_) {
-        state_->handle_message(*this, std::move(msg));
-    }
-}
-
 void client::connect(std::string host, uint16_t port)
 {
     connection_.do_connect(*this, std::move(host), port);
 }
 
+void client::handle_key_down(const key_event& ev)
+{
+    window_.handle_key_down(ev);
+}
+
+void client::handle_key_up(const key_event& ev)
+{
+    window_.handle_key_up(ev);
+}
+
+void client::handle_mouse_down(const mouse_click_event& ev)
+{
+    window_.handle_mouse_down(ev);
+}
+
+void client::handle_mouse_up(const mouse_click_event& ev)
+{
+    window_.handle_mouse_up(ev);
+}
+
+void client::handle_mouse_wheel(const mouse_wheel_event& ev)
+{
+    window_.handle_mouse_wheel(ev);
+}
+
+void client::handle_text(const text_event& ev)
+{
+    window_.handle_text(ev);
+}
+
+void client::handle_quit()
+{
+    window_.handle_quit();
+}
+
+void client::run()
+{
+    while (window_.is_open())
+        run_one();
+}
+
+void client::run_one()
+{
+    window_.poll(*this);
+}
+
 } // !client
 
 } // !mlk
--- a/libclient/malikania/client/client.hpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/client.hpp	Wed Sep 27 20:34:59 2017 +0200
@@ -30,20 +30,24 @@
 
 #include <json.hpp>
 
+#include "dispatcher.hpp"
+
 namespace mlk {
 
 namespace client {
 
 class connection;
 class state;
+class window;
 
 /**
  * \brief Main client game class
  */
-class client {
+class client : public dispatcher {
 private:
     boost::asio::io_service& service_;
     mlk::client::connection& connection_;
+    mlk::client::window& window_;
     std::unique_ptr<state> state_;
 
 public:
@@ -51,9 +55,12 @@
      * Construct the client, no connection attempt will be made yet.
      *
      * \param service the I/O service
-     * \param the connection object
+     * \param connection the connection object
+     * \param window the window
      */
-    client(boost::asio::io_service& service, mlk::client::connection& connection) noexcept;
+    client(boost::asio::io_service& service,
+           mlk::client::connection& connection,
+           mlk::client::window& window) noexcept;
 
     /**
      * Virtual destructor defaulted.
@@ -91,32 +98,49 @@
     void connect(std::string host, std::uint16_t port);
 
     /**
-     * Handle disconnection, usually called when a read/send operation returned
-     * 0 bytes.
+     * Run the client until the game window is closed.
      */
-    virtual void handle_disconnect();
+    void run();
 
     /**
-     * Handle unexpected network error.
-     *
-     * \param code the error code
+     * Same as run except that it does not block.
+     */
+    void run_one();
+
+    /**
+     * \copydoc dispatcher::handle_key_down
      */
-    virtual void handle_error(boost::system::error_code code);
+    void handle_key_down(const key_event& ev) override;
+
+    /**
+     * \copydoc dispatcher::handle_key_up
+     */
+    void handle_key_up(const key_event& ev) override;
 
     /**
-     * Handle connection.
-     *
-     * \param code the possible error code
+     * \copydoc dispatcher::handle_mouse_down
      */
-    virtual void handle_connect(boost::system::error_code code);
+    void handle_mouse_down(const mouse_click_event& ev) override;
+
+    /**
+     * \copydoc dispatcher::handle_mouse_up
+     */
+    void handle_mouse_up(const mouse_click_event& ev) override;
 
     /**
-     * Handle reading operation.
-     *
-     * \param code the possible error code
-     * \param msg the received message
+     * \copydoc dispatcher::handle_mouse_wheel
+     */
+    void handle_mouse_wheel(const mouse_wheel_event& ev) override;
+
+    /**
+     * \copydoc dispatcher::handle_text
      */
-    virtual void handle_read(boost::system::error_code, nlohmann::json msg);
+    void handle_text(const text_event& ev) override;
+
+    /**
+     * \copydoc dispatcher::handle_quit
+     */
+    void handle_quit() override;
 };
 
 } // !client
--- a/libclient/malikania/client/connection.cpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/connection.cpp	Wed Sep 27 20:34:59 2017 +0200
@@ -34,6 +34,7 @@
 
 void connection::handshake(client& client)
 {
+#if 0
     socket_.async_handshake(boost::asio::ssl::stream_base::client, [this, &client] (auto code) {
         if (code) {
             client.handle_error(code);
@@ -41,10 +42,12 @@
             client.handle_connect(code);
         }
     });
+#endif
 }
 
 void connection::do_connect(client& client, std::string host, std::uint16_t port)
 {
+#if 0
     using tcp = boost::asio::ip::tcp;
 
     auto resolver = std::make_shared<tcp::resolver>(service_);
@@ -63,10 +66,12 @@
             });
         }
     });
+#endif
 }
 
 void connection::flush(client& client)
 {
+#if 0
     auto buffer = boost::asio::buffer(output_.front().data(), output_.front().length());
 
     boost::asio::async_write(socket_, buffer, [this, &client] (auto code, auto xfer) {
@@ -80,10 +85,12 @@
             this->flush(client);
         }
     });
+#endif
 }
 
 void connection::do_send(client& client, nlohmann::json message)
 {
+#if 0
     assert(message.is_object());
 
     auto in_progress = !output_.empty();
@@ -93,10 +100,12 @@
     if (!in_progress) {
         flush(client);
     }
+#endif
 }
 
 void connection::do_read(client& client)
 {
+#if 0
 #if !defined(NDEBUG)
     assert(!is_reading);
 
@@ -126,6 +135,7 @@
             }
         }
     });
+#endif
 }
 
 } // !client
--- a/libclient/malikania/client/dispatcher.hpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/dispatcher.hpp	Wed Sep 27 20:34:59 2017 +0200
@@ -145,7 +145,7 @@
      *
      * \param ev the event
      */
-    virtual void handle_text_event(const text_event& ev)
+    virtual void handle_text(const text_event& ev)
     {
         (void)ev;
     }
--- a/libclient/malikania/client/state.hpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/state.hpp	Wed Sep 27 20:34:59 2017 +0200
@@ -24,7 +24,7 @@
  * \brief Game client state.
  */
 
-#include <memory>
+#include "dispatcher.hpp"
 
 namespace mlk {
 
@@ -35,7 +35,7 @@
 /**
  * \brief Game client state.
  */
-class state {
+class state : public dispatcher {
 public:
     /**
      * Default constructor.
@@ -46,18 +46,6 @@
      * Virtual destructor defaulted.
      */
     virtual ~state() noexcept = default;
-
-    /**
-     * Handle a network message.
-     *
-     * \param client the client
-     * \param msg the network message
-     */
-    virtual void handle_message(client& client, nlohmann::json msg)
-    {
-        (void)client;
-        (void)msg;
-    }
 };
 
 } // !client
--- a/libclient/malikania/client/window.cpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/window.cpp	Wed Sep 27 20:34:59 2017 +0200
@@ -151,9 +151,9 @@
     backend_->draw_text(text, font, point);
 }
 
-void window::poll()
+void window::poll(mlk::client::dispatcher& dp)
 {
-    backend_->poll(*this);
+    backend_->poll(dp);
 }
 
 void window::handle_key_down(const key_event&)
--- a/libclient/malikania/client/window.hpp	Fri Oct 06 16:30:24 2017 +0200
+++ b/libclient/malikania/client/window.hpp	Wed Sep 27 20:34:59 2017 +0200
@@ -279,8 +279,10 @@
 
     /**
      * Poll all pending events and call appropriate functions.
+     *
+     * \param dispatcher the event dispatcher
      */
-    void poll();
+    void poll(dispatcher& dp);
 
     /**
      * \copydoc dispatcher::handle_key_down