Mercurial > wow > hansgar_and_franzok_assist
comparison Libs/DF/slider.lua @ 11:2f09fe4be15c
Added an Options Panel.
| author | Tercio |
|---|---|
| date | Mon, 20 Apr 2015 16:34:18 -0300 |
| parents | |
| children | 6dd01dcec75e |
comparison
equal
deleted
inserted
replaced
| 10:f1e32be6773e | 11:2f09fe4be15c |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 local DF = _G ["DetailsFramework"] | |
| 5 local _ | |
| 6 | |
| 7 local _rawset = rawset --> lua local | |
| 8 local _rawget = rawget --> lua local | |
| 9 local _setmetatable = setmetatable --> lua local | |
| 10 local _unpack = unpack --> lua local | |
| 11 local _type = type --> lua local | |
| 12 local _math_floor = math.floor --> lua local | |
| 13 local loadstring = loadstring --> lua local | |
| 14 | |
| 15 local SharedMedia = LibStub:GetLibrary("LibSharedMedia-3.0") | |
| 16 | |
| 17 local cleanfunction = function() end | |
| 18 local APISliderFunctions = false | |
| 19 local SliderMetaFunctions = {} | |
| 20 local NameLessSlider = 1 | |
| 21 | |
| 22 ------------------------------------------------------------------------------------------------------------ | |
| 23 --> metatables | |
| 24 | |
| 25 SliderMetaFunctions.__call = function (_table, value) | |
| 26 if (not value) then | |
| 27 if (_table.isSwitch) then | |
| 28 | |
| 29 if (type (value) == "boolean") then --> false | |
| 30 return _table.slider:SetValue (1) | |
| 31 end | |
| 32 | |
| 33 if (_table.slider:GetValue() == 1) then | |
| 34 return false | |
| 35 else | |
| 36 return true | |
| 37 end | |
| 38 end | |
| 39 return _table.slider:GetValue() | |
| 40 else | |
| 41 if (_table.isSwitch) then | |
| 42 if (type (value) == "boolean") then | |
| 43 if (value) then | |
| 44 _table.slider:SetValue (2) | |
| 45 else | |
| 46 _table.slider:SetValue (1) | |
| 47 end | |
| 48 else | |
| 49 _table.slider:SetValue (value) | |
| 50 end | |
| 51 return | |
| 52 end | |
| 53 | |
| 54 return _table.slider:SetValue (value) | |
| 55 end | |
| 56 end | |
| 57 | |
| 58 ------------------------------------------------------------------------------------------------------------ | |
| 59 --> members | |
| 60 | |
| 61 --> tooltip | |
| 62 local gmember_tooltip = function (_object) | |
| 63 return _object:GetTooltip() | |
| 64 end | |
| 65 --> shown | |
| 66 local gmember_shown = function (_object) | |
| 67 return _object:IsShown() | |
| 68 end | |
| 69 --> frame width | |
| 70 local gmember_width = function (_object) | |
| 71 return _object.slider:GetWidth() | |
| 72 end | |
| 73 --> frame height | |
| 74 local gmember_height = function (_object) | |
| 75 return _object.slider:GetHeight() | |
| 76 end | |
| 77 --> locked | |
| 78 local gmember_locked = function (_object) | |
| 79 return _rawget (_object, "lockdown") | |
| 80 end | |
| 81 --> fractional | |
| 82 local gmember_fractional = function (_object) | |
| 83 return _rawget (_object, "useDecimals") | |
| 84 end | |
| 85 --> value | |
| 86 local gmember_value = function (_object) | |
| 87 return _object() | |
| 88 end | |
| 89 | |
| 90 local get_members_function_index = { | |
| 91 ["tooltip"] = gmember_tooltip, | |
| 92 ["shown"] = gmember_shown, | |
| 93 ["width"] = gmember_width, | |
| 94 ["height"] = gmember_height, | |
| 95 ["locked"] = gmember_locked, | |
| 96 ["fractional"] = gmember_fractional, | |
| 97 ["value"] = gmember_value, | |
| 98 } | |
| 99 | |
| 100 SliderMetaFunctions.__index = function (_table, _member_requested) | |
| 101 | |
| 102 local func = get_members_function_index [_member_requested] | |
| 103 if (func) then | |
| 104 return func (_table, _member_requested) | |
| 105 end | |
| 106 | |
| 107 local fromMe = _rawget (_table, _member_requested) | |
| 108 if (fromMe) then | |
| 109 return fromMe | |
| 110 end | |
| 111 | |
| 112 return SliderMetaFunctions [_member_requested] | |
| 113 end | |
| 114 | |
| 115 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 116 | |
| 117 --> tooltip | |
| 118 local smember_tooltip = function (_object, _value) | |
| 119 return _object:SetTooltip (_value) | |
| 120 end | |
| 121 --> show | |
| 122 local smember_show = function (_object, _value) | |
| 123 if (_value) then | |
| 124 return _object:Show() | |
| 125 else | |
| 126 return _object:Hide() | |
| 127 end | |
| 128 end | |
| 129 --> hide | |
| 130 local smember_hide = function (_object, _value) | |
| 131 if (not _value) then | |
| 132 return _object:Show() | |
| 133 else | |
| 134 return _object:Hide() | |
| 135 end | |
| 136 end | |
| 137 --> frame width | |
| 138 local smember_width = function (_object, _value) | |
| 139 return _object.slider:SetWidth (_value) | |
| 140 end | |
| 141 --> frame height | |
| 142 local smember_height = function (_object, _value) | |
| 143 return _object.slider:SetHeight (_value) | |
| 144 end | |
| 145 --> locked | |
| 146 local smember_locked = function (_object, _value) | |
| 147 if (_value) then | |
| 148 return self:Disable() | |
| 149 else | |
| 150 return self:Enable() | |
| 151 end | |
| 152 end | |
| 153 --> backdrop | |
| 154 local smember_backdrop = function (_object, _value) | |
| 155 return _object.slider:SetBackdrop (_value) | |
| 156 end | |
| 157 --> fractional | |
| 158 local smember_fractional = function (_object, _value) | |
| 159 return _rawset (_object, "useDecimals", _value) | |
| 160 end | |
| 161 --> value | |
| 162 local smember_value = function (_object, _value) | |
| 163 _object (_value) | |
| 164 end | |
| 165 | |
| 166 local set_members_function_index = { | |
| 167 ["tooltip"] = smember_tooltip, | |
| 168 ["show"] = smember_show, | |
| 169 ["hide"] = smember_hide, | |
| 170 ["backdrop"] = smember_backdrop, | |
| 171 ["width"] = smember_width, | |
| 172 ["height"] = smember_height, | |
| 173 ["locked"] = smember_locked, | |
| 174 ["fractional"] = smember_fractional, | |
| 175 ["value"] = smember_value, | |
| 176 } | |
| 177 | |
| 178 SliderMetaFunctions.__newindex = function (_table, _key, _value) | |
| 179 local func = set_members_function_index [_key] | |
| 180 if (func) then | |
| 181 return func (_table, _value) | |
| 182 else | |
| 183 return _rawset (_table, _key, _value) | |
| 184 end | |
| 185 end | |
| 186 | |
| 187 ------------------------------------------------------------------------------------------------------------ | |
| 188 --> methods | |
| 189 | |
| 190 --> show & hide | |
| 191 function SliderMetaFunctions:IsShown() | |
| 192 return self.slider:IsShown() | |
| 193 end | |
| 194 function SliderMetaFunctions:Show() | |
| 195 return self.slider:Show() | |
| 196 end | |
| 197 function SliderMetaFunctions:Hide() | |
| 198 return self.slider:Hide() | |
| 199 end | |
| 200 | |
| 201 --> fixed value | |
| 202 function SliderMetaFunctions:SetFixedParameter (value) | |
| 203 _rawset (self, "FixedValue", value) | |
| 204 end | |
| 205 | |
| 206 --> set value | |
| 207 function SliderMetaFunctions:SetValue (value) | |
| 208 return self (value) | |
| 209 end | |
| 210 | |
| 211 -- thumb size | |
| 212 function SliderMetaFunctions:SetThumbSize (w, h) | |
| 213 if (not w) then | |
| 214 w = self.thumb:GetWidth() | |
| 215 end | |
| 216 if (not h) then | |
| 217 h = self.thumb:GetHeight() | |
| 218 end | |
| 219 return self.thumb:SetSize (w, h) | |
| 220 end | |
| 221 | |
| 222 | |
| 223 -- setpoint | |
| 224 function SliderMetaFunctions:SetPoint (v1, v2, v3, v4, v5) | |
| 225 v1, v2, v3, v4, v5 = DF:CheckPoints (v1, v2, v3, v4, v5, self) | |
| 226 if (not v1) then | |
| 227 print ("Invalid parameter for SetPoint") | |
| 228 return | |
| 229 end | |
| 230 return self.widget:SetPoint (v1, v2, v3, v4, v5) | |
| 231 end | |
| 232 | |
| 233 -- sizes | |
| 234 function SliderMetaFunctions:SetSize (w, h) | |
| 235 if (w) then | |
| 236 self.slider:SetWidth (w) | |
| 237 end | |
| 238 if (h) then | |
| 239 return self.slider:SetHeight (h) | |
| 240 end | |
| 241 end | |
| 242 | |
| 243 -- tooltip | |
| 244 function SliderMetaFunctions:SetTooltip (tooltip) | |
| 245 if (tooltip) then | |
| 246 return _rawset (self, "have_tooltip", tooltip) | |
| 247 else | |
| 248 return _rawset (self, "have_tooltip", nil) | |
| 249 end | |
| 250 end | |
| 251 function SliderMetaFunctions:GetTooltip() | |
| 252 return _rawget (self, "have_tooltip") | |
| 253 end | |
| 254 | |
| 255 -- frame levels | |
| 256 function SliderMetaFunctions:GetFrameLevel() | |
| 257 return self.slider:GetFrameLevel() | |
| 258 end | |
| 259 function SliderMetaFunctions:SetFrameLevel (level, frame) | |
| 260 if (not frame) then | |
| 261 return self.slider:SetFrameLevel (level) | |
| 262 else | |
| 263 local framelevel = frame:GetFrameLevel (frame) + level | |
| 264 return self.slider:SetFrameLevel (framelevel) | |
| 265 end | |
| 266 end | |
| 267 | |
| 268 -- frame stratas | |
| 269 function SliderMetaFunctions:SetFrameStrata() | |
| 270 return self.slider:GetFrameStrata() | |
| 271 end | |
| 272 function SliderMetaFunctions:SetFrameStrata (strata) | |
| 273 if (_type (strata) == "table") then | |
| 274 self.slider:SetFrameStrata (strata:GetFrameStrata()) | |
| 275 else | |
| 276 self.slider:SetFrameStrata (strata) | |
| 277 end | |
| 278 end | |
| 279 | |
| 280 -- enabled | |
| 281 function SliderMetaFunctions:IsEnabled() | |
| 282 return not _rawget (self, "lockdown") | |
| 283 end | |
| 284 function SliderMetaFunctions:Enable() | |
| 285 self.slider:Enable() | |
| 286 if (not self.lock_texture) then | |
| 287 DF:NewImage (self, [[Interface\PetBattles\PetBattle-LockIcon]], 12, 12, "overlay", {0.0546875, 0.9453125, 0.0703125, 0.9453125}, "lock_texture", "$parentLockTexture") | |
| 288 self.lock_texture:SetDesaturated (true) | |
| 289 self.lock_texture:SetPoint ("center", self.amt, "center") | |
| 290 end | |
| 291 self.lock_texture:Hide() | |
| 292 self.slider.amt:Show() | |
| 293 self:SetAlpha (1) | |
| 294 return _rawset (self, "lockdown", false) | |
| 295 end | |
| 296 | |
| 297 function SliderMetaFunctions:Disable() | |
| 298 | |
| 299 self.slider:Disable() | |
| 300 self.slider.amt:Hide() | |
| 301 self:SetAlpha (.4) | |
| 302 | |
| 303 if (not self.lock_texture) then | |
| 304 DF:NewImage (self, [[Interface\PetBattles\PetBattle-LockIcon]], 12, 12, "overlay", {0.0546875, 0.9453125, 0.0703125, 0.9453125}, "lock_texture", "$parentLockTexture") | |
| 305 self.lock_texture:SetDesaturated (true) | |
| 306 self.lock_texture:SetPoint ("center", self.amt, "center") | |
| 307 end | |
| 308 self.lock_texture:Show() | |
| 309 | |
| 310 return _rawset (self, "lockdown", true) | |
| 311 end | |
| 312 | |
| 313 --> hooks | |
| 314 function SliderMetaFunctions:SetHook (hookType, func) | |
| 315 if (func) then | |
| 316 _rawset (self, hookType.."Hook", func) | |
| 317 else | |
| 318 _rawset (self, hookType.."Hook", nil) | |
| 319 end | |
| 320 end | |
| 321 | |
| 322 ------------------------------------------------------------------------------------------------------------ | |
| 323 --> scripts | |
| 324 | |
| 325 local OnEnter = function (slider) | |
| 326 | |
| 327 if (_rawget (slider.MyObject, "lockdown")) then | |
| 328 return | |
| 329 end | |
| 330 | |
| 331 DetailsFrameworkSliderButtons1:ShowMe (slider) | |
| 332 | |
| 333 if (slider.MyObject.OnEnterHook) then | |
| 334 local interrupt = slider.MyObject.OnEnterHook (slider) | |
| 335 if (interrupt) then | |
| 336 return | |
| 337 end | |
| 338 end | |
| 339 | |
| 340 slider.thumb:SetAlpha (1) | |
| 341 | |
| 342 if (slider.MyObject.have_tooltip and slider.MyObject.have_tooltip ~= "Right Click to Type the Value") then | |
| 343 GameCooltip2:Preset (2) | |
| 344 GameCooltip2:AddLine (slider.MyObject.have_tooltip) | |
| 345 GameCooltip2:ShowCooltip (slider, "tooltip") | |
| 346 else | |
| 347 GameCooltip2:Preset (1) | |
| 348 GameCooltip2:AddLine ("Right Click to Type the Value") | |
| 349 GameCooltip2:AddIcon ([[Interface\TUTORIALFRAME\UI-TUTORIAL-FRAME]], 1, 1, 16, 16, 0.015625, 0.15671875, 0.640625, 0.798828125) | |
| 350 GameCooltip2:ShowCooltip (slider, "tooltip") | |
| 351 end | |
| 352 | |
| 353 local parent = slider:GetParent().MyObject | |
| 354 if (parent and parent.type == "panel") then | |
| 355 if (parent.GradientEnabled) then | |
| 356 parent:RunGradient() | |
| 357 end | |
| 358 end | |
| 359 | |
| 360 end | |
| 361 | |
| 362 local OnLeave = function (slider) | |
| 363 | |
| 364 if (_rawget (slider.MyObject, "lockdown")) then | |
| 365 return | |
| 366 end | |
| 367 | |
| 368 DetailsFrameworkSliderButtons1:PrepareToHide() | |
| 369 | |
| 370 if (slider.MyObject.OnLeaveHook) then | |
| 371 local interrupt = slider.MyObject.OnLeaveHook (slider) | |
| 372 if (interrupt) then | |
| 373 return | |
| 374 end | |
| 375 end | |
| 376 | |
| 377 slider.thumb:SetAlpha (.7) | |
| 378 | |
| 379 if (slider.MyObject.have_tooltip) then | |
| 380 GameCooltip2:ShowMe (false) | |
| 381 end | |
| 382 | |
| 383 local parent = slider:GetParent().MyObject | |
| 384 if (parent and parent.type == "panel") then | |
| 385 if (parent.GradientEnabled) then | |
| 386 parent:RunGradient (false) | |
| 387 end | |
| 388 end | |
| 389 | |
| 390 end | |
| 391 | |
| 392 | |
| 393 local f = CreateFrame ("frame", "DetailsFrameworkSliderButtons1", UIParent) | |
| 394 f:Hide() | |
| 395 --f:SetBackdrop ({bgFile = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]], tile = true, tileSize = 5}) | |
| 396 f:SetHeight (18) | |
| 397 | |
| 398 local t = 0 | |
| 399 f.is_going_hide = false | |
| 400 local going_hide = function (self, elapsed) | |
| 401 t = t + elapsed | |
| 402 if (t > 0.3) then | |
| 403 f:Hide() | |
| 404 f:SetScript ("OnUpdate", nil) | |
| 405 f.is_going_hide = false | |
| 406 end | |
| 407 end | |
| 408 | |
| 409 function f:ShowMe (host) | |
| 410 f:SetPoint ("bottomleft", host, "topleft", -3, -5) | |
| 411 f:SetPoint ("bottomright", host, "topright", 3, -5) | |
| 412 f:SetFrameStrata (host:GetFrameStrata()) | |
| 413 f:SetFrameLevel (host:GetFrameLevel()) | |
| 414 f:Show() | |
| 415 if (f.is_going_hide) then | |
| 416 f:SetScript ("OnUpdate", nil) | |
| 417 f.is_going_hide = false | |
| 418 end | |
| 419 | |
| 420 f.host = host.MyObject | |
| 421 end | |
| 422 | |
| 423 function f:PrepareToHide() | |
| 424 f.is_going_hide = true | |
| 425 t = 0 | |
| 426 f:SetScript ("OnUpdate", going_hide) | |
| 427 end | |
| 428 | |
| 429 local button_plus = CreateFrame ("button", "DetailsFrameworkSliderButtonsPlusButton", f) | |
| 430 local button_minor = CreateFrame ("button", "DetailsFrameworkSliderButtonsMinorButton", f) | |
| 431 | |
| 432 button_plus:SetScript ("OnEnter", function (self) | |
| 433 if (f.is_going_hide) then | |
| 434 f:SetScript ("OnUpdate", nil) | |
| 435 f.is_going_hide = false | |
| 436 end | |
| 437 end) | |
| 438 button_minor:SetScript ("OnEnter", function (self) | |
| 439 if (f.is_going_hide) then | |
| 440 f:SetScript ("OnUpdate", nil) | |
| 441 f.is_going_hide = false | |
| 442 end | |
| 443 end) | |
| 444 | |
| 445 button_plus:SetScript ("OnLeave", function (self) | |
| 446 f:PrepareToHide() | |
| 447 end) | |
| 448 button_minor:SetScript ("OnLeave", function (self) | |
| 449 f:PrepareToHide() | |
| 450 end) | |
| 451 | |
| 452 button_plus:SetNormalTexture ([[Interface\Buttons\UI-PlusButton-Up]]) | |
| 453 button_minor:SetNormalTexture ([[Interface\Buttons\UI-MinusButton-Up]]) | |
| 454 | |
| 455 button_plus:SetPushedTexture ([[Interface\Buttons\UI-PlusButton-Down]]) | |
| 456 button_minor:SetPushedTexture ([[Interface\Buttons\UI-MinusButton-Down]]) | |
| 457 | |
| 458 button_plus:SetDisabledTexture ([[Interface\Buttons\UI-PlusButton-Disabled]]) | |
| 459 button_minor:SetDisabledTexture ([[Interface\Buttons\UI-MinusButton-Disabled]]) | |
| 460 | |
| 461 button_plus:SetHighlightTexture ([[Interface\Buttons\UI-PlusButton-Hilight]]) | |
| 462 button_minor:SetHighlightTexture ([[Interface\Buttons\UI-PlusButton-Hilight]]) | |
| 463 | |
| 464 --button_minor:SetPoint ("bottomleft", f, "bottomleft", -6, -13) | |
| 465 --button_plus:SetPoint ("bottomright", f, "bottomright", 6, -13) | |
| 466 | |
| 467 button_minor:SetPoint ("bottomright", f, "bottomright", 13, -13) | |
| 468 button_plus:SetPoint ("left", button_minor, "right", -2, 0) | |
| 469 | |
| 470 button_plus:SetSize (16, 16) | |
| 471 button_minor:SetSize (16, 16) | |
| 472 | |
| 473 local timer = 0 | |
| 474 local change_timer = 0 | |
| 475 | |
| 476 -- -- -- | |
| 477 | |
| 478 local plus_button_script = function() | |
| 479 | |
| 480 local current = f.host.value | |
| 481 local editbox = SliderMetaFunctions.editbox_typevalue | |
| 482 | |
| 483 if (f.host.fine_tuning) then | |
| 484 f.host:SetValue (current + f.host.fine_tuning) | |
| 485 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 486 SliderMetaFunctions.editbox_typevalue:SetText (tostring (string.format ("%.2f", current + f.host.fine_tuning))) | |
| 487 end | |
| 488 else | |
| 489 if (f.host.useDecimals) then | |
| 490 f.host:SetValue (current + 0.1) | |
| 491 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 492 SliderMetaFunctions.editbox_typevalue:SetText (string.format ("%.2f", current + 0.1)) | |
| 493 end | |
| 494 else | |
| 495 f.host:SetValue (current + 1) | |
| 496 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 497 SliderMetaFunctions.editbox_typevalue:SetText (tostring (math.floor (current + 1))) | |
| 498 end | |
| 499 end | |
| 500 end | |
| 501 | |
| 502 end | |
| 503 | |
| 504 button_plus:SetScript ("OnMouseUp", function (self) | |
| 505 if (not button_plus.got_click) then | |
| 506 plus_button_script() | |
| 507 end | |
| 508 button_plus.got_click = false | |
| 509 self:SetScript ("OnUpdate", nil) | |
| 510 end) | |
| 511 | |
| 512 local on_update = function (self, elapsed) | |
| 513 timer = timer + elapsed | |
| 514 if (timer > 0.4) then | |
| 515 change_timer = change_timer + elapsed | |
| 516 if (change_timer > 0.1) then | |
| 517 change_timer = 0 | |
| 518 plus_button_script() | |
| 519 button_plus.got_click = true | |
| 520 end | |
| 521 end | |
| 522 end | |
| 523 button_plus:SetScript ("OnMouseDown", function (self) | |
| 524 timer = 0 | |
| 525 change_timer = 0 | |
| 526 self:SetScript ("OnUpdate", on_update) | |
| 527 end) | |
| 528 | |
| 529 -- -- -- | |
| 530 | |
| 531 local minor_button_script = function() | |
| 532 local current = f.host.value | |
| 533 local editbox = SliderMetaFunctions.editbox_typevalue | |
| 534 | |
| 535 if (f.host.fine_tuning) then | |
| 536 f.host:SetValue (current - f.host.fine_tuning) | |
| 537 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 538 SliderMetaFunctions.editbox_typevalue:SetText (tostring (string.format ("%.2f", current - f.host.fine_tuning))) | |
| 539 end | |
| 540 else | |
| 541 if (f.host.useDecimals) then | |
| 542 f.host:SetValue (current - 0.1) | |
| 543 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 544 SliderMetaFunctions.editbox_typevalue:SetText (string.format ("%.2f", current - 0.1)) | |
| 545 end | |
| 546 else | |
| 547 f.host:SetValue (current - 1) | |
| 548 if (editbox and SliderMetaFunctions.editbox_typevalue:IsShown()) then | |
| 549 SliderMetaFunctions.editbox_typevalue:SetText (tostring (math.floor (current - 1))) | |
| 550 end | |
| 551 end | |
| 552 end | |
| 553 end | |
| 554 | |
| 555 button_minor:SetScript ("OnMouseUp", function (self) | |
| 556 if (not button_minor.got_click) then | |
| 557 minor_button_script() | |
| 558 end | |
| 559 button_minor.got_click = false | |
| 560 self:SetScript ("OnUpdate", nil) | |
| 561 end) | |
| 562 | |
| 563 local on_update = function (self, elapsed) | |
| 564 timer = timer + elapsed | |
| 565 if (timer > 0.4) then | |
| 566 change_timer = change_timer + elapsed | |
| 567 if (change_timer > 0.1) then | |
| 568 change_timer = 0 | |
| 569 minor_button_script() | |
| 570 button_minor.got_click = true | |
| 571 end | |
| 572 end | |
| 573 end | |
| 574 button_minor:SetScript ("OnMouseDown", function (self) | |
| 575 timer = 0 | |
| 576 change_timer = 0 | |
| 577 self:SetScript ("OnUpdate", on_update) | |
| 578 end) | |
| 579 | |
| 580 function SliderMetaFunctions:TypeValue() | |
| 581 if (not self.isSwitch) then | |
| 582 | |
| 583 if (not SliderMetaFunctions.editbox_typevalue) then | |
| 584 | |
| 585 local editbox = CreateFrame ("EditBox", "DetailsFrameworkSliderEditBox", UIParent) | |
| 586 | |
| 587 editbox:SetSize (40, 20) | |
| 588 editbox:SetJustifyH ("center") | |
| 589 editbox:SetBackdrop ({bgFile = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]], | |
| 590 edgeFile = "Interface\\Buttons\\UI-SliderBar-Border", --edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], | |
| 591 tile = true, edgeSize = 8, tileSize = 5}) | |
| 592 editbox:SetFontObject ("GameFontHighlightSmall") | |
| 593 | |
| 594 editbox:SetScript ("OnEnterPressed", function() | |
| 595 editbox:ClearFocus() | |
| 596 editbox:Hide() | |
| 597 editbox:GetParent().MyObject.typing_value = false | |
| 598 editbox:GetParent().MyObject.value = tonumber (editbox:GetText()) | |
| 599 end) | |
| 600 | |
| 601 editbox:SetScript ("OnEscapePressed", function() | |
| 602 editbox:ClearFocus() | |
| 603 editbox:Hide() | |
| 604 editbox:GetParent().MyObject.typing_value = false | |
| 605 editbox:GetParent().MyObject.value = tonumber (self.typing_value_started) | |
| 606 end) | |
| 607 | |
| 608 editbox:SetScript ("OnTextChanged", function() | |
| 609 editbox:GetParent().MyObject.typing_can_change = true | |
| 610 editbox:GetParent().MyObject.value = tonumber (editbox:GetText()) | |
| 611 editbox:GetParent().MyObject.typing_can_change = false | |
| 612 | |
| 613 -- esse self fica como o primeiro a ser alterado | |
| 614 --print ("text changed", self:GetName()) | |
| 615 --print () | |
| 616 end) | |
| 617 | |
| 618 SliderMetaFunctions.editbox_typevalue = editbox | |
| 619 end | |
| 620 | |
| 621 local pvalue = self.previous_value [2] | |
| 622 self:SetValue (pvalue) | |
| 623 | |
| 624 self.typing_value = true | |
| 625 self.typing_value_started = pvalue | |
| 626 | |
| 627 SliderMetaFunctions.editbox_typevalue:SetSize (self.width, self.height) | |
| 628 SliderMetaFunctions.editbox_typevalue:SetPoint ("center", self.widget, "center") | |
| 629 SliderMetaFunctions.editbox_typevalue:SetFocus() | |
| 630 SliderMetaFunctions.editbox_typevalue:SetParent (self.widget) | |
| 631 SliderMetaFunctions.editbox_typevalue:SetFrameLevel (self.widget:GetFrameLevel()+1) | |
| 632 | |
| 633 if (self.useDecimals) then | |
| 634 SliderMetaFunctions.editbox_typevalue:SetText (tostring (string.format ("%.1f", self.value))) | |
| 635 else | |
| 636 SliderMetaFunctions.editbox_typevalue:SetText (tostring (math.floor (self.value))) | |
| 637 end | |
| 638 | |
| 639 SliderMetaFunctions.editbox_typevalue:HighlightText() | |
| 640 | |
| 641 SliderMetaFunctions.editbox_typevalue:Show() | |
| 642 end | |
| 643 end | |
| 644 | |
| 645 local OnMouseDown = function (slider, button) | |
| 646 if (button == "RightButton") then | |
| 647 slider.MyObject:TypeValue() | |
| 648 end | |
| 649 end | |
| 650 | |
| 651 local OnMouseUp = function (slider, button) | |
| 652 --if (button == "RightButton") then | |
| 653 -- if (slider.MyObject.typing_value) then | |
| 654 -- slider.MyObject:SetValue (slider.MyObject.previous_value [2]) | |
| 655 -- end | |
| 656 --end | |
| 657 end | |
| 658 | |
| 659 local OnHide = function (slider) | |
| 660 if (slider.MyObject.OnHideHook) then | |
| 661 local interrupt = slider.MyObject.OnHideHook (slider) | |
| 662 if (interrupt) then | |
| 663 return | |
| 664 end | |
| 665 end | |
| 666 | |
| 667 if (slider.MyObject.typing_value) then | |
| 668 SliderMetaFunctions.editbox_typevalue:ClearFocus() | |
| 669 SliderMetaFunctions.editbox_typevalue:SetText ("") | |
| 670 slider.MyObject.typing_valu = false | |
| 671 end | |
| 672 end | |
| 673 | |
| 674 local OnShow = function (slider) | |
| 675 if (slider.MyObject.OnShowHook) then | |
| 676 local interrupt = slider.MyObject.OnShowHook (slider) | |
| 677 if (interrupt) then | |
| 678 return | |
| 679 end | |
| 680 end | |
| 681 end | |
| 682 | |
| 683 local table_insert = table.insert | |
| 684 local table_remove = table.remove | |
| 685 | |
| 686 local OnValueChanged = function (slider) | |
| 687 | |
| 688 local amt = slider:GetValue() | |
| 689 | |
| 690 if (slider.MyObject.typing_value and not slider.MyObject.typing_can_change) then | |
| 691 slider.MyObject:SetValue (slider.MyObject.typing_value_started) | |
| 692 return | |
| 693 end | |
| 694 | |
| 695 table_insert (slider.MyObject.previous_value, 1, amt) | |
| 696 table_remove (slider.MyObject.previous_value, 4) | |
| 697 | |
| 698 if (slider.MyObject.OnValueChangeHook) then | |
| 699 local interrupt = slider.MyObject.OnValueChangeHook (slider, slider.MyObject.FixedValue, amt) | |
| 700 if (interrupt) then | |
| 701 return | |
| 702 end | |
| 703 end | |
| 704 | |
| 705 if (slider.MyObject.OnValueChanged) then | |
| 706 slider.MyObject.OnValueChanged (slider, slider.MyObject.FixedValue, amt) | |
| 707 end | |
| 708 | |
| 709 if (amt < 10 and amt >= 1) then | |
| 710 amt = "0"..amt | |
| 711 end | |
| 712 | |
| 713 if (slider.MyObject.useDecimals) then | |
| 714 slider.amt:SetText (string.format ("%.2f", amt)) | |
| 715 else | |
| 716 slider.amt:SetText (math.floor (amt)) | |
| 717 end | |
| 718 slider.MyObject.ivalue = amt | |
| 719 end | |
| 720 | |
| 721 ------------------------------------------------------------------------------------------------------------ | |
| 722 --> object constructor | |
| 723 | |
| 724 local SwitchOnClick = function (self, button, forced_value, value) | |
| 725 | |
| 726 local slider = self.MyObject | |
| 727 | |
| 728 if (_rawget (slider, "lockdown")) then | |
| 729 return | |
| 730 end | |
| 731 | |
| 732 if (forced_value) then | |
| 733 _rawset (slider, "value", not value) | |
| 734 end | |
| 735 | |
| 736 if (_rawget (slider, "value")) then --actived | |
| 737 | |
| 738 _rawset (slider, "value", false) | |
| 739 slider._text:SetText (slider._ltext) | |
| 740 slider._thumb:ClearAllPoints() | |
| 741 | |
| 742 slider:SetBackdropColor (1, 0, 0, 0.4) | |
| 743 slider._thumb:SetPoint ("left", slider.widget, "left") | |
| 744 | |
| 745 else | |
| 746 | |
| 747 _rawset (slider, "value", true) | |
| 748 slider._text:SetText (slider._rtext) | |
| 749 slider._thumb:ClearAllPoints() | |
| 750 | |
| 751 slider:SetBackdropColor (0, 0, 1, 0.4) | |
| 752 slider._thumb:SetPoint ("right", slider.widget, "right") | |
| 753 | |
| 754 end | |
| 755 | |
| 756 if (slider.OnSwitch and not forced_value) then | |
| 757 local value = _rawget (slider, "value") | |
| 758 if (slider.return_func) then | |
| 759 value = slider:return_func (value) | |
| 760 end | |
| 761 slider.OnSwitch (slider, slider.FixedValue, value) | |
| 762 end | |
| 763 | |
| 764 end | |
| 765 | |
| 766 local default_switch_func = function (self, passed_value) | |
| 767 if (self.value) then | |
| 768 return false | |
| 769 else | |
| 770 return true | |
| 771 end | |
| 772 end | |
| 773 | |
| 774 local switch_get_value = function (self) | |
| 775 return self.value | |
| 776 end | |
| 777 | |
| 778 local switch_set_value = function (self, value) | |
| 779 if (self.switch_func) then | |
| 780 value = self:switch_func (value) | |
| 781 end | |
| 782 | |
| 783 SwitchOnClick (self.widget, nil, true, value) | |
| 784 end | |
| 785 | |
| 786 local switch_set_fixparameter = function (self, value) | |
| 787 _rawset (self, "FixedValue", value) | |
| 788 end | |
| 789 | |
| 790 local switch_disable = function (self) | |
| 791 if (not self.lock_texture) then | |
| 792 DF:NewImage (self, [[Interface\PetBattles\PetBattle-LockIcon]], 12, 12, "overlay", {0.0546875, 0.9453125, 0.0703125, 0.9453125}, "lock_texture", "$parentLockTexture") | |
| 793 self.lock_texture:SetDesaturated (true) | |
| 794 self.lock_texture:SetPoint ("center", self._thumb, "center") | |
| 795 end | |
| 796 | |
| 797 self.lock_texture:Show() | |
| 798 self._text:Hide() | |
| 799 self:SetAlpha (.4) | |
| 800 _rawset (self, "lockdown", true) | |
| 801 end | |
| 802 local switch_enable = function (self) | |
| 803 if (not self.lock_texture) then | |
| 804 DF:NewImage (self, [[Interface\PetBattles\PetBattle-LockIcon]], 12, 12, "overlay", {0.0546875, 0.9453125, 0.0703125, 0.9453125}, "lock_texture", "$parentLockTexture") | |
| 805 self.lock_texture:SetDesaturated (true) | |
| 806 self.lock_texture:SetPoint ("center", self._thumb, "center") | |
| 807 end | |
| 808 | |
| 809 self.lock_texture:Hide() | |
| 810 self._text:Show() | |
| 811 self:SetAlpha (1) | |
| 812 return _rawset (self, "lockdown", false) | |
| 813 end | |
| 814 | |
| 815 function DF:CreateSwitch (parent, on_switch, default_value, w, h, ltext, rtext, member, name, color_inverted, switch_func, return_func, with_label) | |
| 816 local switch, label = DF:NewSwitch (parent, parent, name, member, w or 60, h or 20, ltext, rtext, default_value, color_inverted, switch_func, return_func, with_label) | |
| 817 if (on_switch) then | |
| 818 switch.OnSwitch = on_switch | |
| 819 end | |
| 820 return switch, label | |
| 821 end | |
| 822 | |
| 823 function DF:NewSwitch (parent, container, name, member, w, h, ltext, rtext, default_value, color_inverted, switch_func, return_func, with_label) | |
| 824 | |
| 825 --> early checks | |
| 826 if (not name) then | |
| 827 name = "DetailsFrameWorkSlider" .. NameLessSlider | |
| 828 NameLessSlider = NameLessSlider + 1 | |
| 829 elseif (not parent) then | |
| 830 return nil | |
| 831 end | |
| 832 if (not container) then | |
| 833 container = parent | |
| 834 end | |
| 835 | |
| 836 --> defaults | |
| 837 ltext = ltext or "OFF" | |
| 838 rtext = rtext or "ON" | |
| 839 | |
| 840 --> build frames | |
| 841 | |
| 842 w = w or 60 | |
| 843 h = h or 20 | |
| 844 | |
| 845 local slider = DF:NewButton (parent, container, name, member, w, h) | |
| 846 | |
| 847 slider.switch_func = switch_func | |
| 848 slider.return_func = return_func | |
| 849 slider.SetValue = switch_set_value | |
| 850 slider.GetValue = switch_get_value | |
| 851 slider.SetFixedParameter = switch_set_fixparameter | |
| 852 slider.Disable = switch_disable | |
| 853 slider.Enable = switch_enable | |
| 854 | |
| 855 if (member) then | |
| 856 parent [member] = slider | |
| 857 end | |
| 858 | |
| 859 slider:SetBackdrop ({edgeFile = [[Interface\Buttons\UI-SliderBar-Border]], edgeSize = 8, | |
| 860 bgFile = [[Interface\AddOns\Details\images\background]], insets = {left = 3, right = 3, top = 5, bottom = 5}}) | |
| 861 | |
| 862 local thumb = slider:CreateTexture (nil, "artwork") | |
| 863 thumb:SetTexture ("Interface\\Buttons\\UI-ScrollBar-Knob") | |
| 864 thumb:SetSize (34+(h*0.2), h*1.2) | |
| 865 thumb:SetAlpha (0.7) | |
| 866 thumb:SetPoint ("left", slider.widget, "left") | |
| 867 | |
| 868 local text = slider:CreateFontString (nil, "overlay", "GameFontHighlightSmall") | |
| 869 text:SetTextColor (.8, .8, .8, 1) | |
| 870 text:SetPoint ("center", thumb, "center") | |
| 871 | |
| 872 slider._text = text | |
| 873 slider._thumb = thumb | |
| 874 slider._ltext = ltext | |
| 875 slider._rtext = rtext | |
| 876 slider.thumb = thumb | |
| 877 | |
| 878 slider.invert_colors = color_inverted | |
| 879 | |
| 880 slider:SetScript ("OnClick", SwitchOnClick) | |
| 881 | |
| 882 slider:SetValue (default_value) | |
| 883 | |
| 884 slider.isSwitch = true | |
| 885 | |
| 886 if (with_label) then | |
| 887 local label = DF:CreateLabel (slider.widget, with_label, nil, nil, nil, "label", nil, "overlay") | |
| 888 label.text = with_label | |
| 889 slider.widget:SetPoint ("left", label.widget, "right", 2, 0) | |
| 890 with_label = label | |
| 891 end | |
| 892 | |
| 893 return slider, with_label | |
| 894 end | |
| 895 | |
| 896 function DF:CreateSlider (parent, w, h, min, max, step, defaultv, isDecemal, member, name, with_label) | |
| 897 local slider, label = DF:NewSlider (parent, parent, name, member, w, h, min, max, step, defaultv, isDecemal, false, with_label) | |
| 898 return slider, label | |
| 899 end | |
| 900 | |
| 901 function DF:NewSlider (parent, container, name, member, w, h, min, max, step, defaultv, isDecemal, isSwitch, with_label) | |
| 902 | |
| 903 --> early checks | |
| 904 if (not name) then | |
| 905 name = "DetailsFrameworkSlider" .. DF.SliderCounter | |
| 906 DF.SliderCounter = DF.SliderCounter + 1 | |
| 907 end | |
| 908 if (not parent) then | |
| 909 return nil | |
| 910 end | |
| 911 if (not container) then | |
| 912 container = parent | |
| 913 end | |
| 914 | |
| 915 if (name:find ("$parent")) then | |
| 916 name = name:gsub ("$parent", parent:GetName()) | |
| 917 end | |
| 918 | |
| 919 local SliderObject = {type = "slider", dframework = true} | |
| 920 | |
| 921 if (member) then | |
| 922 parent [member] = SliderObject | |
| 923 end | |
| 924 | |
| 925 if (parent.dframework) then | |
| 926 parent = parent.widget | |
| 927 end | |
| 928 if (container.dframework) then | |
| 929 container = container.widget | |
| 930 end | |
| 931 | |
| 932 --> defaults | |
| 933 min = min or 1 | |
| 934 max = max or 2 | |
| 935 step = step or 1 | |
| 936 defaultv = defaultv or min | |
| 937 | |
| 938 w = w or 130 | |
| 939 h = h or 19 | |
| 940 | |
| 941 --> default members: | |
| 942 --> hooks | |
| 943 SliderObject.OnEnterHook = nil | |
| 944 SliderObject.OnLeaveHook = nil | |
| 945 SliderObject.OnHideHook = nil | |
| 946 SliderObject.OnShowHook = nil | |
| 947 SliderObject.OnValueChangeHook = nil | |
| 948 --> misc | |
| 949 SliderObject.lockdown = false | |
| 950 SliderObject.container = container | |
| 951 SliderObject.have_tooltip = nil | |
| 952 SliderObject.FixedValue = nil | |
| 953 SliderObject.useDecimals = isDecemal or false | |
| 954 | |
| 955 SliderObject.slider = CreateFrame ("slider", name, parent) | |
| 956 SliderObject.widget = SliderObject.slider | |
| 957 | |
| 958 if (not APISliderFunctions) then | |
| 959 APISliderFunctions = true | |
| 960 local idx = getmetatable (SliderObject.slider).__index | |
| 961 for funcName, funcAddress in pairs (idx) do | |
| 962 if (not SliderMetaFunctions [funcName]) then | |
| 963 SliderMetaFunctions [funcName] = function (object, ...) | |
| 964 local x = loadstring ( "return _G."..object.slider:GetName()..":"..funcName.."(...)") | |
| 965 return x (...) | |
| 966 end | |
| 967 end | |
| 968 end | |
| 969 end | |
| 970 | |
| 971 SliderObject.slider.MyObject = SliderObject | |
| 972 SliderObject.slider:SetWidth (w) | |
| 973 SliderObject.slider:SetHeight (h) | |
| 974 SliderObject.slider:SetOrientation ("horizontal") | |
| 975 SliderObject.slider:SetMinMaxValues (min, max) | |
| 976 SliderObject.slider:SetValueStep (step) | |
| 977 SliderObject.slider:SetValue (defaultv) | |
| 978 SliderObject.ivalue = defaultv | |
| 979 | |
| 980 SliderObject.slider:SetBackdrop ({edgeFile = "Interface\\Buttons\\UI-SliderBar-Border", edgeSize = 8}) | |
| 981 SliderObject.slider:SetBackdropColor (0.9, 0.7, 0.7, 1.0) | |
| 982 | |
| 983 SliderObject.thumb = SliderObject.slider:CreateTexture (nil, "artwork") | |
| 984 SliderObject.thumb:SetTexture ("Interface\\Buttons\\UI-ScrollBar-Knob") | |
| 985 SliderObject.thumb:SetSize (30+(h*0.2), h*1.2) | |
| 986 SliderObject.thumb:SetAlpha (0.7) | |
| 987 SliderObject.slider:SetThumbTexture (SliderObject.thumb) | |
| 988 SliderObject.slider.thumb = SliderObject.thumb | |
| 989 | |
| 990 if (not isSwitch) then | |
| 991 SliderObject.have_tooltip = "Right Click to Type the Value" | |
| 992 end | |
| 993 | |
| 994 SliderObject.amt = SliderObject.slider:CreateFontString (nil, "overlay", "GameFontHighlightSmall") | |
| 995 | |
| 996 local amt = defaultv | |
| 997 if (amt < 10 and amt >= 1) then | |
| 998 amt = "0"..amt | |
| 999 end | |
| 1000 | |
| 1001 if (SliderObject.useDecimals) then | |
| 1002 SliderObject.amt:SetText (string.format ("%.2f", amt)) | |
| 1003 else | |
| 1004 SliderObject.amt:SetText (math.floor (amt)) | |
| 1005 end | |
| 1006 | |
| 1007 SliderObject.amt:SetTextColor (.8, .8, .8, 1) | |
| 1008 SliderObject.amt:SetPoint ("center", SliderObject.thumb, "center") | |
| 1009 SliderObject.slider.amt = SliderObject.amt | |
| 1010 | |
| 1011 SliderObject.previous_value = {defaultv or 0, 0, 0} | |
| 1012 | |
| 1013 --> hooks | |
| 1014 SliderObject.slider:SetScript ("OnEnter", OnEnter) | |
| 1015 SliderObject.slider:SetScript ("OnLeave", OnLeave) | |
| 1016 SliderObject.slider:SetScript ("OnHide", OnHide) | |
| 1017 SliderObject.slider:SetScript ("OnShow", OnShow) | |
| 1018 SliderObject.slider:SetScript ("OnValueChanged", OnValueChanged) | |
| 1019 SliderObject.slider:SetScript ("OnMouseDown", OnMouseDown) | |
| 1020 SliderObject.slider:SetScript ("OnMouseUp", OnMouseUp) | |
| 1021 | |
| 1022 | |
| 1023 _setmetatable (SliderObject, SliderMetaFunctions) | |
| 1024 | |
| 1025 if (with_label) then | |
| 1026 local label = DF:CreateLabel (SliderObject.slider, with_label, nil, nil, nil, "label", nil, "overlay") | |
| 1027 label.text = with_label | |
| 1028 SliderObject.slider:SetPoint ("left", label.widget, "right", 2, 0) | |
| 1029 with_label = label | |
| 1030 end | |
| 1031 | |
| 1032 return SliderObject, with_label | |
| 1033 | |
| 1034 end |
