Skip to content

Commit

Permalink
More Factory Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
davemoore22 committed Sep 13, 2024
1 parent cfa6ba8 commit 596aa71
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 123 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ EQUALS = =
CMAKE_SOURCE_DIR = /home/dave/sorcery

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/dave/sorcery
CMAKE_BINARY_DIR = /home/dave/Development/wizardry/sorcery-sfml

#=============================================================================
# Targets provided globally by CMake.
Expand All @@ -78,7 +78,7 @@ package/fast: package
# Special rule for the target package_source
package_source:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Run CPack packaging tool for source..."
/usr/bin/cpack --config ./CPackSourceConfig.cmake /home/dave/sorcery/CPackSourceConfig.cmake
/usr/bin/cpack --config ./CPackSourceConfig.cmake /home/dave/Development/wizardry/sorcery-sfml/CPackSourceConfig.cmake
.PHONY : package_source

# Special rule for the target package_source
Expand Down Expand Up @@ -107,9 +107,9 @@ rebuild_cache/fast: rebuild_cache

# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/dave/sorcery/CMakeFiles /home/dave/sorcery//CMakeFiles/progress.marks
$(CMAKE_COMMAND) -E cmake_progress_start /home/dave/Development/wizardry/sorcery-sfml/CMakeFiles /home/dave/Development/wizardry/sorcery-sfml//CMakeFiles/progress.marks
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/dave/sorcery/CMakeFiles 0
$(CMAKE_COMMAND) -E cmake_progress_start /home/dave/Development/wizardry/sorcery-sfml/CMakeFiles 0
.PHONY : all

