EventHandlerOptions.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "EventHandlerOptions.h"
  28. #include <Rocket/Core/ElementDocument.h>
  29. #include <Rocket/Core/ElementUtilities.h>
  30. #include <Rocket/Core/Event.h>
  31. #include <Rocket/Controls/ElementFormControlInput.h>
  32. #include "EventManager.h"
  33. #include "GameDetails.h"
  34. EventHandlerOptions::EventHandlerOptions()
  35. {
  36. }
  37. EventHandlerOptions::~EventHandlerOptions()
  38. {
  39. }
  40. void EventHandlerOptions::ProcessEvent(Rocket::Core::Event& event, const Rocket::Core::String& value)
  41. {
  42. // Sent from the 'onload' of the options screen; we set the options on the interface to match those previously set
  43. // this game session.
  44. if (value == "restore")
  45. {
  46. // Fetch the document from the target of the 'onload' event. From here we can fetch the options elements by ID
  47. // to manipulate them directly.
  48. Rocket::Core::ElementDocument* options_body = event.GetTargetElement()->GetOwnerDocument();
  49. if (options_body == NULL)
  50. return;
  51. // Get the current graphics setting, and translate that into the ID of the radio button we need to set.
  52. Rocket::Core::String graphics_option_id;
  53. switch (GameDetails::GetGraphicsQuality())
  54. {
  55. case GameDetails::GOOD: graphics_option_id = "good"; break;
  56. case GameDetails::OK: graphics_option_id = "ok"; break;
  57. case GameDetails::BAD: graphics_option_id = "bad"; break;
  58. default: break;
  59. }
  60. // Fetch the radio button from the document by ID, cast it to a radio button interface and set it as checked.
  61. // This will automatically pop the other radio buttons in the set. Note that we could have not cast and called
  62. // the 'Click()' function instead, but this method will avoid event overhead.
  63. Rocket::Controls::ElementFormControlInput* graphics_option = dynamic_cast< Rocket::Controls::ElementFormControlInput* >(options_body->GetElementById(graphics_option_id));
  64. if (graphics_option != NULL)
  65. graphics_option->SetAttribute("checked", "");
  66. // Fetch the reverb option by ID and set its checked status from the game options.
  67. Rocket::Controls::ElementFormControlInput* reverb_option = dynamic_cast< Rocket::Controls::ElementFormControlInput* >(options_body->GetElementById("reverb"));
  68. if (reverb_option != NULL)
  69. {
  70. if (GameDetails::GetReverb())
  71. reverb_option->SetAttribute("checked", "");
  72. else
  73. reverb_option->RemoveAttribute("checked");
  74. }
  75. // Similarly, fetch the 3D spatialisation option by ID and set its checked status.
  76. Rocket::Controls::ElementFormControlInput* spatialisation_option = dynamic_cast< Rocket::Controls::ElementFormControlInput* >(options_body->GetElementById("3d"));
  77. if (spatialisation_option != NULL)
  78. {
  79. if (GameDetails::Get3DSpatialisation())
  80. spatialisation_option->SetAttribute("checked", "");
  81. else
  82. spatialisation_option->RemoveAttribute("checked");
  83. }
  84. }
  85. // Sent from the 'onsubmit' action of the options menu; we read the values sent from the form and make the
  86. // necessary changes on the game details structure.
  87. else if (value == "store")
  88. {
  89. // First check which button was clicked to submit the form; if it was 'cancel', then we don't want to
  90. // propagate the changes.
  91. if (event.GetParameter< Rocket::Core::String >("submit", "cancel") == "accept")
  92. {
  93. // Fetch the results of the form submission. These are stored as parameters directly on the event itself.
  94. // Like HTML form events, the name of the parameter is the 'name' attribute of the control, and the value
  95. // is whatever was put into the 'value' attribute. Checkbox values are only sent through if the box was
  96. // clicked. Radio buttons send through one value for the active button.
  97. Rocket::Core::String graphics = event.GetParameter< Rocket::Core::String >("graphics", "ok");
  98. bool reverb = event.GetParameter< Rocket::Core::String >("reverb", "") == "true";
  99. bool spatialisation = event.GetParameter< Rocket::Core::String >("3d", "") == "true";
  100. if (graphics == "good")
  101. GameDetails::SetGraphicsQuality(GameDetails::GOOD);
  102. else if (graphics == "ok")
  103. GameDetails::SetGraphicsQuality(GameDetails::OK);
  104. else if (graphics == "bad")
  105. GameDetails::SetGraphicsQuality(GameDetails::BAD);
  106. GameDetails::SetReverb(reverb);
  107. GameDetails::Set3DSpatialisation(spatialisation);
  108. }
  109. }
  110. // This event is sent from the 'onchange' of the bad graphics radio button. It shows or hides the bad graphics
  111. // warning message.
  112. else if (value == "bad_graphics")
  113. {
  114. Rocket::Core::ElementDocument* options_body = event.GetTargetElement()->GetOwnerDocument();
  115. if (options_body == NULL)
  116. return;
  117. Rocket::Core::Element* bad_warning = options_body->GetElementById("bad_warning");
  118. if (bad_warning)
  119. {
  120. // The 'value' parameter of an onchange event is set to the value the control would send if it was
  121. // submitted; so, the empty string if it is clear or to the 'value' attribute of the control if it is set.
  122. if (event.GetParameter< Rocket::Core::String >("value", "").Empty())
  123. bad_warning->SetProperty("display", "none");
  124. else
  125. bad_warning->SetProperty("display", "block");
  126. }
  127. }
  128. }