summaryrefslogtreecommitdiff
path: root/commands.cpp
blob: 44b3586d631d1e0ab341e647158c6ac809906a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Sudoku: A plug-in for the Video Disk Recorder
 *
 * Copyright (C) 2008-2010, Thomas Günther <tom@toms-cafe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "commands.h"
#include "menu.h"
#include <vdr/i18n.h>
#include <vdr/config.h>
#include <vdr/osdbase.h>
#include <vdr/osd.h>
#include <assert.h>


namespace SudokuPlugin
{
  /** Command ids */
  enum
  {
    Cmd_LowerEdge = 100,
    CmdGenerate,
    CmdReset,
    CmdExit,
    CmdMark,
    CmdNextCell,
    CmdNextNumber,
    CmdLoad,
    CmdSave,
    CmdSetup,
    CmdUndo,
    CmdRedo,
    // Add new command ids only here, don't remove commands.
    Cmd_UpperEdge,
    Cmd_Count = Cmd_UpperEdge-Cmd_LowerEdge-1,
    // Default commands
    Cmd_KeyRedDefault = CmdMark,
    Cmd_KeyGreenDefault = CmdUndo,
    Cmd_KeyYellowDefault = CmdRedo
  };
} // namespace SudokuPlugin


using namespace SudokuPlugin;


//--- class SudokuPlugin::CommandList ------------------------------------------

/** List of commands - sorted for the commands menu */
const struct CommandList::List CommandList::list[] =
{
  { CmdGenerate,   trNOOP("Generate a new puzzle"), &Menu::generate    },
  { CmdLoad,       trNOOP("Load a puzzle"),         &Menu::load        },
  { CmdSave,       trNOOP("Save the puzzle"),       &Menu::save        },
  { CmdUndo,       trNOOP("Undo last action"),      &Menu::undo        },
  { CmdRedo,       trNOOP("Redo last action"),      &Menu::redo        },
  { CmdMark,       trNOOP("Mark/unmark"),           &Menu::toggle_mark },
  { CmdNextCell,   trNOOP("Next cell"),             &Menu::next_cell   },
  { CmdNextNumber, trNOOP("Next number"),           &Menu::next_number },
  { CmdReset,      trNOOP("Reset the puzzle"),      &Menu::reset       },
  { CmdSetup,      trNOOP("Open setup menu"),       &Menu::open_setup  },
  { CmdExit,       trNOOP("Exit"),                  &Menu::exit        }
};

/** Command id of a command */
int CommandList::id(int index)
{
  assert(index >= 0 && index < count());
  return list[index].id;
}

/** Translated text of a command */
const char* CommandList::text(int index)
{
  assert(index >= 0 && index < count());
  return tr(list[index].text);
}

/** Command type of a command */
CommandType CommandList::command(int index)
{
  assert(index >= 0 && index < count());
  return list[index].cmd;
}

/** Count of commands */
int CommandList::count()
{
  assert(Cmd_Count == sizeof(list)/sizeof(*list));
  return Cmd_Count;
}

/** List with translated texts of all commands */
const char* const* CommandList::texts()
{
  static const char* text_list[Cmd_Count];
  for (int idx = 0; idx < count(); ++idx)
    text_list[idx] = tr(list[idx].text);
  return text_list;
}

/** Search a command id. Return default index if not found. */
int CommandList::id_to_index(int id, int default_index)
{
  int idx = 0;
  for (; idx < count(); ++idx)
    if (list[idx].id == id)
      break;
  if (idx >= count())
    idx = default_index;
  assert(idx >= 0 && idx < count());
  return idx;
}

/** Default index for the red key */
int CommandList::key_red_default_index()
{
  return id_to_index(Cmd_KeyRedDefault);
}

/** Default index for the green key */
int CommandList::key_green_default_index()
{
  return id_to_index(Cmd_KeyGreenDefault);
}

/** Default index for the yellow key */
int CommandList::key_yellow_default_index()
{
  return id_to_index(Cmd_KeyYellowDefault);
}


//--- class SudokuPlugin::CommandMenu ------------------------------------------

/** Constructor */
CommandMenu::CommandMenu() :
  cOsdMenu(trVDR("Commands"))
{
  SetHasHotkeys();
  for (int idx = 0; idx < CommandList::count(); ++idx)
    Add(new Command(hk(CommandList::text(idx)), CommandList::command(idx)));
  command = NULL;
}

/** Get the selected menu command. */
CommandType CommandMenu::get_selected_command() const
{
  return command;
}

/** Process user events. */
eOSState CommandMenu::ProcessKey(eKeys key)
{
  eOSState state = cOsdMenu::ProcessKey(key);
  if (state == osUnknown)
  {
    state = osContinue;
    if (key == kOk)
    {
      Command* item = (Command*)Get(Current());
      if (item)
        command = item->get_command();
      state = osBack;
    }
  }
  return state;
}


//--- class SudokuPlugin::Command ----------------------------------------------

/** Constructor */
Command::Command(const char* text, CommandType command) :
  cOsdItem(text), command(command)
{
  assert(text != NULL);
  assert(command != NULL);
}

/** Get the menu command. */
CommandType Command::get_command() const
{
  return command;
}