-- ADV test -- -- Use json.lua -- rxi/json.lua: A lightweight JSON library for Lua -- https://github.com/rxi/json.lua function love.load() -- load JSON json = require 'json' text = love.filesystem.read('data.json') data = json.decode(text) -- get scene name scene_names = {} for k, _ in pairs(data) do scene_names[#scene_names+1] = k end -- load images imgs = {} for _, v in ipairs(scene_names) do imgs[v] = love.graphics.newImage(data[v]["path"]) end linkname = "Title" cscene = data[linkname] cur_img = imgs[linkname] cur_cursor = "" setNormalCursor() area_debug = false cur_time = 0 step = 0 end function getCrossVec2D(vx1, vy1, vx2, vy2) return (vx1 * vy2 - vx2 * vy1) end function checkInConvexPoly(pos, mx, my) local cc = {} local x0, y0, x1, y1 x0, y0 = pos[#pos - 1], pos[#pos] for i = 1, #pos - 1, 2 do x1, y1 = pos[i], pos[i + 1] cc[#cc + 1] = (getCrossVec2D(x1 - x0, y1 - y0, mx - x1, my - y1) < 0) x0, y0 = x1, y1 end local c0, c1 c0 = cc[1] for i = 2, #cc do c1 = cc[i] if c0 ~= c1 then return false end c0 = c1 end return true end function checkInArea(area, mx, my) local result = false local kind = area["type"] local pos = area["pos"] if kind == "rect" then local x0, y0, x1, y1 x0, y0 = pos[1], pos[2] x1, y1 = pos[3], pos[4] if x0 <= mx and mx <= x1 and y0 <= my and my <= y1 then result = true end elseif kind == "poly" then result = checkInConvexPoly(pos, mx, my) end return result end function setNormalCursor() if cur_cursor ~= "arrow" then local normal_cursor = love.mouse.getSystemCursor("arrow") love.mouse.setCursor(normal_cursor) cur_cursor = "arrow" end end function setHandCursor() if cur_cursor ~= "hand" then local hand_cursor = love.mouse.getSystemCursor("hand") love.mouse.setCursor(hand_cursor) cur_cursor = "hand" end end function love.update(dt) cur_time = cur_time + dt if step == 0 then local areas = cscene["area"] local link = nil local mx, my, down mx, my = love.mouse.getPosition() down = love.mouse.isDown(1) for i = 1, #areas do local area = areas[i] if checkInArea(area, mx, my) then setHandCursor() link = area["link"] elseif not link then setNormalCursor() end end if down and link then cur_time = 0 old_img = cur_img linkname = link cscene = data[linkname] cur_img = imgs[linkname] setNormalCursor() step = 1 end elseif step == 1 then -- change effect cur_time = cur_time + dt if cur_time >= 0.5 then cur_time = 0 step = 0 end end end function draw_area(areas) for _, area in ipairs(areas) do local link = area["link"] local kind = area["type"] local pos = area["pos"] if kind == "rect" then local x0, y0, x1, y1 x0, y0 = pos[1], pos[2] x1, y1 = pos[3], pos[4] w, h = x1 - x0, y1 - y0 love.graphics.setColor(0, 1, 0, 0.5) love.graphics.rectangle("fill", x0, y0, w, h) elseif kind == "poly" then love.graphics.setColor(0, 1, 0.5, 0.5) love.graphics.polygon("fill", pos) end end love.graphics.setColor(1, 1, 1, 1) end function love.draw() love.graphics.clear(0, 0, 0, 1) if step == 0 then -- normal draw love.graphics.setColor(1, 1, 1, 1) love.graphics.draw(cur_img, 0, 0) if area_debug then draw_area(cscene["area"]) end elseif step == 1 then -- change effect love.graphics.setColor(1, 1, 1, 1) love.graphics.draw(old_img, 0, 0) local a = 0 if cur_time >= 0 then a = cur_time / 0.5 if a > 1.0 then a = 1.0 end end love.graphics.setColor(1, 1, 1, a) love.graphics.draw(cur_img, 0, 0) end -- love.graphics.setColor(1,1,1,1) -- love.graphics.print("ESC to exit.", 8, 0 * 16) end function love.keypressed(key, scancode, isrepeat) if key == "escape" then love.event.quit() elseif key == "a" then area_debug = not area_debug end end