Grids are identified using ServerClientIDs instead of name
This commit is contained in:
parent
54942ee2dc
commit
0c0d7d8439
11 changed files with 70 additions and 69 deletions
|
|
@ -232,10 +232,10 @@ void DrawingProgram::tool_options_gui() {
|
|||
}
|
||||
}
|
||||
|
||||
void DrawingProgram::modify_grid(const std::string& gridToModifyName) {
|
||||
void DrawingProgram::modify_grid(ServerClientID gridToModifyID) {
|
||||
switch_to_tool(DrawingProgramToolType::GRIDMODIFY);
|
||||
GridModifyTool* gridModToolPtr = static_cast<GridModifyTool*>(drawTool.get());
|
||||
gridModToolPtr->set_grid_name(gridToModifyName);
|
||||
gridModToolPtr->set_grid_id(gridToModifyID);
|
||||
}
|
||||
|
||||
void DrawingProgram::update() {
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class DrawingProgram {
|
|||
|
||||
void clear_draw_cache();
|
||||
|
||||
void modify_grid(const std::string& gridToModifyName);
|
||||
void modify_grid(ServerClientID gridToModifyID);
|
||||
private:
|
||||
std::optional<std::chrono::steady_clock::time_point> badFrametimeTimePoint;
|
||||
std::optional<std::chrono::steady_clock::time_point> unorderedObjectsExistTimePoint;
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ GridModifyTool::GridModifyTool(DrawingProgram& initDrawP):
|
|||
{
|
||||
}
|
||||
|
||||
void GridModifyTool::set_grid_name(const std::string& gridToModifyName) {
|
||||
gridName = gridToModifyName;
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridName);
|
||||
void GridModifyTool::set_grid_id(ServerClientID newGridID) {
|
||||
gridID = newGridID;
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridID);
|
||||
if(gridFoundIt != drawP.world.gridMan.grids.end()) {
|
||||
WorldGrid& g = gridFoundIt->second;
|
||||
CoordSpaceHelper newCam;
|
||||
|
|
@ -30,10 +30,10 @@ void GridModifyTool::gui_toolbox() {
|
|||
Toolbar& t = drawP.world.main.toolbar;
|
||||
t.gui.push_id("Grid modify tool");
|
||||
t.gui.text_label_centered("Edit Grid");
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridName);
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridID);
|
||||
if(gridFoundIt != drawP.world.gridMan.grids.end()) {
|
||||
WorldGrid& g = gridFoundIt->second;
|
||||
t.gui.text_label("Name: " + gridName);
|
||||
t.gui.input_text_field("grid name", "Name", &g.name);
|
||||
t.gui.checkbox_field("Visible", "Visible", &g.visible);
|
||||
size_t typeSelected = static_cast<size_t>(g.gridType);
|
||||
std::vector<std::string> listOfGridTypes = {
|
||||
|
|
@ -47,10 +47,10 @@ void GridModifyTool::gui_toolbox() {
|
|||
t.gui.dropdown_select("filepicker select type", &typeSelected, listOfGridTypes, 200.0f);
|
||||
});
|
||||
g.gridType = static_cast<WorldGrid::GridType>(typeSelected);
|
||||
uint32_t sDiv = g.get_subdivisions();
|
||||
uint32_t sDiv = g.subdivisions;
|
||||
t.gui.input_scalar_field<uint32_t>("Subdivisions", "Subdivisions", &sDiv, 1, 10);
|
||||
g.set_subdivisions(sDiv);
|
||||
bool divOut = g.get_remove_divisions_outwards();
|
||||
bool divOut = g.removeDivisionsOutwards;
|
||||
t.gui.checkbox_field("Subdivide outwards", "Subdivide outwards", &divOut);
|
||||
g.set_remove_divisions_outwards(divOut);
|
||||
t.gui.left_to_right_line_layout([&]() {
|
||||
|
|
@ -76,7 +76,7 @@ void GridModifyTool::gui_toolbox() {
|
|||
}
|
||||
|
||||
void GridModifyTool::tool_update() {
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridName);
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridID);
|
||||
if(gridFoundIt != drawP.world.gridMan.grids.end()) {
|
||||
WorldGrid& g = gridFoundIt->second;
|
||||
switch(selectionMode) {
|
||||
|
|
@ -138,7 +138,7 @@ bool GridModifyTool::prevent_undo_or_redo() {
|
|||
}
|
||||
|
||||
void GridModifyTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridName);
|
||||
auto gridFoundIt = drawP.world.gridMan.grids.find(gridID);
|
||||
if(gridFoundIt != drawP.world.gridMan.grids.end()) {
|
||||
WorldGrid& g = gridFoundIt->second;
|
||||
if(g.bounds.has_value()) {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ class DrawingProgram;
|
|||
class GridModifyTool : public DrawingProgramToolBase {
|
||||
public:
|
||||
GridModifyTool(DrawingProgram& initDrawP);
|
||||
void set_grid_name(const std::string& gridToModifyName);
|
||||
void set_grid_id(ServerClientID newGridID);
|
||||
virtual DrawingProgramToolType get_type() override;
|
||||
virtual void gui_toolbox() override;
|
||||
virtual void tool_update() override;
|
||||
|
|
@ -14,6 +14,6 @@ class GridModifyTool : public DrawingProgramToolBase {
|
|||
virtual bool prevent_undo_or_redo() override;
|
||||
virtual void switch_tool(DrawingProgramToolType newTool) override;
|
||||
private:
|
||||
std::string gridName;
|
||||
ServerClientID gridID;
|
||||
unsigned selectionMode = 0;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#include "GridManager.hpp"
|
||||
#include "SharedTypes.hpp"
|
||||
#include "World.hpp"
|
||||
#include "MainProgram.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
GridManager::GridManager(World& w):
|
||||
world(w) {}
|
||||
|
|
@ -8,30 +10,37 @@ GridManager::GridManager(World& w):
|
|||
void GridManager::init_client_callbacks() {
|
||||
}
|
||||
|
||||
void GridManager::add_grid(const std::string& name) {
|
||||
ServerClientID GridManager::add_default_grid(const std::string& newName) {
|
||||
ServerClientID newID = world.get_new_id();
|
||||
WorldGrid g;
|
||||
g.name = newName;
|
||||
g.color = color_mul_alpha(convert_vec4<Vector4f>(world.canvasTheme.toolFrontColor), 0.6f);
|
||||
g.set_subdivisions(7);
|
||||
g.size = world.drawData.cam.c.inverseScale * WorldScalar(WorldGrid::GRID_UNIT_PIXEL_SIZE);
|
||||
g.offset = world.drawData.cam.c.pos + world.drawData.cam.c.dir_from_space(world.main.window.size.cast<float>() * 0.5f);
|
||||
grids[name] = g;
|
||||
grids[newID] = g;
|
||||
changed = true;
|
||||
return newID;
|
||||
}
|
||||
|
||||
void GridManager::remove_grid(ServerClientID idToRemove) {
|
||||
grids.erase(idToRemove);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
void GridManager::remove_grid(const std::string& name) {
|
||||
grids.erase(name);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& GridManager::sorted_names() {
|
||||
const std::vector<ServerClientID>& GridManager::sorted_grid_ids() {
|
||||
if(changed) {
|
||||
sortedNames.clear();
|
||||
sortedGridIDs.clear();
|
||||
for(auto& [k, v] : grids)
|
||||
sortedNames.emplace_back(k);
|
||||
std::sort(sortedNames.begin(), sortedNames.end());
|
||||
sortedGridIDs.emplace_back(k);
|
||||
std::sort(sortedGridIDs.begin(), sortedGridIDs.end(), [&](ServerClientID a, ServerClientID b) {
|
||||
std::string nameA = grids[a].get_display_name();
|
||||
std::string nameB = grids[b].get_display_name();
|
||||
bool namesEqual = nameA == nameB;
|
||||
return (!namesEqual && std::lexicographical_compare(nameA.begin(), nameA.end(), nameB.begin(), nameB.end())) || (namesEqual && a < b);
|
||||
});
|
||||
changed = false;
|
||||
}
|
||||
return sortedNames;
|
||||
return sortedGridIDs;
|
||||
}
|
||||
|
||||
void GridManager::draw(SkCanvas* canvas, const DrawData& drawData) {
|
||||
|
|
|
|||
|
|
@ -7,14 +7,18 @@ class GridManager {
|
|||
public:
|
||||
GridManager(World& w);
|
||||
void init_client_callbacks();
|
||||
void add_grid(const std::string& name);
|
||||
void remove_grid(const std::string& name);
|
||||
void draw(SkCanvas* canvas, const DrawData& drawData);
|
||||
const std::vector<std::string>& sorted_names();
|
||||
|
||||
std::unordered_map<std::string, WorldGrid> grids;
|
||||
ServerClientID add_default_grid(const std::string& newName);
|
||||
|
||||
void remove_grid(ServerClientID idToRemove);
|
||||
void draw(SkCanvas* canvas, const DrawData& drawData);
|
||||
ClientPortionID get_max_id(ServerPortionID serverID) const;
|
||||
|
||||
const std::vector<ServerClientID>& sorted_grid_ids();
|
||||
|
||||
std::unordered_map<ServerClientID, WorldGrid> grids;
|
||||
World& world;
|
||||
private:
|
||||
bool changed = true;
|
||||
std::vector<std::string> sortedNames;
|
||||
std::vector<ServerClientID> sortedGridIDs;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -558,13 +558,13 @@ void Toolbar::grid_menu(bool justOpened) {
|
|||
gui.obstructing_window();
|
||||
gui.text_label_centered("Grids");
|
||||
float entryHeight = 25.0f;
|
||||
if(main.world->gridMan.sorted_names().empty())
|
||||
if(main.world->gridMan.sorted_grid_ids().empty())
|
||||
gui.text_label_centered("No grids yet...");
|
||||
std::string toDelete;
|
||||
gui.scroll_bar_many_entries_area("grid menu entries", entryHeight, main.world->gridMan.sorted_names().size(), [&](size_t i, bool isListHovered) {
|
||||
const std::string& gridName = main.world->gridMan.sorted_names()[i];
|
||||
WorldGrid& grid = main.world->gridMan.grids[gridName];
|
||||
bool selectedEntry = gridName == gridMenu.newName;
|
||||
ServerClientID toDelete{0, 0};
|
||||
gui.scroll_bar_many_entries_area("grid menu entries", entryHeight, main.world->gridMan.sorted_grid_ids().size(), [&](size_t i, bool isListHovered) {
|
||||
ServerClientID gridID = main.world->gridMan.sorted_grid_ids()[i];
|
||||
WorldGrid& grid = main.world->gridMan.grids[gridID];
|
||||
bool selectedEntry = gridID == gridMenu.gridSelected;
|
||||
CLAY({
|
||||
.layout = {
|
||||
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(entryHeight)},
|
||||
|
|
@ -574,7 +574,7 @@ void Toolbar::grid_menu(bool justOpened) {
|
|||
},
|
||||
.backgroundColor = selectedEntry ? convert_vec4<Clay_Color>(io->theme->backColor1) : convert_vec4<Clay_Color>(io->theme->backColor2)
|
||||
}) {
|
||||
gui.text_label(gridName);
|
||||
gui.text_label(grid.get_display_name());
|
||||
bool miniButtonClicked = false;
|
||||
CLAY({
|
||||
.layout = {
|
||||
|
|
@ -590,34 +590,30 @@ void Toolbar::grid_menu(bool justOpened) {
|
|||
}
|
||||
if(gui.svg_icon_button("edit pencil", "data/icons/pencil.svg", false, entryHeight, false)) {
|
||||
miniButtonClicked = true;
|
||||
main.world->drawProg.modify_grid(gridName);
|
||||
main.world->drawProg.modify_grid(gridID);
|
||||
stop_displaying_grid_menu();
|
||||
}
|
||||
if(gui.svg_icon_button("delete trash", "data/icons/trash.svg", false, entryHeight, false)) {
|
||||
miniButtonClicked = true;
|
||||
toDelete = gridName;
|
||||
toDelete = gridID;
|
||||
}
|
||||
}
|
||||
if(Clay_Hovered() && io->mouse.leftClick && isListHovered && !miniButtonClicked) {
|
||||
gridMenu.newName = gridName;
|
||||
gridMenu.gridSelected = gridID;
|
||||
if(io->mouse.leftClick >= 2) {
|
||||
main.world->drawProg.modify_grid(gridName);
|
||||
main.world->drawProg.modify_grid(gridID);
|
||||
stop_displaying_grid_menu();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if(!toDelete.empty())
|
||||
if(toDelete != ServerClientID{0, 0})
|
||||
main.world->gridMan.remove_grid(toDelete);
|
||||
bool gridExists = std::find(main.world->gridMan.sorted_names().begin(), main.world->gridMan.sorted_names().end(), gridMenu.newName) != main.world->gridMan.sorted_names().end();
|
||||
gui.left_to_right_line_layout([&]() {
|
||||
gui.input_text("grid text input", &gridMenu.newName);
|
||||
if(!gridExists) {
|
||||
if(gui.svg_icon_button("grid add button", "data/icons/plus.svg", false, 25.0f) && !gridMenu.newName.empty()) {
|
||||
main.world->gridMan.add_grid(gridMenu.newName);
|
||||
main.world->drawProg.modify_grid(gridMenu.newName);
|
||||
stop_displaying_grid_menu();
|
||||
}
|
||||
if(gui.svg_icon_button("grid add button", "data/icons/plus.svg", false, 25.0f)) {
|
||||
main.world->drawProg.modify_grid(main.world->gridMan.add_default_grid(gridMenu.newName));
|
||||
stop_displaying_grid_menu();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -172,6 +172,7 @@ class Toolbar {
|
|||
struct GridMenu {
|
||||
bool popupOpen = false;
|
||||
std::string newName;
|
||||
ServerClientID gridSelected;
|
||||
} gridMenu;
|
||||
|
||||
bool useNativeFilePicker = true;
|
||||
|
|
|
|||
|
|
@ -63,12 +63,6 @@ World::World(MainProgram& initMain, OpenWorldInfo& worldInfo):
|
|||
bMan.init_client_callbacks();
|
||||
init_client_callbacks();
|
||||
con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_INITIAL_DATA, displayName, false);
|
||||
|
||||
gridMan.add_grid("b");
|
||||
gridMan.grids["b"].size = WorldScalar(100000);
|
||||
gridMan.grids["b"].set_remove_divisions_outwards(true);
|
||||
gridMan.grids["b"].gridType = WorldGrid::GridType::CIRCLE_POINTS;
|
||||
gridMan.grids["b"].set_subdivisions(10);
|
||||
}
|
||||
|
||||
void World::init_client_callbacks() {
|
||||
|
|
|
|||
|
|
@ -195,18 +195,14 @@ void WorldGrid::set_remove_divisions_outwards(bool v) {
|
|||
subdivisions = std::max<uint32_t>(subdivisions, 2);
|
||||
}
|
||||
|
||||
bool WorldGrid::get_remove_divisions_outwards() {
|
||||
return removeDivisionsOutwards;
|
||||
}
|
||||
|
||||
void WorldGrid::set_subdivisions(uint32_t v) {
|
||||
subdivisions = v;
|
||||
if(removeDivisionsOutwards)
|
||||
subdivisions = std::max<uint32_t>(subdivisions, 2);
|
||||
}
|
||||
|
||||
uint32_t WorldGrid::get_subdivisions() {
|
||||
return subdivisions;
|
||||
std::string WorldGrid::get_display_name() {
|
||||
return name.empty() ? "New Grid" : name;
|
||||
}
|
||||
|
||||
void WorldGrid::draw(GridManager& gMan, SkCanvas* canvas, const DrawData& drawData) {
|
||||
|
|
|
|||
|
|
@ -8,28 +8,29 @@ class GridManager;
|
|||
class WorldGrid {
|
||||
public:
|
||||
void draw(GridManager& gMan, SkCanvas* canvas, const DrawData& drawData);
|
||||
|
||||
enum class GridType : unsigned {
|
||||
CIRCLE_POINTS = 0,
|
||||
SQUARE_POINTS,
|
||||
SQUARE_LINES,
|
||||
RULED
|
||||
} gridType = GridType::CIRCLE_POINTS;
|
||||
std::string name;
|
||||
WorldScalar size{50000000};
|
||||
bool visible = true;
|
||||
WorldVec offset{0, 0};
|
||||
std::optional<SCollision::AABB<WorldScalar>> bounds;
|
||||
Vector4f color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
bool removeDivisionsOutwards = false;
|
||||
uint32_t subdivisions = 1;
|
||||
|
||||
std::string get_display_name();
|
||||
|
||||
void set_remove_divisions_outwards(bool v);
|
||||
bool get_remove_divisions_outwards();
|
||||
|
||||
void set_subdivisions(uint32_t v);
|
||||
uint32_t get_subdivisions();
|
||||
|
||||
static unsigned GRID_UNIT_PIXEL_SIZE;
|
||||
private:
|
||||
bool removeDivisionsOutwards = false;
|
||||
uint32_t subdivisions = 1;
|
||||
|
||||
struct ShaderData {
|
||||
SkColor4f gridColor;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue