From 8a2b62b788983b7d63596dc8c0b01d1a8752a6f3 Mon Sep 17 00:00:00 2001 From: parameterized Date: Fri, 5 Oct 2018 19:37:14 -0400 Subject: [PATCH] procedural item info panel, hand cursor --- client.lua | 2 -- cursor.lua | 21 +++++++++++++++++ gfx/ui/cursors/hand.png | Bin 0 -> 251 bytes gfx/ui/item_info.png | Bin 0 -> 1928 bytes hud.lua | 25 ++++++++++++++++++++ loadassets.lua | 9 +++++--- lootBags.lua | 8 ++++++- main.lua | 12 +++++----- menu.lua | 10 +++++--- shaders/outline.glsl | 4 ++-- shaders/panel.glsl | 50 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 cursor.lua create mode 100644 gfx/ui/cursors/hand.png create mode 100644 gfx/ui/item_info.png create mode 100644 shaders/panel.glsl diff --git a/client.lua b/client.lua index b871fd4..73d8280 100644 --- a/client.lua +++ b/client.lua @@ -24,7 +24,6 @@ function client.connect(ip, port) gameState = 'menu' menu.state = 'main' client.close() - love.mouse.setVisible(true) love.mouse.setGrabbed(false) end, add = function(self, data) @@ -133,7 +132,6 @@ function client.startGame(data) p.name = data.name p:setState(data) gameState = 'playing' - love.mouse.setVisible(false) if menu.cursorLockBtn.active then love.mouse.setGrabbed(true) end end diff --git a/cursor.lua b/cursor.lua new file mode 100644 index 0000000..2a0d1bc --- /dev/null +++ b/cursor.lua @@ -0,0 +1,21 @@ + +cursor = {} + +cursor.main = { + img = gfx.cursors.main, + ox = 0, oy = 0 +} +cursor.hand = { + img = gfx.cursors.hand, + ox = 2, oy = 0 +} + +cursor.cursor = cursor.main + +function cursor.draw() + local mx, my = window2game(love.mouse.getPosition()) + mx, my = lume.round(mx), lume.round(my) + + love.graphics.setColor(1, 1, 1) + love.graphics.draw(cursor.cursor.img, mx, my, 0, 1, 1, cursor.cursor.ox, cursor.cursor.oy) +end diff --git a/gfx/ui/cursors/hand.png b/gfx/ui/cursors/hand.png new file mode 100644 index 0000000000000000000000000000000000000000..f6c72b47ce080d1ce562c01485ee63e333e28a2c GIT binary patch literal 251 zcmeAS@N?(olHy`uVBq!ia0vp^96-#)!3HEdkIOdzDaPU;cPEB*=VV?2IV|apzK#qG z8~eHcB(eheoCO|{#S9F5he4R}c>anMprB-lYeY$Kep*R+Vo@qXL1JcJiC$i6iGqoq zfu51!M}^F(Kt+>=Jva5t|2K-hQNM56<`gzXh?@Q$Pl@1h?i5>seCsz=&J;+P3;I=(S q;Fg(*w~bBG${Mv>0(P%(b&0zu+nKXk&i)9{S_V&7KbLh*2~7aBcvATQ literal 0 HcmV?d00001 diff --git a/gfx/ui/item_info.png b/gfx/ui/item_info.png new file mode 100644 index 0000000000000000000000000000000000000000..7f4b20849175e50f76e142c58b325bdd8bdda7d3 GIT binary patch literal 1928 zcmX|Cdpy(o8((%>k}=|7E@ubPkF;cq#b`G7lY7Ewh7ry|wpxvZRW9e8P)Fpru3YA_ zWpYlYWk{5}*bYUKOIa7W3=!w+yu5yYJfG+FeqNu?^Lc&V&-?j2>F%x$P&v392n2#U zITAbpe-<#JECg7K7lbUp?V@@**n&8{TJHhyT{!L}4g{*p+_ypA4dA`8j($`SNTF@p zc6G#5h5|u#nw>ArD<+IaC&y7hvE+zo+Tmyl4S_mrde}6?sK5mTf~+|aaNZZsd@QiJ zF++g0JLuZ#67*qiCz5<7Wu6oyCE>GRF|8VoBm1&`w&mlmUhPbOhG}zXE@F&}r;T2| zE|%U{`)Y_ib^81WM@m$z#*L=71RYVAhcH{V(rq5EuhMEi_%MlWY8$Jo7qg$6gc)G5 z*kSbB3AwYq#jme;Ql?G3spF3?6W`v%r45O+vI*-hm}haXi|tqfow2vaB?4J=r!&z) z5Wm7PI(pDHRRlJ%YZs&XI9&_1*)_vXBDyN7b5O3i+>sI=roh?ihj{o@g|3MlMRWR+ zri{*#-C;8&tIF~`3jRWm$no}^3iWq$9F&LD4+&FPC-HH)_PmCo<-m4(W-D?Q&a5=S z%twuhv2H_cQRib^ZZsIqbuo~^Ok62uLW>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nSF?6U{Hdb>|6;P I+n|(x0chN*A^-pY literal 0 HcmV?d00001 diff --git a/hud.lua b/hud.lua index dacc4a4..5399623 100644 --- a/hud.lua +++ b/hud.lua @@ -117,6 +117,7 @@ function hud.load() love.graphics.setColor(c, c, 1) end if mx > self.x and mx < self.x + self.img:getWidth() and my > self.y and my < self.y + self.img:getHeight() then + cursor.cursor = cursor.hand local r, g, b = love.graphics.getColor() love.graphics.setColor(r*0.8, g*0.8, b*0.8) end @@ -253,6 +254,8 @@ function hud.keypressed(k, scancode, isrepeat) end function hud.draw() + local _shader = love.graphics.getShader() + local mx, my = window2game(love.mouse.getPosition()) mx, my = lume.round(mx), lume.round(my) @@ -273,6 +276,7 @@ function hud.draw() else love.graphics.setColor(1, 1, 1) if mx > v.x and mx < v.x + v.img:getWidth() and my > v.y and my < v.y + v.img:getHeight() then + cursor.cursor = cursor.hand love.graphics.setColor(0.8, 0.8, 0.8) end love.graphics.draw(v.img, lume.round(v.x), lume.round(v.y)) @@ -321,13 +325,19 @@ function hud.draw() -- inventory items love.graphics.push() + local bag = playerController.player.inventory local panel = hud.inventoryPanel love.graphics.translate(panel.x, panel.y) local pmx = mx - lume.round(panel.x) local pmy = my - lume.round(panel.y) for slotId, slot in ipairs(hud.inventorySlots) do + local item = bag.items[slotId] if pmx >= slot.x and pmx <= slot.x + slot.w and pmy >= slot.y and pmy <= slot.y + slot.h and panel.open then + if item then + cursor.cursor = cursor.hand + lootBags.client.hoveredItem = item + end love.graphics.setColor(1, 1, 1, 0.4) love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h) end @@ -342,6 +352,20 @@ function hud.draw() end love.graphics.pop() + -- item info + if lootBags.client.hoveredItem then + love.graphics.setColor(1, 1, 1) + love.graphics.setShader(shaders.panel) + local w = 104 + local h = 91 + local x = lume.clamp(mx - w, 0, gsx - w) + local y = lume.clamp(my - h, 0, gsy - h) + shaders.panel:send('box', {x, y, w, h}) + love.graphics.rectangle('fill', x, y, w, h) + love.graphics.setShader(_shader) + love.graphics.draw(gfx.ui.itemInfo, x, y) + end + -- held item local heldItem = lootBags.client.heldItem if heldItem.bagId then @@ -352,6 +376,7 @@ function hud.draw() if bag then local item = bag.items[heldItem.slotId] if item then + cursor.cursor = cursor.hand love.graphics.setColor(1, 1, 1) love.graphics.draw(gfx.items[item], mx + heldItem.offset.x, my + heldItem.offset.y) end diff --git a/loadassets.lua b/loadassets.lua index 65fc678..50cca83 100644 --- a/loadassets.lua +++ b/loadassets.lua @@ -18,7 +18,8 @@ canvases = { gfx = { cursors = { - main = love.graphics.newImage('gfx/ui/cursors/cursor.png') + main = love.graphics.newImage('gfx/ui/cursors/cursor.png'), + hand = love.graphics.newImage('gfx/ui/cursors/hand.png') }, logo = love.graphics.newImage('gfx/logo.png'), logoAnim = love.graphics.newImage('gfx/logo_anim.png'), @@ -29,7 +30,8 @@ gfx = { left = love.graphics.newImage('gfx/ui/buttons/left.png'), right = love.graphics.newImage('gfx/ui/buttons/right.png') }, - bag = love.graphics.newImage('gfx/ui/bagui.png') + bag = love.graphics.newImage('gfx/ui/bagui.png'), + itemInfo = love.graphics.newImage('gfx/ui/item_info.png') }, hud = { frame = love.graphics.newImage('gfx/ui/hud/frame.png'), @@ -138,7 +140,8 @@ shaders = { outline = love.graphics.newShader('shaders/outline.glsl'), hpBar = love.graphics.newShader('shaders/hpBar.glsl'), mapGen = love.graphics.newShader('shaders/mapGen.glsl'), - mapRender = love.graphics.newShader('shaders/mapRender.glsl') + mapRender = love.graphics.newShader('shaders/mapRender.glsl'), + panel = love.graphics.newShader('shaders/panel.glsl') } local tileCanv = love.graphics.newCanvas(15, 15) diff --git a/lootBags.lua b/lootBags.lua index 2574b8e..d2b67a1 100644 --- a/lootBags.lua +++ b/lootBags.lua @@ -6,6 +6,7 @@ lootBags = { client = { openRange = 30, closest = {id=nil, dist=nil, open=false}, + hoveredItem = nil, heldItem = {bagId=nil, slotId=nil, offset={x=0, y=0}} } } @@ -94,6 +95,7 @@ function lootBags.client.update(dt) heldItem.bagId = nil heldItem.slotId = nil end + lootBags.client.hoveredItem = nil end function lootBags.client.mousepressed(x, y, btn) @@ -208,14 +210,18 @@ function lootBags.client.draw() local bmx = wmx - (lume.round(bag.x) - lume.round(img:getWidth()/2)) local bmy = wmy - (lume.round(bag.y) - img:getHeight() - 20) for slotId, slot in ipairs(lootBags.client.slots) do + local item = bag.items[slotId] if bmx >= slot.x and bmx <= slot.x + slot.w and bmy >= slot.y and bmy <= slot.y + slot.h then + if item then + cursor.cursor = cursor.hand + lootBags.client.hoveredItem = item + end love.graphics.setColor(1, 1, 1, 0.4) love.graphics.rectangle('fill', slot.x, slot.y, slot.w, slot.h) end local heldItem = lootBags.client.heldItem if not (heldItem.bagId == bag.id and heldItem.slotId == slotId) then - local item = bag.items[slotId] if item then love.graphics.setColor(1, 1, 1) love.graphics.draw(gfx.items[item], slot.x, slot.y) diff --git a/main.lua b/main.lua index 872d969..64cdfc4 100644 --- a/main.lua +++ b/main.lua @@ -7,9 +7,10 @@ Camera = require 'lib.camera' require 'utils' require 'loadassets' -require 'text' require 'server' require 'client' +require 'cursor' +require 'text' require 'menu' require 'hud' require 'physics' @@ -32,6 +33,7 @@ function love.load() drawDebug = false menu.load() hud.load() + love.mouse.setVisible(false) end gameScale = math.min(ssx/gsx, ssy/gsy) @@ -72,6 +74,7 @@ end function love.update(dt) time = time + dt love.window.setTitle('Tier (' .. love.timer.getFPS() .. ' FPS)') + cursor.cursor = cursor.main if server.running then server.update(dt) end @@ -81,7 +84,7 @@ function love.update(dt) menu.update(dt) gcTimer = gcTimer - dt if gcTimer < 0 then - collectgarbage() + --collectgarbage('collect') gcTimer = 10 end end @@ -146,7 +149,6 @@ function love.keypressed(k, scancode, isrepeat) if client.connected then client.close() end - love.mouse.setVisible(true) love.mouse.setGrabbed(false) elseif k == 'f1' then drawDebug = not drawDebug @@ -197,12 +199,10 @@ function love.draw() hud.draw() chat.draw() - - love.graphics.setColor(1, 1, 1) - love.graphics.draw(gfx.cursors.main, mx, my, 0, 1, 1, 0, 0) -- hotspot 0, 0 end menu.draw() + cursor.draw() -- draw game on game2x setGameCanvas2x() diff --git a/menu.lua b/menu.lua index d345115..96e3c4d 100644 --- a/menu.lua +++ b/menu.lua @@ -191,6 +191,7 @@ function menu.load() end end, draw=function(v, mx, my) if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then + cursor.cursor = cursor.hand love.graphics.setColor(0.3, 0.3, 0.3) else love.graphics.setColor(0.4, 0.4, 0.4) @@ -372,6 +373,7 @@ function menu.draw() v.draw(v, mx, my) else if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then + cursor.cursor = cursor.hand love.graphics.setColor(0.3, 0.3, 0.3) else if v.type == 'toggle' and v.active then @@ -396,9 +398,11 @@ function menu.draw() if v.draw then v.draw(v, mx, my) else - if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh - and (menu.activeInput == v or menu.activeInput == nil) or menu.activeInput == v then - love.graphics.setColor(0.3, 0.3, 0.3) + if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then + cursor.cursor = cursor.hand + if (menu.activeInput == v or menu.activeInput == nil) or menu.activeInput == v then + love.graphics.setColor(0.3, 0.3, 0.3) + end else love.graphics.setColor(0.6, 0.6, 0.6) end diff --git a/shaders/outline.glsl b/shaders/outline.glsl index 4d3e99e..41a8b8c 100644 --- a/shaders/outline.glsl +++ b/shaders/outline.glsl @@ -1,6 +1,6 @@ -extern vec2 stepSize; -extern vec4 outlineColor; +uniform vec2 stepSize; +uniform vec4 outlineColor; vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { diff --git a/shaders/panel.glsl b/shaders/panel.glsl new file mode 100644 index 0000000..e0a76c9 --- /dev/null +++ b/shaders/panel.glsl @@ -0,0 +1,50 @@ + +uniform vec4 box; + +vec4 c1 = vec4(vec3(0.0), 1.0); +vec4 c2 = vec4(vec3(38.0/255.0), 1.0); +vec4 c3 = vec4(vec3(51.0/255.0), 1.0); +vec4 c4 = vec4(vec3(63.0/255.0), 1.0); + +int curve_x[7] = int[](6, 4, 3, 2, 2, 1, 1); +int curve_y[6] = int[](7, 5, 3, 2, 1, 1); + +vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) +{ + vec2 uv = screen_coords; + uv -= box.xy; + vec2 border_dist = vec2(min(uv.x, box.z-uv.x), min(uv.y, box.w-uv.y)); + int bdx = int(border_dist.x); + int bdy = int(border_dist.y); + vec4 pixel = vec4(0.0); + bool inner = true; + if (bdx < 7) { + if (bdy == curve_x[bdx]) { + pixel = c1; + } + if (bdy <= curve_x[bdx]) { + inner = false; + } + } + if (bdy < 6) { + if (bdx == curve_y[bdy]) { + pixel = c1; + } + if (bdx <= curve_y[bdy]) { + inner = false; + } + } + if (inner) { + pixel = c2; + } + if (bdx == 0 && bdy > 6 || bdy == 0 && bdx > 7) { + pixel = c1; + } + if (bdx > 3 && bdy > 4 && !(bdx == 4 && bdy == 5)) { + pixel = c3; + if (int(mod(uv.x + uv.y, 9.0)) == 1) { + pixel = c4; + } + } + return pixel*color; +}