Graphics module small refactor

Graphics module small refactor

-------------------------------------------------------------------------------
-- Provides simple way to load, render and draw things.
-------------------------------------------------------------------------------
-- The primary responsibility for the non.graphics module is the drawing of
-- lines, shapes, text, Images and other Drawable objects onto the screen. Its
-- secondary responsibilities include loading external files (including Images
-- and Fonts) into memory and managing screen geometry.
-- NÖN's coordinate system is rooted in the upper-left corner of the screen,
-- which is at location (0, 0). The x axis is horizontal: larger values are
-- further to the right. The y axis is vertical: larger values are further
-- towards the bottom.
--
-- @module non.graphics

import java, Font, Image, Quad from non
Constants = require "non.constants"
Color = java.require "com.badlogic.gdx.graphics.Color"
Gdx = java.require "com.badlogic.gdx.Gdx"
GL20 = java.require "com.badlogic.gdx.graphics.GL20"
Matrix4 = java.require "com.badlogic.gdx.math.Matrix4"
NonVM = java.require "non.NonVM"
OrthographicCamera = java.require "com.badlogic.gdx.graphics.OrthographicCamera"
ShapeRender = java.require "com.badlogic.gdx.graphics.glutils.ShapeRenderer"
SpriteBatch = java.require "com.badlogic.gdx.graphics.g2d.SpriteBatch"

shader = SpriteBatch\createDefaultShader!
batch = java.new SpriteBatch, 1000, shader
shapes = java.new ShapeRender
font = Font!
shapes\setAutoShapeType true
matrix = java.new Matrix4
color = java.new Color, 1, 1, 1, 1
background = java.new Color, 0.4, 0.3, 0.4, 1
blending = "alpha"
batch\setColor color
shapes\setColor color
font.font\setColor color
camera = java.new OrthographicCamera
camera\setToOrtho true
batch\setProjectionMatrix camera.combined
shapes\setProjectionMatrix camera.combined
matrixDirty = false

check = (textureBased) ->
  if textureBased
    if matrixDirty
      batch\setTransformMatrix matrix
      matrixDirty = false

    if shapes\isDrawing! then NonVM.util\endShapes shapes
    if not batch\isDrawing! then batch\begin!
  else
    if matrixDirty
      shapes\setTransformMatrix matrix
      matrixDirty = false

    if batch\isDrawing! then NonVM.util\endBatch batch
    if not shapes\isDrawing! then shapes\begin!

arc = (mode, x, y, radius, angle1, angle2) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\arc x, y, radius, math.deg(angle1), math.deg(angle2)

circle = (mode, x, y, radius) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\circle x, y, radius

clear = ->
  Gdx.gl\glClearColor background.r, background.g, background.b, background.a
  Gdx.gl\glClear GL20.GL_COLOR_BUFFER_BIT

