+;;;; Ru*** roLeplay Playground virtual tabletop
+;;;; Copyright (C) 2022 Zull
+;;;;
+;;;; This program is free software: you can redistribute it and/or modify
+;;;; it under the terms of the GNU General Public License as published by
+;;;; the Free Software Foundation, either version 3 of the License, or
+;;;; (at your option) any later version.
+;;;;
+;;;; This program is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;;; GNU General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU General Public License
+;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+(in-package :rulp.geometry)
+
+(defparameter *span* 70)
+
+(defun simple-grid (coordinates)
+ "this calculate a simple grid"
+ (cons (* *span* (car coordinates))
+ (* *span* (cdr coordinates))))
+
+;; NOTE: on SLY parameters that contain a function symbol will actually contain
+;; the whole function, try edit the function the *grid-function* parameter links
+;; to and you will see the lambda defined with grid-closure would not change. But
+;; if you recompile this parameter the lambda would update.
+(defparameter *grid-function* #'simple-grid)
+
+(defun grid-closure ()
+ "this function create a lambda to be used for calculation on lower levels of the code
+(pretty much on the layer package, used for correcting placement of entities)"
+ (lambda (coordinates) (apply *grid-function* (list coordinates))))
+
+(defgeneric to-grid (grid par)
+ (:documentation "this converts a par (a pair of number into a cons) in the pixel values that
+would set fit the par into the grid.
+
+(to-grid grid par) -> par
+
+for example a square grid with span 40 would transform the par (4 . 2) into (160
+. 80). When the given grid is t the method will apply the identity
+transformation therefore the method will return the input
+
+(to-grid t par) = par"))
+
+(defmethod to-grid ((grid t) par)
+ (declare (ignore grid))
+ par)
+
+(defgeneric from-grid (grid par)
+ (:documentation "this converts a par from the pixel format into the discrete values of ℤ². This
+is the inverse of to-grid and it is meant for creating discrete coordinates from
+human input.
+
+(from-grid grid par) -> par
+
+when the grid is t the method will become an identity map, returing the input 'as is'.
+
+(from-grid t par) = par"))
+
+(defmethod from-grid ((grid t) par)
+ (declare (ignore grid))
+ par)
+
+(defclass squaregrid ()
+ ((span :accessor span
+ :initarg :span
+ :initform 70
+ :type integer
+ :documentation "the length of the squares that compose the square grid")
+ ))
+
+(defmethod to-grid ((grid squaregrid) par)
+ (cons (* (span grid) (car par))
+ (* (span grid) (cdr par))))
+
+(defmethod from-grid ((grid squaregrid) par)
+ (cons (floor (/ (car par) (span grid)))
+ (floor (/ (cdr par) (span grid))))
+ )
+
+(defmethod display ((object entity) (grid squaregrid))
+ (let ((adj-coos (spr (span grid) (coordinates object)))
+ (adj-size (spr (span grid) (size object))))
+ (render (get-screen (background object))
+ (car adj-coos)
+ (cdr adj-coos)
+ (car adj-size)
+ (cdr adj-size)
+ )))