Created
November 23, 2014 18:33
-
-
Save SK83RJOSH/d70991e707b0de12445a to your computer and use it in GitHub Desktop.
A shiv for using Images with ImagePanels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ImagePanel Image Shiv | |
-- Example Usage: ImagePanel.CreateFromImage(Image.Create(AssetLocation.Game, "bm_smg.dds")); | |
function ImagePanel.CreateFromImage(image, ...) | |
local panel = ImagePanel.Create(...) | |
panel:SetImage("RakNet.png") | |
panel:SetUV(Vector2.Zero, Vector2.Zero) | |
function panel:SetImage(image) | |
self.image = image | |
self:SetUV(self.uv1 or Vector2.Zero, self.uv2 or Vector2.One) | |
end | |
function panel:SetUV(uv1, uv2) | |
self.uv1 = uv1 | |
self.uv2 = uv2 | |
end | |
function panel:GetTextureSize() | |
return self.image:GetSize() | |
end | |
function panel:GetClip() | |
local position = panel:RelativeToAbsolute(Vector2.Zero) | |
local size = panel:GetSize() | |
local parent = panel:GetParent() | |
while parent do | |
local pPosition = parent:RelativeToAbsolute(Vector2.Zero) | |
local pSize = parent:GetSize() | |
position.x = math.max(position.x, pPosition.x) | |
position.y = math.max(position.y, pPosition.y) | |
size.x = math.min(position.x + size.x, pPosition.x + pSize.x) - position.x | |
size.y = math.min(position.y + size.y, pPosition.y + pSize.y) - position.y | |
parent = parent:GetParent() | |
end | |
return position, size | |
end | |
panel:Subscribe("Render", function() | |
if panel.image then | |
Render:SetClip(true, panel:GetClip()) | |
panel.image:Draw(panel:RelativeToAbsolute(Vector2.Zero), panel:GetSize(), panel.uv1, panel.uv2) | |
Render:SetClip(false) | |
end | |
end) | |
if image then | |
panel:SetImage(image) | |
end | |
return panel | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment