Skip to content

Commit

Permalink
Merge pull request #8 from cyritegamestudios/alpha
Browse files Browse the repository at this point in the history
Merging alpha to main
  • Loading branch information
cyrite authored Sep 13, 2023
2 parents 988ec57 + 7d18037 commit a18f75c
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ImageCollectionViewCell.__index = ImageCollectionViewCell
function ImageCollectionViewCell.new(item)
local self = setmetatable(CollectionViewCell.new(item), ImageCollectionViewCell)

self.imageView = ImageView.new(item:getImagePath())
self.imageView = ImageView.new(item:getRepeat().x, item:getRepeat().y, item:getAlpha())

self:addSubview(self.imageView)

Expand Down
39 changes: 28 additions & 11 deletions cylibs/ui/collection_view/collection_view_data_source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ local ContainerCollectionViewCell = require('cylibs/ui/collection_view/cells/con
local Event = require('cylibs/events/Luvent')
local ImageCollectionViewCell = require('cylibs/ui/collection_view/cells/image_collection_view_cell')
local ImageItem = require('cylibs/ui/collection_view/items/image_item')
local IndexedItem = require('cylibs/ui/collection_view/indexed_item')
local IndexPath = require('cylibs/ui/collection_view/index_path')
local TextCollectionViewCell = require('cylibs/ui/collection_view/cells/text_collection_view_cell')
local TextItem = require('cylibs/ui/collection_view/items/text_item')
local ViewItem = require('cylibs/ui/collection_view/items/view_item')
local IndexPath = require('cylibs/ui/collection_view/index_path')


-- Define a simple collection data source table
local CollectionViewDataSource = {}
Expand Down Expand Up @@ -52,15 +54,26 @@ end

-- Add an item to a specific section, creating the section if it doesn't exist
function CollectionViewDataSource:addItem(item, indexPath)
self:addItems(L{ IndexedItem.new(item, indexPath) })
end

---
-- Adds a list of IndexedItems to the collection view.
--
-- @tparam list indexedItems A list of IndexedItems containing both items and their corresponding index paths.
--
function CollectionViewDataSource:addItems(indexedItems)
local snapshot = self:createSnapshot() -- Create a snapshot before making changes

local section = indexPath.section
local row = indexPath.row
for indexedItem in indexedItems:it() do
local section = indexedItem:getIndexPath().section
local row = indexedItem:getIndexPath().row

if not self.sections[section] then
table.insert(self.sections, section, {items = {}})
if not self.sections[section] then
table.insert(self.sections, section, {items = {}})
end
table.insert(self.sections[section].items, row, indexedItem:getItem())
end
table.insert(self.sections[section].items, row, item)

-- Generate a diff
local diff = self:generateDiff(snapshot)
Expand Down Expand Up @@ -99,14 +112,18 @@ end

-- Update an item at a specific IndexPath
function CollectionViewDataSource:updateItem(item, indexPath)
local currentItem = self.sections[indexPath.section].items[indexPath.row]
if currentItem and currentItem == item then
return
end
self:updateItems(L{ IndexedItem.new(item, indexPath) })
end

function CollectionViewDataSource:updateItems(indexedItems)
local snapshot = self:createSnapshot() -- Create a snapshot before making changes

self.sections[indexPath.section].items[indexPath.row] = item
for indexedItem in indexedItems:it() do
local currentItem = self.sections[indexedItem:getIndexPath().section].items[indexedItem:getIndexPath().row]
if not (currentItem and currentItem == indexedItem:getItem()) then
self.sections[indexedItem:getIndexPath().section].items[indexedItem:getIndexPath().row] = indexedItem:getItem()
end
end

-- Generate a diff
local diff = self:generateDiff(snapshot)
Expand Down
17 changes: 17 additions & 0 deletions cylibs/ui/collection_view/collection_view_delegate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function CollectionViewDelegate.new(collectionView)

self.collectionView = collectionView
self.selectedIndexPaths = S{}
self.highlightedIndexPaths = S{}

self.disposeBag = DisposeBag.new()

Expand Down Expand Up @@ -56,6 +57,7 @@ function CollectionViewDelegate.new(collectionView)
end
elseif type == Mouse.Event.Move then
if not cell:isHighlighted() then
self:deHighlightAllItems()
self:highlightItemAtIndexPath(item, indexPath)
end
end
Expand Down Expand Up @@ -180,6 +182,8 @@ function CollectionViewDelegate:highlightItemAtIndexPath(item, indexPath)
local cell = self.collectionView:getDataSource():cellForItemAtIndexPath(indexPath)
cell:setHighlighted(true)

self.highlightedIndexPaths:add(indexPath)

self:didHighlightItemAtIndexPath():trigger(item, indexPath)
end

Expand All @@ -198,7 +202,20 @@ function CollectionViewDelegate:deHighlightItemAtIndexPath(item, indexPath)
local cell = self.collectionView:getDataSource():cellForItemAtIndexPath(indexPath)
cell:setHighlighted(false)

self.highlightedIndexPaths = self.highlightedIndexPaths:filter(function(existingIndexPath) return existingIndexPath ~= indexPath end)

self:didDehighlightItemAtIndexPath():trigger(item, indexPath)
end

---
-- De-highlights all currently highlighted items in the collection view.
--
function CollectionViewDelegate:deHighlightAllItems()
for indexPath in self.highlightedIndexPaths:it() do
local item = self.collectionView:getDataSource():itemAtIndexPath(indexPath)
self:deHighlightItemAtIndexPath(item, indexPath)
end
end


return CollectionViewDelegate
50 changes: 50 additions & 0 deletions cylibs/ui/collection_view/indexed_item.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local IndexPath = require('cylibs/ui/collection_view/index_path')

local IndexedItem = {}
IndexedItem.__index = IndexedItem
IndexedItem.__type = "IndexedItem"

---
-- Creates a new IndexedItem with the specified item and index path.
--
-- @tparam any item The associated item.
-- @tparam IndexPath indexPath The index path of the item.
-- @treturn IndexedItem The newly created IndexedItem.
--
function IndexedItem.new(item, indexPath)
local self = setmetatable({}, IndexedItem)
self.item = item
self.indexPath = indexPath
return self
end

---
-- Returns the associated item of this IndexedItem.
--
-- @treturn any The associated item.
--
function IndexedItem:getItem()
return self.item
end

---
-- Returns the index path of this IndexedItem.
--
-- @treturn IndexPath The index path of the item.
--
function IndexedItem:getIndexPath()
return self.indexPath
end

---
-- Checks if this IndexedItem is equal to another.
--
-- @tparam IndexedItem otherItem The other IndexedItem to compare with.
-- @treturn boolean True if the items and index paths are equal, false otherwise.
--
function IndexedItem:__eq(otherItem)
return otherItem.__type == IndexedItem.__type and self:getIndexPath() == otherItem:getIndexPath()
and self:getItem() == otherItem:getItem()
end

return IndexedItem
40 changes: 40 additions & 0 deletions cylibs/ui/collection_view/items/image_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function ImageItem.new(imagePath, width, height)
local self = setmetatable({}, ImageItem)
self.imagePath = imagePath
self.size = Frame.new(0, 0, width, height)
self.alpha = 255
self.repeatX = 1
self.repeatY = 1
return self
end

Expand All @@ -34,6 +37,43 @@ function ImageItem:getSize()
return { width = self.size.width, height = self.size.height }
end

---
-- Sets the pattern repeat of the image.
-- @tparam number x Repeat X
-- @tparam number y Repeat Y
--
function ImageItem:setRepeat(x, y)
self.repeatX = x
self.repeatY = y
end

---
-- Get the pattern repeat of the image.
-- @treturn Frame A frame containing the repeatX and repeatY
--
function ImageItem:getRepeat()
return Frame.new(self.repeatX, self.repeatY)
end

---
-- Sets the alpha (transparency) value of the image item.
--
-- @tparam number alpha The alpha value (0 for fully transparent, 255 for fully opaque).
--
function ImageItem:setAlpha(alpha)
self.alpha = alpha
end

---
-- Retrieves the alpha (transparency) value of the image item.
--
-- @treturn number The alpha value (0 for fully transparent, 255 for fully opaque).
--
function ImageItem:getAlpha()
return self.alpha
end


---
-- Checks if this ImageItem is equal to another.
-- @tparam ImageItem otherItem The other ImageItem to compare with.
Expand Down
10 changes: 7 additions & 3 deletions cylibs/ui/collection_view/layouts/horizontal_flow_layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ local Padding = require('cylibs/ui/style/padding')
local HorizontalFlowLayout = {}
HorizontalFlowLayout.__index = HorizontalFlowLayout

function HorizontalFlowLayout.new(itemSpacing, padding)
function HorizontalFlowLayout.new(itemSpacing, padding, sectionSpacing)
local self = setmetatable({}, HorizontalFlowLayout)
self.itemSpacing = itemSpacing or 0
self.padding = padding or Padding.equal(0)
self.sectionSpacing = sectionSpacing or 0
self.scrollEnabled = false
return self
end

Expand All @@ -30,16 +32,18 @@ function HorizontalFlowLayout:layoutSubviews(collectionView)
local cell = collectionView:getDataSource():cellForItemAtIndexPath(indexPath)
cell:setItem(item)

collectionView:addSubview(cell)
collectionView:getContentView():addSubview(cell)

local cellSize = self:sizeForItemAtIndexPath(collectionView, cell)

cell:setPosition(xOffset, self.padding.top)
cell:setSize(cellSize.width, cellSize.height)
cell:setSize(cellSize.width, cellSize.height - self.padding.top - self.padding.bottom)
cell:setVisible(collectionView:getContentView():isVisible())
cell:layoutIfNeeded()

xOffset = xOffset + cellSize.width + self.itemSpacing
end
xOffset = xOffset + self.sectionSpacing
end

self.width = xOffset + self.padding.right
Expand Down
5 changes: 5 additions & 0 deletions cylibs/ui/collection_view/layouts/vertical_flow_layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ function VerticalFlowLayout:sizeForItemAtIndexPath(collectionView, cell)
return { width = collectionView:getSize().width, height = cell:getItemSize() } -- Adjust as needed
end

local num_layout_called = 0

function VerticalFlowLayout:layoutSubviews(collectionView)
num_layout_called = num_layout_called + 1

local yOffset = self.padding.top
for section = 1, collectionView:getDataSource():numberOfSections() do
local numberOfItems = collectionView:getDataSource():numberOfItemsInSection(section)
Expand All @@ -48,6 +52,7 @@ function VerticalFlowLayout:layoutSubviews(collectionView)
-- Set position and size of the cell
cell:setPosition(self.padding.left, yOffset)
cell:setSize(cellSize.width - self.padding.left - self.padding.right, cellSize.height)
cell:setVisible(collectionView:getContentView():isVisible())
cell:layoutIfNeeded()

yOffset = yOffset + cellSize.height + self.itemSpacing
Expand Down
1 change: 1 addition & 0 deletions cylibs/ui/cylibs-ui-includes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ImageCollectionViewCell = require('cylibs/ui/collection_view/cells/image_collect
ImageItem = require('cylibs/ui/collection_view/items/image_item')
ImageView = require('cylibs/ui/image_view')
IndexPath = require('cylibs/ui/collection_view/index_path')
IndexedItem = require('cylibs/ui/collection_view/indexed_item')
ListView = require('cylibs/ui/list_view/list_view')
ListViewItemStyle = require('cylibs/ui/style/list_view_item_style')
Mouse = require('cylibs/ui/input/mouse')
Expand Down
20 changes: 13 additions & 7 deletions cylibs/ui/image_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ num_images_created = 0
-- @tparam string imagePath The path to the image to be displayed in the ImageView.
-- @treturn ImageView The newly created ImageView instance.
--
function ImageView.new()
function ImageView.new(repeatX, repeatY, alpha)
local self = setmetatable(View.new(), ImageView)

self.repeatX = repeatX or 1
self.repeatY = repeatY or 1
self.alpha = alpha or 255
self.imageLoader = ImageLoader.new()
self.image = Image.new()
self.image:fit(true)
self.image:hide()
self.image:draggable(false)

self:getDisposeBag():addAny(L{ self.imageLoader, self.image })

Expand Down Expand Up @@ -50,11 +54,10 @@ function ImageView:loadImage(imagePath)

self.imageLoader = ImageLoader.new()

self:getDisposeBag():add(self.imageLoader:onImageLoaded():addAction(function(_)
self.imageLoader:onImageLoaded():addAction(function(_)
self:setNeedsLayout()
self:layoutIfNeeded()
end), self.imageLoader:onImageLoaded())

end)
self.imageLoader:loadImage(self.image, imagePath)
end

Expand All @@ -72,13 +75,16 @@ function ImageView:layoutIfNeeded()

local position = self:getAbsolutePosition()

if not self:isVisible() then
--self.image:hide()
local isVisible = self:isVisible() and self.imageLoader:isLoaded() and string.len(self.image:path()) > 0
if self.superview then
isVisible = isVisible and self.superview:isVisible()
end

self.image:visible(self:isVisible() and self.imageLoader:isLoaded() and string.len(self.image:path()) > 0)
self.image:repeat_xy(self.repeatX, self.repeatY)
self.image:visible(isVisible)
self.image:pos(position.x, position.y)
self.image:size(self:getSize().width, self:getSize().height)
self.image:alpha(self.alpha)
end

return ImageView
1 change: 1 addition & 0 deletions cylibs/ui/scroll_view/scroll_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function ScrollView:layoutIfNeeded()
end

self.contentView.frame = Frame.new(self.contentOffset.x, self.contentOffset.y, self.frame.width, self.frame.height)
self.contentView:setVisible(self:isVisible())

self.contentView:setNeedsLayout()
self.contentView:layoutIfNeeded()
Expand Down
12 changes: 12 additions & 0 deletions cylibs/ui/style/text_style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ TextStyle.Default = {
255,
true
),
HeaderSmall = TextStyle.new(
Color.clear,
Color.clear,
"Arial",
11,
Color.white,
Color.lightGrey,
2,
1,
255,
true
),
}

return TextStyle
Expand Down
Loading

0 comments on commit a18f75c

Please sign in to comment.