Ticket #4039: 4039-2.patch

File 4039-2.patch, 12.4 KB (added by Damien PIQUET, 8 years ago)

Resize & revert changes fixes; corrected class description (reported by Imarok and s0600204)

  • binaries/data/mods/public/gui/options/options.js

     
    130130        control.checked = checked;
    131131        control.onPress = onUpdate;
    132132        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;
    133185    case "number":
    134         // TODO: Slider
    135186    case "string":
    136187        control = Engine.GetGUIObjectByName(category + "Input[" + i + "]");
    137188        let caption;
     
    306357        // and the possible function calls (which are of number or string types)
    307358        if (control.parameters.function)
    308359        {
    309             if (control.type !== "string" && control.type !== "number")
     360            if (control.type !== "string" && control.type !== "number" && control.type !== "slider")
    310361            {
    311362                warn("Invalid type option " + control.type + " defined with function for " + item + ": will not be reverted");
    312363                continue;
  • binaries/data/mods/public/gui/options/options.json

     
    196196    "soundSetting":
    197197    [
    198198        {
    199             "type": "number",
     199            "type": "slider",
    200200            "label": "Master Gain",
    201201            "tooltip": "Master audio gain",
    202202            "parameters": { "config": "sound.mastergain", "function": "SetMasterGain", "min": "0" }
    203203        },
    204204        {
    205             "type": "number",
     205            "type": "slider",
    206206            "label": "Music Gain",
    207207            "tooltip": "In game music gain",
    208208            "parameters": { "config": "sound.musicgain", "function": "SetMusicGain", "min": "0" }
    209209        },
    210210        {
    211             "type": "number",
     211            "type": "slider",
    212212            "label": "Ambient Gain",
    213213            "tooltip": "In game ambient sound gain",
    214214            "parameters": { "config": "sound.ambientgain", "function": "SetAmbientGain", "min": "0" }
    215215        },
    216216        {
    217             "type": "number",
     217            "type": "slider",
    218218            "label": "Action Gain",
    219219            "tooltip": "In game unit action sound gain",
    220220            "parameters": { "config": "sound.actiongain", "function": "SetActionGain", "min": "0" }
    221221        },
    222222        {
    223             "type": "number",
     223            "type": "slider",
    224224            "label": "UI Gain",
    225225            "tooltip": "UI sound gain",
    226226            "parameters": { "config": "sound.uigain", "function": "SetUIGain", "min": "0" }
  • binaries/data/mods/public/gui/options/options.xml

     
    5151                <object name="soundSetting[n]" size="0 25 100% 50" hidden="true">
    5252                    <object name="soundSettingLabel[n]" size="0 0 65% 100%" type="text" style="ModernLabelText" text_align="left"/>
    5353                    <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"/>
    5555                    <object name="soundSettingDropdown[n]" size="70% 0 100%-8 100%" type="dropdown" style="ModernDropDown" hidden="true"/>
    5656                </object>
    5757            </repeat>
  • source/gui/CGUI.cpp

     
    3333#include "CProgressBar.h"
    3434#include "CRadioButton.h"
    3535#include "CText.h"
     36#include "CSlider.h"
    3637#include "CTooltip.h"
    3738#include "MiniMap.h"
    3839
     
    333334    AddObjectType("olist",          &COList::ConstructObject);
    334335    AddObjectType("dropdown",       &CDropDown::ConstructObject);
    335336    AddObjectType("tooltip",        &CTooltip::ConstructObject);
     337    AddObjectType("slider",         &CSlider::ConstructObject);
    336338}
    337339
    338340void 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
     26CSlider::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
     44CSlider::~CSlider()
     45{
     46}
     47
     48void 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
     118void 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
     139CRect 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
     150float 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
     157void 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
     167void 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
     179void 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
     187void 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
     195void CSlider::UpdateCachedSize()
     196{
     197    IGUIObject::UpdateCachedSize();
     198    this->ComputeCursorWidth();
     199    this->UpdateCursorLeft();
     200}
     201
     202void 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 */
     28class CSlider : public IGUIObject
     29{
     30    GUI_OBJECT(CSlider)
     31
     32public:
     33    CSlider();
     34    virtual ~CSlider();
     35
     36    /**
     37     * @see IGUIObject#UpdateCachedSize()
     38     */
     39    void UpdateCachedSize();
     40
     41protected:
     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