/examples/MoreDucks/MoreDucks.d

http://github.com/wilkie/djehuty · D · 99 lines · 78 code · 20 blank · 1 comment · 6 complexity · c8cc7a788c8a8c357f2f97a661c95c0e MD5 · raw file

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