Maybe because get-extent in the super class vaches some information and
that cache doesn't get maintained if you override the method.

Robby

On Sun, Jul 21, 2019 at 12:09 PM Alex Harsanyi <alexharsa...@gmail.com>
wrote:

>
> I am not entirely sure why (because I already spent more than 15 minutes
> on this :-) ), but the problem is with the `get-extent` method: just remove
> it, and let `image-snip%` handle the method.
>
> The `copy` method is not used in this case.
>
> Also, in your `on-char` and `on-event` methods, you might want to pass
> back any un-handled events back to the editor's `on-default-event` and
> `on-default-char` methods, otherwise, you will notice that the editor stops
> responding to some events since your code discards them.
>
> Alex.
>
> On Tuesday, July 16, 2019 at 11:01:41 PM UTC+8, Kshitij Sachan wrote:
>>
>> Update:
>>
>> I've narrowed down the problem to the `copy` method of the snip. When I
>> delete the copy method, I can see the snip perfectly, but it can't be
>> resized (because no copy method implemented).
>>
>> I removed the GL component, so now I'm only attempting to extend the
>> image-snip% class to resize a custom bitmap I load in (in my case just a
>> plain red circle I drew). When I highlight over the snip with my cursor, I
>> can see the bitmap below and it is resizing as well on a click (using
>> `set-bitmap`). However, if I don't highlight over the snip it appears plain
>> white.
>>
>> Another observations that may or may not help is:
>> Calling (send s get-bitmap) always returns the bitmap of the same size,
>> even if I've pressed on the snip several times to resize it.
>>
>> Here's the simplified code:
>> (require racket/gui
>>          racket/gui/base)
>>
>> ;; bitmap stuff
>> (define bits (make-bitmap 250 250))
>> (define bits-dc (new bitmap-dc% [bitmap bits]))
>> (send bits-dc set-brush "red" 'solid)
>> (send bits-dc set-pen "black" 3 'solid)
>> (send bits-dc draw-ellipse 1 1 248 248)
>>
>> (define snip (make-object image-snip% bits))
>>
>> ;; snip stuff
>> (define scroll-snip%
>>   (class image-snip%
>>     (init bit-init)
>>     (define snip-bitmap bit-init)
>>     (inherit/super get-admin set-flags set-bitmap)
>>     (init-field [w 100] [h 100])
>>
>>     (super-make-object snip-bitmap)
>>     (set-flags (list 'handles-events))
>>
>>     (define/override (get-extent dc x y width height descent space lspace
>> rspace)
>>       (define (maybe-set-box! b v) (when b (set-box! b v)))
>>       (maybe-set-box! width w)
>>       (maybe-set-box! height h)
>>       (maybe-set-box! descent 1.0)
>>       (maybe-set-box! space 1.0)
>>       (maybe-set-box! lspace 1.0)
>>       (maybe-set-box! rspace 1.0))
>>
>>     (define/override (on-char dc x y editorx editory event)
>>       (cond
>>         [(equal? (send event get-key-code) 'down)
>>          (define admin (get-admin))
>>          (set! w (+ w 20))
>>          (set! h (+ h 20))
>>
>>          ;; brush/pen/draw ellipse
>>          (define new-bits (make-object bitmap% w h))
>>          (send bits-dc set-bitmap new-bits)
>>          (send bits-dc draw-ellipse x y (- w 2) (- h 2))
>>          (set! snip-bitmap new-bits)
>>
>>          (when admin
>>            (send admin resized this #t))]
>>         [(equal? (send event get-key-code) 'up)
>>          (define admin (get-admin))
>>          (set! w (- w 20))
>>          (set! h (- h 20))
>>
>>          ;; brush/pen/draw ellipse
>>          (define new-bits (make-object bitmap% w h))
>>          (send bits-dc set-bitmap new-bits)
>>          (send bits-dc draw-ellipse x y (- w 2) (- h 2))
>>          (super set-bitmap new-bits)
>>
>>          (when admin
>>            (send admin resized this #t))]
>>         [(equal? (send event get-key-code) 'escape)
>>          (define admin (get-admin))
>>          (set! w 150)
>>          (set! h 150)
>>
>>          ;; brush/pen/draw ellipse
>>          (define new-bits (make-object bitmap% w h))
>>          (send bits-dc set-bitmap new-bits)
>>          (send bits-dc draw-ellipse x y (- w 2) (- h 2))
>>          (super set-bitmap new-bits)
>>
>>          (when admin
>>            (send admin resized this #t))]))
>>
>>     (define/override (on-event dc x y editorx editory event)
>>       (cond
>>         [(send event button-down?)
>>          (define admin (get-admin))
>>          (set! w (+ w 20))
>>          (set! h (+ h 20))
>>
>>          ;; brush/pen/draw ellipse
>>          (define new-bits (make-object bitmap% w h))
>>          (send bits-dc set-bitmap new-bits)
>>          (send bits-dc draw-ellipse x y (- w 2) (- h 2))
>>          (super set-bitmap new-bits)
>>
>>          (when admin
>>            (send admin resized this #t))]))
>>
>>     (define/override (draw dc x y left top right bottom dx dy draw-caret)
>>       ;; brush/pen/draw ellipse
>>       ;(define new-bits (make-object bitmap% w h))
>>       ;(send bits-dc set-bitmap new-bits)
>>       ;(send bits-dc draw-ellipse x y (- w 2) (- h 2))
>>       ;(set! snip-bitmap new-bits)
>>       (super draw dc x y left top right bottom dx dy draw-caret))
>>
>>     (define/override (copy)
>>       (make-object scroll-snip% snip-bitmap))
>>     ))
>>
>> (define s (make-object scroll-snip% bits))
>> s
>>
>> I believe there must be a problem with the default implementation of the
>> `copy` method, but I don't know enough to parse what's going on there. Here
>> is the Racket source code implementation of an image-snip%
>> https://github.com/racket/snip/blob/1a0ef3670ebb13e8738d838db167c3abdbf3df28/snip-lib/racket/snip/private/snip.rkt#L888
>> .
>>
>> On Friday, July 12, 2019 at 1:19:39 PM UTC-4, Kshitij Sachan wrote:
>>>
>>> I've been trying to render an OpenGL context using a gl-bitmap and then
>>> displaying that in a snip (to enable user input). I extended the image snip
>>> class and overrode the basic methods to draw, get-extent, etc.
>>>
>>> When I try to display my snip, I see nothing, but when I highlight over
>>> the snip with my cursor I can see the shape underneath! It's clearly
>>> rendering, and I can render the gl-bitmap by itself fine when it is not
>>> connected to a snip. What could possibly be making my snip appear white?
>>>
>>> My two hypotheses are:
>>> 1) I didn't set up the snip-class% correctly. I have no clue what the
>>> purpose of this is (the documentation is challenging to follow here), and I
>>> don't know what argument to provide to `(set-classname "(lib ...)")` (what
>>> should replace the ...?)
>>> 2) I didn't override one of the relevant functions such as
>>> set-unmodified?
>>>
>>> Here is my code (MakeGLSupport and drawAFrame are both calls to extern
>>> "C" functions I'm accessing using the FFI. They setup the triangles in
>>> OpenGL and render those triangles, respectively):
>>>
>>> #lang racket
>>>
>>> (require racket/gui/base
>>>          ffi/unsafe
>>>          ffi/unsafe/define)
>>>
>>> ;; ffi code
>>> (define my-ffi (ffi-lib
>>> "/Users/kshitijsachan/Documents/geopipe/builddir/lib/Debug/libgldraw"))
>>> (define-ffi-definer define-gltest my-ffi)
>>> ;; type def
>>> (define _GLTest-pointer (_cpointer 'GLTest))
>>> ;; functions
>>> (define-gltest makeGLTest (_fun _int _int -> _GLTest-pointer))
>>> (define-gltest drawAFrame (_fun _GLTest-pointer -> _void))
>>>
>>> ;; global variables
>>> (define screen-width 100)
>>> (define screen-height 100)
>>>
>>> ;; gl code
>>> (define gl-config (new gl-config%))
>>> (send gl-config set-double-buffered #f)
>>> (send gl-config set-legacy? #f)
>>> (define gl-bitmap (make-gl-bitmap screen-width screen-height gl-config))
>>> (define gl-dc (new bitmap-dc% [bitmap gl-bitmap]))
>>> (define gl-context (send gl-dc get-gl-context))
>>> (define opengl-snip (make-object image-snip% gl-bitmap))
>>>
>>> ;; bitmap
>>> (define my-bitmap (make-bitmap screen-width screen-height #t))
>>> (define my-dc (new bitmap-dc% [bitmap my-bitmap]))
>>> (send my-dc set-brush "red" 'solid)
>>> (send my-dc set-pen "black" 3 'solid)
>>> (send my-dc draw-ellipse 1 1 98 98)
>>>
>>> ;; snip-class (THIS IS WHAT I HAVE A QUESTION ON)
>>> (define scroll-snip-class%
>>>    (class snip-class%
>>>      (inherit set-classname)
>>>
>>>      (super-new)
>>>      (set-classname "(lib test-snip)")
>>>      ;; not sure if necessary
>>>      (define/override (read f)
>>>        (define width-b (box 0.0))
>>>        (define height-b (box 0.0))
>>>        (send f get width-b)
>>>        (send f get height-b)
>>>        (new scroll-snip% [snip-width (unbox width-b)] [snip-height
>>> (unbox height-b)]))
>>>      ))
>>>
>>> (define scroll-snip-class (new scroll-snip-class%))
>>>
>>> ;; custom image-snip (THIS IS ALSO WHAT I HAVE A QUESTION ON)
>>> (define scroll-snip%
>>>   (class image-snip%
>>>     ;; set up fields
>>>     (init bit)
>>>     (define snip-bitmap bit)
>>>     (init-field [snip-width screen-width] [snip-height screen-height])
>>>
>>>     ;; make image-snip
>>>     (super-make-object snip-bitmap)
>>>
>>>     ;; add ability to handle events
>>>     (inherit/super set-flags get-admin set-snipclass)
>>>     (set-snipclass scroll-snip-class)
>>>     (set-flags (list 'handles-events))
>>>
>>>     ;; override methods
>>>     (define/override (get-extent dc x y w h descent space lspace rspace)
>>>       (define (maybe-set-box! b v) (when b (set-box! b v)))
>>>       (maybe-set-box! w snip-width)
>>>       (maybe-set-box! h snip-height)
>>>       (maybe-set-box! descent 1.0)
>>>       (maybe-set-box! space 1.0)
>>>       (maybe-set-box! lspace 1.0)
>>>       (maybe-set-box! rspace 1.0))
>>>
>>>     ;; there seems to be a problem when copying (snip looks white but
>>> can be seen when highlighted)
>>>     (define/override (copy)
>>>       (new scroll-snip% [bit snip-bitmap]))
>>>
>>>     (define/override (draw dc x y left top right bottom dx dy draw-caret)
>>>       (send gl-context call-as-current
>>>       (lambda () (drawAFrame (makeGLTest screen-width screen-height))
>>> gl-bitmap))
>>>       (super draw dc x y left top right bottom dx dy draw-caret)
>>>       )
>>>
>>>     (define/override (on-char dc x y editorx editory event)
>>>       (cond
>>>         [(equal? (send event get-key-code) 'up)
>>>          (define admin (get-admin))
>>>          (set! snip-width (+ snip-width 15))
>>>          (set! snip-height (+ snip-height 15))
>>>          (when admin
>>>            (send admin resized this #t))]
>>>         [(equal? (send event get-key-code) 'down)
>>>          (define admin (get-admin))
>>>          (set! snip-width (- snip-width 20))
>>>          (set! snip-height (- snip-height 20))
>>>          (when admin
>>>            (send admin resized this #t))]))
>>>     ))
>>>
>>> (new scroll-snip% [bit gl-bitmap])
>>>
>>> Any help would be appreciated! I've been stuck on this for quite a while
>>> :)
>>>
>>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/dd125579-49d8-431d-939b-155f6ff720e2%40googlegroups.com
> <https://groups.google.com/d/msgid/racket-users/dd125579-49d8-431d-939b-155f6ff720e2%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAL3TdOONBw2WHALcWHWV3wPn7HPnSpP57MGkMzS3VrP9OhDoLA%40mail.gmail.com.

Reply via email to