Skip to content

Commit

Permalink
scaffold tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
PyryM committed May 10, 2016
1 parent beae1ce commit ee167f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
25 changes: 11 additions & 14 deletions dist/scripts/examples/07_scaffold.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local AppScaffold = require("utils/appscaffold.t").AppScaffold
local icosphere = require("geometry/icosphere.t")
local pbr = require("shaders/pbr.t")
local Object3D = require('gfx/object3d.t').Object3D
local orbitcam = require('gui/orbitcam.t')

function randu(magnitude)
return (math.random() * 2.0 - 1.0)*(magnitude or 1.0)
Expand All @@ -24,27 +25,23 @@ function createGeometry()
end
end

function preRender(appSelf)
rotator.quaternion:fromEuler({x=0,y=app.time*0.25,z=0})
rotator:updateMatrix()
end

function init()
app = AppScaffold({title = "07_scaffold",
width = 1280,
height = 720,
usenvg = false})
app.preRender = preRender

rotator = Object3D()
app.scene:add(rotator)
rotator:add(app.camera)

app.camera.position:set(0.0, 0.0, 10.0)
app.camera:updateMatrix()
app.userEventHandler = onSdlEvent
camerarig = orbitcam.OrbitCameraRig(app.camera)
camerarig:setZoomLimits(1.0, 30.0)
camerarig:set(0, 0, 15.0)
createGeometry()
end

function onSdlEvent(selfapp, evt)
camerarig:updateFromSDL(evt)
end

function update()
camerarig:update(1.0 / 60.0)
app:update()
end
end
31 changes: 24 additions & 7 deletions dist/scripts/gui/orbitcam.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,34 @@ function OrbitCameraRig:init(target)
self.radRate = 0.4
self.panRate = 0.01

self.lastMouseX = 0
self.lastMouseX = 0
self.lastMouseY = 0

-- mouse buttons: 1 = left, 2 = middle, 4 = right
self.rotateMouseButton = 1
self.rotateMouseButton = 1
self.panMouseButton = 2

self.minrad = 0.1
self.maxrad = 10.0
self.orbitpoint = math.Vector(0, 0, 0)
end

function OrbitCameraRig:setZoomLimits(minrad, maxrad)
self.minrad = minrad
self.maxrad = maxrad
return self
end

function OrbitCameraRig:set(theta, phi, rad)
self.phi = phi
self.theta = theta
self.rad = rad
self.phiTarget = phi
self.thetaTarget = theta
self.radTarget = rad
return self
end

-- currently just uses a simple exponential approach
local function tweenTo(val, target, alpha, tolerance, dt)
local dval = target - val
Expand Down Expand Up @@ -84,11 +100,11 @@ function OrbitCameraRig:updateSDLZoom(evt)
end

function OrbitCameraRig:updateFromSDL(evt)
if evt.event_type == sdl.TRUSS_SDL_EVENT_MOUSEWHEEL then
if evt.event_type == sdl.EVENT_MOUSEWHEEL then
self:updateSDLZoom(evt)
return
end
if evt.event_type ~= sdl.TRUSS_SDL_EVENT_MOUSEMOVE then return end
if evt.event_type ~= sdl.EVENT_MOUSEMOVE then return end

local x, y = evt.x, evt.y
local buttons = evt.flags
Expand Down Expand Up @@ -123,8 +139,9 @@ function OrbitCameraRig:updateMatrix_()
local x = rr * math.cos(self.theta)
local z = rr * math.sin(self.theta)
local scale = self.scale
local target = self.target

local pos = target.pos
local pos = target.position
local quat = target.quaternion

pos:set(x, y, z)
Expand All @@ -135,4 +152,4 @@ function OrbitCameraRig:updateMatrix_()
target:updateMatrix()
end

return {OrbitCameraRig = OrbitCameraRig}
return {OrbitCameraRig = OrbitCameraRig}
12 changes: 10 additions & 2 deletions dist/scripts/utils/appscaffold.t
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function AppScaffold:initNVG()
self.nvgfont = nanovg.nvgCreateFont(self.nvg, "sans", "font/VeraMono.ttf")
end

function AppScaffold:onKeyDown(keyname, modifiers)
function AppScaffold:onKeyDown_(keyname, modifiers)
log.info("Keydown: " .. keyname)
if self.keybindings[keyname] ~= nil then
self.keybindings[keyname](keyname, modifiers)
Expand All @@ -128,14 +128,22 @@ function AppScaffold:setKeyBinding(keyname, func)
self.keybindings[keyname] = func
end

function AppScaffold:onKey(keyname, func)
self:setKeyBinding(keyname, func)
end

function AppScaffold:onMouseMove(func)
self.mousemove = func
end

function AppScaffold:updateEvents()
for evt in sdl:events() do
if evt.event_type == sdl.EVENT_KEYDOWN or evt.event_type == sdl.EVENT_KEYUP then
local keyname = ffi.string(evt.keycode)
if evt.event_type == sdl.EVENT_KEYDOWN then
if not self.downkeys[keyname] then
self.downkeys[keyname] = true
self:onKeyDown(keyname, evt.flags)
self:onKeyDown_(keyname, evt.flags)
end
else -- keyup
self.downkeys[keyname] = false
Expand Down

0 comments on commit ee167f8

Please sign in to comment.