# The main clean target
Expand Down
6 changes: 5 additions & 1 deletion inc/resources/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Sorcery {
// Forward Declarations
class Dialog;
class Display;
class Frame;
class Game;
class Graphics;
class Menu;
Expand All @@ -49,7 +50,10 @@ class Factory {
// Public Methods
auto make_dialog(const std::string &component, const WindowDialogType type = WindowDialogType::OK,
const unsigned int duration = 0) -> std::unique_ptr<Dialog>;
auto make_menu(const std::string &component, const MenuType type) -> std::unique_ptr<Menu>;
auto make_menu(const std::string &component, const MenuType type, std::optional<MenuMode> mode = std::nullopt,
std::optional<unsigned int> data = 0, const bool reload = false) -> std::unique_ptr<Menu>;
auto make_frame(const std::string &component) -> std::unique_ptr<Frame>;
auto make_menu_frame(const std::string &component) -> std::unique_ptr<Frame>;

private:

Expand Down
65 changes: 11 additions & 54 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,60 +180,21 @@ auto Sorcery::Engine::_initalise_components() -> void {
// Get the Window and Graphics to Display
_window = _display->window->get_window();

// Setup Custom Components
// Setup Menus
_camp_menu = _factory->make_menu("engine_base_ui:camp_menu", MenuType::CAMP);
_search_menu = _factory->make_menu("engine_base_ui:search_menu", MenuType::SEARCH);
_get_menu = _factory->make_menu("engine_base_ui:get_menu", MenuType::CHARACTERS_HERE);
_action_menu = _factory->make_menu("engine_base_ui:action_menu", MenuType::ACTION);
_elevator_a_d_menu = _factory->make_menu("engine_base_ui:elevator_a_d_menu", MenuType::ELEVATOR_A_D);

const Component menu_fc{(*_display->layout)["engine_base_ui:camp_menu_frame"]};
_camp_menu_frame = std::make_unique<Frame>(
_display->ui_texture, menu_fc.w, menu_fc.h, menu_fc.colour, menu_fc.background, menu_fc.alpha);
_camp_menu_frame->setPosition(_display->window->get_x(_camp_menu_frame->sprite, menu_fc.x),
_display->window->get_y(_camp_menu_frame->sprite, menu_fc.y));

const Component search_fc{(*_display->layout)["engine_base_ui:search_menu_frame"]};
_search_menu_frame = std::make_unique<Frame>(
_display->ui_texture, search_fc.w, search_fc.h, search_fc.colour, search_fc.background, search_fc.alpha);
_search_menu_frame->setPosition(_display->window->get_x(_search_menu_frame->sprite, search_fc.x),
_display->window->get_y(_search_menu_frame->sprite, search_fc.y));

const Component get_fc{(*_display->layout)["engine_base_ui:get_menu_frame"]};
_get_menu_frame = std::make_unique<Frame>(
_display->ui_texture, get_fc.w, get_fc.h, get_fc.colour, get_fc.background, get_fc.alpha);
_get_menu_frame->setPosition(_display->window->get_x(_get_menu_frame->sprite, get_fc.x),
_display->window->get_y(_get_menu_frame->sprite, get_fc.y));

const Component action_fc{(*_display->layout)["engine_base_ui:action_menu_frame"]};
_action_menu_frame = std::make_unique<Frame>(
_display->ui_texture, action_fc.w, action_fc.h, action_fc.colour, action_fc.background, action_fc.alpha);
_action_menu_frame->setPosition(_display->window->get_x(_action_menu_frame->sprite, action_fc.x),
_display->window->get_y(_action_menu_frame->sprite, action_fc.y));

const Component elevator_a_d_fc{(*_display->layout)["engine_base_ui:elevator_a_d_menu_frame"]};
_elevator_a_d_menu_frame = std::make_unique<Frame>(_display->ui_texture, elevator_a_d_fc.w, elevator_a_d_fc.h,
elevator_a_d_fc.colour, elevator_a_d_fc.background, elevator_a_d_fc.alpha);
_elevator_a_d_menu_frame->setPosition(_display->window->get_x(_elevator_a_d_menu_frame->sprite, elevator_a_d_fc.x),
_display->window->get_y(_elevator_a_d_menu_frame->sprite, elevator_a_d_fc.y));

const Component elevator_a_f_fc{(*_display->layout)["engine_base_ui:elevator_a_f_menu_frame"]};
_elevator_a_f_menu_frame = std::make_unique<Frame>(_display->ui_texture, elevator_a_f_fc.w, elevator_a_f_fc.h,
elevator_a_f_fc.colour, elevator_a_f_fc.background, elevator_a_f_fc.alpha);
_elevator_a_f_menu_frame->setPosition(_display->window->get_x(_elevator_a_f_menu_frame->sprite, elevator_a_f_fc.x),
_display->window->get_y(_elevator_a_f_menu_frame->sprite, elevator_a_f_fc.y));

const Component vfb_fc{(*_display->layout)["engine_base_ui:view_frame_small"]};
_view_frame_small = std::make_unique<Frame>(
_display->ui_texture, vfb_fc.w, vfb_fc.h, vfb_fc.colour, vfb_fc.background, vfb_fc.alpha);
_view_frame_small->setPosition(_display->window->get_x(_view_frame_small->sprite, vfb_fc.x),
_display->window->get_y(_view_frame_small->sprite, vfb_fc.y));

const Component vfs_fc{(*_display->layout)["engine_base_ui:view_frame_big"]};
_view_frame_big = std::make_unique<Frame>(
_display->ui_texture, vfs_fc.w, vfs_fc.h, vfs_fc.colour, vfs_fc.background, vfs_fc.alpha);
_view_frame_big->setPosition(_display->window->get_x(_view_frame_big->sprite, vfs_fc.x),
_display->window->get_y(_view_frame_big->sprite, vfs_fc.y));
_elevator_a_f_menu = _factory->make_menu("engine_base_ui:elevator_a_f_menu", MenuType::ELEVATOR_A_F);
_camp_menu_frame = _factory->make_menu_frame("engine_base_ui:camp_menu_frame");
_search_menu_frame = _factory->make_menu_frame("engine_base_ui:search_menu_frame");
_get_menu_frame = _factory->make_menu_frame("engine_base_ui:get_menu_frame");
_action_menu_frame = _factory->make_menu_frame("engine_base_ui:action_menu_frame");
_elevator_a_d_menu_frame = _factory->make_menu_frame("engine_base_ui:elevator_a_d_menu_frame");
_elevator_a_f_menu_frame = _factory->make_menu_frame("engine_base_ui:elevator_a_f_menu_frame");
_view_frame_small = _factory->make_frame("engine_base_ui:view_frame_small");
_view_frame_big = _factory->make_frame("engine_base_ui:view_frame_big");

// Dialogs
_ouch = _factory->make_dialog("engine_base_ui:ouch", WindowDialogType::TIMED, DELAY_OUCH);
Expand Down Expand Up @@ -327,11 +288,7 @@ auto Sorcery::Engine::_place_components() -> void {
const Component map_c{(*_display->layout)["engine_base_ui:map"]};
_map->setPosition(map_c.pos());

const Component cc_fc{(*_display->layout)["engine_base_ui:character_frame"]};
_cur_char_frame =
std::make_unique<Frame>(_display->ui_texture, cc_fc.w, cc_fc.h, cc_fc.colour, cc_fc.background, cc_fc.alpha);
_cur_char_frame->setPosition(_display->window->get_x(_cur_char_frame->sprite, cc_fc.x),
_display->window->get_y(_cur_char_frame->sprite, cc_fc.y));
_cur_char_frame = _factory->make_frame("engine_base_ui:character_frame");
}

auto Sorcery::Engine::_refresh() const -> void {
Expand Down
71 changes: 16 additions & 55 deletions src/engine/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ auto Sorcery::Inspect::start(std::optional<unsigned int> character_id) -> std::o
if (!_restricted) {

_in_character = false;
const auto menu_type{_mode == MenuMode::TAVERN ? MenuType::CHARACTER_ROSTER : MenuType::PARTY_CHARACTERS};
_menu.reset();
auto menu_type{_mode == MenuMode::TAVERN ? MenuType::CHARACTER_ROSTER : MenuType::PARTY_CHARACTERS};
_menu = std::make_unique<Menu>(_system, _display, _graphics, _game, menu_type, _mode);
_menu->reload();
_menu->generate((*_display->layout)[_screen_key + ":menu"]);
_menu->setPosition(_display->get_centre_x(_menu->get_width()), (*_display->layout)[_screen_key + ":menu"].y);
_menu = _factory->make_menu(_screen_key + ":menu", menu_type, _mode, std::nullopt, true);

_cur_char_id = -1;

Expand Down Expand Up @@ -157,23 +154,9 @@ auto Sorcery::Inspect::start(std::optional<unsigned int> character_id) -> std::o
_bg.setPosition(_display->window->get_x(_bg, bg_c.x), _display->window->get_y(_bg, bg_c.y));
}

const Component menu_fc{(*_display->layout)[_screen_key + ":menu_frame"]};
_menu_frame = std::make_unique<Frame>(
_display->ui_texture, menu_fc.w, menu_fc.h, menu_fc.colour, menu_fc.background, menu_fc.alpha);
_menu_frame->setPosition(_display->window->get_x(_menu_frame->sprite, menu_fc.x),
_display->window->get_y(_menu_frame->sprite, menu_fc.y));

const Component cc_fc{(*_display->layout)[_screen_key + ":character_frame"]};
_cur_char_frame = std::make_unique<Frame>(
_display->ui_texture, cc_fc.w, cc_fc.h, cc_fc.colour, cc_fc.background, cc_fc.alpha);
_cur_char_frame->setPosition(_display->window->get_x(_cur_char_frame->sprite, cc_fc.x),
_display->window->get_y(_cur_char_frame->sprite, cc_fc.y));

const Component p_fc{(*_display->layout)["roster:preview_frame"]};
_preview_frame =
std::make_unique<Frame>(_display->ui_texture, p_fc.w, p_fc.h, p_fc.colour, p_fc.background, p_fc.alpha);
_preview_frame->setPosition(_display->window->get_x(_preview_frame->sprite, p_fc.x),
_display->window->get_y(_preview_frame->sprite, p_fc.y));
_menu_frame = _factory->make_menu_frame(_screen_key + ":menu_frame");
_cur_char_frame = _factory->make_frame(_screen_key + ":character_frame");
_preview_frame = _factory->make_frame("roster:preview_frame");

// Clear the window
_window->clear();
Expand Down Expand Up @@ -284,44 +267,22 @@ auto Sorcery::Inspect::_handle_in_character(unsigned int character_id) -> std::o
_in_trade = false;

_item_action_menu.reset();
_item_action_menu =
std::make_unique<Menu>(_system, _display, _graphics, _game, MenuType::ITEM_ACTION, MenuMode::ACTION);
_item_action_menu->generate((*_display->layout)["character_summary:item_action_menu"]);
_item_action_menu->setPosition(_display->get_centre_x(_item_action_menu->get_width()),
(*_display->layout)["character_summary:item_action_menu"].y);

const Component item_action_menu_fc{(*_display->layout)["character_summary:item_action_menu_frame"]};
_item_action_menu_frame.reset();
_item_action_menu_frame = std::make_unique<Frame>(_display->ui_texture, item_action_menu_fc.w,
item_action_menu_fc.h, item_action_menu_fc.colour, item_action_menu_fc.background, item_action_menu_fc.alpha);
_item_action_menu_frame->setPosition(
_display->window->get_x(_item_action_menu_frame->sprite, item_action_menu_fc.x),
_display->window->get_y(_item_action_menu_frame->sprite, item_action_menu_fc.y));

const Component item_display_fc{(*_display->layout)["inspect:item_display_frame"]};
_item_display.reset();
_item_display_frame.reset();
_item_display_frame = std::make_unique<Frame>(_display->ui_texture, item_display_fc.w, item_display_fc.h,
item_display_fc.colour, item_display_fc.background, item_display_fc.alpha);
_item_display_frame->setPosition(_display->window->get_x(_item_display_frame->sprite, item_display_fc.x),
_display->window->get_y(_item_display_frame->sprite, item_display_fc.y));
_item_trade_menu.reset();
_item_trade_frame.reset();

_item_display.reset();
_item_display = std::make_unique<ItemDisplay>(_system, _display, _graphics, _game);
_item_display->setPosition((*_display->layout)["inspect:item_display"].pos());
_item_action_menu =
_factory->make_menu("character_summary:item_action_menu", MenuType::ITEM_ACTION, MenuMode::ACTION);
_item_trade_menu =
_factory->make_menu("inspect:item_trade_menu", MenuType::CHARACTER_TRADE, MenuMode::NO_MODE, character_id);

const Component it_fc{(*_display->layout)["inspect:item_trade_frame"]};
_item_trade_frame.reset();
_item_trade_frame =
std::make_unique<Frame>(_display->ui_texture, it_fc.w, it_fc.h, it_fc.colour, it_fc.background, it_fc.alpha);
_item_trade_frame->setPosition(_display->window->get_x(_item_trade_frame->sprite, it_fc.x),
_display->window->get_y(_item_trade_frame->sprite, it_fc.y));
_item_action_menu_frame = _factory->make_menu_frame("character_summary:item_action_menu_frame");
_item_display_frame = _factory->make_frame("inspect:item_display_frame");

_item_trade_menu.reset();
_item_trade_menu = std::make_unique<Menu>(
_system, _display, _graphics, _game, MenuType::CHARACTER_TRADE, MenuMode::NO_MODE, character_id);
_item_trade_menu->generate((*_display->layout)["inspect:item_trade_menu"]);
_item_trade_menu->setPosition(
_display->get_centre_x(_item_trade_menu->get_width()), (*_display->layout)["inspect:item_trade_menu"].y);
_item_display = std::make_unique<ItemDisplay>(_system, _display, _graphics, _game);
_item_display->setPosition((*_display->layout)["inspect:item_display"].pos());

std::optional<std::vector<MenuEntry>::const_iterator> item_action_selected{_item_action_menu->items.begin()};
std::optional<std::vector<MenuEntry>::const_iterator> item_trade_selected{_item_trade_menu->items.begin()};
Expand Down
11 changes: 4 additions & 7 deletions src/frontend/mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ Sorcery::MainMenu::MainMenu(System *system, Display *display, Graphics *graphics
// Get the Window and Graphics to Display
_window = _display->window->get_window();

// Setup the Factory
_factory = std::make_unique<Factory>(_system, _display, _graphics, _game);

// Create the Main Menu
_menu_stage = MainMenuType::ATTRACT_MODE;
_main_menu = std::make_unique<Menu>(_system, _display, _graphics, _game, MenuType::MAIN);
_main_menu->generate((*_display->layout)["main_menu_attract:main_menu"]);
_main_menu->setPosition(
_display->get_centre_x(_main_menu->get_width()), (*_display->layout)["main_menu_attract:main_menu"].y);
_main_menu = _factory->make_menu("main_menu_attract:main_menu", MenuType::MAIN);

// Setup Custom Components
Component any_key_c{(*_display->layout)["main_menu_attract:press_any_key"]};
Expand All @@ -62,9 +62,6 @@ Sorcery::MainMenu::MainMenu(System *system, Display *display, Graphics *graphics
auto y{any_key_c.y == -1 ? _display->window->centre.y : any_key_c.y};
_press_any_key->setPosition(x, y);

// Setup the Factory
_factory = std::make_unique<Factory>(_system, _display, _graphics, _game);

// Now set up attract mode data
_attract_mode =
std::make_unique<AttractMode>(_graphics, (*_display->layout)["main_menu_attract:attract_creatures"]);
Expand Down
31 changes: 29 additions & 2 deletions src/resources/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "core/graphics.hpp"
#include "core/system.hpp"
#include "gui/dialog.hpp"
#include "gui/frame.hpp"
#include "gui/menu.hpp"
#include "resources/define.hpp"

Expand All @@ -52,11 +53,37 @@ auto Sorcery::Factory::make_dialog(
return dialog;
}

auto Sorcery::Factory::make_menu(const std::string &component, const MenuType type) -> std::unique_ptr<Menu> {
auto Sorcery::Factory::make_menu(const std::string &component, const MenuType type, std::optional<MenuMode> mode,
std::optional<unsigned int> data, const bool reload) -> std::unique_ptr<Menu> {

auto menu = std::make_unique<Menu>(_system, _display, _graphics, _game, type);
auto menu = std::make_unique<Menu>(_system, _display, _graphics, _game, type, mode, data);
if (reload)
menu->reload();
menu->generate((*_display->layout)[component]);
menu->setPosition(_display->get_centre_x(menu->get_width()), (*_display->layout)[component].y);

return menu;
}

auto Sorcery::Factory::make_frame(const std::string &component) -> std::unique_ptr<Frame> {

const Component comp{(*_display->layout)[component]};

auto frame =
std::make_unique<Frame>(_display->ui_texture, comp.w, comp.h, comp.colour, comp.background, comp.alpha);
frame->setPosition(_display->window->get_x(frame->sprite, comp.x), _display->window->get_y(frame->sprite, comp.y));

return frame;
}

auto Sorcery::Factory::make_menu_frame(const std::string &component) -> std::unique_ptr<Frame> {

const Component comp{(*_display->layout)[component]};

auto menu_frame =
std::make_unique<Frame>(_display->ui_texture, comp.w, comp.h, comp.colour, comp.background, comp.alpha);
menu_frame->setPosition(
_display->window->get_x(menu_frame->sprite, comp.x), _display->window->get_y(menu_frame->sprite, comp.y));

return menu_frame;
}

0 comments on commit 596aa71

Please sign in to comment.