#lang racket ;; interfaces and constants needed by more than one class. (provide SWorld<%> Widget<%> SWidget<%> SWidgetListener<%> SWidgetPublisher<%>) (provide CANVAS-WIDTH CANVAS-HEIGHT) ;; CONSTANTS needed by more than one class. (define CANVAS-WIDTH 400) (define CANVAS-HEIGHT 300) ;; INTERFACES ;; An SWorld is an object of any class that implements SWorld<%> (define SWorld<%> (interface () ; -> Void ; GIVEN: no arguments ; EFFECT: updates this world to its state after a tick after-tick ; Integer Integer MouseEvent-> Void ; GIVEN: an (x,y) location ; EFFECT: updates this world to the state that should follow the ; given mouse event at the given location. after-mouse-event ; KeyEvent : KeyEvent -> Void ; GIVEN: a key event ; EFFECT: updates this world to the state that should follow the ; given key event after-key-event ; -> Scene ; GIVEN: a scene ; RETURNS: a scene that depicts this World to-scene ; Widget -> Void ; GIVEN: A widget ; EFFECT: add the given widget to the world add-widget ; SWidget -> Void ; GIVEN: A stateful widget ; EFFECT: add the given widget to the world add-stateful-widget )) ;; A Widget is an object of any class that implements Widget<%> (define Widget<%> (interface () ; -> Widget ; GIVEN: no arguments ; RETURNS: the state of this object that should follow at time t+1. after-tick ; Integer Integer -> Widget ; GIVEN: an (x,y) location ; RETURNS: the state of this object that should follow the ; specified mouse event at the given location. after-button-down after-button-up after-drag ; KeyEvent -> Widget ; GIVEN: a key event and a time ; RETURNS: the state of this object that should follow the ; given key event after-key-event ; Scene -> Scene ; GIVEN: a scene ; RETURNS: a scene like the given one, but with this object ; painted on it. add-to-scene )) ;; An SWidget is an object of any class that implements the SWidget<%> ;; interface. ;; A SWidget is like a Widget, but it is stable (stateful). (define SWidget<%> (interface () ; -> Void ; GIVEN: no arguments ; EFFECT: updates this widget to the state it should have ; following a tick. after-tick ; Integer Integer -> Void ; GIVEN: a location ; EFFECT: updates this widget to the state it should have ; following the specified mouse event at the given location. after-button-down after-button-up after-drag ; KeyEvent : KeyEvent -> Void ; GIVEN: a key event ; EFFECT: updates this widget to the state it should have ; following the given key event after-key-event ; Scene -> Scene ; GIVEN: a scene ; RETURNS: a scene like the given one, but with this object ; painted on it. add-to-scene )) ;; while we're at it, we'll rename the interfaces to reflect their ;; generic nature. ;; Additional method for SBall% and other classes that receive messages ;; from the wall: (define SWidgetListener<%> (interface (SWidget<%>) ; Int -> Void ; EFFECT: updates the ball's cached value of the wall's position update-wall-pos )) ;; Additional method for classes that send messages to ;; SWidgetListeners. In our example, SWall% is the only such class. (define SWidgetPublisher<%> (interface (SWidget<%>) ; SWidgetListener -> Int ; GIVEN: An SWidgetListener ; EFFECT: registers the listener to receive position updates from this wall. ; RETURNS: the current x-position of the wall register ))