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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include "GridManager.h"
#include <iostream>
#include <map>
#include "itextstream.h"
#include "debugging/debugging.h"
#include "imodule.h"
#include "icommandsystem.h"
#include "ipreferencesystem.h"
#include "string/string.h"
#include "registry/registry.h"
#include "i18n.h"
#include "GridItem.h"
#include <functional>
#include <fmt/format.h>
#include "module/StaticModule.h"
namespace ui
{
namespace
{
const char* const RKEY_DEFAULT_GRID_SIZE = "user/ui/grid/defaultGridPower";
const char* const RKEY_GRID_LOOK_MAJOR = "user/ui/grid/majorGridLook";
const char* const RKEY_GRID_LOOK_MINOR = "user/ui/grid/minorGridLook";
}
GridManager::GridManager() :
_activeGridSize(GRID_8)
{}
const std::string& GridManager::getName() const
{
static std::string _name(MODULE_GRID);
return _name;
}
const StringSet& GridManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_COMMANDSYSTEM);
_dependencies.insert(MODULE_PREFERENCESYSTEM);
}
return _dependencies;
}
void GridManager::initialiseModule(const IApplicationContext& ctx)
{
populateGridItems();
registerCommands();
constructPreferences();
// Load the default value from the registry
loadDefaultValue();
}
void GridManager::shutdownModule()
{
// Map the [GRID_0125...GRID_256] values (starting from -3) to [0..N]
int registryValue = static_cast<int>(_activeGridSize) - static_cast<int>(GRID_0125);
registry::setValue(RKEY_DEFAULT_GRID_SIZE, registryValue);
}
void GridManager::loadDefaultValue()
{
// Get the registry value
int registryValue = registry::getValue<int>(RKEY_DEFAULT_GRID_SIZE);
// Map the [0..N] values to [GRID_0125...GRID_256]
int mapped = registryValue + static_cast<int>(GRID_0125);
if (mapped >= GRID_0125 && mapped <= GRID_256)
{
_activeGridSize = static_cast<GridSize>(mapped);
}
else
{
_activeGridSize = GRID_8;
}
}
void GridManager::populateGridItems()
{
// Populate the GridItem map
for (int size = GRID_0125; size <= GRID_256; size++)
{
_gridItems.emplace_back(
grid::getStringForSize(static_cast<GridSize>(size)),
GridItem(static_cast<GridSize>(size), *this)
);
}
}
void GridManager::registerCommands()
{
GlobalCommandSystem().addCommand("SetGrid",
std::bind(&GridManager::setGridCmd, this, std::placeholders::_1),
{ cmd::ARGTYPE_STRING });
// Grid size shortcuts are defined in commandsystem.xml like "SetGrid4" => "SetGrid 16"
// Create shortcut statements that can accept accelerator bindings
GlobalCommandSystem().addCommand("GridDown", std::bind(&GridManager::gridDownCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("GridUp", std::bind(&GridManager::gridUpCmd, this, std::placeholders::_1));
}
ComboBoxValueList GridManager::getGridList()
{
ComboBoxValueList returnValue;
for (const NamedGridItem& i : _gridItems)
{
returnValue.push_back(i.first);
}
return returnValue;
}
void GridManager::constructPreferences()
{
IPreferencePage& page = GlobalPreferenceSystem().getPage(_("Settings/Grid"));
page.appendCombo(_("Default Grid Size"), RKEY_DEFAULT_GRID_SIZE, getGridList());
ComboBoxValueList looks;
looks.push_back(_("Lines"));
looks.push_back(_("Dotted Lines"));
looks.push_back(_("More Dotted Lines"));
looks.push_back(_("Crosses"));
looks.push_back(_("Dots"));
looks.push_back(_("Big Dots"));
looks.push_back(_("Squares"));
page.appendCombo(_("Major Grid Style"), RKEY_GRID_LOOK_MAJOR, looks);
page.appendCombo(_("Minor Grid Style"), RKEY_GRID_LOOK_MINOR, looks);
}
sigc::signal<void> GridManager::signal_gridChanged() const
{
return _sigGridChanged;
}
void GridManager::gridChangeNotify()
{
_sigGridChanged();
}
void GridManager::setGridCmd(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: SetGrid [";
for (const auto& item : _gridItems)
{
rError() << item.first << "|";
}
rError() << "]" << std::endl;
return;
}
// Look up the grid item by the argument string
auto gridStr = args[0].getString();
for (const auto& item : _gridItems)
{
if (item.first == gridStr)
{
setGridSize(item.second.getGridSize());
return;
}
}
rError() << "Unknown grid size: " << gridStr << std::endl;
}
void GridManager::gridDownCmd(const cmd::ArgumentList& args)
{
gridDown();
}
void GridManager::gridDown()
{
if (_activeGridSize > GRID_0125)
{
int _activeGridIndex = static_cast<int>(_activeGridSize);
_activeGridIndex--;
setGridSize(static_cast<GridSize>(_activeGridIndex));
}
}
void GridManager::gridUpCmd(const cmd::ArgumentList& args)
{
gridUp();
}
void GridManager::gridUp()
{
if (_activeGridSize < GRID_256)
{
int _activeGridIndex = static_cast<int>(_activeGridSize);
_activeGridIndex++;
setGridSize(static_cast<GridSize>(_activeGridIndex));
}
}
void GridManager::setGridSize(GridSize gridSize)
{
if (_activeGridSize != gridSize)
{
_activeGridSize = gridSize;
gridChangeNotify();
}
}
float GridManager::getGridSize(grid::Space space) const
{
return pow(2.0f, static_cast<float>(getGridPower(space)));
}
int GridManager::getGridPower(grid::Space space) const
{
int power = static_cast<int>(_activeGridSize);
// Texture space is using smaller grid sizes, since it doesn't make much
// sense to have grid spacing greater than 1.0
if (space == grid::Space::Texture)
{
power -= 7;
if (power < -10) power = -10;
if (power > 0) power = 0;
}
return power;
}
int GridManager::getGridBase(grid::Space space) const
{
return 2;
}
GridLook GridManager::getLookFromNumber(int i)
{
if (i >= GRIDLOOK_LINES && i <= GRIDLOOK_SQUARES)
{
return static_cast<GridLook>(i);
}
return GRIDLOOK_LINES;
}
GridLook GridManager::getMajorLook() const
{
return getLookFromNumber(registry::getValue<int>(RKEY_GRID_LOOK_MAJOR));
}
GridLook GridManager::getMinorLook() const
{
return getLookFromNumber(registry::getValue<int>(RKEY_GRID_LOOK_MINOR));
}
module::StaticModuleRegistration<GridManager> staticGridManagerModule;
}
|