File size: 5,294 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
class OptionSelector extends OptionSelectorBase
{
protected Widget m_PreviousOption;
protected Widget m_NextOption;
protected TextWidget m_SelectedOption;
protected int m_SelectedOptionIndex;
protected ref array<string> m_Options;
void OptionSelector(Widget parent, int current_index, ScriptedWidgetEventHandler parent_c, bool disabled)
{
m_Options = { "#server_browser_disabled", "#server_browser_show", "#server_browser_hide" };
m_ParentClass = parent_c;
m_SelectorType = 2;
if (current_index < 0 || current_index >= m_Options.Count())
{
m_SelectedOptionIndex = 0;
}
else
{
m_SelectedOptionIndex = current_index;
}
m_Root = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/option_selector.layout", parent);
#ifdef PLATFORM_CONSOLE
m_Parent = parent.GetParent().GetParent();
#else
#ifdef PLATFORM_WINDOWS
m_Parent = parent.GetParent();
#endif
#endif
m_SelectedOption = TextWidget.Cast(m_Root.FindAnyWidget("option_label"));
m_PreviousOption = m_Root.FindAnyWidget("prev_option");
m_NextOption = m_Root.FindAnyWidget("next_option");
#ifdef PLATFORM_CONSOLE
m_NextOption.Show(false);
m_PreviousOption.Show(false);
#endif
m_SelectedOption.SetText(m_Options.Get(m_SelectedOptionIndex));
m_Enabled = !disabled;
if (m_Enabled)
{
Enable();
}
else
{
Disable();
}
m_Parent.SetHandler(this);
}
void ~OptionSelector()
{
delete m_Root;
}
override bool OnMouseButtonUp(Widget w, int x, int y, int button)
{
if (button == MouseState.LEFT)
{
if (w == m_NextOption)
{
SetNextOption();
return true;
}
else if (w == m_PreviousOption)
{
SetPrevOption();
return true;
}
}
return false;
}
override bool OnClick(Widget w, int x, int y, int button)
{
if (button == MouseState.LEFT)
{
if (w == m_Parent)
SetNextOption();
}
return true;
}
override bool OnMouseEnter(Widget w, int x, int y)
{
return super.OnMouseEnter(w, x, y);
}
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
{
return super.OnMouseLeave(w, enterW, x, y);
}
void Reset()
{
m_SelectedOptionIndex = 0;
m_SelectedOption.SetText(m_Options.Get(m_SelectedOptionIndex));
m_OptionChanged.Invoke(m_SelectedOptionIndex);
}
void SetNextOption()
{
m_SelectedOptionIndex++;
if (m_SelectedOptionIndex >= m_Options.Count())
{
m_SelectedOptionIndex = 0;
}
m_SelectedOption.SetText(m_Options.Get(m_SelectedOptionIndex));
m_OptionChanged.Invoke(m_SelectedOptionIndex);
}
void SetPrevOption()
{
m_SelectedOptionIndex--;
if (m_SelectedOptionIndex < 0)
{
m_SelectedOptionIndex = m_Options.Count() - 1;
}
m_SelectedOption.SetText(m_Options.Get(m_SelectedOptionIndex));
m_OptionChanged.Invoke(m_SelectedOptionIndex);
}
array<string> GetOptions()
{
return m_Options;
}
bool IsSet()
{
return m_SelectedOptionIndex != 0;
}
//! Returns 'true' if current index == 1 (default 'enabled' value). Take care, as different selectors may follow different logic!
bool IsEnabled()
{
return m_SelectedOptionIndex == 1;
}
//! Returns false for the selector in 'disabled' states
bool IsSelectorEnabled()
{
return m_Enabled;
}
string GetStringValue()
{
return m_Options.Get(m_SelectedOptionIndex);
}
void SetStringOption(string option, bool fire_event = true)
{
int index = m_Options.Find(option);
if (index > -1)
{
m_SelectedOptionIndex = index;
m_SelectedOption.SetText(m_Options.Get(m_SelectedOptionIndex));
if (fire_event)
m_OptionChanged.Invoke(m_SelectedOptionIndex);
}
}
void ColorOption()
{
switch (m_SelectedOptionIndex)
{
case 0:
{
m_SelectedOption.SetColor(ARGB(255, 255, 255, 255));
break;
}
case 1:
{
m_SelectedOption.SetColor(ARGB(255, 0, 255, 0));
break;
}
case 2:
{
m_SelectedOption.SetColor(ARGB(255, 255, 0, 0));
break;
}
}
}
override bool IsFocusable(Widget w)
{
if (w)
{
return (w == m_Parent || w == m_NextOption || w == m_PreviousOption);
}
return false;
}
override void Enable()
{
super.Enable();
#ifndef PLATFORM_CONSOLE
m_NextOption.ClearFlags(WidgetFlags.IGNOREPOINTER);
m_NextOption.Show(true);
m_PreviousOption.ClearFlags(WidgetFlags.IGNOREPOINTER);
m_PreviousOption.Show(true);
#else
m_Parent.ClearFlags(WidgetFlags.NOFOCUS);
m_Parent.ClearFlags(WidgetFlags.IGNOREPOINTER);
#endif
}
override void Disable()
{
super.Disable();
#ifndef PLATFORM_CONSOLE
m_NextOption.SetFlags(WidgetFlags.IGNOREPOINTER);
m_NextOption.Show(false);
m_PreviousOption.SetFlags(WidgetFlags.IGNOREPOINTER);
m_PreviousOption.Show(false);
#else
m_Parent.SetFlags(WidgetFlags.NOFOCUS);
m_Parent.SetFlags(WidgetFlags.IGNOREPOINTER);
#endif
}
override void ColorNormalConsole(Widget w)
{
super.ColorNormalConsole(w);
if (!w)
return;
if (m_SelectedOption)
{
m_SelectedOption.SetColor(ARGB(255,255,255,255));
}
}
override void ColorDisabledConsole(Widget w)
{
super.ColorDisabledConsole(w);
if (!w)
return;
if (m_SelectedOption)
{
m_SelectedOption.SetColor(ARGB(120,255,255,255));
}
}
}
|