Ticket #4039: 4039-2.patch
File 4039-2.patch, 12.4 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/options/options.js
130 130 control.checked = checked; 131 131 control.onPress = onUpdate; 132 132 break; 133 case "slider": 134 control = Engine.GetGUIObjectByName(category + "Input[" + i + "]"); 135 let ratio; 136 let callbackFunction; 137 let minvalue; 138 let maxvalue; 139 140 for (let param of Object.keys(option.parameters)) 141 { 142 switch (param) 143 { 144 case "config": 145 ratio = Engine.ConfigDB_GetValue("user", key); 146 break; 147 case "function": 148 if (Engine[option.parameters.function]) 149 callbackFunction = option.parameters.function; 150 break; 151 case "min": 152 minvalue = option.parameters.min; 153 break; 154 case "max": 155 maxvalue = option.parameters.max; 156 break; 157 default: 158 warn("Unknown option source type '" + param + "'"); 159 } 160 } 161 162 onUpdate = function(key, callbackFunction, minvalue, maxvalue) 163 { 164 return function() 165 { 166 if (minvalue && +minvalue > +this.ratio) 167 this.ratio = minvalue; 168 if (maxvalue && +maxvalue < +this.ratio) 169 this.ratio = maxvalue; 170 if (Engine.ConfigDB_GetValue("user", key) === this.ratio) 171 return; 172 Engine.ConfigDB_CreateValue("user", key, this.ratio); 173 Engine.ConfigDB_SetChanges("user", true); 174 if (callbackFunction) 175 Engine[callbackFunction](+this.ratio); 176 177 updateOptionPanel(); 178 }; 179 }(key, callbackFunction, minvalue, maxvalue); 180 181 control.ratio = ratio; 182 control.onPress = onUpdate; 183 control.onMouseWheelEvent = onUpdate; 184 break; 133 185 case "number": 134 // TODO: Slider135 186 case "string": 136 187 control = Engine.GetGUIObjectByName(category + "Input[" + i + "]"); 137 188 let caption; … … 306 357 // and the possible function calls (which are of number or string types) 307 358 if (control.parameters.function) 308 359 { 309 if (control.type !== "string" && control.type !== "number" )360 if (control.type !== "string" && control.type !== "number" && control.type !== "slider") 310 361 { 311 362 warn("Invalid type option " + control.type + " defined with function for " + item + ": will not be reverted"); 312 363 continue; -
binaries/data/mods/public/gui/options/options.json
196 196 "soundSetting": 197 197 [ 198 198 { 199 "type": " number",199 "type": "slider", 200 200 "label": "Master Gain", 201 201 "tooltip": "Master audio gain", 202 202 "parameters": { "config": "sound.mastergain", "function": "SetMasterGain", "min": "0" } 203 203 }, 204 204 { 205 "type": " number",205 "type": "slider", 206 206 "label": "Music Gain", 207 207 "tooltip": "In game music gain", 208 208 "parameters": { "config": "sound.musicgain", "function": "SetMusicGain", "min": "0" } 209 209 }, 210 210 { 211 "type": " number",211 "type": "slider", 212 212 "label": "Ambient Gain", 213 213 "tooltip": "In game ambient sound gain", 214 214 "parameters": { "config": "sound.ambientgain", "function": "SetAmbientGain", "min": "0" } 215 215 }, 216 216 { 217 "type": " number",217 "type": "slider", 218 218 "label": "Action Gain", 219 219 "tooltip": "In game unit action sound gain", 220 220 "parameters": { "config": "sound.actiongain", "function": "SetActionGain", "min": "0" } 221 221 }, 222 222 { 223 "type": " number",223 "type": "slider", 224 224 "label": "UI Gain", 225 225 "tooltip": "UI sound gain", 226 226 "parameters": { "config": "sound.uigain", "function": "SetUIGain", "min": "0" } -
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="soundSettingInput[n]" size="70% 0 100%-8 100%" type=" input" style="ModernInput" hidden="true"/>54 <object name="soundSettingInput[n]" size="70% 0 100%-8 100%" type="slider" style="ModernInput" sprite_bar="ModernButtonYellow" 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 … … 333 334 AddObjectType("olist", &COList::ConstructObject); 334 335 AddObjectType("dropdown", &CDropDown::ConstructObject); 335 336 AddObjectType("tooltip", &CTooltip::ConstructObject); 337 AddObjectType("slider", &CSlider::ConstructObject); 336 338 } 337 339 338 340 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 20 #include "CSlider.h" 21 22 #include "GUI.h" 23 24 #include "lib/ogl.h" 25 26 CSlider::CSlider() 27 { 28 AddSetting(GUIST_float, "buffer_zone"); 29 AddSetting(GUIST_float, "ratio"); 30 AddSetting(GUIST_int, "cell_id"); 31 AddSetting(GUIST_CGUISpriteInstance, "sprite"); 32 AddSetting(GUIST_CGUISpriteInstance, "sprite_bar"); 33 AddSetting(GUIST_CStrW, "tooltip"); 34 AddSetting(GUIST_CStr, "tooltip_style"); 35 36 // Private settings 37 AddSetting(GUIST_CStrW, "_icon_tooltip"); 38 AddSetting(GUIST_CStr, "_icon_tooltip_style"); 39 40 this->m_IsPressed = false; 41 this->cursor_width = 1; 42 } 43 44 CSlider::~CSlider() 45 { 46 } 47 48 void CSlider::HandleMessage(SGUIMessage& Message) 49 { 50 switch (Message.type) 51 { 52 case GUIM_SETTINGS_UPDATED: 53 GUI<float>::GetSetting(this, "ratio", this->m_Pos); 54 this->ComputeCursorWidth(); 55 this->UpdateCursorLeft(); 56 break; 57 58 case GUIM_MOUSE_WHEEL_DOWN: 59 { 60 this->ScrollMinus(); 61 this->UpdateRatio(); 62 63 this->SendEvent(GUIM_PRESSED, "press"); 64 break; 65 } 66 case GUIM_MOUSE_WHEEL_UP: 67 { 68 this->ScrollPlus(); 69 this->UpdateRatio(); 70 71 this->SendEvent(GUIM_PRESSED, "press"); 72 break; 73 } 74 case GUIM_LOAD: 75 { 76 GUI<float>::GetSetting(this, "ratio", this->m_Pos); 77 this->ComputeCursorWidth(); 78 this->UpdateCursorLeft(); 79 break; 80 } 81 case GUIM_MOUSE_PRESS_LEFT: 82 { 83 this->m_IsPressed = true; 84 85 this->m_Pos = this->GetRatioFromMousePos(); 86 this->UpdateRatio(); 87 this->SendEvent(GUIM_PRESSED, "press"); 88 break; 89 } 90 case GUIM_MOUSE_RELEASE_LEFT: 91 { 92 this->m_IsPressed = false; 93 break; 94 } 95 case GUIM_MOUSE_MOTION: 96 { 97 if (this->m_IsPressed) 98 { 99 if (!this->MouseOver()) 100 { 101 this->m_IsPressed = false; 102 } 103 else 104 { 105 this->m_Pos = this->GetRatioFromMousePos(); 106 this->UpdateRatio(); 107 this->SendEvent(GUIM_PRESSED, "press"); 108 } 109 } 110 111 break; 112 } 113 default: 114 break; 115 } 116 } 117 118 void CSlider::Draw() 119 { 120 float bz = this->GetBufferedZ(); 121 122 if (!this->GetGUI()) 123 return; 124 125 CGUISpriteInstance* sprite; 126 CGUISpriteInstance* cursorSprite; 127 int cell_id; 128 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite); 129 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_bar", cursorSprite); 130 GUI<int>::GetSetting(this, "cell_id", cell_id); 131 132 this->GetGUI()->DrawSprite(*sprite, cell_id, bz, this->m_CachedActualSize); 133 134 // Draw a sprite at the % position 135 CRect cursorPos = this->GetCursorRect(); 136 this->GetGUI()->DrawSprite(*cursorSprite, cell_id, bz, cursorPos); 137 } 138 139 CRect CSlider::GetCursorRect() 140 { 141 // Compute cursor position relative to it's widget 142 CRect cursorPos = this->m_CachedActualSize; 143 144 cursorPos.left = this->cursor_left; 145 cursorPos.right = cursorPos.left + this->cursor_width; 146 147 return cursorPos; 148 } 149 150 float CSlider::GetRatioFromMousePos() { 151 CPos mouse = this->GetMousePos(); 152 153 float widget_offset = mouse.x - this->m_CachedActualSize.left; 154 return widget_offset / this->m_CachedActualSize.GetWidth(); 155 } 156 157 void CSlider::UpdateRatio() { 158 if (this->m_Pos <= 0.f || this->m_Pos >= 1.f) 159 return; 160 161 GUI<float>::SetSetting(this, "ratio", this->m_Pos); 162 163 // recompute the cursor position 164 this->UpdateCursorLeft(); 165 } 166 167 void CSlider::UpdateCursorLeft() { 168 float leftOffset = (this->m_Pos) * (this->m_CachedActualSize.left - this->m_CachedActualSize.right); 169 170 this->cursor_left = this->m_CachedActualSize.left - leftOffset; 171 172 // correct the cursor pos if it gets out of the widget 173 if (this->cursor_left < this->m_CachedActualSize.left) 174 this->cursor_left = this->m_CachedActualSize.left; 175 else if (this->cursor_left + this->cursor_width > this->m_CachedActualSize.right) 176 this->cursor_left = this->m_CachedActualSize.right - this->cursor_width; 177 } 178 179 void CSlider::ScrollPlus() 180 { 181 this->m_Pos += 0.01f; 182 183 if (this->m_Pos >= 1.f) 184 this->m_Pos = 1.f; 185 } 186 187 void CSlider::ScrollMinus() 188 { 189 this->m_Pos -= 0.01f; 190 191 if (this->m_Pos <= 0.f) 192 this->m_Pos = 0.f; 193 } 194 195 void CSlider::UpdateCachedSize() 196 { 197 IGUIObject::UpdateCachedSize(); 198 this->ComputeCursorWidth(); 199 this->UpdateCursorLeft(); 200 } 201 202 void CSlider::ComputeCursorWidth() 203 { 204 // Cursor should 5% of it's parent with a max ? 205 this->cursor_width = this->m_CachedActualSize.GetWidth() * 0.1f; 206 } -
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 /** 37 * @see IGUIObject#UpdateCachedSize() 38 */ 39 void UpdateCachedSize(); 40 41 protected: 42 43 /** 44 * Get the cursos rect 45 */ 46 CRect GetCursorRect(); 47 48 /** 49 * @see IGUIObject#HandleMessage() 50 */ 51 virtual void HandleMessage(SGUIMessage& Message); 52 53 /** 54 * Draws the Slider 55 */ 56 virtual void Draw(); 57 58 /** 59 * Update ratio 60 */ 61 void UpdateRatio(); 62 63 /** 64 * Update cursor left 65 */ 66 void UpdateCursorLeft(); 67 68 /** 69 * Get ratio from mouse position 70 */ 71 float GetRatioFromMousePos(); 72 73 /** 74 * Increase scroll one step 75 */ 76 void ScrollPlus(); 77 78 /** 79 * Decrease scroll one step 80 */ 81 void ScrollMinus(); 82 83 /** 84 * Compute the cursor width 85 */ 86 void ComputeCursorWidth(); 87 88 /** 89 * The cursor width 90 */ 91 float cursor_width; 92 93 /** 94 * Relative position of the slider cursor 95 */ 96 float m_Pos; 97 98 /** 99 * The widget is pressed 100 */ 101 bool m_IsPressed; 102 103 /** 104 * Current cursor left offset 105 */ 106 float cursor_left; 107 108 }; 109 110 #endif // INCLUDED_CSLIDER