PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/MoreDucks/MoreDucks.d

http://github.com/wilkie/djehuty
D | 99 lines | 78 code | 20 blank | 1 comment | 6 complexity | c8cc7a788c8a8c357f2f97a661c95c0e MD5 | raw file
 1import djehuty;
 2
 3import gui.application;
 4import gui.window;
 5import gui.button;
 6import gui.widget;
 7
 8import io.console;
 9
10import graphics.graphics;
11import resource.image;
12
13class MyControl : Widget {
14	this() {
15		super(0,0,360,297);
16	}
17
18	override void onAdd() {
19	}
20
21	override void onDraw(ref Graphics g) {
22		if (images[curImage] is null) {
23			string path = "";
24			switch(curImage) {
25				case 0:
26					path = "baby_ducks.png";
27					break;
28				case 1:
29					path = "duckling.png";
30					break;
31				case 2:
32				default:
33					path = "ducks-cute.png";
34					break;
35			}
36			images[curImage] = new Image("examples/MoreDucks/" ~ path);
37		}
38		g.drawImage(this.left,this.top,images[curImage]);
39	}
40
41	void nextImage() {
42		if(curImage == images.length-1) {
43			curImage = 0;
44			return;
45		}
46
47		curImage++;
48	}
49
50private:
51	Image[3] images;
52	int curImage = 1;
53}
54
55class MyWindow : Window {
56	this() {
57		super("OMG DUCKS",WindowStyle.Fixed,Color.Gray,WindowPosition.Center,360,297);
58	}
59
60	override void onAdd() {
61		push(imageBox = new MyControl());
62		push(button = new Button(1,1,358,48,"MORE DUCKS!"));
63	}
64
65	override bool onSignal(Dispatcher d, uint signal) {
66		if(d is button) {
67			if(signal == Button.Signal.Selected) {
68				imageBox.nextImage();
69				redraw();
70				return true;
71			}
72		}
73
74		return false;
75	}
76
77
78private:
79	Button button;
80	MyControl imageBox;
81}
82
83class MyApp : GuiApplication {
84	// Start an application instance
85	static this() { new MyApp(); }
86
87	override void onApplicationStart() {
88		wnd = new MyWindow();
89		wnd.visible = true;
90
91		push(wnd);
92	}
93
94	override void onApplicationEnd() {
95	}
96
97private:
98	MyWindow wnd;
99}