-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8792096
commit b2f133e
Showing
16 changed files
with
686 additions
and
308 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
local StudioService = game:GetService("StudioService") | ||
local AssetService = game:GetService("AssetService") | ||
|
||
local Rojo = script:FindFirstAncestor("Rojo") | ||
local Plugin = Rojo.Plugin | ||
local Packages = Rojo.Packages | ||
|
||
local Roact = require(Packages.Roact) | ||
|
||
local e = Roact.createElement | ||
|
||
local EditableImage = require(Plugin.App.Components.EditableImage) | ||
|
||
local imageCache = {} | ||
local function getImageSizeAndPixels(image) | ||
if not imageCache[image] then | ||
local editableImage = AssetService:CreateEditableImageAsync(image) | ||
imageCache[image] = { | ||
Size = editableImage.Size, | ||
Pixels = editableImage:ReadPixels(Vector2.zero, editableImage.Size), | ||
} | ||
end | ||
|
||
return imageCache[image].Size, table.clone(imageCache[image].Pixels) | ||
end | ||
|
||
local function getRecoloredClassIcon(className, color) | ||
local iconProps = StudioService:GetClassIcon(className) | ||
|
||
if iconProps and color then | ||
local success, editableImageSize, editableImagePixels = pcall(function() | ||
local size, pixels = getImageSizeAndPixels(iconProps.Image) | ||
|
||
local minVal, maxVal = math.huge, -math.huge | ||
for i = 1, #pixels, 4 do | ||
if pixels[i + 3] == 0 then | ||
continue | ||
end | ||
local pixelVal = math.max(pixels[i], pixels[i + 1], pixels[i + 2]) | ||
|
||
minVal = math.min(minVal, pixelVal) | ||
maxVal = math.max(maxVal, pixelVal) | ||
end | ||
|
||
local hue, sat, val = color:ToHSV() | ||
for i = 1, #pixels, 4 do | ||
if pixels[i + 3] == 0 then | ||
continue | ||
end | ||
|
||
local pixelVal = math.max(pixels[i], pixels[i + 1], pixels[i + 2]) | ||
local newVal = val | ||
if minVal < maxVal then | ||
-- Remap minVal - maxVal to val*0.9 - val | ||
newVal = val * (0.9 + 0.1 * (pixelVal - minVal) / (maxVal - minVal)) | ||
end | ||
|
||
local newPixelColor = Color3.fromHSV(hue, sat, newVal) | ||
pixels[i], pixels[i + 1], pixels[i + 2] = newPixelColor.R, newPixelColor.G, newPixelColor.B | ||
end | ||
return size, pixels | ||
end) | ||
if success then | ||
iconProps.EditableImagePixels = editableImagePixels | ||
iconProps.EditableImageSize = editableImageSize | ||
end | ||
end | ||
|
||
return iconProps | ||
end | ||
|
||
local ClassIcon = Roact.PureComponent:extend("ClassIcon") | ||
|
||
function ClassIcon:init() | ||
self.state = { | ||
iconProps = nil, | ||
} | ||
end | ||
|
||
function ClassIcon:updateIcon() | ||
local props = self.props | ||
local iconProps = getRecoloredClassIcon(props.className, props.color) | ||
self:setState({ | ||
iconProps = iconProps, | ||
}) | ||
end | ||
|
||
function ClassIcon:didMount() | ||
self:updateIcon() | ||
end | ||
|
||
function ClassIcon:didUpdate(lastProps) | ||
if lastProps.className ~= self.props.className or lastProps.color ~= self.props.color then | ||
self:updateIcon() | ||
end | ||
end | ||
|
||
function ClassIcon:render() | ||
local iconProps = self.state.iconProps | ||
if not iconProps then | ||
return nil | ||
end | ||
|
||
return e( | ||
"ImageLabel", | ||
{ | ||
Size = self.props.size, | ||
Position = self.props.position, | ||
LayoutOrder = self.props.layoutOrder, | ||
AnchorPoint = self.props.anchorPoint, | ||
ImageTransparency = self.props.transparency, | ||
Image = iconProps.Image, | ||
ImageRectOffset = iconProps.ImageRectOffset, | ||
ImageRectSize = iconProps.ImageRectSize, | ||
BackgroundTransparency = 1, | ||
}, | ||
if iconProps.EditableImagePixels | ||
then e(EditableImage, { | ||
size = iconProps.EditableImageSize, | ||
pixels = iconProps.EditableImagePixels, | ||
}) | ||
else nil | ||
) | ||
end | ||
|
||
return ClassIcon |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
local Rojo = script:FindFirstAncestor("Rojo") | ||
local Packages = Rojo.Packages | ||
|
||
local Roact = require(Packages.Roact) | ||
|
||
local e = Roact.createElement | ||
|
||
local EditableImage = Roact.PureComponent:extend("EditableImage") | ||
|
||
function EditableImage:init() | ||
self.ref = Roact.createRef() | ||
end | ||
|
||
function EditableImage:writePixels() | ||
local image = self.ref.current | ||
if not image then | ||
return | ||
end | ||
if not self.props.pixels then | ||
return | ||
end | ||
|
||
image:WritePixels(Vector2.zero, self.props.size, self.props.pixels) | ||
end | ||
|
||
function EditableImage:render() | ||
return e("EditableImage", { | ||
Size = self.props.size, | ||
[Roact.Ref] = self.ref, | ||
}) | ||
end | ||
|
||
function EditableImage:didMount() | ||
self:writePixels() | ||
end | ||
|
||
function EditableImage:didUpdate() | ||
self:writePixels() | ||
end | ||
|
||
return EditableImage |
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
Oops, something went wrong.