44604e604b4dea04fa5a4523b049dc3975f0b229
[sdk] / samples / guiAndGfx / bitmapsAndKB / KBController / controller.ec
1 import "ecere"
2
3 BitmapResource knightGfx { ":knight.png" };
4 BitmapResource dragonGfx { ":dragon.png" };
5 BitmapResource mageGfx { ":mage.png" };
6 BitmapResource archerGfx { ":archer.png" };
7 BitmapResource castleGfx { ":castle.png" };
8
9 Array<BitmapResource> gfxResources { [ knightGfx, dragonGfx, mageGfx, archerGfx, castleGfx ] };
10
11 class WorldObject
12 {
13    bool selectable;
14    BitmapResource res;
15
16    void Render(Surface surface)
17    {
18       Bitmap bmp = res ? res.bitmap : null;
19       if(bmp)
20       {
21          surface.Blit(bmp, x, y, 0,0, bmp.width, bmp.height);
22          if(this == selected)
23          {
24             surface.foreground = lime;
25             surface.Rectangle(x-1, y-1, x + bmp.width, y + bmp.height);
26             surface.foreground = white;
27             surface.Rectangle(x-2, y-2, x + bmp.width+1, y + bmp.height+1);
28          }
29       }
30    }
31
32 public:
33    int x, y;
34 }
35
36 class Knight : WorldObject { res = knightGfx; selectable = true; }
37 class Dragon : WorldObject { res = dragonGfx; }
38 class Mage : WorldObject { res = mageGfx; selectable = true; }
39 class Archer : WorldObject { res = archerGfx; selectable = true; }
40 class Castle : WorldObject { res = castleGfx; }
41
42 Array<WorldObject> objects
43 { [
44    Castle { 180, 150 },
45    Mage { 50, 50 },
46    Archer { 150, 250 },
47    Knight { 380, 290 },
48    Knight { 120, 150 },
49    Dragon { 320, 50 }
50 ] };
51
52 WorldObject selected;
53
54 class WorldObjectController : WindowController<WorldObject>
55 {
56    bool OnKeyHit(WorldObjectController controller, Key key, unichar ch)
57    {
58       switch(key)
59       {
60          case tab:
61          {
62             Iterator<WorldObject> it { objects };
63             it.Find(selected);
64             do
65             {
66                if(!it.Next())
67                   it.Next();
68                selected = it.data;
69             } while(!selected.selectable);
70             controller.controlled = selected;
71             break;
72          }
73          case left: x -= 5; break;
74          case right: x += 5; break;
75          case up: y -= 5; break;
76          case down: y += 5; break;
77       }
78       controller.window.Update(null);
79       return true;
80    }
81 }
82
83 class MainWindow : Window
84 {
85    text = "Keyboard Input Using Controllers";
86    background = black;
87    borderStyle = sizable;
88    hasMaximize = true;
89    hasMinimize = true;
90    hasClose = true;
91    size = { 576, 432 };
92
93    controller = WorldObjectController { this };
94
95    bool OnCreate()
96    {
97       selected = objects[1];
98       ((WorldObjectController)controller).controlled = selected;
99       return true;
100    }
101
102    bool OnLoadGraphics()
103    {
104       for(r : gfxResources)
105          AddResource(r);
106       return true;
107    }
108
109    void OnUnloadGraphics()
110    {
111       for(r : gfxResources)
112          RemoveResource(r);
113    }
114
115    void OnDestroy()
116    {
117       controller = null;
118       objects.Free();
119    }
120
121    void OnRedraw(Surface surface)
122    {
123       for(o : objects)
124          o.Render(surface);
125    }
126 }
127
128 MainWindow mainWindow {};