Просмотр исходного кода

Get mirror-hud-0.16 example running

mcc 2 лет назад
Родитель
Сommit
8abe99d07d
2 измененных файлов с 58 добавлено и 13 удалено
  1. 1 2
      examples/UI/Window_HUD/main.lua
  2. 57 11
      examples/UI/Window_HUD/mouse.lua

+ 1 - 2
examples/UI/Window_HUD/main.lua

@@ -107,9 +107,8 @@ end
 
 -- Draw HUD overlay
 function lovr.mirror(pass)
-	if mirror then mirror() end
 	pass:origin()
-  pass:setViewPose(1, mat4())
+	pass:setViewPose(1, mat4())
 	pass:setProjection(1, matrix) -- Switch to screen space coordinates
 	drawGrid(pass)
 

+ 57 - 11
examples/UI/Window_HUD/mouse.lua

@@ -1,17 +1,31 @@
 -- Source: https://github.com/bjornbytes/lovr-mouse/
--- Source: c2f704db2463e05c453580b80b26200d5dd508a9
+-- 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 [[
-  typedef enum {
+  enum {
     GLFW_CURSOR = 0x00033001,
     GLFW_CURSOR_NORMAL = 0x00034001,
     GLFW_CURSOR_HIDDEN = 0x00034002,
-    GLFW_CURSOR_DISABLED = 0x00034003
-  } Constants;
-
+    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);
@@ -22,6 +36,9 @@ ffi.cdef [[
   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);
@@ -33,12 +50,12 @@ local window = ffi.C.os_get_glfw_window()
 
 local mouse = {}
 
--- Lovr uses framebuffer scale for everything, but glfw uses window scale for events.
+-- 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]
+  return lovr.system.getWindowWidth() / x[0]
 end
 
 function mouse.getX()
@@ -63,16 +80,18 @@ end
 function mouse.setX(x)
   local y = mouse.getY()
   local scale = mouse.getScale()
-  C.glfwSetCursorPos(window, x/scale, y/scale)
+  C.glfwSetCursorPos(window, x / scale, y / scale)
 end
 
 function mouse.setY(y)
   local x = mouse.getX()
-  C.glfwSetCursorPos(window, x/scale, y/scale)
+  local scale = mouse.getScale()
+  C.glfwSetCursorPos(window, x / scale, y / scale)
 end
 
 function mouse.setPosition(x, y)
-  C.glfwSetCursorPos(window, x/scale, y/scale)
+  local scale = mouse.getScale()
+  C.glfwSetCursorPos(window, x / scale, y / scale)
 end
 
 function mouse.isDown(button, ...)
@@ -88,6 +107,33 @@ 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()
@@ -109,7 +155,7 @@ end)
 C.glfwSetScrollCallback(window, function(target, x, y)
   if target == window then
     local scale = mouse.getScale()
-    lovr.event.push('wheelmoved', x*scale, y*scale)
+    lovr.event.push('wheelmoved', x * scale, y * scale)
   end
 end)