Vultraz

Untitled

Jun 22nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. /*
  2.    Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY.
  10.  
  11.    See the COPYING file for more details.
  12. */
  13.  
  14. #pragma once
  15.  
  16. #include "gui/dialogs/modeless_dialog.hpp"
  17.  
  18. #include <SDL_keycode.h>
  19.  
  20. #include <functional>
  21. #include <memory>
  22.  
  23. namespace gui2
  24. {
  25. class text_box;
  26.  
  27. namespace dialogs
  28. {
  29.  
  30. /**
  31.  * A simple command console class.
  32.  *
  33.  * This class doesn't explicitly handle any processing of commands itself. Instead, it consists
  34.  * of a singleton modeless dialog that can be invoked from any area of the game using the static
  35.  * @display function. A callback function should be passed in that will handle processing of the
  36.  * command input.
  37.  *
  38.  * @todo This dialog's design definitely needs to be improved (multiline input area, for one).
  39.  * Also might be worth looking if more actual command handling could be merged in here.
  40.  */
  41. class command_console : public modeless_dialog
  42. {
  43. private:
  44.     /** Callback function signature. */
  45.     using callback_t = std::function<void(std::string)>;
  46.  
  47. public:
  48.     command_console(const command_console&) = delete;
  49.     command_console& operator=(const command_console&) = delete;
  50.  
  51.     command_console(const std::string& prompt, callback_t callback);
  52.  
  53.     ~command_console()
  54.     {
  55.         singleton_ = nullptr;
  56.     }
  57.  
  58.     //enum CONSOLE_MODE { MODE_NONE, MODE_SEARCH, MODE_MESSAGE, MODE_COMMAND, MODE_AI };
  59.  
  60.     /**
  61.      * Shows the command console. This resets the singleton instance and displays the
  62.      * dialog until dismissed by user action.
  63.      *
  64.      * Since this is a modeless dialog, we can't use the usual method of creating a temporary
  65.      * dialog object here. Modal dialogs have a display loop that keeps that open; modeless
  66.      * dialogs, however, don't, meaning the object would immediately be destroyed and hidden.
  67.      */
  68.     static void display(CVideo& video, const std::string& prompt, callback_t callback = nullptr)
  69.     {
  70.         singleton_.reset(new command_console(prompt, callback));
  71.         singleton_->show(video, true);
  72.     }
  73.  
  74. private:
  75.     /** Inherited from modeless_dialog, implemented by REGISTER_DIALOG. */
  76.     virtual const std::string& window_id() const override;
  77.  
  78.     /** Inherited from modeless_dialog. */
  79.     virtual void post_build(window& window) override;
  80.  
  81.     /** Inherited from modeless_dialog. */
  82.     virtual void pre_show(window& window) override;
  83.  
  84.     void window_key_press_callback(const SDL_Keycode key);
  85.  
  86.     void input_key_press_callback(const SDL_Keycode key);
  87.  
  88.     /** The input text box. */
  89.     text_box* input_;
  90.  
  91.     /** Text action prompt. */
  92.     std::string prompt_;
  93.  
  94.     /** The callback to process the inputted command. */
  95.     callback_t command_callback_;
  96.  
  97.     /** Dialog singleton. */
  98.     static std::unique_ptr<command_console> singleton_;
  99. };
  100.  
  101. } // namespace dialogs
  102. } // namespace gui2
Advertisement
Add Comment
Please, Sign In to add comment