Skip to content

Commit

Permalink
reduce duplicated list of elements
Browse files Browse the repository at this point in the history
- made tag_tuple able to instantiate any template type that accepts the same template parameters as the tag_tuple
- suffix element_tags and tool_tags with `_t` to denote that they are types and not variables
  • Loading branch information
btzy committed May 26, 2018
1 parent dd1ab0a commit 6b7e642
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ Release/
*.sln
*.filters
*.user

# Save files for Circuit Playground
*.sav
9 changes: 6 additions & 3 deletions CircuitPlayground/gamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

class GameState {
private:

// the possible elements that a pixel can represent
// std::monostate is a 'default' state, which represents an empty pixel
using element_variant_t = std::variant<std::monostate, ConductiveWire, InsulatedWire, Signal, Source, PositiveRelay, NegativeRelay, AndGate, OrGate, NandGate, NorGate>;
// for consistency, the element order in this tag_tuple should be the same as the variant above
using element_tags = extensions::tag_tuple<std::monostate, ConductiveWire, InsulatedWire, Signal, Source, PositiveRelay, NegativeRelay, AndGate, OrGate, NandGate, NorGate>;
using element_tags_t = extensions::tag_tuple<std::monostate, ConductiveWire, InsulatedWire, Signal, Source, PositiveRelay, NegativeRelay, AndGate, OrGate, NandGate, NorGate>;

using element_variant_t = element_tags_t::instantiate<std::variant>;

extensions::heap_matrix<element_variant_t> dataMatrix;

friend class StateManager;
Expand Down
2 changes: 1 addition & 1 deletion CircuitPlayground/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MainWindow {
public:

// compile-time type tag which stores the list of available elements
using tool_tags = extensions::tag_tuple<Selector, Panner, Eraser, ConductiveWire, InsulatedWire, Signal, Source, PositiveRelay, NegativeRelay, AndGate, OrGate, NandGate, NorGate>;
using tool_tags_t = extensions::tag_tuple<Selector, Panner, Eraser, ConductiveWire, InsulatedWire, Signal, Source, PositiveRelay, NegativeRelay, AndGate, OrGate, NandGate, NorGate>;

// logical units
constexpr static int LOGICAL_TOOLBOX_WIDTH = 128;
Expand Down
4 changes: 2 additions & 2 deletions CircuitPlayground/playarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void PlayArea::processMouseMotionEvent(const SDL_MouseMotionEvent& event) {
int offsetY = physicalOffsetY - translationY;
offsetX = extensions::div_floor(offsetX, scale);
offsetY = extensions::div_floor(offsetY, scale);
MainWindow::tool_tags::get(mainWindow.selectedToolIndices[*drawingIndex], [this, offsetX, offsetY](const auto tool_tag) {
MainWindow::tool_tags_t::get(mainWindow.selectedToolIndices[*drawingIndex], [this, offsetX, offsetY](const auto tool_tag) {
using Tool = typename decltype(tool_tag)::type;
if constexpr (std::is_base_of_v<Pencil, Tool>) {
processDrawingTool<Tool>(offsetX, offsetY);
Expand Down Expand Up @@ -126,7 +126,7 @@ void PlayArea::processMouseButtonEvent(const SDL_MouseButtonEvent& event) {
offsetY = extensions::div_floor(offsetY, scale);

size_t inputHandleIndex = MainWindow::resolveInputHandleIndex(event);
MainWindow::tool_tags::get(mainWindow.selectedToolIndices[inputHandleIndex], [this, event, offsetX, offsetY, inputHandleIndex](const auto tool_tag) {
MainWindow::tool_tags_t::get(mainWindow.selectedToolIndices[inputHandleIndex], [this, event, offsetX, offsetY, inputHandleIndex](const auto tool_tag) {
// 'Tool' is the type of tool (e.g. Selector)
using Tool = typename decltype(tool_tag)::type;

Expand Down
4 changes: 2 additions & 2 deletions CircuitPlayground/statemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void StateManager::readSave() {
saveFile.read(reinterpret_cast<char*>(&element_index), sizeof element_index);

GameState::element_variant_t element;
GameState::element_tags::get(element_index, [&element](const auto element_tag) {
GameState::element_tags_t::get(element_index, [&element](const auto element_tag) {
using Element = typename decltype(element_tag)::type;
element = Element();
});
Expand All @@ -115,7 +115,7 @@ void StateManager::writeSave() {
for (int32_t y = 0; y != gameState.dataMatrix.height(); ++y) {
for (int32_t x = 0; x != gameState.dataMatrix.width(); ++x) {
GameState::element_variant_t element = gameState.dataMatrix[{x, y}];
GameState::element_tags::for_each([&element, &saveFile](const auto element_tag, const auto index_tag) {
GameState::element_tags_t::for_each([&element, &saveFile](const auto element_tag, const auto index_tag) {
size_t index = element.index();
if (index == decltype(index_tag)::value) {
saveFile.write(reinterpret_cast<char*>(&index), sizeof index);
Expand Down
12 changes: 9 additions & 3 deletions CircuitPlayground/tag_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ namespace extensions {


/**
* Invokes callback(tag<T>) for the T at the given index.
* If index is out of bounds, then callback will not be invoked.
*/
* Invokes callback(tag<T>) for the T at the given index.
* If index is out of bounds, then callback will not be invoked.
*/
template <typename Callback>
inline static void get(const size_t index, Callback&& callback) {
get_by_index<0>(index, std::forward<Callback>(callback));
}

/**
* Generic template instantiation helper
*/
template <template <typename...> typename TemplateType>
using instantiate = TemplateType<T...>;

};

}
8 changes: 4 additions & 4 deletions CircuitPlayground/toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ void Toolbox::render(SDL_Renderer* renderer) const {


// draw the buttons to the screen one-by-one
MainWindow::tool_tags::for_each([this, renderer, button_font](const auto tool_tag, const auto index_tag) {
MainWindow::tool_tags_t::for_each([this, renderer, button_font](const auto tool_tag, const auto index_tag) {
// 'Tool' is the type of tool (e.g. ConductiveWire)
using Tool = typename decltype(tool_tag)::type;
// 'index' is the index of this element inside the tool_tags
// 'index' is the index of this element inside the tool_tags_t
constexpr size_t index = decltype(index_tag)::value;

SDL_Color backgroundColorForText = MainWindow::backgroundColor;
Expand Down Expand Up @@ -116,7 +116,7 @@ void Toolbox::processMouseMotionEvent(const SDL_MouseMotionEvent& event) {

// element index
size_t index = static_cast<size_t>((offsetY - PADDING_VERTICAL) / (BUTTON_HEIGHT + BUTTON_SPACING));
if (index >= MainWindow::tool_tags::size) return;
if (index >= MainWindow::tool_tags_t::size) return;

// check if the mouse is on the button spacing instead of on the actual button
if ((offsetY - PADDING_VERTICAL) - static_cast<int32_t>(index) * (BUTTON_HEIGHT + BUTTON_SPACING) >= BUTTON_HEIGHT) return;
Expand All @@ -136,7 +136,7 @@ void Toolbox::processMouseButtonDownEvent(const SDL_MouseButtonEvent& event) {

// element index
size_t index = static_cast<size_t>((offsetY - PADDING_VERTICAL) / (BUTTON_HEIGHT + BUTTON_SPACING));
if (index >= MainWindow::tool_tags::size) return;
if (index >= MainWindow::tool_tags_t::size) return;

// check if the mouse is on the button spacing instead of on the actual button
if ((offsetY - PADDING_VERTICAL) - static_cast<int32_t>(index) * (BUTTON_HEIGHT + BUTTON_SPACING) >= BUTTON_HEIGHT) return;
Expand Down

0 comments on commit 6b7e642

Please sign in to comment.