jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/
|
| 2 | /* Copyright (c) FIRST 2011. All Rights Reserved. */
|
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */
|
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
| 5 | /*----------------------------------------------------------------------------*/
|
| 6 |
|
| 7 | #include "SmartDashboard/SendableChooser.h"
|
| 8 | #include "networktables2/type/StringArray.h"
|
| 9 |
|
| 10 | #include <stdio.h>
|
| 11 |
|
| 12 | static const char *kDefault = "default";
|
| 13 | static const char *kOptions = "options";
|
| 14 | static const char *kSelected = "selected";
|
| 15 |
|
| 16 | SendableChooser::SendableChooser()
|
| 17 | {
|
| 18 | m_defaultChoice = "";
|
| 19 | }
|
| 20 |
|
| 21 | /**
|
| 22 | * Adds the given object to the list of options. On the {@link SmartDashboard} on the desktop,
|
| 23 | * the object will appear as the given name.
|
| 24 | * @param name the name of the option
|
| 25 | * @param object the option
|
| 26 | */
|
| 27 | void SendableChooser::AddObject(const char *name, void *object)
|
| 28 | {
|
| 29 | m_choices[name] = object;
|
| 30 | }
|
| 31 |
|
| 32 | /**
|
| 33 | * Add the given object to the list of options and marks it as the default.
|
| 34 | * Functionally, this is very close to {@link SendableChooser#AddObject(const char *name, void *object) AddObject(...)}
|
| 35 | * except that it will use this as the default option if none other is explicitly selected.
|
| 36 | * @param name the name of the option
|
| 37 | * @param object the option
|
| 38 | */
|
| 39 | void SendableChooser::AddDefault(const char *name, void *object)
|
| 40 | {
|
| 41 | m_defaultChoice = name;
|
| 42 | AddObject(name, object);
|
| 43 | }
|
| 44 |
|
| 45 | /**
|
| 46 | * Returns the selected option. If there is none selected, it will return the default. If there is none selected
|
| 47 | * and no default, then it will return {@code NULL}.
|
| 48 | * @return the option selected
|
| 49 | */
|
| 50 | void *SendableChooser::GetSelected()
|
| 51 | {
|
| 52 | std::string selected = m_table->GetString(kSelected, m_defaultChoice);
|
| 53 | if (selected == "")
|
| 54 | return NULL;
|
| 55 | else
|
| 56 | return m_choices[selected];
|
| 57 | }
|
| 58 |
|
| 59 | void SendableChooser::InitTable(ITable* subtable) {
|
| 60 | StringArray keys;
|
| 61 | m_table = subtable;
|
| 62 | if (m_table != NULL) {
|
| 63 | std::map<std::string, void *>::iterator iter;
|
| 64 | for (iter = m_choices.begin(); iter != m_choices.end(); iter++) {
|
| 65 | keys.add(iter->first);
|
| 66 | }
|
| 67 | m_table->PutValue(kOptions, keys);
|
| 68 | m_table->PutString(kDefault, m_defaultChoice);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | ITable* SendableChooser::GetTable() {
|
| 73 | return m_table;
|
| 74 | }
|
| 75 |
|
| 76 | std::string SendableChooser::GetSmartDashboardType() {
|
| 77 | return "String Chooser";
|
| 78 | }
|