| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "DialogGameVolume.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "application/ApplicationComponents.h" |
| 13 |
#include "application/ApplicationVolumeHandling.h" |
| 14 |
#include "dialogs/GUIDialogVolumeBar.h" |
| 15 |
#include "guilib/GUIComponent.h" |
| 16 |
#include "guilib/GUIDialog.h" |
| 17 |
#include "guilib/GUIMessage.h" |
| 18 |
#include "guilib/GUISliderControl.h" |
| 19 |
#include "guilib/GUIWindowManager.h" |
| 20 |
#include "guilib/LocalizeStrings.h" |
| 21 |
#include "guilib/WindowIDs.h" |
| 22 |
#include "interfaces/AnnouncementManager.h" |
| 23 |
#include "utils/Variant.h" |
| 24 |
|
| 25 |
#include <cmath> |
| 26 |
|
| 27 |
using namespace KODI; |
| 28 |
using namespace GAME; |
| 29 |
|
| 30 |
#define CONTROL_LABEL 12 //! @todo Remove me |
| 31 |
|
| 32 |
CDialogGameVolume::CDialogGameVolume() |
| 33 |
{ |
| 34 |
// Initialize CGUIWindow |
| 35 |
SetID(WINDOW_DIALOG_GAME_VOLUME); |
| 36 |
m_loadType = KEEP_IN_MEMORY; |
| 37 |
} |
| 38 |
|
| 39 |
bool CDialogGameVolume::OnMessage(CGUIMessage& message) |
| 40 |
{ |
| 41 |
switch (message.GetMessage()) |
| 42 |
{ |
| 43 |
case GUI_MSG_STATE_CHANGED: |
| 44 |
{ |
| 45 |
int controlId = message.GetControlId(); |
| 46 |
if (controlId == GetID()) |
| 47 |
{ |
| 48 |
OnStateChanged(); |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
break; |
| 53 |
} |
| 54 |
default: |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
return CGUIDialogSlider::OnMessage(message); |
| 59 |
} |
| 60 |
|
| 61 |
void CDialogGameVolume::OnInitWindow() |
| 62 |
{ |
| 63 |
m_volumePercent = m_oldVolumePercent = GetVolumePercent(); |
| 64 |
|
| 65 |
CGUIDialogSlider::OnInitWindow(); |
| 66 |
|
| 67 |
// Set slider parameters |
| 68 |
SetModalityType(DialogModalityType::MODAL); |
| 69 |
SetSlider(GetLabel(), GetVolumePercent(), VOLUME_MIN, VOLUME_DELTA, VOLUME_MAX, this, nullptr); |
| 70 |
|
| 71 |
SET_CONTROL_HIDDEN(CONTROL_LABEL); |
| 72 |
|
| 73 |
CGUIDialogVolumeBar* dialogVolumeBar = dynamic_cast<CGUIDialogVolumeBar*>( |
| 74 |
CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_DIALOG_VOLUME_BAR)); |
| 75 |
if (dialogVolumeBar != nullptr) |
| 76 |
dialogVolumeBar->RegisterCallback(this); |
| 77 |
|
| 78 |
CServiceBroker::GetAnnouncementManager()->AddAnnouncer(this); |
| 79 |
} |
| 80 |
|
| 81 |
void CDialogGameVolume::OnDeinitWindow(int nextWindowID) |
| 82 |
{ |
| 83 |
CServiceBroker::GetAnnouncementManager()->RemoveAnnouncer(this); |
| 84 |
|
| 85 |
CGUIDialogVolumeBar* dialogVolumeBar = dynamic_cast<CGUIDialogVolumeBar*>( |
| 86 |
CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_DIALOG_VOLUME_BAR)); |
| 87 |
if (dialogVolumeBar != nullptr) |
| 88 |
dialogVolumeBar->UnregisterCallback(this); |
| 89 |
|
| 90 |
CGUIDialogSlider::OnDeinitWindow(nextWindowID); |
| 91 |
} |
| 92 |
|
| 93 |
void CDialogGameVolume::OnSliderChange(void* data, CGUISliderControl* slider) |
| 94 |
{ |
| 95 |
const float volumePercent = slider->GetFloatValue(); |
| 96 |
|
| 97 |
if (std::fabs(volumePercent - m_volumePercent) > 0.1f) |
| 98 |
{ |
| 99 |
m_volumePercent = volumePercent; |
| 100 |
auto& components = CServiceBroker::GetAppComponents(); |
| 101 |
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>(); |
| 102 |
appVolume->SetVolume(volumePercent, true); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
bool CDialogGameVolume::IsShown() const |
| 107 |
{ |
| 108 |
return m_active; |
| 109 |
} |
| 110 |
|
| 111 |
void CDialogGameVolume::Announce(ANNOUNCEMENT::AnnouncementFlag flag, |
| 112 |
const std::string& sender, |
| 113 |
const std::string& message, |
| 114 |
const CVariant& data) |
| 115 |
{ |
| 116 |
if (flag == ANNOUNCEMENT::Application && message == "OnVolumeChanged") |
| 117 |
{ |
| 118 |
const float volumePercent = static_cast<float>(data["volume"].asDouble()); |
| 119 |
|
| 120 |
if (std::fabs(volumePercent - m_volumePercent) > 0.1f) |
| 121 |
{ |
| 122 |
m_volumePercent = volumePercent; |
| 123 |
|
| 124 |
CGUIMessage msg(GUI_MSG_STATE_CHANGED, GetID(), GetID()); |
| 125 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void CDialogGameVolume::OnStateChanged() |
| 131 |
{ |
| 132 |
if (m_volumePercent != m_oldVolumePercent) |
| 133 |
{ |
| 134 |
m_oldVolumePercent = m_volumePercent; |
| 135 |
SetSlider(GetLabel(), m_volumePercent, VOLUME_MIN, VOLUME_DELTA, VOLUME_MAX, this, nullptr); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
float CDialogGameVolume::GetVolumePercent() const |
| 140 |
{ |
| 141 |
const auto& components = CServiceBroker::GetAppComponents(); |
| 142 |
const auto appVolume = components.GetComponent<CApplicationVolumeHandling>(); |
| 143 |
return appVolume->GetVolumePercent(); |
| 144 |
} |
| 145 |
|
| 146 |
std::string CDialogGameVolume::GetLabel() |
| 147 |
{ |
| 148 |
return g_localizeStrings.Get(13376); // "Volume" |
| 149 |
} |