play with picture

Discussion of Common Lisp
Post Reply
freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

play with picture

Post by freezy39 » Wed May 12, 2010 3:28 pm

Hello guys,
could you help me a little bit please?

I want to learn affairs . I mean.... I draw circle with red color.
I need to know, how to implement this: when I click at this circle, it will change the color to black . And when I click again, it will change it back to red.

Could you paste me the code for this pls ?


method for click should be:

Code: Select all

(defmethod send-mouse-down ((w window) button x y)
(window-mouse-down w
button
(move (make-instance 'point) x y)))

Code: Select all

(defmethod install-callbacks ((w window))
(mg:set-callback (slot-value w 'mg-window)
:display (lambda (mgw)
(declare (ignore mgw))
(redraw w)))
(mg:set-callback
(slot-value w 'mg-window)
:mouse-down (lambda (mgw button x y)
(declare (ignore mgw))
(send-mouse-down w button x y)))
w)

Code: Select all

(defmethod window-mouse-down ((w window) button position)
w)

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: play with picture

Post by Jasper » Thu May 13, 2010 6:24 am

You need a library that allows you to make guis or draw/user input. There are lots of bindings for CL that could do this. Which you choose depends on what you want. Personally i prefer CLG for gui, and Lispbuilder-SDL(with cl-opengl) for the other stuff. If you're using Able, Ltk is probably the easiest to start with at this point(though it seems inferior to CLG except at one point to me), as Able uses that.

You know how to install .asd files? Being annoyed with asdf-install, i usually just download the tarball, extract it in a directory where i put the cl libs, and then i have ~/.sbcl/systems/more.asd with:(required from ~/.sbclrc)(i found adding all those damned symlinks cumbersome.

Code: Select all

(defpackage :asd-directories (:use :cl :asdf))
(in-package :asd-directories)

(defun add (path)
  (pushnew path asdf:*central-registry* :test 'equal))
In Able, there is just a folder you can put the CL libraries in, and if there are no defeaters, you can use it. Otherwise, first see if you're missing dependencies, and search them on cliki, unless it turns out it is more complicated than that.. then it might be better to just ask.

Btw, in this, it seems better to me to use initargs on the class here:

Code: Select all

(move (make-instance 'point) x y))) ;instead
(make-instance 'point :x x :y y)
Unfortunately a vanilla vector library seems missing.. Perhaps i should make a little page for what i use my self. Based on VECTOR, i also made a more ugly version based on classes, but i decided to trust the compiler more. (I don't think that deriving from a numeric-vector -like class is ever a good idea, imo better keep them slots.)

If you want examples, CLG has an hello-world and docs can be gleaned to be usually straightforwardly analogous to here, but yes, it needs to have better docs. And you need to do:

Code: Select all

(setq swank:*communication-style* :spawn)
(gtk:clg-init-with-threading)
And i noticed it sometimes doesn't work first-window of a session. cl-gtk2 is very similar. Not automatically generated, though.. And i haven't ever used gtk(via cl) from windows, which might turn out to be a pain to do, i don't know. However, the gtk ones use classes nicely. The style of using CONFIGURE to change widgets in Ltk is a bit dinky, and in using it, i have felt restricted in what i could do.

Lispbuilder-sdl has a tonne of examples, if you're making a game light-on gui this one might be handy, though there aught to be game-gui libraries for that. It seems likely you can find the docs and figure out how to do it, but feel free to ask if you want a more specific explanation.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: play with picture

Post by nuntius » Thu May 13, 2010 9:28 am

Jasper wrote:The style of using CONFIGURE to change widgets in Ltk is a bit dinky, and in using it, i have felt restricted in what i could do.
CONFIGURE is the entry to Ltk's message-passing object-orientation. A schemer might write (send object message); Ltk says (configure widget options). It would not be very difficult to wrap Ltk in another package exposing proper defstruct-style accessors (including setf expanders).

There is room for improvement in Tk and Ltk; but CONFIGURE has never been my obstacle.

AFAICT, the Tk canvas set the standard for many systems to follow. Unfortunately, it lost momentum and now lacks "standard" features such as rotation. I would be curious whether the new HTML canvas will replace Tk as the simple tool of choice. Is there a browser that exposes the necessary API to not look like a browser? Any good CL libs for HTML canvas manipulation?

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: play with picture

Post by Jasper » Thu May 13, 2010 12:04 pm

You are completely right, by deriving and making an INITIALIZE-INSTANCE which subsequently uses CONFIGURE as needed based on keyword arguments would do it.(This is also very neat to do with the other cl gui libs) Still, if it isn't in some library, one has to wonder if a lot of duplication is being done with it. Maybe it can be done very quickly with macros taking just the different things to configure for the different widgets, as arguments though.

I can't really put my finger on where i felt restricted with ltk, though... Maybe i was wrong. I'll probably try make a LTK output for GIL at some point, then i can compare. (Currently still working on CLG output.) Edit: the C gtk is development-wise alive, and it is damned rich, and well-designed, at least from the user perspective. So that way, at least, it is pretty hard to beat.

For the OP: Tried to make an example, but it doesn't quite do it; text won't change.

Code: Select all

(in-package :ltk) ;Just for convenience.. make your package.

(defmacro setf- (operation variable &rest args)
  "Change by function."
  `(setf ,variable (,operation ,variable ,@args)))

(defun start ()
  (with-ltk ()
    (let (red-p
          (label (make-instance 'button :text "Red")))
      (setf (command label)
            (lambda ()
              (setf- not red-p)
              (multiple-value-bind (background text)
        	  (if red-p (values :blue "Blue")
                      (values :red "Red")) ;Could also set bitmap this way.
                (configure label :background background)
                (configure label :activebackground background) ;Thought i'd do it for me.
                (configure label :text text)))) ;Text won't change :/
      (pack label))))
I remember LTK following the theme of my desktop, but it didn't this time.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: play with picture

Post by nuntius » Thu May 13, 2010 9:02 pm

I posted a moderately full-featured sudoku gameboard to the ltk-user list recently. It illustrates a number of techniques, though the code itself is nothing to be proud of. Probably significant room for improvement, especially in how I render the board. Relaxing weekend projects don't need to be production quality. ;)

Post Reply