Riftui

Riftui (https://www.riftui.com/forums/index.php)
-   Tutorials & Other Helpful Info. (https://www.riftui.com/forums/forumdisplay.php?f=12)
-   -   snippets/plugin (https://www.riftui.com/forums/showthread.php?t=30)

alkazar 06-09-11 03:16 AM

snippets/plugin
 
Hi,

Let's post here any snippet/improvements you've done so far

here is my UI.CreateButton function

Code:

-- UI.CreateButton by Alkazar@grimnir
function UI.CreateButton(parent, name, text, handler)
        -- create a host frame for our button
        local buttonFrame = UI.CreateFrame("Frame", name, parent)
       
        -- set default background color
        buttonFrame.BgColorR = 0
        buttonFrame.BgColorG = 0
        buttonFrame.BgColorB = 0
        buttonFrame.BgColorA = 1
        buttonFrame:SetBackgroundColor(0, 0, 0, 1)
        -- set focus background color
        buttonFrame.focusBgColorR = 0.2
        buttonFrame.focusBgColorG = 0.2
        buttonFrame.focusBgColorB = 0.2
        buttonFrame.focusBgColorA = 1
       
        -- label the button
        buttonFrame.text = UI.CreateFrame("Text", name.."Text", buttonFrame)
        buttonFrame.text:SetText(text)
       
        -- fit the stuff
        buttonFrame.text:SetHeight(buttonFrame.text:GetFullHeight())
        buttonFrame.text:SetWidth(buttonFrame.text:GetFullWidth())
        buttonFrame:SetHeight(buttonFrame.text:GetFullHeight())
        buttonFrame:SetWidth(buttonFrame.text:GetFullWidth())
    buttonFrame.text:SetPoint("CENTER", buttonFrame, "CENTER")
       
        -- set the handler
        buttonFrame.handler = handler
       
        function buttonFrame.Event:LeftDown()
                self:SetBackgroundColor(self.focusBgColorR, self.focusBgColorG, self.focusBgColorB, self.focusBgColorA)
        end
       
        function buttonFrame.Event:LeftUp()
                self:SetBackgroundColor(self.BgColorR, self.BgColorG, self.BgColorB, self.BgColorA)
                self:handler()
        end
       
        return buttonFrame
end

I guess it's self documented :)
You may be able to access members after the default setup

Please notice that our button will fire handler when left click is released, and only change the background when left click is pressed. Right now, even if the mouse get away, releasing the click will still trigger (which is not a bad thing from the API). But I couldn't figure out a way to check wether we're still focusing. Gonna update as soon as the API permits it.

You're free to use it anywhere you want, until you keep the heading commentary. No credit in readme (or whatever alike) needed. If you modify, you may put a heading line telling that this is not the original code.

Malorn 06-09-11 09:10 AM

Nice start. One question though - why fire the button handler when left click is released and not when it is pressed? To me that is an unnecessary delay and will be perceived as lag by the user.

I would recommend putting the handler in LeftDown() and remove LeftUp() entirely.

RightDown() might be useful for bringing up a context menu, in which case RightUp() would then select whatever option the mouse is hovering over. But in the case of the left button I strongly believe optimizing for performance is the way to go.

On that note, do we know if Rift addons support dependencies? Libraries with many functions like this would be helpful. Sure, we can import them directly into our apps but the value of libraries is that you update them in one place and retain some sanity with versioning.

alkazar 06-09-11 10:44 AM

As far as I know, RightDown (and RightUp) is not yet in the API.

My goal was to provide an easy way to create a form button like we always seen. Just look at the "submit reply" button below the post form.
Press, and keep it pressed. You can still change advice until you release.
When you click on the close button on the top left (may depend on your OS and theme), that's the same. So I wanted to keep my customs :)

So, this is typically a form button, rather than an ability one, although I'm sure - I never really paid attention to it - this is the same case.

If you still want your button to fire instantly, then you can customize the function.

Note that we don't yet have the complete API, and we have no idea on the programming level. Right now, we got GUI basics, and we can do some stuff with. But I think they may provide us button, textarea, scrollbars and so on... We could do this with a few more primitives to handle keyboard input and mouse move (in fact, mouseover and mouseout would be enough !), but this will be CPU hungry, where native would be far more efficient I guess.

So, keep having a look at each patch. I don't know if they're gonna update the pastebin, but I will refresh everyday.
Right now, it's a thumbnail, more than the real API, and nothing is certain about what we might be able to do, and how.

Contagious 06-10-11 01:12 PM

Not really a snippet, but if you want to keep track of a buff/debuff on a target that can be applied by more than one person you can use the caster field in the Inspect.Buff.Detail table. To retrieve the player id you can use a self buff that you know the player will use (or for a general addon go through all possible self buffs) and check out the caster field of that buff.

It is kind of a hack and requires the player to use a self buff, but it's better than nothing (at least in my Electrified case). If I don't make sense look in my addon code, although I don't recommend that haha (worse layout than the api documentation ;))

phoenik 06-16-11 10:08 AM

For those who like to use hex colors and are off put by the RGB we have now here you go
Code:

local function GetRGB(hex)
        local tbl = {}
        tbl.r = tonumber("0x" .. string.sub(hex, 1, 2)) / 255
        tbl.g = tonumber("0x" .. string.sub(hex, 3, 4)) / 255
        tbl.b = tonumber("0x" .. string.sub(hex, 5, 6)) / 255
        return tbl
end

Pretty simple. Returns a table with r, g, b values that you can use to easily set a color for your element.

Example Use:
Code:

local context = UI.CreateContext("SampleContext")
local frame = UI.CreateFrame("Frame", "SampleFrame", context)
local rgb = GetRGB("FF00DD")
frame:SetBackgroundColor(rgb.r, rgb.g, rgb.b, 1)

Can probably implement this in a way that lets users enter a hex number into the console for a color :D.

alkazar 06-19-11 10:03 AM

I don't remember whether it is possible or not in lua, but I suggest you the following use:

local r, g, b = GetRGB("FF00DD")


All times are GMT -6. The time now is 05:02 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2022 MMOUI