瀏覽代碼

Simplify Window_HUD;

- mirror quirkiness is resolved
- lovr-mouse no longer needed (!)
bjorn 1 年之前
父節點
當前提交
14d34bc928
共有 2 個文件被更改,包括 18 次插入210 次删除
  1. 18 48
      examples/Flatscreen/Window_HUD/main.lua
  2. 0 162
      examples/Flatscreen/Window_HUD/mouse.lua

+ 18 - 48
examples/Flatscreen/Window_HUD/main.lua

@@ -6,12 +6,6 @@
 
 local shader = require 'shader'
 
--- In order for lovr.mouse to work, and therefore for this example to work,
--- we must be using LuaJIT and we must be using GLFW (ie: we can't be on Android)
-if type(jit) == 'table' and lovr.system.getOS() ~= "android" then
-  lovr.mouse = require 'mouse'
-end
-
 local mirror = lovr.mirror              -- Backup lovr.mirror before it is overwritten
 local font = lovr.graphics.newFont(24)  -- Font appropriate for screen-space usage
 font:setPixelDensity(1)
@@ -47,22 +41,20 @@ local matrix = lovr.math.newMat4():orthographic(-aspect, aspect, -1, 1, -64, 64)
 local grid = {}
 for x=1,cells do grid[x] = {} end
 
-function lovr.load()
-  lovr.handlers['mousepressed'] = function(x,y)
-    local inx = x * width / pixwidth - width/2    -- Convert pixel x,y to our coordinate system
-    local iny = y * height / pixheight - height/2
-    local gridorigin = -gridspan - cellheight     -- Upper left of grid ()
-    local gx = (inx - gridorigin) / cellheight    -- Convert coordinate system to grid cells
-    local gy = (iny - gridorigin) / cellheight
-    local fx = math.floor(gx)
-    local fy = math.floor(gy)
-    if fx >= 1 and fy >= 1 and fx <= cells and fy <= cells   -- If the click was within the grid
-       and not (fx == bannedcell and fy == bannedcell) then  -- and was not the banned center cell
-      if grid[fx][fy] then
-        grid[fx][fy] = nil                -- toggle off
-      else
-        grid[fx][fy] = lovr.math.random() -- toggle on (random height)
-      end
+function lovr.mousepressed(x, y, b)
+  local inx = x * width / pixwidth - width/2    -- Convert pixel x,y to our coordinate system
+  local iny = y * height / pixheight - height/2
+  local gridorigin = -gridspan - cellheight     -- Upper left of grid ()
+  local gx = (inx - gridorigin) / cellheight    -- Convert coordinate system to grid cells
+  local gy = (iny - gridorigin) / cellheight
+  local fx = math.floor(gx)
+  local fy = math.floor(gy)
+  if fx >= 1 and fy >= 1 and fx <= cells and fy <= cells   -- If the click was within the grid
+     and not (fx == bannedcell and fy == bannedcell) then  -- and was not the banned center cell
+    if grid[fx][fy] then
+      grid[fx][fy] = nil                -- toggle off
+    else
+      grid[fx][fy] = lovr.math.random() -- toggle on (random height)
     end
   end
 end
@@ -137,38 +129,16 @@ local function draw(pass)
     if gray then floorbox(pass,x,y,gray) end
   end end
   pass:setShader()
-
-  if not lovr.mouse then -- If you can't click, you can't create any blocks
-    pass:text('This example only works on a desktop computer.', 0, 1.7, -3, .2)
-  end
 end
 
 -- Handle LOVR
 
--- LOVR expects that if you have both a lovr.mirror and a lovr.draw, it's because you want to draw
--- different things in the headset and the window. So in 0.16, it "clears" before calling lovr.mirror
--- (more accurately, it draws the headset and the window to different back buffers entirely).
--- However, we want OUR window to show the headset contents, and ON TOP of that, we want to draw.
--- So we have to trick LOVR a little bit...
-
--- Our strategy will be different on Desktop (the simulator) vs other drivers.
-local isDesktop = lovr.headset.getDriver() == "desktop"
-
 function lovr.draw(pass)
   draw(pass)       -- Headset contents
-
-  -- On desktop, we define ONLY lovr.draw, no lovr.mirror, and draw the mirror contents inside lovr.draw().
-  -- This is because on desktop, a lovr.mirror being present will prevent lovr.draw() from being called at all.
-  if isDesktop then
-    mirror(pass) -- Mirror contents
-  end
 end
 
--- When headsets are plugged in, we want both a lovr.draw and a lovr.mirror.
-if not isDesktop then
-  local originalMirror = lovr.mirror -- By default, LOVR will have given us a mirror that displays the headset
-  function lovr.mirror(pass)
-    originalMirror(pass) -- Headset texture (note: this will fill the depth buffer with z=0)
-    mirror(pass)         -- Mirror contents
-  end
+local originalMirror = lovr.mirror -- By default, LOVR will have given us a mirror that displays the headset
+function lovr.mirror(pass)
+  originalMirror(pass) -- Headset texture (note: this will fill the depth buffer with z=0)
+  mirror(pass)         -- Mirror contents
 end

+ 0 - 162
examples/Flatscreen/Window_HUD/mouse.lua

@@ -1,162 +0,0 @@
--- Source: https://github.com/bjornbytes/lovr-mouse/
--- Source: 7e34853a5764c4727ae689c971c90f8944a7f0c8
-
-assert(type(jit) == 'table' and lovr.system.getOS() ~= 'Android', 'lovr-mouse cannot run on this platform')
-local ffi = require 'ffi'
-local C = ffi.os == 'Windows' and ffi.load('glfw3') or ffi.C
-
-ffi.cdef [[
-  enum {
-    GLFW_CURSOR = 0x00033001,
-    GLFW_CURSOR_NORMAL = 0x00034001,
-    GLFW_CURSOR_HIDDEN = 0x00034002,
-    GLFW_CURSOR_DISABLED = 0x00034003,
-    GLFW_ARROW_CURSOR = 0x00036001,
-    GLFW_IBEAM_CURSOR = 0x00036002,
-    GLFW_CROSSHAIR_CURSOR = 0x00036003,
-    GLFW_HAND_CURSOR = 0x00036004,
-    GLFW_HRESIZE_CURSOR = 0x00036005,
-    GLFW_VRESIZE_CURSOR = 0x00036006
-  };
-
-  typedef struct {
-    int width;
-    int height;
-    unsigned char* pixels;
-  } GLFWimage;
-
-  typedef struct GLFWcursor GLFWcursor;
-  typedef struct GLFWwindow GLFWwindow;
-  typedef void(*GLFWmousebuttonfun)(GLFWwindow*, int, int, int);
-  typedef void(*GLFWcursorposfun)(GLFWwindow*, double, double);
-  typedef void(*GLFWscrollfun)(GLFWwindow*, double, double);
-
-  GLFWwindow* os_get_glfw_window(void);
-  void glfwGetInputMode(GLFWwindow* window, int mode);
-  void glfwSetInputMode(GLFWwindow* window, int mode, int value);
-  void glfwGetCursorPos(GLFWwindow* window, double* x, double* y);
-  void glfwSetCursorPos(GLFWwindow* window, double x, double y);
-  GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot);
-  GLFWcursor* glfwCreateStandardCursor(int kind);
-  void glfwSetCursor(GLFWwindow* window, GLFWcursor* cursor);
-  int glfwGetMouseButton(GLFWwindow* window, int button);
-  void glfwGetWindowSize(GLFWwindow* window, int* width, int* height);
-  GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun callback);
-  GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* window, GLFWcursorposfun callback);
-  GLFWcursorposfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun callback);
-]]
-
-local window = ffi.C.os_get_glfw_window()
-
-local mouse = {}
-
--- LÖVR uses framebuffer scale for everything, but glfw uses window scale for events.
--- It is necessary to convert between the two at all boundaries.
-function mouse.getScale()
-  local x, _ = ffi.new('int[1]'), ffi.new('int[1]')
-  C.glfwGetWindowSize(window, x, _)
-  return lovr.system.getWindowWidth() / x[0]
-end
-
-function mouse.getX()
-  local x = ffi.new('double[1]')
-  C.glfwGetCursorPos(window, x, nil)
-  return x[0] * mouse.getScale()
-end
-
-function mouse.getY()
-  local y = ffi.new('double[1]')
-  C.glfwGetCursorPos(window, nil, y)
-  return y[0] * mouse.getScale()
-end
-
-function mouse.getPosition()
-  local x, y = ffi.new('double[1]'), ffi.new('double[1]')
-  local scale = mouse.getScale()
-  C.glfwGetCursorPos(window, x, y)
-  return x[0] * scale, y[0] * scale
-end
-
-function mouse.setX(x)
-  local y = mouse.getY()
-  local scale = mouse.getScale()
-  C.glfwSetCursorPos(window, x / scale, y / scale)
-end
-
-function mouse.setY(y)
-  local x = mouse.getX()
-  local scale = mouse.getScale()
-  C.glfwSetCursorPos(window, x / scale, y / scale)
-end
-
-function mouse.setPosition(x, y)
-  local scale = mouse.getScale()
-  C.glfwSetCursorPos(window, x / scale, y / scale)
-end
-
-function mouse.isDown(button, ...)
-  if not button then return false end
-  return C.glfwGetMouseButton(window, button - 1) > 0 or mouse.isDown(...)
-end
-
-function mouse.getRelativeMode()
-  return C.glfwGetInputMode(window, C.GLFW_CURSOR) == C.GLFW_CURSOR_DISABLED
-end
-
-function mouse.setRelativeMode(enable)
-  C.glfwSetInputMode(window, C.GLFW_CURSOR, enable and C.GLFW_CURSOR_DISABLED or C.GLFW_CURSOR_NORMAL)
-end
-
-function mouse.newCursor(source, hotx, hoty)
-  if type(source) == 'string' or tostring(source) == 'Blob' then
-    source = lovr.data.newImage(source, false)
-  else
-    assert(tostring(source) == 'Image', 'Bad argument #1 to newCursor (Image expected)')
-  end
-  local image = ffi.new('GLFWimage', source:getWidth(), source:getHeight(), source:getPointer())
-  return C.glfwCreateCursor(image, hotx or 0, hoty or 0)
-end
-
-function mouse.getSystemCursor(kind)
-  local kinds = {
-    arrow = C.GLFW_ARROW_CURSOR,
-    ibeam = C.GLFW_IBEAM_CURSOR,
-    crosshair = C.GLFW_CROSSHAIR_CURSOR,
-    hand = C.GLFW_HAND_CURSOR,
-    sizewe = C.GLFW_HRESIZE_CURSOR,
-    sizens = C.GLFW_VRESIZE_CURSOR
-  }
-  assert(kinds[kind], string.format('Unknown cursor %q', tostring(kind)))
-  return C.glfwCreateStandardCursor(kinds[kind])
-end
-
-function mouse.setCursor(cursor)
-  C.glfwSetCursor(window, cursor)
-end
-
-C.glfwSetMouseButtonCallback(window, function(target, button, action, mods)
-  if target == window then
-    local x, y = mouse.getPosition()
-    lovr.event.push(action > 0 and 'mousepressed' or 'mousereleased', x, y, button + 1, false)
-  end
-end)
-
-local px, py = mouse.getPosition()
-C.glfwSetCursorPosCallback(window, function(target, x, y)
-  if target == window then
-    local scale = mouse.getScale()
-    x = x * scale
-    y = y * scale
-    lovr.event.push('mousemoved', x, y, x - px, y - py, false)
-    px, py = x, y
-  end
-end)
-
-C.glfwSetScrollCallback(window, function(target, x, y)
-  if target == window then
-    local scale = mouse.getScale()
-    lovr.event.push('wheelmoved', x * scale, y * scale)
-  end
-end)
-
-return mouse