;;;; 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 . (in-package :graphics) ;; this functions are changed every frame with the mouse position. This ;; is then used everywhere. This assure syncronization between two functions. ;; The values are stored in a cons and can be exported with (x *mouse-position*) ;; and (y *mouse-position*) (defparameter *mouse-position* '(0 . 0) "contains the mouse coordinates in the screen, this is updated by frame") (defparameter *mouse-previous-position* '(0 . 0) "contains the mouse coordinates on the previous frame, this is updated by frame using *mouse-position*. The usefullness of this value is to create velocity") (defparameter *is-mouse-hold* nil "this variable is modified when a button is hold down. This doesn't specifies which button is being hold.") (defparameter +mode+ '+normal-mode+) (defun mouse-actions (keybinds) "given an association list of keybinds it returns the associated element when the button is pressed" (loop :for button :in keybinds :collect (when (sdl2:mouse-state-p (eval (car button))) ; BUG: watch out for this eval (cdr button)))) ;; NOTE: this is a frame dependent version, correction is easy when i'll have ;; time deltas. (defun velocity (actual-point previous-point) "calculate the delta between two points. This is absolutely frame dependent" (cons (- (car actual-point) (car previous-point)) (- (cdr actual-point) (cdr previous-point)))) (defun panning (value delta) "destructive function. It update the value with the given delta. Both value and delta are coordinate cons (like '(x . y))" (setf (car value) (+ (car value) (car delta)) (cdr value) (+ (cdr value) (cdr delta)))) ;; (defgeneric select-entry (x y p) ;; (:documentation "operate with menues, create them, destroy them and apply them")) ;; (defgeneric key-activate (x y pressed p)) ;; (defmethod key-activate (x y pressed (p plane)) ;; ) ;; (defgeneric activate (x y pressed p) ;; (:documentation "given x y and the button pressed it do actions")) ;; (defmethod activate (x y pressed (p plane)) ;; (loop :for key :in *mouse-keybinds* ;; :do ;; (when (sdl2:mouse-state-p (eval (car key))) ;; (apply (cadr key) `(,x ,y ,p)) ;; ))) ;; FIXME: this method can be designed non destructively. by just returning the position ;; in the list of the selected entity (defgeneric select-pointer (coordinate p)) (defmethod select-pointer (coordinate (p plane)) "with left button it select and deselect entities the map-gplane contain" (let ((mouse-point (sdl2:make-rect (car coordinate) (cdr coordinate) 10 10)) (entities (entities-list p))) (setf *pointer* nil) (loop :for obj :in entities :for obj-nth :from 0 :to (length entities) :do (when (sdl2:has-intersect mouse-point (screen-destination obj p)) (setf *pointer* obj-nth))) )) (defgeneric move-entity (coordinate p)) (defmethod move-entity (coordinate (p plane)) "with right button it move the entity around the plane" (when (numberp *pointer*) (let* ((x-offset (x p)) (y-offset (y p)) (i-x (floor (/ (- (car coordinate) x-offset) (grid-dimension p)))) (i-y (floor (/ (- (cdr coordinate) y-offset) (grid-dimension p)))) (object (nth *pointer* (entities-list p)))) (setf (coordinate object) (cons i-x i-y)) ;; (setf (x object) i-x) ;; (setf (y object) i-y) )))