draw = (image, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true

  w = image\getWidth!
  h = image\getHeight!
  srcX = 0
  srcY = 0
  srcW = w
  srcH = h
  x -= ox
  y -= oy

  batch\draw image.texture, x, y, ox, oy, w, h, sx, sy, r, srcX, srcY, srcW, srcH, false, true

drawq = (image, quad, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true

  w = quad.width
  h = quad.height
  srcX = quad.x
  srcY = quad.y
  srcW = quad.sw
  srcH = quad.sh
  x -= ox
  y -= oy

  batch\draw image.texture, x, y, ox, oy, w, h, sx, sy, r, srcX, srcY, srcW, srcH, false, true

ellipse = (mode, x, y, width, height) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\ellipse x, y, width, height

getBackgroundColor = ->
  return background.r * 255, background.g * 255, background.b * 255, background.a * 255

getBlendMode = ->
  return blending

getColor = ->
  return color.r * 255, color.g * 255, color.b * 255, color.a * 255

getFont = ->
  return font

line = (x1, y1, x2, y2) ->
  check false
  shapes\line x1, y1, x2, y2

newFont = (filename, size, filetype) ->
  return Font filename, size, filetype

newImage = (filename, format, filetype) ->
  return Image filename, format, filetype

newQuad = (x, y, width, height, sw, sh) ->
  return Quad x, y, width, height, sw, sh

origin = ->
  matrix\idt!
  matrixDirty = true

point = (x, y) ->
  check false
  shapes\point x, y, 0

polygon = (mode, ...) ->
  check false
  shapes\set Constants.shapetypes[mode]
  args = table.pack ...

  if type args[1] == "table"
    shapes\polygon args[1]
  else
    shapes\polygon args

present = ->
  if shapes\isDrawing! then NonVM.util\endShapes shapes
  if batch\isDrawing! then NonVM.util\endBatch batch

print = (text, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true
  tmp = nil

  if r != 0
    tmp = batch\getTransformMatrix!
    translate x, y
    rotate r
    translate -x, -y
    batch\setTransformMatrix matrix
    matrixDirty = false

  font.font\getData!\setScale sx, -sy
  font.font\draw batch, text, x - ox * sx, y - oy * sy

  if r != 0
    translate x, y
    rotate -r
    translate -x, -y
    batch\setTransformMatrix tmp
    matrixDirty = false

printf = (text, width=0, align="left", x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true
  tmp = nil

  if r != 0
    tmp = batch\getTransformMatrix!
    translate x, y
    rotate r
    translate -x, -y
    batch\setTransformMatrix matrix
    matrixDirty = false

  font.font\getData!\setScale sx, -sy
  font.font\draw batch, text, x - ox * sx, y - oy * sy, width, Constants.aligns[align], true

  if r != 0
    translate x, y
    rotate -r
    translate -x, -y
    batch\setTransformMatrix tmp
    matrixDirty = false

rectangle = (mode, x, y, width, height) ->
  check false
  shapes\set Constants.shapetypes[mode]

  shapes\rect x, y, width, height

reset = ->
  shader = SpriteBatch\createDefaultShader!
  batch\setShader shader

  background\set 0.4, 0.3, 0.4, 1
  color\set 1, 1, 1, 1

  batch\setColor color
  shapes\setColor color
  font.font\setColor color
  matrix\idt!
  matrixDirty = true

rotate = (radians) ->
  matrix\rotate 0, 0, 1, math.deg(radians)
  matrixDirty = true

scale = (sx, sy) ->
  matrix\scale sx, sy, 1
  matrixDirty = true

setBackgroundColor = (r, g, b, a=255) ->
  background\set r / 255, g / 255, b / 255, a / 255

setBlendMode = (mode) ->
  blending = mode
  blendmode = Constants.blendmodes[blending]
  batch\setBlendFunction blendmode[1], blendmode[2]

setColor =  (r, g, b, a=255) ->
  color\set r / 255, g / 255, b / 255, a / 255
  batch\setColor color
  shapes\setColor color
  font.font\setColor color

setFont = (newFont) ->
  font = newFont

setNewFont = (filename, size, filetype) ->
  font = newFont filename, size, filetype

translate = (tx, ty) ->
  matrix\translate tx, ty, 0
  matrixDirty = true

{
  :arc
  :circle
  :clear
  :draw
  :drawq
  :ellipse
  :getBackgroundColor
  :getBlendMode
  :getColor
  :getFont
  :line
  :newFont
  :newImage
  :newQuad
  :origin
  :point
  :polygon
  :present
  :print
  :printf
  :rectangle
  :reset
  :rotate
  :scale
  :setBackgroundColor
  :setBlendMode
  :setColor
  :setFont
  :setNewFont
  :translate
}

-------------------------------------------------------------------------------
-- Provides simple way to load, render and draw things.
-------------------------------------------------------------------------------
-- The primary responsibility for the non.graphics module is the drawing of
-- lines, shapes, text, Images and other Drawable objects onto the screen. Its
-- secondary responsibilities include loading external files (including Images
-- and Fonts) into memory and managing screen geometry.
-- NÖN's coordinate system is rooted in the upper-left corner of the screen,
-- which is at location (0, 0). The x axis is horizontal: larger values are
-- further to the right. The y axis is vertical: larger values are further
-- towards the bottom.
--
-- @module non.graphics

import java, Font, Image, Quad from non
Constants = require "non.constants"
Color = java.require "com.badlogic.gdx.graphics.Color"
Gdx = java.require "com.badlogic.gdx.Gdx"
GL20 = java.require "com.badlogic.gdx.graphics.GL20"
Matrix4 = java.require "com.badlogic.gdx.math.Matrix4"
NonVM = java.require "non.NonVM"
OrthographicCamera = java.require "com.badlogic.gdx.graphics.OrthographicCamera"
ShapeRender = java.require "com.badlogic.gdx.graphics.glutils.ShapeRenderer"
SpriteBatch = java.require "com.badlogic.gdx.graphics.g2d.SpriteBatch"

class Transform
  new: =>
    @matrix = java.new Matrix4

  identity: =>
    @matrix\idt!

  rotate: (radians) =>
    degrees = math.deg radians
    @matrix\rotate 0, 0, 1, degrees

  scale: (x, y) =>
    @matrix\scale x, y, 1

  translate: (x, y) =>
    @matrix\translate x, y, 0

shader = SpriteBatch\createDefaultShader!
batch = java.new SpriteBatch, 1000, shader
shapes = java.new ShapeRender
font = Font!
shapes\setAutoShapeType true
transform = Transform!
color = java.new Color, 1, 1, 1, 1
background = java.new Color, 0.4, 0.3, 0.4, 1
blending = "alpha"
batch\setColor color
shapes\setColor color
font.font\setColor color
camera = java.new OrthographicCamera
camera\setToOrtho true
batch\setProjectionMatrix camera.combined
shapes\setProjectionMatrix camera.combined
matrixDirty = false

check = (textureBased) ->
  if textureBased
    if matrixDirty
      batch\setTransformMatrix transform.matrix
      matrixDirty = false

    if shapes\isDrawing! then NonVM.util\endShapes shapes
    if not batch\isDrawing! then batch\begin!
  else
    if matrixDirty
      shapes\setTransformMatrix transform.matrix
      matrixDirty = false

    if batch\isDrawing! then NonVM.util\endBatch batch
    if not shapes\isDrawing! then shapes\begin!

arc = (mode, x, y, radius, angle1, angle2) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\arc x, y, radius, math.deg(angle1), math.deg(angle2)

circle = (mode, x, y, radius) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\circle x, y, radius

clear = ->
  Gdx.gl\glClearColor background.r, background.g, background.b, background.a
  Gdx.gl\glClear GL20.GL_COLOR_BUFFER_BIT

draw = (image, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true

  w = image\getWidth!
  h = image\getHeight!
  srcX = 0
  srcY = 0
  srcW = w
  srcH = h
  x -= ox
  y -= oy

  batch\draw image.texture, x, y, ox, oy, w, h, sx, sy, r, srcX, srcY, srcW, srcH, false, true

drawq = (image, quad, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true

  w = quad.width
  h = quad.height
  srcX = quad.x
  srcY = quad.y
  srcW = quad.sw
  srcH = quad.sh
  x -= ox
  y -= oy

  batch\draw image.texture, x, y, ox, oy, w, h, sx, sy, r, srcX, srcY, srcW, srcH, false, true

ellipse = (mode, x, y, width, height) ->
  check false
  shapes\set Constants.shapetypes[mode]
  shapes\ellipse x, y, width, height

getBackgroundColor = ->
  return background.r * 255, background.g * 255, background.b * 255, background.a * 255

getBlendMode = ->
  return blending

getColor = ->
  return color.r * 255, color.g * 255, color.b * 255, color.a * 255

getFont = ->
  return font

line = (x1, y1, x2, y2) ->
  check false
  shapes\line x1, y1, x2, y2

newFont = (filename, size, filetype) ->
  return Font filename, size, filetype

newImage = (filename, format, filetype) ->
  return Image filename, format, filetype

newQuad = (x, y, width, height, sw, sh) ->
  return Quad x, y, width, height, sw, sh

origin = ->
  transform\identity!
  matrixDirty = true

point = (x, y) ->
  check false
  shapes\point x, y, 0

polygon = (mode, ...) ->
  check false
  shapes\set Constants.shapetypes[mode]
  args = table.pack ...

  if type args[1] == "table"
    shapes\polygon args[1]
  else
    shapes\polygon args

present = ->
  if shapes\isDrawing! then NonVM.util\endShapes shapes
  if batch\isDrawing! then NonVM.util\endBatch batch

print = (text, x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true
  tmp = nil

  if r != 0
    tmp = batch\getTransformMatrix!
    transform\translate(x, y)\rotate(r)\translate(-x, -y)
    batch\setTransformMatrix transform.matrix

  font.font\getData!\setScale sx, -sy
  font.font\draw batch, text, x - ox * sx, y - oy * sy

  if r != 0
    transform\translate(x, y)\rotate(-r)\translate(-x, -y)
    batch\setTransformMatrix tmp

printf = (text, width=0, align="left", x=0, y=0, r=0, sx=1, sy=1, ox=0, oy=0) ->
  check true
  tmp = nil

  if r != 0
    tmp = batch\getTransformMatrix!
    transform\translate(x, y)\rotate(r)\translate(-x, -y)
    batch\setTransformMatrix transform.matrix

  font.font\getData!\setScale sx, -sy
  font.font\draw batch, text, x - ox * sx, y - oy * sy, width, Constants.aligns[align], true

  if r != 0
    transform\translate(x, y)\rotate(-r)\translate(-x, -y)
    batch\setTransformMatrix tmp

rectangle = (mode, x, y, width, height) ->
  check false
  shapes\set Constants.shapetypes[mode]

  shapes\rect x, y, width, height

reset = ->
  shader = SpriteBatch\createDefaultShader!
  batch\setShader shader

  background\set 0.4, 0.3, 0.4, 1
  color\set 1, 1, 1, 1

  batch\setColor color
  shapes\setColor color
  font.font\setColor color
  transform\identity!
  matrixDirty = true

rotate = (radians) ->
  transform\rotate radians
  matrixDirty = true

scale = (sx, sy) ->
  transform\scale sx, sy
  matrixDirty = true

setBackgroundColor = (r, g, b, a=255) ->
  background\set r / 255, g / 255, b / 255, a / 255

setBlendMode = (mode) ->
  blending = mode
  blendmode = Constants.blendmodes[blending]
  batch\setBlendFunction blendmode[1], blendmode[2]

setColor =  (r, g, b, a=255) ->
  color\set r / 255, g / 255, b / 255, a / 255
  batch\setColor color
  shapes\setColor color
  font.font\setColor color

setFont = (newFont) ->
  font = newFont

setNewFont = (filename, size, filetype) ->
  font = newFont filename, size, filetype

translate = (tx, ty) ->
  transform\translate tx, ty
  matrixDirty = true

{
  :arc
  :circle
  :clear
  :draw
  :drawq
  :ellipse
  :getBackgroundColor
  :getBlendMode
  :getColor
  :getFont
  :line
  :newFont
  :newImage
  :newQuad
  :origin
  :point
  :polygon
  :present
  :print
  :printf
  :rectangle
  :reset
  :rotate
  :scale
  :setBackgroundColor
  :setBlendMode
  :setColor
  :setFont
  :setNewFont
  :translate
}


Another go at the Joystick API

Another go at the Joystick API

export ^

class JoyButtonState
	BUTTON_ON: 1
	BUTTON_OFF: 2
	BUTTON_PRESSED: 3
	BUTTON_RELEASED: 4

Joystick = nil

if LP.__love "0.8"
	Joystick = class

else if LP.__love "0.9"
	Joystick = class
		new: =>
			@@joysticks = love.joystick.getJoysticks! if @@joysticks == nil
			@@opened = {}

			-- Each axis contained in an array
			@__axis = {}
			-- A point containing the joystick's ball value
			@ball = Point!
			-- A map of the buttons and their state
			@buttons = {}
			-- A map of the buttons and their state from the previous update
			@__last_buttons = {}
			-- A point containing the joystick's hat value
			@hat = {}
			-- Determines the joystick's deadZone.
			-- Anything under this value will be considered 0 to prevent jitter.
			@deadZone = 0
			-- If the joystick is currently connected.
			@connected = false

			open @

		open = =>
			print #@@joysticks
			i = 1
			for k, v in pairs @@opened
				break if k != i
				i += 1
			if i > #@@joysticks
				print "No more joysticks"
			return if i > #@@joysticks
			@@opened[i] = @@joysticks[i]
			@__number = i
			@joystick = @@joysticks
			@connected = true

			@buttons[i] = JoyButtonState.BUTTON_OFF for i=1, @joystick\getButtonCount!
			@__last_buttons = {k, v for k, v in pairs @buttons}

		close: =>
			@@opened[@__number] = nil
			@joystick = nil
			@__number = 0
			@connected = false

		-- If the joystick button is held down.
		-- @param button The button index to check.
		check: (button) => @buttons[button] == JoyButtonState.BUTTON_ON

		-- Returns the axis value (from 0 to 1)
		-- @param a The axis index to retrieve starting at 0
		getAxis: (a) => @__axis[a]

		-- If the joystick button was pressed this frame.
		-- @param button The button index to check.
		pressed: (button) => @buttons[button] == JoyButtonState.BUTTON_PRESSED

		-- If the joystick button was released this frame.
		-- @param button The button index to check.
		released: (button) => @buttons[button] == JoyButtonState.BUTTON_PRESSED

		-- Updates the joystick's state.
		update: =>
			updateButtons @
			updateAxes @
			updateBalls @
			updateHats @

		updateButtons = =>
			@__last_buttons = {k, v for k, v in pairs @buttons}
			for i=1, #@buttons
				button = @joystick\isDown i
				if button
					if @__last_buttons[i] == JoyButtonState.BUTTON_PRESSED or @__last_buttons[i] == JoyButtonState.BUTTON_ON
						@buttons[i] = JoyButtonState.BUTTON_ON
					else
						@buttons[i] = JoyButtonState.BUTTON_PRESSED
				else
					if @__last_buttons[i] == JoyButtonState.BUTTON_PRESSED or @__last_buttons[i] == JoyButtonState.BUTTON_ON
						@buttons[i] = JoyButtonState.BUTTON_RELEASED
					else
						@buttons[i] = JoyButtonState.BUTTON_OFF

		updateAxes = =>

		updateBalls = =>

		updateHats = =>

export ^

class JoyButtonState
	BUTTON_ON: 1
	BUTTON_OFF: 2
	BUTTON_PRESSED: 3
	BUTTON_RELEASED: 4

loveJoystick = nil
if LP.__love "0.8"
	loveJoystick = class

else if LP.__love "0.9"
	loveJoystick = class

class Joystick extends loveJoystick
	new: =>
		@test!
		-- Each axis contained in an array
		@__axis = {}
		-- A point containing the joystick's ball value
		@ball = Point!
		-- A map of the buttons and their state
		@buttons = {}
		-- A point containing the joystick's hat value
		@hat = {}
		-- Determines the joystick's deadZone.
		-- Anything under this value will be considered 0 to prevent jitter.
		@deadZone = 0