Skip to content

Example App

The Fern edited this page Oct 4, 2020 · 19 revisions

Let's imagine the following code is placed in garrysmod/addons/myfirstapp/lua/gpapps/testapp.lua. The GPhone will automatically add it to the App-list once the map loads.

You can also place it in garrysmod/data/gphone/apps/testapp.txt, though you'll have to enable the ConVar gphone_csapp and other players won't be able to use it.

APP.Name        = "TestApp"                       -- The name of the App
APP.Author      = "TesterChester"                 -- The author's name
APP.Negative    = false                           -- Whether the App should use negative top-colors or not
APP.Icon        = "https://example.com/icon.png"  -- The icon. Can point to a local file or an online file
function APP.Run( frame, w, h, ratio )            -- Called when the App is first opened (frame, width, height, resolution ratio)
    function frame:Paint( x, y, w, h )
        draw.RoundedBox( 0, 0, 0, w, h, Color( 200, 200, 200, 255 ) )
    end
    
    local button = GPnl.AddPanel( frame, "panel" ) -- Creates a panel of type "panel"
    button:SetPos( 32*ratio, 32*ratio )            -- By multiplying with "ratio" we can support all resolutions
    button:SetSize( w - 64*ratio, 64*ratio )
    button.Color = Color( 0, 255, 0 ) -- Green color
    function button:Paint( x, y, w, h )
        draw.RoundedBox( 0, 0, 0, w, h, self.Color ) -- Paint the button using the color
    end
    function button:OnClick() -- Called when we click
        self.Color = Color( 255, 0, 0 ) -- Red color
    end
end
function APP.Think( frame, w, h, ratio ) -- Called every time the SWEP.Think function is called
    print("thonk")
end
function APP.Stop( frame ) -- Called when the App is closed
    print("closed")
end

hook.Add("GPhonePreRenderTopbar", "TopbarBackgroundTest", function(w, h) -- Draw a green background behind the Topbar
    surface.SetDrawColor(0, 255, 0, 255)
    surface.DrawRect(0, 0, w, h)
end)

Congrats. You now have an app with a green button that turns red once clicked. It also overrides the top-bar with a green color. This App obviously isn't that helpful, but you could practially do anything GLua allows if you are willing to put in the work.

The default Apps can be found here if you want to read through them and see how they work.