Annotation of Examples/AppKit/BackspaceViews/FlexyCube/FlexyCubeView.m, revision 1.1.1.1

1.1       root        1: #import "FlexyCubeView.h"
                      2: #import "Thinker.h"
                      3: #import <appkit/NXImage.h>
                      4: #import <appkit/Panel.h>
                      5: #import <dpsclient/wraps.h>
                      6: #import <libc.h>
                      7: #import <math.h>
                      8: 
                      9: #import "FlexyCubeWraps.h"
                     10: 
                     11: #define ACCELERATION   1.15
                     12: #define FLEXY  0.5
                     13: 
                     14: #define CUBESIZE       150
                     15: #define MINSPEED       1
                     16: 
                     17: @implementation FlexyCubeView
                     18: 
                     19: - drawSelf:(const NXRect *)rects :(int)rectCount
                     20: {
                     21:        if (!rects || !rectCount) return self;
                     22:        [super drawSelf:rects :rectCount];
                     23:        [self drawCube: 1.0];
                     24:        return self;
                     25: }
                     26: 
                     27: - drawCube: (float) gray
                     28: {
                     29:        float x, y, w, h;
                     30: 
                     31:        x = imageRect.origin.x;
                     32:        y = imageRect.origin.y;
                     33:        w = imageRect.size.width;
                     34:        h = imageRect.size.height;
                     35: 
                     36:        if (x < 0.0) {
                     37:                w += x;
                     38:                x = 0.0;
                     39:        } else if (x + w > bounds.size.width)
                     40:                w = bounds.size.width - x;
                     41: 
                     42:        if (y < 0.0) {
                     43:                h += y;
                     44:                y = 0.0;
                     45:        } else if (y + h > bounds.size.height)
                     46:                h = bounds.size.height - y;
                     47: 
                     48:        // Avoid leaving border smudges
                     49:        x += 1; y += 1; w -= 2; h -= 2;
                     50: 
                     51:        if (w <= 0 || h <= 0)
                     52:        // Ugh, this won't do.
                     53:                return self;
                     54: 
                     55:        PSgsave();
                     56:        PStranslate(x, y);
                     57: 
                     58:        drawNeXTCube(w, h, gray);
                     59: 
                     60:        PSgrestore();
                     61: 
                     62:        return self;
                     63: }
                     64: 
                     65: - adjustSpeed: (float *) speedp
                     66:        minSpeed: (float) minSpeed
                     67:        maxSpeed: (float) maxSpeed
                     68:        pos: (float *) posp
                     69:        iMin: (float) iMin
                     70:        iMax: (float) iMax
                     71: {
                     72:        float speed = *speedp;
                     73:        float pnt = *posp;
                     74: 
                     75:        if (pnt < iMin)
                     76:        {
                     77:                if (speed < 0)
                     78:                        speed /= ACCELERATION;
                     79:                else
                     80:                        speed *= ACCELERATION;
                     81:        }
                     82:        else if (pnt > iMax)
                     83:        {
                     84:                if (speed > 0)
                     85:                {
                     86:                        speed /= ACCELERATION;
                     87:                }
                     88:                else
                     89:                        speed *= ACCELERATION;
                     90:        }
                     91:        else if (fabs(speed) < maxSpeed)
                     92:                speed *= ACCELERATION;
                     93:        else if (fabs(speed) > maxSpeed)
                     94:                speed /= ACCELERATION;
                     95: 
                     96:        // Change direction when we start going too slowly
                     97:        if (fabs(speed) < minSpeed)
                     98:                speed = -speed;
                     99: 
                    100:        pnt += speed;
                    101: 
                    102:        *posp = pnt;
                    103:        *speedp = speed;
                    104: 
                    105:        return self;
                    106: }
                    107: 
                    108: - oneStep
                    109: {
                    110:        static int xCount = 100, yCount = 150;
                    111:        
                    112:        NXPoint iMin, iMax;
                    113:        float x, y;
                    114: 
                    115:        iMin.x = 0.0;
                    116:        iMin.y = 0.0;
                    117:        iMax.x = maxCoord.x;
                    118:        iMax.y = maxCoord.y;
                    119: 
                    120:        x = imageRect.origin.x;
                    121:        y = imageRect.origin.y;
                    122: 
                    123:        PSsetgray(0);
                    124:        NXRectFill(&imageRect);
                    125: 
                    126:        [self adjustSpeed: &delta.x minSpeed: MINSPEED maxSpeed: targetSpeed.x
                    127:         pos: &x iMin: iMin.x iMax: iMax.x];
                    128:        [self adjustSpeed: &delta.y minSpeed: MINSPEED maxSpeed: targetSpeed.y
                    129:         pos: &y iMin: iMin.y iMax: iMax.y];
                    130: 
                    131:        imageRect.origin.x = x;
                    132:        imageRect.origin.y = y;
                    133: 
                    134:        [self drawCube: 1.0];
                    135: 
                    136:        // Every once in an odd moon, change the targetSpeeds
                    137:        if (--xCount < 0)
                    138:        {
                    139:                xCount = random() % 1000;
                    140:                targetSpeed.x = randBetween(3,12);
                    141:        }
                    142:        if (--yCount < 0)
                    143:        {
                    144:                yCount = random() % 1000;
                    145:                targetSpeed.y = randBetween(3,12);
                    146:        }
                    147: 
                    148:        return self;
                    149: }
                    150: 
                    151: - initFrame:(NXRect *)frameRect
                    152: {
                    153:        [super initFrame: frameRect];
                    154: 
                    155:        imageRect.size.width = imageRect.size.height = CUBESIZE;
                    156: 
                    157:        return self;
                    158: }
                    159: 
                    160: - setImageConstraints
                    161: {
                    162:        [super setImageConstraints];
                    163: 
                    164:        if (imageRect.origin.x > maxCoord.x ||
                    165:                imageRect.origin.y > maxCoord.y)
                    166:        {
                    167:                imageRect.origin.x = randBetween(0, maxCoord.x);
                    168:                imageRect.origin.y = randBetween(0, maxCoord.y);
                    169:        }
                    170: 
                    171:        delta.x = targetSpeed.x = 6;
                    172:        delta.y = targetSpeed.y = 5;
                    173: 
                    174:        return self;
                    175: }
                    176: 
                    177: - setImage: image
                    178: {
                    179:        return self;
                    180: }
                    181: 
                    182: @end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.