Index: binaries/data/mods/public/gui/options/options.js
===================================================================
--- binaries/data/mods/public/gui/options/options.js (révision 18332)
+++ binaries/data/mods/public/gui/options/options.js (copie de travail)
@@ -130,8 +130,59 @@
control.checked = checked;
control.onPress = onUpdate;
break;
+ case "slider":
+ control = Engine.GetGUIObjectByName(category + "Input[" + i + "]");
+ let ratio;
+ let callbackFunction;
+ let minvalue;
+ let maxvalue;
+
+ for (let param of Object.keys(option.parameters))
+ {
+ switch (param)
+ {
+ case "config":
+ ratio = Engine.ConfigDB_GetValue("user", key);
+ break;
+ case "function":
+ if (Engine[option.parameters.function])
+ callbackFunction = option.parameters.function;
+ break;
+ case "min":
+ minvalue = option.parameters.min;
+ break;
+ case "max":
+ maxvalue = option.parameters.max;
+ break;
+ default:
+ warn("Unknown option source type '" + param + "'");
+ }
+ }
+
+ onUpdate = function(key, callbackFunction, minvalue, maxvalue)
+ {
+ return function()
+ {
+ if (minvalue && +minvalue > +this.ratio)
+ this.ratio = minvalue;
+ if (maxvalue && +maxvalue < +this.ratio)
+ this.ratio = maxvalue;
+ if (Engine.ConfigDB_GetValue("user", key) === this.ratio)
+ return;
+ Engine.ConfigDB_CreateValue("user", key, this.ratio);
+ Engine.ConfigDB_SetChanges("user", true);
+ if (callbackFunction)
+ Engine[callbackFunction](+this.ratio);
+
+ updateOptionPanel();
+ };
+ }(key, callbackFunction, minvalue, maxvalue);
+
+ control.ratio = ratio;
+ control.onPress = onUpdate;
+ control.onMouseWheelEvent = onUpdate;
+ break;
case "number":
- // TODO: Slider
case "string":
control = Engine.GetGUIObjectByName(category + "Input[" + i + "]");
let caption;
@@ -306,7 +357,7 @@
// and the possible function calls (which are of number or string types)
if (control.parameters.function)
{
- if (control.type !== "string" && control.type !== "number")
+ if (control.type !== "string" && control.type !== "number" && control.type !== "slider")
{
warn("Invalid type option " + control.type + " defined with function for " + item + ": will not be reverted");
continue;
Index: binaries/data/mods/public/gui/options/options.json
===================================================================
--- binaries/data/mods/public/gui/options/options.json (révision 18332)
+++ binaries/data/mods/public/gui/options/options.json (copie de travail)
@@ -196,31 +196,31 @@
"soundSetting":
[
{
- "type": "number",
+ "type": "slider",
"label": "Master Gain",
"tooltip": "Master audio gain",
"parameters": { "config": "sound.mastergain", "function": "SetMasterGain", "min": "0" }
},
{
- "type": "number",
+ "type": "slider",
"label": "Music Gain",
"tooltip": "In game music gain",
"parameters": { "config": "sound.musicgain", "function": "SetMusicGain", "min": "0" }
},
{
- "type": "number",
+ "type": "slider",
"label": "Ambient Gain",
"tooltip": "In game ambient sound gain",
"parameters": { "config": "sound.ambientgain", "function": "SetAmbientGain", "min": "0" }
},
{
- "type": "number",
+ "type": "slider",
"label": "Action Gain",
"tooltip": "In game unit action sound gain",
"parameters": { "config": "sound.actiongain", "function": "SetActionGain", "min": "0" }
},
{
- "type": "number",
+ "type": "slider",
"label": "UI Gain",
"tooltip": "UI sound gain",
"parameters": { "config": "sound.uigain", "function": "SetUIGain", "min": "0" }
Index: binaries/data/mods/public/gui/options/options.xml
===================================================================
--- binaries/data/mods/public/gui/options/options.xml (révision 18332)
+++ binaries/data/mods/public/gui/options/options.xml (copie de travail)
@@ -51,7 +51,7 @@
Index: source/gui/CGUI.cpp
===================================================================
--- source/gui/CGUI.cpp (révision 18332)
+++ source/gui/CGUI.cpp (copie de travail)
@@ -33,6 +33,7 @@
#include "CProgressBar.h"
#include "CRadioButton.h"
#include "CText.h"
+#include "CSlider.h"
#include "CTooltip.h"
#include "MiniMap.h"
@@ -333,6 +334,7 @@
AddObjectType("olist", &COList::ConstructObject);
AddObjectType("dropdown", &CDropDown::ConstructObject);
AddObjectType("tooltip", &CTooltip::ConstructObject);
+ AddObjectType("slider", &CSlider::ConstructObject);
}
void CGUI::Draw()
Index: source/gui/CSlider.cpp
===================================================================
--- source/gui/CSlider.cpp (révision 0)
+++ source/gui/CSlider.cpp (copie de travail)
@@ -0,0 +1,206 @@
+/* Copyright (C) 2016 Wildfire Games.
+ * This file is part of 0 A.D.
+ *
+ * 0 A.D. is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 0 A.D. is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with 0 A.D. If not, see .
+ */
+
+#include "precompiled.h"
+
+#include "CSlider.h"
+
+#include "GUI.h"
+
+#include "lib/ogl.h"
+
+CSlider::CSlider()
+{
+ AddSetting(GUIST_float, "buffer_zone");
+ AddSetting(GUIST_float, "ratio");
+ AddSetting(GUIST_int, "cell_id");
+ AddSetting(GUIST_CGUISpriteInstance, "sprite");
+ AddSetting(GUIST_CGUISpriteInstance, "sprite_bar");
+ AddSetting(GUIST_CStrW, "tooltip");
+ AddSetting(GUIST_CStr, "tooltip_style");
+
+ // Private settings
+ AddSetting(GUIST_CStrW, "_icon_tooltip");
+ AddSetting(GUIST_CStr, "_icon_tooltip_style");
+
+ this->m_IsPressed = false;
+ this->cursor_width = 1;
+}
+
+CSlider::~CSlider()
+{
+}
+
+void CSlider::HandleMessage(SGUIMessage& Message)
+{
+ switch (Message.type)
+ {
+ case GUIM_SETTINGS_UPDATED:
+ GUI::GetSetting(this, "ratio", this->m_Pos);
+ this->ComputeCursorWidth();
+ this->UpdateCursorLeft();
+ break;
+
+ case GUIM_MOUSE_WHEEL_DOWN:
+ {
+ this->ScrollMinus();
+ this->UpdateRatio();
+
+ this->SendEvent(GUIM_PRESSED, "press");
+ break;
+ }
+ case GUIM_MOUSE_WHEEL_UP:
+ {
+ this->ScrollPlus();
+ this->UpdateRatio();
+
+ this->SendEvent(GUIM_PRESSED, "press");
+ break;
+ }
+ case GUIM_LOAD:
+ {
+ GUI::GetSetting(this, "ratio", this->m_Pos);
+ this->ComputeCursorWidth();
+ this->UpdateCursorLeft();
+ break;
+ }
+ case GUIM_MOUSE_PRESS_LEFT:
+ {
+ this->m_IsPressed = true;
+
+ this->m_Pos = this->GetRatioFromMousePos();
+ this->UpdateRatio();
+ this->SendEvent(GUIM_PRESSED, "press");
+ break;
+ }
+ case GUIM_MOUSE_RELEASE_LEFT:
+ {
+ this->m_IsPressed = false;
+ break;
+ }
+ case GUIM_MOUSE_MOTION:
+ {
+ if (this->m_IsPressed)
+ {
+ if (!this->MouseOver())
+ {
+ this->m_IsPressed = false;
+ }
+ else
+ {
+ this->m_Pos = this->GetRatioFromMousePos();
+ this->UpdateRatio();
+ this->SendEvent(GUIM_PRESSED, "press");
+ }
+ }
+
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+void CSlider::Draw()
+{
+ float bz = this->GetBufferedZ();
+
+ if (!this->GetGUI())
+ return;
+
+ CGUISpriteInstance* sprite;
+ CGUISpriteInstance* cursorSprite;
+ int cell_id;
+ GUI::GetSettingPointer(this, "sprite", sprite);
+ GUI::GetSettingPointer(this, "sprite_bar", cursorSprite);
+ GUI::GetSetting(this, "cell_id", cell_id);
+
+ this->GetGUI()->DrawSprite(*sprite, cell_id, bz, this->m_CachedActualSize);
+
+ // Draw a sprite at the % position
+ CRect cursorPos = this->GetCursorRect();
+ this->GetGUI()->DrawSprite(*cursorSprite, cell_id, bz, cursorPos);
+}
+
+CRect CSlider::GetCursorRect()
+{
+ // Compute cursor position relative to it's widget
+ CRect cursorPos = this->m_CachedActualSize;
+
+ cursorPos.left = this->cursor_left;
+ cursorPos.right = cursorPos.left + this->cursor_width;
+
+ return cursorPos;
+}
+
+float CSlider::GetRatioFromMousePos() {
+ CPos mouse = this->GetMousePos();
+
+ float widget_offset = mouse.x - this->m_CachedActualSize.left;
+ return widget_offset / this->m_CachedActualSize.GetWidth();
+}
+
+void CSlider::UpdateRatio() {
+ if (this->m_Pos <= 0.f || this->m_Pos >= 1.f)
+ return;
+
+ GUI::SetSetting(this, "ratio", this->m_Pos);
+
+ // recompute the cursor position
+ this->UpdateCursorLeft();
+}
+
+void CSlider::UpdateCursorLeft() {
+ float leftOffset = (this->m_Pos) * (this->m_CachedActualSize.left - this->m_CachedActualSize.right);
+
+ this->cursor_left = this->m_CachedActualSize.left - leftOffset;
+
+ // correct the cursor pos if it gets out of the widget
+ if (this->cursor_left < this->m_CachedActualSize.left)
+ this->cursor_left = this->m_CachedActualSize.left;
+ else if (this->cursor_left + this->cursor_width > this->m_CachedActualSize.right)
+ this->cursor_left = this->m_CachedActualSize.right - this->cursor_width;
+}
+
+void CSlider::ScrollPlus()
+{
+ this->m_Pos += 0.01f;
+
+ if (this->m_Pos >= 1.f)
+ this->m_Pos = 1.f;
+}
+
+void CSlider::ScrollMinus()
+{
+ this->m_Pos -= 0.01f;
+
+ if (this->m_Pos <= 0.f)
+ this->m_Pos = 0.f;
+}
+
+void CSlider::UpdateCachedSize()
+{
+ IGUIObject::UpdateCachedSize();
+ this->ComputeCursorWidth();
+ this->UpdateCursorLeft();
+}
+
+void CSlider::ComputeCursorWidth()
+{
+ // Cursor should 5% of it's parent with a max ?
+ this->cursor_width = this->m_CachedActualSize.GetWidth() * 0.1f;
+}
Index: source/gui/CSlider.h
===================================================================
--- source/gui/CSlider.h (révision 0)
+++ source/gui/CSlider.h (copie de travail)
@@ -0,0 +1,110 @@
+/* Copyright (C) 2016 Wildfire Games.
+ * This file is part of 0 A.D.
+ *
+ * 0 A.D. is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 0 A.D. is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with 0 A.D. If not, see .
+ */
+
+#ifndef INCLUDED_CSLIDER
+#define INCLUDED_CSLIDER
+
+#include "GUI.h"
+
+/**
+ * Slider control.
+ *
+ * @see IGUIObject
+ */
+class CSlider : public IGUIObject
+{
+ GUI_OBJECT(CSlider)
+
+public:
+ CSlider();
+ virtual ~CSlider();
+
+ /**
+ * @see IGUIObject#UpdateCachedSize()
+ */
+ void UpdateCachedSize();
+
+protected:
+
+ /**
+ * Get the cursos rect
+ */
+ CRect GetCursorRect();
+
+ /**
+ * @see IGUIObject#HandleMessage()
+ */
+ virtual void HandleMessage(SGUIMessage& Message);
+
+ /**
+ * Draws the Slider
+ */
+ virtual void Draw();
+
+ /**
+ * Update ratio
+ */
+ void UpdateRatio();
+
+ /**
+ * Update cursor left
+ */
+ void UpdateCursorLeft();
+
+ /**
+ * Get ratio from mouse position
+ */
+ float GetRatioFromMousePos();
+
+ /**
+ * Increase scroll one step
+ */
+ void ScrollPlus();
+
+ /**
+ * Decrease scroll one step
+ */
+ void ScrollMinus();
+
+ /**
+ * Compute the cursor width
+ */
+ void ComputeCursorWidth();
+
+ /**
+ * The cursor width
+ */
+ float cursor_width;
+
+ /**
+ * Relative position of the slider cursor
+ */
+ float m_Pos;
+
+ /**
+ * The widget is pressed
+ */
+ bool m_IsPressed;
+
+ /**
+ * Current cursor left offset
+ */
+ float cursor_left;
+
+};
+
+#endif // INCLUDED_CSLIDER