~gmult-team/gmult/stable

« back to all changes in this revision

Viewing changes to gmult/GtkMult.vala

  • Committer: Michael Terry
  • Date: 2008-09-05 15:33:43 UTC
  • Revision ID: michael.terry@ubuntu.com-20080905153343-bdhn241sqkmdaa3j
add hint menu option

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    score = new Gtk.Label ("");
37
37
    canvas = null;
38
38
    status_timeout_id = 0;
39
 
    solve_action = null;
 
39
    solve_action = null;
 
40
    hint_action = null;
40
41
    
41
42
    // Convert pixel space to desired point size
42
43
    Value dpiValue = Value (typeof (int));
100
101
  }
101
102
  
102
103
  Gtk.Action solve_action;
103
 
  Gtk.Widget setup_menu ()
 
104
  Gtk.Action hint_action;
 
105
  private Gtk.Widget setup_menu ()
104
106
  {
105
107
    var action_group = new Gtk.ActionGroup ("gmult-actions");
106
108
    
111
113
    action.activate += on_menu_game_new;
112
114
    action_group.add_action_with_accel (action, "<control>N");
113
115
    
 
116
    action = new Gtk.Action ("HintAction", _("_Hint"), null, null);
 
117
    action.activate += on_menu_game_hint;
 
118
    action_group.add_action_with_accel (action, "<control>H");
 
119
    this.hint_action = action;
 
120
    
114
121
    action = new Gtk.Action ("SolveAction", _("_Solve"), null, null);
115
122
    action.activate += on_menu_game_solve;
116
123
    action_group.add_action (action);
127
134
    action.activate += on_menu_help_about;
128
135
    action_group.add_action (action);
129
136
    
130
 
    var ui = "\
131
 
<ui>\
132
 
  <menubar>\
133
 
    <menu name=\"GameMenu\" action=\"GameMenuAction\">\
134
 
      <menuitem name=\"New\" action=\"NewAction\" />\
135
 
      <separator />\
136
 
      <menuitem name=\"Solve\" action=\"SolveAction\" />\
137
 
      <separator />\
138
 
      <menuitem name=\"Close\" action=\"CloseAction\" />\
139
 
    </menu>\
140
 
    <menu name=\"HelpMenu\" action=\"HelpMenuAction\">\
141
 
      <menuitem name=\"About\" action=\"AboutAction\"/>\
142
 
    </menu>\
143
 
  </menubar>\
144
 
</ui>";
 
137
    var ui = """
 
138
<ui>
 
139
  <menubar>
 
140
    <menu name="GameMenu" action="GameMenuAction">
 
141
      <menuitem name="New" action="NewAction" />
 
142
      <separator />
 
143
      <menuitem name="Hint" action="HintAction" />
 
144
      <menuitem name="Solve" action="SolveAction" />
 
145
      <separator />
 
146
      <menuitem name="Close" action="CloseAction" />
 
147
    </menu>
 
148
    <menu name="HelpMenu" action="HelpMenuAction">
 
149
      <menuitem name="About" action="AboutAction"/>
 
150
    </menu>
 
151
  </menubar>
 
152
</ui>""";
145
153
 
146
154
    var manager = new Gtk.UIManager ();
147
155
    try {
156
164
    menu_item.select += on_menu_select_new;
157
165
    menu_item.deselect += on_menu_deselect;
158
166
    
 
167
    menu_item = (Gtk.Item)manager.get_widget ("/ui/menubar/GameMenu/Hint");
 
168
    menu_item.select += on_menu_select_hint;
 
169
    menu_item.deselect += on_menu_deselect;
 
170
    
159
171
    menu_item = (Gtk.Item)manager.get_widget ("/ui/menubar/GameMenu/Solve");
160
172
    menu_item.select += on_menu_select_solve;
161
173
    menu_item.deselect += on_menu_deselect;
238
250
      switch (response)
239
251
      {
240
252
      case MultPuzzleGuessStatus.WRONG:
241
 
        // Translators: %1$s is the letter, %2$s is the digit
 
253
        // Translators: First argument is letter, second is digit
242
254
        message_pattern = _("Incorrect — %1$s is not %2$s");
243
255
        break;
244
256
      case MultPuzzleGuessStatus.CORRECT:
245
 
        // Translators: %1$s is the letter, %2$s is the digit
 
257
        // Translators: First argument is letter, second is digit
246
258
        message_pattern = _("Correct — %1$s is %2$s");
247
259
        break;
248
260
      default:
264
276
  private void on_puzzle_change (MultPuzzle p)
265
277
  {
266
278
    solve_action.set_sensitive (!p.is_done);
 
279
    hint_action.set_sensitive (!p.is_done);
267
280
  }
268
281
 
269
282
  private bool pop_guess_feedback ()
338
351
     status.push (context_id, _("Solve this game"));
339
352
  }
340
353
  
 
354
  private void on_menu_game_hint()
 
355
  {
 
356
    cheated = true;
 
357
    
 
358
    // Pick a random unknown digit.  Keep trying until we actually solve one
 
359
    // (because the digit may not have been in the puzzle -- we want to be
 
360
    // more useful than that.
 
361
    var character = MultPuzzleChar.INVALID;
 
362
    var digit = 0;
 
363
    var needed = this.puzzle.get_needed_digits();
 
364
    var num_needed = 0;
 
365
    foreach (bool n in needed)
 
366
      num_needed += n ? 1 : 0;
 
367
    if (num_needed == 0) // shouldn't happen
 
368
      return;
 
369
    
 
370
    var choice = Random.int_range(0, num_needed);
 
371
    for (int i = 0; i < 10; ++i) {
 
372
      if (needed[i] && choice == 0) {
 
373
        character = this.puzzle.solve_digit(i);
 
374
        digit = i;
 
375
        break;
 
376
      }
 
377
      else if (needed[i])
 
378
        --choice;
 
379
    }
 
380
    
 
381
    var context_id = status.get_context_id ("guess-feedback");
 
382
    
 
383
    // Clear any previous message
 
384
    status.pop (context_id);
 
385
    if (status_timeout_id != 0)
 
386
      Source.remove (status_timeout_id);
 
387
    status_timeout_id = 0;
 
388
    
 
389
    // Translators: First argument is letter, second is digit
 
390
    var message_pattern = _("%1$s is %2$s");
 
391
    var letter_str = _("%c".printf ((char)character));
 
392
    var digit_str = _("%i".printf (digit));
 
393
    status.push (context_id, message_pattern.printf (letter_str, digit_str));
 
394
    status_timeout_id = Timeout.add_seconds (5, pop_guess_feedback);
 
395
    
 
396
    update_score ();
 
397
  }
 
398
  
 
399
  private void on_menu_select_hint ()
 
400
  {
 
401
    var context_id = status.get_context_id ("menu-hover");
 
402
     status.push (context_id, _("Reveal a digit at random"));
 
403
  }
 
404
  
341
405
  private void handle_about_uri (Gtk.AboutDialog about, string link)
342
406
  {
343
407
    AppLaunchContext context = new AppLaunchContext ();