OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / test / src_dir / vala1.vala
1 using GLib;
2
3
4
5 // Class which makes the event
6 public class Game : Object {
7     // Note the use of the signal keyword
8     public signal void score_change (int newScore, ref bool cancel);
9
10     int _score;
11
12         // Score Property
13     public int score {
14         get {
15             return _score;
16             }
17         set {
18             if (_score != value) {
19                 bool cancel = false;
20                 score_change (value, ref cancel);
21                 if (! cancel)
22                     _score = value;
23             }
24         }
25     }
26 }
27
28 // Class which handles the event
29 public class Referee : Object
30 {
31     public Game game { get; construct; }
32
33     public Referee (construct Game game) {
34     }
35
36     construct {
37         // Monitor when a score changes in the game
38         game.score_change += game_score_change;
39     }
40
41     // Notice how this method signature matches the score_change signal's signature
42     private void game_score_change (Game game, int new_score, ref bool cancel) {
43         if (new_score < 100)
44             stdout.printf ("Good Score\n");
45         else {
46             cancel = true;
47             stdout.printf ("No Score can be that high!\n");
48         }
49     }
50 }
51
52 // Class to test it all
53 public class GameTest : Object
54 {
55     public static void main () {
56         var game = new Game ();
57         var referee = new Referee (game);
58         game.score = 70;
59         game.score = 110;
60     }
61 }