changeset 144:67eac3fc099f

Client: add prototype of login state, #712
author David Demelier <markand@malikania.fr>
date Thu, 28 Sep 2017 13:01:45 +0200
parents 7627d614f3e0
children c8510782d40d
files libclient/CMakeLists.txt libclient/malikania/client/client.cpp libclient/malikania/client/state/login_state.cpp libclient/malikania/client/state/login_state.hpp libclient/malikania/client/state/splashscreen_state.cpp libclient/malikania/client/theme.hpp
diffstat 6 files changed, 180 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Thu Sep 28 12:49:27 2017 +0200
+++ b/libclient/CMakeLists.txt	Thu Sep 28 13:01:45 2017 +0200
@@ -40,6 +40,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/client/rectangle.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/sprite.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state.hpp
+    ${libmlk-client_SOURCE_DIR}/malikania/client/state/login_state.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/splashscreen_state.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/theme.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/unique_layout.hpp
@@ -63,6 +64,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/client/image.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/label.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/sprite.cpp
+    ${libmlk-client_SOURCE_DIR}/malikania/client/state/login_state.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/state/splashscreen_state.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/theme.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/client/unique_layout.cpp
--- a/libclient/malikania/client/client.cpp	Thu Sep 28 12:49:27 2017 +0200
+++ b/libclient/malikania/client/client.cpp	Thu Sep 28 13:01:45 2017 +0200
@@ -51,31 +51,49 @@
 void client::handle_key_down(const key_event& ev)
 {
     window_.handle_key_down(ev);
+
+    if (state_)
+        state_->handle_key_down(ev);
 }
 
 void client::handle_key_up(const key_event& ev)
 {
     window_.handle_key_up(ev);
+
+    if (state_)
+        state_->handle_key_up(ev);
 }
 
 void client::handle_mouse_down(const mouse_click_event& ev)
 {
     window_.handle_mouse_down(ev);
+
+    if (state_)
+        state_->handle_mouse_down(ev);
 }
 
 void client::handle_mouse_up(const mouse_click_event& ev)
 {
     window_.handle_mouse_up(ev);
+
+    if (state_)
+        state_->handle_mouse_up(ev);
 }
 
 void client::handle_mouse_wheel(const mouse_wheel_event& ev)
 {
     window_.handle_mouse_wheel(ev);
+
+    if (state_)
+        state_->handle_mouse_wheel(ev);
 }
 
 void client::handle_text(const text_event& ev)
 {
     window_.handle_text(ev);
+
+    if (state_)
+        state_->handle_text(ev);
 }
 
 void client::handle_quit()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/client/state/login_state.cpp	Thu Sep 28 13:01:45 2017 +0200
@@ -0,0 +1,92 @@
+/*
+ * login_state.cpp -- login state
+ *
+ * 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.
+ */
+
+#include "color.hpp"
+#include "client.hpp"
+#include "login_state.hpp"
+#include "theme.hpp"
+#include "unicode.hpp"
+#include "window.hpp"
+
+namespace mlk {
+
+namespace client {
+
+login_state::login_state(client& clt)
+{
+    clt.window().start_edit();
+}
+
+void login_state::handle_text(const text_event& ev)
+{
+    if (index_ == 0)
+        login_ += ev.text;
+    else
+        password_ += ev.text;
+}
+
+void login_state::update(client&)
+{
+}
+
+void login_state::handle_key_down(const key_event& ev)
+{
+    switch (ev.key) {
+    case key::tab:
+        index_ ++;
+
+        if (index_ == 2)
+            index_ = 0;
+        break;
+    case key::backspace:
+        if (index_ == 0)
+            login_.pop_back();
+        else
+            password_.pop_back();
+        break;
+    case key::enter:
+        // TODO: add lobby_state when done.
+        break;
+    default:
+        break;
+    }
+}
+
+void login_state::draw(client& clt)
+{
+    auto& win = clt.window();
+    const auto& font = win.theme().font();
+
+    win.set_drawing_color(mlk::client::color(0xffffffff));
+    win.clear();
+    win.set_drawing_color(mlk::client::color(0xff000000));
+    win.draw_text("Please log in", font, {10, 10});
+    win.draw_text("login: ", font, {10, 50});
+    win.draw_text("password: ", font, {10, 70});
+
+    if (!login_.empty())
+        win.draw_text(unicode::to_utf8(login_), font, {70, 50});
+    if (!password_.empty())
+        win.draw_text(std::string(password_.length(), '*'), font, {70, 70});
+
+    win.present();
+}
+
+} // !client
+
+} // !mlk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/client/state/login_state.hpp	Thu Sep 28 13:01:45 2017 +0200
@@ -0,0 +1,55 @@
+/*
+ * login_state.cpp -- login state
+ *
+ * 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_LOGIN_STATE_HPP
+#define MALIKANIA_CLIENT_LOGIN_STATE_HPP
+
+/**
+ * \file login_state.hpp
+ * \brief Login state.
+ */
+
+#include "state.hpp"
+
+namespace mlk {
+
+namespace client {
+
+/**
+ * \brief Login state.
+ */
+class login_state : public state {
+private:
+    std::size_t index_{0};
+    std::u32string login_;
+    std::u32string password_;
+
+public:
+    login_state(client& clt);
+
+    void update(client&) override;
+    void draw(client& clt) override;
+    void handle_key_down(const key_event& ev) override;
+    void handle_text(const text_event& ev) override;
+};
+
+} // !client
+
+} // !mlk
+
+#endif // !MALIKANIA_CLIENT_LOGIN_STATE_HPP
--- a/libclient/malikania/client/state/splashscreen_state.cpp	Thu Sep 28 12:49:27 2017 +0200
+++ b/libclient/malikania/client/state/splashscreen_state.cpp	Thu Sep 28 13:01:45 2017 +0200
@@ -17,6 +17,7 @@
  */
 
 #include "client.hpp"
+#include "login_state.hpp"
 #include "splashscreen_state.hpp"
 #include "window.hpp"
 
@@ -26,12 +27,12 @@
 
 void splashscreen_state::update(client& clt)
 {
-    (void)clt;
+    if (timer_.elapsed().wall / 1000000LL >= delay_)
+        clt.set_state(std::make_unique<login_state>(clt));
 }
 
 void splashscreen_state::draw(client& clt)
 {
-    // TODO: window.size().
     auto clip = font_.clip(text_);
     auto size = mlk::size(640, 480);
     auto& win = clt.window();
--- a/libclient/malikania/client/theme.hpp	Thu Sep 28 12:49:27 2017 +0200
+++ b/libclient/malikania/client/theme.hpp	Thu Sep 28 13:01:45 2017 +0200
@@ -44,11 +44,11 @@
  */
 class theme {
 private:
-    font m_font;
-    color m_background_color;
-    color m_border_color;
-    color m_text_color;
-    size m_frame_padding;
+    mlk::client::font m_font;
+    mlk::client::color m_background_color;
+    mlk::client::color m_border_color;
+    mlk::client::color m_text_color;
+    mlk::size m_frame_padding;
 
 public:
     /**
@@ -61,6 +61,11 @@
      */
     virtual ~theme() noexcept = default;
 
+    inline const mlk::client::font& font() const noexcept
+    {
+        return m_font;
+    }
+
     inline const color& background_color() const noexcept
     {
         return m_background_color;