Ticket #4039: 4039_slider.2.patch
File 4039_slider.2.patch, 12.9 KB (added by , 7 years ago) |
---|
-
binaries/data/mods/mod/gui/common/modern/sprites.xml
822 822 texture_size="0 0 24 24" 823 823 /> 824 824 </sprite> 825 <sprite name="ModernSliderButton"> 826 <image texture="global/modern/tick-on.png" 827 real_texture_placement="0 0 27 27" 828 size="50%-10 50%-10 50%+11 50%+11" 829 /> 830 </sprite> 831 <sprite name="ModernSliderLine"> 832 <image texture="global/modern/gold-separator.png" 833 real_texture_placement = "0 0 806 1" 834 size="0 50%-1 100% 50%+1" 835 /> 836 </sprite> 825 837 </sprites> -
binaries/data/mods/mod/gui/gui.rnc
107 107 attribute text_valign { valign }?& 108 108 attribute tooltip { text }?& 109 109 attribute tooltip_style { text }? 110 attribute width { xsd:decimal }? 110 111 111 112 ## 112 113 # Objects # -
binaries/data/mods/mod/gui/gui.rng
413 413 <optional> 414 414 <attribute name="tooltip_style"/> 415 415 </optional> 416 <optional> 417 <attribute name="width"> 418 <data type="decimal"/> 419 </attribute> 420 </optional> 416 421 </interleave> 417 422 </define> 418 423 <define name="objects"> -
binaries/data/mods/public/gui/options/options.js
131 131 control.checked = checked; 132 132 control.onPress = onUpdate; 133 133 break; 134 case "slider": 135 control = Engine.GetGUIObjectByName(category + "Slider[" + i + "]"); 136 let value; 137 let callbackFunction; 138 let minvalue; 139 let maxvalue; 140 141 for (let param of Object.keys(option.parameters)) 142 { 143 switch (param) 144 { 145 case "config": 146 value = +Engine.ConfigDB_GetValue("user", key); 147 break; 148 case "function": 149 if (Engine[option.parameters.function]) 150 callbackFunction = option.parameters.function; 151 break; 152 case "min": 153 minvalue = +option.parameters.min; 154 break; 155 case "max": 156 maxvalue = +option.parameters.max; 157 break; 158 default: 159 warn("Unknown option source type '" + param + "'"); 160 } 161 } 162 163 onUpdate = function(key, callbackFunction, minvalue, maxvalue) 164 { 165 return function() 166 { 167 if (Engine.ConfigDB_GetValue("user", key) === this.value) 168 return; 169 Engine.ConfigDB_CreateValue("user", key, this.value); 170 Engine.ConfigDB_SetChanges("user", true); 171 if (callbackFunction) 172 Engine[callbackFunction](+this.value); 173 updateOptionPanel(); 174 }; 175 }(key, callbackFunction, minvalue, maxvalue); 176 177 control.value = value; 178 control.max_value = maxvalue; 179 control.min_value = minvalue; 180 control.onValueChange = onUpdate; 181 break; 134 182 case "number": 135 // TODO: Slider136 183 case "string": 137 184 control = Engine.GetGUIObjectByName(category + "Input[" + i + "]"); 138 185 let caption; … … 310 357 // and the possible function calls (which are of number or string types) 311 358 if (control.parameters.function) 312 359 { 313 if (control.type !== "string" && control.type !== "number" )360 if (control.type !== "string" && control.type !== "number" && control.type !== "slider") 314 361 { 315 362 warn("Invalid type option " + control.type + " defined with function for " + item + ": will not be reverted"); 316 363 continue; -
binaries/data/mods/public/gui/options/options.json
208 208 "soundSetting": 209 209 [ 210 210 { 211 "type": " number",211 "type": "slider", 212 212 "label": "Master Gain", 213 213 "tooltip": "Master audio gain", 214 "parameters": { "config": "sound.mastergain", "function": "SetMasterGain", "min": "0"}214 "parameters": { "config": "sound.mastergain", "function": "SetMasterGain", "min": 0.0, "max": 10.0 } 215 215 }, 216 216 { 217 "type": " number",217 "type": "slider", 218 218 "label": "Music Gain", 219 219 "tooltip": "In game music gain", 220 "parameters": { "config": "sound.musicgain", "function": "SetMusicGain", "min": "0"}220 "parameters": { "config": "sound.musicgain", "function": "SetMusicGain", "min": 0.0, "max": 10.0 } 221 221 }, 222 222 { 223 "type": " number",223 "type": "slider", 224 224 "label": "Ambient Gain", 225 225 "tooltip": "In game ambient sound gain", 226 "parameters": { "config": "sound.ambientgain", "function": "SetAmbientGain", "min": "0"}226 "parameters": { "config": "sound.ambientgain", "function": "SetAmbientGain", "min": 0.0, "max": 10.0 } 227 227 }, 228 228 { 229 "type": " number",229 "type": "slider", 230 230 "label": "Action Gain", 231 231 "tooltip": "In game unit action sound gain", 232 "parameters": { "config": "sound.actiongain", "function": "SetActionGain", "min": "0"}232 "parameters": { "config": "sound.actiongain", "function": "SetActionGain", "min": 0.0, "max": 10.0 } 233 233 }, 234 234 { 235 "type": " number",235 "type": "slider", 236 236 "label": "UI Gain", 237 237 "tooltip": "UI sound gain", 238 "parameters": { "config": "sound.uigain", "function": "SetUIGain", "min": "0"}238 "parameters": { "config": "sound.uigain", "function": "SetUIGain", "min": 0.0, "max": 10.0 } 239 239 }, 240 240 { 241 241 "type": "boolean", -
binaries/data/mods/public/gui/options/options.xml
51 51 <object name="soundSetting[n]" size="0 25 100% 50" hidden="true"> 52 52 <object name="soundSettingLabel[n]" size="0 0 65% 100%" type="text" style="ModernLabelText" text_align="left"/> 53 53 <object name="soundSettingTickbox[n]" size="90% 5 100% 100%+5" type="checkbox" style="ModernTickBox" hidden="true"/> 54 <object name="soundSetting Input[n]" size="70% 0 100%-8 100%" type="input" style="ModernInput" hidden="true"/>54 <object name="soundSettingSlider[n]" size="70% 0 100%-8 100%" type="slider" width="20" sprite="ModernSliderButton" sprite_bar="ModernSliderLine" hidden="true"/> 55 55 <object name="soundSettingDropdown[n]" size="70% 0 100%-8 100%" type="dropdown" style="ModernDropDown" hidden="true"/> 56 56 </object> 57 57 </repeat> -
source/gui/CGUI.cpp
33 33 #include "CProgressBar.h" 34 34 #include "CRadioButton.h" 35 35 #include "CText.h" 36 #include "CSlider.h" 36 37 #include "CTooltip.h" 37 38 #include "MiniMap.h" 38 39 … … 312 313 AddObjectType("olist", &COList::ConstructObject); 313 314 AddObjectType("dropdown", &CDropDown::ConstructObject); 314 315 AddObjectType("tooltip", &CTooltip::ConstructObject); 316 AddObjectType("slider", &CSlider::ConstructObject); 315 317 } 316 318 317 319 void CGUI::Draw() -
source/gui/CSlider.cpp
1 /* Copyright (C) 2016 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. 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 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 #include "CSlider.h" 20 #include "GUI.h" 21 #include "lib/ogl.h" 22 #include "ps/CLogger.h" 23 24 25 CSlider::CSlider() 26 : m_IsPressed(false), m_ButtonSide(0) 27 { 28 AddSetting(GUIST_float, "value"); 29 AddSetting(GUIST_float, "min_value"); 30 AddSetting(GUIST_float, "max_value"); 31 AddSetting(GUIST_int, "cell_id"); 32 AddSetting(GUIST_CGUISpriteInstance, "sprite"); 33 AddSetting(GUIST_CGUISpriteInstance, "sprite_bar"); 34 AddSetting(GUIST_float, "width"); 35 36 GUI<float>::GetSetting(this, "value", m_Value); 37 GUI<float>::GetSetting(this, "min_value", m_MinValue); 38 GUI<float>::GetSetting(this, "max_value", m_MaxValue); 39 GUI<float>::GetSetting(this, "width", m_ButtonSide); 40 m_Value = Clamp(m_Value, m_MinValue, m_MaxValue); 41 } 42 43 CSlider::~CSlider() 44 { 45 } 46 47 void CSlider::HandleMessage(SGUIMessage& Message) 48 { 49 switch (Message.type) 50 { 51 case GUIM_SETTINGS_UPDATED: 52 { 53 GUI<float>::GetSetting(this, "value", m_Value); 54 GUI<float>::GetSetting(this, "min_value", m_MinValue); 55 GUI<float>::GetSetting(this, "max_value", m_MaxValue); 56 GUI<float>::GetSetting(this, "width", m_ButtonSide); 57 m_Value = Clamp(m_Value, m_MinValue, m_MaxValue); 58 break; 59 } 60 case GUIM_MOUSE_WHEEL_DOWN: 61 { 62 if (m_IsPressed) 63 break; 64 m_Value = std::max(m_Value - 0.01f, m_MinValue); 65 UpdateValue(); 66 break; 67 } 68 case GUIM_MOUSE_WHEEL_UP: 69 { 70 if (m_IsPressed) 71 break; 72 m_Value = std::min(m_Value + 0.01f, m_MaxValue); 73 UpdateValue(); 74 break; 75 } 76 case GUIM_MOUSE_PRESS_LEFT: 77 { 78 if (GetButtonRect().PointInside(GetMousePos())) 79 { 80 m_Mouse = GetMousePos(); 81 m_IsPressed = true; 82 } 83 break; 84 } 85 case GUIM_MOUSE_RELEASE_LEFT: 86 { 87 m_IsPressed = false; 88 break; 89 } 90 case GUIM_MOUSE_MOTION: 91 { 92 if (!MouseOver()) 93 m_IsPressed = false; 94 if (m_IsPressed) 95 { 96 float ratio = (m_MaxValue - m_MinValue) / (m_CachedActualSize.GetWidth() - m_ButtonSide); 97 float difference = float(GetMousePos().x - m_Mouse.x) * ratio; 98 m_Mouse = GetMousePos(); 99 m_Value = Clamp(m_Value + difference, m_MinValue, m_MaxValue); 100 UpdateValue(); 101 } 102 break; 103 } 104 default: 105 break; 106 } 107 } 108 109 void CSlider::Draw() 110 { 111 float bz = GetBufferedZ(); 112 113 if (!GetGUI()) 114 return; 115 116 CGUISpriteInstance* sprite; 117 CGUISpriteInstance* sprite_button; 118 int cell_id; 119 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_bar", sprite); 120 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite_button); 121 GUI<int>::GetSetting(this, "cell_id", cell_id); 122 123 CRect slider_line(m_CachedActualSize); 124 slider_line.left += m_ButtonSide / 2.0f; 125 slider_line.right -= m_ButtonSide / 2.0f; 126 GetGUI()->DrawSprite(*sprite, cell_id, bz, slider_line); 127 GetGUI()->DrawSprite(*sprite_button, cell_id, bz, GetButtonRect()); 128 } 129 130 void CSlider::UpdateValue() 131 { 132 GUI<float>::SetSetting(this, "value", m_Value); 133 ScriptEvent("valuechange"); 134 } 135 136 CRect CSlider::GetButtonRect() 137 { 138 float ratio = m_MaxValue > m_MinValue ? (m_Value - m_MinValue) / (m_MaxValue - m_MinValue) : 0.0f; 139 float x = m_CachedActualSize.left + ratio * (m_CachedActualSize.GetWidth() - m_ButtonSide); 140 float y = m_CachedActualSize.top + (m_CachedActualSize.GetHeight() - m_ButtonSide) / 2.0; 141 return CRect(x, y, x + m_ButtonSide, y + m_ButtonSide); 142 } -
source/gui/CSlider.h
1 /* Copyright (C) 2016 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. 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 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef INCLUDED_CSLIDER 19 #define INCLUDED_CSLIDER 20 21 #include "GUI.h" 22 23 /** 24 * Slider control. 25 * 26 * @see IGUIObject 27 */ 28 class CSlider : public IGUIObject 29 { 30 GUI_OBJECT(CSlider) 31 32 public: 33 CSlider(); 34 virtual ~CSlider(); 35 36 protected: 37 38 /** 39 * @see IGUIObject#HandleMessage() 40 */ 41 virtual void HandleMessage(SGUIMessage& Message); 42 43 /** 44 * Draws the Slider 45 */ 46 virtual void Draw(); 47 48 /** 49 * Change settings and send the script event 50 */ 51 void UpdateValue(); 52 53 CRect GetButtonRect(); 54 55 float m_MinValue, m_MaxValue, m_Value; 56 57 private: 58 bool m_IsPressed; 59 60 CPos m_Mouse; 61 62 float m_ButtonSide; 63 }; 64 65 #endif // INCLUDED_CSLIDER