Advertisement
Vultraz

Untitled

Jun 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 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. #define GETTEXT_DOMAIN "wesnoth-lib"
  15.  
  16. #include "gui/dialogs/command_console.hpp"
  17.  
  18. #include "gui/auxiliary/find_widget.hpp"
  19. #include "gui/dialogs/modal_dialog.hpp"
  20. #include "gui/widgets/label.hpp"
  21. #include "gui/widgets/settings.hpp"
  22. #include "gui/widgets/text_box.hpp"
  23. #include "gui/widgets/window.hpp"
  24.  
  25. namespace gui2
  26. {
  27. namespace dialogs
  28. {
  29. REGISTER_DIALOG(command_console)
  30.  
  31. std::unique_ptr<command_console> command_console::singleton_ = nullptr;
  32.  
  33. command_console::command_console(const std::string& prompt, callback_t callback)
  34.     : input_(nullptr)
  35.     , prompt_(prompt)
  36.     , command_callback_(callback)
  37. {
  38. }
  39.  
  40. void command_console::post_build(window& window)
  41. {
  42.     // Allow ESC to dismiss the console.
  43.     connect_signal_pre_key_press(window,
  44.         std::bind(&command_console::window_key_press_callback, this, _5));
  45.  
  46.     input_ = find_widget<text_box>(&window, "input", false, true);
  47.  
  48.     // Execute provided callback on ENTER press.
  49.     connect_signal_pre_key_press(*input_,
  50.         std::bind(&command_console::input_key_press_callback, this, _5));
  51. }
  52.  
  53. void command_console::pre_show(window& window)
  54. {
  55.     find_widget<label>(&window, "prompt", false).set_label(prompt_);
  56.  
  57.     window.keyboard_capture(input_);
  58. }
  59.  
  60. void command_console::window_key_press_callback(const SDL_Keycode key)
  61. {
  62.     if(key == SDLK_ESCAPE) {
  63.         hide();
  64.     }
  65. }
  66.  
  67. void command_console::input_key_press_callback(const SDL_Keycode key)
  68. {
  69.     if(key == SDLK_RETURN || key == SDLK_KP_ENTER) {
  70.         // Execute callback.
  71.         if(command_callback_ != nullptr) {
  72.             command_callback_(input_->get_value());
  73.         }
  74.  
  75.         // Dismiss console.
  76.         hide();
  77.     }
  78. }
  79.  
  80. } // namespace dialogs
  81. } // namespace gui2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement