;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname textbox-demo) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;;; Case Study: Let's discuss how to represent a GUI text-box, ;;; and write a couple of functions for it. ;;; Follow steps of design-recipe. ;;; (You may uncomment the 'big-bang' call at very end, to demo. ;;; But only do that AFTER all test-cases pass!) ;;; ;;; See also: textbox-demo-v2.rkt (require 2htdp/image) (require 2htdp/universe) ; Data def'n: (define-struct textbox (txt crsr)) ; make-textbox : string, natural -> textbox ; interpreation: ; `txt` is the contents of the textbox (string) ; `crsr` is the #of chars of `txt` which precede the crsr-location ; must be in the interval [0, `(string-length txt)`]. ; Recall: racket auto-creates constructor as above, getters, and a predicate: ; textbox-txt : textbox -> string ; textbox-crsr : textbox -> natural ; textbox? : ANY -> boolean ; examples of data (make-textbox "hello" 0) ; crsr before entire word (make-textbox "hello" 1) (make-textbox "hello" 5) (make-textbox "hello" 3) (make-textbox "h" 0) (make-textbox "h" 1) (define EMPTY-TEXTBOX (make-textbox "" 0)) ; template: ; func-for-textbox : textbox -> ??? ; (define (func-for-textbox a-textbox) (... (textbox-txt a-textbox) ... (textbox-crsr a-textbox) ...)) ;=========== (check-expect (handle-key (make-textbox "hello" 5) "right") (make-textbox "hello" 5)) (check-expect (handle-key (make-textbox "hello" 0) "left") (make-textbox "hello" 0)) (check-expect (handle-key (make-textbox "hello" 0) "\b") (make-textbox "hello" 0)) (check-expect (handle-key (make-textbox "hello" 3) "z") (make-textbox "helzlo" 4)) (check-expect (handle-key (make-textbox "hello" 3) "right") (make-textbox "hello" 4)) (check-expect (handle-key (make-textbox "hello" 3) "left") (make-textbox "hello" 2)) (check-expect (handle-key (make-textbox "hello" 3) "\b") (make-textbox "helo" 2)) (check-expect (handle-key EMPTY-TEXTBOX "z") (make-textbox "z" 1)) (check-expect (handle-key EMPTY-TEXTBOX "left") EMPTY-TEXTBOX) (check-expect (handle-key EMPTY-TEXTBOX "right") EMPTY-TEXTBOX) (check-expect (handle-key EMPTY-TEXTBOX "\b") EMPTY-TEXTBOX) ; handle-key : textbox?, key-event? -> textbox? ; Update `a-tb` ("a textbox") to incorporate `key`. ; (define (handle-key a-tb key) (cond [(key=? key "right") (make-textbox (textbox-txt a-tb) (min (add1 (textbox-crsr a-tb)) (string-length (textbox-txt a-tb))))] [(key=? key "left") (make-textbox (textbox-txt a-tb) (max 0 (sub1 (textbox-crsr a-tb))))] [(key=? key "\b") (make-textbox (string-append (substring (textbox-txt a-tb) 0 (max 0 (sub1 (textbox-crsr a-tb)))) (substring (textbox-txt a-tb) (textbox-crsr a-tb))) (max 0 (sub1 (textbox-crsr a-tb))))] [else (make-textbox (string-append (substring (textbox-txt a-tb) 0 (textbox-crsr a-tb)) key (substring (textbox-txt a-tb) (textbox-crsr a-tb))) (add1 (textbox-crsr a-tb)))] ) ) (check-expect (draw (make-textbox "hello" 3)) (overlay/align "left" "center" (beside (text "hel" 20 'blue) (rectangle 1 17 'solid 'orange) (text "lo" 20 'blue)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-textbox "hello" 0)) (overlay/align "left" "center" (beside (rectangle 1 17 'solid 'orange) (text "hello" 20 'blue)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-textbox "hello" 5)) (overlay/align "left" "center" (beside (text "hello" 20 'blue) (rectangle 1 17 'solid 'orange)) (rectangle 200 20 'outline 'green))) (check-expect (draw (make-textbox "" 0)) (overlay/align "left" "center" (rectangle 1 17 'solid 'orange) (rectangle 200 20 'outline 'green))) ; draw : textbox? -> image? ; Create an image corresponding to the textbox. ; (define (draw a-textbox) (overlay/align "left" "center" (beside (text (substring (textbox-txt a-textbox) 0 (textbox-crsr a-textbox)) 20 'blue) (rectangle 1 17 'solid 'orange) (text (substring (textbox-txt a-textbox) (textbox-crsr a-textbox)) 20 'blue)) (rectangle 200 20 'outline 'green))) ;----------------------------------- (require 2htdp/universe) ; Note: usually we place any `require`s at the top of our file. #;(big-bang (make-textbox "" 0) [on-draw draw] [on-key handle-key]) #| @author ibarland @version 2018-Sep-27 @license: CC-BY 4.0 -- you are free to share and adapt this file for any purpose, provided you include appropriate attribution. https://creativecommons.org/licenses/by/4.0/ https://creativecommons.org/licenses/by/4.0/legalcode Including a link to the *original* file satisifies "appropriate attribution". |#