dcb76bea975db262d7116c2caf5f6e942cdbada4
[sdk] / samples / games / cards / ruff / src / scores.ec
1 import "ruff.ec"
2
3 define GREEN   = Color { 0,128,0 };
4 define BLUE    = Color { 0,0,128 };
5 define PINK    = Color { 128,64,64 };
6 define VIOLET  = Color { 128,0,128 };
7 define ORANGE  = Color { 255,128,0 };
8 define PURPLE  = Color { 255,0,128 };
9 define CYAN    = Color { 0,255,255 };
10 define RED     = Color { 192,0,0 };
11
12 class Scores : Window
13 {
14    inactive = true;
15    isRemote = true;
16    background = white;
17    borderStyle = fixed, hasVertScroll = true, text = "The Scores", anchor = Anchor { horz = 0.3, vert = 0.1 }, clientSize = Size { 316,441 };
18    
19    bool OnPostCreate()
20    {
21       SetScrollLineStep(1, 20);
22       SetScrollArea(1, 102 * 20, true);
23       return true;
24    }
25
26    void OnResize(int w, int h)
27    {
28       vertScroll.pageStep = 400;
29    }
30
31    void ShowScore(Surface surface, int x, int y, int score, int r)
32    {
33       char temp[256];
34         if(score < 0)
35         {
36                 sprintf(temp,"(%d)",-score);
37          surface.SetForeground((r % 2) ? RED : ORANGE);
38         }
39         else
40         {
41                 sprintf(temp,"%d",score);
42          surface.SetForeground((r % 2) ? black : white);
43         }
44         surface.CenterTextf(x,y,temp);
45    }
46
47    void OnRedraw(Surface surface)
48    {
49         int r;
50         int scores[2]={0,0};
51       Ruff ruff = (Ruff)master;
52       
53         if(ruff.game.players)
54         {
55          int scoreScroll = vertScroll.thumbPosition / 20;
56
57          surface.SetBackground(BLUE);
58                 surface.Area(1,1,49,39);
59
60                 surface.SetBackground(BLUE);
61                 surface.Area(151,1,199,39);
62
63          // Name Boxes
64          surface.SetBackground(CYAN);
65                 surface.Area(51,1,149,39);
66                 surface.Area(201,1,299,39);
67
68          surface.CenterTextf(100,0,ruff.game.players[0].name);
69                 surface.CenterTextf(250,0,ruff.game.players[1].name);
70                 surface.CenterTextf(100,20,ruff.game.players[2].name);
71                 surface.CenterTextf(250,20,ruff.game.players[3].name);
72
73                 for(r=0; r<20; r++)
74                 {
75             Round * round = &ruff.game.rounds[r + scoreScroll];
76             int shuffle = (ruff.game.rounds[0].shuffle + r + scoreScroll) % 4;
77
78             // Line Background
79                         surface.SetBackground(PINK);
80                         surface.Area(1,41+r*20,24,59+r*20);
81                         surface.SetBackground(VIOLET);
82                         surface.Area(26,41+r*20,49,59+r*20);
83
84                         if((r+scoreScroll)%2)
85                                 surface.SetBackground(white);
86                         else
87                                 surface.SetBackground(GREEN);
88                         surface.Area(51,41+r*20,149,59+r*20);
89                         surface.Area(201,41+r*20,299,59+r*20);
90
91             surface.SetBackground(((r+scoreScroll)%2) ? PURPLE : ORANGE);
92                         surface.Area(151,41+r*20,174,59+r*20);
93                         surface.SetBackground(((r+scoreScroll)%2) ? ORANGE : PURPLE);
94                         surface.Area(176,41+r*20,199,59+r*20);
95
96             // Round Number
97             surface.SetForeground(white);
98                         surface.CenterTextf(12,40+r*20,"%d",(r+scoreScroll)+1);
99
100                         // Shuffle
101                         surface.CenterTextf(37,40+r*20,"%c",ruff.game.players[shuffle].name[0]);
102
103                         // Bet
104             if(r + scoreScroll < ruff.game.round || 
105                (r + scoreScroll == ruff.game.round && !ruff.game.betting && ruff.game.gameStarted))
106             {
107                int x = (round->bet.player%2) ? 188 : 162;
108                int y = 40+r*20;
109                            if(round->bet.howMuch == RUFF)
110                            {
111                                    surface.SetForeground(Color{ 0,0,192 });
112                   surface.CenterTextf(x, y, "R");
113                            }
114                            else
115                {
116                               surface.SetForeground(white);
117                               if(round->bet.howMuch==100)
118                      surface.CenterTextf(x, y, "C");
119                   else
120                      surface.CenterTextf(x, y, "%d",round->bet.howMuch);
121                }
122             }
123                 }
124
125          // Scores
126                 for(r=0; r<scoreScroll; r++)
127                 {
128             Round * round = &ruff.game.rounds[r];
129                         scores[0] += round->scores[0];
130                         scores[1] += round->scores[1];
131                 }
132                 for(r=scoreScroll; r<ruff.game.round+1; r++)
133                 {
134             Round * round = &ruff.game.rounds[r];
135                         scores[0] += round->scores[0];
136                         scores[1] += round->scores[1];
137
138             if(r < ruff.game.round || 
139                (r == ruff.game.round && !ruff.game.betting && ruff.game.gameStarted))
140             {
141                            // Winning box
142                if(r < ruff.game.round)
143                {
144                               surface.SetBackground(RED);
145                               if((scores[0] >= 500 || scores[1] >= 500) && scores[0] != scores[1])
146                   {
147                                       if(scores[0] > scores[1])
148                                               surface.Area(51,41+(r-scoreScroll)*20,149,59+(r-scoreScroll)*20);
149                                       else
150                                               surface.Area(201,41+(r-scoreScroll)*20,299,59+(r-scoreScroll)*20);
151                   }
152                }
153                                    
154                            // Current Score
155                ShowScore(surface,  75, 40 + (r-scoreScroll) * 20, round->scores[0], r);
156                ShowScore(surface, 225, 40 + (r-scoreScroll) * 20, round->scores[1], r);
157             }
158
159             if(r<ruff.game.round)
160             {
161                            // Total Score
162                ShowScore(surface, 125, 40 + (r-scoreScroll) * 20, scores[0], r);
163                ShowScore(surface, 275, 40 + (r-scoreScroll) * 20, scores[1], r);
164             }
165                 }
166         }
167    }
168
169    void OnVScroll(ScrollBarAction action, int position, Key key)
170    {
171       Update(null);
172    }
173 }