Skip to content

Commit

Permalink
forgot git add
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Feb 12, 2024
1 parent 4fec190 commit 3720185
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions ui/screens/levelselect/level_preview.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
local game_handler = require("game_handler")
local threadify = require("threadify")
local download = threadify.require("ui.overlay.packs.download_thread")

local preview = {}
preview.__index = preview

function preview:new()
return setmetatable({
rotation = 0,
}, preview)
end

function preview:set(game_version, pack, level, has_pack)
local promise
if has_pack == nil then
has_pack = true
end
if has_pack then
promise = game_handler.get_preview_data(game_version, pack)
else
promise = download.get_preview_data(game_version, pack)
end
if promise then
promise:done(function(data)
self.data = data[level]
self.vertices = {}
self.rotation = 0
end)
end
end

function preview:draw(fullscreen)
if not self.data then
return
end
if fullscreen then
local w, h = love.graphics.getDimensions()
love.graphics.push()
love.graphics.translate(w / 2, h / 2)
local zoom_factor = 1 / math.max(1024 / w, 768 / h)
love.graphics.scale(zoom_factor, zoom_factor)
love.graphics.rotate(self.rotation)
self.rotation = self.rotation - self.data.rotation_speed * love.timer.getDelta()
end
self.vertices = self.vertices or {}
local distance = fullscreen and 10000 or 48
local pivot_thickness = fullscreen and 5 or 2
for i = 1, self.data.sides do
local angle1 = i * 2 * math.pi / self.data.sides - math.pi / 2
local cos1 = math.cos(angle1)
local sin1 = math.sin(angle1)
local angle2 = angle1 + 2 * math.pi / self.data.sides
local cos2 = math.cos(angle2)
local sin2 = math.sin(angle2)
-- background
love.graphics.setColor(self.data.background_colors[i])
love.graphics.polygon("fill", 0, 0, cos1 * distance, sin1 * distance, cos2 * distance, sin2 * distance)
self.vertices[(i - 1) * 2 + 1] = cos1 * distance
self.vertices[i * 2] = sin1 * distance
end
local cap_mult = fullscreen and 0.0075 or 1 / 3
-- pivot
love.graphics.setColor(self.data.pivot_color)
local pivot_mult = cap_mult + pivot_thickness / 2 / distance
love.graphics.scale(pivot_mult, pivot_mult)
love.graphics.setLineWidth(pivot_thickness / pivot_mult)
love.graphics.polygon("line", self.vertices)
love.graphics.scale(1 / pivot_mult, 1 / pivot_mult)
-- cap
love.graphics.scale(cap_mult, cap_mult)
love.graphics.setColor(self.data.cap_color)
love.graphics.polygon("fill", self.vertices)
love.graphics.scale(1 / cap_mult, 1 / cap_mult)
if fullscreen then
love.graphics.pop()
end
end

return preview

0 comments on commit 3720185

Please sign in to comment.