extras; samples: Fixed warnings
[sdk] / samples / games / cards / ruff / src / trump.ec
1 import "ruff.ec"
2
3 class Trump : Kind
4 {
5    const char * OnGetString(char * string, void * fieldData, bool * needClass)
6    {
7       switch(this)
8       {
9          case clubs:    strcpy(string, "clubs"); break;
10          case diamonds: strcpy(string, "diamonds"); break;
11          case hearts:   strcpy(string, "hearts"); break;
12          case spades:   strcpy(string, "spades"); break;
13          default:       strcpy(string, "None");
14       }
15       return string;
16    }
17
18    void OnDisplay(Surface surface, int x, int y, int width, TrumpDialog trumpDialog, Alignment alignment, DataDisplayFlags flags)
19    {
20       Bitmap icon = (this != none) ? trumpDialog.icons[this].bitmap : null;
21       char name[10];
22       int w, h;
23       int xStart = icon ? (icon.width + 8) : ((trumpDialog.icons[0].bitmap ? trumpDialog.icons[0].bitmap.width : 0) + 8);
24       int len;
25
26       OnGetString(name, trumpDialog, null);
27       len = strlen(name);
28
29       surface.TextExtent(name, len, &w, &h);
30       surface.WriteText(xStart, y, name, len);
31
32       // Draw the current row stipple
33       if(flags.selected)
34       {
35          surface.Area(xStart - 3, y, xStart - 3, y + h - 1);
36          surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
37       }
38       if(flags.current && flags.active)
39       {
40          surface.LineStipple(0x5555);
41          if(flags.selected)
42             surface.SetForeground(Color { 255, 255, 128 });
43          else
44             surface.SetForeground(black);
45          surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
46          surface.LineStipple(0);
47       }
48
49       if(icon)
50       {
51          surface.SetForeground(white);
52          surface.Blit(icon, x,y,0,0, icon.width, icon.height);
53       }
54    }
55 }
56
57 class TrumpDialog : Window
58 {
59    background = activeBorder;
60    borderStyle = fixed;
61    hasClose = true;
62    tabCycle = true;
63    size = Size { 200, 94 };
64    anchor = Anchor { vert = -28 };
65
66    BitmapResource icons[Kind];
67
68    DataField field { "Trump", userData = this };
69    Button ok
70    {
71       this, text = "OK", isDefault = true, anchor = Anchor { top = 40 }, size = Size { 80 }, hotKey = altO;
72
73       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
74       {
75          Kind trump = (Kind)trumpDrop.GetTag(null);
76          //Kind trump = trumpDrop.GetData(null);
77          Destroy(trump);
78          return true;
79       }
80    };
81    DropBox trumpDrop
82    {
83       this, noHighlight = true, size = Size { 140 }, anchor = Anchor { top = 10 }
84    };
85
86    TrumpDialog()
87    {
88       DataRow row;
89
90       trumpDrop.AddField(field);
91       row = trumpDrop.AddRow(); row.tag = Kind::none; row.SetData(null, none);
92       row = trumpDrop.AddRow(); row.tag = Kind::clubs; row.SetData(null, clubs);
93       row = trumpDrop.AddRow(); row.tag = Kind::diamonds; row.SetData(null, diamonds);
94       row = trumpDrop.AddRow(); row.tag = Kind::hearts; row.SetData(null, hearts);
95       row = trumpDrop.AddRow(); row.tag = Kind::spades; row.SetData(null, spades);
96
97       AddResource(icons[clubs] = BitmapResource { ":clubs.png" });
98       AddResource(icons[diamonds] = BitmapResource { ":diamonds.png" });
99       AddResource(icons[hearts] = BitmapResource { ":hearts.png" });
100       AddResource(icons[spades] = BitmapResource { ":spades.png" });
101    }
102
103    bool OnPostCreate()
104    {
105       RuffGame * game = &((Ruff)master).game;
106
107       trumpDrop.Activate();
108       AI_ComputeBet(&game->players[game->turn]);
109
110       trumpDrop.currentRow = trumpDrop.FindRow(game->players[game->turn].trump);
111       return true;
112    }
113 }